Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(post): burn cosmos transaction fees #2128

Merged
merged 21 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (bank-precompile) [#2095](https://github.com/evmos/evmos/pull/2095) Add `bank` precompile.
- (incentives) [#2070](https://github.com/evmos/evmos/pull/2070) Remove `x/incentives` module and burn incentives pool balance.
- (evm) [#2084](https://github.com/evmos/evmos/pull/2084) Remove `x/claims` params and migrate the `EVMChannels` param to the `x/evm` module params.
- (post) [#2128](https://github.com/evmos/evmos/pull/2128) Add `BurnDecorator` to `PostHandler` to burn cosmos transaction fees.

### API Breaking

Expand Down
14 changes: 8 additions & 6 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import (
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/posthandler"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -125,6 +124,7 @@ import (

"github.com/evmos/evmos/v16/app/ante"
ethante "github.com/evmos/evmos/v16/app/ante/evm"
"github.com/evmos/evmos/v16/app/post"
v10 "github.com/evmos/evmos/v16/app/upgrades/v10"
v11 "github.com/evmos/evmos/v16/app/upgrades/v11"
v12 "github.com/evmos/evmos/v16/app/upgrades/v12"
Expand Down Expand Up @@ -873,14 +873,16 @@ func (app *Evmos) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64)
}

func (app *Evmos) setPostHandler() {
postHandler, err := posthandler.NewPostHandler(
posthandler.HandlerOptions{},
)
if err != nil {
options := post.HandlerOptions{
FeeCollectorName: authtypes.FeeCollectorName,
BankKeeper: app.BankKeeper,
}

if err := options.Validate(); err != nil {
panic(err)
}

app.SetPostHandler(postHandler)
app.SetPostHandler(post.NewPostHandler(options))
}

// BeginBlocker runs the Tendermint ABCI BeginBlock logic. It executes state changes at the beginning
Expand Down
76 changes: 76 additions & 0 deletions app/post/burn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)

package post

import (
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
evmtypes "github.com/evmos/evmos/v16/x/evm/types"
)

var _ sdk.PostDecorator = &BurnDecorator{}

// BurnDecorator is the decorator that burns all the transaction fees from Cosmos transactions.
type BurnDecorator struct {
feeCollectorName string
bankKeeper bankkeeper.Keeper
}

// NewBurnDecorator creates a new instance of the BurnDecorator.
func NewBurnDecorator(feeCollector string, bankKeeper bankkeeper.Keeper) sdk.PostDecorator {
return &BurnDecorator{
feeCollectorName: feeCollector,
bankKeeper: bankKeeper,
}
}

// PostHandle burns all the transaction fees from Cosmos transactions. If an Ethereum transaction is present, this logic
// is skipped.
func (bd BurnDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate, success bool, next sdk.PostHandler) (newCtx sdk.Context, err error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, errorsmod.Wrapf(errortypes.ErrInvalidType, "invalid transaction type %T, expected sdk.FeeTx", tx)
}

// skip logic if there is an Ethereum transaction
for _, msg := range tx.GetMsgs() {
if _, ok := msg.(*evmtypes.MsgEthereumTx); ok {
return next(ctx, tx, simulate, success)
}
}

fees := feeTx.GetFee()

// safety check: ensure the fees are not empty and with positive amounts
// before burning
if len(fees) == 0 || !fees.IsAllPositive() {
return next(ctx, tx, simulate, success)
}

// burn min(balance, fee)
var burnedCoins sdk.Coins
for _, fee := range fees {
balance := bd.bankKeeper.GetBalance(ctx, authtypes.NewModuleAddress(bd.feeCollectorName), fee.Denom)
if !balance.IsPositive() {
continue

Check warning on line 61 in app/post/burn.go

View check run for this annotation

Codecov / codecov/patch

app/post/burn.go#L61

Added line #L61 was not covered by tests
}

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

burnedCoins = append(burnedCoins, 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 {
return ctx, err
}

return next(ctx, tx, simulate, success)
}
170 changes: 170 additions & 0 deletions app/post/burn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)

package post_test

import (
sdkmath "cosmossdk.io/math"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/evmos/evmos/v16/app/post"

// "github.com/evmos/evmos/v16/testutil/integration/evmos/factory"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (s *PostTestSuite) TestPostHandle() {
testCases := []struct {
name string
tx func() sdk.Tx
expPass bool
errContains string
postChecks func()
}{
{
name: "pass - noop with Ethereum message",
tx: func() sdk.Tx {
return s.BuildEthTx()
},
expPass: true,
postChecks: func() {},
},
{
name: "pass - burn fees of a single token with empty end balance",
tx: func() sdk.Tx {
feeAmount := sdk.Coins{sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "btc"}}
amount := feeAmount
s.MintCoinsForFeeCollector(amount)

return s.BuildCosmosTxWithNSendMsg(1, feeAmount)
},
expPass: true,
postChecks: func() {
expected := sdk.Coins{}
balance := s.GetFeeCollectorBalance()
s.Require().Equal(expected, balance)
},
},
{
name: "pass - burn fees of a single token with non-empty end balance",
tx: func() sdk.Tx {
feeAmount := sdk.Coins{sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "evmos"}}
amount := sdk.Coins{sdk.Coin{Amount: sdkmath.NewInt(20), Denom: "evmos"}}
s.MintCoinsForFeeCollector(amount)

return s.BuildCosmosTxWithNSendMsg(1, feeAmount)
},
expPass: true,
postChecks: func() {
expected := sdk.Coins{sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "evmos"}}
balance := s.GetFeeCollectorBalance()
s.Require().Equal(expected, balance)
},
},
{
name: "pass - burn fees of multiple tokens with empty end balance",
tx: func() sdk.Tx {
feeAmount := sdk.Coins{
sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "eth"},
sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "evmos"},
}
amount := feeAmount
s.MintCoinsForFeeCollector(amount)

return s.BuildCosmosTxWithNSendMsg(1, feeAmount)
},
expPass: true,
postChecks: func() {
balance := s.GetFeeCollectorBalance()
s.Require().Equal(sdk.Coins{}, balance)
},
},
{ //nolint:dupl
name: "pass - burn fees of multiple tokens with non-empty end balance",
tx: func() sdk.Tx {
feeAmount := sdk.Coins{
sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "btc"},
sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "evmos"},
}
amount := sdk.Coins{
sdk.Coin{Amount: sdkmath.NewInt(20), Denom: "btc"},
sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "evmos"},
sdk.Coin{Amount: sdkmath.NewInt(3), Denom: "osmo"},
}
s.MintCoinsForFeeCollector(amount)

return s.BuildCosmosTxWithNSendMsg(1, feeAmount)
},
expPass: true,
postChecks: func() {
expected := sdk.Coins{
sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "btc"},
sdk.Coin{Amount: sdkmath.NewInt(3), Denom: "osmo"},
}
balance := s.GetFeeCollectorBalance()
s.Require().Equal(expected, balance)
},
},
{ //nolint:dupl
name: "pass - burn fees of multiple tokens, non-empty end balance, and multiple messages",
tx: func() sdk.Tx {
feeAmount := sdk.Coins{
sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "btc"},
sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "evmos"},
}
amount := sdk.Coins{
sdk.Coin{Amount: sdkmath.NewInt(20), Denom: "btc"},
sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "evmos"},
sdk.Coin{Amount: sdkmath.NewInt(3), Denom: "osmo"},
}
s.MintCoinsForFeeCollector(amount)

return s.BuildCosmosTxWithNSendMsg(100, feeAmount)
},
expPass: true,
postChecks: func() {
expected := sdk.Coins{
sdk.Coin{Amount: sdkmath.NewInt(10), Denom: "btc"},
sdk.Coin{Amount: sdkmath.NewInt(3), Denom: "osmo"},
}
balance := s.GetFeeCollectorBalance()
s.Require().Equal(expected, balance)
},
},
}

for _, tc := range testCases {
// Be sure to have a fresh new network before each test. It is not required for following
// test but it is still a good practice.
s.SetupTest()
s.Run(tc.name, func() {
// start each test with a fresh new block.
err := s.unitNetwork.NextBlock()
s.Require().NoError(err)

burnDecorator := post.NewBurnDecorator(
authtypes.FeeCollectorName,
s.unitNetwork.App.BankKeeper,
)

// In the execution of the PostHandle method, simulate, success, and next have been
// hard-coded because they are not influencing the behavior of the BurnDecorator.
terminator := sdk.ChainPostDecorators(sdk.Terminator{})
_, err = burnDecorator.PostHandle(
s.unitNetwork.GetContext(),
tc.tx(),
false,
false,
terminator,
)

if tc.expPass {
s.Require().NoError(err)
} else {
s.Require().Error(err, "expected error during HandlerOptions validation")
s.Require().Contains(err.Error(), tc.errContains, "expected a different error")
}

tc.postChecks()
})
}
}
38 changes: 38 additions & 0 deletions app/post/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)

package post

import (
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
)

// HandlerOptions are the options required for constructing a PostHandler.
type HandlerOptions struct {
FeeCollectorName string
BankKeeper bankkeeper.Keeper
}

func (h HandlerOptions) Validate() error {
if h.FeeCollectorName == "" {
return errors.New("fee collector name cannot be empty")
}

if h.BankKeeper == nil {
return errors.New("bank keeper cannot be nil")
}

return nil
}

// NewPostHandler returns a new PostHandler decorators chain.
func NewPostHandler(ho HandlerOptions) sdk.PostHandler {
postDecorators := []sdk.PostDecorator{
NewBurnDecorator(ho.FeeCollectorName, ho.BankKeeper),
}

return sdk.ChainPostDecorators(postDecorators...)
}