From 39161248b2426d933eb93baefe6e9fe958f29a86 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 20 Apr 2020 19:35:49 -0400 Subject: [PATCH 1/6] refactor simulation decoder --- types/store.go | 3 +-- x/auth/simulation/decoder.go | 24 +++++++++++++++++------- x/auth/simulation/decoder_test.go | 15 ++++----------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/types/store.go b/types/store.go index 884556cc6b26..5dca0930b8ab 100644 --- a/types/store.go +++ b/types/store.go @@ -3,7 +3,6 @@ package types import ( tmkv "github.com/tendermint/tendermint/libs/kv" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store/types" ) @@ -28,7 +27,7 @@ type ( // StoreDecoderRegistry defines each of the modules store decoders. Used for ImportExport // simulation. -type StoreDecoderRegistry map[string]func(cdc *codec.Codec, kvA, kvB tmkv.Pair) string +type StoreDecoderRegistry map[string]func(cdc interface{}, kvA, kvB tmkv.Pair) string // Iterator over all the keys with a certain prefix in ascending order func KVStorePrefixIterator(kvs KVStore, prefix []byte) Iterator { diff --git a/x/auth/simulation/decoder.go b/x/auth/simulation/decoder.go index 7d5d0d7b52cc..448b4986612c 100644 --- a/x/auth/simulation/decoder.go +++ b/x/auth/simulation/decoder.go @@ -4,24 +4,34 @@ import ( "bytes" "fmt" + gogotypes "github.com/gogo/protobuf/types" tmkv "github.com/tendermint/tendermint/libs/kv" - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/auth/types" ) // DecodeStore unmarshals the KVPair's Value to the corresponding auth type -func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string { +func DecodeStore(cdcI interface{}, kvA, kvB tmkv.Pair) string { + cdc, ok := cdcI.(types.Codec) + if !ok { + panic(fmt.Sprintf("invalid codec: %T", cdcI)) + } + switch { case bytes.Equal(kvA.Key[:1], types.AddressStoreKeyPrefix): - var accA, accB exported.Account - cdc.MustUnmarshalBinaryBare(kvA.Value, &accA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &accB) + accA, err := cdc.UnmarshalAccount(kvA.Value) + if err != nil { + panic(err) + } + + accB, err := cdc.UnmarshalAccount(kvB.Value) + if err != nil { + panic(err) + } return fmt.Sprintf("%v\n%v", accA, accB) case bytes.Equal(kvA.Key, types.GlobalAccountNumberKey): - var globalAccNumberA, globalAccNumberB uint64 + var globalAccNumberA, globalAccNumberB gogotypes.UInt64Value cdc.MustUnmarshalBinaryBare(kvA.Value, &globalAccNumberA) cdc.MustUnmarshalBinaryBare(kvB.Value, &globalAccNumberB) return fmt.Sprintf("GlobalAccNumberA: %d\nGlobalAccNumberB: %d", globalAccNumberA, globalAccNumberB) diff --git a/x/auth/simulation/decoder_test.go b/x/auth/simulation/decoder_test.go index 729ca9d79320..9e6eca4e8cef 100644 --- a/x/auth/simulation/decoder_test.go +++ b/x/auth/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package simulation +package simulation_test import ( "fmt" @@ -9,7 +9,8 @@ import ( "github.com/tendermint/tendermint/crypto/ed25519" tmkv "github.com/tendermint/tendermint/libs/kv" - "github.com/cosmos/cosmos-sdk/codec" + codecstd "github.com/cosmos/cosmos-sdk/codec/std" + "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -19,16 +20,8 @@ var ( delAddr1 = sdk.AccAddress(delPk1.Address()) ) -func makeTestCodec() (cdc *codec.Codec) { - cdc = codec.New() - sdk.RegisterCodec(cdc) - codec.RegisterCrypto(cdc) - types.RegisterCodec(cdc) - return -} - func TestDecodeStore(t *testing.T) { - cdc := makeTestCodec() + cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics)) acc := types.NewBaseAccountWithAddress(delAddr1) globalAccNumber := uint64(10) From 51c3d129e0036b456440b493aec45096cd6ac80a Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 21 Apr 2020 10:41:54 -0400 Subject: [PATCH 2/6] Update x/auth --- simapp/utils.go | 7 ++-- simapp/utils_test.go | 5 ++- types/store.go | 2 +- x/auth/module.go | 12 ++++--- x/auth/simulation/decoder.go | 54 +++++++++++++++---------------- x/auth/simulation/decoder_test.go | 29 +++++++++++++---- 6 files changed, 62 insertions(+), 47 deletions(-) diff --git a/simapp/utils.go b/simapp/utils.go index 8cd3822722e6..1b89e4e02a46 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -109,9 +109,8 @@ func PrintStats(db dbm.DB) { // GetSimulationLog unmarshals the KVPair's Value to the corresponding type based on the // each's module store key and the prefix bytes of the KVPair's key. -func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, cdc *codec.Codec, kvAs, kvBs []tmkv.Pair) (log string) { +func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, kvAs, kvBs []tmkv.Pair) (log string) { for i := 0; i < len(kvAs); i++ { - if len(kvAs[i].Value) == 0 && len(kvBs[i].Value) == 0 { // skip if the value doesn't have any bytes continue @@ -119,11 +118,11 @@ func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, cdc *codec decoder, ok := sdr[storeName] if ok { - log += decoder(cdc, kvAs[i], kvBs[i]) + log += decoder(kvAs[i], kvBs[i]) } else { log += fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvAs[i].Key, kvAs[i].Value, kvBs[i].Key, kvBs[i].Value) } } - return + return log } diff --git a/simapp/utils_test.go b/simapp/utils_test.go index 729ceddc286a..c1f36e3c75f5 100644 --- a/simapp/utils_test.go +++ b/simapp/utils_test.go @@ -7,7 +7,6 @@ import ( "github.com/stretchr/testify/require" tmkv "github.com/tendermint/tendermint/libs/kv" - "github.com/cosmos/cosmos-sdk/codec" codecstd "github.com/cosmos/cosmos-sdk/codec/std" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" @@ -17,7 +16,7 @@ func TestGetSimulationLog(t *testing.T) { cdc := codecstd.MakeCodec(ModuleBasics) decoders := make(sdk.StoreDecoderRegistry) - decoders[auth.StoreKey] = func(cdc *codec.Codec, kvAs, kvBs tmkv.Pair) string { return "10" } + decoders[auth.StoreKey] = func(kvAs, kvBs tmkv.Pair) string { return "10" } tests := []struct { store string @@ -44,7 +43,7 @@ func TestGetSimulationLog(t *testing.T) { for _, tt := range tests { tt := tt t.Run(tt.store, func(t *testing.T) { - require.Equal(t, tt.expectedLog, GetSimulationLog(tt.store, decoders, cdc, tt.kvPairs, tt.kvPairs), tt.store) + require.Equal(t, tt.expectedLog, GetSimulationLog(tt.store, decoders, tt.kvPairs, tt.kvPairs), tt.store) }) } } diff --git a/types/store.go b/types/store.go index 5dca0930b8ab..dd8bb17365ac 100644 --- a/types/store.go +++ b/types/store.go @@ -27,7 +27,7 @@ type ( // StoreDecoderRegistry defines each of the modules store decoders. Used for ImportExport // simulation. -type StoreDecoderRegistry map[string]func(cdc interface{}, kvA, kvB tmkv.Pair) string +type StoreDecoderRegistry map[string]func(kvA, kvB tmkv.Pair) string // Iterator over all the keys with a certain prefix in ascending order func KVStorePrefixIterator(kvs KVStore, prefix []byte) Iterator { diff --git a/x/auth/module.go b/x/auth/module.go index 3c3a9acf0cbc..2f425d645956 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -27,7 +27,9 @@ var ( ) // AppModuleBasic defines the basic application module used by the auth module. -type AppModuleBasic struct{} +type AppModuleBasic struct { + cdc Codec +} // Name returns the auth module's name. func (AppModuleBasic) Name() string { @@ -80,9 +82,9 @@ type AppModule struct { } // NewAppModule creates a new AppModule object -func NewAppModule(accountKeeper AccountKeeper) AppModule { +func NewAppModule(cdc Codec, accountKeeper AccountKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, + AppModuleBasic: AppModuleBasic{cdc: cdc}, accountKeeper: accountKeeper, } } @@ -156,8 +158,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { } // RegisterStoreDecoder registers a decoder for auth module's types -func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = simulation.NewDecodeStore(am.cdc) } // WeightedOperations doesn't return any auth module operation. diff --git a/x/auth/simulation/decoder.go b/x/auth/simulation/decoder.go index 448b4986612c..e14670a36dc1 100644 --- a/x/auth/simulation/decoder.go +++ b/x/auth/simulation/decoder.go @@ -10,33 +10,33 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/types" ) -// DecodeStore unmarshals the KVPair's Value to the corresponding auth type -func DecodeStore(cdcI interface{}, kvA, kvB tmkv.Pair) string { - cdc, ok := cdcI.(types.Codec) - if !ok { - panic(fmt.Sprintf("invalid codec: %T", cdcI)) - } - - switch { - case bytes.Equal(kvA.Key[:1], types.AddressStoreKeyPrefix): - accA, err := cdc.UnmarshalAccount(kvA.Value) - if err != nil { - panic(err) - } - - accB, err := cdc.UnmarshalAccount(kvB.Value) - if err != nil { - panic(err) +// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's +// Value to the corresponding auth type. +func NewDecodeStore(cdc types.Codec) func(kvA, kvB tmkv.Pair) string { + return func(kvA, kvB tmkv.Pair) string { + switch { + case bytes.Equal(kvA.Key[:1], types.AddressStoreKeyPrefix): + accA, err := cdc.UnmarshalAccount(kvA.Value) + if err != nil { + panic(err) + } + + accB, err := cdc.UnmarshalAccount(kvB.Value) + if err != nil { + panic(err) + } + + return fmt.Sprintf("%v\n%v", accA, accB) + + case bytes.Equal(kvA.Key, types.GlobalAccountNumberKey): + var globalAccNumberA, globalAccNumberB gogotypes.UInt64Value + cdc.MustUnmarshalBinaryBare(kvA.Value, &globalAccNumberA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &globalAccNumberB) + + return fmt.Sprintf("GlobalAccNumberA: %d\nGlobalAccNumberB: %d", globalAccNumberA, globalAccNumberB) + + default: + panic(fmt.Sprintf("unexpected %s key %X (%s)", types.ModuleName, kvA.Key, kvA.Key)) } - return fmt.Sprintf("%v\n%v", accA, accB) - - case bytes.Equal(kvA.Key, types.GlobalAccountNumberKey): - var globalAccNumberA, globalAccNumberB gogotypes.UInt64Value - cdc.MustUnmarshalBinaryBare(kvA.Value, &globalAccNumberA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &globalAccNumberB) - return fmt.Sprintf("GlobalAccNumberA: %d\nGlobalAccNumberB: %d", globalAccNumberA, globalAccNumberB) - - default: - panic(fmt.Sprintf("invalid account key %X", kvA.Key)) } } diff --git a/x/auth/simulation/decoder_test.go b/x/auth/simulation/decoder_test.go index 9e6eca4e8cef..77c27abfde74 100644 --- a/x/auth/simulation/decoder_test.go +++ b/x/auth/simulation/decoder_test.go @@ -4,14 +4,15 @@ import ( "fmt" "testing" + gogotypes "github.com/gogo/protobuf/types" "github.com/stretchr/testify/require" - "github.com/tendermint/tendermint/crypto/ed25519" tmkv "github.com/tendermint/tendermint/libs/kv" codecstd "github.com/cosmos/cosmos-sdk/codec/std" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/simulation" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -23,12 +24,26 @@ var ( func TestDecodeStore(t *testing.T) { cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics)) acc := types.NewBaseAccountWithAddress(delAddr1) - globalAccNumber := uint64(10) + dec := simulation.NewDecodeStore(cdc) + + accBz, err := cdc.MarshalAccount(acc) + require.NoError(t, err) + + globalAccNumber := &gogotypes.UInt64Value{Value: 10} kvPairs := tmkv.Pairs{ - tmkv.Pair{Key: types.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(acc)}, - tmkv.Pair{Key: types.GlobalAccountNumberKey, Value: cdc.MustMarshalBinaryBare(globalAccNumber)}, - tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}}, + tmkv.Pair{ + Key: types.AddressStoreKey(delAddr1), + Value: accBz, + }, + tmkv.Pair{ + Key: types.GlobalAccountNumberKey, + Value: cdc.MustMarshalBinaryBare(globalAccNumber), + }, + tmkv.Pair{ + Key: []byte{0x99}, + Value: []byte{0x99}, + }, } tests := []struct { name string @@ -44,9 +59,9 @@ func TestDecodeStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name) } }) } From 0dd415ffb555564f3327f7227a6e343998ace632 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 21 Apr 2020 10:55:19 -0400 Subject: [PATCH 3/6] Update x/bank --- x/bank/module.go | 12 +++++++----- x/bank/simulation/decoder.go | 31 ++++++++++++++++++++----------- x/bank/simulation/decoder_test.go | 25 ++++++++++++------------- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/x/bank/module.go b/x/bank/module.go index d4f547d29333..1cc4b7fa4d0e 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -28,7 +28,9 @@ var ( ) // AppModuleBasic defines the basic application module used by the bank module. -type AppModuleBasic struct{} +type AppModuleBasic struct { + cdc Codec +} // Name returns the bank module's name. func (AppModuleBasic) Name() string { return ModuleName } @@ -78,9 +80,9 @@ type AppModule struct { } // NewAppModule creates a new AppModule object -func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper) AppModule { +func NewAppModule(cdc Codec, keeper Keeper, accountKeeper types.AccountKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, + AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, accountKeeper: accountKeeper, } @@ -153,8 +155,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { } // RegisterStoreDecoder registers a decoder for supply module's types -func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = simulation.NewDecodeStore(am.cdc) } // WeightedOperations returns the all the gov module operations with their respective weights. diff --git a/x/bank/simulation/decoder.go b/x/bank/simulation/decoder.go index 9e1be8895430..790807c771a3 100644 --- a/x/bank/simulation/decoder.go +++ b/x/bank/simulation/decoder.go @@ -6,20 +6,29 @@ import ( tmkv "github.com/tendermint/tendermint/libs/kv" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/x/bank/types" ) -// DecodeStore unmarshals the KVPair's values to the corresponding types. -func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string { - switch { - case bytes.Equal(kvA.Key[:1], types.SupplyKey): - var supplyA, supplyB types.Supply - cdc.MustUnmarshalBinaryBare(kvA.Value, &supplyA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &supplyB) - return fmt.Sprintf("%v\n%v", supplyB, supplyB) +// NewDecodeStore returns a function closure that unmarshals the KVPair's values +// to the corresponding types. +func NewDecodeStore(cdc types.Codec) func(kvA, kvB tmkv.Pair) string { + return func(kvA, kvB tmkv.Pair) string { + switch { + case bytes.Equal(kvA.Key[:1], types.SupplyKey): + supplyA, err := cdc.UnmarshalSupply(kvA.Value) + if err != nil { + panic(err) + } - default: - panic(fmt.Sprintf("unknown %s key %X (%s)", types.ModuleName, kvA.Key, kvA.Key)) + supplyB, err := cdc.UnmarshalSupply(kvB.Value) + if err != nil { + panic(err) + } + + return fmt.Sprintf("%v\n%v", supplyA, supplyB) + + default: + panic(fmt.Sprintf("unexpected %s key %X (%s)", types.ModuleName, kvA.Key, kvA.Key)) + } } } diff --git a/x/bank/simulation/decoder_test.go b/x/bank/simulation/decoder_test.go index f21949be524e..60cd01461f56 100644 --- a/x/bank/simulation/decoder_test.go +++ b/x/bank/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package simulation +package simulation_test import ( "fmt" @@ -7,25 +7,24 @@ import ( "github.com/stretchr/testify/require" tmkv "github.com/tendermint/tendermint/libs/kv" - "github.com/cosmos/cosmos-sdk/codec" + codecstd "github.com/cosmos/cosmos-sdk/codec/std" + "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank/simulation" "github.com/cosmos/cosmos-sdk/x/bank/types" ) -func makeTestCodec() (cdc *codec.Codec) { - cdc = codec.New() - sdk.RegisterCodec(cdc) - codec.RegisterCrypto(cdc) - types.RegisterCodec(cdc) - return -} func TestDecodeStore(t *testing.T) { - cdc := makeTestCodec() + cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics)) + dec := simulation.NewDecodeStore(cdc) totalSupply := types.NewSupply(sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000))) + supplyBz, err := cdc.MarshalSupply(totalSupply) + require.NoError(t, err) + kvPairs := tmkv.Pairs{ - tmkv.Pair{Key: types.SupplyKey, Value: cdc.MustMarshalBinaryBare(totalSupply)}, + tmkv.Pair{Key: types.SupplyKey, Value: supplyBz}, tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}}, } @@ -42,9 +41,9 @@ func TestDecodeStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name) } }) } From b63b33010c23a5749138512f7073d9ee428190b8 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Tue, 21 Apr 2020 13:30:25 -0400 Subject: [PATCH 4/6] migrate remaining modules --- simapp/app.go | 28 +++---- simapp/sim_test.go | 2 +- x/distribution/module.go | 14 ++-- x/distribution/simulation/decoder.go | 91 ++++++++++++----------- x/distribution/simulation/decoder_test.go | 23 +++--- x/gov/module.go | 9 ++- x/gov/simulation/decoder.go | 64 ++++++++-------- x/gov/simulation/decoder_test.go | 20 ++--- x/mint/module.go | 12 +-- x/mint/simulation/decoder.go | 23 +++--- x/mint/simulation/decoder_test.go | 19 ++--- x/slashing/module.go | 12 +-- x/slashing/simulation/decoder.go | 55 +++++++------- x/slashing/simulation/decoder_test.go | 19 ++--- x/staking/module.go | 12 +-- x/staking/simulation/decoder.go | 87 +++++++++++----------- x/staking/simulation/decoder_test.go | 11 ++- 17 files changed, 254 insertions(+), 247 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 958ba627af87..86830256f16b 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -261,15 +261,15 @@ func NewSimApp( // must be passed by reference here. app.mm = module.NewManager( genutil.NewAppModule(app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx), - auth.NewAppModule(app.AccountKeeper), - bank.NewAppModule(app.BankKeeper, app.AccountKeeper), + auth.NewAppModule(appCodec, app.AccountKeeper), + bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), capability.NewAppModule(*app.CapabilityKeeper), crisis.NewAppModule(&app.CrisisKeeper), - gov.NewAppModule(app.GovKeeper, app.AccountKeeper, app.BankKeeper), - mint.NewAppModule(app.MintKeeper, app.AccountKeeper), - slashing.NewAppModule(app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), - distr.NewAppModule(app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), - staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.BankKeeper), + gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper), + mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper), + slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), + distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), + staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper), upgrade.NewAppModule(app.UpgradeKeeper), evidence.NewAppModule(app.EvidenceKeeper), ibc.NewAppModule(app.IBCKeeper), @@ -302,13 +302,13 @@ func NewSimApp( // NOTE: this is not required apps that don't use the simulator for fuzz testing // transactions app.sm = module.NewSimulationManager( - auth.NewAppModule(app.AccountKeeper), - bank.NewAppModule(app.BankKeeper, app.AccountKeeper), - gov.NewAppModule(app.GovKeeper, app.AccountKeeper, app.BankKeeper), - mint.NewAppModule(app.MintKeeper, app.AccountKeeper), - staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.BankKeeper), - distr.NewAppModule(app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), - slashing.NewAppModule(app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), + auth.NewAppModule(appCodec, app.AccountKeeper), + bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), + gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper), + mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper), + staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper), + distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), + slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), params.NewAppModule(app.ParamsKeeper), ) diff --git a/simapp/sim_test.go b/simapp/sim_test.go index 81aa4a066747..08aa0e3c9593 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -165,7 +165,7 @@ func TestAppImportExport(t *testing.T) { require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare") fmt.Printf("compared %d different key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B) - require.Equal(t, len(failedKVAs), 0, GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, app.Codec(), failedKVAs, failedKVBs)) + require.Equal(t, len(failedKVAs), 0, GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, failedKVAs, failedKVBs)) } } diff --git a/x/distribution/module.go b/x/distribution/module.go index 8f3eb7ac0c0d..ee32db9231a8 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -28,7 +28,9 @@ var ( ) // AppModuleBasic defines the basic application module used by the distribution module. -type AppModuleBasic struct{} +type AppModuleBasic struct { + cdc codec.Marshaler +} // Name returns the distribution module's name. func (AppModuleBasic) Name() string { @@ -85,11 +87,11 @@ type AppModule struct { // NewAppModule creates a new AppModule object func NewAppModule( - keeper Keeper, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, - stakingKeeper stakingkeeper.Keeper, + cdc codec.Marshaler, keeper Keeper, accountKeeper types.AccountKeeper, + bankKeeper types.BankKeeper, stakingKeeper stakingkeeper.Keeper, ) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, + AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, accountKeeper: accountKeeper, bankKeeper: bankKeeper, @@ -175,8 +177,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { } // RegisterStoreDecoder registers a decoder for distribution module's types -func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = simulation.NewDecodeStore(am.cdc) } // WeightedOperations returns the all the gov module operations with their respective weights. diff --git a/x/distribution/simulation/decoder.go b/x/distribution/simulation/decoder.go index ce991efca431..0d5f6dba568e 100644 --- a/x/distribution/simulation/decoder.go +++ b/x/distribution/simulation/decoder.go @@ -11,58 +11,61 @@ import ( "github.com/cosmos/cosmos-sdk/x/distribution/types" ) -// DecodeStore unmarshals the KVPair's Value to the corresponding distribution type -func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string { - switch { - case bytes.Equal(kvA.Key[:1], types.FeePoolKey): - var feePoolA, feePoolB types.FeePool - cdc.MustUnmarshalBinaryBare(kvA.Value, &feePoolA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &feePoolB) - return fmt.Sprintf("%v\n%v", feePoolA, feePoolB) +// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's +// Value to the corresponding distribution type. +func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB tmkv.Pair) string { + return func(kvA, kvB tmkv.Pair) string { + switch { + case bytes.Equal(kvA.Key[:1], types.FeePoolKey): + var feePoolA, feePoolB types.FeePool + cdc.MustUnmarshalBinaryBare(kvA.Value, &feePoolA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &feePoolB) + return fmt.Sprintf("%v\n%v", feePoolA, feePoolB) - case bytes.Equal(kvA.Key[:1], types.ProposerKey): - return fmt.Sprintf("%v\n%v", sdk.ConsAddress(kvA.Value), sdk.ConsAddress(kvB.Value)) + case bytes.Equal(kvA.Key[:1], types.ProposerKey): + return fmt.Sprintf("%v\n%v", sdk.ConsAddress(kvA.Value), sdk.ConsAddress(kvB.Value)) - case bytes.Equal(kvA.Key[:1], types.ValidatorOutstandingRewardsPrefix): - var rewardsA, rewardsB types.ValidatorOutstandingRewards - cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB) - return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) + case bytes.Equal(kvA.Key[:1], types.ValidatorOutstandingRewardsPrefix): + var rewardsA, rewardsB types.ValidatorOutstandingRewards + cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB) + return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - case bytes.Equal(kvA.Key[:1], types.DelegatorWithdrawAddrPrefix): - return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value)) + case bytes.Equal(kvA.Key[:1], types.DelegatorWithdrawAddrPrefix): + return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value)) - case bytes.Equal(kvA.Key[:1], types.DelegatorStartingInfoPrefix): - var infoA, infoB types.DelegatorStartingInfo - cdc.MustUnmarshalBinaryBare(kvA.Value, &infoA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &infoB) - return fmt.Sprintf("%v\n%v", infoA, infoB) + case bytes.Equal(kvA.Key[:1], types.DelegatorStartingInfoPrefix): + var infoA, infoB types.DelegatorStartingInfo + cdc.MustUnmarshalBinaryBare(kvA.Value, &infoA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &infoB) + return fmt.Sprintf("%v\n%v", infoA, infoB) - case bytes.Equal(kvA.Key[:1], types.ValidatorHistoricalRewardsPrefix): - var rewardsA, rewardsB types.ValidatorHistoricalRewards - cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB) - return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) + case bytes.Equal(kvA.Key[:1], types.ValidatorHistoricalRewardsPrefix): + var rewardsA, rewardsB types.ValidatorHistoricalRewards + cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB) + return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - case bytes.Equal(kvA.Key[:1], types.ValidatorCurrentRewardsPrefix): - var rewardsA, rewardsB types.ValidatorCurrentRewards - cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB) - return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) + case bytes.Equal(kvA.Key[:1], types.ValidatorCurrentRewardsPrefix): + var rewardsA, rewardsB types.ValidatorCurrentRewards + cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB) + return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - case bytes.Equal(kvA.Key[:1], types.ValidatorAccumulatedCommissionPrefix): - var commissionA, commissionB types.ValidatorAccumulatedCommission - cdc.MustUnmarshalBinaryBare(kvA.Value, &commissionA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &commissionB) - return fmt.Sprintf("%v\n%v", commissionA, commissionB) + case bytes.Equal(kvA.Key[:1], types.ValidatorAccumulatedCommissionPrefix): + var commissionA, commissionB types.ValidatorAccumulatedCommission + cdc.MustUnmarshalBinaryBare(kvA.Value, &commissionA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &commissionB) + return fmt.Sprintf("%v\n%v", commissionA, commissionB) - case bytes.Equal(kvA.Key[:1], types.ValidatorSlashEventPrefix): - var eventA, eventB types.ValidatorSlashEvent - cdc.MustUnmarshalBinaryBare(kvA.Value, &eventA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &eventB) - return fmt.Sprintf("%v\n%v", eventA, eventB) + case bytes.Equal(kvA.Key[:1], types.ValidatorSlashEventPrefix): + var eventA, eventB types.ValidatorSlashEvent + cdc.MustUnmarshalBinaryBare(kvA.Value, &eventA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &eventB) + return fmt.Sprintf("%v\n%v", eventA, eventB) - default: - panic(fmt.Sprintf("invalid distribution key prefix %X", kvA.Key[:1])) + default: + panic(fmt.Sprintf("invalid distribution key prefix %X", kvA.Key[:1])) + } } } diff --git a/x/distribution/simulation/decoder_test.go b/x/distribution/simulation/decoder_test.go index 8eb1c00d44f1..4b2b0e31c4de 100644 --- a/x/distribution/simulation/decoder_test.go +++ b/x/distribution/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package simulation +package simulation_test import ( "fmt" @@ -9,8 +9,10 @@ import ( "github.com/tendermint/tendermint/crypto/ed25519" tmkv "github.com/tendermint/tendermint/libs/kv" - "github.com/cosmos/cosmos-sdk/codec" + codecstd "github.com/cosmos/cosmos-sdk/codec/std" + "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/distribution/simulation" "github.com/cosmos/cosmos-sdk/x/distribution/types" ) @@ -21,16 +23,9 @@ var ( consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes()) ) -func makeTestCodec() (cdc *codec.Codec) { - cdc = codec.New() - sdk.RegisterCodec(cdc) - codec.RegisterCrypto(cdc) - types.RegisterCodec(cdc) - return -} - func TestDecodeDistributionStore(t *testing.T) { - cdc := makeTestCodec() + cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics)) + dec := simulation.NewDecodeStore(cdc) decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.OneDec())} feePool := types.InitialFeePool() @@ -43,7 +38,7 @@ func TestDecodeDistributionStore(t *testing.T) { slashEvent := types.NewValidatorSlashEvent(10, sdk.OneDec()) kvPairs := tmkv.Pairs{ - tmkv.Pair{Key: types.FeePoolKey, Value: cdc.MustMarshalBinaryBare(feePool)}, + tmkv.Pair{Key: types.FeePoolKey, Value: cdc.MustMarshalBinaryBare(&feePool)}, tmkv.Pair{Key: types.ProposerKey, Value: consAddr1.Bytes()}, tmkv.Pair{Key: types.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryBare(outstanding)}, tmkv.Pair{Key: types.GetDelegatorWithdrawAddrKey(delAddr1), Value: delAddr1.Bytes()}, @@ -75,9 +70,9 @@ func TestDecodeDistributionStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name) } }) } diff --git a/x/gov/module.go b/x/gov/module.go index 4b53a38edad9..63fecf1440ce 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -32,6 +32,7 @@ var ( // AppModuleBasic defines the basic application module used by the gov module. type AppModuleBasic struct { + cdc Codec proposalHandlers []client.ProposalHandler // proposal handlers which live in governance cli and rest } @@ -106,9 +107,9 @@ type AppModule struct { } // NewAppModule creates a new AppModule object -func NewAppModule(keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper) AppModule { +func NewAppModule(cdc Codec, keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, + AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, accountKeeper: ak, bankKeeper: bk, @@ -192,8 +193,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { } // RegisterStoreDecoder registers a decoder for gov module's types -func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = simulation.NewDecodeStore(am.cdc) } // WeightedOperations returns the all the gov module operations with their respective weights. diff --git a/x/gov/simulation/decoder.go b/x/gov/simulation/decoder.go index c415117aa485..da04bd99fa35 100644 --- a/x/gov/simulation/decoder.go +++ b/x/gov/simulation/decoder.go @@ -7,39 +7,41 @@ import ( tmkv "github.com/tendermint/tendermint/libs/kv" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/x/gov/types" ) -// DecodeStore unmarshals the KVPair's Value to the corresponding gov type -func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string { - switch { - case bytes.Equal(kvA.Key[:1], types.ProposalsKeyPrefix): - var proposalA, proposalB types.Proposal - cdc.MustUnmarshalBinaryBare(kvA.Value, &proposalA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &proposalB) - return fmt.Sprintf("%v\n%v", proposalA, proposalB) - - case bytes.Equal(kvA.Key[:1], types.ActiveProposalQueuePrefix), - bytes.Equal(kvA.Key[:1], types.InactiveProposalQueuePrefix), - bytes.Equal(kvA.Key[:1], types.ProposalIDKey): - proposalIDA := binary.LittleEndian.Uint64(kvA.Value) - proposalIDB := binary.LittleEndian.Uint64(kvB.Value) - return fmt.Sprintf("proposalIDA: %d\nProposalIDB: %d", proposalIDA, proposalIDB) - - case bytes.Equal(kvA.Key[:1], types.DepositsKeyPrefix): - var depositA, depositB types.Deposit - cdc.MustUnmarshalBinaryBare(kvA.Value, &depositA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &depositB) - return fmt.Sprintf("%v\n%v", depositA, depositB) - - case bytes.Equal(kvA.Key[:1], types.VotesKeyPrefix): - var voteA, voteB types.Vote - cdc.MustUnmarshalBinaryBare(kvA.Value, &voteA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &voteB) - return fmt.Sprintf("%v\n%v", voteA, voteB) - - default: - panic(fmt.Sprintf("invalid governance key prefix %X", kvA.Key[:1])) +// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's +// Value to the corresponding gov type. +func NewDecodeStore(cdc types.Codec) func(kvA, kvB tmkv.Pair) string { + return func(kvA, kvB tmkv.Pair) string { + switch { + case bytes.Equal(kvA.Key[:1], types.ProposalsKeyPrefix): + var proposalA, proposalB types.Proposal + cdc.MustUnmarshalBinaryBare(kvA.Value, &proposalA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &proposalB) + return fmt.Sprintf("%v\n%v", proposalA, proposalB) + + case bytes.Equal(kvA.Key[:1], types.ActiveProposalQueuePrefix), + bytes.Equal(kvA.Key[:1], types.InactiveProposalQueuePrefix), + bytes.Equal(kvA.Key[:1], types.ProposalIDKey): + proposalIDA := binary.LittleEndian.Uint64(kvA.Value) + proposalIDB := binary.LittleEndian.Uint64(kvB.Value) + return fmt.Sprintf("proposalIDA: %d\nProposalIDB: %d", proposalIDA, proposalIDB) + + case bytes.Equal(kvA.Key[:1], types.DepositsKeyPrefix): + var depositA, depositB types.Deposit + cdc.MustUnmarshalBinaryBare(kvA.Value, &depositA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &depositB) + return fmt.Sprintf("%v\n%v", depositA, depositB) + + case bytes.Equal(kvA.Key[:1], types.VotesKeyPrefix): + var voteA, voteB types.Vote + cdc.MustUnmarshalBinaryBare(kvA.Value, &voteA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &voteB) + return fmt.Sprintf("%v\n%v", voteA, voteB) + + default: + panic(fmt.Sprintf("invalid governance key prefix %X", kvA.Key[:1])) + } } } diff --git a/x/gov/simulation/decoder_test.go b/x/gov/simulation/decoder_test.go index 5ae3cab53a60..b2cfaaba43f0 100644 --- a/x/gov/simulation/decoder_test.go +++ b/x/gov/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package simulation +package simulation_test import ( "encoding/binary" @@ -11,8 +11,9 @@ import ( "github.com/tendermint/tendermint/crypto/ed25519" tmkv "github.com/tendermint/tendermint/libs/kv" - "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/gov/simulation" "github.com/cosmos/cosmos-sdk/x/gov/types" ) @@ -21,16 +22,9 @@ var ( delAddr1 = sdk.AccAddress(delPk1.Address()) ) -func makeTestCodec() (cdc *codec.Codec) { - cdc = codec.New() - sdk.RegisterCodec(cdc) - codec.RegisterCrypto(cdc) - types.RegisterCodec(cdc) - return -} - func TestDecodeStore(t *testing.T) { - cdc := makeTestCodec() + cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics)) + dec := simulation.NewDecodeStore(cdc) endTime := time.Now().UTC() @@ -65,9 +59,9 @@ func TestDecodeStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name) } }) } diff --git a/x/mint/module.go b/x/mint/module.go index b3aba891af3c..34a75f991a6c 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -27,7 +27,9 @@ var ( ) // AppModuleBasic defines the basic application module used by the mint module. -type AppModuleBasic struct{} +type AppModuleBasic struct { + cdc codec.Marshaler +} var _ module.AppModuleBasic = AppModuleBasic{} @@ -79,9 +81,9 @@ type AppModule struct { } // NewAppModule creates a new AppModule object -func NewAppModule(keeper Keeper, ak types.AccountKeeper) AppModule { +func NewAppModule(cdc codec.Marshaler, keeper Keeper, ak types.AccountKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, + AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, authKeeper: ak, } @@ -159,8 +161,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { } // RegisterStoreDecoder registers a decoder for mint module's types. -func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = simulation.NewDecodeStore(am.cdc) } // WeightedOperations doesn't return any mint module operation. diff --git a/x/mint/simulation/decoder.go b/x/mint/simulation/decoder.go index 8ff2baeb0e19..49ca7a56fc54 100644 --- a/x/mint/simulation/decoder.go +++ b/x/mint/simulation/decoder.go @@ -10,15 +10,18 @@ import ( "github.com/cosmos/cosmos-sdk/x/mint/types" ) -// DecodeStore unmarshals the KVPair's Value to the corresponding mint type -func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string { - switch { - case bytes.Equal(kvA.Key, types.MinterKey): - var minterA, minterB types.Minter - cdc.MustUnmarshalBinaryBare(kvA.Value, &minterA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &minterB) - return fmt.Sprintf("%v\n%v", minterA, minterB) - default: - panic(fmt.Sprintf("invalid mint key %X", kvA.Key)) +// NewDecodeStore returns a decoder function closure that umarshals the KVPair's +// Value to the corresponding mint type. +func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB tmkv.Pair) string { + return func(kvA, kvB tmkv.Pair) string { + switch { + case bytes.Equal(kvA.Key, types.MinterKey): + var minterA, minterB types.Minter + cdc.MustUnmarshalBinaryBare(kvA.Value, &minterA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &minterB) + return fmt.Sprintf("%v\n%v", minterA, minterB) + default: + panic(fmt.Sprintf("invalid mint key %X", kvA.Key)) + } } } diff --git a/x/mint/simulation/decoder_test.go b/x/mint/simulation/decoder_test.go index 48dfa62bb384..ec0fe0bb62d3 100644 --- a/x/mint/simulation/decoder_test.go +++ b/x/mint/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package simulation +package simulation_test import ( "fmt" @@ -8,19 +8,16 @@ import ( tmkv "github.com/tendermint/tendermint/libs/kv" - "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/mint/simulation" "github.com/cosmos/cosmos-sdk/x/mint/types" ) -func makeTestCodec() (cdc *codec.Codec) { - cdc = codec.New() - sdk.RegisterCodec(cdc) - return -} - func TestDecodeStore(t *testing.T) { - cdc := makeTestCodec() + cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics)) + dec := simulation.NewDecodeStore(cdc) + minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15)) kvPairs := tmkv.Pairs{ @@ -40,9 +37,9 @@ func TestDecodeStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name) } }) } diff --git a/x/slashing/module.go b/x/slashing/module.go index 26876e506c19..79730152e54a 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -29,7 +29,9 @@ var ( ) // AppModuleBasic defines the basic application module used by the slashing module. -type AppModuleBasic struct{} +type AppModuleBasic struct { + cdc codec.Marshaler +} var _ module.AppModuleBasic = AppModuleBasic{} @@ -87,9 +89,9 @@ type AppModule struct { } // NewAppModule creates a new AppModule object -func NewAppModule(keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper, sk stakingkeeper.Keeper) AppModule { +func NewAppModule(cdc codec.Marshaler, keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper, sk stakingkeeper.Keeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, + AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, accountKeeper: ak, bankKeeper: bk, @@ -172,8 +174,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { } // RegisterStoreDecoder registers a decoder for slashing module's types -func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = simulation.NewDecodeStore(am.cdc) } // WeightedOperations returns the all the slashing module operations with their respective weights. diff --git a/x/slashing/simulation/decoder.go b/x/slashing/simulation/decoder.go index 9555737d889c..aebdecc4c755 100644 --- a/x/slashing/simulation/decoder.go +++ b/x/slashing/simulation/decoder.go @@ -5,7 +5,6 @@ import ( "fmt" gogotypes "github.com/gogo/protobuf/types" - "github.com/tendermint/tendermint/crypto" tmkv "github.com/tendermint/tendermint/libs/kv" "github.com/cosmos/cosmos-sdk/codec" @@ -13,30 +12,34 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing/types" ) -// DecodeStore unmarshals the KVPair's Value to the corresponding slashing type -func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string { - switch { - case bytes.Equal(kvA.Key[:1], types.ValidatorSigningInfoKey): - var infoA, infoB types.ValidatorSigningInfo - cdc.MustUnmarshalBinaryBare(kvA.Value, &infoA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &infoB) - return fmt.Sprintf("%v\n%v", infoA, infoB) - - case bytes.Equal(kvA.Key[:1], types.ValidatorMissedBlockBitArrayKey): - var missedA, missedB gogotypes.BoolValue - cdc.MustUnmarshalBinaryBare(kvA.Value, &missedA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &missedB) - return fmt.Sprintf("missedA: %v\nmissedB: %v", missedA.Value, missedB.Value) - - case bytes.Equal(kvA.Key[:1], types.AddrPubkeyRelationKey): - var pubKeyA, pubKeyB crypto.PubKey - cdc.MustUnmarshalBinaryBare(kvA.Value, &pubKeyA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &pubKeyB) - bechPKA := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKeyA) - bechPKB := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKeyB) - return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", bechPKA, bechPKB) - - default: - panic(fmt.Sprintf("invalid slashing key prefix %X", kvA.Key[:1])) +// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's +// Value to the corresponding slashing type. +func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB tmkv.Pair) string { + return func(kvA, kvB tmkv.Pair) string { + switch { + case bytes.Equal(kvA.Key[:1], types.ValidatorSigningInfoKey): + var infoA, infoB types.ValidatorSigningInfo + cdc.MustUnmarshalBinaryBare(kvA.Value, &infoA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &infoB) + return fmt.Sprintf("%v\n%v", infoA, infoB) + + case bytes.Equal(kvA.Key[:1], types.ValidatorMissedBlockBitArrayKey): + var missedA, missedB gogotypes.BoolValue + cdc.MustUnmarshalBinaryBare(kvA.Value, &missedA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &missedB) + return fmt.Sprintf("missedA: %v\nmissedB: %v", missedA.Value, missedB.Value) + + case bytes.Equal(kvA.Key[:1], types.AddrPubkeyRelationKey): + var pubKeyA, pubKeyB gogotypes.StringValue + cdc.MustUnmarshalBinaryBare(kvA.Value, &pubKeyA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &pubKeyB) + + bechPKA := sdk.MustGetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pubKeyA.Value) + bechPKB := sdk.MustGetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pubKeyB.Value) + return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", bechPKA, bechPKB) + + default: + panic(fmt.Sprintf("invalid slashing key prefix %X", kvA.Key[:1])) + } } } diff --git a/x/slashing/simulation/decoder_test.go b/x/slashing/simulation/decoder_test.go index 8d0c8b41e562..cba9a9a38747 100644 --- a/x/slashing/simulation/decoder_test.go +++ b/x/slashing/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package simulation +package simulation_test import ( "fmt" @@ -12,8 +12,10 @@ import ( "github.com/tendermint/tendermint/crypto/ed25519" tmkv "github.com/tendermint/tendermint/libs/kv" + "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/slashing/simulation" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) @@ -25,16 +27,9 @@ var ( consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes()) ) -func makeTestCodec() (cdc *codec.Codec) { - cdc = codec.New() - sdk.RegisterCodec(cdc) - codec.RegisterCrypto(cdc) - types.RegisterCodec(cdc) - return -} - func TestDecodeStore(t *testing.T) { - cdc := makeTestCodec() + cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics)) + dec := simulation.NewDecodeStore(cdc) info := types.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0) bechPK := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, delPk1) @@ -61,9 +56,9 @@ func TestDecodeStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]) }, tt.name) } }) } diff --git a/x/staking/module.go b/x/staking/module.go index 353868f7c57e..cb9dcbc71274 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -32,7 +32,9 @@ var ( ) // AppModuleBasic defines the basic application module used by the staking module. -type AppModuleBasic struct{} +type AppModuleBasic struct { + cdc codec.Marshaler +} var _ module.AppModuleBasic = AppModuleBasic{} @@ -110,9 +112,9 @@ type AppModule struct { } // NewAppModule creates a new AppModule object -func NewAppModule(keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper) AppModule { +func NewAppModule(cdc codec.Marshaler, keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, + AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, accountKeeper: ak, bankKeeper: bk, @@ -195,8 +197,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { } // RegisterStoreDecoder registers a decoder for staking module's types -func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = simulation.NewDecodeStore(am.cdc) } // WeightedOperations returns the all the staking module operations with their respective weights. diff --git a/x/staking/simulation/decoder.go b/x/staking/simulation/decoder.go index 86f1cecc9e00..1445a637a691 100644 --- a/x/staking/simulation/decoder.go +++ b/x/staking/simulation/decoder.go @@ -11,47 +11,50 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) -// DecodeStore unmarshals the KVPair's Value to the corresponding staking type -func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string { - switch { - case bytes.Equal(kvA.Key[:1], types.LastTotalPowerKey): - var powerA, powerB sdk.Int - cdc.MustUnmarshalBinaryBare(kvA.Value, &powerA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &powerB) - return fmt.Sprintf("%v\n%v", powerA, powerB) - - case bytes.Equal(kvA.Key[:1], types.ValidatorsKey): - var validatorA, validatorB types.Validator - cdc.MustUnmarshalBinaryBare(kvA.Value, &validatorA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &validatorB) - return fmt.Sprintf("%v\n%v", validatorA, validatorB) - - case bytes.Equal(kvA.Key[:1], types.LastValidatorPowerKey), - bytes.Equal(kvA.Key[:1], types.ValidatorsByConsAddrKey), - bytes.Equal(kvA.Key[:1], types.ValidatorsByPowerIndexKey): - return fmt.Sprintf("%v\n%v", sdk.ValAddress(kvA.Value), sdk.ValAddress(kvB.Value)) - - case bytes.Equal(kvA.Key[:1], types.DelegationKey): - var delegationA, delegationB types.Delegation - cdc.MustUnmarshalBinaryBare(kvA.Value, &delegationA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &delegationB) - return fmt.Sprintf("%v\n%v", delegationA, delegationB) - - case bytes.Equal(kvA.Key[:1], types.UnbondingDelegationKey), - bytes.Equal(kvA.Key[:1], types.UnbondingDelegationByValIndexKey): - var ubdA, ubdB types.UnbondingDelegation - cdc.MustUnmarshalBinaryBare(kvA.Value, &ubdA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &ubdB) - return fmt.Sprintf("%v\n%v", ubdA, ubdB) - - case bytes.Equal(kvA.Key[:1], types.RedelegationKey), - bytes.Equal(kvA.Key[:1], types.RedelegationByValSrcIndexKey): - var redA, redB types.Redelegation - cdc.MustUnmarshalBinaryBare(kvA.Value, &redA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &redB) - return fmt.Sprintf("%v\n%v", redA, redB) - - default: - panic(fmt.Sprintf("invalid staking key prefix %X", kvA.Key[:1])) +// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's +// Value to the corresponding staking type. +func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB tmkv.Pair) string { + return func(kvA, kvB tmkv.Pair) string { + switch { + case bytes.Equal(kvA.Key[:1], types.LastTotalPowerKey): + var powerA, powerB sdk.IntProto + cdc.MustUnmarshalBinaryBare(kvA.Value, &powerA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &powerB) + return fmt.Sprintf("%v\n%v", powerA, powerB) + + case bytes.Equal(kvA.Key[:1], types.ValidatorsKey): + var validatorA, validatorB types.Validator + cdc.MustUnmarshalBinaryBare(kvA.Value, &validatorA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &validatorB) + return fmt.Sprintf("%v\n%v", validatorA, validatorB) + + case bytes.Equal(kvA.Key[:1], types.LastValidatorPowerKey), + bytes.Equal(kvA.Key[:1], types.ValidatorsByConsAddrKey), + bytes.Equal(kvA.Key[:1], types.ValidatorsByPowerIndexKey): + return fmt.Sprintf("%v\n%v", sdk.ValAddress(kvA.Value), sdk.ValAddress(kvB.Value)) + + case bytes.Equal(kvA.Key[:1], types.DelegationKey): + var delegationA, delegationB types.Delegation + cdc.MustUnmarshalBinaryBare(kvA.Value, &delegationA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &delegationB) + return fmt.Sprintf("%v\n%v", delegationA, delegationB) + + case bytes.Equal(kvA.Key[:1], types.UnbondingDelegationKey), + bytes.Equal(kvA.Key[:1], types.UnbondingDelegationByValIndexKey): + var ubdA, ubdB types.UnbondingDelegation + cdc.MustUnmarshalBinaryBare(kvA.Value, &ubdA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &ubdB) + return fmt.Sprintf("%v\n%v", ubdA, ubdB) + + case bytes.Equal(kvA.Key[:1], types.RedelegationKey), + bytes.Equal(kvA.Key[:1], types.RedelegationByValSrcIndexKey): + var redA, redB types.Redelegation + cdc.MustUnmarshalBinaryBare(kvA.Value, &redA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &redB) + return fmt.Sprintf("%v\n%v", redA, redB) + + default: + panic(fmt.Sprintf("invalid staking key prefix %X", kvA.Key[:1])) + } } } diff --git a/x/staking/simulation/decoder_test.go b/x/staking/simulation/decoder_test.go index 40dd39710e84..e59fc09e4a32 100644 --- a/x/staking/simulation/decoder_test.go +++ b/x/staking/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package simulation +package simulation_test import ( "fmt" @@ -11,7 +11,9 @@ import ( tmkv "github.com/tendermint/tendermint/libs/kv" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/staking/simulation" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -30,7 +32,8 @@ func makeTestCodec() (cdc *codec.Codec) { } func TestDecodeStore(t *testing.T) { - cdc := makeTestCodec() + cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics)) + dec := simulation.NewDecodeStore(cdc) bondTime := time.Now().UTC() @@ -66,9 +69,9 @@ func TestDecodeStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]) }, tt.name) } }) } From a7d6e8f5e8f775fedd8e9752324720d3baa2d147 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Tue, 21 Apr 2020 13:34:32 -0400 Subject: [PATCH 5/6] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b45d148567ef..d707a18b6d82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ older clients. ### API Breaking Changes +* (modules) [\#5664](https://github.com/cosmos/cosmos-sdk/pull/5664) Remove amino `Codec` from simulation `StoreDecoder`, which now returns a function closure in order to unmarshal the key-value pairs. * (x/auth) [\#6029](https://github.com/cosmos/cosmos-sdk/pull/6029) Module accounts have been moved from `x/supply` to `x/auth`. * (x/supply) [\#6010](https://github.com/cosmos/cosmos-sdk/pull/6010) All `x/supply` types and APIs have been moved to `x/bank`. * (baseapp) [\#5865](https://github.com/cosmos/cosmos-sdk/pull/5865) The `SimulationResponse` returned from tx simulation is now JSON encoded instead of Amino binary. From 40d5700d1e20c952479d3a8a62ce68d5e53d5df6 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Tue, 21 Apr 2020 15:26:22 -0400 Subject: [PATCH 6/6] fix tests --- Makefile | 2 +- x/auth/simulation/decoder_test.go | 4 ++-- x/distribution/simulation/decoder_test.go | 12 ++++++------ x/gov/simulation/decoder.go | 11 ++++++++--- x/gov/simulation/decoder_test.go | 10 +++++++--- x/mint/simulation/decoder_test.go | 3 ++- x/slashing/simulation/decoder.go | 6 +----- x/slashing/simulation/decoder_test.go | 10 +++++----- x/staking/simulation/decoder_test.go | 13 +++++++------ 9 files changed, 39 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 9a1b851bb840..367ae9d5845e 100644 --- a/Makefile +++ b/Makefile @@ -119,7 +119,7 @@ test-ledger: test-ledger-mock @go test -mod=readonly -v `go list github.com/cosmos/cosmos-sdk/crypto` -tags='cgo ledger' test-unit: - @VERSION=$(VERSION) go test -mod=readonly $(PACKAGES_NOSIMULATION) -tags='ledger test_ledger_mock' + @VERSION=$(VERSION) go test -mod=readonly ./... -tags='ledger test_ledger_mock' test-race: @VERSION=$(VERSION) go test -mod=readonly -race $(PACKAGES_NOSIMULATION) diff --git a/x/auth/simulation/decoder_test.go b/x/auth/simulation/decoder_test.go index 77c27abfde74..0ed44e92f73f 100644 --- a/x/auth/simulation/decoder_test.go +++ b/x/auth/simulation/decoder_test.go @@ -29,7 +29,7 @@ func TestDecodeStore(t *testing.T) { accBz, err := cdc.MarshalAccount(acc) require.NoError(t, err) - globalAccNumber := &gogotypes.UInt64Value{Value: 10} + globalAccNumber := gogotypes.UInt64Value{Value: 10} kvPairs := tmkv.Pairs{ tmkv.Pair{ @@ -38,7 +38,7 @@ func TestDecodeStore(t *testing.T) { }, tmkv.Pair{ Key: types.GlobalAccountNumberKey, - Value: cdc.MustMarshalBinaryBare(globalAccNumber), + Value: cdc.MustMarshalBinaryBare(&globalAccNumber), }, tmkv.Pair{ Key: []byte{0x99}, diff --git a/x/distribution/simulation/decoder_test.go b/x/distribution/simulation/decoder_test.go index 4b2b0e31c4de..a9d4859e4ca0 100644 --- a/x/distribution/simulation/decoder_test.go +++ b/x/distribution/simulation/decoder_test.go @@ -40,13 +40,13 @@ func TestDecodeDistributionStore(t *testing.T) { kvPairs := tmkv.Pairs{ tmkv.Pair{Key: types.FeePoolKey, Value: cdc.MustMarshalBinaryBare(&feePool)}, tmkv.Pair{Key: types.ProposerKey, Value: consAddr1.Bytes()}, - tmkv.Pair{Key: types.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryBare(outstanding)}, + tmkv.Pair{Key: types.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryBare(&outstanding)}, tmkv.Pair{Key: types.GetDelegatorWithdrawAddrKey(delAddr1), Value: delAddr1.Bytes()}, - tmkv.Pair{Key: types.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshalBinaryBare(info)}, - tmkv.Pair{Key: types.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshalBinaryBare(historicalRewards)}, - tmkv.Pair{Key: types.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryBare(currentRewards)}, - tmkv.Pair{Key: types.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshalBinaryBare(commission)}, - tmkv.Pair{Key: types.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshalBinaryBare(slashEvent)}, + tmkv.Pair{Key: types.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshalBinaryBare(&info)}, + tmkv.Pair{Key: types.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshalBinaryBare(&historicalRewards)}, + tmkv.Pair{Key: types.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryBare(¤tRewards)}, + tmkv.Pair{Key: types.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshalBinaryBare(&commission)}, + tmkv.Pair{Key: types.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshalBinaryBare(&slashEvent)}, tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}}, } diff --git a/x/gov/simulation/decoder.go b/x/gov/simulation/decoder.go index da04bd99fa35..1faeb9145d33 100644 --- a/x/gov/simulation/decoder.go +++ b/x/gov/simulation/decoder.go @@ -16,9 +16,14 @@ func NewDecodeStore(cdc types.Codec) func(kvA, kvB tmkv.Pair) string { return func(kvA, kvB tmkv.Pair) string { switch { case bytes.Equal(kvA.Key[:1], types.ProposalsKeyPrefix): - var proposalA, proposalB types.Proposal - cdc.MustUnmarshalBinaryBare(kvA.Value, &proposalA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &proposalB) + proposalA, err := cdc.UnmarshalProposal(kvA.Value) + if err != nil { + panic(err) + } + proposalB, err := cdc.UnmarshalProposal(kvB.Value) + if err != nil { + panic(err) + } return fmt.Sprintf("%v\n%v", proposalA, proposalB) case bytes.Equal(kvA.Key[:1], types.ActiveProposalQueuePrefix), diff --git a/x/gov/simulation/decoder_test.go b/x/gov/simulation/decoder_test.go index b2cfaaba43f0..64cb81ac1706 100644 --- a/x/gov/simulation/decoder_test.go +++ b/x/gov/simulation/decoder_test.go @@ -11,6 +11,7 @@ import ( "github.com/tendermint/tendermint/crypto/ed25519" tmkv "github.com/tendermint/tendermint/libs/kv" + codecstd "github.com/cosmos/cosmos-sdk/codec/std" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/gov/simulation" @@ -35,11 +36,14 @@ func TestDecodeStore(t *testing.T) { deposit := types.NewDeposit(1, delAddr1, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt()))) vote := types.NewVote(1, delAddr1, types.OptionYes) + proposalBz, err := cdc.MarshalProposal(proposal) + require.NoError(t, err) + kvPairs := tmkv.Pairs{ - tmkv.Pair{Key: types.ProposalKey(1), Value: cdc.MustMarshalBinaryBare(proposal)}, + tmkv.Pair{Key: types.ProposalKey(1), Value: proposalBz}, tmkv.Pair{Key: types.InactiveProposalQueueKey(1, endTime), Value: proposalIDBz}, - tmkv.Pair{Key: types.DepositKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(deposit)}, - tmkv.Pair{Key: types.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(vote)}, + tmkv.Pair{Key: types.DepositKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(&deposit)}, + tmkv.Pair{Key: types.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(&vote)}, tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}}, } diff --git a/x/mint/simulation/decoder_test.go b/x/mint/simulation/decoder_test.go index ec0fe0bb62d3..07cd5bd77cc1 100644 --- a/x/mint/simulation/decoder_test.go +++ b/x/mint/simulation/decoder_test.go @@ -8,6 +8,7 @@ import ( tmkv "github.com/tendermint/tendermint/libs/kv" + codecstd "github.com/cosmos/cosmos-sdk/codec/std" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/mint/simulation" @@ -21,7 +22,7 @@ func TestDecodeStore(t *testing.T) { minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15)) kvPairs := tmkv.Pairs{ - tmkv.Pair{Key: types.MinterKey, Value: cdc.MustMarshalBinaryBare(minter)}, + tmkv.Pair{Key: types.MinterKey, Value: cdc.MustMarshalBinaryBare(&minter)}, tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}}, } tests := []struct { diff --git a/x/slashing/simulation/decoder.go b/x/slashing/simulation/decoder.go index aebdecc4c755..4149652a54af 100644 --- a/x/slashing/simulation/decoder.go +++ b/x/slashing/simulation/decoder.go @@ -8,7 +8,6 @@ import ( tmkv "github.com/tendermint/tendermint/libs/kv" "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) @@ -33,10 +32,7 @@ func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB tmkv.Pair) string { var pubKeyA, pubKeyB gogotypes.StringValue cdc.MustUnmarshalBinaryBare(kvA.Value, &pubKeyA) cdc.MustUnmarshalBinaryBare(kvB.Value, &pubKeyB) - - bechPKA := sdk.MustGetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pubKeyA.Value) - bechPKB := sdk.MustGetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pubKeyB.Value) - return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", bechPKA, bechPKB) + return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", pubKeyA.Value, pubKeyB.Value) default: panic(fmt.Sprintf("invalid slashing key prefix %X", kvA.Key[:1])) diff --git a/x/slashing/simulation/decoder_test.go b/x/slashing/simulation/decoder_test.go index cba9a9a38747..d9af238ef5a8 100644 --- a/x/slashing/simulation/decoder_test.go +++ b/x/slashing/simulation/decoder_test.go @@ -12,8 +12,8 @@ import ( "github.com/tendermint/tendermint/crypto/ed25519" tmkv "github.com/tendermint/tendermint/libs/kv" + codecstd "github.com/cosmos/cosmos-sdk/codec/std" "github.com/cosmos/cosmos-sdk/simapp" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/simulation" "github.com/cosmos/cosmos-sdk/x/slashing/types" @@ -32,13 +32,13 @@ func TestDecodeStore(t *testing.T) { dec := simulation.NewDecodeStore(cdc) info := types.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0) - bechPK := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, delPk1) + bechPK := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, delPk1) missed := gogotypes.BoolValue{Value: true} kvPairs := tmkv.Pairs{ - tmkv.Pair{Key: types.GetValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryBare(info)}, + tmkv.Pair{Key: types.GetValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryBare(&info)}, tmkv.Pair{Key: types.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryBare(&missed)}, - tmkv.Pair{Key: types.GetAddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryBare(delPk1)}, + tmkv.Pair{Key: types.GetAddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryBare(&gogotypes.StringValue{Value: bechPK})}, tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}}, } @@ -58,7 +58,7 @@ func TestDecodeStore(t *testing.T) { case len(tests) - 1: require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]) }, tt.name) + require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name) } }) } diff --git a/x/staking/simulation/decoder_test.go b/x/staking/simulation/decoder_test.go index e59fc09e4a32..01979b501578 100644 --- a/x/staking/simulation/decoder_test.go +++ b/x/staking/simulation/decoder_test.go @@ -11,6 +11,7 @@ import ( tmkv "github.com/tendermint/tendermint/libs/kv" "github.com/cosmos/cosmos-sdk/codec" + codecstd "github.com/cosmos/cosmos-sdk/codec/std" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/staking/simulation" @@ -43,12 +44,12 @@ func TestDecodeStore(t *testing.T) { red := types.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, sdk.OneInt(), sdk.OneDec()) kvPairs := tmkv.Pairs{ - tmkv.Pair{Key: types.LastTotalPowerKey, Value: cdc.MustMarshalBinaryBare(sdk.OneInt())}, - tmkv.Pair{Key: types.GetValidatorKey(valAddr1), Value: cdc.MustMarshalBinaryBare(val)}, + tmkv.Pair{Key: types.LastTotalPowerKey, Value: cdc.MustMarshalBinaryBare(&sdk.IntProto{Int: sdk.OneInt()})}, + tmkv.Pair{Key: types.GetValidatorKey(valAddr1), Value: cdc.MustMarshalBinaryBare(&val)}, tmkv.Pair{Key: types.LastValidatorPowerKey, Value: valAddr1.Bytes()}, - tmkv.Pair{Key: types.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(del)}, - tmkv.Pair{Key: types.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(ubd)}, - tmkv.Pair{Key: types.GetREDKey(delAddr1, valAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(red)}, + tmkv.Pair{Key: types.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(&del)}, + tmkv.Pair{Key: types.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(&ubd)}, + tmkv.Pair{Key: types.GetREDKey(delAddr1, valAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(&red)}, tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}}, } @@ -71,7 +72,7 @@ func TestDecodeStore(t *testing.T) { case len(tests) - 1: require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]) }, tt.name) + require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name) } }) }