Skip to content

Commit

Permalink
rename store subspace, add WithTypeTable
Browse files Browse the repository at this point in the history
  • Loading branch information
mossid committed Oct 10, 2018
1 parent 0397540 commit 5c92a54
Show file tree
Hide file tree
Showing 27 changed files with 165 additions and 151 deletions.
6 changes: 3 additions & 3 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)

Expand All @@ -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),
)

Expand Down
8 changes: 5 additions & 3 deletions cmd/gaia/app/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
22 changes: 11 additions & 11 deletions x/gov/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand All @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -228,39 +228,39 @@ 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
}

// Returns the current Voting Procedure from the global param store
// 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
}

// Returns the current Tallying Procedure from the global param store
// 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)
}

// =====================================================
Expand Down
4 changes: 2 additions & 2 deletions x/gov/simulation/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x/gov/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion x/params/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 16 additions & 16 deletions x/params/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions x/params/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
Expand Down Expand Up @@ -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),
Expand All @@ -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)
Expand Down
19 changes: 0 additions & 19 deletions x/params/store.go

This file was deleted.

19 changes: 19 additions & 0 deletions x/params/subspace.go
Original file line number Diff line number Diff line change
@@ -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...)
}
2 changes: 1 addition & 1 deletion x/params/store/doc.go → x/params/subspace/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store
package subspace

/*
To prevent namespace collision between consumer modules, we define type
Expand Down
6 changes: 3 additions & 3 deletions x/params/store/pair.go → x/params/subspace/pair.go
Original file line number Diff line number Diff line change
@@ -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{}
Expand All @@ -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
}
Loading

0 comments on commit 5c92a54

Please sign in to comment.