Skip to content

Commit

Permalink
chore: add burnt coins metrics (#2246)
Browse files Browse the repository at this point in the history
* add burnt coins metrics

* add changelog entry

* add test case

* small fix

* small refactor
  • Loading branch information
GAtom22 committed Jan 10, 2024
1 parent 7994ffb commit ea75cb9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (outposts) [#2223](https://github.com/evmos/evmos/pull/2223) Update Outposts struct documentation and `ValidateBasic`.
- (p256-precompile) [#2228](https://github.com/evmos/evmos/pull/2228) Adjust p256 precompile address from `0x0b` to `0x100`.
- (staking-precompile) [#2234](https://github.com/evmos/evmos/pull/2234) Fix wrong error messages in `NewMsgCreateValidator`.
- (metrics) [#2246](https://github.com/evmos/evmos/pull/2246) Add burnt cosmos transactions fees metric.

### Bug Fixes

Expand Down
24 changes: 21 additions & 3 deletions app/post/burn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -54,7 +56,7 @@ func (bd BurnDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate, success
}

// burn min(balance, fee)
var burnedCoins sdk.Coins
var burntCoins sdk.Coins
for _, fee := range fees {
balance := bd.bankKeeper.GetBalance(ctx, authtypes.NewModuleAddress(bd.feeCollectorName), fee.Denom)
if !balance.IsPositive() {
Expand All @@ -63,14 +65,30 @@ func (bd BurnDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate, success

amount := sdkmath.MinInt(fee.Amount, balance.Amount)

burnedCoins = append(burnedCoins, sdk.Coin{Denom: fee.Denom, Amount: amount})
burntCoins = append(burntCoins, sdk.Coin{Denom: fee.Denom, Amount: amount})
}

// NOTE: since all Cosmos tx fees are pooled by the fee collector module account,
// we burn them directly from it
if err := bd.bankKeeper.BurnCoins(ctx, bd.feeCollectorName, burnedCoins); err != nil {
if err := bd.bankKeeper.BurnCoins(ctx, bd.feeCollectorName, burntCoins); err != nil {
return ctx, err
}

defer func() {
for _, c := range burntCoins {
// if fee amount is higher than uint64, skip the counter
if !c.Amount.IsUint64() {
continue
}
telemetry.IncrCounterWithLabels(
[]string{"burnt", "tx", "fee", "amount"},
float32(c.Amount.Uint64()),
[]metrics.Label{
telemetry.NewLabel("denom", c.Denom),
},
)
}
}()

return next(ctx, tx, simulate, success)
}
18 changes: 18 additions & 0 deletions app/post/burn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ func (s *PostTestSuite) TestPostHandle() {
s.Require().Equal(expected, balance)
},
},
{
name: "pass - fees exceeds MaxUint64 (~18 EVMOS). Should not panic",
tx: func() sdk.Tx {
amt, ok := sdkmath.NewIntFromString("10000000000000000000000000000000000")
s.Require().True(ok)
feeAmount := sdk.Coins{sdk.Coin{Amount: amt, Denom: "evmos"}}
amount := sdk.Coins{sdk.Coin{Amount: amt, Denom: "evmos"}}
s.MintCoinsForFeeCollector(amount)

return s.BuildCosmosTxWithNSendMsg(1, feeAmount)
},
expPass: true,
postChecks: func() {
expected := sdk.Coins{}
balance := s.GetFeeCollectorBalance()
s.Require().Equal(expected, balance)
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit ea75cb9

Please sign in to comment.