-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OTE-553] Skip sequence increment for timestamp nonce (#1956)
Skip sequence increment for timestamp nonce.
- Loading branch information
1 parent
f3af0fe
commit 0076245
Showing
6 changed files
with
147 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package ante | ||
|
||
import ( | ||
errorsmod "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
ante "github.com/cosmos/cosmos-sdk/x/auth/ante" | ||
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
accountpluskeeper "github.com/dydxprotocol/v4-chain/protocol/x/accountplus/keeper" | ||
) | ||
|
||
// TODO: combine increment sequence and sequence verification into one decorator | ||
// https://github.com/cosmos/cosmos-sdk/pull/18817 | ||
type IncrementSequenceDecorator struct { | ||
ak ante.AccountKeeper | ||
} | ||
|
||
func NewIncrementSequenceDecorator(ak ante.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") | ||
} | ||
|
||
signatures, err := sigTx.GetSignaturesV2() | ||
if err != nil { | ||
return sdk.Context{}, err | ||
} | ||
|
||
for _, signature := range signatures { | ||
if accountpluskeeper.IsTimestampNonce(signature.Sequence) { | ||
// Skip increment for this signature | ||
continue | ||
} | ||
|
||
acc := isd.ak.GetAccount(ctx, signature.PubKey.Address().Bytes()) | ||
if err := acc.SetSequence(acc.GetSequence() + 1); err != nil { | ||
panic(err) | ||
} | ||
|
||
isd.ak.SetAccount(ctx, acc) | ||
} | ||
|
||
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,75 @@ | ||
package ante_test | ||
|
||
import ( | ||
"testing" | ||
|
||
testante "github.com/dydxprotocol/v4-chain/protocol/testutil/ante" | ||
"github.com/stretchr/testify/require" | ||
|
||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" | ||
"github.com/cosmos/cosmos-sdk/testutil/testdata" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/tx/signing" | ||
|
||
customante "github.com/dydxprotocol/v4-chain/protocol/app/ante" | ||
accountpluskeeper "github.com/dydxprotocol/v4-chain/protocol/x/accountplus/keeper" | ||
) | ||
|
||
// Modified from cosmossdk test for IncrementSequenceDecorator | ||
func TestDydxIncrementSequenceDecorator(t *testing.T) { | ||
suite := testante.SetupTestSuite(t, true) | ||
suite.TxBuilder = suite.ClientCtx.TxConfig.NewTxBuilder() | ||
|
||
priv, _, addr := testdata.KeyTestPubAddr() | ||
acc := suite.AccountKeeper.NewAccountWithAddress(suite.Ctx, addr) | ||
require.NoError(t, acc.SetAccountNumber(uint64(50))) | ||
suite.AccountKeeper.SetAccount(suite.Ctx, acc) | ||
|
||
msgs := []sdk.Msg{testdata.NewTestMsg(addr)} | ||
require.NoError(t, suite.TxBuilder.SetMsgs(msgs...)) | ||
privs := []cryptotypes.PrivKey{priv} | ||
accNums := []uint64{suite.AccountKeeper.GetAccount(suite.Ctx, addr).GetAccountNumber()} | ||
feeAmount := testdata.NewTestFeeAmount() | ||
gasLimit := testdata.NewTestGasLimit() | ||
suite.TxBuilder.SetFeeAmount(feeAmount) | ||
suite.TxBuilder.SetGasLimit(gasLimit) | ||
|
||
isd := customante.NewIncrementSequenceDecorator(suite.AccountKeeper) | ||
antehandler := sdk.ChainAnteDecorators(isd) | ||
|
||
testCases := []struct { | ||
ctx sdk.Context | ||
simulate bool | ||
// This value need not be valid (accountSeq + 1). Validity is handed in customante.NewSigVerificationDecorator | ||
signatureSeq uint64 | ||
expectedSeq uint64 | ||
}{ | ||
// tests from cosmossdk checking incrementing seqence | ||
{suite.Ctx.WithIsReCheckTx(true), false, 0, 1}, | ||
{suite.Ctx.WithIsCheckTx(true).WithIsReCheckTx(false), false, 0, 2}, | ||
{suite.Ctx.WithIsReCheckTx(true), false, 0, 3}, | ||
{suite.Ctx.WithIsReCheckTx(true), false, 0, 4}, | ||
{suite.Ctx.WithIsReCheckTx(true), true, 0, 5}, | ||
|
||
// tests checking that tx with timestamp nonces will not increment sequence | ||
{suite.Ctx.WithIsReCheckTx(true), true, accountpluskeeper.TimestampNonceSequenceCutoff, 5}, | ||
{suite.Ctx.WithIsReCheckTx(true), true, accountpluskeeper.TimestampNonceSequenceCutoff + 100000, 5}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
accSeqs := []uint64{tc.signatureSeq} | ||
tx, err := suite.CreateTestTx( | ||
suite.Ctx, | ||
privs, | ||
accNums, | ||
accSeqs, | ||
suite.Ctx.ChainID(), | ||
signing.SignMode_SIGN_MODE_DIRECT, | ||
) | ||
require.NoError(t, err) | ||
|
||
_, err = antehandler(tc.ctx, tx, tc.simulate) | ||
require.NoError(t, err, "unexpected error; tc #%d, %v", i, tc) | ||
require.Equal(t, tc.expectedSeq, suite.AccountKeeper.GetAccount(suite.Ctx, addr).GetSequence()) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
package keeper | ||
|
||
func Placeholder() {} | ||
const TimestampNonceSequenceCutoff uint64 = 1 << 40 // 2^40 | ||
|
||
func IsTimestampNonce(ts uint64) bool { | ||
return ts >= TimestampNonceSequenceCutoff | ||
} |
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