cosmos-sdk/x/auth/tx/encode_decode_test.go
Amaury Martiny 7cd25abb87
Proto Tx with Any (#7276)
* WIP on protobuf keys

* Use Type() and Bytes() in sr25519 pub key Equals

* Add tests

* Add few more tests

* Update other pub/priv key types Equals

* Fix PrivKey's Sign method

* Rename variables in tests

* Fix infinite recursive calls

* Use tm ed25519 keys

* Add Sign and VerifySignature tests

* Remove ed25519 and sr25519 references

* proto linting

* Add proto crypto file

* Implement some of the new multisig proto type methods

* Add tests for MultisigThresholdPubKey

* Add tests for pubkey pb/amino conversion functions

* Move crypto types.go and register new proto pubkeys

* Add missing pointer ref

* Address review comments

* panic in MultisigThresholdPubKey VerifySignature

* Use internal crypto.PubKey in multisig

* Add tests for MultisigThresholdPubKey VerifyMultisignature

* Only keep LegacyAminoMultisigThresholdPubKey and move to proto keys to v1

* Remove conversion functions and introduce internal PubKey type

* Override Amino marshaling for proto pubkeys

* Merge master

* Make proto-gen

* Start removal of old PubKeyMultisigThreshold references

* Fix tests

* Fix solomachine

* Fix ante handler tests

* Pull latest go-amino

* Remove ed25519

* Remove old secp256k1 PubKey and PrivKey

* Uncomment test case

* Fix linting issues

* More linting

* Revert tests keys values

* Add Amino overrides to proto keys

* Add pubkey test

* Fix tests

* Use threshold isntead of K

* Standardize Type

* Revert standardize types commit

* Fix build

* Fix lint

* Fix lint

* Add comment

* Register crypto.PubKey

* Add empty key in BuildSimTx

* Simplify proto names

* Unpack interfaces for signing desc

* Fix IBC tests?

* Format proto

* Use secp256k1 in ibc

* Fixed merge issues

* Uncomment tests

* Update x/ibc/testing/solomachine.go

* UnpackInterfaces for solomachine types

* Remove old multisig

* Add amino marshal for multisig

* Fix lint

* Correctly register amino

* One test left!

* Remove old struct

* Fix test

* Fix test

* Unpack into tmcrypto

* Remove old threshold pubkey tests

* Fix register amino

* Fix lint

* Use sdk crypto PubKey in multisig UnpackInterfaces

* Potential fix?

* Register LegacyAminoPubKey

* Register our own PubKey

* Register tmcrypto PubKey

* Register both PubKeys

* Register interfaces in test

* Refactor fiels

* Add comments

* Use anil's suggestion

* Add norace back

* Check nil

* Address comments

* FIx lint

* Add tests for solomachine unpack interfaces

* Fix query tx by hash

* Better name in amino register

* Display StdTx instead of proto Tx

* Remove useless check

Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com>
Co-authored-by: blushi <marie.gauthier63@gmail.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
2020-09-21 16:48:28 +00:00

163 lines
4.2 KiB
Go

package tx
import (
"fmt"
"testing"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/stretchr/testify/require"
"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"
)
func TestDefaultTxDecoderError(t *testing.T) {
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)
encoder := DefaultTxEncoder()
decoder := DefaultTxDecoder(cdc)
builder := newBuilder()
err := builder.SetMsgs(testdata.NewTestMsg())
require.NoError(t, err)
txBz, err := encoder(builder.GetTx())
require.NoError(t, err)
_, err = decoder(txBz)
require.EqualError(t, err, "no registered implementations of type types.Msg: tx parse error")
registry.RegisterImplementations((*sdk.Msg)(nil), &testdata.TestMsg{})
_, err = decoder(txBz)
require.NoError(t, err)
}
func TestUnknownFields(t *testing.T) {
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)
decoder := DefaultTxDecoder(cdc)
tests := []struct {
name string
body *testdata.TestUpdatedTxBody
authInfo *testdata.TestUpdatedAuthInfo
shouldErr bool
shouldAminoErr string
}{
{
name: "no new fields should pass",
body: &testdata.TestUpdatedTxBody{
Memo: "foo",
},
authInfo: &testdata.TestUpdatedAuthInfo{},
shouldErr: false,
},
{
name: "non-critical fields in TxBody should not error on decode, but should error with amino",
body: &testdata.TestUpdatedTxBody{
Memo: "foo",
SomeNewFieldNonCriticalField: "blah",
},
authInfo: &testdata.TestUpdatedAuthInfo{},
shouldErr: false,
shouldAminoErr: fmt.Sprintf("%s: %s", aminoNonCriticalFieldsError, sdkerrors.ErrInvalidRequest.Error()),
},
{
name: "critical fields in TxBody should error on decode",
body: &testdata.TestUpdatedTxBody{
Memo: "foo",
SomeNewField: 10,
},
authInfo: &testdata.TestUpdatedAuthInfo{},
shouldErr: true,
},
{
name: "critical fields in AuthInfo should error on decode",
body: &testdata.TestUpdatedTxBody{
Memo: "foo",
},
authInfo: &testdata.TestUpdatedAuthInfo{
NewField_3: []byte("xyz"),
},
shouldErr: true,
},
{
name: "non-critical fields in AuthInfo should error on decode",
body: &testdata.TestUpdatedTxBody{
Memo: "foo",
},
authInfo: &testdata.TestUpdatedAuthInfo{
NewField_1024: []byte("xyz"),
},
shouldErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
bodyBz, err := tt.body.Marshal()
require.NoError(t, err)
authInfoBz, err := tt.authInfo.Marshal()
require.NoError(t, err)
txRaw := &tx.TxRaw{
BodyBytes: bodyBz,
AuthInfoBytes: authInfoBz,
}
txBz, err := txRaw.Marshal()
require.NoError(t, err)
_, err = decoder(txBz)
if tt.shouldErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
if tt.shouldAminoErr != "" {
handler := signModeLegacyAminoJSONHandler{}
decoder := DefaultTxDecoder(codec.NewProtoCodec(codectypes.NewInterfaceRegistry()))
theTx, err := decoder(txBz)
require.NoError(t, err)
_, err = handler.GetSignBytes(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signing.SignerData{}, theTx)
require.EqualError(t, err, tt.shouldAminoErr)
}
})
}
t.Log("test TxRaw no new fields, should succeed")
txRaw := &testdata.TestUpdatedTxRaw{}
txBz, err := txRaw.Marshal()
require.NoError(t, err)
_, err = decoder(txBz)
require.NoError(t, err)
t.Log("new field in TxRaw should fail")
txRaw = &testdata.TestUpdatedTxRaw{
NewField_5: []byte("abc"),
}
txBz, err = txRaw.Marshal()
require.NoError(t, err)
_, err = decoder(txBz)
require.Error(t, err)
//
t.Log("new \"non-critical\" field in TxRaw should fail")
txRaw = &testdata.TestUpdatedTxRaw{
NewField_1024: []byte("abc"),
}
txBz, err = txRaw.Marshal()
require.NoError(t, err)
_, err = decoder(txBz)
require.Error(t, err)
}