From 84fa36452a475b93d9f92f95e83cd3480486ab20 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Tue, 30 Jul 2019 17:42:33 +0200 Subject: [PATCH 01/18] move store decoders to modules --- simapp/utils.go | 232 ++------------------ simapp/utils_test.go | 268 ------------------------ x/auth/simulation/store.go | 31 +++ x/auth/simulation/store_test.go | 63 ++++++ x/distribution/simulation/store.go | 69 ++++++ x/distribution/simulation/store_test.go | 85 ++++++++ x/gov/simulation/store.go | 46 ++++ x/gov/simulation/store_test.go | 77 +++++++ x/mint/simulation/store.go | 25 +++ x/mint/simulation/store_test.go | 50 +++++ x/slashing/simulation/store.go | 42 ++++ x/slashing/simulation/store_test.go | 67 ++++++ x/staking/simulation/store.go | 58 +++++ x/staking/simulation/store_test.go | 76 +++++++ x/supply/simulation/store.go | 25 +++ x/supply/simulation/store_test.go | 53 +++++ 16 files changed, 781 insertions(+), 486 deletions(-) create mode 100644 x/auth/simulation/store.go create mode 100644 x/auth/simulation/store_test.go create mode 100644 x/distribution/simulation/store.go create mode 100644 x/distribution/simulation/store_test.go create mode 100644 x/gov/simulation/store.go create mode 100644 x/gov/simulation/store_test.go create mode 100644 x/mint/simulation/store.go create mode 100644 x/mint/simulation/store_test.go create mode 100644 x/slashing/simulation/store.go create mode 100644 x/slashing/simulation/store_test.go create mode 100644 x/staking/simulation/store.go create mode 100644 x/staking/simulation/store_test.go create mode 100644 x/supply/simulation/store.go create mode 100644 x/supply/simulation/store_test.go diff --git a/simapp/utils.go b/simapp/utils.go index b4f14db26a79..9bce23138481 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -2,8 +2,6 @@ package simapp import ( - "bytes" - "encoding/binary" "encoding/json" "fmt" "io" @@ -11,8 +9,6 @@ import ( "math/rand" "time" - "github.com/tendermint/tendermint/crypto" - "github.com/tendermint/tendermint/crypto/secp256k1" cmn "github.com/tendermint/tendermint/libs/common" "github.com/tendermint/tendermint/libs/log" tmtypes "github.com/tendermint/tendermint/types" @@ -22,15 +18,21 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/bank" + authsim "github.com/cosmos/cosmos-sdk/x/auth/simulation" "github.com/cosmos/cosmos-sdk/x/distribution" + distributionsim "github.com/cosmos/cosmos-sdk/x/distribution/simulation" "github.com/cosmos/cosmos-sdk/x/genaccounts" "github.com/cosmos/cosmos-sdk/x/gov" + govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation" "github.com/cosmos/cosmos-sdk/x/mint" + mintsim "github.com/cosmos/cosmos-sdk/x/mint/simulation" "github.com/cosmos/cosmos-sdk/x/simulation" "github.com/cosmos/cosmos-sdk/x/slashing" + slashingsim "github.com/cosmos/cosmos-sdk/x/slashing/simulation" "github.com/cosmos/cosmos-sdk/x/staking" + stakingsim "github.com/cosmos/cosmos-sdk/x/staking/simulation" "github.com/cosmos/cosmos-sdk/x/supply" + supplysim "github.com/cosmos/cosmos-sdk/x/supply/simulation" ) // List of available flags for the simulator @@ -513,19 +515,19 @@ func GetSimulationLog(storeName string, cdcA, cdcB *codec.Codec, kvs []cmn.KVPai switch storeName { case auth.StoreKey: - log += DecodeAccountStore(cdcA, cdcB, kvA, kvB) + log += authsim.DecodeStore(cdcA, cdcB, kvA, kvB) case mint.StoreKey: - log += DecodeMintStore(cdcA, cdcB, kvA, kvB) + log += mintsim.DecodeStore(cdcA, cdcB, kvA, kvB) case staking.StoreKey: - log += DecodeStakingStore(cdcA, cdcB, kvA, kvB) + log += stakingsim.DecodeStore(cdcA, cdcB, kvA, kvB) case slashing.StoreKey: - log += DecodeSlashingStore(cdcA, cdcB, kvA, kvB) + log += slashingsim.DecodeStore(cdcA, cdcB, kvA, kvB) case gov.StoreKey: - log += DecodeGovStore(cdcA, cdcB, kvA, kvB) + log += govsim.DecodeStore(cdcA, cdcB, kvA, kvB) case distribution.StoreKey: - log += DecodeDistributionStore(cdcA, cdcB, kvA, kvB) + log += distributionsim.DecodeStore(cdcA, cdcB, kvA, kvB) case supply.StoreKey: - log += DecodeSupplyStore(cdcA, cdcB, kvA, kvB) + log += supplysim.DecodeStore(cdcA, cdcB, kvA, kvB) default: log += fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvA.Key, kvA.Value, kvB.Key, kvB.Value) } @@ -533,209 +535,3 @@ func GetSimulationLog(storeName string, cdcA, cdcB *codec.Codec, kvs []cmn.KVPai return } - -// DecodeAccountStore unmarshals the KVPair's Value to the corresponding auth type -func DecodeAccountStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { - switch { - case bytes.Equal(kvA.Key[:1], auth.AddressStoreKeyPrefix): - var accA, accB auth.Account - cdcA.MustUnmarshalBinaryBare(kvA.Value, &accA) - cdcB.MustUnmarshalBinaryBare(kvB.Value, &accB) - return fmt.Sprintf("%v\n%v", accA, accB) - case bytes.Equal(kvA.Key, auth.GlobalAccountNumberKey): - var globalAccNumberA, globalAccNumberB uint64 - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &globalAccNumberA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &globalAccNumberB) - return fmt.Sprintf("GlobalAccNumberA: %d\nGlobalAccNumberB: %d", globalAccNumberA, globalAccNumberB) - default: - panic(fmt.Sprintf("invalid account key %X", kvA.Key)) - } -} - -// DecodeMintStore unmarshals the KVPair's Value to the corresponding mint type -func DecodeMintStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { - switch { - case bytes.Equal(kvA.Key, mint.MinterKey): - var minterA, minterB mint.Minter - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &minterA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &minterB) - return fmt.Sprintf("%v\n%v", minterA, minterB) - default: - panic(fmt.Sprintf("invalid mint key %X", kvA.Key)) - } -} - -// DecodeDistributionStore unmarshals the KVPair's Value to the corresponding distribution type -func DecodeDistributionStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { - switch { - case bytes.Equal(kvA.Key[:1], distribution.FeePoolKey): - var feePoolA, feePoolB distribution.FeePool - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &feePoolA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &feePoolB) - return fmt.Sprintf("%v\n%v", feePoolA, feePoolB) - - case bytes.Equal(kvA.Key[:1], distribution.ProposerKey): - return fmt.Sprintf("%v\n%v", sdk.ConsAddress(kvA.Value), sdk.ConsAddress(kvB.Value)) - - case bytes.Equal(kvA.Key[:1], distribution.ValidatorOutstandingRewardsPrefix): - var rewardsA, rewardsB distribution.ValidatorOutstandingRewards - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) - return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - - case bytes.Equal(kvA.Key[:1], distribution.DelegatorWithdrawAddrPrefix): - return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value)) - - case bytes.Equal(kvA.Key[:1], distribution.DelegatorStartingInfoPrefix): - var infoA, infoB distribution.DelegatorStartingInfo - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &infoA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &infoB) - return fmt.Sprintf("%v\n%v", infoA, infoB) - - case bytes.Equal(kvA.Key[:1], distribution.ValidatorHistoricalRewardsPrefix): - var rewardsA, rewardsB distribution.ValidatorHistoricalRewards - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) - return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - - case bytes.Equal(kvA.Key[:1], distribution.ValidatorCurrentRewardsPrefix): - var rewardsA, rewardsB distribution.ValidatorCurrentRewards - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) - return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - - case bytes.Equal(kvA.Key[:1], distribution.ValidatorAccumulatedCommissionPrefix): - var commissionA, commissionB distribution.ValidatorAccumulatedCommission - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &commissionA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &commissionB) - return fmt.Sprintf("%v\n%v", commissionA, commissionB) - - case bytes.Equal(kvA.Key[:1], distribution.ValidatorSlashEventPrefix): - var eventA, eventB distribution.ValidatorSlashEvent - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &eventA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &eventB) - return fmt.Sprintf("%v\n%v", eventA, eventB) - - default: - panic(fmt.Sprintf("invalid distribution key prefix %X", kvA.Key[:1])) - } -} - -// DecodeStakingStore unmarshals the KVPair's Value to the corresponding staking type -func DecodeStakingStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { - switch { - case bytes.Equal(kvA.Key[:1], staking.LastTotalPowerKey): - var powerA, powerB sdk.Int - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &powerA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &powerB) - return fmt.Sprintf("%v\n%v", powerA, powerB) - - case bytes.Equal(kvA.Key[:1], staking.ValidatorsKey): - var validatorA, validatorB staking.Validator - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &validatorA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &validatorB) - return fmt.Sprintf("%v\n%v", validatorA, validatorB) - - case bytes.Equal(kvA.Key[:1], staking.LastValidatorPowerKey), - bytes.Equal(kvA.Key[:1], staking.ValidatorsByConsAddrKey), - bytes.Equal(kvA.Key[:1], staking.ValidatorsByPowerIndexKey): - return fmt.Sprintf("%v\n%v", sdk.ValAddress(kvA.Value), sdk.ValAddress(kvB.Value)) - - case bytes.Equal(kvA.Key[:1], staking.DelegationKey): - var delegationA, delegationB staking.Delegation - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &delegationA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &delegationB) - return fmt.Sprintf("%v\n%v", delegationA, delegationB) - - case bytes.Equal(kvA.Key[:1], staking.UnbondingDelegationKey), - bytes.Equal(kvA.Key[:1], staking.UnbondingDelegationByValIndexKey): - var ubdA, ubdB staking.UnbondingDelegation - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &ubdA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &ubdB) - return fmt.Sprintf("%v\n%v", ubdA, ubdB) - - case bytes.Equal(kvA.Key[:1], staking.RedelegationKey), - bytes.Equal(kvA.Key[:1], staking.RedelegationByValSrcIndexKey): - var redA, redB staking.Redelegation - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &redA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &redB) - return fmt.Sprintf("%v\n%v", redA, redB) - - default: - panic(fmt.Sprintf("invalid staking key prefix %X", kvA.Key[:1])) - } -} - -// DecodeSlashingStore unmarshals the KVPair's Value to the corresponding slashing type -func DecodeSlashingStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { - switch { - case bytes.Equal(kvA.Key[:1], slashing.ValidatorSigningInfoKey): - var infoA, infoB slashing.ValidatorSigningInfo - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &infoA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &infoB) - return fmt.Sprintf("%v\n%v", infoA, infoB) - - case bytes.Equal(kvA.Key[:1], slashing.ValidatorMissedBlockBitArrayKey): - var missedA, missedB bool - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &missedA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &missedB) - return fmt.Sprintf("missedA: %v\nmissedB: %v", missedA, missedB) - - case bytes.Equal(kvA.Key[:1], slashing.AddrPubkeyRelationKey): - var pubKeyA, pubKeyB crypto.PubKey - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &pubKeyA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &pubKeyB) - bechPKA := sdk.MustBech32ifyAccPub(pubKeyA) - bechPKB := sdk.MustBech32ifyAccPub(pubKeyB) - return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", bechPKA, bechPKB) - - default: - panic(fmt.Sprintf("invalid slashing key prefix %X", kvA.Key[:1])) - } -} - -// DecodeGovStore unmarshals the KVPair's Value to the corresponding gov type -func DecodeGovStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { - switch { - case bytes.Equal(kvA.Key[:1], gov.ProposalsKeyPrefix): - var proposalA, proposalB gov.Proposal - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &proposalA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &proposalB) - return fmt.Sprintf("%v\n%v", proposalA, proposalB) - - case bytes.Equal(kvA.Key[:1], gov.ActiveProposalQueuePrefix), - bytes.Equal(kvA.Key[:1], gov.InactiveProposalQueuePrefix), - bytes.Equal(kvA.Key[:1], gov.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], gov.DepositsKeyPrefix): - var depositA, depositB gov.Deposit - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &depositA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &depositB) - return fmt.Sprintf("%v\n%v", depositA, depositB) - - case bytes.Equal(kvA.Key[:1], gov.VotesKeyPrefix): - var voteA, voteB gov.Vote - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &voteA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &voteB) - return fmt.Sprintf("%v\n%v", voteA, voteB) - - default: - panic(fmt.Sprintf("invalid governance key prefix %X", kvA.Key[:1])) - } -} - -// DecodeSupplyStore unmarshals the KVPair's Value to the corresponding supply type -func DecodeSupplyStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { - switch { - case bytes.Equal(kvA.Key[:1], supply.SupplyKey): - var supplyA, supplyB supply.Supply - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &supplyA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &supplyB) - return fmt.Sprintf("%v\n%v", supplyB, supplyB) - default: - panic(fmt.Sprintf("invalid supply key %X", kvA.Key)) - } -} diff --git a/simapp/utils_test.go b/simapp/utils_test.go index c75e6d4684de..86fe8a56497e 100644 --- a/simapp/utils_test.go +++ b/simapp/utils_test.go @@ -1,10 +1,6 @@ package simapp import ( - "encoding/binary" - "fmt" - "time" - "testing" "github.com/stretchr/testify/require" @@ -93,267 +89,3 @@ func TestGetSimulationLog(t *testing.T) { }) } } - -func TestDecodeAccountStore(t *testing.T) { - cdc := makeTestCodec() - acc := auth.NewBaseAccountWithAddress(delAddr1) - globalAccNumber := uint64(10) - - kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: auth.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(acc)}, - cmn.KVPair{Key: auth.GlobalAccountNumberKey, Value: cdc.MustMarshalBinaryLengthPrefixed(globalAccNumber)}, - cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, - } - tests := []struct { - name string - expectedLog string - }{ - {"Minter", fmt.Sprintf("%v\n%v", acc, acc)}, - {"GlobalAccNumber", fmt.Sprintf("GlobalAccNumberA: %d\nGlobalAccNumberB: %d", globalAccNumber, globalAccNumber)}, - {"other", ""}, - } - - for i, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - switch i { - case len(tests) - 1: - require.Panics(t, func() { DecodeAccountStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) - default: - require.Equal(t, tt.expectedLog, DecodeAccountStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) - } - }) - } -} - -func TestDecodeMintStore(t *testing.T) { - cdc := makeTestCodec() - minter := mint.NewMinter(sdk.OneDec(), sdk.NewDec(15)) - - kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: mint.MinterKey, Value: cdc.MustMarshalBinaryLengthPrefixed(minter)}, - cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, - } - tests := []struct { - name string - expectedLog string - }{ - {"Minter", fmt.Sprintf("%v\n%v", minter, minter)}, - {"other", ""}, - } - - for i, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - switch i { - case len(tests) - 1: - require.Panics(t, func() { DecodeMintStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) - default: - require.Equal(t, tt.expectedLog, DecodeMintStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) - } - }) - } -} - -func TestDecodeDistributionStore(t *testing.T) { - cdc := makeTestCodec() - - decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.OneDec())} - feePool := distr.InitialFeePool() - feePool.CommunityPool = decCoins - info := distr.NewDelegatorStartingInfo(2, sdk.OneDec(), 200) - outstanding := distr.ValidatorOutstandingRewards{decCoins[0]} - commission := distr.ValidatorAccumulatedCommission{decCoins[0]} - historicalRewards := distr.NewValidatorHistoricalRewards(decCoins, 100) - currentRewards := distr.NewValidatorCurrentRewards(decCoins, 5) - slashEvent := distr.NewValidatorSlashEvent(10, sdk.OneDec()) - - kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: distr.FeePoolKey, Value: cdc.MustMarshalBinaryLengthPrefixed(feePool)}, - cmn.KVPair{Key: distr.ProposerKey, Value: consAddr1.Bytes()}, - cmn.KVPair{Key: distr.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(outstanding)}, - cmn.KVPair{Key: distr.GetDelegatorWithdrawAddrKey(delAddr1), Value: delAddr1.Bytes()}, - cmn.KVPair{Key: distr.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(info)}, - cmn.KVPair{Key: distr.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshalBinaryLengthPrefixed(historicalRewards)}, - cmn.KVPair{Key: distr.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(currentRewards)}, - cmn.KVPair{Key: distr.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(commission)}, - cmn.KVPair{Key: distr.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshalBinaryLengthPrefixed(slashEvent)}, - cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, - } - - tests := []struct { - name string - expectedLog string - }{ - {"FeePool", fmt.Sprintf("%v\n%v", feePool, feePool)}, - {"Proposer", fmt.Sprintf("%v\n%v", consAddr1, consAddr1)}, - {"ValidatorOutstandingRewards", fmt.Sprintf("%v\n%v", outstanding, outstanding)}, - {"DelegatorWithdrawAddr", fmt.Sprintf("%v\n%v", delAddr1, delAddr1)}, - {"DelegatorStartingInfo", fmt.Sprintf("%v\n%v", info, info)}, - {"ValidatorHistoricalRewards", fmt.Sprintf("%v\n%v", historicalRewards, historicalRewards)}, - {"ValidatorCurrentRewards", fmt.Sprintf("%v\n%v", currentRewards, currentRewards)}, - {"ValidatorAccumulatedCommission", fmt.Sprintf("%v\n%v", commission, commission)}, - {"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() { DecodeDistributionStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) - default: - require.Equal(t, tt.expectedLog, DecodeDistributionStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) - } - }) - } -} - -func TestDecodeStakingStore(t *testing.T) { - cdc := makeTestCodec() - - bondTime := time.Now().UTC() - - val := staking.NewValidator(valAddr1, delPk1, staking.NewDescription("test", "test", "test", "test")) - del := staking.NewDelegation(delAddr1, valAddr1, sdk.OneDec()) - ubd := staking.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, sdk.OneInt()) - red := staking.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, sdk.OneInt(), sdk.OneDec()) - - kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: staking.LastTotalPowerKey, Value: cdc.MustMarshalBinaryLengthPrefixed(sdk.OneInt())}, - cmn.KVPair{Key: staking.GetValidatorKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(val)}, - cmn.KVPair{Key: staking.LastValidatorPowerKey, Value: valAddr1.Bytes()}, - cmn.KVPair{Key: staking.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(del)}, - cmn.KVPair{Key: staking.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(ubd)}, - cmn.KVPair{Key: staking.GetREDKey(delAddr1, valAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(red)}, - cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, - } - - tests := []struct { - name string - expectedLog string - }{ - {"LastTotalPower", fmt.Sprintf("%v\n%v", sdk.OneInt(), sdk.OneInt())}, - {"Validator", fmt.Sprintf("%v\n%v", val, val)}, - {"LastValidatorPower/ValidatorsByConsAddr/ValidatorsByPowerIndex", fmt.Sprintf("%v\n%v", valAddr1, valAddr1)}, - {"Delegation", fmt.Sprintf("%v\n%v", del, del)}, - {"UnbondingDelegation", fmt.Sprintf("%v\n%v", ubd, ubd)}, - {"Redelegation", fmt.Sprintf("%v\n%v", red, red)}, - {"other", ""}, - } - for i, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - switch i { - case len(tests) - 1: - require.Panics(t, func() { DecodeStakingStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) - default: - require.Equal(t, tt.expectedLog, DecodeStakingStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) - } - }) - } -} - -func TestDecodeSlashingStore(t *testing.T) { - cdc := makeTestCodec() - - info := slashing.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0) - bechPK := sdk.MustBech32ifyAccPub(delPk1) - missed := true - - kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: slashing.GetValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(info)}, - cmn.KVPair{Key: slashing.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryLengthPrefixed(missed)}, - cmn.KVPair{Key: slashing.GetAddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(delPk1)}, - cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, - } - - tests := []struct { - name string - expectedLog string - }{ - {"ValidatorSigningInfo", fmt.Sprintf("%v\n%v", info, info)}, - {"ValidatorMissedBlockBitArray", fmt.Sprintf("missedA: %v\nmissedB: %v", missed, missed)}, - {"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", bechPK, bechPK)}, - {"other", ""}, - } - for i, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - switch i { - case len(tests) - 1: - require.Panics(t, func() { DecodeSlashingStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) - default: - require.Equal(t, tt.expectedLog, DecodeSlashingStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) - } - }) - } -} - -func TestDecodeGovStore(t *testing.T) { - cdc := makeTestCodec() - - endTime := time.Now().UTC() - - content := gov.ContentFromProposalType("test", "test", gov.ProposalTypeText) - proposal := gov.NewProposal(content, 1, endTime, endTime.Add(24*time.Hour)) - proposalIDBz := make([]byte, 8) - binary.LittleEndian.PutUint64(proposalIDBz, 1) - deposit := gov.NewDeposit(1, delAddr1, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt()))) - vote := gov.NewVote(1, delAddr1, gov.OptionYes) - - kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: gov.ProposalKey(1), Value: cdc.MustMarshalBinaryLengthPrefixed(proposal)}, - cmn.KVPair{Key: gov.InactiveProposalQueueKey(1, endTime), Value: proposalIDBz}, - cmn.KVPair{Key: gov.DepositKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(deposit)}, - cmn.KVPair{Key: gov.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(vote)}, - cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, - } - - tests := []struct { - name string - expectedLog string - }{ - {"proposals", fmt.Sprintf("%v\n%v", proposal, proposal)}, - {"proposal IDs", "proposalIDA: 1\nProposalIDB: 1"}, - {"deposits", fmt.Sprintf("%v\n%v", deposit, deposit)}, - {"votes", fmt.Sprintf("%v\n%v", vote, vote)}, - {"other", ""}, - } - - for i, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - switch i { - case len(tests) - 1: - require.Panics(t, func() { DecodeGovStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) - default: - require.Equal(t, tt.expectedLog, DecodeGovStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) - } - }) - } -} - -func TestDecodeSupplyStore(t *testing.T) { - cdc := makeTestCodec() - - totalSupply := supply.NewSupply(sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000))) - - kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: supply.SupplyKey, Value: cdc.MustMarshalBinaryLengthPrefixed(totalSupply)}, - cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, - } - - tests := []struct { - name string - expectedLog string - }{ - {"Supply", fmt.Sprintf("%v\n%v", totalSupply, totalSupply)}, - {"other", ""}, - } - - for i, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - switch i { - case len(tests) - 1: - require.Panics(t, func() { DecodeSupplyStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) - default: - require.Equal(t, tt.expectedLog, DecodeSupplyStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) - } - }) - } -} diff --git a/x/auth/simulation/store.go b/x/auth/simulation/store.go new file mode 100644 index 000000000000..528d756849e7 --- /dev/null +++ b/x/auth/simulation/store.go @@ -0,0 +1,31 @@ +package simulation + +import ( + "bytes" + "fmt" + + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/exported" +) + +// DecodeStore unmarshals the KVPair's Value to the corresponding auth type +func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { + switch { + case bytes.Equal(kvA.Key[:1], auth.AddressStoreKeyPrefix): + var accA, accB exported.Account + cdcA.MustUnmarshalBinaryBare(kvA.Value, &accA) + cdcB.MustUnmarshalBinaryBare(kvB.Value, &accB) + return fmt.Sprintf("%v\n%v", accA, accB) + case bytes.Equal(kvA.Key, auth.GlobalAccountNumberKey): + var globalAccNumberA, globalAccNumberB uint64 + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &globalAccNumberA) + cdcB.MustUnmarshalBinaryLengthPrefixed(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/store_test.go b/x/auth/simulation/store_test.go new file mode 100644 index 000000000000..c59f6ab8eec6 --- /dev/null +++ b/x/auth/simulation/store_test.go @@ -0,0 +1,63 @@ +package simulation + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/tendermint/tendermint/crypto/ed25519" + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + "github.com/cosmos/cosmos-sdk/x/auth" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var ( + delPk1 = ed25519.GenPrivKey().PubKey() + delAddr1 = sdk.AccAddress(delPk1.Address()) + valAddr1 = sdk.ValAddress(delPk1.Address()) + consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes()) +) + +func makeTestCodec() (cdc *codec.Codec) { + cdc = codec.New() + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + auth.RegisterCodec(cdc) + return +} + +func TestDecodeStore(t *testing.T) { + cdc := makeTestCodec() + acc := auth.NewBaseAccountWithAddress(delAddr1) + globalAccNumber := uint64(10) + + kvPairs := cmn.KVPairs{ + cmn.KVPair{Key: auth.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(acc)}, + cmn.KVPair{Key: auth.GlobalAccountNumberKey, Value: cdc.MustMarshalBinaryLengthPrefixed(globalAccNumber)}, + cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, + } + tests := []struct { + name string + expectedLog string + }{ + {"Minter", fmt.Sprintf("%v\n%v", acc, acc)}, + {"GlobalAccNumber", fmt.Sprintf("GlobalAccNumberA: %d\nGlobalAccNumberB: %d", globalAccNumber, globalAccNumber)}, + {"other", ""}, + } + + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + switch i { + case len(tests) - 1: + require.Panics(t, func() { DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + default: + require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + } + }) + } +} diff --git a/x/distribution/simulation/store.go b/x/distribution/simulation/store.go new file mode 100644 index 000000000000..26b919e64830 --- /dev/null +++ b/x/distribution/simulation/store.go @@ -0,0 +1,69 @@ +package simulation + +import ( + "bytes" + "fmt" + + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/distribution" +) + +// DecodeStore unmarshals the KVPair's Value to the corresponding distribution type +func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { + switch { + case bytes.Equal(kvA.Key[:1], distribution.FeePoolKey): + var feePoolA, feePoolB distribution.FeePool + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &feePoolA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &feePoolB) + return fmt.Sprintf("%v\n%v", feePoolA, feePoolB) + + case bytes.Equal(kvA.Key[:1], distribution.ProposerKey): + return fmt.Sprintf("%v\n%v", sdk.ConsAddress(kvA.Value), sdk.ConsAddress(kvB.Value)) + + case bytes.Equal(kvA.Key[:1], distribution.ValidatorOutstandingRewardsPrefix): + var rewardsA, rewardsB distribution.ValidatorOutstandingRewards + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) + return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) + + case bytes.Equal(kvA.Key[:1], distribution.DelegatorWithdrawAddrPrefix): + return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value)) + + case bytes.Equal(kvA.Key[:1], distribution.DelegatorStartingInfoPrefix): + var infoA, infoB distribution.DelegatorStartingInfo + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &infoA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &infoB) + return fmt.Sprintf("%v\n%v", infoA, infoB) + + case bytes.Equal(kvA.Key[:1], distribution.ValidatorHistoricalRewardsPrefix): + var rewardsA, rewardsB distribution.ValidatorHistoricalRewards + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) + return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) + + case bytes.Equal(kvA.Key[:1], distribution.ValidatorCurrentRewardsPrefix): + var rewardsA, rewardsB distribution.ValidatorCurrentRewards + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) + return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) + + case bytes.Equal(kvA.Key[:1], distribution.ValidatorAccumulatedCommissionPrefix): + var commissionA, commissionB distribution.ValidatorAccumulatedCommission + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &commissionA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &commissionB) + return fmt.Sprintf("%v\n%v", commissionA, commissionB) + + case bytes.Equal(kvA.Key[:1], distribution.ValidatorSlashEventPrefix): + var eventA, eventB distribution.ValidatorSlashEvent + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &eventA) + cdcB.MustUnmarshalBinaryLengthPrefixed(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/store_test.go b/x/distribution/simulation/store_test.go new file mode 100644 index 000000000000..f0cc7caea871 --- /dev/null +++ b/x/distribution/simulation/store_test.go @@ -0,0 +1,85 @@ +package simulation + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/tendermint/tendermint/crypto/ed25519" + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + distr "github.com/cosmos/cosmos-sdk/x/distribution" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var ( + delPk1 = ed25519.GenPrivKey().PubKey() + delAddr1 = sdk.AccAddress(delPk1.Address()) + valAddr1 = sdk.ValAddress(delPk1.Address()) + consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes()) +) + +func makeTestCodec() (cdc *codec.Codec) { + cdc = codec.New() + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + distr.RegisterCodec(cdc) + return +} + +func TestDecodeDistributionStore(t *testing.T) { + cdc := makeTestCodec() + + decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.OneDec())} + feePool := distr.InitialFeePool() + feePool.CommunityPool = decCoins + info := distr.NewDelegatorStartingInfo(2, sdk.OneDec(), 200) + outstanding := distr.ValidatorOutstandingRewards{decCoins[0]} + commission := distr.ValidatorAccumulatedCommission{decCoins[0]} + historicalRewards := distr.NewValidatorHistoricalRewards(decCoins, 100) + currentRewards := distr.NewValidatorCurrentRewards(decCoins, 5) + slashEvent := distr.NewValidatorSlashEvent(10, sdk.OneDec()) + + kvPairs := cmn.KVPairs{ + cmn.KVPair{Key: distr.FeePoolKey, Value: cdc.MustMarshalBinaryLengthPrefixed(feePool)}, + cmn.KVPair{Key: distr.ProposerKey, Value: consAddr1.Bytes()}, + cmn.KVPair{Key: distr.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(outstanding)}, + cmn.KVPair{Key: distr.GetDelegatorWithdrawAddrKey(delAddr1), Value: delAddr1.Bytes()}, + cmn.KVPair{Key: distr.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(info)}, + cmn.KVPair{Key: distr.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshalBinaryLengthPrefixed(historicalRewards)}, + cmn.KVPair{Key: distr.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(currentRewards)}, + cmn.KVPair{Key: distr.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(commission)}, + cmn.KVPair{Key: distr.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshalBinaryLengthPrefixed(slashEvent)}, + cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, + } + + tests := []struct { + name string + expectedLog string + }{ + {"FeePool", fmt.Sprintf("%v\n%v", feePool, feePool)}, + {"Proposer", fmt.Sprintf("%v\n%v", consAddr1, consAddr1)}, + {"ValidatorOutstandingRewards", fmt.Sprintf("%v\n%v", outstanding, outstanding)}, + {"DelegatorWithdrawAddr", fmt.Sprintf("%v\n%v", delAddr1, delAddr1)}, + {"DelegatorStartingInfo", fmt.Sprintf("%v\n%v", info, info)}, + {"ValidatorHistoricalRewards", fmt.Sprintf("%v\n%v", historicalRewards, historicalRewards)}, + {"ValidatorCurrentRewards", fmt.Sprintf("%v\n%v", currentRewards, currentRewards)}, + {"ValidatorAccumulatedCommission", fmt.Sprintf("%v\n%v", commission, commission)}, + {"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() { DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + default: + require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + } + }) + } +} diff --git a/x/gov/simulation/store.go b/x/gov/simulation/store.go new file mode 100644 index 000000000000..3c30fb51f904 --- /dev/null +++ b/x/gov/simulation/store.go @@ -0,0 +1,46 @@ +package simulation + +import ( + "bytes" + "encoding/binary" + "fmt" + + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + "github.com/cosmos/cosmos-sdk/x/gov" +) + +// DecodeStore unmarshals the KVPair's Value to the corresponding gov type +func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { + switch { + case bytes.Equal(kvA.Key[:1], gov.ProposalsKeyPrefix): + var proposalA, proposalB gov.Proposal + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &proposalA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &proposalB) + return fmt.Sprintf("%v\n%v", proposalA, proposalB) + + case bytes.Equal(kvA.Key[:1], gov.ActiveProposalQueuePrefix), + bytes.Equal(kvA.Key[:1], gov.InactiveProposalQueuePrefix), + bytes.Equal(kvA.Key[:1], gov.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], gov.DepositsKeyPrefix): + var depositA, depositB gov.Deposit + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &depositA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &depositB) + return fmt.Sprintf("%v\n%v", depositA, depositB) + + case bytes.Equal(kvA.Key[:1], gov.VotesKeyPrefix): + var voteA, voteB gov.Vote + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &voteA) + cdcB.MustUnmarshalBinaryLengthPrefixed(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/store_test.go b/x/gov/simulation/store_test.go new file mode 100644 index 000000000000..286c7c50fe3c --- /dev/null +++ b/x/gov/simulation/store_test.go @@ -0,0 +1,77 @@ +package simulation + +import ( + "encoding/binary" + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/tendermint/tendermint/crypto/ed25519" + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + "github.com/cosmos/cosmos-sdk/x/gov" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var ( + delPk1 = ed25519.GenPrivKey().PubKey() + delAddr1 = sdk.AccAddress(delPk1.Address()) + valAddr1 = sdk.ValAddress(delPk1.Address()) + consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes()) +) + +func makeTestCodec() (cdc *codec.Codec) { + cdc = codec.New() + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + gov.RegisterCodec(cdc) + return +} + +func TestDecodeStore(t *testing.T) { + cdc := makeTestCodec() + + endTime := time.Now().UTC() + + content := gov.ContentFromProposalType("test", "test", gov.ProposalTypeText) + proposal := gov.NewProposal(content, 1, endTime, endTime.Add(24*time.Hour)) + proposalIDBz := make([]byte, 8) + binary.LittleEndian.PutUint64(proposalIDBz, 1) + deposit := gov.NewDeposit(1, delAddr1, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt()))) + vote := gov.NewVote(1, delAddr1, gov.OptionYes) + + kvPairs := cmn.KVPairs{ + cmn.KVPair{Key: gov.ProposalKey(1), Value: cdc.MustMarshalBinaryLengthPrefixed(proposal)}, + cmn.KVPair{Key: gov.InactiveProposalQueueKey(1, endTime), Value: proposalIDBz}, + cmn.KVPair{Key: gov.DepositKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(deposit)}, + cmn.KVPair{Key: gov.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(vote)}, + cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, + } + + tests := []struct { + name string + expectedLog string + }{ + {"proposals", fmt.Sprintf("%v\n%v", proposal, proposal)}, + {"proposal IDs", "proposalIDA: 1\nProposalIDB: 1"}, + {"deposits", fmt.Sprintf("%v\n%v", deposit, deposit)}, + {"votes", fmt.Sprintf("%v\n%v", vote, vote)}, + {"other", ""}, + } + + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + switch i { + case len(tests) - 1: + require.Panics(t, func() { DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + default: + require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + } + }) + } +} diff --git a/x/mint/simulation/store.go b/x/mint/simulation/store.go new file mode 100644 index 000000000000..3314ead57b32 --- /dev/null +++ b/x/mint/simulation/store.go @@ -0,0 +1,25 @@ +package simulation + +import ( + "bytes" + "fmt" + + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + "github.com/cosmos/cosmos-sdk/x/mint" +) + +// DecodeStore unmarshals the KVPair's Value to the corresponding mint type +func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { + switch { + case bytes.Equal(kvA.Key, mint.MinterKey): + var minterA, minterB mint.Minter + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &minterA) + cdcB.MustUnmarshalBinaryLengthPrefixed(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/store_test.go b/x/mint/simulation/store_test.go new file mode 100644 index 000000000000..8a02ee126721 --- /dev/null +++ b/x/mint/simulation/store_test.go @@ -0,0 +1,50 @@ +package simulation + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" + + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + "github.com/cosmos/cosmos-sdk/x/mint" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func makeTestCodec() (cdc *codec.Codec) { + cdc = codec.New() + sdk.RegisterCodec(cdc) + return +} + +func TestDecodeStore(t *testing.T) { + cdc := makeTestCodec() + minter := mint.NewMinter(sdk.OneDec(), sdk.NewDec(15)) + + kvPairs := cmn.KVPairs{ + cmn.KVPair{Key: mint.MinterKey, Value: cdc.MustMarshalBinaryLengthPrefixed(minter)}, + cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, + } + tests := []struct { + name string + expectedLog string + }{ + {"Minter", fmt.Sprintf("%v\n%v", minter, minter)}, + {"other", ""}, + } + + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + switch i { + case len(tests) - 1: + require.Panics(t, func() { DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + default: + require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + } + }) + } +} diff --git a/x/slashing/simulation/store.go b/x/slashing/simulation/store.go new file mode 100644 index 000000000000..9f17fc59fda2 --- /dev/null +++ b/x/slashing/simulation/store.go @@ -0,0 +1,42 @@ +package simulation + +import ( + "bytes" + "fmt" + + "github.com/tendermint/tendermint/crypto" + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/slashing" +) + +// DecodeStore unmarshals the KVPair's Value to the corresponding slashing type +func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { + switch { + case bytes.Equal(kvA.Key[:1], slashing.ValidatorSigningInfoKey): + var infoA, infoB slashing.ValidatorSigningInfo + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &infoA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &infoB) + return fmt.Sprintf("%v\n%v", infoA, infoB) + + case bytes.Equal(kvA.Key[:1], slashing.ValidatorMissedBlockBitArrayKey): + var missedA, missedB bool + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &missedA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &missedB) + return fmt.Sprintf("missedA: %v\nmissedB: %v", missedA, missedB) + + case bytes.Equal(kvA.Key[:1], slashing.AddrPubkeyRelationKey): + var pubKeyA, pubKeyB crypto.PubKey + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &pubKeyA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &pubKeyB) + bechPKA := sdk.MustBech32ifyAccPub(pubKeyA) + bechPKB := sdk.MustBech32ifyAccPub(pubKeyB) + 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/store_test.go b/x/slashing/simulation/store_test.go new file mode 100644 index 000000000000..3de4116a2fde --- /dev/null +++ b/x/slashing/simulation/store_test.go @@ -0,0 +1,67 @@ +package simulation + +import ( + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/tendermint/tendermint/crypto/ed25519" + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/x/slashing" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var ( + delPk1 = ed25519.GenPrivKey().PubKey() + delAddr1 = sdk.AccAddress(delPk1.Address()) + valAddr1 = sdk.ValAddress(delPk1.Address()) + consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes()) +) + +func makeTestCodec() (cdc *codec.Codec) { + cdc = codec.New() + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + slashing.RegisterCodec(cdc) + return +} + +func TestDecodeStore(t *testing.T) { + cdc := makeTestCodec() + + info := slashing.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0) + bechPK := sdk.MustBech32ifyAccPub(delPk1) + missed := true + + kvPairs := cmn.KVPairs{ + cmn.KVPair{Key: slashing.GetValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(info)}, + cmn.KVPair{Key: slashing.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryLengthPrefixed(missed)}, + cmn.KVPair{Key: slashing.GetAddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(delPk1)}, + cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, + } + + tests := []struct { + name string + expectedLog string + }{ + {"ValidatorSigningInfo", fmt.Sprintf("%v\n%v", info, info)}, + {"ValidatorMissedBlockBitArray", fmt.Sprintf("missedA: %v\nmissedB: %v", missed, missed)}, + {"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", bechPK, bechPK)}, + {"other", ""}, + } + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + switch i { + case len(tests) - 1: + require.Panics(t, func() { DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + default: + require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + } + }) + } +} diff --git a/x/staking/simulation/store.go b/x/staking/simulation/store.go new file mode 100644 index 000000000000..7e82c144a7d8 --- /dev/null +++ b/x/staking/simulation/store.go @@ -0,0 +1,58 @@ +package simulation + +import ( + "bytes" + "fmt" + + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/staking" +) + +// DecodeStore unmarshals the KVPair's Value to the corresponding staking type +func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { + switch { + case bytes.Equal(kvA.Key[:1], staking.LastTotalPowerKey): + var powerA, powerB sdk.Int + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &powerA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &powerB) + return fmt.Sprintf("%v\n%v", powerA, powerB) + + case bytes.Equal(kvA.Key[:1], staking.ValidatorsKey): + var validatorA, validatorB staking.Validator + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &validatorA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &validatorB) + return fmt.Sprintf("%v\n%v", validatorA, validatorB) + + case bytes.Equal(kvA.Key[:1], staking.LastValidatorPowerKey), + bytes.Equal(kvA.Key[:1], staking.ValidatorsByConsAddrKey), + bytes.Equal(kvA.Key[:1], staking.ValidatorsByPowerIndexKey): + return fmt.Sprintf("%v\n%v", sdk.ValAddress(kvA.Value), sdk.ValAddress(kvB.Value)) + + case bytes.Equal(kvA.Key[:1], staking.DelegationKey): + var delegationA, delegationB staking.Delegation + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &delegationA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &delegationB) + return fmt.Sprintf("%v\n%v", delegationA, delegationB) + + case bytes.Equal(kvA.Key[:1], staking.UnbondingDelegationKey), + bytes.Equal(kvA.Key[:1], staking.UnbondingDelegationByValIndexKey): + var ubdA, ubdB staking.UnbondingDelegation + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &ubdA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &ubdB) + return fmt.Sprintf("%v\n%v", ubdA, ubdB) + + case bytes.Equal(kvA.Key[:1], staking.RedelegationKey), + bytes.Equal(kvA.Key[:1], staking.RedelegationByValSrcIndexKey): + var redA, redB staking.Redelegation + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &redA) + cdcB.MustUnmarshalBinaryLengthPrefixed(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/store_test.go b/x/staking/simulation/store_test.go new file mode 100644 index 000000000000..7df3d8b4ee7f --- /dev/null +++ b/x/staking/simulation/store_test.go @@ -0,0 +1,76 @@ +package simulation + +import ( + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/tendermint/tendermint/crypto/ed25519" + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/x/staking" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var ( + delPk1 = ed25519.GenPrivKey().PubKey() + delAddr1 = sdk.AccAddress(delPk1.Address()) + valAddr1 = sdk.ValAddress(delPk1.Address()) + consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes()) +) + +func makeTestCodec() (cdc *codec.Codec) { + cdc = codec.New() + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + staking.RegisterCodec(cdc) + return +} + +func TestDecodeStore(t *testing.T) { + cdc := makeTestCodec() + + bondTime := time.Now().UTC() + + val := staking.NewValidator(valAddr1, delPk1, staking.NewDescription("test", "test", "test", "test")) + del := staking.NewDelegation(delAddr1, valAddr1, sdk.OneDec()) + ubd := staking.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, sdk.OneInt()) + red := staking.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, sdk.OneInt(), sdk.OneDec()) + + kvPairs := cmn.KVPairs{ + cmn.KVPair{Key: staking.LastTotalPowerKey, Value: cdc.MustMarshalBinaryLengthPrefixed(sdk.OneInt())}, + cmn.KVPair{Key: staking.GetValidatorKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(val)}, + cmn.KVPair{Key: staking.LastValidatorPowerKey, Value: valAddr1.Bytes()}, + cmn.KVPair{Key: staking.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(del)}, + cmn.KVPair{Key: staking.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(ubd)}, + cmn.KVPair{Key: staking.GetREDKey(delAddr1, valAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(red)}, + cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, + } + + tests := []struct { + name string + expectedLog string + }{ + {"LastTotalPower", fmt.Sprintf("%v\n%v", sdk.OneInt(), sdk.OneInt())}, + {"Validator", fmt.Sprintf("%v\n%v", val, val)}, + {"LastValidatorPower/ValidatorsByConsAddr/ValidatorsByPowerIndex", fmt.Sprintf("%v\n%v", valAddr1, valAddr1)}, + {"Delegation", fmt.Sprintf("%v\n%v", del, del)}, + {"UnbondingDelegation", fmt.Sprintf("%v\n%v", ubd, ubd)}, + {"Redelegation", fmt.Sprintf("%v\n%v", red, red)}, + {"other", ""}, + } + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + switch i { + case len(tests) - 1: + require.Panics(t, func() { DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + default: + require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + } + }) + } +} diff --git a/x/supply/simulation/store.go b/x/supply/simulation/store.go new file mode 100644 index 000000000000..80ec57947ba2 --- /dev/null +++ b/x/supply/simulation/store.go @@ -0,0 +1,25 @@ +package simulation + +import ( + "bytes" + "fmt" + + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + "github.com/cosmos/cosmos-sdk/x/supply" +) + +// DecodeStore unmarshals the KVPair's Value to the corresponding supply type +func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { + switch { + case bytes.Equal(kvA.Key[:1], supply.SupplyKey): + var supplyA, supplyB supply.Supply + cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &supplyA) + cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &supplyB) + return fmt.Sprintf("%v\n%v", supplyB, supplyB) + default: + panic(fmt.Sprintf("invalid supply key %X", kvA.Key)) + } +} diff --git a/x/supply/simulation/store_test.go b/x/supply/simulation/store_test.go new file mode 100644 index 000000000000..63915d6368e0 --- /dev/null +++ b/x/supply/simulation/store_test.go @@ -0,0 +1,53 @@ +package simulation + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" + + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" + + "github.com/cosmos/cosmos-sdk/x/supply" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func makeTestCodec() (cdc *codec.Codec) { + cdc = codec.New() + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + supply.RegisterCodec(cdc) + return +} +func TestDecodeStore(t *testing.T) { + cdc := makeTestCodec() + + totalSupply := supply.NewSupply(sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000))) + + kvPairs := cmn.KVPairs{ + cmn.KVPair{Key: supply.SupplyKey, Value: cdc.MustMarshalBinaryLengthPrefixed(totalSupply)}, + cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, + } + + tests := []struct { + name string + expectedLog string + }{ + {"Supply", fmt.Sprintf("%v\n%v", totalSupply, totalSupply)}, + {"other", ""}, + } + + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + switch i { + case len(tests) - 1: + require.Panics(t, func() { DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + default: + require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + } + }) + } +} From 687125a2309be7ca9f778a82cd3dacb9ff813ee2 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 5 Aug 2019 18:22:02 +0200 Subject: [PATCH 02/18] fix --- simapp/utils.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/simapp/utils.go b/simapp/utils.go index 9bce23138481..f8e38f1a4259 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -9,6 +9,7 @@ import ( "math/rand" "time" + "github.com/tendermint/tendermint/crypto/secp256k1" cmn "github.com/tendermint/tendermint/libs/common" "github.com/tendermint/tendermint/libs/log" tmtypes "github.com/tendermint/tendermint/types" @@ -19,6 +20,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" authsim "github.com/cosmos/cosmos-sdk/x/auth/simulation" + "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/distribution" distributionsim "github.com/cosmos/cosmos-sdk/x/distribution/simulation" "github.com/cosmos/cosmos-sdk/x/genaccounts" From eeb0469c39ecd930b13186e3684f73ebfa704799 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 5 Aug 2019 20:51:54 +0200 Subject: [PATCH 03/18] module pattern --- simapp/app.go | 2 ++ simapp/utils.go | 26 +++++++++----------------- types/module/module.go | 7 +++++++ types/store.go | 5 +++++ x/auth/module.go | 7 +++++++ x/distribution/module.go | 7 +++++++ x/gov/module.go | 7 +++++++ x/mint/module.go | 7 +++++++ x/slashing/module.go | 7 +++++++ x/staking/module.go | 7 +++++++ x/supply/module.go | 7 +++++++ 11 files changed, 72 insertions(+), 17 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index c4e55bd1f2d5..28e9021f554e 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -110,6 +110,7 @@ func NewSimApp( ) *SimApp { cdc := MakeCodec() + storeDecoderRegistry := make(sdk.StoreDecoderRegistry) bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...) bApp.SetCommitMultiStoreTracer(traceStore) @@ -194,6 +195,7 @@ func NewSimApp( app.mm.RegisterInvariants(&app.crisisKeeper) app.mm.RegisterRoutes(app.Router(), app.QueryRouter()) + app.mm.RegisterStoreDecoders(storeDecoderRegistry) // initialize stores app.MountKVStores(keys) diff --git a/simapp/utils.go b/simapp/utils.go index f8e38f1a4259..b3c38cb22440 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -504,7 +504,11 @@ func GenStakingGenesisState( // 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, cdcA, cdcB *codec.Codec, kvs []cmn.KVPair) (log string) { +func GetSimulationLog(sdr sdk.StoreDecoderRegistry, storeName string, cdcA, cdcB *codec.Codec, kvs []cmn.KVPair) (log string) { + if kvs%2 != 0 { + panic("KVPairs are not multiple of 2. There should be one for each app store") + } + var kvA, kvB cmn.KVPair for i := 0; i < len(kvs); i += 2 { kvA = kvs[i] @@ -515,22 +519,10 @@ func GetSimulationLog(storeName string, cdcA, cdcB *codec.Codec, kvs []cmn.KVPai continue } - switch storeName { - case auth.StoreKey: - log += authsim.DecodeStore(cdcA, cdcB, kvA, kvB) - case mint.StoreKey: - log += mintsim.DecodeStore(cdcA, cdcB, kvA, kvB) - case staking.StoreKey: - log += stakingsim.DecodeStore(cdcA, cdcB, kvA, kvB) - case slashing.StoreKey: - log += slashingsim.DecodeStore(cdcA, cdcB, kvA, kvB) - case gov.StoreKey: - log += govsim.DecodeStore(cdcA, cdcB, kvA, kvB) - case distribution.StoreKey: - log += distributionsim.DecodeStore(cdcA, cdcB, kvA, kvB) - case supply.StoreKey: - log += supplysim.DecodeStore(cdcA, cdcB, kvA, kvB) - default: + decoder, ok := sdr[storeName] + if ok { + log += decoder(cdcA, cdcB, kvA, kvB) + } else { log += fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvA.Key, kvA.Value, kvB.Key, kvB.Value) } } diff --git a/types/module/module.go b/types/module/module.go index f6083882d979..c81ff0533e45 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -236,6 +236,13 @@ func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) { } } +// register all module routes and module querier routes +func (m *Manager) RegisterStoreDecoders(sd map[string]func(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string) { + for _, module := range m.Modules { + module.RegisterStoreDecoder(sd) + } +} + // register all module routes and module querier routes func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) { for _, module := range m.Modules { diff --git a/types/store.go b/types/store.go index 1ebeb62aed21..f0ff87a6abb9 100644 --- a/types/store.go +++ b/types/store.go @@ -3,6 +3,7 @@ package types import ( cmn "github.com/tendermint/tendermint/libs/common" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store/types" ) @@ -24,6 +25,10 @@ type ( Iterator = types.Iterator ) +// StoreDecoderRegistry defines each of the modules store decoders. Used for ImportExport +// simulation. +type StoreDecoderRegistry map[string]func(cdcA, cdcB *codec.Codec, kvA, kvB KVPair) string + // Iterator over all the keys with a certain prefix in ascending order func KVStorePrefixIterator(kvs KVStore, prefix []byte) Iterator { return types.KVStorePrefixIterator(kvs, prefix) diff --git a/x/auth/module.go b/x/auth/module.go index ef978e020082..29929c29d222 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -14,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/auth/client/rest" "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/auth/simulation" ) var ( @@ -87,6 +88,12 @@ func (AppModule) Name() string { // register invariants func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} +// RegisterStoreDecoder registers the function to decode the types stored in the +// KVStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { + sdr[StoreKey] = simulation.DecodeStore +} + // module message route name func (AppModule) Route() string { return "" } diff --git a/x/distribution/module.go b/x/distribution/module.go index 6fe513ad613e..4cae8b3a42e5 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -15,6 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/distribution/client/cli" "github.com/cosmos/cosmos-sdk/x/distribution/client/rest" "github.com/cosmos/cosmos-sdk/x/distribution/types" + "github.com/cosmos/cosmos-sdk/x/distribution/simulation" ) var ( @@ -91,6 +92,12 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { RegisterInvariants(ir, am.keeper) } +// RegisterStoreDecoder registers the function to decode the types stored in the +// KVStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { + sdr[StoreKey] = simulation.DecodeStore +} + // module message route name func (AppModule) Route() string { return RouterKey diff --git a/x/gov/module.go b/x/gov/module.go index bd50770dc2d9..8b5fcd64d2ed 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -16,6 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/gov/client/cli" "github.com/cosmos/cosmos-sdk/x/gov/client/rest" "github.com/cosmos/cosmos-sdk/x/gov/types" + "github.com/cosmos/cosmos-sdk/x/gov/simulation" ) var ( @@ -115,6 +116,12 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { RegisterInvariants(ir, am.keeper) } +// RegisterStoreDecoder registers the function to decode the types stored in the +// KVStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { + sdr[StoreKey] = simulation.DecodeStore +} + // module message route name func (AppModule) Route() string { return RouterKey diff --git a/x/mint/module.go b/x/mint/module.go index e8e07d94efd8..4b67f6c1e3bd 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -14,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/mint/client/cli" "github.com/cosmos/cosmos-sdk/x/mint/client/rest" + "github.com/cosmos/cosmos-sdk/x/mint/simulation" ) var ( @@ -85,6 +86,12 @@ func (AppModule) Name() string { // register invariants func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} +// RegisterStoreDecoder registers the function to decode the types stored in the +// KVStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { + sdr[StoreKey] = simulation.DecodeStore +} + // module message route name func (AppModule) Route() string { return "" } diff --git a/x/slashing/module.go b/x/slashing/module.go index e3ee17fc2c7e..1efd1442fcb5 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -15,6 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" "github.com/cosmos/cosmos-sdk/x/slashing/client/rest" "github.com/cosmos/cosmos-sdk/x/slashing/internal/types" + "github.com/cosmos/cosmos-sdk/x/slashing/simulation" ) var ( @@ -92,6 +93,12 @@ func (AppModule) Name() string { // register invariants func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} +// RegisterStoreDecoder registers the function to decode the types stored in the +// KVStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { + sdr[StoreKey] = simulation.DecodeStore +} + // module message route name func (AppModule) Route() string { return RouterKey diff --git a/x/staking/module.go b/x/staking/module.go index 8dcb26c66ec6..881873aea067 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -19,6 +19,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/client/cli" "github.com/cosmos/cosmos-sdk/x/staking/client/rest" "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/cosmos/cosmos-sdk/x/staking/simulation" ) var ( @@ -124,6 +125,12 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { RegisterInvariants(ir, am.keeper) } +// RegisterStoreDecoder registers the function to decode the types stored in the +// KVStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { + sdr[StoreKey] = simulation.DecodeStore +} + // module message route name func (AppModule) Route() string { return RouterKey diff --git a/x/supply/module.go b/x/supply/module.go index 3a987e2fa14d..2c92369c64c4 100644 --- a/x/supply/module.go +++ b/x/supply/module.go @@ -15,6 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/supply/client/cli" "github.com/cosmos/cosmos-sdk/x/supply/client/rest" "github.com/cosmos/cosmos-sdk/x/supply/internal/types" + "github.com/cosmos/cosmos-sdk/x/supply/simulation" ) var ( @@ -89,6 +90,12 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { RegisterInvariants(ir, am.keeper) } +// RegisterStoreDecoder registers the function to decode the types stored in the +// KVStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { + sdr[StoreKey] = simulation.DecodeStore +} + // module message route name func (AppModule) Route() string { return RouterKey From 5c41dd2ff128094cc34b4924607504fa8a195c35 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Wed, 7 Aug 2019 15:06:48 +0200 Subject: [PATCH 04/18] compile --- simapp/sim_test.go | 6 +-- simapp/utils.go | 9 +---- types/module/module.go | 9 ++++- types/store.go | 2 +- x/auth/module.go | 6 +-- x/auth/simulation/{ => decoder}/store.go | 8 ++-- x/auth/simulation/{ => decoder}/store_test.go | 12 +++--- x/bank/module.go | 3 ++ x/crisis/module.go | 3 ++ x/distribution/module.go | 6 +-- .../simulation/{ => decoder}/store.go | 37 +++++++++--------- .../simulation/{ => decoder}/store_test.go | 39 ++++++++++--------- x/distribution/simulation/msgs.go | 7 ++-- x/gov/module.go | 6 +-- x/gov/simulation/{ => decoder}/store.go | 22 +++++------ x/gov/simulation/{ => decoder}/store_test.go | 22 +++++------ x/mint/module.go | 2 +- x/mint/simulation/store.go | 6 +-- x/mint/simulation/store_test.go | 6 +-- x/slashing/module.go | 6 +-- x/slashing/simulation/{ => decoder}/store.go | 12 +++--- .../simulation/{ => decoder}/store_test.go | 14 +++---- x/staking/module.go | 6 +-- x/staking/simulation/{ => decoder}/store.go | 32 +++++++-------- .../simulation/{ => decoder}/store_test.go | 26 ++++++------- x/supply/module.go | 2 +- x/supply/simulation/store.go | 7 ++-- x/supply/simulation/store_test.go | 9 +++-- 28 files changed, 166 insertions(+), 159 deletions(-) rename x/auth/simulation/{ => decoder}/store.go (82%) rename x/auth/simulation/{ => decoder}/store_test.go (79%) rename x/distribution/simulation/{ => decoder}/store.go (61%) rename x/distribution/simulation/{ => decoder}/store_test.go (54%) rename x/gov/simulation/{ => decoder}/store.go (68%) rename x/gov/simulation/{ => decoder}/store_test.go (65%) rename x/slashing/simulation/{ => decoder}/store.go (78%) rename x/slashing/simulation/{ => decoder}/store_test.go (72%) rename x/staking/simulation/{ => decoder}/store.go (61%) rename x/staking/simulation/{ => decoder}/store_test.go (59%) diff --git a/simapp/sim_test.go b/simapp/sim_test.go index fd4c98334540..54edb79e67bc 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -217,7 +217,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - distrsim.SimulateMsgSetWithdrawAddress(app.accountKeeper, app.distrKeeper), + distrsim.SimulateMsgSetWithdrawAddress(app.distrKeeper), }, { func(_ *rand.Rand) int { @@ -228,7 +228,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - distrsim.SimulateMsgWithdrawDelegatorReward(app.accountKeeper, app.distrKeeper), + distrsim.SimulateMsgWithdrawDelegatorReward(app.distrKeeper), }, { func(_ *rand.Rand) int { @@ -239,7 +239,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - distrsim.SimulateMsgWithdrawValidatorCommission(app.accountKeeper, app.distrKeeper), + distrsim.SimulateMsgWithdrawValidatorCommission(app.distrKeeper), }, { func(_ *rand.Rand) int { diff --git a/simapp/utils.go b/simapp/utils.go index b3c38cb22440..778658b0e763 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -19,22 +19,15 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - authsim "github.com/cosmos/cosmos-sdk/x/auth/simulation" "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/distribution" - distributionsim "github.com/cosmos/cosmos-sdk/x/distribution/simulation" "github.com/cosmos/cosmos-sdk/x/genaccounts" "github.com/cosmos/cosmos-sdk/x/gov" - govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation" "github.com/cosmos/cosmos-sdk/x/mint" - mintsim "github.com/cosmos/cosmos-sdk/x/mint/simulation" "github.com/cosmos/cosmos-sdk/x/simulation" "github.com/cosmos/cosmos-sdk/x/slashing" - slashingsim "github.com/cosmos/cosmos-sdk/x/slashing/simulation" "github.com/cosmos/cosmos-sdk/x/staking" - stakingsim "github.com/cosmos/cosmos-sdk/x/staking/simulation" "github.com/cosmos/cosmos-sdk/x/supply" - supplysim "github.com/cosmos/cosmos-sdk/x/supply/simulation" ) // List of available flags for the simulator @@ -505,7 +498,7 @@ func GenStakingGenesisState( // 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(sdr sdk.StoreDecoderRegistry, storeName string, cdcA, cdcB *codec.Codec, kvs []cmn.KVPair) (log string) { - if kvs%2 != 0 { + if len(kvs)%2 != 0 { panic("KVPairs are not multiple of 2. There should be one for each app store") } diff --git a/types/module/module.go b/types/module/module.go index c81ff0533e45..288d6dd9488c 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -132,6 +132,7 @@ type AppModule interface { // registers RegisterInvariants(sdk.InvariantRegistry) + RegisterStoreDecoder(sdk.StoreDecoderRegistry) // routes Route() string @@ -139,6 +140,7 @@ type AppModule interface { QuerierRoute() string NewQuerierHandler() sdk.Querier + // ABCI BeginBlock(sdk.Context, abci.RequestBeginBlock) EndBlock(sdk.Context, abci.RequestEndBlock) []abci.ValidatorUpdate } @@ -159,6 +161,9 @@ func NewGenesisOnlyAppModule(amg AppModuleGenesis) AppModule { // register invariants func (GenesisOnlyAppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} +// RegisterStoreDecoder empty store decoder registry +func (GenesisOnlyAppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} + // module message route ngame func (GenesisOnlyAppModule) Route() string { return "" } @@ -236,8 +241,8 @@ func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) { } } -// register all module routes and module querier routes -func (m *Manager) RegisterStoreDecoders(sd map[string]func(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string) { +// RegisterStoreDecoders registers the each type decoder with the respective key +func (m *Manager) RegisterStoreDecoders(sd sdk.StoreDecoderRegistry) { for _, module := range m.Modules { module.RegisterStoreDecoder(sd) } diff --git a/types/store.go b/types/store.go index f0ff87a6abb9..3146db310f2d 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(cdcA, cdcB *codec.Codec, kvA, kvB KVPair) string +type StoreDecoderRegistry map[string]func(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) 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 29929c29d222..274a72c02633 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -14,7 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/auth/client/rest" "github.com/cosmos/cosmos-sdk/x/auth/types" - "github.com/cosmos/cosmos-sdk/x/auth/simulation" + "github.com/cosmos/cosmos-sdk/x/auth/simulation/decoder" ) var ( @@ -90,8 +90,8 @@ func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterStoreDecoder registers the function to decode the types stored in the // KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore } // module message route name diff --git a/x/auth/simulation/store.go b/x/auth/simulation/decoder/store.go similarity index 82% rename from x/auth/simulation/store.go rename to x/auth/simulation/decoder/store.go index 528d756849e7..a539bfb1a787 100644 --- a/x/auth/simulation/store.go +++ b/x/auth/simulation/decoder/store.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "bytes" @@ -8,19 +8,19 @@ import ( "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/auth/exported" ) // DecodeStore unmarshals the KVPair's Value to the corresponding auth type func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { - case bytes.Equal(kvA.Key[:1], auth.AddressStoreKeyPrefix): + case bytes.Equal(kvA.Key[:1], types.AddressStoreKeyPrefix): var accA, accB exported.Account cdcA.MustUnmarshalBinaryBare(kvA.Value, &accA) cdcB.MustUnmarshalBinaryBare(kvB.Value, &accB) return fmt.Sprintf("%v\n%v", accA, accB) - case bytes.Equal(kvA.Key, auth.GlobalAccountNumberKey): + case bytes.Equal(kvA.Key, types.GlobalAccountNumberKey): var globalAccNumberA, globalAccNumberB uint64 cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &globalAccNumberA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &globalAccNumberB) diff --git a/x/auth/simulation/store_test.go b/x/auth/simulation/decoder/store_test.go similarity index 79% rename from x/auth/simulation/store_test.go rename to x/auth/simulation/decoder/store_test.go index c59f6ab8eec6..3a92ec976dee 100644 --- a/x/auth/simulation/store_test.go +++ b/x/auth/simulation/decoder/store_test.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "fmt" @@ -11,7 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -27,18 +27,18 @@ func makeTestCodec() (cdc *codec.Codec) { cdc = codec.New() sdk.RegisterCodec(cdc) codec.RegisterCrypto(cdc) - auth.RegisterCodec(cdc) + types.RegisterCodec(cdc) return } func TestDecodeStore(t *testing.T) { cdc := makeTestCodec() - acc := auth.NewBaseAccountWithAddress(delAddr1) + acc := types.NewBaseAccountWithAddress(delAddr1) globalAccNumber := uint64(10) kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: auth.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(acc)}, - cmn.KVPair{Key: auth.GlobalAccountNumberKey, Value: cdc.MustMarshalBinaryLengthPrefixed(globalAccNumber)}, + cmn.KVPair{Key: types.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(acc)}, + cmn.KVPair{Key: types.GlobalAccountNumberKey, Value: cdc.MustMarshalBinaryLengthPrefixed(globalAccNumber)}, cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, } tests := []struct { diff --git a/x/bank/module.go b/x/bank/module.go index bd0a8d54dbc6..f5c49eecac6f 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -85,6 +85,9 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { keeper.RegisterInvariants(ir, am.accountKeeper) } +// RegisterStoreDecoder doesn't register any KVPair +func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} + // module message route name func (AppModule) Route() string { return RouterKey } diff --git a/x/crisis/module.go b/x/crisis/module.go index bbcdaf0f2247..87474b07585d 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -85,6 +85,9 @@ func (AppModule) Name() string { // register invariants func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} +// RegisterStoreDecoder doesn't register any KVPair +func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} + // module querier route name func (AppModule) Route() string { return RouterKey diff --git a/x/distribution/module.go b/x/distribution/module.go index 4cae8b3a42e5..58192b1b7df7 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -15,7 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/distribution/client/cli" "github.com/cosmos/cosmos-sdk/x/distribution/client/rest" "github.com/cosmos/cosmos-sdk/x/distribution/types" - "github.com/cosmos/cosmos-sdk/x/distribution/simulation" + "github.com/cosmos/cosmos-sdk/x/distribution/simulation/decoder" ) var ( @@ -94,8 +94,8 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { // RegisterStoreDecoder registers the function to decode the types stored in the // KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore } // module message route name diff --git a/x/distribution/simulation/store.go b/x/distribution/simulation/decoder/store.go similarity index 61% rename from x/distribution/simulation/store.go rename to x/distribution/simulation/decoder/store.go index 26b919e64830..7207b7bb9c49 100644 --- a/x/distribution/simulation/store.go +++ b/x/distribution/simulation/decoder/store.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "bytes" @@ -9,56 +9,57 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/distribution" + "github.com/cosmos/cosmos-sdk/x/distribution/keeper" + "github.com/cosmos/cosmos-sdk/x/distribution/types" ) // DecodeStore unmarshals the KVPair's Value to the corresponding distribution type func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { - case bytes.Equal(kvA.Key[:1], distribution.FeePoolKey): - var feePoolA, feePoolB distribution.FeePool + case bytes.Equal(kvA.Key[:1], keeper.FeePoolKey): + var feePoolA, feePoolB types.FeePool cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &feePoolA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &feePoolB) return fmt.Sprintf("%v\n%v", feePoolA, feePoolB) - case bytes.Equal(kvA.Key[:1], distribution.ProposerKey): + case bytes.Equal(kvA.Key[:1], keeper.ProposerKey): return fmt.Sprintf("%v\n%v", sdk.ConsAddress(kvA.Value), sdk.ConsAddress(kvB.Value)) - case bytes.Equal(kvA.Key[:1], distribution.ValidatorOutstandingRewardsPrefix): - var rewardsA, rewardsB distribution.ValidatorOutstandingRewards + case bytes.Equal(kvA.Key[:1], keeper.ValidatorOutstandingRewardsPrefix): + var rewardsA, rewardsB types.ValidatorOutstandingRewards cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - case bytes.Equal(kvA.Key[:1], distribution.DelegatorWithdrawAddrPrefix): + case bytes.Equal(kvA.Key[:1], keeper.DelegatorWithdrawAddrPrefix): return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value)) - case bytes.Equal(kvA.Key[:1], distribution.DelegatorStartingInfoPrefix): - var infoA, infoB distribution.DelegatorStartingInfo + case bytes.Equal(kvA.Key[:1], keeper.DelegatorStartingInfoPrefix): + var infoA, infoB types.DelegatorStartingInfo cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &infoA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &infoB) return fmt.Sprintf("%v\n%v", infoA, infoB) - case bytes.Equal(kvA.Key[:1], distribution.ValidatorHistoricalRewardsPrefix): - var rewardsA, rewardsB distribution.ValidatorHistoricalRewards + case bytes.Equal(kvA.Key[:1], keeper.ValidatorHistoricalRewardsPrefix): + var rewardsA, rewardsB types.ValidatorHistoricalRewards cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - case bytes.Equal(kvA.Key[:1], distribution.ValidatorCurrentRewardsPrefix): - var rewardsA, rewardsB distribution.ValidatorCurrentRewards + case bytes.Equal(kvA.Key[:1], keeper.ValidatorCurrentRewardsPrefix): + var rewardsA, rewardsB types.ValidatorCurrentRewards cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - case bytes.Equal(kvA.Key[:1], distribution.ValidatorAccumulatedCommissionPrefix): - var commissionA, commissionB distribution.ValidatorAccumulatedCommission + case bytes.Equal(kvA.Key[:1], keeper.ValidatorAccumulatedCommissionPrefix): + var commissionA, commissionB types.ValidatorAccumulatedCommission cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &commissionA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &commissionB) return fmt.Sprintf("%v\n%v", commissionA, commissionB) - case bytes.Equal(kvA.Key[:1], distribution.ValidatorSlashEventPrefix): - var eventA, eventB distribution.ValidatorSlashEvent + case bytes.Equal(kvA.Key[:1], keeper.ValidatorSlashEventPrefix): + var eventA, eventB types.ValidatorSlashEvent cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &eventA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &eventB) return fmt.Sprintf("%v\n%v", eventA, eventB) diff --git a/x/distribution/simulation/store_test.go b/x/distribution/simulation/decoder/store_test.go similarity index 54% rename from x/distribution/simulation/store_test.go rename to x/distribution/simulation/decoder/store_test.go index f0cc7caea871..cdae27cae1e7 100644 --- a/x/distribution/simulation/store_test.go +++ b/x/distribution/simulation/decoder/store_test.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "fmt" @@ -11,7 +11,8 @@ import ( "github.com/cosmos/cosmos-sdk/codec" - distr "github.com/cosmos/cosmos-sdk/x/distribution" + "github.com/cosmos/cosmos-sdk/x/distribution/keeper" + "github.com/cosmos/cosmos-sdk/x/distribution/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -27,7 +28,7 @@ func makeTestCodec() (cdc *codec.Codec) { cdc = codec.New() sdk.RegisterCodec(cdc) codec.RegisterCrypto(cdc) - distr.RegisterCodec(cdc) + types.RegisterCodec(cdc) return } @@ -35,25 +36,25 @@ func TestDecodeDistributionStore(t *testing.T) { cdc := makeTestCodec() decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.OneDec())} - feePool := distr.InitialFeePool() + feePool := types.InitialFeePool() feePool.CommunityPool = decCoins - info := distr.NewDelegatorStartingInfo(2, sdk.OneDec(), 200) - outstanding := distr.ValidatorOutstandingRewards{decCoins[0]} - commission := distr.ValidatorAccumulatedCommission{decCoins[0]} - historicalRewards := distr.NewValidatorHistoricalRewards(decCoins, 100) - currentRewards := distr.NewValidatorCurrentRewards(decCoins, 5) - slashEvent := distr.NewValidatorSlashEvent(10, sdk.OneDec()) + info := types.NewDelegatorStartingInfo(2, sdk.OneDec(), 200) + outstanding := types.ValidatorOutstandingRewards{decCoins[0]} + commission := types.ValidatorAccumulatedCommission{decCoins[0]} + historicalRewards := types.NewValidatorHistoricalRewards(decCoins, 100) + currentRewards := types.NewValidatorCurrentRewards(decCoins, 5) + slashEvent := types.NewValidatorSlashEvent(10, sdk.OneDec()) kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: distr.FeePoolKey, Value: cdc.MustMarshalBinaryLengthPrefixed(feePool)}, - cmn.KVPair{Key: distr.ProposerKey, Value: consAddr1.Bytes()}, - cmn.KVPair{Key: distr.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(outstanding)}, - cmn.KVPair{Key: distr.GetDelegatorWithdrawAddrKey(delAddr1), Value: delAddr1.Bytes()}, - cmn.KVPair{Key: distr.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(info)}, - cmn.KVPair{Key: distr.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshalBinaryLengthPrefixed(historicalRewards)}, - cmn.KVPair{Key: distr.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(currentRewards)}, - cmn.KVPair{Key: distr.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(commission)}, - cmn.KVPair{Key: distr.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshalBinaryLengthPrefixed(slashEvent)}, + cmn.KVPair{Key: keeper.FeePoolKey, Value: cdc.MustMarshalBinaryLengthPrefixed(feePool)}, + cmn.KVPair{Key: keeper.ProposerKey, Value: consAddr1.Bytes()}, + cmn.KVPair{Key: keeper.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(outstanding)}, + cmn.KVPair{Key: keeper.GetDelegatorWithdrawAddrKey(delAddr1), Value: delAddr1.Bytes()}, + cmn.KVPair{Key: keeper.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(info)}, + cmn.KVPair{Key: keeper.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshalBinaryLengthPrefixed(historicalRewards)}, + cmn.KVPair{Key: keeper.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(currentRewards)}, + cmn.KVPair{Key: keeper.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(commission)}, + cmn.KVPair{Key: keeper.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshalBinaryLengthPrefixed(slashEvent)}, cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, } diff --git a/x/distribution/simulation/msgs.go b/x/distribution/simulation/msgs.go index ae002010c17a..30784ca999a8 100644 --- a/x/distribution/simulation/msgs.go +++ b/x/distribution/simulation/msgs.go @@ -6,7 +6,6 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/distribution" "github.com/cosmos/cosmos-sdk/x/gov" govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation" @@ -14,7 +13,7 @@ import ( ) // SimulateMsgSetWithdrawAddress generates a MsgSetWithdrawAddress with random values. -func SimulateMsgSetWithdrawAddress(m auth.AccountKeeper, k distribution.Keeper) simulation.Operation { +func SimulateMsgSetWithdrawAddress(k distribution.Keeper) simulation.Operation { handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { @@ -39,7 +38,7 @@ func SimulateMsgSetWithdrawAddress(m auth.AccountKeeper, k distribution.Keeper) } // SimulateMsgWithdrawDelegatorReward generates a MsgWithdrawDelegatorReward with random values. -func SimulateMsgWithdrawDelegatorReward(m auth.AccountKeeper, k distribution.Keeper) simulation.Operation { +func SimulateMsgWithdrawDelegatorReward(k distribution.Keeper) simulation.Operation { handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { @@ -64,7 +63,7 @@ func SimulateMsgWithdrawDelegatorReward(m auth.AccountKeeper, k distribution.Kee } // SimulateMsgWithdrawValidatorCommission generates a MsgWithdrawValidatorCommission with random values. -func SimulateMsgWithdrawValidatorCommission(m auth.AccountKeeper, k distribution.Keeper) simulation.Operation { +func SimulateMsgWithdrawValidatorCommission(k distribution.Keeper) simulation.Operation { handler := distribution.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { diff --git a/x/gov/module.go b/x/gov/module.go index 8b5fcd64d2ed..6ea3611fe210 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -16,7 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/gov/client/cli" "github.com/cosmos/cosmos-sdk/x/gov/client/rest" "github.com/cosmos/cosmos-sdk/x/gov/types" - "github.com/cosmos/cosmos-sdk/x/gov/simulation" + "github.com/cosmos/cosmos-sdk/x/gov/simulation/decoder" ) var ( @@ -118,8 +118,8 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { // RegisterStoreDecoder registers the function to decode the types stored in the // KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore } // module message route name diff --git a/x/gov/simulation/store.go b/x/gov/simulation/decoder/store.go similarity index 68% rename from x/gov/simulation/store.go rename to x/gov/simulation/decoder/store.go index 3c30fb51f904..3c67fa33aed2 100644 --- a/x/gov/simulation/store.go +++ b/x/gov/simulation/decoder/store.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "bytes" @@ -9,33 +9,33 @@ import ( "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/gov" + "github.com/cosmos/cosmos-sdk/x/gov/types" ) // DecodeStore unmarshals the KVPair's Value to the corresponding gov type func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { - case bytes.Equal(kvA.Key[:1], gov.ProposalsKeyPrefix): - var proposalA, proposalB gov.Proposal + case bytes.Equal(kvA.Key[:1], types.ProposalsKeyPrefix): + var proposalA, proposalB types.Proposal cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &proposalA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &proposalB) return fmt.Sprintf("%v\n%v", proposalA, proposalB) - case bytes.Equal(kvA.Key[:1], gov.ActiveProposalQueuePrefix), - bytes.Equal(kvA.Key[:1], gov.InactiveProposalQueuePrefix), - bytes.Equal(kvA.Key[:1], gov.ProposalIDKey): + 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], gov.DepositsKeyPrefix): - var depositA, depositB gov.Deposit + case bytes.Equal(kvA.Key[:1], types.DepositsKeyPrefix): + var depositA, depositB types.Deposit cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &depositA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &depositB) return fmt.Sprintf("%v\n%v", depositA, depositB) - case bytes.Equal(kvA.Key[:1], gov.VotesKeyPrefix): - var voteA, voteB gov.Vote + case bytes.Equal(kvA.Key[:1], types.VotesKeyPrefix): + var voteA, voteB types.Vote cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &voteA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &voteB) return fmt.Sprintf("%v\n%v", voteA, voteB) diff --git a/x/gov/simulation/store_test.go b/x/gov/simulation/decoder/store_test.go similarity index 65% rename from x/gov/simulation/store_test.go rename to x/gov/simulation/decoder/store_test.go index 286c7c50fe3c..fb8cc907d8ed 100644 --- a/x/gov/simulation/store_test.go +++ b/x/gov/simulation/decoder/store_test.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "encoding/binary" @@ -13,7 +13,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/gov" + "github.com/cosmos/cosmos-sdk/x/gov/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -29,7 +29,7 @@ func makeTestCodec() (cdc *codec.Codec) { cdc = codec.New() sdk.RegisterCodec(cdc) codec.RegisterCrypto(cdc) - gov.RegisterCodec(cdc) + types.RegisterCodec(cdc) return } @@ -38,18 +38,18 @@ func TestDecodeStore(t *testing.T) { endTime := time.Now().UTC() - content := gov.ContentFromProposalType("test", "test", gov.ProposalTypeText) - proposal := gov.NewProposal(content, 1, endTime, endTime.Add(24*time.Hour)) + content := types.ContentFromProposalType("test", "test", types.ProposalTypeText) + proposal := types.NewProposal(content, 1, endTime, endTime.Add(24*time.Hour)) proposalIDBz := make([]byte, 8) binary.LittleEndian.PutUint64(proposalIDBz, 1) - deposit := gov.NewDeposit(1, delAddr1, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt()))) - vote := gov.NewVote(1, delAddr1, gov.OptionYes) + deposit := types.NewDeposit(1, delAddr1, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt()))) + vote := types.NewVote(1, delAddr1, types.OptionYes) kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: gov.ProposalKey(1), Value: cdc.MustMarshalBinaryLengthPrefixed(proposal)}, - cmn.KVPair{Key: gov.InactiveProposalQueueKey(1, endTime), Value: proposalIDBz}, - cmn.KVPair{Key: gov.DepositKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(deposit)}, - cmn.KVPair{Key: gov.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(vote)}, + cmn.KVPair{Key: types.ProposalKey(1), Value: cdc.MustMarshalBinaryLengthPrefixed(proposal)}, + cmn.KVPair{Key: types.InactiveProposalQueueKey(1, endTime), Value: proposalIDBz}, + cmn.KVPair{Key: types.DepositKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(deposit)}, + cmn.KVPair{Key: types.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(vote)}, cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, } diff --git a/x/mint/module.go b/x/mint/module.go index 4b67f6c1e3bd..a3d0549f449d 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -88,7 +88,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterStoreDecoder registers the function to decode the types stored in the // KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { sdr[StoreKey] = simulation.DecodeStore } diff --git a/x/mint/simulation/store.go b/x/mint/simulation/store.go index 3314ead57b32..e38b57b66fae 100644 --- a/x/mint/simulation/store.go +++ b/x/mint/simulation/store.go @@ -8,14 +8,14 @@ import ( "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/mint" + "github.com/cosmos/cosmos-sdk/x/mint/internal/types" ) // DecodeStore unmarshals the KVPair's Value to the corresponding mint type func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { - case bytes.Equal(kvA.Key, mint.MinterKey): - var minterA, minterB mint.Minter + case bytes.Equal(kvA.Key, types.MinterKey): + var minterA, minterB types.Minter cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &minterA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &minterB) return fmt.Sprintf("%v\n%v", minterA, minterB) diff --git a/x/mint/simulation/store_test.go b/x/mint/simulation/store_test.go index 8a02ee126721..6b1fc84f4815 100644 --- a/x/mint/simulation/store_test.go +++ b/x/mint/simulation/store_test.go @@ -10,7 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/mint" + "github.com/cosmos/cosmos-sdk/x/mint/internal/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -23,10 +23,10 @@ func makeTestCodec() (cdc *codec.Codec) { func TestDecodeStore(t *testing.T) { cdc := makeTestCodec() - minter := mint.NewMinter(sdk.OneDec(), sdk.NewDec(15)) + minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15)) kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: mint.MinterKey, Value: cdc.MustMarshalBinaryLengthPrefixed(minter)}, + cmn.KVPair{Key: types.MinterKey, Value: cdc.MustMarshalBinaryLengthPrefixed(minter)}, cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, } tests := []struct { diff --git a/x/slashing/module.go b/x/slashing/module.go index 1efd1442fcb5..ca74439780e0 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -15,7 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" "github.com/cosmos/cosmos-sdk/x/slashing/client/rest" "github.com/cosmos/cosmos-sdk/x/slashing/internal/types" - "github.com/cosmos/cosmos-sdk/x/slashing/simulation" + "github.com/cosmos/cosmos-sdk/x/slashing/simulation/decoder" ) var ( @@ -95,8 +95,8 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterStoreDecoder registers the function to decode the types stored in the // KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore } // module message route name diff --git a/x/slashing/simulation/store.go b/x/slashing/simulation/decoder/store.go similarity index 78% rename from x/slashing/simulation/store.go rename to x/slashing/simulation/decoder/store.go index 9f17fc59fda2..701fca3caa05 100644 --- a/x/slashing/simulation/store.go +++ b/x/slashing/simulation/decoder/store.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "bytes" @@ -10,25 +10,25 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/slashing" + "github.com/cosmos/cosmos-sdk/x/slashing/internal/types" ) // DecodeStore unmarshals the KVPair's Value to the corresponding slashing type func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { - case bytes.Equal(kvA.Key[:1], slashing.ValidatorSigningInfoKey): - var infoA, infoB slashing.ValidatorSigningInfo + case bytes.Equal(kvA.Key[:1], types.ValidatorSigningInfoKey): + var infoA, infoB types.ValidatorSigningInfo cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &infoA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &infoB) return fmt.Sprintf("%v\n%v", infoA, infoB) - case bytes.Equal(kvA.Key[:1], slashing.ValidatorMissedBlockBitArrayKey): + case bytes.Equal(kvA.Key[:1], types.ValidatorMissedBlockBitArrayKey): var missedA, missedB bool cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &missedA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &missedB) return fmt.Sprintf("missedA: %v\nmissedB: %v", missedA, missedB) - case bytes.Equal(kvA.Key[:1], slashing.AddrPubkeyRelationKey): + case bytes.Equal(kvA.Key[:1], types.AddrPubkeyRelationKey): var pubKeyA, pubKeyB crypto.PubKey cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &pubKeyA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &pubKeyB) diff --git a/x/slashing/simulation/store_test.go b/x/slashing/simulation/decoder/store_test.go similarity index 72% rename from x/slashing/simulation/store_test.go rename to x/slashing/simulation/decoder/store_test.go index 3de4116a2fde..c6e38c61a433 100644 --- a/x/slashing/simulation/store_test.go +++ b/x/slashing/simulation/decoder/store_test.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "fmt" @@ -11,7 +11,7 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/slashing" + "github.com/cosmos/cosmos-sdk/x/slashing/internal/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -27,21 +27,21 @@ func makeTestCodec() (cdc *codec.Codec) { cdc = codec.New() sdk.RegisterCodec(cdc) codec.RegisterCrypto(cdc) - slashing.RegisterCodec(cdc) + types.RegisterCodec(cdc) return } func TestDecodeStore(t *testing.T) { cdc := makeTestCodec() - info := slashing.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0) + info := types.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0) bechPK := sdk.MustBech32ifyAccPub(delPk1) missed := true kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: slashing.GetValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(info)}, - cmn.KVPair{Key: slashing.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryLengthPrefixed(missed)}, - cmn.KVPair{Key: slashing.GetAddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(delPk1)}, + cmn.KVPair{Key: types.GetValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(info)}, + cmn.KVPair{Key: types.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryLengthPrefixed(missed)}, + cmn.KVPair{Key: types.GetAddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(delPk1)}, cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, } diff --git a/x/staking/module.go b/x/staking/module.go index 881873aea067..59dc5173a1ac 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -19,7 +19,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/client/cli" "github.com/cosmos/cosmos-sdk/x/staking/client/rest" "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/cosmos/cosmos-sdk/x/staking/simulation" + "github.com/cosmos/cosmos-sdk/x/staking/simulation/decoder" ) var ( @@ -127,8 +127,8 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { // RegisterStoreDecoder registers the function to decode the types stored in the // KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { - sdr[StoreKey] = simulation.DecodeStore +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore } // module message route name diff --git a/x/staking/simulation/store.go b/x/staking/simulation/decoder/store.go similarity index 61% rename from x/staking/simulation/store.go rename to x/staking/simulation/decoder/store.go index 7e82c144a7d8..8b4fcf24a457 100644 --- a/x/staking/simulation/store.go +++ b/x/staking/simulation/decoder/store.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "bytes" @@ -9,45 +9,45 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/staking/types" ) // DecodeStore unmarshals the KVPair's Value to the corresponding staking type func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { - case bytes.Equal(kvA.Key[:1], staking.LastTotalPowerKey): + case bytes.Equal(kvA.Key[:1], types.LastTotalPowerKey): var powerA, powerB sdk.Int cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &powerA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &powerB) return fmt.Sprintf("%v\n%v", powerA, powerB) - case bytes.Equal(kvA.Key[:1], staking.ValidatorsKey): - var validatorA, validatorB staking.Validator + case bytes.Equal(kvA.Key[:1], types.ValidatorsKey): + var validatorA, validatorB types.Validator cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &validatorA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &validatorB) return fmt.Sprintf("%v\n%v", validatorA, validatorB) - case bytes.Equal(kvA.Key[:1], staking.LastValidatorPowerKey), - bytes.Equal(kvA.Key[:1], staking.ValidatorsByConsAddrKey), - bytes.Equal(kvA.Key[:1], staking.ValidatorsByPowerIndexKey): + 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], staking.DelegationKey): - var delegationA, delegationB staking.Delegation + case bytes.Equal(kvA.Key[:1], types.DelegationKey): + var delegationA, delegationB types.Delegation cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &delegationA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &delegationB) return fmt.Sprintf("%v\n%v", delegationA, delegationB) - case bytes.Equal(kvA.Key[:1], staking.UnbondingDelegationKey), - bytes.Equal(kvA.Key[:1], staking.UnbondingDelegationByValIndexKey): - var ubdA, ubdB staking.UnbondingDelegation + case bytes.Equal(kvA.Key[:1], types.UnbondingDelegationKey), + bytes.Equal(kvA.Key[:1], types.UnbondingDelegationByValIndexKey): + var ubdA, ubdB types.UnbondingDelegation cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &ubdA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &ubdB) return fmt.Sprintf("%v\n%v", ubdA, ubdB) - case bytes.Equal(kvA.Key[:1], staking.RedelegationKey), - bytes.Equal(kvA.Key[:1], staking.RedelegationByValSrcIndexKey): - var redA, redB staking.Redelegation + case bytes.Equal(kvA.Key[:1], types.RedelegationKey), + bytes.Equal(kvA.Key[:1], types.RedelegationByValSrcIndexKey): + var redA, redB types.Redelegation cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &redA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &redB) return fmt.Sprintf("%v\n%v", redA, redB) diff --git a/x/staking/simulation/store_test.go b/x/staking/simulation/decoder/store_test.go similarity index 59% rename from x/staking/simulation/store_test.go rename to x/staking/simulation/decoder/store_test.go index 7df3d8b4ee7f..8a5533342bff 100644 --- a/x/staking/simulation/store_test.go +++ b/x/staking/simulation/decoder/store_test.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "fmt" @@ -11,7 +11,7 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/staking/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -27,7 +27,7 @@ func makeTestCodec() (cdc *codec.Codec) { cdc = codec.New() sdk.RegisterCodec(cdc) codec.RegisterCrypto(cdc) - staking.RegisterCodec(cdc) + types.RegisterCodec(cdc) return } @@ -36,18 +36,18 @@ func TestDecodeStore(t *testing.T) { bondTime := time.Now().UTC() - val := staking.NewValidator(valAddr1, delPk1, staking.NewDescription("test", "test", "test", "test")) - del := staking.NewDelegation(delAddr1, valAddr1, sdk.OneDec()) - ubd := staking.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, sdk.OneInt()) - red := staking.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, sdk.OneInt(), sdk.OneDec()) + val := types.NewValidator(valAddr1, delPk1, types.NewDescription("test", "test", "test", "test")) + del := types.NewDelegation(delAddr1, valAddr1, sdk.OneDec()) + ubd := types.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, sdk.OneInt()) + red := types.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, sdk.OneInt(), sdk.OneDec()) kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: staking.LastTotalPowerKey, Value: cdc.MustMarshalBinaryLengthPrefixed(sdk.OneInt())}, - cmn.KVPair{Key: staking.GetValidatorKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(val)}, - cmn.KVPair{Key: staking.LastValidatorPowerKey, Value: valAddr1.Bytes()}, - cmn.KVPair{Key: staking.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(del)}, - cmn.KVPair{Key: staking.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(ubd)}, - cmn.KVPair{Key: staking.GetREDKey(delAddr1, valAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(red)}, + cmn.KVPair{Key: types.LastTotalPowerKey, Value: cdc.MustMarshalBinaryLengthPrefixed(sdk.OneInt())}, + cmn.KVPair{Key: types.GetValidatorKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(val)}, + cmn.KVPair{Key: types.LastValidatorPowerKey, Value: valAddr1.Bytes()}, + cmn.KVPair{Key: types.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(del)}, + cmn.KVPair{Key: types.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(ubd)}, + cmn.KVPair{Key: types.GetREDKey(delAddr1, valAddr1, valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(red)}, cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, } diff --git a/x/supply/module.go b/x/supply/module.go index 2c92369c64c4..91a6fc11dfd9 100644 --- a/x/supply/module.go +++ b/x/supply/module.go @@ -92,7 +92,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { // RegisterStoreDecoder registers the function to decode the types stored in the // KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) string) { +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { sdr[StoreKey] = simulation.DecodeStore } diff --git a/x/supply/simulation/store.go b/x/supply/simulation/store.go index 80ec57947ba2..7a322be368f9 100644 --- a/x/supply/simulation/store.go +++ b/x/supply/simulation/store.go @@ -8,14 +8,15 @@ import ( "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/supply" + "github.com/cosmos/cosmos-sdk/x/supply/internal/keeper" + "github.com/cosmos/cosmos-sdk/x/supply/internal/types" ) // DecodeStore unmarshals the KVPair's Value to the corresponding supply type func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { - case bytes.Equal(kvA.Key[:1], supply.SupplyKey): - var supplyA, supplyB supply.Supply + case bytes.Equal(kvA.Key[:1], keeper.SupplyKey): + var supplyA, supplyB types.Supply cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &supplyA) cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &supplyB) return fmt.Sprintf("%v\n%v", supplyB, supplyB) diff --git a/x/supply/simulation/store_test.go b/x/supply/simulation/store_test.go index 63915d6368e0..6b22530766de 100644 --- a/x/supply/simulation/store_test.go +++ b/x/supply/simulation/store_test.go @@ -10,7 +10,8 @@ import ( "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/supply" + "github.com/cosmos/cosmos-sdk/x/supply/internal/keeper" + "github.com/cosmos/cosmos-sdk/x/supply/internal/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -19,16 +20,16 @@ func makeTestCodec() (cdc *codec.Codec) { cdc = codec.New() sdk.RegisterCodec(cdc) codec.RegisterCrypto(cdc) - supply.RegisterCodec(cdc) + types.RegisterCodec(cdc) return } func TestDecodeStore(t *testing.T) { cdc := makeTestCodec() - totalSupply := supply.NewSupply(sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000))) + totalSupply := types.NewSupply(sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000))) kvPairs := cmn.KVPairs{ - cmn.KVPair{Key: supply.SupplyKey, Value: cdc.MustMarshalBinaryLengthPrefixed(totalSupply)}, + cmn.KVPair{Key: keeper.SupplyKey, Value: cdc.MustMarshalBinaryLengthPrefixed(totalSupply)}, cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, } From 865a5e48d8a11589c7b48d3bc4a11d93a9288c2a Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Wed, 7 Aug 2019 15:53:25 +0200 Subject: [PATCH 05/18] update Decoders params --- simapp/app.go | 3 +- simapp/sim_test.go | 4 +-- simapp/utils.go | 6 ++-- simapp/utils_test.go | 2 +- types/module/module.go | 7 +++-- types/store.go | 2 +- x/auth/simulation/decoder/store.go | 10 +++---- x/auth/simulation/decoder/store_test.go | 4 +-- x/distribution/simulation/decoder/store.go | 30 +++++++++---------- .../simulation/decoder/store_test.go | 4 +-- x/gov/simulation/decoder/store.go | 14 ++++----- x/mint/simulation/store.go | 6 ++-- x/mint/simulation/store_test.go | 4 +-- x/slashing/simulation/decoder/store.go | 14 ++++----- x/slashing/simulation/decoder/store_test.go | 4 +-- x/staking/simulation/decoder/store.go | 22 +++++++------- x/staking/simulation/decoder/store_test.go | 4 +-- x/supply/simulation/store.go | 6 ++-- x/supply/simulation/store_test.go | 4 +-- 19 files changed, 76 insertions(+), 74 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 28e9021f554e..290104444ade 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -110,7 +110,6 @@ func NewSimApp( ) *SimApp { cdc := MakeCodec() - storeDecoderRegistry := make(sdk.StoreDecoderRegistry) bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...) bApp.SetCommitMultiStoreTracer(traceStore) @@ -195,7 +194,7 @@ func NewSimApp( app.mm.RegisterInvariants(&app.crisisKeeper) app.mm.RegisterRoutes(app.Router(), app.QueryRouter()) - app.mm.RegisterStoreDecoders(storeDecoderRegistry) + app.mm.RegisterStoreDecoders() // initialize stores app.MountKVStores(keys) diff --git a/simapp/sim_test.go b/simapp/sim_test.go index 54edb79e67bc..59f01decedd9 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -599,9 +599,9 @@ func TestAppImportExport(t *testing.T) { prefixes := storeKeysPrefix.Prefixes storeA := ctxA.KVStore(storeKeyA) storeB := ctxB.KVStore(storeKeyB) - failedKVs := sdk.DiffKVStores(storeA, storeB, prefixes) + failedKVs := sdk.DiffKVStores(storeA, storeB, prefixes) fmt.Printf("Compared %d key/value pairs between %s and %s\n", len(failedKVs)/2, storeKeyA, storeKeyB) - require.Len(t, failedKVs, 0, GetSimulationLog(storeKeyA.Name(), app.cdc, newApp.cdc, failedKVs)) + require.Len(t, failedKVs, 0, GetSimulationLog(storeKeyA.Name(), app.mm.StoreDecoder, app.cdc, failedKVs)) } } diff --git a/simapp/utils.go b/simapp/utils.go index 778658b0e763..a09101d03d0f 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -497,7 +497,7 @@ func GenStakingGenesisState( // 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(sdr sdk.StoreDecoderRegistry, storeName string, cdcA, cdcB *codec.Codec, kvs []cmn.KVPair) (log string) { +func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, cdc *codec.Codec, kvs []cmn.KVPair) (log string) { if len(kvs)%2 != 0 { panic("KVPairs are not multiple of 2. There should be one for each app store") } @@ -511,10 +511,10 @@ func GetSimulationLog(sdr sdk.StoreDecoderRegistry, storeName string, cdcA, cdcB // skip if the value doesn't have any bytes continue } - + decoder, ok := sdr[storeName] if ok { - log += decoder(cdcA, cdcB, kvA, kvB) + log += decoder(cdc, kvA, kvB) } else { log += fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvA.Key, kvA.Value, kvB.Key, kvB.Value) } diff --git a/simapp/utils_test.go b/simapp/utils_test.go index 86fe8a56497e..13ca2a4ff7c4 100644 --- a/simapp/utils_test.go +++ b/simapp/utils_test.go @@ -85,7 +85,7 @@ func TestGetSimulationLog(t *testing.T) { for _, tt := range tests { t.Run(tt.store, func(t *testing.T) { - require.NotPanics(t, func() { GetSimulationLog(tt.store, cdc, cdc, tt.kvPairs) }, tt.store) + require.NotPanics(t, func() { GetSimulationLog(tt.store, make(sdk.StoreDecoderRegistry) , cdc, tt.kvPairs) }, tt.store) }) } } diff --git a/types/module/module.go b/types/module/module.go index 288d6dd9488c..bd61a96fd4de 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -189,6 +189,7 @@ func (GenesisOnlyAppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []ab // operations for a group of modules type Manager struct { Modules map[string]AppModule + StoreDecoders sdk.StoreDecoderRegistry OrderInitGenesis []string OrderExportGenesis []string OrderBeginBlockers []string @@ -199,6 +200,7 @@ type Manager struct { func NewManager(modules ...AppModule) *Manager { moduleMap := make(map[string]AppModule) + decoders := make(sdk.StoreDecoderRegistry) var modulesStr []string for _, module := range modules { moduleMap[module.Name()] = module @@ -207,6 +209,7 @@ func NewManager(modules ...AppModule) *Manager { return &Manager{ Modules: moduleMap, + StoreDecoders: decoders, OrderInitGenesis: modulesStr, OrderExportGenesis: modulesStr, OrderBeginBlockers: modulesStr, @@ -242,9 +245,9 @@ func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) { } // RegisterStoreDecoders registers the each type decoder with the respective key -func (m *Manager) RegisterStoreDecoders(sd sdk.StoreDecoderRegistry) { +func (m *Manager) RegisterStoreDecoders() { for _, module := range m.Modules { - module.RegisterStoreDecoder(sd) + module.RegisterStoreDecoder(m.StoreDecoders) } } diff --git a/types/store.go b/types/store.go index 3146db310f2d..7bb4a41d6f79 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(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string +type StoreDecoderRegistry map[string]func(cdc *codec.Codec, kvA, kvB cmn.KVPair) 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/store.go b/x/auth/simulation/decoder/store.go index a539bfb1a787..dfbe757981df 100644 --- a/x/auth/simulation/decoder/store.go +++ b/x/auth/simulation/decoder/store.go @@ -13,17 +13,17 @@ import ( ) // DecodeStore unmarshals the KVPair's Value to the corresponding auth type -func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], types.AddressStoreKeyPrefix): var accA, accB exported.Account - cdcA.MustUnmarshalBinaryBare(kvA.Value, &accA) - cdcB.MustUnmarshalBinaryBare(kvB.Value, &accB) + cdc.MustUnmarshalBinaryBare(kvA.Value, &accA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &accB) return fmt.Sprintf("%v\n%v", accA, accB) case bytes.Equal(kvA.Key, types.GlobalAccountNumberKey): var globalAccNumberA, globalAccNumberB uint64 - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &globalAccNumberA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &globalAccNumberB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &globalAccNumberA) + cdc.MustUnmarshalBinaryLengthPrefixed(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/store_test.go b/x/auth/simulation/decoder/store_test.go index 3a92ec976dee..2c31f01187ed 100644 --- a/x/auth/simulation/decoder/store_test.go +++ b/x/auth/simulation/decoder/store_test.go @@ -54,9 +54,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, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } diff --git a/x/distribution/simulation/decoder/store.go b/x/distribution/simulation/decoder/store.go index 7207b7bb9c49..a3b5e5456a80 100644 --- a/x/distribution/simulation/decoder/store.go +++ b/x/distribution/simulation/decoder/store.go @@ -14,12 +14,12 @@ import ( ) // DecodeStore unmarshals the KVPair's Value to the corresponding distribution type -func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], keeper.FeePoolKey): var feePoolA, feePoolB types.FeePool - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &feePoolA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &feePoolB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &feePoolA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &feePoolB) return fmt.Sprintf("%v\n%v", feePoolA, feePoolB) case bytes.Equal(kvA.Key[:1], keeper.ProposerKey): @@ -27,8 +27,8 @@ func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { case bytes.Equal(kvA.Key[:1], keeper.ValidatorOutstandingRewardsPrefix): var rewardsA, rewardsB types.ValidatorOutstandingRewards - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) case bytes.Equal(kvA.Key[:1], keeper.DelegatorWithdrawAddrPrefix): @@ -36,32 +36,32 @@ func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { case bytes.Equal(kvA.Key[:1], keeper.DelegatorStartingInfoPrefix): var infoA, infoB types.DelegatorStartingInfo - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &infoA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &infoB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &infoA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &infoB) return fmt.Sprintf("%v\n%v", infoA, infoB) case bytes.Equal(kvA.Key[:1], keeper.ValidatorHistoricalRewardsPrefix): var rewardsA, rewardsB types.ValidatorHistoricalRewards - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) case bytes.Equal(kvA.Key[:1], keeper.ValidatorCurrentRewardsPrefix): var rewardsA, rewardsB types.ValidatorCurrentRewards - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) case bytes.Equal(kvA.Key[:1], keeper.ValidatorAccumulatedCommissionPrefix): var commissionA, commissionB types.ValidatorAccumulatedCommission - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &commissionA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &commissionB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &commissionA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &commissionB) return fmt.Sprintf("%v\n%v", commissionA, commissionB) case bytes.Equal(kvA.Key[:1], keeper.ValidatorSlashEventPrefix): var eventA, eventB types.ValidatorSlashEvent - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &eventA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &eventB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &eventA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &eventB) return fmt.Sprintf("%v\n%v", eventA, eventB) default: diff --git a/x/distribution/simulation/decoder/store_test.go b/x/distribution/simulation/decoder/store_test.go index cdae27cae1e7..a0ef63edddfa 100644 --- a/x/distribution/simulation/decoder/store_test.go +++ b/x/distribution/simulation/decoder/store_test.go @@ -77,9 +77,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, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } diff --git a/x/gov/simulation/decoder/store.go b/x/gov/simulation/decoder/store.go index 3c67fa33aed2..3aabca188ea1 100644 --- a/x/gov/simulation/decoder/store.go +++ b/x/gov/simulation/decoder/store.go @@ -13,12 +13,12 @@ import ( ) // DecodeStore unmarshals the KVPair's Value to the corresponding gov type -func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], types.ProposalsKeyPrefix): var proposalA, proposalB types.Proposal - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &proposalA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &proposalB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &proposalA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &proposalB) return fmt.Sprintf("%v\n%v", proposalA, proposalB) case bytes.Equal(kvA.Key[:1], types.ActiveProposalQueuePrefix), @@ -30,14 +30,14 @@ func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { case bytes.Equal(kvA.Key[:1], types.DepositsKeyPrefix): var depositA, depositB types.Deposit - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &depositA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &depositB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &depositA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &depositB) return fmt.Sprintf("%v\n%v", depositA, depositB) case bytes.Equal(kvA.Key[:1], types.VotesKeyPrefix): var voteA, voteB types.Vote - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &voteA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &voteB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &voteA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &voteB) return fmt.Sprintf("%v\n%v", voteA, voteB) default: diff --git a/x/mint/simulation/store.go b/x/mint/simulation/store.go index e38b57b66fae..5042e3a09eda 100644 --- a/x/mint/simulation/store.go +++ b/x/mint/simulation/store.go @@ -12,12 +12,12 @@ import ( ) // DecodeStore unmarshals the KVPair's Value to the corresponding mint type -func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key, types.MinterKey): var minterA, minterB types.Minter - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &minterA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &minterB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &minterA) + cdc.MustUnmarshalBinaryLengthPrefixed(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/store_test.go b/x/mint/simulation/store_test.go index 6b1fc84f4815..389aa257ee5e 100644 --- a/x/mint/simulation/store_test.go +++ b/x/mint/simulation/store_test.go @@ -41,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, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } diff --git a/x/slashing/simulation/decoder/store.go b/x/slashing/simulation/decoder/store.go index 701fca3caa05..7d79115863c2 100644 --- a/x/slashing/simulation/decoder/store.go +++ b/x/slashing/simulation/decoder/store.go @@ -14,24 +14,24 @@ import ( ) // DecodeStore unmarshals the KVPair's Value to the corresponding slashing type -func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], types.ValidatorSigningInfoKey): var infoA, infoB types.ValidatorSigningInfo - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &infoA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &infoB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &infoA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &infoB) return fmt.Sprintf("%v\n%v", infoA, infoB) case bytes.Equal(kvA.Key[:1], types.ValidatorMissedBlockBitArrayKey): var missedA, missedB bool - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &missedA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &missedB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &missedA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &missedB) return fmt.Sprintf("missedA: %v\nmissedB: %v", missedA, missedB) case bytes.Equal(kvA.Key[:1], types.AddrPubkeyRelationKey): var pubKeyA, pubKeyB crypto.PubKey - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &pubKeyA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &pubKeyB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &pubKeyA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &pubKeyB) bechPKA := sdk.MustBech32ifyAccPub(pubKeyA) bechPKB := sdk.MustBech32ifyAccPub(pubKeyB) return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", bechPKA, bechPKB) diff --git a/x/slashing/simulation/decoder/store_test.go b/x/slashing/simulation/decoder/store_test.go index c6e38c61a433..06039eb5bae1 100644 --- a/x/slashing/simulation/decoder/store_test.go +++ b/x/slashing/simulation/decoder/store_test.go @@ -58,9 +58,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, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } diff --git a/x/staking/simulation/decoder/store.go b/x/staking/simulation/decoder/store.go index 8b4fcf24a457..8215265659c5 100644 --- a/x/staking/simulation/decoder/store.go +++ b/x/staking/simulation/decoder/store.go @@ -13,18 +13,18 @@ import ( ) // DecodeStore unmarshals the KVPair's Value to the corresponding staking type -func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], types.LastTotalPowerKey): var powerA, powerB sdk.Int - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &powerA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &powerB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &powerA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &powerB) return fmt.Sprintf("%v\n%v", powerA, powerB) case bytes.Equal(kvA.Key[:1], types.ValidatorsKey): var validatorA, validatorB types.Validator - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &validatorA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &validatorB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &validatorA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &validatorB) return fmt.Sprintf("%v\n%v", validatorA, validatorB) case bytes.Equal(kvA.Key[:1], types.LastValidatorPowerKey), @@ -34,22 +34,22 @@ func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { case bytes.Equal(kvA.Key[:1], types.DelegationKey): var delegationA, delegationB types.Delegation - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &delegationA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &delegationB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &delegationA) + cdc.MustUnmarshalBinaryLengthPrefixed(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 - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &ubdA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &ubdB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &ubdA) + cdc.MustUnmarshalBinaryLengthPrefixed(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 - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &redA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &redB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &redA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &redB) return fmt.Sprintf("%v\n%v", redA, redB) default: diff --git a/x/staking/simulation/decoder/store_test.go b/x/staking/simulation/decoder/store_test.go index 8a5533342bff..a5ff718bb028 100644 --- a/x/staking/simulation/decoder/store_test.go +++ b/x/staking/simulation/decoder/store_test.go @@ -67,9 +67,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, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } diff --git a/x/supply/simulation/store.go b/x/supply/simulation/store.go index 7a322be368f9..d49abeb60961 100644 --- a/x/supply/simulation/store.go +++ b/x/supply/simulation/store.go @@ -13,12 +13,12 @@ import ( ) // DecodeStore unmarshals the KVPair's Value to the corresponding supply type -func DecodeStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], keeper.SupplyKey): var supplyA, supplyB types.Supply - cdcA.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &supplyA) - cdcB.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &supplyB) + cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &supplyA) + cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &supplyB) return fmt.Sprintf("%v\n%v", supplyB, supplyB) default: panic(fmt.Sprintf("invalid supply key %X", kvA.Key)) diff --git a/x/supply/simulation/store_test.go b/x/supply/simulation/store_test.go index 6b22530766de..28ae93e9ce8c 100644 --- a/x/supply/simulation/store_test.go +++ b/x/supply/simulation/store_test.go @@ -45,9 +45,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, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, DecodeStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } From 45a0e3caec6b0e89309d1ab2143ba5b77eb5a877 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Wed, 7 Aug 2019 16:44:33 +0200 Subject: [PATCH 06/18] fix --- simapp/sim_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simapp/sim_test.go b/simapp/sim_test.go index 59f01decedd9..a1b0738fd05d 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -601,7 +601,7 @@ func TestAppImportExport(t *testing.T) { storeB := ctxB.KVStore(storeKeyB) failedKVs := sdk.DiffKVStores(storeA, storeB, prefixes) fmt.Printf("Compared %d key/value pairs between %s and %s\n", len(failedKVs)/2, storeKeyA, storeKeyB) - require.Len(t, failedKVs, 0, GetSimulationLog(storeKeyA.Name(), app.mm.StoreDecoder, app.cdc, failedKVs)) + require.Len(t, failedKVs, 0, GetSimulationLog(storeKeyA.Name(), app.mm.StoreDecoders, app.cdc, failedKVs)) } } From da6de9f5616ce84e75a9638be03601f519634ddb Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Thu, 8 Aug 2019 13:55:19 +0200 Subject: [PATCH 07/18] address @colin-axner comments --- client/cmd_test.go | 7 ++--- simapp/sim_test.go | 2 +- simapp/utils.go | 2 +- simapp/utils_test.go | 2 +- types/module/module.go | 4 +-- x/auth/module.go | 2 +- x/auth/simulation/decoder/store.go | 2 +- x/auth/simulation/decoder/store_test.go | 2 +- x/crisis/module.go | 2 +- x/distribution/module.go | 2 +- x/distribution/simulation/decoder/store.go | 16 +++++------ .../simulation/decoder/store_test.go | 28 +++++++++---------- x/gov/module.go | 2 +- x/mint/module.go | 4 +-- x/mint/simulation/{ => decoder}/store.go | 2 +- x/mint/simulation/{ => decoder}/store_test.go | 2 +- x/staking/module.go | 2 +- x/supply/module.go | 4 +-- x/supply/simulation/{ => decoder}/store.go | 2 +- .../simulation/{ => decoder}/store_test.go | 2 +- 20 files changed, 45 insertions(+), 46 deletions(-) rename x/mint/simulation/{ => decoder}/store.go (97%) rename x/mint/simulation/{ => decoder}/store_test.go (98%) rename x/supply/simulation/{ => decoder}/store.go (97%) rename x/supply/simulation/{ => decoder}/store_test.go (98%) diff --git a/client/cmd_test.go b/client/cmd_test.go index ea71399a60bc..7dfb1cc817e1 100644 --- a/client/cmd_test.go +++ b/client/cmd_test.go @@ -1,4 +1,3 @@ -// nolint: misspell package client_test import ( @@ -38,10 +37,10 @@ func TestValidateCmd(t *testing.T) { args []string wantErr bool }{ - {"misspelled command", []string{"comission"}, true}, + {"misspelled command", []string{"comission"}, true}, // nolint: misspell {"no command provided", []string{}, false}, - {"help flag", []string{"comission", "--help"}, false}, - {"shorthand help flag", []string{"comission", "-h"}, false}, + {"help flag", []string{"commission", "--help"}, false}, // nolint: misspell + {"shorthand help flag", []string{"commission", "-h"}, false}, // nolint: misspell } for _, tt := range tests { diff --git a/simapp/sim_test.go b/simapp/sim_test.go index a1b0738fd05d..e3d328ecbc17 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -599,7 +599,7 @@ func TestAppImportExport(t *testing.T) { prefixes := storeKeysPrefix.Prefixes storeA := ctxA.KVStore(storeKeyA) storeB := ctxB.KVStore(storeKeyB) - failedKVs := sdk.DiffKVStores(storeA, storeB, prefixes) + failedKVs := sdk.DiffKVStores(storeA, storeB, prefixes) fmt.Printf("Compared %d key/value pairs between %s and %s\n", len(failedKVs)/2, storeKeyA, storeKeyB) require.Len(t, failedKVs, 0, GetSimulationLog(storeKeyA.Name(), app.mm.StoreDecoders, app.cdc, failedKVs)) } diff --git a/simapp/utils.go b/simapp/utils.go index a09101d03d0f..6d8a0b1ce67f 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -511,7 +511,7 @@ func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, cdc *codec // skip if the value doesn't have any bytes continue } - + decoder, ok := sdr[storeName] if ok { log += decoder(cdc, kvA, kvB) diff --git a/simapp/utils_test.go b/simapp/utils_test.go index 13ca2a4ff7c4..7a63d560250d 100644 --- a/simapp/utils_test.go +++ b/simapp/utils_test.go @@ -85,7 +85,7 @@ func TestGetSimulationLog(t *testing.T) { for _, tt := range tests { t.Run(tt.store, func(t *testing.T) { - require.NotPanics(t, func() { GetSimulationLog(tt.store, make(sdk.StoreDecoderRegistry) , cdc, tt.kvPairs) }, tt.store) + require.NotPanics(t, func() { GetSimulationLog(tt.store, make(sdk.StoreDecoderRegistry), cdc, tt.kvPairs) }, tt.store) }) } } diff --git a/types/module/module.go b/types/module/module.go index bd61a96fd4de..5e9d57758daf 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -189,7 +189,7 @@ func (GenesisOnlyAppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []ab // operations for a group of modules type Manager struct { Modules map[string]AppModule - StoreDecoders sdk.StoreDecoderRegistry + StoreDecoders sdk.StoreDecoderRegistry OrderInitGenesis []string OrderExportGenesis []string OrderBeginBlockers []string @@ -209,7 +209,7 @@ func NewManager(modules ...AppModule) *Manager { return &Manager{ Modules: moduleMap, - StoreDecoders: decoders, + StoreDecoders: decoders, OrderInitGenesis: modulesStr, OrderExportGenesis: modulesStr, OrderBeginBlockers: modulesStr, diff --git a/x/auth/module.go b/x/auth/module.go index 274a72c02633..8437e7837b49 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -13,8 +13,8 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/auth/client/rest" - "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/auth/simulation/decoder" + "github.com/cosmos/cosmos-sdk/x/auth/types" ) var ( diff --git a/x/auth/simulation/decoder/store.go b/x/auth/simulation/decoder/store.go index dfbe757981df..9258795183f0 100644 --- a/x/auth/simulation/decoder/store.go +++ b/x/auth/simulation/decoder/store.go @@ -8,8 +8,8 @@ import ( "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/auth/types" "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 diff --git a/x/auth/simulation/decoder/store_test.go b/x/auth/simulation/decoder/store_test.go index 2c31f01187ed..c518ba7a6587 100644 --- a/x/auth/simulation/decoder/store_test.go +++ b/x/auth/simulation/decoder/store_test.go @@ -45,7 +45,7 @@ func TestDecodeStore(t *testing.T) { name string expectedLog string }{ - {"Minter", fmt.Sprintf("%v\n%v", acc, acc)}, + {"Account", fmt.Sprintf("%v\n%v", acc, acc)}, {"GlobalAccNumber", fmt.Sprintf("GlobalAccNumberA: %d\nGlobalAccNumberB: %d", globalAccNumber, globalAccNumber)}, {"other", ""}, } diff --git a/x/crisis/module.go b/x/crisis/module.go index 87474b07585d..5dac8fe2ee13 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -85,7 +85,7 @@ func (AppModule) Name() string { // register invariants func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} -// RegisterStoreDecoder doesn't register any KVPair +// RegisterStoreDecoder doesn't register any KVPair func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} // module querier route name diff --git a/x/distribution/module.go b/x/distribution/module.go index 58192b1b7df7..b526954450e7 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -14,8 +14,8 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/distribution/client/cli" "github.com/cosmos/cosmos-sdk/x/distribution/client/rest" - "github.com/cosmos/cosmos-sdk/x/distribution/types" "github.com/cosmos/cosmos-sdk/x/distribution/simulation/decoder" + "github.com/cosmos/cosmos-sdk/x/distribution/types" ) var ( diff --git a/x/distribution/simulation/decoder/store.go b/x/distribution/simulation/decoder/store.go index a3b5e5456a80..6e4bae40078e 100644 --- a/x/distribution/simulation/decoder/store.go +++ b/x/distribution/simulation/decoder/store.go @@ -22,43 +22,43 @@ func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string { cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &feePoolB) return fmt.Sprintf("%v\n%v", feePoolA, feePoolB) - case bytes.Equal(kvA.Key[:1], keeper.ProposerKey): + case bytes.Equal(kvA.Key[:1], keeper.ProposerKey): return fmt.Sprintf("%v\n%v", sdk.ConsAddress(kvA.Value), sdk.ConsAddress(kvB.Value)) - case bytes.Equal(kvA.Key[:1], keeper.ValidatorOutstandingRewardsPrefix): + case bytes.Equal(kvA.Key[:1], keeper.ValidatorOutstandingRewardsPrefix): var rewardsA, rewardsB types.ValidatorOutstandingRewards cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - case bytes.Equal(kvA.Key[:1], keeper.DelegatorWithdrawAddrPrefix): + case bytes.Equal(kvA.Key[:1], keeper.DelegatorWithdrawAddrPrefix): return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value)) - case bytes.Equal(kvA.Key[:1], keeper.DelegatorStartingInfoPrefix): + case bytes.Equal(kvA.Key[:1], keeper.DelegatorStartingInfoPrefix): var infoA, infoB types.DelegatorStartingInfo cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &infoA) cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &infoB) return fmt.Sprintf("%v\n%v", infoA, infoB) - case bytes.Equal(kvA.Key[:1], keeper.ValidatorHistoricalRewardsPrefix): + case bytes.Equal(kvA.Key[:1], keeper.ValidatorHistoricalRewardsPrefix): var rewardsA, rewardsB types.ValidatorHistoricalRewards cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - case bytes.Equal(kvA.Key[:1], keeper.ValidatorCurrentRewardsPrefix): + case bytes.Equal(kvA.Key[:1], keeper.ValidatorCurrentRewardsPrefix): var rewardsA, rewardsB types.ValidatorCurrentRewards cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &rewardsA) cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &rewardsB) return fmt.Sprintf("%v\n%v", rewardsA, rewardsB) - case bytes.Equal(kvA.Key[:1], keeper.ValidatorAccumulatedCommissionPrefix): + case bytes.Equal(kvA.Key[:1], keeper.ValidatorAccumulatedCommissionPrefix): var commissionA, commissionB types.ValidatorAccumulatedCommission cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &commissionA) cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &commissionB) return fmt.Sprintf("%v\n%v", commissionA, commissionB) - case bytes.Equal(kvA.Key[:1], keeper.ValidatorSlashEventPrefix): + case bytes.Equal(kvA.Key[:1], keeper.ValidatorSlashEventPrefix): var eventA, eventB types.ValidatorSlashEvent cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &eventA) cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &eventB) diff --git a/x/distribution/simulation/decoder/store_test.go b/x/distribution/simulation/decoder/store_test.go index a0ef63edddfa..f7d464f89682 100644 --- a/x/distribution/simulation/decoder/store_test.go +++ b/x/distribution/simulation/decoder/store_test.go @@ -38,23 +38,23 @@ func TestDecodeDistributionStore(t *testing.T) { decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.OneDec())} feePool := types.InitialFeePool() feePool.CommunityPool = decCoins - info := types.NewDelegatorStartingInfo(2, sdk.OneDec(), 200) - outstanding := types.ValidatorOutstandingRewards{decCoins[0]} - commission := types.ValidatorAccumulatedCommission{decCoins[0]} - historicalRewards := types.NewValidatorHistoricalRewards(decCoins, 100) - currentRewards := types.NewValidatorCurrentRewards(decCoins, 5) - slashEvent := types.NewValidatorSlashEvent(10, sdk.OneDec()) + info := types.NewDelegatorStartingInfo(2, sdk.OneDec(), 200) + outstanding := types.ValidatorOutstandingRewards{decCoins[0]} + commission := types.ValidatorAccumulatedCommission{decCoins[0]} + historicalRewards := types.NewValidatorHistoricalRewards(decCoins, 100) + currentRewards := types.NewValidatorCurrentRewards(decCoins, 5) + slashEvent := types.NewValidatorSlashEvent(10, sdk.OneDec()) kvPairs := cmn.KVPairs{ cmn.KVPair{Key: keeper.FeePoolKey, Value: cdc.MustMarshalBinaryLengthPrefixed(feePool)}, - cmn.KVPair{Key: keeper.ProposerKey, Value: consAddr1.Bytes()}, - cmn.KVPair{Key: keeper.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(outstanding)}, - cmn.KVPair{Key: keeper.GetDelegatorWithdrawAddrKey(delAddr1), Value: delAddr1.Bytes()}, - cmn.KVPair{Key: keeper.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(info)}, - cmn.KVPair{Key: keeper.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshalBinaryLengthPrefixed(historicalRewards)}, - cmn.KVPair{Key: keeper.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(currentRewards)}, - cmn.KVPair{Key: keeper.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(commission)}, - cmn.KVPair{Key: keeper.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshalBinaryLengthPrefixed(slashEvent)}, + cmn.KVPair{Key: keeper.ProposerKey, Value: consAddr1.Bytes()}, + cmn.KVPair{Key: keeper.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(outstanding)}, + cmn.KVPair{Key: keeper.GetDelegatorWithdrawAddrKey(delAddr1), Value: delAddr1.Bytes()}, + cmn.KVPair{Key: keeper.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(info)}, + cmn.KVPair{Key: keeper.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshalBinaryLengthPrefixed(historicalRewards)}, + cmn.KVPair{Key: keeper.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(currentRewards)}, + cmn.KVPair{Key: keeper.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(commission)}, + cmn.KVPair{Key: keeper.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshalBinaryLengthPrefixed(slashEvent)}, cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}}, } diff --git a/x/gov/module.go b/x/gov/module.go index 6ea3611fe210..5bc873d86def 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -15,8 +15,8 @@ import ( "github.com/cosmos/cosmos-sdk/x/gov/client" "github.com/cosmos/cosmos-sdk/x/gov/client/cli" "github.com/cosmos/cosmos-sdk/x/gov/client/rest" - "github.com/cosmos/cosmos-sdk/x/gov/types" "github.com/cosmos/cosmos-sdk/x/gov/simulation/decoder" + "github.com/cosmos/cosmos-sdk/x/gov/types" ) var ( diff --git a/x/mint/module.go b/x/mint/module.go index a3d0549f449d..285c0356541b 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -14,7 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/mint/client/cli" "github.com/cosmos/cosmos-sdk/x/mint/client/rest" - "github.com/cosmos/cosmos-sdk/x/mint/simulation" + "github.com/cosmos/cosmos-sdk/x/mint/simulation/decoder" ) var ( @@ -89,7 +89,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterStoreDecoder registers the function to decode the types stored in the // KVStore func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = simulation.DecodeStore + sdr[StoreKey] = decoder.DecodeStore } // module message route name diff --git a/x/mint/simulation/store.go b/x/mint/simulation/decoder/store.go similarity index 97% rename from x/mint/simulation/store.go rename to x/mint/simulation/decoder/store.go index 5042e3a09eda..49d0ff3382b1 100644 --- a/x/mint/simulation/store.go +++ b/x/mint/simulation/decoder/store.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "bytes" diff --git a/x/mint/simulation/store_test.go b/x/mint/simulation/decoder/store_test.go similarity index 98% rename from x/mint/simulation/store_test.go rename to x/mint/simulation/decoder/store_test.go index 389aa257ee5e..8d3f0c413b8c 100644 --- a/x/mint/simulation/store_test.go +++ b/x/mint/simulation/decoder/store_test.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "fmt" diff --git a/x/staking/module.go b/x/staking/module.go index 59dc5173a1ac..83b0cea5cded 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -18,8 +18,8 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/staking/client/cli" "github.com/cosmos/cosmos-sdk/x/staking/client/rest" - "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/cosmos/cosmos-sdk/x/staking/simulation/decoder" + "github.com/cosmos/cosmos-sdk/x/staking/types" ) var ( diff --git a/x/supply/module.go b/x/supply/module.go index 91a6fc11dfd9..e1f21bd0ba5b 100644 --- a/x/supply/module.go +++ b/x/supply/module.go @@ -15,7 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/supply/client/cli" "github.com/cosmos/cosmos-sdk/x/supply/client/rest" "github.com/cosmos/cosmos-sdk/x/supply/internal/types" - "github.com/cosmos/cosmos-sdk/x/supply/simulation" + "github.com/cosmos/cosmos-sdk/x/supply/simulation/decoder" ) var ( @@ -93,7 +93,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { // RegisterStoreDecoder registers the function to decode the types stored in the // KVStore func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = simulation.DecodeStore + sdr[StoreKey] = decoder.DecodeStore } // module message route name diff --git a/x/supply/simulation/store.go b/x/supply/simulation/decoder/store.go similarity index 97% rename from x/supply/simulation/store.go rename to x/supply/simulation/decoder/store.go index d49abeb60961..da9ce0761672 100644 --- a/x/supply/simulation/store.go +++ b/x/supply/simulation/decoder/store.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "bytes" diff --git a/x/supply/simulation/store_test.go b/x/supply/simulation/decoder/store_test.go similarity index 98% rename from x/supply/simulation/store_test.go rename to x/supply/simulation/decoder/store_test.go index 28ae93e9ce8c..38f30ea0971b 100644 --- a/x/supply/simulation/store_test.go +++ b/x/supply/simulation/decoder/store_test.go @@ -1,4 +1,4 @@ -package simulation +package decoder import ( "fmt" From bd71a172c8600b0a0fde49e5adee5dc7c3b06999 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Thu, 8 Aug 2019 21:31:14 +0200 Subject: [PATCH 08/18] Update cmd_test.go --- client/cmd_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/cmd_test.go b/client/cmd_test.go index 7dfb1cc817e1..675cc296aeb4 100644 --- a/client/cmd_test.go +++ b/client/cmd_test.go @@ -39,8 +39,8 @@ func TestValidateCmd(t *testing.T) { }{ {"misspelled command", []string{"comission"}, true}, // nolint: misspell {"no command provided", []string{}, false}, - {"help flag", []string{"commission", "--help"}, false}, // nolint: misspell - {"shorthand help flag", []string{"commission", "-h"}, false}, // nolint: misspell + {"help flag", []string{"comission", "--help"}, false}, // nolint: misspell + {"shorthand help flag", []string{"comission", "-h"}, false}, // nolint: misspell } for _, tt := range tests { From fc1e72a310fd24c1f828414e7ed3c27c5610bd71 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 9 Aug 2019 15:20:32 +0200 Subject: [PATCH 09/18] simulation manager --- simapp/app.go | 7 ++- types/module/module.go | 102 +++++++++++++++++++++++++-------------- x/auth/module.go | 73 ++++++++++++++++------------ x/bank/module.go | 70 ++++++++++++++++----------- x/crisis/module.go | 24 ++++++--- x/distribution/module.go | 66 ++++++++++++++++--------- x/genaccounts/module.go | 51 +++++++++++++------- x/genutil/module.go | 40 ++++++++++----- x/mint/module.go | 67 ++++++++++++++++--------- x/params/module.go | 21 ++++---- x/slashing/module.go | 59 ++++++++++++++-------- x/staking/module.go | 78 ++++++++++++++++++------------ x/supply/module.go | 74 ++++++++++++++++------------ 13 files changed, 459 insertions(+), 273 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 346acccba51d..2b787cb0a32f 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -101,6 +101,9 @@ type SimApp struct { // the module manager mm *module.Manager + + // simulation manager + sm *module.SimulationManager } // NewSimApp returns a reference to an initialized SimApp. @@ -199,7 +202,9 @@ func NewSimApp( app.mm.RegisterInvariants(&app.crisisKeeper) app.mm.RegisterRoutes(app.Router(), app.QueryRouter()) - app.mm.RegisterStoreDecoders() + + app.sm = module.NewSimulationManager(app.mm.Modules) + app.sm.RegisterStoreDecoders() // initialize stores app.MountKVStores(keys) diff --git a/types/module/module.go b/types/module/module.go index 5e9d57758daf..8cbd8c47cb6d 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -3,6 +3,7 @@ Package module contains application module patterns and associated "manager" fun The module pattern has been broken down by: - independent module functionality (AppModuleBasic) - inter-dependent module genesis functionality (AppModuleGenesis) + - inter-dependent module simulation functionality (AppModuleSimulation) - inter-dependent module full functionality (AppModule) inter-dependent module functionality is module functionality which somehow @@ -41,6 +42,7 @@ import ( ) //__________________________________________________________________________________________ + // AppModuleBasic is the standard form for basic non-dependant elements of an application module. type AppModuleBasic interface { Name() string @@ -56,9 +58,10 @@ type AppModuleBasic interface { GetQueryCmd(*codec.Codec) *cobra.Command } -// collections of AppModuleBasic +// BasicManager is a collection of AppModuleBasic type BasicManager map[string]AppModuleBasic +// NewBasicManager creates a new BasicManager object func NewBasicManager(modules ...AppModuleBasic) BasicManager { moduleMap := make(map[string]AppModuleBasic) for _, module := range modules { @@ -67,14 +70,14 @@ func NewBasicManager(modules ...AppModuleBasic) BasicManager { return moduleMap } -// RegisterCodecs registers all module codecs +// RegisterCodec registers all module codecs func (bm BasicManager) RegisterCodec(cdc *codec.Codec) { for _, b := range bm { b.RegisterCodec(cdc) } } -// Provided default genesis information for all modules +// DefaultGenesis provided default genesis information for all modules func (bm BasicManager) DefaultGenesis() map[string]json.RawMessage { genesis := make(map[string]json.RawMessage) for _, b := range bm { @@ -83,7 +86,7 @@ func (bm BasicManager) DefaultGenesis() map[string]json.RawMessage { return genesis } -// Provided default genesis information for all modules +// ValidateGenesis provided default genesis information for all modules func (bm BasicManager) ValidateGenesis(genesis map[string]json.RawMessage) error { for _, b := range bm { if err := b.ValidateGenesis(genesis[b.Name()]); err != nil { @@ -93,14 +96,14 @@ func (bm BasicManager) ValidateGenesis(genesis map[string]json.RawMessage) error return nil } -// RegisterRestRoutes registers all module rest routes +// RegisterRESTRoutes registers all module rest routes func (bm BasicManager) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { for _, b := range bm { b.RegisterRESTRoutes(ctx, rtr) } } -// add all tx commands to the rootTxCmd +// AddTxCommands adds all tx commands to the rootTxCmd func (bm BasicManager) AddTxCommands(rootTxCmd *cobra.Command, cdc *codec.Codec) { for _, b := range bm { if cmd := b.GetTxCmd(cdc); cmd != nil { @@ -109,7 +112,7 @@ func (bm BasicManager) AddTxCommands(rootTxCmd *cobra.Command, cdc *codec.Codec) } } -// add all query commands to the rootQueryCmd +// AddQueryCommands adds all query commands to the rootQueryCmd func (bm BasicManager) AddQueryCommands(rootQueryCmd *cobra.Command, cdc *codec.Codec) { for _, b := range bm { if cmd := b.GetQueryCmd(cdc); cmd != nil { @@ -119,6 +122,7 @@ func (bm BasicManager) AddQueryCommands(rootQueryCmd *cobra.Command, cdc *codec. } //_________________________________________________________ + // AppModuleGenesis is the standard form for an application module genesis functions type AppModuleGenesis interface { AppModuleBasic @@ -126,13 +130,20 @@ type AppModuleGenesis interface { ExportGenesis(sdk.Context) json.RawMessage } +// AppModuleSimulation defines the standard functions that every module should expose +// for the SDK blockchain simulator +type AppModuleSimulation interface { + // register a func to decode the each module's defined types from their corresponding store key + RegisterStoreDecoder(sdk.StoreDecoderRegistry) +} + // AppModule is the standard form for an application module type AppModule interface { AppModuleGenesis + AppModuleSimulation // registers RegisterInvariants(sdk.InvariantRegistry) - RegisterStoreDecoder(sdk.StoreDecoderRegistry) // routes Route() string @@ -146,7 +157,8 @@ type AppModule interface { } //___________________________ -// app module + +// GenesisOnlyAppModule is an AppModule that only has import/export functionality type GenesisOnlyAppModule struct { AppModuleGenesis } @@ -158,49 +170,48 @@ func NewGenesisOnlyAppModule(amg AppModuleGenesis) AppModule { } } -// register invariants +// RegisterInvariants register no invariants func (GenesisOnlyAppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterStoreDecoder empty store decoder registry func (GenesisOnlyAppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} -// module message route ngame +// Route empty module message route func (GenesisOnlyAppModule) Route() string { return "" } -// module handler +// NewHandler empty module handler func (GenesisOnlyAppModule) NewHandler() sdk.Handler { return nil } -// module querier route ngame +// QuerierRoute empty module querier route func (GenesisOnlyAppModule) QuerierRoute() string { return "" } -// module querier +// NewQuerierHandler empty module querier func (gam GenesisOnlyAppModule) NewQuerierHandler() sdk.Querier { return nil } -// module begin-block +// BeginBlock empty module begin-block func (gam GenesisOnlyAppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {} -// module end-block +// EndBlock empty module end-block func (GenesisOnlyAppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } //____________________________________________________________________________ -// module manager provides the high level utility for managing and executing + +// Manager defines a module manager that provides the high level utility for managing and executing // operations for a group of modules type Manager struct { Modules map[string]AppModule - StoreDecoders sdk.StoreDecoderRegistry OrderInitGenesis []string OrderExportGenesis []string OrderBeginBlockers []string OrderEndBlockers []string } -// NewModuleManager creates a new Manager object +// NewManager creates a new Manager object func NewManager(modules ...AppModule) *Manager { moduleMap := make(map[string]AppModule) - decoders := make(sdk.StoreDecoderRegistry) var modulesStr []string for _, module := range modules { moduleMap[module.Name()] = module @@ -209,7 +220,6 @@ func NewManager(modules ...AppModule) *Manager { return &Manager{ Modules: moduleMap, - StoreDecoders: decoders, OrderInitGenesis: modulesStr, OrderExportGenesis: modulesStr, OrderBeginBlockers: modulesStr, @@ -217,41 +227,34 @@ func NewManager(modules ...AppModule) *Manager { } } -// set the order of init genesis calls +// SetOrderInitGenesis sets the order of init genesis calls func (m *Manager) SetOrderInitGenesis(moduleNames ...string) { m.OrderInitGenesis = moduleNames } -// set the order of export genesis calls +// SetOrderExportGenesis sets the order of export genesis calls func (m *Manager) SetOrderExportGenesis(moduleNames ...string) { m.OrderExportGenesis = moduleNames } -// set the order of set begin-blocker calls +// SetOrderBeginBlockers sets the order of set begin-blocker calls func (m *Manager) SetOrderBeginBlockers(moduleNames ...string) { m.OrderBeginBlockers = moduleNames } -// set the order of set end-blocker calls +// SetOrderEndBlockers sets the order of set end-blocker calls func (m *Manager) SetOrderEndBlockers(moduleNames ...string) { m.OrderEndBlockers = moduleNames } -// register all module routes and module querier routes +// RegisterInvariants registers all module routes and module querier routes func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) { for _, module := range m.Modules { module.RegisterInvariants(ir) } } -// RegisterStoreDecoders registers the each type decoder with the respective key -func (m *Manager) RegisterStoreDecoders() { - for _, module := range m.Modules { - module.RegisterStoreDecoder(m.StoreDecoders) - } -} - -// register all module routes and module querier routes +// RegisterRoutes registers all module routes and module querier routes func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) { for _, module := range m.Modules { if module.Route() != "" { @@ -263,7 +266,7 @@ func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) } } -// perform init genesis functionality for modules +// InitGenesis performs init genesis functionality for modules func (m *Manager) InitGenesis(ctx sdk.Context, genesisData map[string]json.RawMessage) abci.ResponseInitChain { var validatorUpdates []abci.ValidatorUpdate for _, moduleName := range m.OrderInitGenesis { @@ -286,7 +289,7 @@ func (m *Manager) InitGenesis(ctx sdk.Context, genesisData map[string]json.RawMe } } -// perform export genesis functionality for modules +// ExportGenesis performs export genesis functionality for modules func (m *Manager) ExportGenesis(ctx sdk.Context) map[string]json.RawMessage { genesisData := make(map[string]json.RawMessage) for _, moduleName := range m.OrderExportGenesis { @@ -337,4 +340,29 @@ func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) abci.Respo } } -// DONTCOVER +//____________________________________________________________________________ + +// SimulationManager defines a simulation manager that provides the high level utility +// for managing and executing simulation functionalities for a group of modules +type SimulationManager struct { + Modules map[string]AppModule + StoreDecoders sdk.StoreDecoderRegistry +} + +// NewSimulationManager creates a new SimulationManager object +func NewSimulationManager(moduleMap map[string]AppModule) *SimulationManager { + + decoders := make(sdk.StoreDecoderRegistry) + + return &SimulationManager{ + Modules: moduleMap, + StoreDecoders: decoders, + } +} + +// RegisterStoreDecoders registers each of the modules' store decoders into a map +func (sm *SimulationManager) RegisterStoreDecoders() { + for _, module := range sm.Modules { + module.RegisterStoreDecoder(sm.StoreDecoders) + } +} diff --git a/x/auth/module.go b/x/auth/module.go index 8437e7837b49..2c2c79a4c692 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -18,29 +18,31 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) -// app module basics object +// AppModuleBasic defines the basic application module used by the auth module. type AppModuleBasic struct{} -// module name +// Name returns the auth module's name. func (AppModuleBasic) Name() string { return types.ModuleName } -// register module codec +// RegisterCodec registers the auth module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { types.RegisterCodec(cdc) } -// default genesis state +// DefaultGenesis returns default genesis state as raw bytes for the auth +// module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return types.ModuleCdc.MustMarshalJSON(types.DefaultGenesisState()) } -// module validate genesis +// ValidateGenesis performs genesis state validation for the auth module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data types.GenesisState err := types.ModuleCdc.UnmarshalJSON(bz, &data) @@ -50,67 +52,76 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return types.ValidateGenesis(data) } -// register rest routes +// RegisterRESTRoutes registers the REST routes for the auth module. func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { rest.RegisterRoutes(ctx, rtr, types.StoreKey) } -// get the root tx command of this module +// GetTxCmd returns the root tx command for the auth module. func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { return cli.GetTxCmd(cdc) } -// get the root query command of this module +// GetQueryCmd returns no root query command for the auth module. func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(cdc) } -//___________________________ -// app module object +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the auth module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers a decoder for auth module's types +func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the auth module. type AppModule struct { AppModuleBasic + AppModuleSimulation + accountKeeper AccountKeeper } // NewAppModule creates a new AppModule object func NewAppModule(accountKeeper AccountKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, - accountKeeper: accountKeeper, + AppModuleBasic: AppModuleBasic{}, + AppModuleSimulation: AppModuleSimulation{}, + accountKeeper: accountKeeper, } } -// module name +// Name returns the auth module's name. func (AppModule) Name() string { return types.ModuleName } -// register invariants +// RegisterInvariants performs a no-op. func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} -// RegisterStoreDecoder registers the function to decode the types stored in the -// KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore -} - -// module message route name +// Route returns the message routing key for the auth module. func (AppModule) Route() string { return "" } -// module handler +// NewHandler returns an sdk.Handler for the auth module. func (AppModule) NewHandler() sdk.Handler { return nil } -// module querier route name +// QuerierRoute returns the auth module's querier route name. func (AppModule) QuerierRoute() string { return types.QuerierRoute } -// module querier +// NewQuerierHandler returns the auth module sdk.Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.accountKeeper) } -// module init-genesis +// InitGenesis performs genesis initialization for the auth module. It returns +// no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState types.ModuleCdc.MustUnmarshalJSON(data, &genesisState) @@ -118,16 +129,18 @@ func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.Va return []abci.ValidatorUpdate{} } -// module export genesis +// ExportGenesis returns the exported genesis state as raw bytes for the auth +// module. func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { gs := ExportGenesis(ctx, am.accountKeeper) return types.ModuleCdc.MustMarshalJSON(gs) } -// module begin-block +// BeginBlock returns the begin blocker for the auth module. func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} -// module end-block +// EndBlock returns the end blocker for the auth module. It returns no validator +// updates. func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } diff --git a/x/bank/module.go b/x/bank/module.go index f5c49eecac6f..3cc8b543ef09 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -19,25 +19,27 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) -// app module basics object +// AppModuleBasic defines the basic application module used by the auth module. type AppModuleBasic struct{} -// module name +// Name returns the auth module's name. func (AppModuleBasic) Name() string { return ModuleName } -// register module codec +// RegisterCodec registers the auth module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { RegisterCodec(cdc) } -// default genesis state +// DefaultGenesis returns default genesis state as raw bytes for the auth +// module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) } -// module validate genesis +// ValidateGenesis performs genesis state validation for the auth module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState err := ModuleCdc.UnmarshalJSON(bz, &data) @@ -47,23 +49,34 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } -// register rest routes +// RegisterRESTRoutes registers the REST routes for the auth module. func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { rest.RegisterRoutes(ctx, rtr) } -// get the root tx command of this module +// GetTxCmd returns the root tx command for the auth module. func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { return cli.GetTxCmd(cdc) } -// get the root query command of this module +// GetQueryCmd returns no root query command for the auth module. func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil } -//___________________________ -// app module +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the auth module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers no decoder for auth module's types +func (AppModuleSimulation) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} + +//____________________________________________________________________________ + +// AppModule implements an application module for the auth module. type AppModule struct { AppModuleBasic + AppModuleSimulation + keeper Keeper accountKeeper types.AccountKeeper } @@ -71,38 +84,37 @@ type AppModule struct { // NewAppModule creates a new AppModule object func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, - keeper: keeper, - accountKeeper: accountKeeper, + AppModuleBasic: AppModuleBasic{}, + AppModuleSimulation: AppModuleSimulation{}, + keeper: keeper, + accountKeeper: accountKeeper, } } -// module name +// Name returns the auth module's name. func (AppModule) Name() string { return ModuleName } -// register invariants +// RegisterInvariants registers the auth module invariants. func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { keeper.RegisterInvariants(ir, am.accountKeeper) } -// RegisterStoreDecoder doesn't register any KVPair -func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} - -// module message route name +// Route returns the message routing key for the auth module. func (AppModule) Route() string { return RouterKey } -// module handler +// NewHandler returns an sdk.Handler for the auth module. func (am AppModule) NewHandler() sdk.Handler { return NewHandler(am.keeper) } -// module querier route name +// QuerierRoute returns the auth module's querier route name. func (AppModule) QuerierRoute() string { return RouterKey } -// module querier +// NewQuerierHandler returns the auth module sdk.Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return keeper.NewQuerier(am.keeper) } -// module init-genesis +// InitGenesis performs genesis initialization for the auth module. It returns +// no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState ModuleCdc.MustUnmarshalJSON(data, &genesisState) @@ -110,16 +122,18 @@ func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.Va return []abci.ValidatorUpdate{} } -// module export genesis +// ExportGenesis returns the exported genesis state as raw bytes for the auth +// module. func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { gs := ExportGenesis(ctx, am.keeper) return ModuleCdc.MustMarshalJSON(gs) } -// module begin-block +// BeginBlock returns the begin blocker for the auth module. func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} -// module end-block +// EndBlock returns the end blocker for the auth module. It returns no validator +// updates. func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } diff --git a/x/crisis/module.go b/x/crisis/module.go index 07a37bd648f8..2d40328437f7 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -18,8 +18,9 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) // AppModuleBasic defines the basic application module used by the crisis module. @@ -61,9 +62,20 @@ func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { // GetQueryCmd returns no root query command for the crisis module. func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil } +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the crisis module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers a decoder for the crisis module's types +func (AppModuleSimulation) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} + +//____________________________________________________________________________ + // AppModule implements an application module for the crisis module. type AppModule struct { AppModuleBasic + AppModuleSimulation // NOTE: We store a reference to the keeper here so that after a module // manager is created, the invariants can be properly registered and @@ -74,8 +86,9 @@ type AppModule struct { // NewAppModule creates a new AppModule object func NewAppModule(keeper *keeper.Keeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, - keeper: keeper, + AppModuleBasic: AppModuleBasic{}, + AppModuleSimulation: AppModuleSimulation{}, + keeper: keeper, } } @@ -87,9 +100,6 @@ func (AppModule) Name() string { // RegisterInvariants performs a no-op. func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} -// RegisterStoreDecoder doesn't register any KVPair -func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} - // Route returns the message routing key for the crisis module. func (AppModule) Route() string { return RouterKey diff --git a/x/distribution/module.go b/x/distribution/module.go index b526954450e7..9376cad087b0 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -19,29 +19,31 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) -// app module basics object +// AppModuleBasic defines the basic application module used by the distribution module. type AppModuleBasic struct{} -// module name +// Name returns the distribution module's name. func (AppModuleBasic) Name() string { return ModuleName } -// register module codec +// RegisterCodec registers the distribution module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { RegisterCodec(cdc) } -// default genesis state +// DefaultGenesis returns default genesis state as raw bytes for the distribution +// module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) } -// module validate genesis +// ValidateGenesis performs genesis state validation for the distribution module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState err := ModuleCdc.UnmarshalJSON(bz, &data) @@ -51,24 +53,36 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } -// register rest routes +// RegisterRESTRoutes registers the REST routes for the distribution module. func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { rest.RegisterRoutes(ctx, rtr, StoreKey) } -// get the root tx command of this module +// GetTxCmd returns the root tx command for the distribution module. func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { return cli.GetTxCmd(StoreKey, cdc) } -// get the root query command of this module +// GetQueryCmd returns no root query command for the distribution module. func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(StoreKey, cdc) } -// app module +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the distribution module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers no decoder for distribution module's types +func (AppModuleSimulation) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} + +//____________________________________________________________________________ + +// AppModule implements an application module for the distribution module. type AppModule struct { AppModuleBasic + AppModuleSimulation + keeper Keeper supplyKeeper types.SupplyKeeper } @@ -76,18 +90,19 @@ type AppModule struct { // NewAppModule creates a new AppModule object func NewAppModule(keeper Keeper, supplyKeeper types.SupplyKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, - keeper: keeper, - supplyKeeper: supplyKeeper, + AppModuleBasic: AppModuleBasic{}, + AppModuleSimulation: AppModuleSimulation{}, + keeper: keeper, + supplyKeeper: supplyKeeper, } } -// module name +// Name returns the distribution module's name. func (AppModule) Name() string { return ModuleName } -// register invariants +// RegisterInvariants registers the distribution module invariants. func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { RegisterInvariants(ir, am.keeper) } @@ -98,27 +113,28 @@ func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { sdr[StoreKey] = decoder.DecodeStore } -// module message route name +// Route returns the message routing key for the distribution module. func (AppModule) Route() string { return RouterKey } -// module handler +// NewHandler returns an sdk.Handler for the distribution module. func (am AppModule) NewHandler() sdk.Handler { return NewHandler(am.keeper) } -// module querier route name +// QuerierRoute returns the distribution module's querier route name. func (AppModule) QuerierRoute() string { return QuerierRoute } -// module querier +// NewQuerierHandler returns the distribution module sdk.Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } -// module init-genesis +// InitGenesis performs genesis initialization for the distribution module. It returns +// no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState ModuleCdc.MustUnmarshalJSON(data, &genesisState) @@ -126,18 +142,20 @@ func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.Va return []abci.ValidatorUpdate{} } -// module export genesis +// ExportGenesis returns the exported genesis state as raw bytes for the distribution +// module. func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { gs := ExportGenesis(ctx, am.keeper) return ModuleCdc.MustMarshalJSON(gs) } -// module begin-block +// BeginBlock returns the begin blocker for the distribution module. func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { BeginBlocker(ctx, req, am.keeper) } -// module end-block +// EndBlock returns the end blocker for the distribution module. It returns no validator +// updates. func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } diff --git a/x/genaccounts/module.go b/x/genaccounts/module.go index 90c01de8b15e..4720cfbd8e15 100644 --- a/x/genaccounts/module.go +++ b/x/genaccounts/module.go @@ -17,27 +17,29 @@ import ( ) var ( - _ module.AppModuleGenesis = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleGenesis = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) -// app module basics object +// AppModuleBasic defines the basic application module used by the genesis accounts module. type AppModuleBasic struct{} -// module name +// Name returns the genesis accounts module's name. func (AppModuleBasic) Name() string { return ModuleName } -// register module codec +// RegisterCodec registers the genesis accounts module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {} -// default genesis state +// DefaultGenesis returns default genesis state as raw bytes for the genesis accounts +// module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return ModuleCdc.MustMarshalJSON(GenesisState{}) } -// module validate genesis +// ValidateGenesis performs genesis state validation for the genesis accounts module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState err := ModuleCdc.UnmarshalJSON(bz, &data) @@ -47,17 +49,18 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } -// register rest routes +// RegisterRESTRoutes registers the REST routes for the genesis accounts module. func (AppModuleBasic) RegisterRESTRoutes(_ context.CLIContext, _ *mux.Router) {} -// get the root tx command of this module +// GetTxCmd returns the root tx command for the genesis accounts module. func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil } -// get the root query command of this module +// GetQueryCmd returns no root query command for the genesis accounts module. func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil } // extra function from sdk.AppModuleBasic -// iterate the genesis accounts and perform an operation at each of them + +// IterateGenesisAccounts iterates over the genesis accounts and perform an operation at each of them // - to used by other modules func (AppModuleBasic) IterateGenesisAccounts(cdc *codec.Codec, appGenesis map[string]json.RawMessage, iterateFn func(exported.Account) (stop bool)) { @@ -71,10 +74,21 @@ func (AppModuleBasic) IterateGenesisAccounts(cdc *codec.Codec, appGenesis map[st } } -//___________________________ -// app module +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the genesis accounts module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers a decoder for genesis accounts module's types +func (AppModuleSimulation) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} + +//____________________________________________________________________________ + +// AppModule implements an application module for the genesis accounts module. type AppModule struct { AppModuleBasic + AppModuleSimulation + accountKeeper types.AccountKeeper } @@ -82,12 +96,14 @@ type AppModule struct { func NewAppModule(accountKeeper types.AccountKeeper) module.AppModule { return module.NewGenesisOnlyAppModule(AppModule{ - AppModuleBasic: AppModuleBasic{}, - accountKeeper: accountKeeper, + AppModuleBasic: AppModuleBasic{}, + AppModuleSimulation: AppModuleSimulation{}, + accountKeeper: accountKeeper, }) } -// module init-genesis +// InitGenesis performs genesis initialization for the genesis accounts module. It returns +// no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState ModuleCdc.MustUnmarshalJSON(data, &genesisState) @@ -95,7 +111,8 @@ func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.Va return []abci.ValidatorUpdate{} } -// module export genesis +// ExportGenesis returns the exported genesis state as raw bytes for the genesis accounts +// module. func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { gs := ExportGenesis(ctx, am.accountKeeper) return ModuleCdc.MustMarshalJSON(gs) diff --git a/x/genutil/module.go b/x/genutil/module.go index f6051fccb549..1eda794b2fcb 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -18,25 +18,27 @@ import ( var ( _ module.AppModuleGenesis = AppModule{} _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) -// app module basics object +// AppModuleBasic defines the basic application module used by the genutil module. type AppModuleBasic struct{} -// module name +// Name returns the genutil module's name. func (AppModuleBasic) Name() string { return ModuleName } -// register module codec +// RegisterCodec registers the genutil module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {} -// default genesis state +// DefaultGenesis returns default genesis state as raw bytes for the genutil +// module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return ModuleCdc.MustMarshalJSON(GenesisState{}) } -// module validate genesis +// ValidateGenesis performs genesis state validation for the genutil module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState err := ModuleCdc.UnmarshalJSON(bz, &data) @@ -46,19 +48,30 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } -// register rest routes +// RegisterRESTRoutes registers the REST routes for the genutil module. func (AppModuleBasic) RegisterRESTRoutes(_ context.CLIContext, _ *mux.Router) {} -// get the root tx command of this module +// GetTxCmd returns the root tx command for the genutil module. func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil } -// get the root query command of this module +// GetQueryCmd returns no root query command for the genutil module. func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil } -//___________________________ -// app module +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the auth module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers a decoder for genutil module's types +func (AppModuleSimulation) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} + +//____________________________________________________________________________ + +// AppModule implements an application module for the genutil module. type AppModule struct { AppModuleBasic + AppModuleSimulation + accountKeeper types.AccountKeeper stakingKeeper types.StakingKeeper deliverTx deliverTxfn @@ -70,20 +83,23 @@ func NewAppModule(accountKeeper types.AccountKeeper, return module.NewGenesisOnlyAppModule(AppModule{ AppModuleBasic: AppModuleBasic{}, + AppModuleSimulation: AppModuleSimulation{}, accountKeeper: accountKeeper, stakingKeeper: stakingKeeper, deliverTx: deliverTx, }) } -// module init-genesis +// InitGenesis performs genesis initialization for the genutil module. It returns +// no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState ModuleCdc.MustUnmarshalJSON(data, &genesisState) return InitGenesis(ctx, ModuleCdc, am.stakingKeeper, am.deliverTx, genesisState) } -// module export genesis +// ExportGenesis returns the exported genesis state as raw bytes for the genutil +// module. func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { return nil } diff --git a/x/mint/module.go b/x/mint/module.go index 285c0356541b..861f12b5d568 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -18,29 +18,31 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) -// app module basics object +// AppModuleBasic defines the basic application module used by the mint module. type AppModuleBasic struct{} var _ module.AppModuleBasic = AppModuleBasic{} -// module name +// Name returns the mint module's name. func (AppModuleBasic) Name() string { return ModuleName } -// register module codec +// RegisterCodec registers the mint module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {} -// default genesis state +// DefaultGenesis returns default genesis state as raw bytes for the mint +// module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) } -// module validate genesis +// ValidateGenesis performs genesis state validation for the mint module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState err := ModuleCdc.UnmarshalJSON(bz, &data) @@ -50,40 +52,54 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } -// register rest routes +// RegisterRESTRoutes registers the REST routes for the mint module. func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { rest.RegisterRoutes(ctx, rtr) } -// get the root tx command of this module +// GetTxCmd returns the root tx command for the mint module. func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil } -// get the root query command of this module +// GetQueryCmd returns no root query command for the mint module. func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(cdc) } -//___________________________ -// app module +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the mint module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers a decoder for mint module's types +func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the mint module. type AppModule struct { AppModuleBasic + AppModuleSimulation + keeper Keeper } // NewAppModule creates a new AppModule object func NewAppModule(keeper Keeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, - keeper: keeper, + AppModuleBasic: AppModuleBasic{}, + AppModuleSimulation: AppModuleSimulation{}, + keeper: keeper, } } -// module name +// Name returns the mint module's name. func (AppModule) Name() string { return ModuleName } -// register invariants +// RegisterInvariants registers the mint module invariants. func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterStoreDecoder registers the function to decode the types stored in the @@ -92,23 +108,24 @@ func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { sdr[StoreKey] = decoder.DecodeStore } -// module message route name +// Route returns the message routing key for the mint module. func (AppModule) Route() string { return "" } -// module handler +// NewHandler returns an sdk.Handler for the mint module. func (am AppModule) NewHandler() sdk.Handler { return nil } -// module querier route name +// QuerierRoute returns the mint module's querier route name. func (AppModule) QuerierRoute() string { return QuerierRoute } -// module querier +// NewQuerierHandler returns the mint module sdk.Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } -// module init-genesis +// InitGenesis performs genesis initialization for the mint module. It returns +// no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState ModuleCdc.MustUnmarshalJSON(data, &genesisState) @@ -116,18 +133,20 @@ func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.Va return []abci.ValidatorUpdate{} } -// module export genesis +// ExportGenesis returns the exported genesis state as raw bytes for the mint +// module. func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { gs := ExportGenesis(ctx, am.keeper) return ModuleCdc.MustMarshalJSON(gs) } -// module begin-block +// BeginBlock returns the begin blocker for the mint module. func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) { BeginBlocker(ctx, am.keeper) } -// module end-block +// EndBlock returns the end blocker for the mint module. It returns no validator +// updates. func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } diff --git a/x/params/module.go b/x/params/module.go index e339d954990c..de643c73c03b 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -16,32 +16,31 @@ var ( _ module.AppModuleBasic = AppModuleBasic{} ) -const moduleName = "params" - -// app module basics object +// AppModuleBasic defines the basic application module used by the params module. type AppModuleBasic struct{} -// module name +// Name returns the params module's name. func (AppModuleBasic) Name() string { - return moduleName + return ModuleName } -// register module codec +// RegisterCodec registers the params module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { types.RegisterCodec(cdc) } -// default genesis state +// DefaultGenesis returns default genesis state as raw bytes for the params +// module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return nil } -// module validate genesis +// ValidateGenesis performs genesis state validation for the params module. func (AppModuleBasic) ValidateGenesis(_ json.RawMessage) error { return nil } -// register rest routes +// RegisterRESTRoutes registers the REST routes for the params module. func (AppModuleBasic) RegisterRESTRoutes(_ context.CLIContext, _ *mux.Router) {} -// get the root tx command of this module +// GetTxCmd returns the root tx command for the params module. func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil } -// get the root query command of this module +// GetQueryCmd returns no root query command for the params module. func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil } diff --git a/x/slashing/module.go b/x/slashing/module.go index ca74439780e0..7f8fee3bc152 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -21,29 +21,31 @@ import ( var ( _ module.AppModule = AppModule{} _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) -// app module basics object +// AppModuleBasic defines the basic application module used by the slashing module. type AppModuleBasic struct{} var _ module.AppModuleBasic = AppModuleBasic{} -// module name +// Name returns the slashing module's name. func (AppModuleBasic) Name() string { return types.ModuleName } -// register module codec +// RegisterCodec registers the slashing module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { RegisterCodec(cdc) } -// default genesis state +// DefaultGenesis returns default genesis state as raw bytes for the slashing +// module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) } -// module validate genesis +// ValidateGenesis performs genesis state validation for the slashing module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState err := ModuleCdc.UnmarshalJSON(bz, &data) @@ -53,25 +55,38 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } -// register rest routes +// RegisterRESTRoutes registers the REST routes for the slashing module. func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { rest.RegisterRoutes(ctx, rtr) } -// get the root tx command of this module +// GetTxCmd returns the root tx command for the slashing module. func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { return cli.GetTxCmd(cdc) } -// get the root query command of this module +// GetQueryCmd returns no root query command for the slashing module. func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(StoreKey, cdc) } -//___________________________ -// app module +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the slashing module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers a decoder for slashing module's types +func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the slashing module. type AppModule struct { AppModuleBasic + AppModuleSimulation + keeper Keeper stakingKeeper types.StakingKeeper } @@ -80,17 +95,18 @@ type AppModule struct { func NewAppModule(keeper Keeper, stakingKeeper types.StakingKeeper) AppModule { return AppModule{ AppModuleBasic: AppModuleBasic{}, + AppModuleSimulation: AppModuleSimulation{}, keeper: keeper, stakingKeeper: stakingKeeper, } } -// module name +// Name returns the slashing module's name. func (AppModule) Name() string { return ModuleName } -// register invariants +// RegisterInvariants registers the slashing module invariants. func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterStoreDecoder registers the function to decode the types stored in the @@ -99,27 +115,28 @@ func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { sdr[StoreKey] = decoder.DecodeStore } -// module message route name +// Route returns the message routing key for the slashing module. func (AppModule) Route() string { return RouterKey } -// module handler +// NewHandler returns an sdk.Handler for the slashing module. func (am AppModule) NewHandler() sdk.Handler { return NewHandler(am.keeper) } -// module querier route name +// QuerierRoute returns the slashing module's querier route name. func (AppModule) QuerierRoute() string { return QuerierRoute } -// module querier +// NewQuerierHandler returns the slashing module sdk.Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } -// module init-genesis +// InitGenesis performs genesis initialization for the slashing module. It returns +// no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState ModuleCdc.MustUnmarshalJSON(data, &genesisState) @@ -127,18 +144,20 @@ func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.Va return []abci.ValidatorUpdate{} } -// module export genesis +// ExportGenesis returns the exported genesis state as raw bytes for the slashing +// module. func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { gs := ExportGenesis(ctx, am.keeper) return ModuleCdc.MustMarshalJSON(gs) } -// module begin-block +// BeginBlock returns the begin blocker for the slashing module. func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { BeginBlocker(ctx, req, am.keeper) } -// module end-block +// EndBlock returns the end blocker for the slashing module. It returns no validator +// updates. func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } diff --git a/x/staking/module.go b/x/staking/module.go index 83b0cea5cded..3ce3fec4473c 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -23,31 +23,33 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) -// app module basics object +// AppModuleBasic defines the basic application module used by the staking module. type AppModuleBasic struct{} var _ module.AppModuleBasic = AppModuleBasic{} -// module name +// Name returns the staking module's name. func (AppModuleBasic) Name() string { return ModuleName } -// register module codec +// RegisterCodec registers the staking module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { RegisterCodec(cdc) } -// default genesis state +// DefaultGenesis returns default genesis state as raw bytes for the staking +// module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) } -// module validate genesis +// ValidateGenesis performs genesis state validation for the staking module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState err := ModuleCdc.UnmarshalJSON(bz, &data) @@ -57,17 +59,17 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } -// register rest routes +// RegisterRESTRoutes registers the REST routes for the staking module. func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { rest.RegisterRoutes(ctx, rtr) } -// get the root tx command of this module +// GetTxCmd returns the root tx command for the staking module. func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { return cli.GetTxCmd(StoreKey, cdc) } -// get the root query command of this module +// GetQueryCmd returns no root query command for the staking module. func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(StoreKey, cdc) } @@ -93,9 +95,23 @@ func (AppModuleBasic) BuildCreateValidatorMsg(cliCtx context.CLIContext, return cli.BuildCreateValidatorMsg(cliCtx, txBldr) } -// app module +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the staking module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers a decoder for staking module's types +func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the staking module. type AppModule struct { AppModuleBasic + AppModuleSimulation + keeper Keeper distrKeeper types.DistributionKeeper accKeeper types.AccountKeeper @@ -107,67 +123,65 @@ func NewAppModule(keeper Keeper, distrKeeper types.DistributionKeeper, accKeeper supplyKeeper types.SupplyKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, - keeper: keeper, - distrKeeper: distrKeeper, - accKeeper: accKeeper, - supplyKeeper: supplyKeeper, + AppModuleBasic: AppModuleBasic{}, + AppModuleSimulation: AppModuleSimulation{}, + keeper: keeper, + distrKeeper: distrKeeper, + accKeeper: accKeeper, + supplyKeeper: supplyKeeper, } } -// module name +// Name returns the staking module's name. func (AppModule) Name() string { return ModuleName } -// register invariants +// RegisterInvariants registers the staking module invariants. func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { RegisterInvariants(ir, am.keeper) } -// RegisterStoreDecoder registers the function to decode the types stored in the -// KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore -} - -// module message route name +// Route returns the message routing key for the staking module. func (AppModule) Route() string { return RouterKey } -// module handler +// NewHandler returns an sdk.Handler for the staking module. func (am AppModule) NewHandler() sdk.Handler { return NewHandler(am.keeper) } -// module querier route name +// QuerierRoute returns the staking module's querier route name. func (AppModule) QuerierRoute() string { return QuerierRoute } -// module querier +// NewQuerierHandler returns the staking module sdk.Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } -// module init-genesis +// InitGenesis performs genesis initialization for the staking module. It returns +// no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState ModuleCdc.MustUnmarshalJSON(data, &genesisState) return InitGenesis(ctx, am.keeper, am.accKeeper, am.supplyKeeper, genesisState) } -// module export genesis +// ExportGenesis returns the exported genesis state as raw bytes for the staking +// module. func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { gs := ExportGenesis(ctx, am.keeper) return ModuleCdc.MustMarshalJSON(gs) } -// module begin-block +// BeginBlock returns the begin blocker for the staking module. func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} -// module end-block +// EndBlock returns the end blocker for the staking module. It returns no validator +// updates. func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return EndBlocker(ctx, am.keeper) } diff --git a/x/supply/module.go b/x/supply/module.go index e1f21bd0ba5b..e3576b466f60 100644 --- a/x/supply/module.go +++ b/x/supply/module.go @@ -19,29 +19,31 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) -// app module basics object +// AppModuleBasic defines the basic application module used by the supply module. type AppModuleBasic struct{} -// module name +// Name returns the supply module's name. func (AppModuleBasic) Name() string { return ModuleName } -// register module codec +// RegisterCodec registers the supply module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { RegisterCodec(cdc) } -// default genesis state +// DefaultGenesis returns default genesis state as raw bytes for the supply +// module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) } -// module validate genesis +// ValidateGenesis performs genesis state validation for the supply module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState err := ModuleCdc.UnmarshalJSON(bz, &data) @@ -51,22 +53,36 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } -// register rest routes +// RegisterRESTRoutes registers the REST routes for the supply module. func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { rest.RegisterRoutes(ctx, rtr) } -// get the root tx command of this module +// GetTxCmd returns the root tx command for the supply module. func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil } -// get the root query command of this module +// GetQueryCmd returns no root query command for the supply module. func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(cdc) } -// app module +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the supply module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers a decoder for supply module's types +func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore +} + +//____________________________________________________________________________ + +// AppModule implements an application module for the supply module. type AppModule struct { AppModuleBasic + AppModuleSimulation + keeper Keeper ak types.AccountKeeper } @@ -74,47 +90,43 @@ type AppModule struct { // NewAppModule creates a new AppModule object func NewAppModule(keeper Keeper, ak types.AccountKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, - keeper: keeper, - ak: ak, + AppModuleBasic: AppModuleBasic{}, + AppModuleSimulation: AppModuleSimulation{}, + keeper: keeper, + ak: ak, } } -// module name +// Name returns the supply module's name. func (AppModule) Name() string { return ModuleName } -// register invariants +// RegisterInvariants registers the supply module invariants. func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { RegisterInvariants(ir, am.keeper) } -// RegisterStoreDecoder registers the function to decode the types stored in the -// KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore -} - -// module message route name +// Route returns the message routing key for the supply module. func (AppModule) Route() string { return RouterKey } -// module handler +// NewHandler returns an sdk.Handler for the supply module. func (am AppModule) NewHandler() sdk.Handler { return nil } -// module querier route name +// QuerierRoute returns the supply module's querier route name. func (AppModule) QuerierRoute() string { return QuerierRoute } -// module querier +// NewQuerierHandler returns the supply module sdk.Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } -// module init-genesis +// InitGenesis performs genesis initialization for the supply module. It returns +// no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState ModuleCdc.MustUnmarshalJSON(data, &genesisState) @@ -122,16 +134,18 @@ func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.Va return []abci.ValidatorUpdate{} } -// module export genesis +// ExportGenesis returns the exported genesis state as raw bytes for the supply +// module. func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { gs := ExportGenesis(ctx, am.keeper) return ModuleCdc.MustMarshalJSON(gs) } -// module begin-block +// BeginBlock returns the begin blocker for the supply module. func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} -// module end-block +// EndBlock returns the end blocker for the supply module. It returns no validator +// updates. func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } From cca3a19807b3f2e1c28e773fcda528c3b7409e68 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 9 Aug 2019 15:33:55 +0200 Subject: [PATCH 10/18] mino fixes --- simapp/sim_test.go | 2 +- x/genutil/module.go | 12 ++++++------ x/slashing/module.go | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/simapp/sim_test.go b/simapp/sim_test.go index f4cef72e5a22..b7069359eee7 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -601,7 +601,7 @@ func TestAppImportExport(t *testing.T) { storeB := ctxB.KVStore(storeKeyB) failedKVs := sdk.DiffKVStores(storeA, storeB, prefixes) fmt.Printf("Compared %d key/value pairs between %s and %s\n", len(failedKVs)/2, storeKeyA, storeKeyB) - require.Len(t, failedKVs, 0, GetSimulationLog(storeKeyA.Name(), app.mm.StoreDecoders, app.cdc, failedKVs)) + require.Len(t, failedKVs, 0, GetSimulationLog(storeKeyA.Name(), app.sm.StoreDecoders, app.cdc, failedKVs)) } } diff --git a/x/genutil/module.go b/x/genutil/module.go index 1eda794b2fcb..bf1b0ceb49b8 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -16,8 +16,8 @@ import ( ) var ( - _ module.AppModuleGenesis = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleGenesis = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} _ module.AppModuleSimulation = AppModuleSimulation{} ) @@ -82,11 +82,11 @@ func NewAppModule(accountKeeper types.AccountKeeper, stakingKeeper types.StakingKeeper, deliverTx deliverTxfn) module.AppModule { return module.NewGenesisOnlyAppModule(AppModule{ - AppModuleBasic: AppModuleBasic{}, + AppModuleBasic: AppModuleBasic{}, AppModuleSimulation: AppModuleSimulation{}, - accountKeeper: accountKeeper, - stakingKeeper: stakingKeeper, - deliverTx: deliverTx, + accountKeeper: accountKeeper, + stakingKeeper: stakingKeeper, + deliverTx: deliverTx, }) } diff --git a/x/slashing/module.go b/x/slashing/module.go index 7f8fee3bc152..dbdf4edce8a0 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -19,8 +19,8 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} _ module.AppModuleSimulation = AppModuleSimulation{} ) @@ -94,10 +94,10 @@ type AppModule struct { // NewAppModule creates a new AppModule object func NewAppModule(keeper Keeper, stakingKeeper types.StakingKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, + AppModuleBasic: AppModuleBasic{}, AppModuleSimulation: AppModuleSimulation{}, - keeper: keeper, - stakingKeeper: stakingKeeper, + keeper: keeper, + stakingKeeper: stakingKeeper, } } From c48a03591719b7c2308be64fa1667677036824ea Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 9 Aug 2019 15:52:54 +0200 Subject: [PATCH 11/18] cleanup --- x/bank/module.go | 42 +++++++++++------------ x/crisis/module.go | 4 +-- x/distribution/module.go | 12 +++---- x/genaccounts/module.go | 6 ++-- x/gov/module.go | 74 +++++++++++++++++++++++----------------- x/mint/module.go | 8 +---- x/slashing/module.go | 6 ---- 7 files changed, 73 insertions(+), 79 deletions(-) diff --git a/x/bank/module.go b/x/bank/module.go index 3cc8b543ef09..c2545036a758 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -24,22 +24,22 @@ var ( _ module.AppModuleSimulation = AppModuleSimulation{} ) -// AppModuleBasic defines the basic application module used by the auth module. +// AppModuleBasic defines the basic application module used by the bank module. type AppModuleBasic struct{} -// Name returns the auth module's name. +// Name returns the bank module's name. func (AppModuleBasic) Name() string { return ModuleName } -// RegisterCodec registers the auth module's types for the given codec. +// RegisterCodec registers the bank module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { RegisterCodec(cdc) } -// DefaultGenesis returns default genesis state as raw bytes for the auth +// DefaultGenesis returns default genesis state as raw bytes for the bank // module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) } -// ValidateGenesis performs genesis state validation for the auth module. +// ValidateGenesis performs genesis state validation for the bank module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState err := ModuleCdc.UnmarshalJSON(bz, &data) @@ -49,30 +49,30 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } -// RegisterRESTRoutes registers the REST routes for the auth module. +// RegisterRESTRoutes registers the REST routes for the bank module. func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { rest.RegisterRoutes(ctx, rtr) } -// GetTxCmd returns the root tx command for the auth module. +// GetTxCmd returns the root tx command for the bank module. func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { return cli.GetTxCmd(cdc) } -// GetQueryCmd returns no root query command for the auth module. +// GetQueryCmd returns no root query command for the bank module. func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil } //____________________________________________________________________________ -// AppModuleSimulation defines the module simulation functions used by the auth module. +// AppModuleSimulation defines the module simulation functions used by the bank module. type AppModuleSimulation struct{} -// RegisterStoreDecoder registers no decoder for auth module's types +// RegisterStoreDecoder performs a no-op. func (AppModuleSimulation) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} //____________________________________________________________________________ -// AppModule implements an application module for the auth module. +// AppModule implements an application module for the bank module. type AppModule struct { AppModuleBasic AppModuleSimulation @@ -91,29 +91,29 @@ func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper) AppModule { } } -// Name returns the auth module's name. +// Name returns the bank module's name. func (AppModule) Name() string { return ModuleName } -// RegisterInvariants registers the auth module invariants. +// RegisterInvariants registers the bank module invariants. func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { keeper.RegisterInvariants(ir, am.accountKeeper) } -// Route returns the message routing key for the auth module. +// Route returns the message routing key for the bank module. func (AppModule) Route() string { return RouterKey } -// NewHandler returns an sdk.Handler for the auth module. +// NewHandler returns an sdk.Handler for the bank module. func (am AppModule) NewHandler() sdk.Handler { return NewHandler(am.keeper) } -// QuerierRoute returns the auth module's querier route name. +// QuerierRoute returns the bank module's querier route name. func (AppModule) QuerierRoute() string { return RouterKey } -// NewQuerierHandler returns the auth module sdk.Querier. +// NewQuerierHandler returns the bank module sdk.Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return keeper.NewQuerier(am.keeper) } -// InitGenesis performs genesis initialization for the auth module. It returns +// InitGenesis performs genesis initialization for the bank module. It returns // no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState @@ -122,17 +122,17 @@ func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.Va return []abci.ValidatorUpdate{} } -// ExportGenesis returns the exported genesis state as raw bytes for the auth +// ExportGenesis returns the exported genesis state as raw bytes for the bank // module. func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { gs := ExportGenesis(ctx, am.keeper) return ModuleCdc.MustMarshalJSON(gs) } -// BeginBlock returns the begin blocker for the auth module. +// BeginBlock performs a no-op. func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} -// EndBlock returns the end blocker for the auth module. It returns no validator +// EndBlock returns the end blocker for the bank module. It returns no validator // updates. func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} diff --git a/x/crisis/module.go b/x/crisis/module.go index 2d40328437f7..ec84c736d3c8 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -67,7 +67,7 @@ func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil } // AppModuleSimulation defines the module simulation functions used by the crisis module. type AppModuleSimulation struct{} -// RegisterStoreDecoder registers a decoder for the crisis module's types +// RegisterStoreDecoder performs a no-op. func (AppModuleSimulation) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} //____________________________________________________________________________ @@ -134,7 +134,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { return types.ModuleCdc.MustMarshalJSON(gs) } -// BeginBlock returns the begin blocker for the crisis module. +// BeginBlock performs a no-op. func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} // EndBlock returns the end blocker for the crisis module. It returns no validator diff --git a/x/distribution/module.go b/x/distribution/module.go index 9376cad087b0..bf838fd0d938 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -73,8 +73,10 @@ func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { // AppModuleSimulation defines the module simulation functions used by the distribution module. type AppModuleSimulation struct{} -// RegisterStoreDecoder registers no decoder for distribution module's types -func (AppModuleSimulation) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} +// RegisterStoreDecoder registers a decoder for distribution module's types +func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore +} //____________________________________________________________________________ @@ -107,12 +109,6 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { RegisterInvariants(ir, am.keeper) } -// RegisterStoreDecoder registers the function to decode the types stored in the -// KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore -} - // Route returns the message routing key for the distribution module. func (AppModule) Route() string { return RouterKey diff --git a/x/genaccounts/module.go b/x/genaccounts/module.go index 4720cfbd8e15..753687ae4d1b 100644 --- a/x/genaccounts/module.go +++ b/x/genaccounts/module.go @@ -49,10 +49,10 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } -// RegisterRESTRoutes registers the REST routes for the genesis accounts module. +// RegisterRESTRoutes registers no REST routes for the genesis accounts module. func (AppModuleBasic) RegisterRESTRoutes(_ context.CLIContext, _ *mux.Router) {} -// GetTxCmd returns the root tx command for the genesis accounts module. +// GetTxCmd returns no root tx command for the genesis accounts module. func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil } // GetQueryCmd returns no root query command for the genesis accounts module. @@ -79,7 +79,7 @@ func (AppModuleBasic) IterateGenesisAccounts(cdc *codec.Codec, appGenesis map[st // AppModuleSimulation defines the module simulation functions used by the genesis accounts module. type AppModuleSimulation struct{} -// RegisterStoreDecoder registers a decoder for genesis accounts module's types +// RegisterStoreDecoder performs a no-op. func (AppModuleSimulation) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} //____________________________________________________________________________ diff --git a/x/gov/module.go b/x/gov/module.go index 35de05a82792..c7c1631fd8f7 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -22,11 +22,12 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) -// AppModuleBasic - app module basics object +// AppModuleBasic defines the basic application module used by the gov module. type AppModuleBasic struct { proposalHandlers []client.ProposalHandler // proposal handlers which live in governance cli and rest } @@ -38,24 +39,23 @@ func NewAppModuleBasic(proposalHandlers ...client.ProposalHandler) AppModuleBasi } } -var _ module.AppModuleBasic = AppModuleBasic{} - -// Name - module name +// Name returns the gov module's name. func (AppModuleBasic) Name() string { return types.ModuleName } -// RegisterCodec -register module codec +// RegisterCodec registers the gov module's types for the given codec. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { RegisterCodec(cdc) } -// DefaultGenesis - default genesis state +// DefaultGenesis returns default genesis state as raw bytes for the gov +// module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) } -// ValidateGenesis - module validate genesis +// ValidateGenesis performs genesis state validation for the gov module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState err := ModuleCdc.UnmarshalJSON(bz, &data) @@ -65,7 +65,7 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } -// RegisterRESTRoutes - register rest routes +// RegisterRESTRoutes registers the REST routes for the gov module. func (a AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { var proposalRESTHandlers []rest.ProposalRESTHandler for _, proposalHandler := range a.proposalHandlers { @@ -75,7 +75,7 @@ func (a AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Rout rest.RegisterRoutes(ctx, rtr, proposalRESTHandlers) } -// GetTxCmd gets the root tx command of this module +// GetTxCmd returns the root tx command for the gov module. func (a AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { var proposalCLIHandlers []*cobra.Command @@ -86,16 +86,28 @@ func (a AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { return cli.GetTxCmd(StoreKey, cdc, proposalCLIHandlers) } -// GetQueryCmd gets the root query command of this module +// GetQueryCmd returns no root query command for the gov module. func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(StoreKey, cdc) } -//___________________________ +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the gov module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder performs a no-op. +func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = decoder.DecodeStore +} + +//____________________________________________________________________________ -// AppModule - app module object +// AppModule implements an application module for the gov module. type AppModule struct { AppModuleBasic + AppModuleSimulation + keeper Keeper supplyKeeper types.SupplyKeeper } @@ -103,13 +115,14 @@ type AppModule struct { // NewAppModule creates a new AppModule object func NewAppModule(keeper Keeper, supplyKeeper types.SupplyKeeper) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{}, - keeper: keeper, - supplyKeeper: supplyKeeper, + AppModuleBasic: AppModuleBasic{}, + AppModuleSimulation: AppModuleSimulation{}, + keeper: keeper, + supplyKeeper: supplyKeeper, } } -// Name - module name +// Name returns the gov module's name. func (AppModule) Name() string { return ModuleName } @@ -119,33 +132,28 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { RegisterInvariants(ir, am.keeper) } -// RegisterStoreDecoder registers the function to decode the types stored in the -// KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore -} - -// Route - module message route name +// Route returns the message routing key for the gov module. func (AppModule) Route() string { return RouterKey } -// NewHandler - module handler +// NewHandler returns an sdk.Handler for the gov module. func (am AppModule) NewHandler() sdk.Handler { return NewHandler(am.keeper) } -// QuerierRoute - module querier route name +// QuerierRoute returns the gov module's querier route name. func (AppModule) QuerierRoute() string { return QuerierRoute } -// NewQuerierHandler - module querier +// NewQuerierHandler returns no sdk.Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) } -// InitGenesis - module init-genesis +// InitGenesis performs genesis initialization for the gov module. It returns +// no validator updates. func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { var genesisState GenesisState ModuleCdc.MustUnmarshalJSON(data, &genesisState) @@ -153,16 +161,18 @@ func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.Va return []abci.ValidatorUpdate{} } -// ExportGenesis - module export genesis +// ExportGenesis returns the exported genesis state as raw bytes for the gov +// module. func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { gs := ExportGenesis(ctx, am.keeper) return ModuleCdc.MustMarshalJSON(gs) } -// BeginBlock - module begin-block +// BeginBlock performs a no-op. func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} -// EndBlock - module end-block +// EndBlock returns the end blocker for the gov module. It returns no validator +// updates. func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { EndBlocker(ctx, am.keeper) return []abci.ValidatorUpdate{} diff --git a/x/mint/module.go b/x/mint/module.go index 861f12b5d568..695ca649a1ab 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -70,7 +70,7 @@ func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { // AppModuleSimulation defines the module simulation functions used by the mint module. type AppModuleSimulation struct{} -// RegisterStoreDecoder registers a decoder for mint module's types +// RegisterStoreDecoder registers a decoder for mint module's types. func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { sdr[StoreKey] = decoder.DecodeStore } @@ -102,12 +102,6 @@ func (AppModule) Name() string { // RegisterInvariants registers the mint module invariants. func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} -// RegisterStoreDecoder registers the function to decode the types stored in the -// KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore -} - // Route returns the message routing key for the mint module. func (AppModule) Route() string { return "" } diff --git a/x/slashing/module.go b/x/slashing/module.go index dbdf4edce8a0..7cfc052d95de 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -109,12 +109,6 @@ func (AppModule) Name() string { // RegisterInvariants registers the slashing module invariants. func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} -// RegisterStoreDecoder registers the function to decode the types stored in the -// KVStore -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore -} - // Route returns the message routing key for the slashing module. func (AppModule) Route() string { return RouterKey From 225bea3ab5043c2997f978a78861be96f7a488bc Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Fri, 9 Aug 2019 19:48:40 +0200 Subject: [PATCH 12/18] Apply suggestions from code review Co-Authored-By: frog power 4000 --- types/module/module.go | 14 +++++++------- x/auth/module.go | 2 +- x/auth/simulation/decoder/store.go | 1 - 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/types/module/module.go b/types/module/module.go index 8cbd8c47cb6d..187864777ee7 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -86,7 +86,7 @@ func (bm BasicManager) DefaultGenesis() map[string]json.RawMessage { return genesis } -// ValidateGenesis provided default genesis information for all modules +// ValidateGenesis provides default genesis information for all modules func (bm BasicManager) ValidateGenesis(genesis map[string]json.RawMessage) error { for _, b := range bm { if err := b.ValidateGenesis(genesis[b.Name()]); err != nil { @@ -170,7 +170,7 @@ func NewGenesisOnlyAppModule(amg AppModuleGenesis) AppModule { } } -// RegisterInvariants register no invariants +// RegisterInvariants is a placeholder function register no invariants func (GenesisOnlyAppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterStoreDecoder empty store decoder registry @@ -179,19 +179,19 @@ func (GenesisOnlyAppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} // Route empty module message route func (GenesisOnlyAppModule) Route() string { return "" } -// NewHandler empty module handler +// NewHandler returns an empty module handler func (GenesisOnlyAppModule) NewHandler() sdk.Handler { return nil } -// QuerierRoute empty module querier route +// QuerierRoute returns an empty module querier route func (GenesisOnlyAppModule) QuerierRoute() string { return "" } -// NewQuerierHandler empty module querier +// NewQuerierHandler returns an empty module querier func (gam GenesisOnlyAppModule) NewQuerierHandler() sdk.Querier { return nil } -// BeginBlock empty module begin-block +// BeginBlock returns an empty module begin-block func (gam GenesisOnlyAppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {} -// EndBlock empty module end-block +// EndBlock returns an empty module end-block func (GenesisOnlyAppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } diff --git a/x/auth/module.go b/x/auth/module.go index 2c2c79a4c692..d0c99dea99fc 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -62,7 +62,7 @@ func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { return cli.GetTxCmd(cdc) } -// GetQueryCmd returns no root query command for the auth module. +// GetQueryCmd returns the root query command for the auth module. func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(cdc) } diff --git a/x/auth/simulation/decoder/store.go b/x/auth/simulation/decoder/store.go index 9258795183f0..75b539895e1e 100644 --- a/x/auth/simulation/decoder/store.go +++ b/x/auth/simulation/decoder/store.go @@ -7,7 +7,6 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/auth/types" ) From 9f3525bfaa7866e13edd899b8be17f23ad15e337 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 9 Aug 2019 20:11:06 +0200 Subject: [PATCH 13/18] address @rigelrozanski comments --- simapp/sim_test.go | 10 ++- simapp/utils.go | 17 ++--- simapp/utils_test.go | 71 ++++++++++--------- store/types/utils.go | 8 ++- types/store.go | 2 +- x/auth/simulation/decoder/store_test.go | 4 +- .../simulation/decoder/store_test.go | 4 +- x/gov/simulation/decoder/store.go | 1 - x/gov/simulation/decoder/store_test.go | 4 +- x/mint/simulation/decoder/store.go | 1 - x/mint/simulation/decoder/store_test.go | 4 +- x/slashing/simulation/decoder/store.go | 1 - x/slashing/simulation/decoder/store_test.go | 3 +- x/staking/simulation/decoder/store.go | 1 - x/staking/simulation/decoder/store_test.go | 3 +- x/supply/simulation/decoder/store.go | 1 - x/supply/simulation/decoder/store_test.go | 4 +- 17 files changed, 62 insertions(+), 77 deletions(-) diff --git a/simapp/sim_test.go b/simapp/sim_test.go index b7069359eee7..8141e0da2649 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -597,11 +597,15 @@ func TestAppImportExport(t *testing.T) { storeKeyA := storeKeysPrefix.A storeKeyB := storeKeysPrefix.B prefixes := storeKeysPrefix.Prefixes + storeA := ctxA.KVStore(storeKeyA) storeB := ctxB.KVStore(storeKeyB) - failedKVs := sdk.DiffKVStores(storeA, storeB, prefixes) - fmt.Printf("Compared %d key/value pairs between %s and %s\n", len(failedKVs)/2, storeKeyA, storeKeyB) - require.Len(t, failedKVs, 0, GetSimulationLog(storeKeyA.Name(), app.sm.StoreDecoders, app.cdc, failedKVs)) + + failedKVAs, failedKVBs := sdk.DiffKVStores(storeA, storeB, prefixes) + require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare") + + fmt.Printf("Compared %d key/value pairs between %s and %s\n", len(failedKVAs), storeKeyA, storeKeyB) + require.Len(t, failedKVAs, 0, GetSimulationLog(storeKeyA.Name(), app.sm.StoreDecoders, app.cdc, failedKVAs, failedKVBs)) } } diff --git a/simapp/utils.go b/simapp/utils.go index 3ad2e23107e7..5bc5073e8a07 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -497,26 +497,19 @@ func GenStakingGenesisState( // 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, kvs []cmn.KVPair) (log string) { - if len(kvs)%2 != 0 { - panic("KVPairs are not multiple of 2. There should be one for each app store") - } - - var kvA, kvB cmn.KVPair - for i := 0; i < len(kvs); i += 2 { - kvA = kvs[i] - kvB = kvs[i+1] +func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, cdc *codec.Codec, kvAs, kvBs []cmn.KVPair) (log string) { + for i := 0; i < len(kvAs); i++ { - if len(kvA.Value) == 0 && len(kvB.Value) == 0 { + if len(kvAs[i].Value) == 0 && len(kvBs[i].Value) == 0 { // skip if the value doesn't have any bytes continue } decoder, ok := sdr[storeName] if ok { - log += decoder(cdc, kvA, kvB) + log += decoder(cdc, kvAs[i], kvBs[i]) } else { - log += fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvA.Key, kvA.Value, kvB.Key, kvB.Value) + log += fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvAs[i].Key, kvAs[i].Value, kvBs[i].Key, kvBs[i].Value) } } diff --git a/simapp/utils_test.go b/simapp/utils_test.go index 7a63d560250d..939cea294c6e 100644 --- a/simapp/utils_test.go +++ b/simapp/utils_test.go @@ -48,44 +48,47 @@ func TestGetSimulationLog(t *testing.T) { store string kvPairs []cmn.KVPair }{ - {auth.StoreKey, []cmn.KVPair{ - {Key: auth.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(auth.BaseAccount{})}, - {Key: auth.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(auth.BaseAccount{})}, - }}, - {mint.StoreKey, []cmn.KVPair{ - {Key: mint.MinterKey, Value: cdc.MustMarshalBinaryLengthPrefixed(mint.Minter{})}, - {Key: mint.MinterKey, Value: cdc.MustMarshalBinaryLengthPrefixed(mint.Minter{})}, - }}, - {staking.StoreKey, []cmn.KVPair{ - {Key: staking.LastValidatorPowerKey, Value: valAddr1.Bytes()}, - {Key: staking.LastValidatorPowerKey, Value: valAddr1.Bytes()}, - }}, - {gov.StoreKey, []cmn.KVPair{ - {Key: gov.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(gov.Vote{})}, - {Key: gov.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(gov.Vote{})}, - }}, - {distribution.StoreKey, []cmn.KVPair{ - {Key: distr.ProposerKey, Value: consAddr1.Bytes()}, - {Key: distr.ProposerKey, Value: consAddr1.Bytes()}, - }}, - {slashing.StoreKey, []cmn.KVPair{ - {Key: slashing.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryLengthPrefixed(true)}, - {Key: slashing.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryLengthPrefixed(true)}, - }}, - {supply.StoreKey, []cmn.KVPair{ - {Key: supply.SupplyKey, Value: cdc.MustMarshalBinaryLengthPrefixed(supply.NewSupply(sdk.Coins{}))}, - {Key: supply.SupplyKey, Value: cdc.MustMarshalBinaryLengthPrefixed(supply.NewSupply(sdk.Coins{}))}, - }}, - {"Empty", []cmn.KVPair{{}, {}}}, - {"OtherStore", []cmn.KVPair{ - {Key: []byte("key"), Value: []byte("value")}, - {Key: []byte("key"), Value: []byte("other_value")}, - }}, + { + auth.StoreKey, + []cmn.KVPair{{Key: auth.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(auth.BaseAccount{})}}, + }, + { + mint.StoreKey, + []cmn.KVPair{{Key: mint.MinterKey, Value: cdc.MustMarshalBinaryLengthPrefixed(mint.Minter{})}}, + }, + { + staking.StoreKey, + []cmn.KVPair{{Key: staking.LastValidatorPowerKey, Value: valAddr1.Bytes()}}, + }, + { + gov.StoreKey, + []cmn.KVPair{{Key: gov.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(gov.Vote{})}}, + }, + { + distribution.StoreKey, + []cmn.KVPair{{Key: distr.ProposerKey, Value: consAddr1.Bytes()}}, + }, + { + slashing.StoreKey, + []cmn.KVPair{{Key: slashing.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryLengthPrefixed(true)}}, + }, + { + supply.StoreKey, + []cmn.KVPair{{Key: supply.SupplyKey, Value: cdc.MustMarshalBinaryLengthPrefixed(supply.NewSupply(sdk.Coins{}))}}, + }, + { + "Empty", + []cmn.KVPair{{}}, + }, + { + "OtherStore", + []cmn.KVPair{{Key: []byte("key"), Value: []byte("value")}}, + }, } for _, tt := range tests { t.Run(tt.store, func(t *testing.T) { - require.NotPanics(t, func() { GetSimulationLog(tt.store, make(sdk.StoreDecoderRegistry), cdc, tt.kvPairs) }, tt.store) + require.NotPanics(t, func() { GetSimulationLog(tt.store, make(sdk.StoreDecoderRegistry), cdc, tt.kvPairs, tt.kvPairs) }, tt.store) }) } } diff --git a/store/types/utils.go b/store/types/utils.go index 39bc7da66500..9efcd02f1646 100644 --- a/store/types/utils.go +++ b/store/types/utils.go @@ -18,7 +18,7 @@ func KVStoreReversePrefixIterator(kvs KVStore, prefix []byte) Iterator { // DiffKVStores compares two KVstores and returns all the key/value pairs // that differ from one another. It also skips value comparison for a set of provided prefixes -func DiffKVStores(a KVStore, b KVStore, prefixesToSkip [][]byte) (kvs []cmn.KVPair) { +func DiffKVStores(a KVStore, b KVStore, prefixesToSkip [][]byte) (kvAs, kvBs []cmn.KVPair) { iterA := a.Iterator(nil, nil) iterB := b.Iterator(nil, nil) @@ -36,7 +36,8 @@ func DiffKVStores(a KVStore, b KVStore, prefixesToSkip [][]byte) (kvs []cmn.KVPa iterB.Next() } if !bytes.Equal(kvA.Key, kvB.Key) { - kvs = append(kvs, []cmn.KVPair{kvA, kvB}...) + kvAs = append(kvAs, kvA) + kvBs = append(kvBs, kvB) } compareValue := true for _, prefix := range prefixesToSkip { @@ -46,7 +47,8 @@ func DiffKVStores(a KVStore, b KVStore, prefixesToSkip [][]byte) (kvs []cmn.KVPa } } if compareValue && !bytes.Equal(kvA.Value, kvB.Value) { - kvs = append(kvs, []cmn.KVPair{kvA, kvB}...) + kvAs = append(kvAs, kvA) + kvBs = append(kvBs, kvB) } } return diff --git a/types/store.go b/types/store.go index 7bb4a41d6f79..a8772117e0bb 100644 --- a/types/store.go +++ b/types/store.go @@ -41,7 +41,7 @@ func KVStoreReversePrefixIterator(kvs KVStore, prefix []byte) Iterator { // DiffKVStores compares two KVstores and returns all the key/value pairs // that differ from one another. It also skips value comparison for a set of provided prefixes -func DiffKVStores(a KVStore, b KVStore, prefixesToSkip [][]byte) []cmn.KVPair { +func DiffKVStores(a KVStore, b KVStore, prefixesToSkip [][]byte) (kvAs, kvBs []cmn.KVPair) { return types.DiffKVStores(a, b, prefixesToSkip) } diff --git a/x/auth/simulation/decoder/store_test.go b/x/auth/simulation/decoder/store_test.go index c518ba7a6587..30dda4256f84 100644 --- a/x/auth/simulation/decoder/store_test.go +++ b/x/auth/simulation/decoder/store_test.go @@ -10,10 +10,8 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - - "github.com/cosmos/cosmos-sdk/x/auth/types" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/types" ) var ( diff --git a/x/distribution/simulation/decoder/store_test.go b/x/distribution/simulation/decoder/store_test.go index f7d464f89682..654be872352f 100644 --- a/x/distribution/simulation/decoder/store_test.go +++ b/x/distribution/simulation/decoder/store_test.go @@ -10,11 +10,9 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/distribution/keeper" "github.com/cosmos/cosmos-sdk/x/distribution/types" - - sdk "github.com/cosmos/cosmos-sdk/types" ) var ( diff --git a/x/gov/simulation/decoder/store.go b/x/gov/simulation/decoder/store.go index 3aabca188ea1..d2096d5228d1 100644 --- a/x/gov/simulation/decoder/store.go +++ b/x/gov/simulation/decoder/store.go @@ -8,7 +8,6 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/gov/types" ) diff --git a/x/gov/simulation/decoder/store_test.go b/x/gov/simulation/decoder/store_test.go index fb8cc907d8ed..b5002d44a3ef 100644 --- a/x/gov/simulation/decoder/store_test.go +++ b/x/gov/simulation/decoder/store_test.go @@ -12,10 +12,8 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - - "github.com/cosmos/cosmos-sdk/x/gov/types" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/gov/types" ) var ( diff --git a/x/mint/simulation/decoder/store.go b/x/mint/simulation/decoder/store.go index 49d0ff3382b1..f2fce0c6d2d2 100644 --- a/x/mint/simulation/decoder/store.go +++ b/x/mint/simulation/decoder/store.go @@ -7,7 +7,6 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/mint/internal/types" ) diff --git a/x/mint/simulation/decoder/store_test.go b/x/mint/simulation/decoder/store_test.go index 8d3f0c413b8c..7ce260fbef36 100644 --- a/x/mint/simulation/decoder/store_test.go +++ b/x/mint/simulation/decoder/store_test.go @@ -9,10 +9,8 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - - "github.com/cosmos/cosmos-sdk/x/mint/internal/types" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/mint/internal/types" ) func makeTestCodec() (cdc *codec.Codec) { diff --git a/x/slashing/simulation/decoder/store.go b/x/slashing/simulation/decoder/store.go index 7d79115863c2..342f42aee612 100644 --- a/x/slashing/simulation/decoder/store.go +++ b/x/slashing/simulation/decoder/store.go @@ -8,7 +8,6 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/internal/types" ) diff --git a/x/slashing/simulation/decoder/store_test.go b/x/slashing/simulation/decoder/store_test.go index 06039eb5bae1..95e540c3240f 100644 --- a/x/slashing/simulation/decoder/store_test.go +++ b/x/slashing/simulation/decoder/store_test.go @@ -11,9 +11,8 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/slashing/internal/types" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/slashing/internal/types" ) var ( diff --git a/x/staking/simulation/decoder/store.go b/x/staking/simulation/decoder/store.go index 8215265659c5..28d037c287fd 100644 --- a/x/staking/simulation/decoder/store.go +++ b/x/staking/simulation/decoder/store.go @@ -7,7 +7,6 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/staking/types" ) diff --git a/x/staking/simulation/decoder/store_test.go b/x/staking/simulation/decoder/store_test.go index a5ff718bb028..3849fd4cd94f 100644 --- a/x/staking/simulation/decoder/store_test.go +++ b/x/staking/simulation/decoder/store_test.go @@ -11,9 +11,8 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/staking/types" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/staking/types" ) var ( diff --git a/x/supply/simulation/decoder/store.go b/x/supply/simulation/decoder/store.go index da9ce0761672..2df8c27b0eba 100644 --- a/x/supply/simulation/decoder/store.go +++ b/x/supply/simulation/decoder/store.go @@ -7,7 +7,6 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/supply/internal/keeper" "github.com/cosmos/cosmos-sdk/x/supply/internal/types" ) diff --git a/x/supply/simulation/decoder/store_test.go b/x/supply/simulation/decoder/store_test.go index 38f30ea0971b..f6df6c7987d7 100644 --- a/x/supply/simulation/decoder/store_test.go +++ b/x/supply/simulation/decoder/store_test.go @@ -9,11 +9,9 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" "github.com/cosmos/cosmos-sdk/codec" - + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/supply/internal/keeper" "github.com/cosmos/cosmos-sdk/x/supply/internal/types" - - sdk "github.com/cosmos/cosmos-sdk/types" ) func makeTestCodec() (cdc *codec.Codec) { From c13ecb6636372fdd9b4e5ad13a54db49ae521988 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 9 Aug 2019 20:17:30 +0200 Subject: [PATCH 14/18] changelog --- .pending/improvements/simulation/_4847-simulation-man | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .pending/improvements/simulation/_4847-simulation-man diff --git a/.pending/improvements/simulation/_4847-simulation-man b/.pending/improvements/simulation/_4847-simulation-man new file mode 100644 index 000000000000..ebb03f01963d --- /dev/null +++ b/.pending/improvements/simulation/_4847-simulation-man @@ -0,0 +1,3 @@ +#4847 `SimApp` and simulation refactors + - Implement `SimulationManager` for executing modules' simulation functionalities in a modularized way + - Add `DecodeStore` to the `SimulationManager` for decoding each module's types From 4959068b468d1f2e51c2cbdf8d49a6497902eb92 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Sat, 10 Aug 2019 08:05:52 +0200 Subject: [PATCH 15/18] Apply suggestions from code review Co-Authored-By: colin axner --- types/module/module.go | 4 ++-- x/distribution/module.go | 2 +- x/genaccounts/module.go | 2 +- x/genutil/module.go | 2 +- x/gov/module.go | 2 +- x/mint/module.go | 4 ++-- x/params/module.go | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/types/module/module.go b/types/module/module.go index 187864777ee7..f45f3176580f 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -77,7 +77,7 @@ func (bm BasicManager) RegisterCodec(cdc *codec.Codec) { } } -// DefaultGenesis provided default genesis information for all modules +// DefaultGenesis provides default genesis information for all modules func (bm BasicManager) DefaultGenesis() map[string]json.RawMessage { genesis := make(map[string]json.RawMessage) for _, b := range bm { @@ -86,7 +86,7 @@ func (bm BasicManager) DefaultGenesis() map[string]json.RawMessage { return genesis } -// ValidateGenesis provides default genesis information for all modules +// ValidateGenesis performs genesis state validation for all modules func (bm BasicManager) ValidateGenesis(genesis map[string]json.RawMessage) error { for _, b := range bm { if err := b.ValidateGenesis(genesis[b.Name()]); err != nil { diff --git a/x/distribution/module.go b/x/distribution/module.go index bf838fd0d938..84da785ee55a 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -63,7 +63,7 @@ func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { return cli.GetTxCmd(StoreKey, cdc) } -// GetQueryCmd returns no root query command for the distribution module. +// GetQueryCmd returns the root query command for the distribution module. func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(StoreKey, cdc) } diff --git a/x/genaccounts/module.go b/x/genaccounts/module.go index 753687ae4d1b..9a1f625aa96b 100644 --- a/x/genaccounts/module.go +++ b/x/genaccounts/module.go @@ -30,7 +30,7 @@ func (AppModuleBasic) Name() string { return ModuleName } -// RegisterCodec registers the genesis accounts module's types for the given codec. +// RegisterCodec performs a no-op. func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {} // DefaultGenesis returns default genesis state as raw bytes for the genesis accounts diff --git a/x/genutil/module.go b/x/genutil/module.go index bf1b0ceb49b8..63e0a93a4acc 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -51,7 +51,7 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { // RegisterRESTRoutes registers the REST routes for the genutil module. func (AppModuleBasic) RegisterRESTRoutes(_ context.CLIContext, _ *mux.Router) {} -// GetTxCmd returns the root tx command for the genutil module. +// GetTxCmd returns no root tx command for the genutil module. func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil } // GetQueryCmd returns no root query command for the genutil module. diff --git a/x/gov/module.go b/x/gov/module.go index c7c1631fd8f7..7bfef32a69e5 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -86,7 +86,7 @@ func (a AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { return cli.GetTxCmd(StoreKey, cdc, proposalCLIHandlers) } -// GetQueryCmd returns no root query command for the gov module. +// GetQueryCmd returns the root query command for the gov module. func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(StoreKey, cdc) } diff --git a/x/mint/module.go b/x/mint/module.go index 695ca649a1ab..22b9f459a67c 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -57,10 +57,10 @@ func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router rest.RegisterRoutes(ctx, rtr) } -// GetTxCmd returns the root tx command for the mint module. +// GetTxCmd returns no root tx command for the mint module. func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil } -// GetQueryCmd returns no root query command for the mint module. +// GetQueryCmd returns the root query command for the mint module. func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(cdc) } diff --git a/x/params/module.go b/x/params/module.go index de643c73c03b..38b93bd5f00c 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -39,7 +39,7 @@ func (AppModuleBasic) ValidateGenesis(_ json.RawMessage) error { return nil } // RegisterRESTRoutes registers the REST routes for the params module. func (AppModuleBasic) RegisterRESTRoutes(_ context.CLIContext, _ *mux.Router) {} -// GetTxCmd returns the root tx command for the params module. +// GetTxCmd returns no root tx command for the params module. func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil } // GetQueryCmd returns no root query command for the params module. From 258e3e6b421f325e4dc5d8c1d2c9f068bea1cb9c Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 12 Aug 2019 17:15:04 +0200 Subject: [PATCH 16/18] restructure modules simulation pkgs --- simapp/sim_test.go | 46 +++++++++---------- x/auth/module.go | 4 +- .../{decoder/store.go => decoder.go} | 2 +- .../store_test.go => decoder_test.go} | 2 +- x/auth/simulation/{ => operations}/fake.go | 2 +- .../operations/msgs.go} | 16 +++---- x/distribution/module.go | 4 +- .../{decoder/store.go => decoder.go} | 2 +- .../store_test.go => decoder_test.go} | 2 +- .../simulation/{ => operations}/msgs.go | 6 +-- x/gov/module.go | 4 +- .../{decoder/store.go => decoder.go} | 2 +- .../store_test.go => decoder_test.go} | 2 +- x/gov/simulation/{ => operations}/msgs.go | 2 +- x/mint/module.go | 4 +- .../{decoder/store.go => decoder.go} | 2 +- .../store_test.go => decoder_test.go} | 2 +- x/params/simulation/{ => operations}/msgs.go | 2 +- x/slashing/module.go | 4 +- .../{decoder/store.go => decoder.go} | 2 +- .../store_test.go => decoder_test.go} | 2 +- .../simulation/{ => operations}/msgs.go | 2 +- x/staking/module.go | 4 +- .../{decoder/store.go => decoder.go} | 2 +- .../store_test.go => decoder_test.go} | 2 +- x/staking/simulation/{ => operations}/msgs.go | 2 +- x/supply/module.go | 4 +- .../{decoder/store.go => decoder.go} | 2 +- .../store_test.go => decoder_test.go} | 2 +- 29 files changed, 67 insertions(+), 67 deletions(-) rename x/auth/simulation/{decoder/store.go => decoder.go} (98%) rename x/auth/simulation/{decoder/store_test.go => decoder_test.go} (98%) rename x/auth/simulation/{ => operations}/fake.go (99%) rename x/bank/{simulation.go => simulation/operations/msgs.go} (94%) rename x/distribution/simulation/{decoder/store.go => decoder.go} (99%) rename x/distribution/simulation/{decoder/store_test.go => decoder_test.go} (99%) rename x/distribution/simulation/{ => operations}/msgs.go (96%) rename x/gov/simulation/{decoder/store.go => decoder.go} (98%) rename x/gov/simulation/{decoder/store_test.go => decoder_test.go} (99%) rename x/gov/simulation/{ => operations}/msgs.go (99%) rename x/mint/simulation/{decoder/store.go => decoder.go} (97%) rename x/mint/simulation/{decoder/store_test.go => decoder_test.go} (98%) rename x/params/simulation/{ => operations}/msgs.go (99%) rename x/slashing/simulation/{decoder/store.go => decoder.go} (98%) rename x/slashing/simulation/{decoder/store_test.go => decoder_test.go} (99%) rename x/slashing/simulation/{ => operations}/msgs.go (98%) rename x/staking/simulation/{decoder/store.go => decoder.go} (99%) rename x/staking/simulation/{decoder/store_test.go => decoder_test.go} (99%) rename x/staking/simulation/{ => operations}/msgs.go (99%) rename x/supply/simulation/{decoder/store.go => decoder.go} (97%) rename x/supply/simulation/{decoder/store_test.go => decoder_test.go} (98%) diff --git a/simapp/sim_test.go b/simapp/sim_test.go index 8141e0da2649..8f4048edec20 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -20,20 +20,20 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - authsim "github.com/cosmos/cosmos-sdk/x/auth/simulation" - "github.com/cosmos/cosmos-sdk/x/bank" + authsimops "github.com/cosmos/cosmos-sdk/x/auth/simulation/operations" + banksimops "github.com/cosmos/cosmos-sdk/x/bank/simulation/operations" distr "github.com/cosmos/cosmos-sdk/x/distribution" - distrsim "github.com/cosmos/cosmos-sdk/x/distribution/simulation" + distrsimops "github.com/cosmos/cosmos-sdk/x/distribution/simulation/operations" "github.com/cosmos/cosmos-sdk/x/gov" - govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation" + govsimops "github.com/cosmos/cosmos-sdk/x/gov/simulation/operations" "github.com/cosmos/cosmos-sdk/x/mint" "github.com/cosmos/cosmos-sdk/x/params" - paramsim "github.com/cosmos/cosmos-sdk/x/params/simulation" + paramsimops "github.com/cosmos/cosmos-sdk/x/params/simulation/operations" "github.com/cosmos/cosmos-sdk/x/simulation" "github.com/cosmos/cosmos-sdk/x/slashing" - slashingsim "github.com/cosmos/cosmos-sdk/x/slashing/simulation" + slashingsimops "github.com/cosmos/cosmos-sdk/x/slashing/simulation/operations" "github.com/cosmos/cosmos-sdk/x/staking" - stakingsim "github.com/cosmos/cosmos-sdk/x/staking/simulation" + stakingsimops "github.com/cosmos/cosmos-sdk/x/staking/simulation/operations" "github.com/cosmos/cosmos-sdk/x/supply" ) @@ -184,7 +184,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - authsim.SimulateDeductFee(app.accountKeeper, app.supplyKeeper), + authsimops.SimulateDeductFee(app.accountKeeper, app.supplyKeeper), }, { func(_ *rand.Rand) int { @@ -195,7 +195,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - bank.SimulateMsgSend(app.accountKeeper, app.bankKeeper), + banksimops.SimulateMsgSend(app.accountKeeper, app.bankKeeper), }, { func(_ *rand.Rand) int { @@ -206,7 +206,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - bank.SimulateSingleInputMsgMultiSend(app.accountKeeper, app.bankKeeper), + banksimops.SimulateSingleInputMsgMultiSend(app.accountKeeper, app.bankKeeper), }, { func(_ *rand.Rand) int { @@ -217,7 +217,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - distrsim.SimulateMsgSetWithdrawAddress(app.distrKeeper), + distrsimops.SimulateMsgSetWithdrawAddress(app.distrKeeper), }, { func(_ *rand.Rand) int { @@ -228,7 +228,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - distrsim.SimulateMsgWithdrawDelegatorReward(app.distrKeeper), + distrsimops.SimulateMsgWithdrawDelegatorReward(app.distrKeeper), }, { func(_ *rand.Rand) int { @@ -239,7 +239,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - distrsim.SimulateMsgWithdrawValidatorCommission(app.distrKeeper), + distrsimops.SimulateMsgWithdrawValidatorCommission(app.distrKeeper), }, { func(_ *rand.Rand) int { @@ -250,7 +250,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - govsim.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, govsim.SimulateTextProposalContent), + govsimops.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, govsimops.SimulateTextProposalContent), }, { func(_ *rand.Rand) int { @@ -261,7 +261,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - govsim.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, distrsim.SimulateCommunityPoolSpendProposalContent(app.distrKeeper)), + govsimops.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, distrsimops.SimulateCommunityPoolSpendProposalContent(app.distrKeeper)), }, { func(_ *rand.Rand) int { @@ -272,7 +272,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - govsim.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, paramsim.SimulateParamChangeProposalContent), + govsimops.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, paramsimops.SimulateParamChangeProposalContent), }, { func(_ *rand.Rand) int { @@ -283,7 +283,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - govsim.SimulateMsgDeposit(app.govKeeper), + govsimops.SimulateMsgDeposit(app.govKeeper), }, { func(_ *rand.Rand) int { @@ -294,7 +294,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - stakingsim.SimulateMsgCreateValidator(app.accountKeeper, app.stakingKeeper), + stakingsimops.SimulateMsgCreateValidator(app.accountKeeper, app.stakingKeeper), }, { func(_ *rand.Rand) int { @@ -305,7 +305,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - stakingsim.SimulateMsgEditValidator(app.stakingKeeper), + stakingsimops.SimulateMsgEditValidator(app.stakingKeeper), }, { func(_ *rand.Rand) int { @@ -316,7 +316,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - stakingsim.SimulateMsgDelegate(app.accountKeeper, app.stakingKeeper), + stakingsimops.SimulateMsgDelegate(app.accountKeeper, app.stakingKeeper), }, { func(_ *rand.Rand) int { @@ -327,7 +327,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - stakingsim.SimulateMsgUndelegate(app.accountKeeper, app.stakingKeeper), + stakingsimops.SimulateMsgUndelegate(app.accountKeeper, app.stakingKeeper), }, { func(_ *rand.Rand) int { @@ -338,7 +338,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - stakingsim.SimulateMsgBeginRedelegate(app.accountKeeper, app.stakingKeeper), + stakingsimops.SimulateMsgBeginRedelegate(app.accountKeeper, app.stakingKeeper), }, { func(_ *rand.Rand) int { @@ -349,7 +349,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - slashingsim.SimulateMsgUnjail(app.slashingKeeper), + slashingsimops.SimulateMsgUnjail(app.slashingKeeper), }, } } diff --git a/x/auth/module.go b/x/auth/module.go index d0c99dea99fc..cb45b4913dc0 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -13,7 +13,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/auth/client/rest" - "github.com/cosmos/cosmos-sdk/x/auth/simulation/decoder" + "github.com/cosmos/cosmos-sdk/x/auth/simulation" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -74,7 +74,7 @@ type AppModuleSimulation struct{} // RegisterStoreDecoder registers a decoder for auth module's types func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore + sdr[StoreKey] = simulation.DecodeStore } //____________________________________________________________________________ diff --git a/x/auth/simulation/decoder/store.go b/x/auth/simulation/decoder.go similarity index 98% rename from x/auth/simulation/decoder/store.go rename to x/auth/simulation/decoder.go index 75b539895e1e..933421630bb9 100644 --- a/x/auth/simulation/decoder/store.go +++ b/x/auth/simulation/decoder.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "bytes" diff --git a/x/auth/simulation/decoder/store_test.go b/x/auth/simulation/decoder_test.go similarity index 98% rename from x/auth/simulation/decoder/store_test.go rename to x/auth/simulation/decoder_test.go index 30dda4256f84..7b2cbe710a6a 100644 --- a/x/auth/simulation/decoder/store_test.go +++ b/x/auth/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "fmt" diff --git a/x/auth/simulation/fake.go b/x/auth/simulation/operations/fake.go similarity index 99% rename from x/auth/simulation/fake.go rename to x/auth/simulation/operations/fake.go index 18e528f7bcb0..9a516785cb5d 100644 --- a/x/auth/simulation/fake.go +++ b/x/auth/simulation/operations/fake.go @@ -1,4 +1,4 @@ -package simulation +package operations import ( "errors" diff --git a/x/bank/simulation.go b/x/bank/simulation/operations/msgs.go similarity index 94% rename from x/bank/simulation.go rename to x/bank/simulation/operations/msgs.go index 85532502eaab..32ff0ad6eb01 100644 --- a/x/bank/simulation.go +++ b/x/bank/simulation/operations/msgs.go @@ -1,4 +1,4 @@ -package bank +package operations import ( "fmt" @@ -8,16 +8,16 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/bank/internal/keeper" + "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/bank/internal/types" "github.com/cosmos/cosmos-sdk/x/mock" "github.com/cosmos/cosmos-sdk/x/simulation" ) -// SendTx tests and runs a single msg send where both +// SimulateMsgSend tests and runs a single msg send where both // accounts already exist. -func SimulateMsgSend(mapper types.AccountKeeper, bk keeper.Keeper) simulation.Operation { - handler := NewHandler(bk) +func SimulateMsgSend(mapper types.AccountKeeper, bk bank.Keeper) simulation.Operation { + handler := bank.NewHandler(bk) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) ( opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { @@ -108,10 +108,10 @@ func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper types.AccountKeeper, msg return nil } -// SingleInputSendMsg tests and runs a single msg multisend, with one input and one output, where both +// SimulateSingleInputMsgMultiSend tests and runs a single msg multisend, with one input and one output, where both // accounts already exist. -func SimulateSingleInputMsgMultiSend(mapper types.AccountKeeper, bk keeper.Keeper) simulation.Operation { - handler := NewHandler(bk) +func SimulateSingleInputMsgMultiSend(mapper types.AccountKeeper, bk bank.Keeper) simulation.Operation { + handler := bank.NewHandler(bk) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) ( opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { diff --git a/x/distribution/module.go b/x/distribution/module.go index 84da785ee55a..c156e67aad49 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -14,7 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/distribution/client/cli" "github.com/cosmos/cosmos-sdk/x/distribution/client/rest" - "github.com/cosmos/cosmos-sdk/x/distribution/simulation/decoder" + "github.com/cosmos/cosmos-sdk/x/distribution/simulation" "github.com/cosmos/cosmos-sdk/x/distribution/types" ) @@ -75,7 +75,7 @@ type AppModuleSimulation struct{} // RegisterStoreDecoder registers a decoder for distribution module's types func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore + sdr[StoreKey] = simulation.DecodeStore } //____________________________________________________________________________ diff --git a/x/distribution/simulation/decoder/store.go b/x/distribution/simulation/decoder.go similarity index 99% rename from x/distribution/simulation/decoder/store.go rename to x/distribution/simulation/decoder.go index 6e4bae40078e..c1b8dce42821 100644 --- a/x/distribution/simulation/decoder/store.go +++ b/x/distribution/simulation/decoder.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "bytes" diff --git a/x/distribution/simulation/decoder/store_test.go b/x/distribution/simulation/decoder_test.go similarity index 99% rename from x/distribution/simulation/decoder/store_test.go rename to x/distribution/simulation/decoder_test.go index 654be872352f..25d846e252d9 100644 --- a/x/distribution/simulation/decoder/store_test.go +++ b/x/distribution/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "fmt" diff --git a/x/distribution/simulation/msgs.go b/x/distribution/simulation/operations/msgs.go similarity index 96% rename from x/distribution/simulation/msgs.go rename to x/distribution/simulation/operations/msgs.go index 30784ca999a8..6d30aac278c3 100644 --- a/x/distribution/simulation/msgs.go +++ b/x/distribution/simulation/operations/msgs.go @@ -1,4 +1,4 @@ -package simulation +package operations import ( "fmt" @@ -8,7 +8,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/distribution" "github.com/cosmos/cosmos-sdk/x/gov" - govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation" + govsimops "github.com/cosmos/cosmos-sdk/x/gov/simulation/operations" "github.com/cosmos/cosmos-sdk/x/simulation" ) @@ -87,7 +87,7 @@ func SimulateMsgWithdrawValidatorCommission(k distribution.Keeper) simulation.Op } // SimulateCommunityPoolSpendProposalContent generates random community-pool-spend proposal content -func SimulateCommunityPoolSpendProposalContent(k distribution.Keeper) govsim.ContentSimulator { +func SimulateCommunityPoolSpendProposalContent(k distribution.Keeper) govsimops.ContentSimulator { return func(r *rand.Rand, _ *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) gov.Content { recipientAcc := simulation.RandomAcc(r, accs) coins := sdk.Coins{} diff --git a/x/gov/module.go b/x/gov/module.go index 7bfef32a69e5..81100e0ffae9 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -17,7 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/gov/client" "github.com/cosmos/cosmos-sdk/x/gov/client/cli" "github.com/cosmos/cosmos-sdk/x/gov/client/rest" - "github.com/cosmos/cosmos-sdk/x/gov/simulation/decoder" + "github.com/cosmos/cosmos-sdk/x/gov/simulation" "github.com/cosmos/cosmos-sdk/x/gov/types" ) @@ -98,7 +98,7 @@ type AppModuleSimulation struct{} // RegisterStoreDecoder performs a no-op. func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore + sdr[StoreKey] = simulation.DecodeStore } //____________________________________________________________________________ diff --git a/x/gov/simulation/decoder/store.go b/x/gov/simulation/decoder.go similarity index 98% rename from x/gov/simulation/decoder/store.go rename to x/gov/simulation/decoder.go index d2096d5228d1..475721e8769e 100644 --- a/x/gov/simulation/decoder/store.go +++ b/x/gov/simulation/decoder.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "bytes" diff --git a/x/gov/simulation/decoder/store_test.go b/x/gov/simulation/decoder_test.go similarity index 99% rename from x/gov/simulation/decoder/store_test.go rename to x/gov/simulation/decoder_test.go index b5002d44a3ef..05d980023c2e 100644 --- a/x/gov/simulation/decoder/store_test.go +++ b/x/gov/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "encoding/binary" diff --git a/x/gov/simulation/msgs.go b/x/gov/simulation/operations/msgs.go similarity index 99% rename from x/gov/simulation/msgs.go rename to x/gov/simulation/operations/msgs.go index 0477e8273b88..f7dcba519de0 100644 --- a/x/gov/simulation/msgs.go +++ b/x/gov/simulation/operations/msgs.go @@ -1,4 +1,4 @@ -package simulation +package operations import ( "fmt" diff --git a/x/mint/module.go b/x/mint/module.go index 22b9f459a67c..33b93ab54e81 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -14,7 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/mint/client/cli" "github.com/cosmos/cosmos-sdk/x/mint/client/rest" - "github.com/cosmos/cosmos-sdk/x/mint/simulation/decoder" + "github.com/cosmos/cosmos-sdk/x/mint/simulation" ) var ( @@ -72,7 +72,7 @@ type AppModuleSimulation struct{} // RegisterStoreDecoder registers a decoder for mint module's types. func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore + sdr[StoreKey] = simulation.DecodeStore } //____________________________________________________________________________ diff --git a/x/mint/simulation/decoder/store.go b/x/mint/simulation/decoder.go similarity index 97% rename from x/mint/simulation/decoder/store.go rename to x/mint/simulation/decoder.go index f2fce0c6d2d2..0f1ab1960989 100644 --- a/x/mint/simulation/decoder/store.go +++ b/x/mint/simulation/decoder.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "bytes" diff --git a/x/mint/simulation/decoder/store_test.go b/x/mint/simulation/decoder_test.go similarity index 98% rename from x/mint/simulation/decoder/store_test.go rename to x/mint/simulation/decoder_test.go index 7ce260fbef36..b6501baa7f06 100644 --- a/x/mint/simulation/decoder/store_test.go +++ b/x/mint/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "fmt" diff --git a/x/params/simulation/msgs.go b/x/params/simulation/operations/msgs.go similarity index 99% rename from x/params/simulation/msgs.go rename to x/params/simulation/operations/msgs.go index db7669ae09ed..b6b869dc6f23 100644 --- a/x/params/simulation/msgs.go +++ b/x/params/simulation/operations/msgs.go @@ -1,4 +1,4 @@ -package simulation +package operations import ( "encoding/json" diff --git a/x/slashing/module.go b/x/slashing/module.go index 7cfc052d95de..986915a11531 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -15,7 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" "github.com/cosmos/cosmos-sdk/x/slashing/client/rest" "github.com/cosmos/cosmos-sdk/x/slashing/internal/types" - "github.com/cosmos/cosmos-sdk/x/slashing/simulation/decoder" + "github.com/cosmos/cosmos-sdk/x/slashing/simulation" ) var ( @@ -77,7 +77,7 @@ type AppModuleSimulation struct{} // RegisterStoreDecoder registers a decoder for slashing module's types func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore + sdr[StoreKey] = simulation.DecodeStore } //____________________________________________________________________________ diff --git a/x/slashing/simulation/decoder/store.go b/x/slashing/simulation/decoder.go similarity index 98% rename from x/slashing/simulation/decoder/store.go rename to x/slashing/simulation/decoder.go index 342f42aee612..99af7c0b722b 100644 --- a/x/slashing/simulation/decoder/store.go +++ b/x/slashing/simulation/decoder.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "bytes" diff --git a/x/slashing/simulation/decoder/store_test.go b/x/slashing/simulation/decoder_test.go similarity index 99% rename from x/slashing/simulation/decoder/store_test.go rename to x/slashing/simulation/decoder_test.go index 95e540c3240f..1c8f8f93c542 100644 --- a/x/slashing/simulation/decoder/store_test.go +++ b/x/slashing/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "fmt" diff --git a/x/slashing/simulation/msgs.go b/x/slashing/simulation/operations/msgs.go similarity index 98% rename from x/slashing/simulation/msgs.go rename to x/slashing/simulation/operations/msgs.go index 01acf4a81980..5772101b16b2 100644 --- a/x/slashing/simulation/msgs.go +++ b/x/slashing/simulation/operations/msgs.go @@ -1,4 +1,4 @@ -package simulation +package operations import ( "fmt" diff --git a/x/staking/module.go b/x/staking/module.go index 3ce3fec4473c..4bfd0c70aab5 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -18,7 +18,7 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/staking/client/cli" "github.com/cosmos/cosmos-sdk/x/staking/client/rest" - "github.com/cosmos/cosmos-sdk/x/staking/simulation/decoder" + "github.com/cosmos/cosmos-sdk/x/staking/simulation" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -102,7 +102,7 @@ type AppModuleSimulation struct{} // RegisterStoreDecoder registers a decoder for staking module's types func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore + sdr[StoreKey] = simulation.DecodeStore } //____________________________________________________________________________ diff --git a/x/staking/simulation/decoder/store.go b/x/staking/simulation/decoder.go similarity index 99% rename from x/staking/simulation/decoder/store.go rename to x/staking/simulation/decoder.go index 28d037c287fd..45aba449f807 100644 --- a/x/staking/simulation/decoder/store.go +++ b/x/staking/simulation/decoder.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "bytes" diff --git a/x/staking/simulation/decoder/store_test.go b/x/staking/simulation/decoder_test.go similarity index 99% rename from x/staking/simulation/decoder/store_test.go rename to x/staking/simulation/decoder_test.go index cd27001515cb..19950c97aad4 100644 --- a/x/staking/simulation/decoder/store_test.go +++ b/x/staking/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "fmt" diff --git a/x/staking/simulation/msgs.go b/x/staking/simulation/operations/msgs.go similarity index 99% rename from x/staking/simulation/msgs.go rename to x/staking/simulation/operations/msgs.go index f36a5e6d9802..caf6dd09c0bc 100644 --- a/x/staking/simulation/msgs.go +++ b/x/staking/simulation/operations/msgs.go @@ -1,4 +1,4 @@ -package simulation +package operations import ( "fmt" diff --git a/x/supply/module.go b/x/supply/module.go index e3576b466f60..5607b4d3207d 100644 --- a/x/supply/module.go +++ b/x/supply/module.go @@ -15,7 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/supply/client/cli" "github.com/cosmos/cosmos-sdk/x/supply/client/rest" "github.com/cosmos/cosmos-sdk/x/supply/internal/types" - "github.com/cosmos/cosmos-sdk/x/supply/simulation/decoder" + "github.com/cosmos/cosmos-sdk/x/supply/simulation" ) var ( @@ -73,7 +73,7 @@ type AppModuleSimulation struct{} // RegisterStoreDecoder registers a decoder for supply module's types func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[StoreKey] = decoder.DecodeStore + sdr[StoreKey] = simulation.DecodeStore } //____________________________________________________________________________ diff --git a/x/supply/simulation/decoder/store.go b/x/supply/simulation/decoder.go similarity index 97% rename from x/supply/simulation/decoder/store.go rename to x/supply/simulation/decoder.go index 2df8c27b0eba..7e22f33a5478 100644 --- a/x/supply/simulation/decoder/store.go +++ b/x/supply/simulation/decoder.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "bytes" diff --git a/x/supply/simulation/decoder/store_test.go b/x/supply/simulation/decoder_test.go similarity index 98% rename from x/supply/simulation/decoder/store_test.go rename to x/supply/simulation/decoder_test.go index f6df6c7987d7..329b3e752747 100644 --- a/x/supply/simulation/decoder/store_test.go +++ b/x/supply/simulation/decoder_test.go @@ -1,4 +1,4 @@ -package decoder +package simulation import ( "fmt" From f615c7f3d4511fe58fc0fe640755bd6565f55fad Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Tue, 13 Aug 2019 22:24:08 +0200 Subject: [PATCH 17/18] address @alexanderbez comments --- .../simulation/_4847-simulation-man | 3 -- CHANGELOG.md | 3 ++ types/module/module.go | 29 +------------------ types/module/simulation.go | 27 +++++++++++++++++ 4 files changed, 31 insertions(+), 31 deletions(-) delete mode 100644 .pending/improvements/simulation/_4847-simulation-man create mode 100644 types/module/simulation.go diff --git a/.pending/improvements/simulation/_4847-simulation-man b/.pending/improvements/simulation/_4847-simulation-man deleted file mode 100644 index ebb03f01963d..000000000000 --- a/.pending/improvements/simulation/_4847-simulation-man +++ /dev/null @@ -1,3 +0,0 @@ -#4847 `SimApp` and simulation refactors - - Implement `SimulationManager` for executing modules' simulation functionalities in a modularized way - - Add `DecodeStore` to the `SimulationManager` for decoding each module's types diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c29204e3b86..3215a02073de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,9 @@ longer panics if the store to load contains substores that we didn't explicitly * (simulation) [\#4824](https://github.com/cosmos/cosmos-sdk/issues/4824) PrintAllInvariants flag will print all failed invariants * (simulation) [\#4490](https://github.com/cosmos/cosmos-sdk/issues/4490) add `InitialBlockHeight` flag to resume a simulation from a given block * Support exporting the simulation stats to a given JSON file +* (simulation) [\#4847](https://github.com/cosmos/cosmos-sdk/issues/4847) `SimApp` and simulation refactors + * Implement `SimulationManager` for executing modules' simulation functionalities in a modularized way + * Add `DecodeStore` to the `SimulationManager` for decoding each module's types * (store) [\#4792](https://github.com/cosmos/cosmos-sdk/issues/4792) panic on non-registered store * (types) [\#4821](https://github.com/cosmos/cosmos-sdk/issues/4821) types/errors package added with support for stacktraces. It is meant as a more feature-rich replacement for sdk.Errors in the mid-term. diff --git a/types/module/module.go b/types/module/module.go index f45f3176580f..1501d6a394c0 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -338,31 +338,4 @@ func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) abci.Respo ValidatorUpdates: validatorUpdates, Events: ctx.EventManager().ABCIEvents(), } -} - -//____________________________________________________________________________ - -// SimulationManager defines a simulation manager that provides the high level utility -// for managing and executing simulation functionalities for a group of modules -type SimulationManager struct { - Modules map[string]AppModule - StoreDecoders sdk.StoreDecoderRegistry -} - -// NewSimulationManager creates a new SimulationManager object -func NewSimulationManager(moduleMap map[string]AppModule) *SimulationManager { - - decoders := make(sdk.StoreDecoderRegistry) - - return &SimulationManager{ - Modules: moduleMap, - StoreDecoders: decoders, - } -} - -// RegisterStoreDecoders registers each of the modules' store decoders into a map -func (sm *SimulationManager) RegisterStoreDecoders() { - for _, module := range sm.Modules { - module.RegisterStoreDecoder(sm.StoreDecoders) - } -} +} \ No newline at end of file diff --git a/types/module/simulation.go b/types/module/simulation.go new file mode 100644 index 000000000000..c6901677dc2e --- /dev/null +++ b/types/module/simulation.go @@ -0,0 +1,27 @@ +package module + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// SimulationManager defines a simulation manager that provides the high level utility +// for managing and executing simulation functionalities for a group of modules +type SimulationManager struct { + Modules map[string]AppModule + StoreDecoders sdk.StoreDecoderRegistry +} + +// NewSimulationManager creates a new SimulationManager object +func NewSimulationManager(moduleMap map[string]AppModule) *SimulationManager { + return &SimulationManager{ + Modules: moduleMap, + StoreDecoders: make(sdk.StoreDecoderRegistry), + } +} + +// RegisterStoreDecoders registers each of the modules' store decoders into a map +func (sm *SimulationManager) RegisterStoreDecoders() { + for _, module := range sm.Modules { + module.RegisterStoreDecoder(sm.StoreDecoders) + } +} From 7a1b573c0196b7b1a2599c43f54ae8f87500e060 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Tue, 13 Aug 2019 23:48:43 +0200 Subject: [PATCH 18/18] fix --- simapp/sim_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simapp/sim_test.go b/simapp/sim_test.go index afe893517b00..180e05b83a6f 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -184,7 +184,7 @@ func testAndRunTxs(app *SimApp) []simulation.WeightedOperation { }) return v }(nil), - authsim.SimulateDeductFee(app.AccountKeeper, app.SupplyKeeper), + authsimops.SimulateDeductFee(app.AccountKeeper, app.SupplyKeeper), }, { func(_ *rand.Rand) int {