Merge PR #6037: refactor simulation decoder

This commit is contained in:
Federico Kunze 2020-04-21 17:33:56 -04:00 committed by GitHub
parent 65048683b5
commit 3ee975c57b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 375 additions and 333 deletions

View File

@ -57,6 +57,7 @@ older clients.
### API Breaking Changes
* (modules) [\#5664](https://github.com/cosmos/cosmos-sdk/pull/5664) Remove amino `Codec` from simulation `StoreDecoder`, which now returns a function closure in order to unmarshal the key-value pairs.
* (x/auth) [\#6029](https://github.com/cosmos/cosmos-sdk/pull/6029) Module accounts have been moved from `x/supply` to `x/auth`.
* (x/supply) [\#6010](https://github.com/cosmos/cosmos-sdk/pull/6010) All `x/supply` types and APIs have been moved to `x/bank`.
* (baseapp) [\#5865](https://github.com/cosmos/cosmos-sdk/pull/5865) The `SimulationResponse` returned from tx simulation is now JSON encoded instead of Amino binary.

View File

@ -119,7 +119,7 @@ test-ledger: test-ledger-mock
@go test -mod=readonly -v `go list github.com/cosmos/cosmos-sdk/crypto` -tags='cgo ledger'
test-unit:
@VERSION=$(VERSION) go test -mod=readonly $(PACKAGES_NOSIMULATION) -tags='ledger test_ledger_mock'
@VERSION=$(VERSION) go test -mod=readonly ./... -tags='ledger test_ledger_mock'
test-race:
@VERSION=$(VERSION) go test -mod=readonly -race $(PACKAGES_NOSIMULATION)

View File

@ -261,15 +261,15 @@ func NewSimApp(
// must be passed by reference here.
app.mm = module.NewManager(
genutil.NewAppModule(app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx),
auth.NewAppModule(app.AccountKeeper),
bank.NewAppModule(app.BankKeeper, app.AccountKeeper),
auth.NewAppModule(appCodec, app.AccountKeeper),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
capability.NewAppModule(*app.CapabilityKeeper),
crisis.NewAppModule(&app.CrisisKeeper),
gov.NewAppModule(app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(app.MintKeeper, app.AccountKeeper),
slashing.NewAppModule(app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
distr.NewAppModule(app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
upgrade.NewAppModule(app.UpgradeKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
ibc.NewAppModule(app.IBCKeeper),
@ -302,13 +302,13 @@ func NewSimApp(
// NOTE: this is not required apps that don't use the simulator for fuzz testing
// transactions
app.sm = module.NewSimulationManager(
auth.NewAppModule(app.AccountKeeper),
bank.NewAppModule(app.BankKeeper, app.AccountKeeper),
gov.NewAppModule(app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(app.MintKeeper, app.AccountKeeper),
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
distr.NewAppModule(app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
slashing.NewAppModule(app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
auth.NewAppModule(appCodec, app.AccountKeeper),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
params.NewAppModule(app.ParamsKeeper),
)

View File

@ -165,7 +165,7 @@ func TestAppImportExport(t *testing.T) {
require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare")
fmt.Printf("compared %d different key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B)
require.Equal(t, len(failedKVAs), 0, GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, app.Codec(), failedKVAs, failedKVBs))
require.Equal(t, len(failedKVAs), 0, GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, failedKVAs, failedKVBs))
}
}

View File

@ -109,9 +109,8 @@ func PrintStats(db dbm.DB) {
// GetSimulationLog unmarshals the KVPair's Value to the corresponding type based on the
// each's module store key and the prefix bytes of the KVPair's key.
func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, cdc *codec.Codec, kvAs, kvBs []tmkv.Pair) (log string) {
func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, kvAs, kvBs []tmkv.Pair) (log string) {
for i := 0; i < len(kvAs); i++ {
if len(kvAs[i].Value) == 0 && len(kvBs[i].Value) == 0 {
// skip if the value doesn't have any bytes
continue
@ -119,11 +118,11 @@ func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, cdc *codec
decoder, ok := sdr[storeName]
if ok {
log += decoder(cdc, kvAs[i], kvBs[i])
log += decoder(kvAs[i], kvBs[i])
} else {
log += fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvAs[i].Key, kvAs[i].Value, kvBs[i].Key, kvBs[i].Value)
}
}
return
return log
}

View File

@ -7,7 +7,6 @@ import (
"github.com/stretchr/testify/require"
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
codecstd "github.com/cosmos/cosmos-sdk/codec/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
@ -17,7 +16,7 @@ func TestGetSimulationLog(t *testing.T) {
cdc := codecstd.MakeCodec(ModuleBasics)
decoders := make(sdk.StoreDecoderRegistry)
decoders[auth.StoreKey] = func(cdc *codec.Codec, kvAs, kvBs tmkv.Pair) string { return "10" }
decoders[auth.StoreKey] = func(kvAs, kvBs tmkv.Pair) string { return "10" }
tests := []struct {
store string
@ -44,7 +43,7 @@ func TestGetSimulationLog(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.store, func(t *testing.T) {
require.Equal(t, tt.expectedLog, GetSimulationLog(tt.store, decoders, cdc, tt.kvPairs, tt.kvPairs), tt.store)
require.Equal(t, tt.expectedLog, GetSimulationLog(tt.store, decoders, tt.kvPairs, tt.kvPairs), tt.store)
})
}
}

View File

@ -3,7 +3,6 @@ package types
import (
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/types"
)
@ -28,7 +27,7 @@ type (
// StoreDecoderRegistry defines each of the modules store decoders. Used for ImportExport
// simulation.
type StoreDecoderRegistry map[string]func(cdc *codec.Codec, kvA, kvB tmkv.Pair) string
type StoreDecoderRegistry map[string]func(kvA, kvB tmkv.Pair) string
// Iterator over all the keys with a certain prefix in ascending order
func KVStorePrefixIterator(kvs KVStore, prefix []byte) Iterator {

View File

@ -27,7 +27,9 @@ var (
)
// AppModuleBasic defines the basic application module used by the auth module.
type AppModuleBasic struct{}
type AppModuleBasic struct {
cdc Codec
}
// Name returns the auth module's name.
func (AppModuleBasic) Name() string {
@ -80,9 +82,9 @@ type AppModule struct {
}
// NewAppModule creates a new AppModule object
func NewAppModule(accountKeeper AccountKeeper) AppModule {
func NewAppModule(cdc Codec, accountKeeper AccountKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
AppModuleBasic: AppModuleBasic{cdc: cdc},
accountKeeper: accountKeeper,
}
}
@ -156,8 +158,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
}
// RegisterStoreDecoder registers a decoder for auth module's types
func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.DecodeStore
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.NewDecodeStore(am.cdc)
}
// WeightedOperations doesn't return any auth module operation.

View File

@ -4,29 +4,39 @@ import (
"bytes"
"fmt"
gogotypes "github.com/gogo/protobuf/types"
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/auth/exported"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
// DecodeStore unmarshals the KVPair's Value to the corresponding auth type
func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.AddressStoreKeyPrefix):
var accA, accB exported.Account
cdc.MustUnmarshalBinaryBare(kvA.Value, &accA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &accB)
return fmt.Sprintf("%v\n%v", accA, accB)
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding auth type.
func NewDecodeStore(cdc types.Codec) func(kvA, kvB tmkv.Pair) string {
return func(kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.AddressStoreKeyPrefix):
accA, err := cdc.UnmarshalAccount(kvA.Value)
if err != nil {
panic(err)
}
case bytes.Equal(kvA.Key, types.GlobalAccountNumberKey):
var globalAccNumberA, globalAccNumberB uint64
cdc.MustUnmarshalBinaryBare(kvA.Value, &globalAccNumberA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &globalAccNumberB)
return fmt.Sprintf("GlobalAccNumberA: %d\nGlobalAccNumberB: %d", globalAccNumberA, globalAccNumberB)
accB, err := cdc.UnmarshalAccount(kvB.Value)
if err != nil {
panic(err)
}
default:
panic(fmt.Sprintf("invalid account key %X", kvA.Key))
return fmt.Sprintf("%v\n%v", accA, accB)
case bytes.Equal(kvA.Key, types.GlobalAccountNumberKey):
var globalAccNumberA, globalAccNumberB gogotypes.UInt64Value
cdc.MustUnmarshalBinaryBare(kvA.Value, &globalAccNumberA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &globalAccNumberB)
return fmt.Sprintf("GlobalAccNumberA: %d\nGlobalAccNumberB: %d", globalAccNumberA, globalAccNumberB)
default:
panic(fmt.Sprintf("unexpected %s key %X (%s)", types.ModuleName, kvA.Key, kvA.Key))
}
}
}

View File

@ -1,16 +1,18 @@
package simulation
package simulation_test
import (
"fmt"
"testing"
gogotypes "github.com/gogo/protobuf/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/ed25519"
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
codecstd "github.com/cosmos/cosmos-sdk/codec/std"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/simulation"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
@ -19,23 +21,29 @@ var (
delAddr1 = sdk.AccAddress(delPk1.Address())
)
func makeTestCodec() (cdc *codec.Codec) {
cdc = codec.New()
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
types.RegisterCodec(cdc)
return
}
func TestDecodeStore(t *testing.T) {
cdc := makeTestCodec()
cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics))
acc := types.NewBaseAccountWithAddress(delAddr1)
globalAccNumber := uint64(10)
dec := simulation.NewDecodeStore(cdc)
accBz, err := cdc.MarshalAccount(acc)
require.NoError(t, err)
globalAccNumber := gogotypes.UInt64Value{Value: 10}
kvPairs := tmkv.Pairs{
tmkv.Pair{Key: types.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(acc)},
tmkv.Pair{Key: types.GlobalAccountNumberKey, Value: cdc.MustMarshalBinaryBare(globalAccNumber)},
tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
tmkv.Pair{
Key: types.AddressStoreKey(delAddr1),
Value: accBz,
},
tmkv.Pair{
Key: types.GlobalAccountNumberKey,
Value: cdc.MustMarshalBinaryBare(&globalAccNumber),
},
tmkv.Pair{
Key: []byte{0x99},
Value: []byte{0x99},
},
}
tests := []struct {
name string
@ -51,9 +59,9 @@ func TestDecodeStore(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
switch i {
case len(tests) - 1:
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name)
default:
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name)
}
})
}

View File

@ -28,7 +28,9 @@ var (
)
// AppModuleBasic defines the basic application module used by the bank module.
type AppModuleBasic struct{}
type AppModuleBasic struct {
cdc Codec
}
// Name returns the bank module's name.
func (AppModuleBasic) Name() string { return ModuleName }
@ -78,9 +80,9 @@ type AppModule struct {
}
// NewAppModule creates a new AppModule object
func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper) AppModule {
func NewAppModule(cdc Codec, keeper Keeper, accountKeeper types.AccountKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
AppModuleBasic: AppModuleBasic{cdc: cdc},
keeper: keeper,
accountKeeper: accountKeeper,
}
@ -153,8 +155,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
}
// RegisterStoreDecoder registers a decoder for supply module's types
func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.DecodeStore
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.NewDecodeStore(am.cdc)
}
// WeightedOperations returns the all the gov module operations with their respective weights.

View File

@ -6,20 +6,29 @@ import (
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
// DecodeStore unmarshals the KVPair's values to the corresponding types.
func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.SupplyKey):
var supplyA, supplyB types.Supply
cdc.MustUnmarshalBinaryBare(kvA.Value, &supplyA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &supplyB)
return fmt.Sprintf("%v\n%v", supplyB, supplyB)
// NewDecodeStore returns a function closure that unmarshals the KVPair's values
// to the corresponding types.
func NewDecodeStore(cdc types.Codec) func(kvA, kvB tmkv.Pair) string {
return func(kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.SupplyKey):
supplyA, err := cdc.UnmarshalSupply(kvA.Value)
if err != nil {
panic(err)
}
default:
panic(fmt.Sprintf("unknown %s key %X (%s)", types.ModuleName, kvA.Key, kvA.Key))
supplyB, err := cdc.UnmarshalSupply(kvB.Value)
if err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", supplyA, supplyB)
default:
panic(fmt.Sprintf("unexpected %s key %X (%s)", types.ModuleName, kvA.Key, kvA.Key))
}
}
}

View File

@ -1,4 +1,4 @@
package simulation
package simulation_test
import (
"fmt"
@ -7,25 +7,24 @@ import (
"github.com/stretchr/testify/require"
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
codecstd "github.com/cosmos/cosmos-sdk/codec/std"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/simulation"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
func makeTestCodec() (cdc *codec.Codec) {
cdc = codec.New()
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
types.RegisterCodec(cdc)
return
}
func TestDecodeStore(t *testing.T) {
cdc := makeTestCodec()
cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics))
dec := simulation.NewDecodeStore(cdc)
totalSupply := types.NewSupply(sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000)))
supplyBz, err := cdc.MarshalSupply(totalSupply)
require.NoError(t, err)
kvPairs := tmkv.Pairs{
tmkv.Pair{Key: types.SupplyKey, Value: cdc.MustMarshalBinaryBare(totalSupply)},
tmkv.Pair{Key: types.SupplyKey, Value: supplyBz},
tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
}
@ -42,9 +41,9 @@ func TestDecodeStore(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
switch i {
case len(tests) - 1:
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name)
default:
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name)
}
})
}

View File

@ -28,7 +28,9 @@ var (
)
// AppModuleBasic defines the basic application module used by the distribution module.
type AppModuleBasic struct{}
type AppModuleBasic struct {
cdc codec.Marshaler
}
// Name returns the distribution module's name.
func (AppModuleBasic) Name() string {
@ -85,11 +87,11 @@ type AppModule struct {
// NewAppModule creates a new AppModule object
func NewAppModule(
keeper Keeper, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper,
stakingKeeper stakingkeeper.Keeper,
cdc codec.Marshaler, keeper Keeper, accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper, stakingKeeper stakingkeeper.Keeper,
) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
AppModuleBasic: AppModuleBasic{cdc: cdc},
keeper: keeper,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
@ -175,8 +177,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
}
// RegisterStoreDecoder registers a decoder for distribution module's types
func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.DecodeStore
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.NewDecodeStore(am.cdc)
}
// WeightedOperations returns the all the gov module operations with their respective weights.

View File

@ -11,58 +11,61 @@ import (
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)
// DecodeStore unmarshals the KVPair's Value to the corresponding distribution type
func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.FeePoolKey):
var feePoolA, feePoolB types.FeePool
cdc.MustUnmarshalBinaryBare(kvA.Value, &feePoolA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &feePoolB)
return fmt.Sprintf("%v\n%v", feePoolA, feePoolB)
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding distribution type.
func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB tmkv.Pair) string {
return func(kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.FeePoolKey):
var feePoolA, feePoolB types.FeePool
cdc.MustUnmarshalBinaryBare(kvA.Value, &feePoolA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &feePoolB)
return fmt.Sprintf("%v\n%v", feePoolA, feePoolB)
case bytes.Equal(kvA.Key[:1], types.ProposerKey):
return fmt.Sprintf("%v\n%v", sdk.ConsAddress(kvA.Value), sdk.ConsAddress(kvB.Value))
case bytes.Equal(kvA.Key[:1], types.ProposerKey):
return fmt.Sprintf("%v\n%v", sdk.ConsAddress(kvA.Value), sdk.ConsAddress(kvB.Value))
case bytes.Equal(kvA.Key[:1], types.ValidatorOutstandingRewardsPrefix):
var rewardsA, rewardsB types.ValidatorOutstandingRewards
cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB)
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)
case bytes.Equal(kvA.Key[:1], types.ValidatorOutstandingRewardsPrefix):
var rewardsA, rewardsB types.ValidatorOutstandingRewards
cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB)
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)
case bytes.Equal(kvA.Key[:1], types.DelegatorWithdrawAddrPrefix):
return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value))
case bytes.Equal(kvA.Key[:1], types.DelegatorWithdrawAddrPrefix):
return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value))
case bytes.Equal(kvA.Key[:1], types.DelegatorStartingInfoPrefix):
var infoA, infoB types.DelegatorStartingInfo
cdc.MustUnmarshalBinaryBare(kvA.Value, &infoA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &infoB)
return fmt.Sprintf("%v\n%v", infoA, infoB)
case bytes.Equal(kvA.Key[:1], types.DelegatorStartingInfoPrefix):
var infoA, infoB types.DelegatorStartingInfo
cdc.MustUnmarshalBinaryBare(kvA.Value, &infoA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &infoB)
return fmt.Sprintf("%v\n%v", infoA, infoB)
case bytes.Equal(kvA.Key[:1], types.ValidatorHistoricalRewardsPrefix):
var rewardsA, rewardsB types.ValidatorHistoricalRewards
cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB)
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)
case bytes.Equal(kvA.Key[:1], types.ValidatorHistoricalRewardsPrefix):
var rewardsA, rewardsB types.ValidatorHistoricalRewards
cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB)
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)
case bytes.Equal(kvA.Key[:1], types.ValidatorCurrentRewardsPrefix):
var rewardsA, rewardsB types.ValidatorCurrentRewards
cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB)
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)
case bytes.Equal(kvA.Key[:1], types.ValidatorCurrentRewardsPrefix):
var rewardsA, rewardsB types.ValidatorCurrentRewards
cdc.MustUnmarshalBinaryBare(kvA.Value, &rewardsA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &rewardsB)
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)
case bytes.Equal(kvA.Key[:1], types.ValidatorAccumulatedCommissionPrefix):
var commissionA, commissionB types.ValidatorAccumulatedCommission
cdc.MustUnmarshalBinaryBare(kvA.Value, &commissionA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &commissionB)
return fmt.Sprintf("%v\n%v", commissionA, commissionB)
case bytes.Equal(kvA.Key[:1], types.ValidatorAccumulatedCommissionPrefix):
var commissionA, commissionB types.ValidatorAccumulatedCommission
cdc.MustUnmarshalBinaryBare(kvA.Value, &commissionA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &commissionB)
return fmt.Sprintf("%v\n%v", commissionA, commissionB)
case bytes.Equal(kvA.Key[:1], types.ValidatorSlashEventPrefix):
var eventA, eventB types.ValidatorSlashEvent
cdc.MustUnmarshalBinaryBare(kvA.Value, &eventA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &eventB)
return fmt.Sprintf("%v\n%v", eventA, eventB)
case bytes.Equal(kvA.Key[:1], types.ValidatorSlashEventPrefix):
var eventA, eventB types.ValidatorSlashEvent
cdc.MustUnmarshalBinaryBare(kvA.Value, &eventA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &eventB)
return fmt.Sprintf("%v\n%v", eventA, eventB)
default:
panic(fmt.Sprintf("invalid distribution key prefix %X", kvA.Key[:1]))
default:
panic(fmt.Sprintf("invalid distribution key prefix %X", kvA.Key[:1]))
}
}
}

View File

@ -1,4 +1,4 @@
package simulation
package simulation_test
import (
"fmt"
@ -9,8 +9,10 @@ import (
"github.com/tendermint/tendermint/crypto/ed25519"
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
codecstd "github.com/cosmos/cosmos-sdk/codec/std"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/simulation"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)
@ -21,16 +23,9 @@ var (
consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes())
)
func makeTestCodec() (cdc *codec.Codec) {
cdc = codec.New()
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
types.RegisterCodec(cdc)
return
}
func TestDecodeDistributionStore(t *testing.T) {
cdc := makeTestCodec()
cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics))
dec := simulation.NewDecodeStore(cdc)
decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.OneDec())}
feePool := types.InitialFeePool()
@ -43,15 +38,15 @@ func TestDecodeDistributionStore(t *testing.T) {
slashEvent := types.NewValidatorSlashEvent(10, sdk.OneDec())
kvPairs := tmkv.Pairs{
tmkv.Pair{Key: types.FeePoolKey, Value: cdc.MustMarshalBinaryBare(feePool)},
tmkv.Pair{Key: types.FeePoolKey, Value: cdc.MustMarshalBinaryBare(&feePool)},
tmkv.Pair{Key: types.ProposerKey, Value: consAddr1.Bytes()},
tmkv.Pair{Key: types.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryBare(outstanding)},
tmkv.Pair{Key: types.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryBare(&outstanding)},
tmkv.Pair{Key: types.GetDelegatorWithdrawAddrKey(delAddr1), Value: delAddr1.Bytes()},
tmkv.Pair{Key: types.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshalBinaryBare(info)},
tmkv.Pair{Key: types.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshalBinaryBare(historicalRewards)},
tmkv.Pair{Key: types.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryBare(currentRewards)},
tmkv.Pair{Key: types.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshalBinaryBare(commission)},
tmkv.Pair{Key: types.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshalBinaryBare(slashEvent)},
tmkv.Pair{Key: types.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshalBinaryBare(&info)},
tmkv.Pair{Key: types.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshalBinaryBare(&historicalRewards)},
tmkv.Pair{Key: types.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryBare(&currentRewards)},
tmkv.Pair{Key: types.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshalBinaryBare(&commission)},
tmkv.Pair{Key: types.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshalBinaryBare(&slashEvent)},
tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
}
@ -75,9 +70,9 @@ func TestDecodeDistributionStore(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
switch i {
case len(tests) - 1:
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name)
default:
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name)
}
})
}

View File

@ -32,6 +32,7 @@ var (
// AppModuleBasic defines the basic application module used by the gov module.
type AppModuleBasic struct {
cdc Codec
proposalHandlers []client.ProposalHandler // proposal handlers which live in governance cli and rest
}
@ -106,9 +107,9 @@ type AppModule struct {
}
// NewAppModule creates a new AppModule object
func NewAppModule(keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper) AppModule {
func NewAppModule(cdc Codec, keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
AppModuleBasic: AppModuleBasic{cdc: cdc},
keeper: keeper,
accountKeeper: ak,
bankKeeper: bk,
@ -192,8 +193,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
}
// RegisterStoreDecoder registers a decoder for gov module's types
func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.DecodeStore
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.NewDecodeStore(am.cdc)
}
// WeightedOperations returns the all the gov module operations with their respective weights.

View File

@ -7,39 +7,46 @@ import (
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
// DecodeStore unmarshals the KVPair's Value to the corresponding gov type
func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.ProposalsKeyPrefix):
var proposalA, proposalB types.Proposal
cdc.MustUnmarshalBinaryBare(kvA.Value, &proposalA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &proposalB)
return fmt.Sprintf("%v\n%v", proposalA, proposalB)
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding gov type.
func NewDecodeStore(cdc types.Codec) func(kvA, kvB tmkv.Pair) string {
return func(kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.ProposalsKeyPrefix):
proposalA, err := cdc.UnmarshalProposal(kvA.Value)
if err != nil {
panic(err)
}
proposalB, err := cdc.UnmarshalProposal(kvB.Value)
if err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", proposalA, proposalB)
case bytes.Equal(kvA.Key[:1], types.ActiveProposalQueuePrefix),
bytes.Equal(kvA.Key[:1], types.InactiveProposalQueuePrefix),
bytes.Equal(kvA.Key[:1], types.ProposalIDKey):
proposalIDA := binary.LittleEndian.Uint64(kvA.Value)
proposalIDB := binary.LittleEndian.Uint64(kvB.Value)
return fmt.Sprintf("proposalIDA: %d\nProposalIDB: %d", proposalIDA, proposalIDB)
case bytes.Equal(kvA.Key[:1], types.ActiveProposalQueuePrefix),
bytes.Equal(kvA.Key[:1], types.InactiveProposalQueuePrefix),
bytes.Equal(kvA.Key[:1], types.ProposalIDKey):
proposalIDA := binary.LittleEndian.Uint64(kvA.Value)
proposalIDB := binary.LittleEndian.Uint64(kvB.Value)
return fmt.Sprintf("proposalIDA: %d\nProposalIDB: %d", proposalIDA, proposalIDB)
case bytes.Equal(kvA.Key[:1], types.DepositsKeyPrefix):
var depositA, depositB types.Deposit
cdc.MustUnmarshalBinaryBare(kvA.Value, &depositA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &depositB)
return fmt.Sprintf("%v\n%v", depositA, depositB)
case bytes.Equal(kvA.Key[:1], types.DepositsKeyPrefix):
var depositA, depositB types.Deposit
cdc.MustUnmarshalBinaryBare(kvA.Value, &depositA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &depositB)
return fmt.Sprintf("%v\n%v", depositA, depositB)
case bytes.Equal(kvA.Key[:1], types.VotesKeyPrefix):
var voteA, voteB types.Vote
cdc.MustUnmarshalBinaryBare(kvA.Value, &voteA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &voteB)
return fmt.Sprintf("%v\n%v", voteA, voteB)
case bytes.Equal(kvA.Key[:1], types.VotesKeyPrefix):
var voteA, voteB types.Vote
cdc.MustUnmarshalBinaryBare(kvA.Value, &voteA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &voteB)
return fmt.Sprintf("%v\n%v", voteA, voteB)
default:
panic(fmt.Sprintf("invalid governance key prefix %X", kvA.Key[:1]))
default:
panic(fmt.Sprintf("invalid governance key prefix %X", kvA.Key[:1]))
}
}
}

View File

@ -1,4 +1,4 @@
package simulation
package simulation_test
import (
"encoding/binary"
@ -11,8 +11,10 @@ import (
"github.com/tendermint/tendermint/crypto/ed25519"
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
codecstd "github.com/cosmos/cosmos-sdk/codec/std"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/simulation"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
@ -21,16 +23,9 @@ var (
delAddr1 = sdk.AccAddress(delPk1.Address())
)
func makeTestCodec() (cdc *codec.Codec) {
cdc = codec.New()
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
types.RegisterCodec(cdc)
return
}
func TestDecodeStore(t *testing.T) {
cdc := makeTestCodec()
cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics))
dec := simulation.NewDecodeStore(cdc)
endTime := time.Now().UTC()
@ -41,11 +36,14 @@ func TestDecodeStore(t *testing.T) {
deposit := types.NewDeposit(1, delAddr1, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())))
vote := types.NewVote(1, delAddr1, types.OptionYes)
proposalBz, err := cdc.MarshalProposal(proposal)
require.NoError(t, err)
kvPairs := tmkv.Pairs{
tmkv.Pair{Key: types.ProposalKey(1), Value: cdc.MustMarshalBinaryBare(proposal)},
tmkv.Pair{Key: types.ProposalKey(1), Value: proposalBz},
tmkv.Pair{Key: types.InactiveProposalQueueKey(1, endTime), Value: proposalIDBz},
tmkv.Pair{Key: types.DepositKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(deposit)},
tmkv.Pair{Key: types.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(vote)},
tmkv.Pair{Key: types.DepositKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(&deposit)},
tmkv.Pair{Key: types.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryBare(&vote)},
tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
}
@ -65,9 +63,9 @@ func TestDecodeStore(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
switch i {
case len(tests) - 1:
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name)
default:
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name)
}
})
}

View File

@ -27,7 +27,9 @@ var (
)
// AppModuleBasic defines the basic application module used by the mint module.
type AppModuleBasic struct{}
type AppModuleBasic struct {
cdc codec.Marshaler
}
var _ module.AppModuleBasic = AppModuleBasic{}
@ -79,9 +81,9 @@ type AppModule struct {
}
// NewAppModule creates a new AppModule object
func NewAppModule(keeper Keeper, ak types.AccountKeeper) AppModule {
func NewAppModule(cdc codec.Marshaler, keeper Keeper, ak types.AccountKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
AppModuleBasic: AppModuleBasic{cdc: cdc},
keeper: keeper,
authKeeper: ak,
}
@ -159,8 +161,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
}
// RegisterStoreDecoder registers a decoder for mint module's types.
func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.DecodeStore
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.NewDecodeStore(am.cdc)
}
// WeightedOperations doesn't return any mint module operation.

View File

@ -10,15 +10,18 @@ import (
"github.com/cosmos/cosmos-sdk/x/mint/types"
)
// DecodeStore unmarshals the KVPair's Value to the corresponding mint type
func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key, types.MinterKey):
var minterA, minterB types.Minter
cdc.MustUnmarshalBinaryBare(kvA.Value, &minterA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &minterB)
return fmt.Sprintf("%v\n%v", minterA, minterB)
default:
panic(fmt.Sprintf("invalid mint key %X", kvA.Key))
// NewDecodeStore returns a decoder function closure that umarshals the KVPair's
// Value to the corresponding mint type.
func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB tmkv.Pair) string {
return func(kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key, types.MinterKey):
var minterA, minterB types.Minter
cdc.MustUnmarshalBinaryBare(kvA.Value, &minterA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &minterB)
return fmt.Sprintf("%v\n%v", minterA, minterB)
default:
panic(fmt.Sprintf("invalid mint key %X", kvA.Key))
}
}
}

View File

@ -1,4 +1,4 @@
package simulation
package simulation_test
import (
"fmt"
@ -8,23 +8,21 @@ import (
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
codecstd "github.com/cosmos/cosmos-sdk/codec/std"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/mint/simulation"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)
func makeTestCodec() (cdc *codec.Codec) {
cdc = codec.New()
sdk.RegisterCodec(cdc)
return
}
func TestDecodeStore(t *testing.T) {
cdc := makeTestCodec()
cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics))
dec := simulation.NewDecodeStore(cdc)
minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15))
kvPairs := tmkv.Pairs{
tmkv.Pair{Key: types.MinterKey, Value: cdc.MustMarshalBinaryBare(minter)},
tmkv.Pair{Key: types.MinterKey, Value: cdc.MustMarshalBinaryBare(&minter)},
tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
}
tests := []struct {
@ -40,9 +38,9 @@ func TestDecodeStore(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
switch i {
case len(tests) - 1:
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name)
default:
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name)
}
})
}

View File

@ -29,7 +29,9 @@ var (
)
// AppModuleBasic defines the basic application module used by the slashing module.
type AppModuleBasic struct{}
type AppModuleBasic struct {
cdc codec.Marshaler
}
var _ module.AppModuleBasic = AppModuleBasic{}
@ -87,9 +89,9 @@ type AppModule struct {
}
// NewAppModule creates a new AppModule object
func NewAppModule(keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper, sk stakingkeeper.Keeper) AppModule {
func NewAppModule(cdc codec.Marshaler, keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper, sk stakingkeeper.Keeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
AppModuleBasic: AppModuleBasic{cdc: cdc},
keeper: keeper,
accountKeeper: ak,
bankKeeper: bk,
@ -172,8 +174,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
}
// RegisterStoreDecoder registers a decoder for slashing module's types
func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.DecodeStore
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.NewDecodeStore(am.cdc)
}
// WeightedOperations returns the all the slashing module operations with their respective weights.

View File

@ -5,38 +5,37 @@ import (
"fmt"
gogotypes "github.com/gogo/protobuf/types"
"github.com/tendermint/tendermint/crypto"
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)
// DecodeStore unmarshals the KVPair's Value to the corresponding slashing type
func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.ValidatorSigningInfoKey):
var infoA, infoB types.ValidatorSigningInfo
cdc.MustUnmarshalBinaryBare(kvA.Value, &infoA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &infoB)
return fmt.Sprintf("%v\n%v", infoA, infoB)
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding slashing type.
func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB tmkv.Pair) string {
return func(kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.ValidatorSigningInfoKey):
var infoA, infoB types.ValidatorSigningInfo
cdc.MustUnmarshalBinaryBare(kvA.Value, &infoA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &infoB)
return fmt.Sprintf("%v\n%v", infoA, infoB)
case bytes.Equal(kvA.Key[:1], types.ValidatorMissedBlockBitArrayKey):
var missedA, missedB gogotypes.BoolValue
cdc.MustUnmarshalBinaryBare(kvA.Value, &missedA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &missedB)
return fmt.Sprintf("missedA: %v\nmissedB: %v", missedA.Value, missedB.Value)
case bytes.Equal(kvA.Key[:1], types.ValidatorMissedBlockBitArrayKey):
var missedA, missedB gogotypes.BoolValue
cdc.MustUnmarshalBinaryBare(kvA.Value, &missedA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &missedB)
return fmt.Sprintf("missedA: %v\nmissedB: %v", missedA.Value, missedB.Value)
case bytes.Equal(kvA.Key[:1], types.AddrPubkeyRelationKey):
var pubKeyA, pubKeyB crypto.PubKey
cdc.MustUnmarshalBinaryBare(kvA.Value, &pubKeyA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &pubKeyB)
bechPKA := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKeyA)
bechPKB := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKeyB)
return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", bechPKA, bechPKB)
case bytes.Equal(kvA.Key[:1], types.AddrPubkeyRelationKey):
var pubKeyA, pubKeyB gogotypes.StringValue
cdc.MustUnmarshalBinaryBare(kvA.Value, &pubKeyA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &pubKeyB)
return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", pubKeyA.Value, pubKeyB.Value)
default:
panic(fmt.Sprintf("invalid slashing key prefix %X", kvA.Key[:1]))
default:
panic(fmt.Sprintf("invalid slashing key prefix %X", kvA.Key[:1]))
}
}
}

View File

@ -1,4 +1,4 @@
package simulation
package simulation_test
import (
"fmt"
@ -12,8 +12,10 @@ import (
"github.com/tendermint/tendermint/crypto/ed25519"
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
codecstd "github.com/cosmos/cosmos-sdk/codec/std"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/simulation"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)
@ -25,25 +27,18 @@ var (
consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes())
)
func makeTestCodec() (cdc *codec.Codec) {
cdc = codec.New()
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
types.RegisterCodec(cdc)
return
}
func TestDecodeStore(t *testing.T) {
cdc := makeTestCodec()
cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics))
dec := simulation.NewDecodeStore(cdc)
info := types.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0)
bechPK := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, delPk1)
bechPK := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, delPk1)
missed := gogotypes.BoolValue{Value: true}
kvPairs := tmkv.Pairs{
tmkv.Pair{Key: types.GetValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryBare(info)},
tmkv.Pair{Key: types.GetValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryBare(&info)},
tmkv.Pair{Key: types.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryBare(&missed)},
tmkv.Pair{Key: types.GetAddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryBare(delPk1)},
tmkv.Pair{Key: types.GetAddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryBare(&gogotypes.StringValue{Value: bechPK})},
tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
}
@ -61,9 +56,9 @@ func TestDecodeStore(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
switch i {
case len(tests) - 1:
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name)
default:
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name)
}
})
}

View File

@ -32,7 +32,9 @@ var (
)
// AppModuleBasic defines the basic application module used by the staking module.
type AppModuleBasic struct{}
type AppModuleBasic struct {
cdc codec.Marshaler
}
var _ module.AppModuleBasic = AppModuleBasic{}
@ -110,9 +112,9 @@ type AppModule struct {
}
// NewAppModule creates a new AppModule object
func NewAppModule(keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper) AppModule {
func NewAppModule(cdc codec.Marshaler, keeper Keeper, ak types.AccountKeeper, bk types.BankKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
AppModuleBasic: AppModuleBasic{cdc: cdc},
keeper: keeper,
accountKeeper: ak,
bankKeeper: bk,
@ -195,8 +197,8 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
}
// RegisterStoreDecoder registers a decoder for staking module's types
func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.DecodeStore
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[StoreKey] = simulation.NewDecodeStore(am.cdc)
}
// WeightedOperations returns the all the staking module operations with their respective weights.

View File

@ -11,47 +11,50 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
// DecodeStore unmarshals the KVPair's Value to the corresponding staking type
func DecodeStore(cdc *codec.Codec, kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.LastTotalPowerKey):
var powerA, powerB sdk.Int
cdc.MustUnmarshalBinaryBare(kvA.Value, &powerA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &powerB)
return fmt.Sprintf("%v\n%v", powerA, powerB)
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding staking type.
func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB tmkv.Pair) string {
return func(kvA, kvB tmkv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.LastTotalPowerKey):
var powerA, powerB sdk.IntProto
cdc.MustUnmarshalBinaryBare(kvA.Value, &powerA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &powerB)
return fmt.Sprintf("%v\n%v", powerA, powerB)
case bytes.Equal(kvA.Key[:1], types.ValidatorsKey):
var validatorA, validatorB types.Validator
cdc.MustUnmarshalBinaryBare(kvA.Value, &validatorA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &validatorB)
return fmt.Sprintf("%v\n%v", validatorA, validatorB)
case bytes.Equal(kvA.Key[:1], types.ValidatorsKey):
var validatorA, validatorB types.Validator
cdc.MustUnmarshalBinaryBare(kvA.Value, &validatorA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &validatorB)
return fmt.Sprintf("%v\n%v", validatorA, validatorB)
case bytes.Equal(kvA.Key[:1], types.LastValidatorPowerKey),
bytes.Equal(kvA.Key[:1], types.ValidatorsByConsAddrKey),
bytes.Equal(kvA.Key[:1], types.ValidatorsByPowerIndexKey):
return fmt.Sprintf("%v\n%v", sdk.ValAddress(kvA.Value), sdk.ValAddress(kvB.Value))
case bytes.Equal(kvA.Key[:1], types.LastValidatorPowerKey),
bytes.Equal(kvA.Key[:1], types.ValidatorsByConsAddrKey),
bytes.Equal(kvA.Key[:1], types.ValidatorsByPowerIndexKey):
return fmt.Sprintf("%v\n%v", sdk.ValAddress(kvA.Value), sdk.ValAddress(kvB.Value))
case bytes.Equal(kvA.Key[:1], types.DelegationKey):
var delegationA, delegationB types.Delegation
cdc.MustUnmarshalBinaryBare(kvA.Value, &delegationA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &delegationB)
return fmt.Sprintf("%v\n%v", delegationA, delegationB)
case bytes.Equal(kvA.Key[:1], types.DelegationKey):
var delegationA, delegationB types.Delegation
cdc.MustUnmarshalBinaryBare(kvA.Value, &delegationA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &delegationB)
return fmt.Sprintf("%v\n%v", delegationA, delegationB)
case bytes.Equal(kvA.Key[:1], types.UnbondingDelegationKey),
bytes.Equal(kvA.Key[:1], types.UnbondingDelegationByValIndexKey):
var ubdA, ubdB types.UnbondingDelegation
cdc.MustUnmarshalBinaryBare(kvA.Value, &ubdA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &ubdB)
return fmt.Sprintf("%v\n%v", ubdA, ubdB)
case bytes.Equal(kvA.Key[:1], types.UnbondingDelegationKey),
bytes.Equal(kvA.Key[:1], types.UnbondingDelegationByValIndexKey):
var ubdA, ubdB types.UnbondingDelegation
cdc.MustUnmarshalBinaryBare(kvA.Value, &ubdA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &ubdB)
return fmt.Sprintf("%v\n%v", ubdA, ubdB)
case bytes.Equal(kvA.Key[:1], types.RedelegationKey),
bytes.Equal(kvA.Key[:1], types.RedelegationByValSrcIndexKey):
var redA, redB types.Redelegation
cdc.MustUnmarshalBinaryBare(kvA.Value, &redA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &redB)
return fmt.Sprintf("%v\n%v", redA, redB)
case bytes.Equal(kvA.Key[:1], types.RedelegationKey),
bytes.Equal(kvA.Key[:1], types.RedelegationByValSrcIndexKey):
var redA, redB types.Redelegation
cdc.MustUnmarshalBinaryBare(kvA.Value, &redA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &redB)
return fmt.Sprintf("%v\n%v", redA, redB)
default:
panic(fmt.Sprintf("invalid staking key prefix %X", kvA.Key[:1]))
default:
panic(fmt.Sprintf("invalid staking key prefix %X", kvA.Key[:1]))
}
}
}

View File

@ -1,4 +1,4 @@
package simulation
package simulation_test
import (
"fmt"
@ -11,7 +11,10 @@ import (
tmkv "github.com/tendermint/tendermint/libs/kv"
"github.com/cosmos/cosmos-sdk/codec"
codecstd "github.com/cosmos/cosmos-sdk/codec/std"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/simulation"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
@ -30,7 +33,8 @@ func makeTestCodec() (cdc *codec.Codec) {
}
func TestDecodeStore(t *testing.T) {
cdc := makeTestCodec()
cdc := codecstd.NewAppCodec(codecstd.MakeCodec(simapp.ModuleBasics))
dec := simulation.NewDecodeStore(cdc)
bondTime := time.Now().UTC()
@ -40,12 +44,12 @@ func TestDecodeStore(t *testing.T) {
red := types.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, sdk.OneInt(), sdk.OneDec())
kvPairs := tmkv.Pairs{
tmkv.Pair{Key: types.LastTotalPowerKey, Value: cdc.MustMarshalBinaryBare(sdk.OneInt())},
tmkv.Pair{Key: types.GetValidatorKey(valAddr1), Value: cdc.MustMarshalBinaryBare(val)},
tmkv.Pair{Key: types.LastTotalPowerKey, Value: cdc.MustMarshalBinaryBare(&sdk.IntProto{Int: sdk.OneInt()})},
tmkv.Pair{Key: types.GetValidatorKey(valAddr1), Value: cdc.MustMarshalBinaryBare(&val)},
tmkv.Pair{Key: types.LastValidatorPowerKey, Value: valAddr1.Bytes()},
tmkv.Pair{Key: types.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(del)},
tmkv.Pair{Key: types.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(ubd)},
tmkv.Pair{Key: types.GetREDKey(delAddr1, valAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(red)},
tmkv.Pair{Key: types.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(&del)},
tmkv.Pair{Key: types.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(&ubd)},
tmkv.Pair{Key: types.GetREDKey(delAddr1, valAddr1, valAddr1), Value: cdc.MustMarshalBinaryBare(&red)},
tmkv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
}
@ -66,9 +70,9 @@ func TestDecodeStore(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
switch i {
case len(tests) - 1:
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name)
default:
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name)
}
})
}