-
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.
minor refactor on timestamp nonce ante handler and keeper
- Loading branch information
1 parent
749dff9
commit 7542acc
Showing
11 changed files
with
188 additions
and
242 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 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 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,28 @@ | ||
package ante | ||
|
||
import ( | ||
errorsmod "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
accountpluskeeper "github.com/dydxprotocol/v4-chain/protocol/x/accountplus/keeper" | ||
) | ||
|
||
// IsTimestampNonceTx returns `true` if the supplied `tx` consist of a single signature that uses a timestamp nonce | ||
// value for sequence | ||
func IsTimestampNonceTx(ctx sdk.Context, tx sdk.Tx) (bool, error) { | ||
sigTx, ok := tx.(authsigning.SigVerifiableTx) | ||
if !ok { | ||
return false, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type") | ||
} | ||
signatures, err := sigTx.GetSignaturesV2() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if len(signatures) != 1 { | ||
return false, errorsmod.Wrap(sdkerrors.ErrTxDecode, "more than one signature") | ||
} | ||
|
||
return accountpluskeeper.IsTimestampNonce(signatures[0].Sequence), nil | ||
} |
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,85 @@ | ||
package ante_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/testutil/testdata" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/tx/signing" | ||
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" | ||
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants" | ||
sdktest "github.com/dydxprotocol/v4-chain/protocol/testutil/sdk" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/ante" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/keeper" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIsTimestampNonceTx(t *testing.T) { | ||
tests := map[string]struct { | ||
seqs []uint64 | ||
expectedResult bool | ||
expectedErr bool | ||
}{ | ||
"Returns false for non-ts nonce": { | ||
seqs: []uint64{0}, | ||
expectedResult: false, | ||
expectedErr: false, | ||
}, | ||
"Returns true for ts nonce": { | ||
seqs: []uint64{keeper.TimestampNonceSequenceCutoff}, | ||
expectedResult: true, | ||
expectedErr: false, | ||
}, | ||
"Returns error for more than one signature": { | ||
seqs: []uint64{keeper.TimestampNonceSequenceCutoff, keeper.TimestampNonceSequenceCutoff}, | ||
expectedResult: false, | ||
expectedErr: true, | ||
}, | ||
} | ||
|
||
// Run tests. | ||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
// Initialize some test setup which builds a test transaction from a slice of messages. | ||
var reg codectypes.InterfaceRegistry | ||
protoCfg := authtx.NewTxConfig(codec.NewProtoCodec(reg), authtx.DefaultSignModes) | ||
builder := protoCfg.NewTxBuilder() | ||
err := builder.SetMsgs([]sdk.Msg{constants.Msg_Send}...) | ||
require.NoError(t, err) | ||
|
||
// Create signatures | ||
var signatures []signing.SignatureV2 | ||
for _, seq := range tc.seqs { | ||
signatures = append(signatures, getSignature(seq)) | ||
} | ||
err = builder.SetSignatures(signatures...) | ||
|
||
require.NoError(t, err) | ||
tx := builder.GetTx() | ||
ctx, _, _ := sdktest.NewSdkContextWithMultistore() | ||
|
||
// Invoke the function under test. | ||
result, err := ante.IsTimestampNonceTx(ctx, tx) | ||
|
||
// Assert the results. | ||
if tc.expectedErr { | ||
require.NotNil(t, err) | ||
} | ||
require.Equal(t, tc.expectedResult, result) | ||
}) | ||
} | ||
} | ||
|
||
func getSignature(seq uint64) signing.SignatureV2 { | ||
_, pubKey, _ := testdata.KeyTestPubAddr() | ||
return signing.SignatureV2{ | ||
PubKey: pubKey, | ||
Data: &signing.SingleSignatureData{ | ||
SignMode: signing.SignMode_SIGN_MODE_DIRECT, | ||
Signature: nil, | ||
}, | ||
Sequence: seq, | ||
} | ||
} |
Oops, something went wrong.