diff --git a/x/distribution/module.go b/x/distribution/module.go index eb7de978c6d1..d8655be93370 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -169,7 +169,7 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) { // RegisterStoreDecoder registers a decoder for distribution module's types func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { - sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc) + sdr[types.StoreKey] = simtypes.NewStoreDecoderFuncFromCollectionsSchema(am.keeper.Schema) } // ProposalMsgsX returns msgs used for governance proposals for simulations. diff --git a/x/distribution/simulation/decoder.go b/x/distribution/simulation/decoder.go deleted file mode 100644 index 0373ce40d87f..000000000000 --- a/x/distribution/simulation/decoder.go +++ /dev/null @@ -1,68 +0,0 @@ -package simulation - -import ( - "bytes" - "fmt" - - "cosmossdk.io/x/distribution/types" - - "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/kv" -) - -// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's -// Value to the corresponding distribution type. -func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { - return func(kvA, kvB kv.Pair) string { - switch { - case bytes.Equal(kvA.Key[:1], types.FeePoolKey): - var feePoolA, feePoolB types.FeePool - cdc.MustUnmarshal(kvA.Value, &feePoolA) - cdc.MustUnmarshal(kvB.Value, &feePoolB) - return fmt.Sprintf("%v\n%v", feePoolA, feePoolB) - - case bytes.Equal(kvA.Key[:1], types.ValidatorOutstandingRewardsPrefix): - var rewardsA, rewardsB types.ValidatorOutstandingRewards - cdc.MustUnmarshal(kvA.Value, &rewardsA) - cdc.MustUnmarshal(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.DelegatorStartingInfoPrefix): - var infoA, infoB types.DelegatorStartingInfo - cdc.MustUnmarshal(kvA.Value, &infoA) - cdc.MustUnmarshal(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.MustUnmarshal(kvA.Value, &rewardsA) - cdc.MustUnmarshal(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.MustUnmarshal(kvA.Value, &rewardsA) - cdc.MustUnmarshal(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.MustUnmarshal(kvA.Value, &commissionA) - cdc.MustUnmarshal(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.MustUnmarshal(kvA.Value, &eventA) - cdc.MustUnmarshal(kvB.Value, &eventB) - return fmt.Sprintf("%v\n%v", eventA, eventB) - - 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 deleted file mode 100644 index 52b4d1b4886f..000000000000 --- a/x/distribution/simulation/decoder_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package simulation_test - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/require" - - "cosmossdk.io/math" - "cosmossdk.io/x/distribution" - "cosmossdk.io/x/distribution/simulation" - "cosmossdk.io/x/distribution/types" - - codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/kv" - moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" -) - -var ( - delPk1 = ed25519.GenPrivKey().PubKey() - valAddr1 = sdk.ValAddress(delPk1.Address()) -) - -func TestDecodeDistributionStore(t *testing.T) { - encodingConfig := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) - cdc := encodingConfig.Codec - - dec := simulation.NewDecodeStore(cdc) - - decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, math.LegacyOneDec())} - feePool := types.InitialFeePool() - feePool.DecimalPool = decCoins - slashEvent := types.NewValidatorSlashEvent(10, math.LegacyOneDec()) - - kvPairs := kv.Pairs{ - Pairs: []kv.Pair{ - {Key: types.FeePoolKey, Value: cdc.MustMarshal(&feePool)}, - {Key: types.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshal(&slashEvent)}, - {Key: []byte{0x99}, Value: []byte{0x99}}, - }, - } - - tests := []struct { - name string - expectedLog string - }{ - {"FeePool", fmt.Sprintf("%v\n%v", feePool, feePool)}, - {"ValidatorSlashEvent", fmt.Sprintf("%v\n%v", slashEvent, slashEvent)}, - {"other", ""}, - } - for i, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - switch i { - case len(tests) - 1: - require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name) - default: - require.Equal(t, tt.expectedLog, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.name) - } - }) - } -}