x/auth/ante: Migrate tests to use the new client.TxConfig (#6661)

* WIP: using encoding config

* Make it compile, test fails

* test should be okay

* Make tests pass

* Add comments

* Convert more tests

* Make TestAnteHandlerSigErrors work

* Make first 2 tests pass

* TestAnteHandlerAccountNumbers

* Use table tests

* Remove print

* Use test table

* TestAnteHandlerSigErrors

* TestAnteHandlerAccountNumbers

* TestAnteHandlerAccountNumbers

* Refactor TestAccount

* Refactor getSignBytes

* TestAnteHandlerAccountNumbersAtBlockHeightZero

* TestAnteHandlerSequences

* TestAnteHandlerFees

* TestAnteHandlerMultiSigner

* TestAnteHandlerBadSignBytes

* TestAnteHandlerSetPubKey

* TestAnteHandlerSigLimitExceeded

* TestCustomSignatureVerificationGasConsumer

* TestAnteHandlerReCheck

* Make all tests pass

* Refactor a little bit more

* Fee test

* SetupTest

* All tests pass

* Refactor to RunTestCase

* Don't use StdFee

* Revert some little stuff

* Finish up last couple of test cases

* Less verbose

* s/TxGenerator/TxConfig

* Add comments

* Indent

* Move KeyTestPubAddr to testdata

* Move testdata to /testutil

* Revert to use signature: nil step in signing

* Add comments

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
Amaury Martiny
2020-07-20 08:30:12 -04:00
committed by GitHub
co-authored by Alexander Bezobchuk
parent cc96e5c16f
commit 5c0e3b4de5
82 changed files with 1685 additions and 1183 deletions
+3 -3
View File
@@ -59,7 +59,7 @@ func NewSimApp(val Validator) server.Application {
// in-process local testing network.
type Config struct {
Codec codec.Marshaler
TxGenerator client.TxGenerator
TxConfig client.TxConfig
AccountRetriever client.AccountRetriever
AppConstructor AppConstructor // the ABCI application constructor
GenesisState map[string]json.RawMessage // custom gensis state to provide
@@ -84,7 +84,7 @@ func DefaultConfig() Config {
return Config{
Codec: encCfg.Marshaler,
TxGenerator: encCfg.TxGenerator,
TxConfig: encCfg.TxConfig,
AccountRetriever: authtypes.NewAccountRetriever(encCfg.Marshaler),
AppConstructor: NewSimApp,
GenesisState: simapp.ModuleBasics.DefaultGenesis(encCfg.Marshaler),
@@ -298,7 +298,7 @@ func New(t *testing.T, cfg Config) *Network {
WithHomeDir(tmCfg.RootDir).
WithChainID(cfg.ChainID).
WithJSONMarshaler(cfg.Codec).
WithTxGenerator(cfg.TxGenerator).
WithTxConfig(cfg.TxConfig).
WithAccountRetriever(cfg.AccountRetriever)
network.Validators[i] = &Validator{
+73
View File
@@ -0,0 +1,73 @@
package testdata
// DONTCOVER
// nolint
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec/types"
)
type Animal interface {
Greet() string
}
func (c Cat) Greet() string {
return fmt.Sprintf("Meow, my name is %s", c.Moniker)
}
func (d Dog) Greet() string {
return fmt.Sprintf("Roof, my name is %s", d.Name)
}
var _ types.UnpackInterfacesMessage = HasAnimal{}
func (m HasAnimal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var animal Animal
return unpacker.UnpackAny(m.Animal, &animal)
}
type HasAnimalI interface {
TheAnimal() Animal
}
var _ HasAnimalI = &HasAnimal{}
func (m HasAnimal) TheAnimal() Animal {
return m.Animal.GetCachedValue().(Animal)
}
type HasHasAnimalI interface {
TheHasAnimal() HasAnimalI
}
var _ HasHasAnimalI = &HasHasAnimal{}
func (m HasHasAnimal) TheHasAnimal() HasAnimalI {
return m.HasAnimal.GetCachedValue().(HasAnimalI)
}
var _ types.UnpackInterfacesMessage = HasHasAnimal{}
func (m HasHasAnimal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var animal HasAnimalI
return unpacker.UnpackAny(m.HasAnimal, &animal)
}
type HasHasHasAnimalI interface {
TheHasHasAnimal() HasHasAnimalI
}
var _ HasHasAnimalI = &HasHasAnimal{}
func (m HasHasHasAnimal) TheHasHasAnimal() HasHasAnimalI {
return m.HasHasAnimal.GetCachedValue().(HasHasAnimalI)
}
var _ types.UnpackInterfacesMessage = HasHasHasAnimal{}
func (m HasHasHasAnimal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var animal HasHasAnimalI
return unpacker.UnpackAny(m.HasHasAnimal, &animal)
}
+2818
View File
File diff suppressed because it is too large Load Diff
+72
View File
@@ -0,0 +1,72 @@
syntax = "proto3";
package testdata;
import "google/protobuf/any.proto";
import "gogoproto/gogo.proto";
option go_package = "github.com/cosmos/cosmos-sdk/testutil/testdata";
message Dog {
string size = 1;
string name = 2;
}
message Cat {
string moniker = 1;
int32 lives = 2;
}
message HasAnimal {
google.protobuf.Any animal = 1;
int64 x = 2;
}
message HasHasAnimal {
google.protobuf.Any has_animal = 1;
}
message HasHasHasAnimal {
google.protobuf.Any has_has_animal = 1;
}
service TestService {
rpc Echo(EchoRequest) returns (EchoResponse);
rpc SayHello(SayHelloRequest) returns (SayHelloResponse);
rpc TestAny(TestAnyRequest) returns (TestAnyResponse);
}
message EchoRequest {
string message = 1;
}
message EchoResponse {
string message = 1;
}
message SayHelloRequest {
string name = 1;
}
message SayHelloResponse {
string greeting = 1;
}
message TestAnyRequest {
google.protobuf.Any any_animal = 1;
}
message TestAnyResponse {
HasAnimal has_animal = 1;
}
// msg type for testing
message TestMsg {
repeated bytes signers = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];
}
// bad MultiSignature with extra fields
message BadMultiSignature {
option (gogoproto.goproto_unrecognized) = true;
repeated bytes signatures = 1;
bytes malicious_field = 5;
}
+41
View File
@@ -0,0 +1,41 @@
package testdata
import (
"github.com/tendermint/go-amino"
"github.com/cosmos/cosmos-sdk/codec/types"
)
func NewTestInterfaceRegistry() types.InterfaceRegistry {
registry := types.NewInterfaceRegistry()
registry.RegisterInterface("Animal", (*Animal)(nil))
registry.RegisterImplementations(
(*Animal)(nil),
&Dog{},
&Cat{},
)
registry.RegisterImplementations(
(*HasAnimalI)(nil),
&HasAnimal{},
)
registry.RegisterImplementations(
(*HasHasAnimalI)(nil),
&HasHasAnimal{},
)
return registry
}
func NewTestAmino() *amino.Codec {
cdc := amino.NewCodec()
cdc.RegisterInterface((*Animal)(nil), nil)
cdc.RegisterConcrete(&Dog{}, "testdata/Dog", nil)
cdc.RegisterConcrete(&Cat{}, "testdata/Cat", nil)
cdc.RegisterInterface((*HasAnimalI)(nil), nil)
cdc.RegisterConcrete(&HasAnimal{}, "testdata/HasAnimal", nil)
cdc.RegisterInterface((*HasHasAnimalI)(nil), nil)
cdc.RegisterConcrete(&HasHasAnimal{}, "testdata/HasHasAnimal", nil)
return cdc
}
+53
View File
@@ -0,0 +1,53 @@
package testdata
import (
"context"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/cosmos/cosmos-sdk/codec/types"
)
type TestServiceImpl struct{}
func (e TestServiceImpl) TestAny(_ context.Context, request *TestAnyRequest) (*TestAnyResponse, error) {
animal, ok := request.AnyAnimal.GetCachedValue().(Animal)
if !ok {
return nil, fmt.Errorf("expected Animal")
}
any, err := types.NewAnyWithValue(animal.(proto.Message))
if err != nil {
return nil, err
}
return &TestAnyResponse{HasAnimal: &HasAnimal{
Animal: any,
X: 10,
}}, nil
}
func (e TestServiceImpl) Echo(_ context.Context, req *EchoRequest) (*EchoResponse, error) {
return &EchoResponse{Message: req.Message}, nil
}
func (e TestServiceImpl) SayHello(_ context.Context, request *SayHelloRequest) (*SayHelloResponse, error) {
greeting := fmt.Sprintf("Hello %s!", request.Name)
return &SayHelloResponse{Greeting: greeting}, nil
}
var _ TestServiceServer = TestServiceImpl{}
var _ types.UnpackInterfacesMessage = &TestAnyRequest{}
func (m *TestAnyRequest) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var animal Animal
return unpacker.UnpackAny(m.AnyAnimal, &animal)
}
var _ types.UnpackInterfacesMessage = &TestAnyResponse{}
func (m *TestAnyResponse) UnpackInterfaces(unpacker types.AnyUnpacker) error {
return m.HasAnimal.UnpackInterfaces(unpacker)
}
+47
View File
@@ -0,0 +1,47 @@
package testdata
import (
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/secp256k1"
)
// KeyTestPubAddr generates a new secp256k1 keypair.
func KeyTestPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.AccAddress) {
key := secp256k1.GenPrivKey()
pub := key.PubKey()
addr := sdk.AccAddress(pub.Address())
return key, pub, addr
}
// NewTestFeeAmount is a test fee amount.
func NewTestFeeAmount() sdk.Coins {
return sdk.NewCoins(sdk.NewInt64Coin("atom", 150))
}
// NewTestGasLimit is a test fee gas limit.
func NewTestGasLimit() uint64 {
return 100000
}
// NewTestMsg creates a message for testing with the given signers.
func NewTestMsg(addrs ...sdk.AccAddress) *TestMsg {
return &TestMsg{
Signers: addrs,
}
}
var _ sdk.Msg = (*TestMsg)(nil)
func (msg *TestMsg) Route() string { return "TestMsg" }
func (msg *TestMsg) Type() string { return "Test message" }
func (msg *TestMsg) GetSignBytes() []byte {
bz, err := json.Marshal(msg.Signers)
if err != nil {
panic(err)
}
return sdk.MustSortJSON(bz)
}
func (msg *TestMsg) ValidateBasic() error { return nil }