rename store subspace, add WithTypeTable

This commit is contained in:
mossid 2018-10-11 05:01:30 +09:00
parent 03975407ba
commit 5c92a546c6
27 changed files with 165 additions and 151 deletions

View File

@ -97,14 +97,14 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
app.stakeKeeper = stake.NewKeeper(
app.cdc,
app.keyStake, app.tkeyStake,
app.bankKeeper, app.paramsKeeper.Substore(stake.DefaultParamspace, stake.ParamTable()),
app.bankKeeper, app.paramsKeeper.Subspace(stake.DefaultParamspace),
app.RegisterCodespace(stake.DefaultCodespace),
)
app.slashingKeeper = slashing.NewKeeper(
app.cdc,
app.keySlashing,
app.stakeKeeper, app.paramsKeeper.Substore(slashing.DefaultParamspace, slashing.ParamTable()),
app.stakeKeeper, app.paramsKeeper.Subspace(slashing.DefaultParamspace),
app.RegisterCodespace(slashing.DefaultCodespace),
)
@ -115,7 +115,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
app.govKeeper = gov.NewKeeper(
app.cdc,
app.keyGov,
app.paramsKeeper, app.paramsKeeper.Substore(gov.DefaultParamspace, gov.ParamTable()), app.bankKeeper, app.stakeKeeper,
app.paramsKeeper, app.paramsKeeper.Subspace(gov.DefaultParamspace), app.bankKeeper, app.stakeKeeper,
app.RegisterCodespace(gov.DefaultCodespace),
)

View File

@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
tmtypes "github.com/tendermint/tendermint/types"
)
@ -69,8 +70,9 @@ func NewTestGaiaAppGenState(
}
return GenesisState{
Accounts: genAccs,
StakeData: stakeData,
GovData: gov.DefaultGenesisState(),
Accounts: genAccs,
StakeData: stakeData,
SlashingData: slashing.DefaultGenesisState(),
GovData: gov.DefaultGenesisState(),
}, nil
}

View File

@ -20,8 +20,8 @@ var (
)
// Type declaration for parameters
func ParamTable() params.Table {
return params.NewTable(
func ParamTypeTable() params.TypeTable {
return params.NewTypeTable(
ParamStoreKeyDepositProcedure, DepositProcedure{},
ParamStoreKeyVotingProcedure, VotingProcedure{},
ParamStoreKeyTallyingProcedure, TallyingProcedure{},
@ -34,7 +34,7 @@ type Keeper struct {
paramsKeeper params.Keeper
// The reference to the Paramstore to get and set gov specific params
paramStore params.Store
paramSpace params.Subspace
// The reference to the CoinKeeper to modify balances
ck bank.Keeper
@ -60,11 +60,11 @@ type Keeper struct {
// - depositing funds into proposals, and activating upon sufficient funds being deposited
// - users voting on proposals, with weight proportional to stake in the system
// - and tallying the result of the vote.
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramsKeeper params.Keeper, paramStore params.Store, ck bank.Keeper, ds sdk.DelegationSet, codespace sdk.CodespaceType) Keeper {
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramsKeeper params.Keeper, paramSpace params.Subspace, ck bank.Keeper, ds sdk.DelegationSet, codespace sdk.CodespaceType) Keeper {
return Keeper{
storeKey: key,
paramsKeeper: paramsKeeper,
paramStore: paramStore,
paramSpace: paramSpace.WithTypeTable(ParamTypeTable()),
ck: ck,
ds: ds,
vs: ds.GetValidatorSet(),
@ -228,7 +228,7 @@ func (keeper Keeper) activateVotingPeriod(ctx sdk.Context, proposal Proposal) {
// nolint: errcheck
func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure {
var depositProcedure DepositProcedure
keeper.paramStore.Get(ctx, ParamStoreKeyDepositProcedure, &depositProcedure)
keeper.paramSpace.Get(ctx, ParamStoreKeyDepositProcedure, &depositProcedure)
return depositProcedure
}
@ -236,7 +236,7 @@ func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure {
// nolint: errcheck
func (keeper Keeper) GetVotingProcedure(ctx sdk.Context) VotingProcedure {
var votingProcedure VotingProcedure
keeper.paramStore.Get(ctx, ParamStoreKeyVotingProcedure, &votingProcedure)
keeper.paramSpace.Get(ctx, ParamStoreKeyVotingProcedure, &votingProcedure)
return votingProcedure
}
@ -244,23 +244,23 @@ func (keeper Keeper) GetVotingProcedure(ctx sdk.Context) VotingProcedure {
// nolint: errcheck
func (keeper Keeper) GetTallyingProcedure(ctx sdk.Context) TallyingProcedure {
var tallyingProcedure TallyingProcedure
keeper.paramStore.Get(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure)
keeper.paramSpace.Get(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure)
return tallyingProcedure
}
// nolint: errcheck
func (keeper Keeper) setDepositProcedure(ctx sdk.Context, depositProcedure DepositProcedure) {
keeper.paramStore.Set(ctx, ParamStoreKeyDepositProcedure, &depositProcedure)
keeper.paramSpace.Set(ctx, ParamStoreKeyDepositProcedure, &depositProcedure)
}
// nolint: errcheck
func (keeper Keeper) setVotingProcedure(ctx sdk.Context, votingProcedure VotingProcedure) {
keeper.paramStore.Set(ctx, ParamStoreKeyVotingProcedure, &votingProcedure)
keeper.paramSpace.Set(ctx, ParamStoreKeyVotingProcedure, &votingProcedure)
}
// nolint: errcheck
func (keeper Keeper) setTallyingProcedure(ctx sdk.Context, tallyingProcedure TallyingProcedure) {
keeper.paramStore.Set(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure)
keeper.paramSpace.Set(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure)
}
// =====================================================

View File

@ -30,9 +30,9 @@ func TestGovWithRandomMessages(t *testing.T) {
paramKey := sdk.NewKVStoreKey("params")
paramTKey := sdk.NewTransientStoreKey("transient_params")
paramKeeper := params.NewKeeper(mapp.Cdc, paramKey, paramTKey)
stakeKeeper := stake.NewKeeper(mapp.Cdc, stakeKey, stakeTKey, bankKeeper, paramKeeper.Substore(stake.DefaultParamspace, stake.ParamTable()), stake.DefaultCodespace)
stakeKeeper := stake.NewKeeper(mapp.Cdc, stakeKey, stakeTKey, bankKeeper, paramKeeper.Subspace(stake.DefaultParamspace), stake.DefaultCodespace)
govKey := sdk.NewKVStoreKey("gov")
govKeeper := gov.NewKeeper(mapp.Cdc, govKey, paramKeeper, paramKeeper.Substore(gov.DefaultParamspace, gov.ParamTable()), bankKeeper, stakeKeeper, gov.DefaultCodespace)
govKeeper := gov.NewKeeper(mapp.Cdc, govKey, paramKeeper, paramKeeper.Subspace(gov.DefaultParamspace), bankKeeper, stakeKeeper, gov.DefaultCodespace)
mapp.Router().AddRoute("gov", gov.NewHandler(govKeeper))
mapp.SetEndBlocker(func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
gov.EndBlocker(ctx, govKeeper)

View File

@ -34,8 +34,8 @@ func getMockApp(t *testing.T, numGenAccs int) (*mock.App, Keeper, stake.Keeper,
pk := params.NewKeeper(mapp.Cdc, keyGlobalParams, tkeyGlobalParams)
ck := bank.NewBaseKeeper(mapp.AccountMapper)
sk := stake.NewKeeper(mapp.Cdc, keyStake, tkeyStake, ck, pk.Substore(stake.DefaultParamspace, stake.ParamTable()), mapp.RegisterCodespace(stake.DefaultCodespace))
keeper := NewKeeper(mapp.Cdc, keyGov, pk, pk.Substore("testgov", ParamTable()), ck, sk, DefaultCodespace)
sk := stake.NewKeeper(mapp.Cdc, keyStake, tkeyStake, ck, pk.Subspace(stake.DefaultParamspace), mapp.RegisterCodespace(stake.DefaultCodespace))
keeper := NewKeeper(mapp.Cdc, keyGov, pk, pk.Subspace("testgov"), ck, sk, DefaultCodespace)
mapp.Router().AddRoute("gov", NewHandler(keeper))

View File

@ -30,7 +30,7 @@ recommended to use the same name with the module's.
cdc *wire.Codec
key sdk.StoreKey
ps params.Store
ps params.Subspace
}
Pass a params.Store to NewKeeper with DefaultParamSpace (or another)

View File

@ -4,7 +4,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params/store"
"github.com/cosmos/cosmos-sdk/x/params/subspace"
)
// Keeper of the global paramstore
@ -13,7 +13,7 @@ type Keeper struct {
key sdk.StoreKey
tkey sdk.StoreKey
stores map[string]*Store
spaces map[string]*Subspace
}
// NewKeeper constructs a params keeper
@ -23,35 +23,35 @@ func NewKeeper(cdc *codec.Codec, key *sdk.KVStoreKey, tkey *sdk.TransientStoreKe
key: key,
tkey: tkey,
stores: make(map[string]*Store),
spaces: make(map[string]*Subspace),
}
return k
}
// Allocate substore used for keepers
func (k Keeper) Substore(storename string, table Table) Store {
_, ok := k.stores[storename]
// Allocate subspace used for keepers
func (k Keeper) Subspace(spacename string) Subspace {
_, ok := k.spaces[spacename]
if ok {
panic("substore already occupied")
panic("subspace already occupied")
}
if storename == "" {
panic("cannot use empty string for substore")
if spacename == "" {
panic("cannot use empty string for subspace")
}
store := store.NewStore(k.cdc, k.key, k.tkey, storename, table)
space := subspace.NewSubspace(k.cdc, k.key, k.tkey, spacename)
k.stores[storename] = &store
k.spaces[spacename] = &space
return store
return space
}
// Get existing substore from keeper
func (k Keeper) GetSubstore(storename string) (Store, bool) {
store, ok := k.stores[storename]
func (k Keeper) GetSubspace(storename string) (Subspace, bool) {
space, ok := k.spaces[storename]
if !ok {
return Store{}, false
return Subspace{}, false
}
return *store, ok
return *space, ok
}

View File

@ -53,7 +53,7 @@ func TestKeeper(t *testing.T) {
{"key7", 9058701},
}
table := NewTable(
table := NewTypeTable(
[]byte("key1"), int64(0),
[]byte("key2"), int64(0),
[]byte("key3"), int64(0),
@ -68,7 +68,7 @@ func TestKeeper(t *testing.T) {
skey := sdk.NewKVStoreKey("test")
tkey := sdk.NewTransientStoreKey("transient_test")
ctx := defaultContext(skey, tkey)
store := NewKeeper(codec.New(), skey, tkey).Substore("test", table)
store := NewKeeper(codec.New(), skey, tkey).Subspace("test").WithTypeTable(table)
for i, kv := range kvs {
require.NotPanics(t, func() { store.Set(ctx, []byte(kv.key), kv.param) }, "store.Set panics, tc #%d", i)
@ -125,7 +125,7 @@ func TestGet(t *testing.T) {
{"struct", s{1}, s{0}, new(s)},
}
table := NewTable(
table := NewTypeTable(
[]byte("string"), string(""),
[]byte("bool"), bool(false),
[]byte("int16"), int16(0),
@ -140,7 +140,7 @@ func TestGet(t *testing.T) {
[]byte("struct"), s{},
)
store := keeper.Substore("test", table)
store := keeper.Subspace("test").WithTypeTable(table)
for i, kv := range kvs {
require.False(t, store.Modified(ctx, []byte(kv.key)), "store.Modified returns true before setting, tc #%d", i)

View File

@ -1,19 +0,0 @@
package params
import (
"github.com/cosmos/cosmos-sdk/x/params/store"
)
// re-export types from store
type (
Store = store.Store
ReadOnlyStore = store.ReadOnlyStore
ParamStruct = store.ParamStruct
KeyValuePairs = store.KeyValuePairs
Table = store.Table
)
// re-export functions from store
func NewTable(keytypes ...interface{}) Table {
return store.NewTable(keytypes...)
}

19
x/params/subspace.go Normal file
View File

@ -0,0 +1,19 @@
package params
import (
"github.com/cosmos/cosmos-sdk/x/params/subspace"
)
// re-export types from subspace
type (
Subspace = subspace.Subspace
ReadOnlySubspace = subspace.ReadOnlySubspace
ParamSet = subspace.ParamSet
KeyValuePairs = subspace.KeyValuePairs
TypeTable = subspace.TypeTable
)
// re-export functions from subspace
func NewTypeTable(keytypes ...interface{}) TypeTable {
return subspace.NewTypeTable(keytypes...)
}

View File

@ -1,4 +1,4 @@
package store
package subspace
/*
To prevent namespace collision between consumer modules, we define type

View File

@ -1,6 +1,6 @@
package store
package subspace
// Used for associating paramstore key and field of param structs
// Used for associating paramsubspace key and field of param structs
type KeyValuePair struct {
Key []byte
Value interface{}
@ -10,6 +10,6 @@ type KeyValuePair struct {
type KeyValuePairs []KeyValuePair
// Interface for structs containing parameters for a module
type ParamStruct interface {
type ParamSet interface {
KeyValuePairs() KeyValuePairs
}

View File

@ -1,4 +1,4 @@
package store
package subspace
import (
"reflect"
@ -7,55 +7,73 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Additional capicity to be allocated for Store.space
// Additional capicity to be allocated for Subspace.name
// So we don't have to allocate extra space each time appending to the key
const extraKeyCap = 20
// Individual parameter store for each keeper
// Transient store persists for a block, so we use it for
// recording whether the parameter has been changed or not
type Store struct {
type Subspace struct {
cdc *codec.Codec
key sdk.StoreKey // []byte -> []byte, stores parameter
tkey sdk.StoreKey // []byte -> bool, stores parameter change
space []byte
name []byte
table Table
table TypeTable
}
// NewStore constructs a store with namestore
func NewStore(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, space string, table Table) (res Store) {
res = Store{
// NewSubspace constructs a store with namestore
func NewSubspace(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, name string) (res Subspace) {
res = Subspace{
cdc: cdc,
key: key,
tkey: tkey,
}
namebz := []byte(name)
res.name = make([]byte, len(namebz), len(namebz)+extraKeyCap)
copy(res.name, namebz)
return
}
// WithTypeTable initializes TypeTable and returns modified Subspace
func (s Subspace) WithTypeTable(table TypeTable) (res Subspace) {
if table == nil {
panic("SetTypeTable() called with nil TypeTable")
}
if s.table != nil {
panic("SetTypeTable() called on initialized Subspace")
}
res = Subspace{
cdc: s.cdc,
key: s.key,
tkey: s.tkey,
name: s.name,
table: table,
}
spacebz := []byte(space)
res.space = make([]byte, len(spacebz), len(spacebz)+extraKeyCap)
copy(res.space, spacebz)
return
}
// Returns a KVStore identical with ctx.KVStore(s.key).Prefix()
func (s Store) kvStore(ctx sdk.Context) sdk.KVStore {
func (s Subspace) kvStore(ctx sdk.Context) sdk.KVStore {
// append here is safe, appends within a function won't cause
// weird side effects when its singlethreaded
return ctx.KVStore(s.key).Prefix(append(s.space, '/'))
return ctx.KVStore(s.key).Prefix(append(s.name, '/'))
}
// Returns a KVStore identical with ctx.TransientStore(s.tkey).Prefix()
func (s Store) transientStore(ctx sdk.Context) sdk.KVStore {
func (s Subspace) transientStore(ctx sdk.Context) sdk.KVStore {
// append here is safe, appends within a function won't cause
// weird side effects when its singlethreaded
return ctx.TransientStore(s.tkey).Prefix(append(s.space, '/'))
return ctx.TransientStore(s.tkey).Prefix(append(s.name, '/'))
}
// Get parameter from store
func (s Store) Get(ctx sdk.Context, key []byte, ptr interface{}) {
func (s Subspace) Get(ctx sdk.Context, key []byte, ptr interface{}) {
store := s.kvStore(ctx)
bz := store.Get(key)
err := s.cdc.UnmarshalJSON(bz, ptr)
@ -65,7 +83,7 @@ func (s Store) Get(ctx sdk.Context, key []byte, ptr interface{}) {
}
// GetIfExists do not modify ptr if the stored parameter is nil
func (s Store) GetIfExists(ctx sdk.Context, key []byte, ptr interface{}) {
func (s Subspace) GetIfExists(ctx sdk.Context, key []byte, ptr interface{}) {
store := s.kvStore(ctx)
bz := store.Get(key)
if bz == nil {
@ -78,26 +96,26 @@ func (s Store) GetIfExists(ctx sdk.Context, key []byte, ptr interface{}) {
}
// Get raw bytes of parameter from store
func (s Store) GetRaw(ctx sdk.Context, key []byte) []byte {
func (s Subspace) GetRaw(ctx sdk.Context, key []byte) []byte {
store := s.kvStore(ctx)
return store.Get(key)
}
// Check if the parameter is set in the store
func (s Store) Has(ctx sdk.Context, key []byte) bool {
func (s Subspace) Has(ctx sdk.Context, key []byte) bool {
store := s.kvStore(ctx)
return store.Has(key)
}
// Returns true if the parameter is set in the block
func (s Store) Modified(ctx sdk.Context, key []byte) bool {
func (s Subspace) Modified(ctx sdk.Context, key []byte) bool {
tstore := s.transientStore(ctx)
return tstore.Has(key)
}
// Set parameter, return error if stored parameter has different type from input
// Also set to the transient store to record change
func (s Store) Set(ctx sdk.Context, key []byte, param interface{}) {
func (s Subspace) Set(ctx sdk.Context, key []byte, param interface{}) {
store := s.kvStore(ctx)
ty, ok := s.table[string(key)]
@ -122,17 +140,18 @@ func (s Store) Set(ctx sdk.Context, key []byte, param interface{}) {
tstore := s.transientStore(ctx)
tstore.Set(key, []byte{})
}
// Get to ParamStruct
func (s Store) GetStruct(ctx sdk.Context, ps ParamStruct) {
// Get to ParamSet
func (s Subspace) GetParamSet(ctx sdk.Context, ps ParamSet) {
for _, pair := range ps.KeyValuePairs() {
s.Get(ctx, pair.Key, pair.Value)
}
}
// Set from ParamStruct
func (s Store) SetStruct(ctx sdk.Context, ps ParamStruct) {
// Set from ParamSet
func (s Subspace) SetParamSet(ctx sdk.Context, ps ParamSet) {
for _, pair := range ps.KeyValuePairs() {
// pair.Field is a pointer to the field, so indirecting the ptr.
// go-amino automatically handles it but just for sure,
@ -143,37 +162,37 @@ func (s Store) SetStruct(ctx sdk.Context, ps ParamStruct) {
}
}
// Returns internal namespace
func (s Store) Space() string {
return string(s.space)
// Returns name of Subspace
func (s Subspace) Name() string {
return string(s.name)
}
// Wrapper of Store, provides immutable functions only
type ReadOnlyStore struct {
s Store
// Wrapper of Subspace, provides immutable functions only
type ReadOnlySubspace struct {
s Subspace
}
// Exposes Get
func (ros ReadOnlyStore) Get(ctx sdk.Context, key []byte, ptr interface{}) {
func (ros ReadOnlySubspace) Get(ctx sdk.Context, key []byte, ptr interface{}) {
ros.s.Get(ctx, key, ptr)
}
// Exposes GetRaw
func (ros ReadOnlyStore) GetRaw(ctx sdk.Context, key []byte) []byte {
func (ros ReadOnlySubspace) GetRaw(ctx sdk.Context, key []byte) []byte {
return ros.s.GetRaw(ctx, key)
}
// Exposes Has
func (ros ReadOnlyStore) Has(ctx sdk.Context, key []byte) bool {
func (ros ReadOnlySubspace) Has(ctx sdk.Context, key []byte) bool {
return ros.s.Has(ctx, key)
}
// Exposes Modified
func (ros ReadOnlyStore) Modified(ctx sdk.Context, key []byte) bool {
func (ros ReadOnlySubspace) Modified(ctx sdk.Context, key []byte) bool {
return ros.s.Modified(ctx, key)
}
// Exposes Space
func (ros ReadOnlyStore) Space() string {
return ros.s.Space()
func (ros ReadOnlySubspace) Name() string {
return ros.s.Name()
}

View File

@ -1,16 +1,16 @@
package store
package subspace
import (
"reflect"
)
// Table stores appropriate type for each parameter key
type Table map[string]reflect.Type
// TypeTable subspaces appropriate type for each parameter key
type TypeTable map[string]reflect.Type
// Constructs new table
func NewTable(keytypes ...interface{}) (res Table) {
func NewTypeTable(keytypes ...interface{}) (res TypeTable) {
if len(keytypes)%2 != 0 {
panic("odd number arguments in NewTypeTable")
panic("odd number arguments in NewTypeTypeTable")
}
res = make(map[string]reflect.Type)
@ -23,7 +23,7 @@ func NewTable(keytypes ...interface{}) (res Table) {
}
// Register single key-type pair
func (t Table) RegisterType(key []byte, ty interface{}) Table {
func (t TypeTable) RegisterType(key []byte, ty interface{}) TypeTable {
keystr := string(key)
if _, ok := t[keystr]; ok {
panic("duplicate parameter key")
@ -41,8 +41,8 @@ func (t Table) RegisterType(key []byte, ty interface{}) Table {
return t
}
// Register multiple pairs from ParamStruct
func (t Table) RegisterParamStruct(ps ParamStruct) Table {
// Register multiple pairs from ParamSet
func (t TypeTable) RegisterParamSet(ps ParamSet) TypeTable {
for _, kvp := range ps.KeyValuePairs() {
t = t.RegisterType(kvp.Key, kvp.Value)
}

View File

@ -1,4 +1,4 @@
package store
package subspace
import (
"os"
@ -21,7 +21,7 @@ const (
)
// Returns components for testing
func DefaultTestComponents(t *testing.T, table Table) (sdk.Context, Store, func() sdk.CommitID) {
func DefaultTestComponents(t *testing.T, table TypeTable) (sdk.Context, Subspace, func() sdk.CommitID) {
cdc := codec.New()
key := sdk.NewKVStoreKey("params")
tkey := sdk.NewTransientStoreKey("tparams")
@ -34,7 +34,7 @@ func DefaultTestComponents(t *testing.T, table Table) (sdk.Context, Store, func(
err := ms.LoadLatestVersion()
require.Nil(t, err)
ctx := sdk.NewContext(ms, abci.Header{}, false, log.NewTMLogger(os.Stdout))
store := NewStore(cdc, key, tkey, TestParamStore, table)
subspace := NewSubspace(cdc, key, tkey, TestParamStore).WithTypeTable(table)
return ctx, store, ms.Commit
return ctx, subspace, ms.Commit
}

View File

@ -34,8 +34,8 @@ func getMockApp(t *testing.T) (*mock.App, stake.Keeper, Keeper) {
bankKeeper := bank.NewBaseKeeper(mapp.AccountMapper)
paramsKeeper := params.NewKeeper(mapp.Cdc, keyParams, tkeyParams)
stakeKeeper := stake.NewKeeper(mapp.Cdc, keyStake, tkeyStake, bankKeeper, paramsKeeper.Substore(stake.DefaultParamspace, stake.ParamTable()), mapp.RegisterCodespace(stake.DefaultCodespace))
keeper := NewKeeper(mapp.Cdc, keySlashing, stakeKeeper, paramsKeeper.Substore(DefaultParamspace, ParamTable()), mapp.RegisterCodespace(DefaultCodespace))
stakeKeeper := stake.NewKeeper(mapp.Cdc, keyStake, tkeyStake, bankKeeper, paramsKeeper.Subspace(stake.DefaultParamspace), mapp.RegisterCodespace(stake.DefaultCodespace))
keeper := NewKeeper(mapp.Cdc, keySlashing, stakeKeeper, paramsKeeper.Subspace(DefaultParamspace), mapp.RegisterCodespace(DefaultCodespace))
mapp.Router().AddRoute("stake", stake.NewHandler(stakeKeeper))
mapp.Router().AddRoute("slashing", NewHandler(keeper))

View File

@ -24,5 +24,5 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState, sdata types.
keeper.addPubkey(ctx, validator.GetConsPubKey())
}
keeper.paramstore.SetStruct(ctx, &data.Params)
keeper.paramspace.SetParamSet(ctx, &data.Params)
}

View File

@ -19,19 +19,19 @@ type Keeper struct {
storeKey sdk.StoreKey
cdc *codec.Codec
validatorSet sdk.ValidatorSet
paramstore params.Store
paramspace params.Subspace
// codespace
codespace sdk.CodespaceType
}
// NewKeeper creates a slashing keeper
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, vs sdk.ValidatorSet, paramstore params.Store, codespace sdk.CodespaceType) Keeper {
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, vs sdk.ValidatorSet, paramspace params.Subspace, codespace sdk.CodespaceType) Keeper {
keeper := Keeper{
storeKey: key,
cdc: cdc,
validatorSet: vs,
paramstore: paramstore,
paramspace: paramspace.WithTypeTable(ParamTypeTable()),
codespace: codespace,
}
return keeper

View File

@ -23,9 +23,9 @@ var (
KeySlashFractionDowntime = []byte("SlashFractionDowntime")
)
// ParamTable for slashing module
func ParamTable() params.Table {
return params.NewTable().RegisterParamStruct(&Params{})
// ParamTypeTable for slashing module
func ParamTypeTable() params.TypeTable {
return params.NewTypeTable().RegisterParamSet(&Params{})
}
// Params - used for initializing default parameter for slashing at genesis
@ -79,44 +79,44 @@ func DefaultParams() Params {
// MaxEvidenceAge - Max age for evidence - 21 days (3 weeks)
// MaxEvidenceAge = 60 * 60 * 24 * 7 * 3
func (k Keeper) MaxEvidenceAge(ctx sdk.Context) (res time.Duration) {
k.paramstore.Get(ctx, KeyMaxEvidenceAge, &res)
k.paramspace.Get(ctx, KeyMaxEvidenceAge, &res)
return
}
// SignedBlocksWindow - sliding window for downtime slashing
func (k Keeper) SignedBlocksWindow(ctx sdk.Context) (res int64) {
k.paramstore.Get(ctx, KeySignedBlocksWindow, &res)
k.paramspace.Get(ctx, KeySignedBlocksWindow, &res)
return
}
// Downtime slashing thershold - default 50% of the SignedBlocksWindow
func (k Keeper) MinSignedPerWindow(ctx sdk.Context) int64 {
var minSignedPerWindow sdk.Dec
k.paramstore.Get(ctx, KeyMinSignedPerWindow, &minSignedPerWindow)
k.paramspace.Get(ctx, KeyMinSignedPerWindow, &minSignedPerWindow)
signedBlocksWindow := k.SignedBlocksWindow(ctx)
return sdk.NewDec(signedBlocksWindow).Mul(minSignedPerWindow).RoundInt64()
}
// Double-sign unbond duration
func (k Keeper) DoubleSignUnbondDuration(ctx sdk.Context) (res time.Duration) {
k.paramstore.Get(ctx, KeyDoubleSignUnbondDuration, &res)
k.paramspace.Get(ctx, KeyDoubleSignUnbondDuration, &res)
return
}
// Downtime unbond duration
func (k Keeper) DowntimeUnbondDuration(ctx sdk.Context) (res time.Duration) {
k.paramstore.Get(ctx, KeyDowntimeUnbondDuration, &res)
k.paramspace.Get(ctx, KeyDowntimeUnbondDuration, &res)
return
}
// SlashFractionDoubleSign - currently default 5%
func (k Keeper) SlashFractionDoubleSign(ctx sdk.Context) (res sdk.Dec) {
k.paramstore.Get(ctx, KeySlashFractionDoubleSign, &res)
k.paramspace.Get(ctx, KeySlashFractionDoubleSign, &res)
return
}
// SlashFractionDowntime - currently default 1%
func (k Keeper) SlashFractionDowntime(ctx sdk.Context) (res sdk.Dec) {
k.paramstore.Get(ctx, KeySlashFractionDowntime, &res)
k.paramspace.Get(ctx, KeySlashFractionDowntime, &res)
return
}

View File

@ -49,7 +49,7 @@ func createTestCodec() *codec.Codec {
return cdc
}
func createTestInput(t *testing.T, defaults Params) (sdk.Context, bank.Keeper, stake.Keeper, params.Store, Keeper) {
func createTestInput(t *testing.T, defaults Params) (sdk.Context, bank.Keeper, stake.Keeper, params.Subspace, Keeper) {
keyAcc := sdk.NewKVStoreKey("acc")
keyStake := sdk.NewKVStoreKey("stake")
tkeyStake := sdk.NewTransientStoreKey("transient_stake")
@ -72,7 +72,7 @@ func createTestInput(t *testing.T, defaults Params) (sdk.Context, bank.Keeper, s
ck := bank.NewBaseKeeper(accountMapper)
paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams)
sk := stake.NewKeeper(cdc, keyStake, tkeyStake, ck, paramsKeeper.Substore(stake.DefaultParamspace, stake.ParamTable()), stake.DefaultCodespace)
sk := stake.NewKeeper(cdc, keyStake, tkeyStake, ck, paramsKeeper.Subspace(stake.DefaultParamspace), stake.DefaultCodespace)
genesis := stake.DefaultGenesisState()
genesis.Pool.LooseTokens = sdk.NewDec(initCoins.MulRaw(int64(len(addrs))).Int64())
@ -86,7 +86,7 @@ func createTestInput(t *testing.T, defaults Params) (sdk.Context, bank.Keeper, s
})
}
require.Nil(t, err)
paramstore := paramsKeeper.Substore(DefaultParamspace, ParamTable())
paramstore := paramsKeeper.Subspace(DefaultParamspace)
keeper := NewKeeper(cdc, keySlashing, sk, paramstore, DefaultCodespace)
require.NotPanics(t, func() {

View File

@ -45,7 +45,7 @@ func getMockApp(t *testing.T) (*mock.App, Keeper) {
bankKeeper := bank.NewBaseKeeper(mApp.AccountMapper)
pk := params.NewKeeper(mApp.Cdc, keyParams, tkeyParams)
keeper := NewKeeper(mApp.Cdc, keyStake, tkeyStake, bankKeeper, pk.Substore(DefaultParamspace, ParamTable()), mApp.RegisterCodespace(DefaultCodespace))
keeper := NewKeeper(mApp.Cdc, keyStake, tkeyStake, bankKeeper, pk.Subspace(DefaultParamspace), mApp.RegisterCodespace(DefaultCodespace))
mApp.Router().AddRoute("stake", NewHandler(keeper))
mApp.SetEndBlocker(getEndBlocker(keeper))

View File

@ -16,19 +16,19 @@ type Keeper struct {
cdc *codec.Codec
bankKeeper bank.Keeper
hooks sdk.StakingHooks
paramstore params.Store
paramstore params.Subspace
// codespace
codespace sdk.CodespaceType
}
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ck bank.Keeper, paramstore params.Store, codespace sdk.CodespaceType) Keeper {
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ck bank.Keeper, paramstore params.Subspace, codespace sdk.CodespaceType) Keeper {
keeper := Keeper{
storeKey: key,
storeTKey: tkey,
cdc: cdc,
bankKeeper: ck,
paramstore: paramstore,
paramstore: paramstore.WithTypeTable(ParamTypeTable()),
hooks: nil,
codespace: codespace,
}

View File

@ -14,8 +14,8 @@ const (
)
// ParamTable for stake module
func ParamTable() params.Table {
return params.NewTable().RegisterParamStruct(&types.Params{})
func ParamTypeTable() params.TypeTable {
return params.NewTypeTable().RegisterParamSet(&types.Params{})
}
// InflationRateChange - Maximum annual change in inflation rate
@ -74,5 +74,5 @@ func (k Keeper) GetParams(ctx sdk.Context) (res types.Params) {
// set the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetStruct(ctx, &params)
k.paramstore.SetParamSet(ctx, &params)
}

View File

@ -115,7 +115,7 @@ func CreateTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context
ck := bank.NewBaseKeeper(accountMapper)
pk := params.NewKeeper(cdc, keyParams, tkeyParams)
keeper := NewKeeper(cdc, keyStake, tkeyStake, ck, pk.Substore("stake", ParamTable()), types.DefaultCodespace)
keeper := NewKeeper(cdc, keyStake, tkeyStake, ck, pk.Subspace(DefaultParamspace), types.DefaultCodespace)
keeper.SetPool(ctx, types.InitialPool())
keeper.SetParams(ctx, types.DefaultParams())
keeper.InitIntraTxCounter(ctx)

View File

@ -27,7 +27,7 @@ func TestStakeWithRandomMessages(t *testing.T) {
paramsKey := sdk.NewKVStoreKey("params")
paramsTKey := sdk.NewTransientStoreKey("transient_params")
paramstore := params.NewKeeper(mapp.Cdc, paramsKey, paramsTKey).Substore(stake.DefaultParamspace, stake.ParamTable())
paramstore := params.NewKeeper(mapp.Cdc, paramsKey, paramsTKey).Subspace(stake.DefaultParamspace)
stakeKeeper := stake.NewKeeper(mapp.Cdc, stakeKey, stakeTKey, bankKeeper, paramstore, stake.DefaultCodespace)
mapp.Router().AddRoute("stake", stake.NewHandler(stakeKeeper))
mapp.SetEndBlocker(func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {

View File

@ -2,8 +2,6 @@
package stake
import (
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/stake/keeper"
"github.com/cosmos/cosmos-sdk/x/stake/querier"
"github.com/cosmos/cosmos-sdk/x/stake/tags"
@ -160,8 +158,3 @@ var (
TagMoniker = tags.Moniker
TagIdentity = tags.Identity
)
// nolint - reexport
func ParamTable() params.Table {
return keeper.ParamTable()
}

View File

@ -33,7 +33,7 @@ var (
KeyBondDenom = []byte("BondDenom")
)
var _ params.ParamStruct = (*Params)(nil)
var _ params.ParamSet = (*Params)(nil)
// Params defines the high level settings for staking
type Params struct {
@ -48,7 +48,7 @@ type Params struct {
BondDenom string `json:"bond_denom"` // bondable coin denomination
}
// Implements params.ParamStruct
// Implements params.ParamSet
func (p *Params) KeyValuePairs() params.KeyValuePairs {
return params.KeyValuePairs{
{KeyInflationRateChange, &p.InflationRateChange},