-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not use
posthandler
to handle failed tx sequence increment (#…
…92) * fix to do not use posthandler to handle failed tx sequence increment * add test
- Loading branch information
Showing
9 changed files
with
143 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ type contextKey int | |
|
||
const ( | ||
ContextKeyGasPrices contextKey = iota | ||
ContextKeySequenceIncremented | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ante | ||
|
||
import ( | ||
errorsmod "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
authante "github.com/cosmos/cosmos-sdk/x/auth/ante" | ||
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
) | ||
|
||
// IncrementSequenceDecorator is an AnteDecorator that increments the sequence number | ||
// of all signers in a transaction. It also sets a flag in the context to indicate | ||
// that the sequence has been incremented in the ante handler. | ||
type IncrementSequenceDecorator struct { | ||
ak authante.AccountKeeper | ||
} | ||
|
||
func NewIncrementSequenceDecorator(ak authante.AccountKeeper) IncrementSequenceDecorator { | ||
return IncrementSequenceDecorator{ | ||
ak: ak, | ||
} | ||
} | ||
|
||
func (isd IncrementSequenceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { | ||
sigTx, ok := tx.(authsigning.SigVerifiableTx) | ||
if !ok { | ||
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type") | ||
} | ||
|
||
// increment sequence of all signers | ||
signers, err := sigTx.GetSigners() | ||
if err != nil { | ||
return sdk.Context{}, err | ||
} | ||
|
||
for _, signer := range signers { | ||
acc := isd.ak.GetAccount(ctx, signer) | ||
if err := acc.SetSequence(acc.GetSequence() + 1); err != nil { | ||
panic(err) | ||
} | ||
|
||
isd.ak.SetAccount(ctx, acc) | ||
} | ||
|
||
// set a flag in context to indicate that sequence has been incremented in ante handler | ||
ctx = ctx.WithValue(ContextKeySequenceIncremented, true) | ||
return next(ctx, tx, simulate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ante_test | ||
|
||
import ( | ||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" | ||
"github.com/cosmos/cosmos-sdk/testutil/testdata" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/initia-labs/minievm/x/evm/ante" | ||
) | ||
|
||
func (suite *AnteTestSuite) TestIncrementSequenceDecorator() { | ||
suite.SetupTest() // setup | ||
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() | ||
|
||
priv, _, addr := testdata.KeyTestPubAddr() | ||
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr) | ||
suite.NoError(acc.SetAccountNumber(uint64(50))) | ||
suite.app.AccountKeeper.SetAccount(suite.ctx, acc) | ||
|
||
msgs := []sdk.Msg{testdata.NewTestMsg(addr)} | ||
suite.NoError(suite.txBuilder.SetMsgs(msgs...)) | ||
privs := []cryptotypes.PrivKey{priv} | ||
accNums := []uint64{suite.app.AccountKeeper.GetAccount(suite.ctx, addr).GetAccountNumber()} | ||
accSeqs := []uint64{suite.app.AccountKeeper.GetAccount(suite.ctx, addr).GetSequence()} | ||
feeAmount := testdata.NewTestFeeAmount() | ||
gasLimit := testdata.NewTestGasLimit() | ||
suite.txBuilder.SetFeeAmount(feeAmount) | ||
suite.txBuilder.SetGasLimit(gasLimit) | ||
|
||
tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) | ||
suite.NoError(err) | ||
|
||
isd := ante.NewIncrementSequenceDecorator(suite.app.AccountKeeper) | ||
antehandler := sdk.ChainAnteDecorators(isd) | ||
|
||
testCases := []struct { | ||
ctx sdk.Context | ||
simulate bool | ||
expectedSeq uint64 | ||
}{ | ||
{suite.ctx.WithIsReCheckTx(true), false, 1}, | ||
{suite.ctx.WithIsCheckTx(true).WithIsReCheckTx(false), false, 2}, | ||
{suite.ctx.WithIsReCheckTx(true), false, 3}, | ||
{suite.ctx.WithIsReCheckTx(true), false, 4}, | ||
{suite.ctx.WithIsReCheckTx(true), true, 5}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
ctx, err := antehandler(tc.ctx, tx, tc.simulate) | ||
suite.NoError(err, "unexpected error; tc #%d, %v", i, tc) | ||
suite.Equal(tc.expectedSeq, suite.app.AccountKeeper.GetAccount(suite.ctx, addr).GetSequence()) | ||
|
||
// the flag should be set in the context | ||
suite.NotNil(ctx.Value(ante.ContextKeySequenceIncremented)) | ||
} | ||
} |
Oops, something went wrong.