From cc66564d5a0ed6bfb143fd3c9b9d249948e7a0dd Mon Sep 17 00:00:00 2001 From: skosito Date: Fri, 10 May 2024 20:14:41 +0200 Subject: [PATCH] Fix PR comments and build --- ...ity.go => system_tx_priority_decorator.go} | 0 app/ante/system_tx_priority_decorator_test.go | 45 +++++++++++++++++++ app/custom_proposal_handler.go | 6 ++- app/mempool/priority_nonce_mempool.go | 6 +-- app/mempool/senders_with_nonce.go | 10 ++++- changelog.md | 1 + 6 files changed, 62 insertions(+), 6 deletions(-) rename app/ante/{priority.go => system_tx_priority_decorator.go} (100%) create mode 100644 app/ante/system_tx_priority_decorator_test.go diff --git a/app/ante/priority.go b/app/ante/system_tx_priority_decorator.go similarity index 100% rename from app/ante/priority.go rename to app/ante/system_tx_priority_decorator.go diff --git a/app/ante/system_tx_priority_decorator_test.go b/app/ante/system_tx_priority_decorator_test.go new file mode 100644 index 0000000000..f777a98ba1 --- /dev/null +++ b/app/ante/system_tx_priority_decorator_test.go @@ -0,0 +1,45 @@ +package ante_test + +import ( + "math" + "math/rand" + "testing" + "time" + + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + "github.com/zeta-chain/zetacore/app" + "github.com/zeta-chain/zetacore/app/ante" + "github.com/zeta-chain/zetacore/testutil/sample" +) + +func TestSystemTxPriorityDecorator_AnteHandle(t *testing.T) { + txConfig := app.MakeEncodingConfig().TxConfig + + testPrivKey, _ := sample.PrivKeyAddressPair() + + decorator := ante.NewSystemPriorityDecorator() + mmd := MockAnteHandler{} + // set priority to 10 before ante handler + ctx := sdk.Context{}.WithIsCheckTx(true).WithPriority(10) + + tx, err := simtestutil.GenSignedMockTx( + rand.New(rand.NewSource(time.Now().UnixNano())), + txConfig, + []sdk.Msg{}, + sdk.NewCoins(), + simtestutil.DefaultGenTxGas, + "testing-chain-id", + []uint64{0}, + []uint64{0}, + testPrivKey, + ) + require.NoError(t, err) + ctx, err = decorator.AnteHandle(ctx, tx, false, mmd.AnteHandle) + require.NoError(t, err) + + // check that priority is set to max int64 + priorityAfter := ctx.Priority() + require.Equal(t, math.MaxInt64, int(priorityAfter)) +} diff --git a/app/custom_proposal_handler.go b/app/custom_proposal_handler.go index 0639e89125..a35b6d5284 100644 --- a/app/custom_proposal_handler.go +++ b/app/custom_proposal_handler.go @@ -2,7 +2,7 @@ package app // This proposal handler is taken from https://github.com/cosmos/cosmos-sdk/blob/v0.47.10/baseapp/abci_utils.go // Only difference is extraction of senders and nonce from tx. In latest version of cosmos, there is a way to provide adapter for this, but in 0.47.10 this is the only way. -// TODO: remove this once we upgrade cosmos +// TODO: remove this once cosmos is upgraded: https://github.com/zeta-chain/node/issues/2156 import ( "fmt" @@ -75,6 +75,7 @@ func (h *CustomProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHand return func(ctx sdk.Context, req abci.RequestPrepareProposal) abci.ResponsePrepareProposal { var maxBlockGas uint64 if b := ctx.ConsensusParams().Block; b != nil { + // #nosec G701 range checked, cosmos-sdk forked code maxBlockGas = uint64(b.MaxGas) } @@ -90,6 +91,7 @@ func (h *CustomProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHand // XXX: We pass nil as the memTx because we have no way of decoding the // txBz. We'd need to break (update) the ProposalTxVerifier interface. // As a result, we CANNOT account for block max gas. + // #nosec G701 range checked, cosmos-sdk forked code stop := h.txSelector.SelectTxForProposal(uint64(req.MaxTxBytes), maxBlockGas, nil, txBz) if stop { break @@ -149,6 +151,7 @@ func (h *CustomProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHand panic(err) } } else { + // #nosec G701 range checked, cosmos-sdk forked code stop := h.txSelector.SelectTxForProposal(uint64(req.MaxTxBytes), maxBlockGas, memTx, txBz) if stop { break @@ -218,6 +221,7 @@ func (h *CustomProposalHandler) ProcessProposalHandler() sdk.ProcessProposalHand totalTxGas += gasTx.GetGas() } + // #nosec G701 range checked, cosmos-sdk forked code if totalTxGas > uint64(maxBlockGas) { return abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT} } diff --git a/app/mempool/priority_nonce_mempool.go b/app/mempool/priority_nonce_mempool.go index 5d58c951d2..9b1a53091b 100644 --- a/app/mempool/priority_nonce_mempool.go +++ b/app/mempool/priority_nonce_mempool.go @@ -1,6 +1,6 @@ // This proposal handler is taken from https://github.com/cosmos/cosmos-sdk/blob/v0.47.10/types/mempool/priority_nonce.go // Only difference is extraction of senders and nonce from tx. In latest version of cosmos, there is a way to provide adapter for this, but in 0.47.10 this is the only way. -// TODO: remove this once we upgrade cosmos +// TODO: remove this once cosmos is upgraded: https://github.com/zeta-chain/node/issues/2156 package mempool @@ -422,7 +422,7 @@ func IsEmpty(mempool mempool.Mempool) error { return fmt.Errorf("priorityIndex not empty") } - var countKeys []int64 + var countKeys = make([]int64, 0, len(mp.priorityCounts)) for k := range mp.priorityCounts { countKeys = append(countKeys, k) } @@ -433,7 +433,7 @@ func IsEmpty(mempool mempool.Mempool) error { } } - var senderKeys []string + var senderKeys = make([]string, 0, len(mp.senderIndices)) for k := range mp.senderIndices { senderKeys = append(senderKeys, k) } diff --git a/app/mempool/senders_with_nonce.go b/app/mempool/senders_with_nonce.go index 798bcded74..1e3e74e221 100644 --- a/app/mempool/senders_with_nonce.go +++ b/app/mempool/senders_with_nonce.go @@ -1,3 +1,5 @@ +// TODO: use with signer extractor once available https://github.com/zeta-chain/node/issues/2156 + package mempool import ( @@ -9,13 +11,16 @@ import ( evmtypes "github.com/evmos/ethermint/x/evm/types" ) +// GetSendersWithNonce is used to extract sender and nonce information txs +// if tx is ethermint, it is extracted using from and nonce field +// if it's cosmos tx, default cosmos way using signatures is used func GetSendersWithNonce(tx sdk.Tx) ([]SenderWithNonce, error) { + const extensionOptionsEthereumTxTypeUrl = "/ethermint.evm.v1.ExtensionOptionsEthereumTx" if txWithExtensions, ok := tx.(authante.HasExtensionOptionsTx); ok { opts := txWithExtensions.GetExtensionOptions() - if len(opts) > 0 && opts[0].GetTypeUrl() == "/ethermint.evm.v1.ExtensionOptionsEthereumTx" { + if len(opts) > 0 && opts[0].GetTypeUrl() == extensionOptionsEthereumTxTypeUrl { for _, msg := range tx.GetMsgs() { if ethMsg, ok := msg.(*evmtypes.MsgEthereumTx); ok { - return []SenderWithNonce{ { Sender: ethMsg.GetFrom().String(), @@ -35,6 +40,7 @@ type SenderWithNonce struct { Nonce uint64 } +// getSendersWithNonceDefault gets senders and nonces from signatures in cosmos txs func getSendersWithNonceDefault(tx sdk.Tx) ([]SenderWithNonce, error) { sendersWithNonce := []SenderWithNonce{} diff --git a/changelog.md b/changelog.md index 8eedfe2ac3..cc192390b3 100644 --- a/changelog.md +++ b/changelog.md @@ -13,6 +13,7 @@ * [2100](https://github.com/zeta-chain/node/pull/2100) - cosmos v0.47 upgrade * [2145](https://github.com/zeta-chain/node/pull/2145) - add `ibc` and `ibc-transfer` modules * [2135](https://github.com/zeta-chain/node/pull/2135) - add develop build version logic +* [2152](https://github.com/zeta-chain/node/pull/2152) - custom priority nonce mempool ### Refactor