From f60d868a08a581455f1e29d61bad7e62c3ec3f63 Mon Sep 17 00:00:00 2001 From: Evan Han Date: Tue, 7 Sep 2021 15:42:05 +0800 Subject: [PATCH] Merge PR: Pending pool feature (#997) * add pending pool * update go.mod * update go.mod * update ante handler * update go.mod * update go.mod * update go.mod * update go.mod Co-authored-by: MengXiangJian <805442788@qq.com> --- app/ante/eth.go | 70 ++++++++++++++++++++++++++++++------------------- go.mod | 4 +-- go.sum | 12 +++------ 3 files changed, 49 insertions(+), 37 deletions(-) diff --git a/app/ante/eth.go b/app/ante/eth.go index 835718d2db..450589873c 100644 --- a/app/ante/eth.go +++ b/app/ante/eth.go @@ -256,6 +256,7 @@ func (nvd NonceVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim // all will be rejected except the first, since the first needs to be included in a block // before the sequence increments if ctx.IsCheckTx() { + ctx = ctx.WithAccountNonce(seq) // will be checkTx and RecheckTx mode if ctx.IsReCheckTx() { // recheckTx mode @@ -268,36 +269,46 @@ func (nvd NonceVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim ) } } else { - // checkTx mode - checkTxModeNonce := seq - - if !baseapp.IsMempoolEnableRecheck() { - // if is enable recheck, the sequence of checkState will increase after commit(), so we do not need - // to add pending txs len in the mempool. - // but, if disable recheck, we will not increase sequence of checkState (even in force recheck case, we - // will also reset checkState), so we will need to add pending txs len to get the right nonce - gPool := baseapp.GetGlobalMempool() - if gPool != nil { - cnt := gPool.GetUserPendingTxsCnt(common.BytesToAddress(address.Bytes()).String()) - checkTxModeNonce = seq + uint64(cnt) - } - } - - if baseapp.IsMempoolEnableSort() { - if msgEthTx.Data.AccountNonce < seq || msgEthTx.Data.AccountNonce > checkTxModeNonce { + if baseapp.IsMempoolEnablePendingPool() { + if msgEthTx.Data.AccountNonce < seq { return ctx, sdkerrors.Wrapf( sdkerrors.ErrInvalidSequence, - "invalid nonce; got %d, expected in the range of [%d, %d]", - msgEthTx.Data.AccountNonce, seq, checkTxModeNonce, + "invalid nonce; got %d, expected %d", + msgEthTx.Data.AccountNonce, seq, ) } } else { - if msgEthTx.Data.AccountNonce != checkTxModeNonce { - return ctx, sdkerrors.Wrapf( - sdkerrors.ErrInvalidSequence, - "invalid nonce; got %d, expected %d", - msgEthTx.Data.AccountNonce, checkTxModeNonce, - ) + // checkTx mode + checkTxModeNonce := seq + + if !baseapp.IsMempoolEnableRecheck() { + // if is enable recheck, the sequence of checkState will increase after commit(), so we do not need + // to add pending txs len in the mempool. + // but, if disable recheck, we will not increase sequence of checkState (even in force recheck case, we + // will also reset checkState), so we will need to add pending txs len to get the right nonce + gPool := baseapp.GetGlobalMempool() + if gPool != nil { + cnt := gPool.GetUserPendingTxsCnt(common.BytesToAddress(address.Bytes()).String()) + checkTxModeNonce = seq + uint64(cnt) + } + } + + if baseapp.IsMempoolEnableSort() { + if msgEthTx.Data.AccountNonce < seq || msgEthTx.Data.AccountNonce > checkTxModeNonce { + return ctx, sdkerrors.Wrapf( + sdkerrors.ErrInvalidSequence, + "invalid nonce; got %d, expected in the range of [%d, %d]", + msgEthTx.Data.AccountNonce, seq, checkTxModeNonce, + ) + } + } else { + if msgEthTx.Data.AccountNonce != checkTxModeNonce { + return ctx, sdkerrors.Wrapf( + sdkerrors.ErrInvalidSequence, + "invalid nonce; got %d, expected %d", + msgEthTx.Data.AccountNonce, checkTxModeNonce, + ) + } } } } @@ -437,8 +448,13 @@ func (issd IncrementSenderSequenceDecorator) AnteHandle(ctx sdk.Context, tx sdk. // increment sequence of all signers for _, addr := range msgEthTx.GetSigners() { acc := issd.ak.GetAccount(ctx, addr) - - if err := acc.SetSequence(acc.GetSequence() + 1); err != nil { + seq := acc.GetSequence() + if !baseapp.IsMempoolEnablePendingPool() { + seq++ + } else if msgEthTx.Data.AccountNonce == seq { + seq++ + } + if err := acc.SetSequence(seq); err != nil { panic(err) } issd.ak.SetAccount(ctx, acc) diff --git a/go.mod b/go.mod index 1d6013c54a..58cc9c6269 100644 --- a/go.mod +++ b/go.mod @@ -57,8 +57,8 @@ require ( ) replace ( - github.com/cosmos/cosmos-sdk => github.com/okex/cosmos-sdk v0.39.2-exchain15 + github.com/cosmos/cosmos-sdk => github.com/okex/cosmos-sdk v0.39.3-0.20210906101954-ea9c2e5521c8 github.com/tendermint/iavl => github.com/okex/iavl v0.14.3-exchain - github.com/tendermint/tendermint => github.com/okex/tendermint v0.33.9-exchain10 + github.com/tendermint/tendermint => github.com/okex/tendermint v0.33.9-okexchain6.0.20210906101911-c04d43a1f4ba github.com/tendermint/tm-db => github.com/okex/tm-db v0.5.2-exchain1 ) diff --git a/go.sum b/go.sum index 6c0d621095..859c01768b 100644 --- a/go.sum +++ b/go.sum @@ -501,18 +501,14 @@ github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxzi github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= -github.com/okex/cosmos-sdk v0.39.2-exchain15 h1:JO/k3tcK/ozybF0h9lMvhOgAdEnwz2l46Bs0ra37Ois= -github.com/okex/cosmos-sdk v0.39.2-exchain15/go.mod h1:IvlniaZoJAtzILHgcmnfaJ5S125TYJWfJvGqY9zSXb4= -github.com/okex/cosmos-sdk v0.39.3-0.20210831062138-cebb8df97837 h1:dmmLcohm7L9cON+hnHM80jLdkW8I8FEFWYvwJF7CQS4= -github.com/okex/cosmos-sdk v0.39.3-0.20210831062138-cebb8df97837/go.mod h1:IvlniaZoJAtzILHgcmnfaJ5S125TYJWfJvGqY9zSXb4= +github.com/okex/cosmos-sdk v0.39.3-0.20210906101954-ea9c2e5521c8 h1:YZY8B5VDRhDmF3+fiRziVUASPsVFqt6PAq5TcWOaIgk= +github.com/okex/cosmos-sdk v0.39.3-0.20210906101954-ea9c2e5521c8/go.mod h1:IvlniaZoJAtzILHgcmnfaJ5S125TYJWfJvGqY9zSXb4= github.com/okex/iavl v0.14.3-exchain h1:kwRIwpFD6B8mDDqoaxeUN3Pg2GW0Vr+sA+b86renWcA= github.com/okex/iavl v0.14.3-exchain/go.mod h1:vHLYxU/zuxBmxxr1v+5Vnd/JzcIsyK17n9P9RDubPVU= -github.com/okex/tendermint v0.33.9-exchain10 h1:tIMxk2RDv4YxLFht878S1BHTXzA84Ouvz5DtkDSCLGk= -github.com/okex/tendermint v0.33.9-exchain10/go.mod h1:EoGTbJUufUueNIigY3zyO6f7GOj29OdpFhuR8sxWdSU= +github.com/okex/tendermint v0.33.9-okexchain6.0.20210906101911-c04d43a1f4ba h1:mFF3Nuh+PrqN/fxBCDVa9BCawMYOu6LyfhPXxAPsfsA= +github.com/okex/tendermint v0.33.9-okexchain6.0.20210906101911-c04d43a1f4ba/go.mod h1:EoGTbJUufUueNIigY3zyO6f7GOj29OdpFhuR8sxWdSU= github.com/okex/tm-db v0.5.2-exchain1 h1:c8aX7HoNW58GXHEO6e7BR81SwyBfjWJkqGwNUhF3tlc= github.com/okex/tm-db v0.5.2-exchain1/go.mod h1:VrPTx04QJhQ9d8TFUTc2GpPBvBf/U9vIdBIzkjBk7Lk= -github.com/okex/tm-db v0.5.3-0.20210831062251-0eeb93ab0259 h1:2FF18PzBoCWH901/jwArYQGbmkG104zCztoCVmKch94= -github.com/okex/tm-db v0.5.3-0.20210831062251-0eeb93ab0259/go.mod h1:VrPTx04QJhQ9d8TFUTc2GpPBvBf/U9vIdBIzkjBk7Lk= github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=