Skip to content

Commit

Permalink
Problem: max_gas_wanted not working in sdk 0.50 mempool
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Jun 25, 2024
1 parent 9570416 commit 8829150
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 25 deletions.
22 changes: 13 additions & 9 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,21 @@ func New(

// NOTE we use custom transaction decoder that supports the sdk.Tx interface instead of sdk.StdTx
// Setup Mempool and Proposal Handlers
baseAppOptions = append(baseAppOptions, func(app *baseapp.BaseApp) {
mempool := mempool.NewPriorityMempool(mempool.PriorityNonceMempoolConfig[int64]{
baseAppOption := func(bApp *baseapp.BaseApp) {
mempool := NewCustomMempool(mempool.PriorityNonceMempoolConfig[int64]{
TxPriority: mempool.NewDefaultTxPriority(),
SignerExtractor: evmapp.NewEthSignerExtractionAdapter(mempool.NewDefaultSignerExtractionAdapter()),
})
handler := baseapp.NewDefaultProposalHandler(mempool, app)

app.SetMempool(mempool)
app.SetPrepareProposal(handler.PrepareProposalHandler())
app.SetProcessProposal(handler.ProcessProposalHandler())
})
}, bApp)
bApp.SetMempool(mempool)
handler := baseapp.NewDefaultProposalHandler(mempool, bApp)
handler.SetTxSelector(NewExtTxSelector(
baseapp.NewDefaultTxSelector(),
))
handler.SetTxVerifier(NewExtProposalTxVerifier(bApp))
bApp.SetPrepareProposal(handler.PrepareProposalHandler())
bApp.SetProcessProposal(handler.ProcessProposalHandler())
}
baseAppOptions = append(baseAppOptions, baseAppOption)

homePath := cast.ToString(appOpts.Get(flags.FlagHome))
baseAppOptions = memiavlstore.SetupMemIAVL(logger, homePath, appOpts, false, false, baseAppOptions)
Expand Down
52 changes: 52 additions & 0 deletions app/mempool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package app

import (
"context"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/mempool"
)

type customMempoolIterator struct {
txs []sdk.Tx
}

func (i *customMempoolIterator) Next() mempool.Iterator {
if len(i.txs) <= 1 {
return nil
}
return &customMempoolIterator{
txs: i.txs[1:],
}
}

func (i *customMempoolIterator) Tx() sdk.Tx {
return i.txs[0]
}

type CustomMempool[C comparable] struct {
*mempool.PriorityNonceMempool[C]
txVerifier baseapp.ProposalTxVerifier
}

func NewCustomMempool[C comparable](cfg mempool.PriorityNonceMempoolConfig[C], txVerifier baseapp.ProposalTxVerifier) *CustomMempool[C] {
return &CustomMempool[C]{
PriorityNonceMempool: mempool.NewPriorityMempool(cfg),
txVerifier: txVerifier,
}
}

func (mp *CustomMempool[C]) Select(_ context.Context, txs [][]byte) mempool.Iterator {
sdkTxs := make([]sdk.Tx, len(txs))
for i, tx := range txs {
sdkTx, err := mp.txVerifier.TxDecode(tx)
if err != nil {
return nil
}
sdkTxs[i] = sdkTx
}
return &customMempoolIterator{
txs: sdkTxs,
}
}
46 changes: 46 additions & 0 deletions app/proposal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package app

import (
"context"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var (
_ baseapp.TxSelector = &ExtTxSelector{}
_ baseapp.ProposalTxVerifier = &ExtProposalTxVerifier{}
)

// ExtTxSelector extends a baseapp.TxSelector
type ExtTxSelector struct {
baseapp.TxSelector
}

func NewExtTxSelector(parent baseapp.TxSelector) *ExtTxSelector {
return &ExtTxSelector{
TxSelector: parent,
}
}

func (ts *ExtTxSelector) SelectTxForProposal(ctx context.Context, maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte) bool {
// don't pass `memTx` to parent selector so it don't check tx gas wanted against block gas limit,
// it conflicts with the max-tx-gas-wanted logic.
return ts.TxSelector.SelectTxForProposal(ctx, maxTxBytes, maxBlockGas, nil, txBz)
}

// ExtProposalTxVerifier extends a baseapp.ProposalTxVerifier
type ExtProposalTxVerifier struct {
baseapp.ProposalTxVerifier
}

func NewExtProposalTxVerifier(parent baseapp.ProposalTxVerifier) *ExtProposalTxVerifier {
return &ExtProposalTxVerifier{
ProposalTxVerifier: parent,
}
}

func (tv *ExtProposalTxVerifier) ProcessProposalVerifyTx(txBz []byte) (sdk.Tx, error) {
_, err := tv.ProposalTxVerifier.ProcessProposalVerifyTx(txBz)
return nil, err
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ replace (
cosmossdk.io/client/v2 => github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240604100318-cf12e5ad62e4
cosmossdk.io/store => github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240604100318-cf12e5ad62e4
cosmossdk.io/x/tx => github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240604100318-cf12e5ad62e4
github.com/cosmos/cosmos-sdk => github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240604100318-cf12e5ad62e4
github.com/cosmos/cosmos-sdk => github.com/mmsqe/cosmos-sdk v0.46.0-beta2.0.20240625022633-09f3c4959de4
)

replace (
Expand All @@ -276,7 +276,8 @@ replace (
github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2
github.com/ethereum/go-ethereum => github.com/crypto-org-chain/go-ethereum v1.10.20-0.20240425065928-ebb09502e7a7
// block-stm branch
github.com/evmos/ethermint => github.com/crypto-org-chain/ethermint v0.6.1-0.20240609125548-217e3af48fa5
// github.com/evmos/ethermint => github.com/crypto-org-chain/ethermint v0.6.1-0.20240609125548-217e3af48fa5
github.com/evmos/ethermint => ../ethermint
// Fix upstream GHSA-h395-qcrw-5vmq and GHSA-3vp4-m3rf-835h vulnerabilities.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,12 @@ github.com/crypto-org-chain/btree v0.0.0-20240406140148-2687063b042c h1:MOgfS4+F
github.com/crypto-org-chain/btree v0.0.0-20240406140148-2687063b042c/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
github.com/crypto-org-chain/cometbft-db v0.0.0-20231011055109-57922ac52a63 h1:R1QJ9a3XdYMSKo+1RdFifxb/g3lNypC52L/rpYrWoKo=
github.com/crypto-org-chain/cometbft-db v0.0.0-20231011055109-57922ac52a63/go.mod h1:rocwIfnS+kA060x64gkSIRvWB9StSppIkJuo5MWzL24=
github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240604100318-cf12e5ad62e4 h1:tU2H8foSm+CVtU9HPEephknr3C6mTrVeSERwRUs6yFY=
github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240604100318-cf12e5ad62e4/go.mod h1:bIUzWfqXnCF2WTFb2uN+FjzMIG3BsOk+P2QmvMtm4ic=
github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240604100318-cf12e5ad62e4 h1:ucFYZpOSYOi5BE96Y782iuczZK7d6ZJVhUoQ3ijrgf4=
github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240604100318-cf12e5ad62e4/go.mod h1:W5sR4asmVDUhJpEmuXTUBkk/yEefKlXTjVWcNciVSR0=
github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240604100318-cf12e5ad62e4 h1:aW5FkDdXGz+Nl9W5bkTTxkleEo3CNGLl78nU5CxikhQ=
github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240604100318-cf12e5ad62e4/go.mod h1:gjE3DZe4t/+VeIk6CmrouyqiuDbZ7QOVDDq3nLqBTpg=
github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240604100318-cf12e5ad62e4 h1:0SAVpeeKAJQLX+5aygBW57EIyVNtvYXXWs9zDusblKk=
github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240604100318-cf12e5ad62e4/go.mod h1:RTiTs4hkXG6IvYGknvB8p79YgjYJdcbzLUOGJChsPnY=
github.com/crypto-org-chain/ethermint v0.6.1-0.20240609125548-217e3af48fa5 h1:VYXq47qGP86Zf3GEhTvUPOeEpzEWOok57rEZgXhUClA=
github.com/crypto-org-chain/ethermint v0.6.1-0.20240609125548-217e3af48fa5/go.mod h1:U+PuJj12h0eCjzPMAuhn7Zf5aS3P7/oMZQ9kw+gddvE=
github.com/crypto-org-chain/go-block-stm v0.0.0-20240408011717-9f11af197bde h1:sQIHTJfVt5VTrF7po9eZiFkZiPjlHbFvnXtGCOoBjNM=
github.com/crypto-org-chain/go-block-stm v0.0.0-20240408011717-9f11af197bde/go.mod h1:iwQTX9xMX8NV9k3o2BiWXA0SswpsZrDk5q3gA7nWYiE=
github.com/crypto-org-chain/go-ethereum v1.10.20-0.20240425065928-ebb09502e7a7 h1:V43F3JFcqG4MUThf9W/DytnPblpR6CcaLBw2Wx6zTgE=
Expand Down Expand Up @@ -903,6 +899,8 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/pointerstructure v1.2.0 h1:O+i9nHnXS3l/9Wu7r4NrEdwA2VFTicjUEN1uBnDo34A=
github.com/mitchellh/pointerstructure v1.2.0/go.mod h1:BRAsLI5zgXmw97Lf6s25bs8ohIXc3tViBH44KcwB2g4=
github.com/mmsqe/cosmos-sdk v0.46.0-beta2.0.20240625022633-09f3c4959de4 h1:f8YrHUnppfldzQqMKXAr94qrgdHV1pkItvzGw9HaFCM=
github.com/mmsqe/cosmos-sdk v0.46.0-beta2.0.20240625022633-09f3c4959de4/go.mod h1:bIUzWfqXnCF2WTFb2uN+FjzMIG3BsOk+P2QmvMtm4ic=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
Expand Down
10 changes: 3 additions & 7 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ schema = 3
version = "v1.0.0-beta.5"
hash = "sha256-Fy/PbsOsd6iq0Njy3DVWK6HqWsogI+MkE8QslHGWyVg="
[mod."github.com/cosmos/cosmos-sdk"]
version = "v0.0.0-20240604100318-cf12e5ad62e4"
hash = "sha256-rKHP2oF2+W7iP4jor3jkGtieHTjccX9Gm2GQ1yeomC4="
replaced = "github.com/crypto-org-chain/cosmos-sdk"
version = "v0.46.0-beta2.0.20240625022633-09f3c4959de4"
hash = "sha256-zJAMhNGlxE6dXkIVLlJnqaAaELAe2dS5aD3WMAyu7Ww="
replaced = "github.com/mmsqe/cosmos-sdk"
[mod."github.com/cosmos/go-bip39"]
version = "v1.0.0"
hash = "sha256-Qm2aC2vaS8tjtMUbHmlBSagOSqbduEEDwc51qvQaBmA="
Expand Down Expand Up @@ -261,10 +261,6 @@ schema = 3
version = "v1.10.20-0.20240425065928-ebb09502e7a7"
hash = "sha256-lE4G5FaRb3MVi9FFVn+WlwsSTOB4SbjmVboKyQ5yB0A="
replaced = "github.com/crypto-org-chain/go-ethereum"
[mod."github.com/evmos/ethermint"]
version = "v0.6.1-0.20240609125548-217e3af48fa5"
hash = "sha256-7qMs+FFcDtd8RMLS514vulIdlBc4tR+Tj72Y/JWAVtI="
replaced = "github.com/crypto-org-chain/ethermint"
[mod."github.com/fatih/color"]
version = "v1.16.0"
hash = "sha256-Aq/SM28aPJVzvapllQ64R/DM4aZ5CHPewcm/AUJPyJQ="
Expand Down
5 changes: 2 additions & 3 deletions integration_tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,6 @@ def test_contract(cronos):
origin_cmd = None


@pytest.mark.skip(reason="max_gas_wanted not supported now, TODO: #1390")
@pytest.mark.parametrize("max_gas_wanted", [80000000, 40000000, 25000000, 500000, None])
def test_tx_inclusion(cronos, max_gas_wanted):
"""
Expand Down Expand Up @@ -871,12 +870,12 @@ def fn(cmd):
# the transactions should be included according to max_gas_wanted
if max_tx_in_block == 1:
for block_num, next_block_num in zip(block_nums, block_nums[1:]):
assert next_block_num == block_num + 1
assert next_block_num == block_num + 1 or next_block_num == block_num + 2
else:
for num in block_nums[1:max_tx_in_block]:
assert num == block_nums[0]
for num in block_nums[max_tx_in_block:]:
assert num == block_nums[0] + 1
assert num == block_nums[0] + 1 or num == block_nums[0] + 2


def test_replay_protection(cronos):
Expand Down

0 comments on commit 8829150

Please sign in to comment.