From 3ee975c57b6febac693919dba94967ccd21851e2 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Tue, 21 Apr 2020 17:33:56 -0400 Subject: [PATCH] Merge PR #6037: refactor simulation decoder --- CHANGELOG.md | 1 + Makefile | 2 +- simapp/app.go | 28 +++---- simapp/sim_test.go | 2 +- simapp/utils.go | 7 +- simapp/utils_test.go | 5 +- types/store.go | 3 +- x/auth/module.go | 12 +-- x/auth/simulation/decoder.go | 44 ++++++----- x/auth/simulation/decoder_test.go | 44 ++++++----- x/bank/module.go | 12 +-- x/bank/simulation/decoder.go | 31 +++++--- x/bank/simulation/decoder_test.go | 25 +++---- x/distribution/module.go | 14 ++-- x/distribution/simulation/decoder.go | 91 ++++++++++++----------- x/distribution/simulation/decoder_test.go | 35 ++++----- x/gov/module.go | 9 ++- x/gov/simulation/decoder.go | 61 ++++++++------- x/gov/simulation/decoder_test.go | 30 ++++---- x/mint/module.go | 12 +-- x/mint/simulation/decoder.go | 23 +++--- x/mint/simulation/decoder_test.go | 22 +++--- x/slashing/module.go | 12 +-- x/slashing/simulation/decoder.go | 47 ++++++------ x/slashing/simulation/decoder_test.go | 27 +++---- x/staking/module.go | 12 +-- x/staking/simulation/decoder.go | 75 ++++++++++--------- x/staking/simulation/decoder_test.go | 22 +++--- 28 files changed, 375 insertions(+), 333 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 927be5cfc6..0912183045 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. diff --git a/Makefile b/Makefile index 9a1b851bb8..367ae9d584 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/simapp/app.go b/simapp/app.go index 958ba627af..86830256f1 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 81aa4a0667..08aa0e3c95 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/simapp/utils.go b/simapp/utils.go index 8cd3822722..1b89e4e02a 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 729ceddc28..c1f36e3c75 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 884556cc6b..dd8bb17365 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(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 3c3a9acf0c..2f425d6459 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 7d5d0d7b52..e14670a36d 100644 --- a/x/auth/simulation/decoder.go +++ b/x/auth/simulation/decoder.go @@ -4,29 +4,39 @@ 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 { - switch { - case bytes.Equal(kvA.Key[:1], types.AddressStoreKeyPrefix): - var accA, accB exported.Account - cdc.MustUnmarshalBinaryBare(kvA.Value, &accA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &accB) - return fmt.Sprintf("%v\n%v", accA, accB) +// 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) + } - case bytes.Equal(kvA.Key, types.GlobalAccountNumberKey): - var globalAccNumberA, globalAccNumberB uint64 - cdc.MustUnmarshalBinaryBare(kvA.Value, &globalAccNumberA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &globalAccNumberB) - return fmt.Sprintf("GlobalAccNumberA: %d\nGlobalAccNumberB: %d", globalAccNumberA, globalAccNumberB) + accB, err := cdc.UnmarshalAccount(kvB.Value) + if err != nil { + panic(err) + } - default: - panic(fmt.Sprintf("invalid account key %X", 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("unexpected %s key %X (%s)", types.ModuleName, kvA.Key, kvA.Key)) + } } } diff --git a/x/auth/simulation/decoder_test.go b/x/auth/simulation/decoder_test.go index 729ca9d793..0ed44e92f7 100644 --- a/x/auth/simulation/decoder_test.go +++ b/x/auth/simulation/decoder_test.go @@ -1,16 +1,18 @@ -package simulation +package simulation_test 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" - "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/simulation" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -19,23 +21,29 @@ 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) + 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 @@ -51,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/bank/module.go b/x/bank/module.go index d4f547d293..1cc4b7fa4d 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 9e1be88954..790807c771 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 f21949be52..60cd01461f 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) } }) } diff --git a/x/distribution/module.go b/x/distribution/module.go index 8f3eb7ac0c..ee32db9231 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 ce991efca4..0d5f6dba56 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 8eb1c00d44..a9d4859e4c 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,15 +38,15 @@ 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.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}}, } @@ -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 4b53a38eda..63fecf1440 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 c415117aa4..1faeb9145d 100644 --- a/x/gov/simulation/decoder.go +++ b/x/gov/simulation/decoder.go @@ -7,39 +7,46 @@ 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) +// 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): + 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), - 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.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.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) + 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])) + 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 5ae3cab53a..64cb81ac17 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,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/gov/simulation" "github.com/cosmos/cosmos-sdk/x/gov/types" ) @@ -21,16 +23,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() @@ -41,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}}, } @@ -65,9 +63,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 b3aba891af..34a75f991a 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 8ff2baeb0e..49ca7a56fc 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 48dfa62bb3..07cd5bd77c 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,23 +8,21 @@ 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/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{ - 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 { @@ -40,9 +38,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 26876e506c..79730152e5 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 9555737d88..4149652a54 100644 --- a/x/slashing/simulation/decoder.go +++ b/x/slashing/simulation/decoder.go @@ -5,38 +5,37 @@ 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" - sdk "github.com/cosmos/cosmos-sdk/types" "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) +// 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.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) + case bytes.Equal(kvA.Key[:1], types.AddrPubkeyRelationKey): + var pubKeyA, pubKeyB gogotypes.StringValue + cdc.MustUnmarshalBinaryBare(kvA.Value, &pubKeyA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &pubKeyB) + return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", pubKeyA.Value, pubKeyB.Value) - default: - panic(fmt.Sprintf("invalid slashing key prefix %X", kvA.Key[:1])) + 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 8d0c8b41e5..d9af238ef5 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/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/slashing/simulation" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) @@ -25,25 +27,18 @@ 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) + 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}}, } @@ -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 353868f7c5..cb9dcbc712 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 86f1cecc9e..1445a637a6 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) +// 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.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.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.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.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) + 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])) + 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 40dd39710e..01979b5015 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,10 @@ 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" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -30,7 +33,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() @@ -40,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}}, } @@ -66,9 +70,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) } }) }