cosmos-sdk/x/capability/simulation/decoder.go
Federico Kunze 930802e7a1
x/capability: simulations (#6062)
* x/capability: simulations

* update logs

* validate genesis state

* InitGenesis and ExportGenesis functions

* update validation func

* fix import-export sim

* remove nondeterminism from capability genesis

* Update x/capability/types/genesis.go

* Update x/capability/types/genesis.go

* fix tests

* fix merge

* consistency updates

* try fix nondeterminism

* fix conditional

* Fix random index logic

* lint

* lint

Co-authored-by: Aditya Sripal <adityasripal@gmail.com>
Co-authored-by: Aleksandr Bezobchuk <aleks.bezobchuk@gmail.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
2020-04-30 15:07:43 +00:00

35 lines
1.1 KiB
Go

package simulation
import (
"bytes"
"fmt"
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/capability/types"
)
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding capaility 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.KeyIndex):
idxA := sdk.BigEndianToUint64(kvA.Value)
idxB := sdk.BigEndianToUint64(kvB.Value)
return fmt.Sprintf("Index A: %d\nIndex B: %d\n", idxA, idxB)
case bytes.HasPrefix(kvA.Key, types.KeyPrefixIndexCapability):
var capOwnersA, capOwnersB types.CapabilityOwners
cdc.MustUnmarshalBinaryBare(kvA.Value, &capOwnersA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &capOwnersB)
return fmt.Sprintf("CapabilityOwners A: %v\nCapabilityOwners B: %v\n", capOwnersA, capOwnersB)
default:
panic(fmt.Sprintf("invalid %s key prefix %X (%s)", types.ModuleName, kvA.Key, string(kvA.Key)))
}
}
}