-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[OTE-553] Skip sequence increment for timestamp nonce #1956
Changes from all commits
a2d9900
42a35ef
d24dc06
741c8cd
f1a8c12
3e469e6
03b7ed1
952dfdc
207a4db
a6ae93c
3fdb5b8
9e4e31b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment saying this is forked from x/auth/ante/sigverify.go There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add in upcoming PR |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note this technically changes the semantics of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a need to revert to iterating on signers and check that the signer has a signature? |
||
if accountpluskeeper.IsTimestampNonce(signature.Sequence) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a very specific use case for timestamp nonce feature, and it seems to me the following will never happen:
Given this and previous comment, the easiest/least disruptive change (and avoiding a fork) seems to be:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this will be addressed by the ticket i linked above (https://linear.app/dydx/issue/OTE-624/combine-incrementsequencedecorator-into-sigverificationdecorator) |
||
// 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) | ||
} |
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()) | ||
} | ||
} |
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the rationale behind forking this decorator here, vs updating it in the
dydxprotocol/cosmo-sdk
fork? So that we can callaccountpluskeeper
helper function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was under the impression that updating the fork required additional release complexity.
There is actually a ticket to deprecate IncrementSequenceDecorator altogether by adding the incrementation into sigverify. (https://linear.app/dydx/issue/OTE-624/combine-incrementsequencedecorator-into-sigverificationdecorator)
I think I will have time to address this ticket and the other comments before v6 release. Will publish new PR soon