diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 995d3a6a5..bbdf2d9dd 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -88,12 +88,41 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio // add handlers app.bankKeeper = bank.NewBaseKeeper(app.accountMapper) - app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams) - app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.tkeyStake, app.bankKeeper, app.paramsKeeper.Substore(stake.DefaultParamspace), app.RegisterCodespace(stake.DefaultCodespace)) - app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Substore(slashing.DefaultParamspace), app.RegisterCodespace(slashing.DefaultCodespace)) - app.stakeKeeper = app.stakeKeeper.WithHooks(app.slashingKeeper.Hooks()) - app.govKeeper = gov.NewKeeper(app.cdc, app.keyGov, app.paramsKeeper, app.paramsKeeper.Substore(gov.DefaultParamspace), app.bankKeeper, app.stakeKeeper, app.RegisterCodespace(gov.DefaultCodespace)) - app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, app.keyFeeCollection) + + app.paramsKeeper = params.NewKeeper( + app.cdc, + app.keyParams, app.tkeyParams, + ) + + app.stakeKeeper = stake.NewKeeper( + app.cdc, + app.keyStake, app.tkeyStake, + app.bankKeeper, app.paramsKeeper.Substore(stake.DefaultParamspace), + app.RegisterCodespace(stake.DefaultCodespace), + ) + + app.slashingKeeper = slashing.NewKeeper( + app.cdc, + app.keySlashing, + app.stakeKeeper, app.paramsKeeper.Substore(slashing.DefaultParamspace), + app.RegisterCodespace(slashing.DefaultCodespace), + ) + + app.stakeKeeper = app.stakeKeeper.WithHooks( + app.slashingKeeper.Hooks(), + ) + + app.govKeeper = gov.NewKeeper( + app.cdc, + app.keyGov, + app.paramsKeeper, app.paramsKeeper.Substore(gov.DefaultParamspace), app.bankKeeper, app.stakeKeeper, + app.RegisterCodespace(gov.DefaultCodespace), + ) + + app.feeCollectionKeeper = auth.NewFeeCollectionKeeper( + app.cdc, + app.keyFeeCollection, + ) // register message routes app.Router(). diff --git a/store/prefixstore_test.go b/store/prefixstore_test.go index 0b5503d17..ca2154fcc 100644 --- a/store/prefixstore_test.go +++ b/store/prefixstore_test.go @@ -119,7 +119,7 @@ func TestPrefixStoreIterate(t *testing.T) { pIter.Close() } -func mutateFirstByte(bz []byte) { +func incFirstByte(bz []byte) { if bz[0] == byte(255) { bz[0] = byte(0) return @@ -133,15 +133,15 @@ func TestCloneAppend(t *testing.T) { bz := cloneAppend(kvp.key, kvp.value) require.Equal(t, bz, append(kvp.key, kvp.value...)) - mutateFirstByte(bz) + incFirstByte(bz) require.NotEqual(t, bz, append(kvp.key, kvp.value...)) bz = cloneAppend(kvp.key, kvp.value) - mutateFirstByte(kvp.key) + incFirstByte(kvp.key) require.NotEqual(t, bz, append(kvp.key, kvp.value...)) bz = cloneAppend(kvp.key, kvp.value) - mutateFirstByte(kvp.value) + incFirstByte(kvp.value) require.NotEqual(t, bz, append(kvp.key, kvp.value...)) } } diff --git a/x/gov/keeper.go b/x/gov/keeper.go index 39a98336c..0421d17ee 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -22,10 +22,10 @@ var ( // Governance Keeper type Keeper struct { // The reference to the Param Keeper to get and set Global Params - pk params.Keeper + paramsKeeper params.Keeper // The reference to the Paramstore to get and set gov specific params - ps params.Store + paramStore params.Store // The reference to the CoinKeeper to modify balances ck bank.Keeper @@ -51,16 +51,16 @@ 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, pk params.Keeper, ps params.Store, ck bank.Keeper, ds sdk.DelegationSet, codespace sdk.CodespaceType) Keeper { +func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramsKeeper params.Keeper, paramStore params.Store, ck bank.Keeper, ds sdk.DelegationSet, codespace sdk.CodespaceType) Keeper { return Keeper{ - storeKey: key, - pk: pk, - ps: ps, - ck: ck, - ds: ds, - vs: ds.GetValidatorSet(), - cdc: cdc, - codespace: codespace, + storeKey: key, + paramsKeeper: paramsKeeper, + paramStore: paramStore, + ck: ck, + ds: ds, + vs: ds.GetValidatorSet(), + cdc: cdc, + codespace: codespace, } } @@ -219,7 +219,7 @@ func (keeper Keeper) activateVotingPeriod(ctx sdk.Context, proposal Proposal) { // nolint: errcheck func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure { var depositProcedure DepositProcedure - keeper.ps.Get(ctx, ParamStoreKeyDepositProcedure, &depositProcedure) + keeper.paramStore.Get(ctx, ParamStoreKeyDepositProcedure, &depositProcedure) return depositProcedure } @@ -227,7 +227,7 @@ func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure { // nolint: errcheck func (keeper Keeper) GetVotingProcedure(ctx sdk.Context) VotingProcedure { var votingProcedure VotingProcedure - keeper.ps.Get(ctx, ParamStoreKeyVotingProcedure, &votingProcedure) + keeper.paramStore.Get(ctx, ParamStoreKeyVotingProcedure, &votingProcedure) return votingProcedure } @@ -235,23 +235,23 @@ func (keeper Keeper) GetVotingProcedure(ctx sdk.Context) VotingProcedure { // nolint: errcheck func (keeper Keeper) GetTallyingProcedure(ctx sdk.Context) TallyingProcedure { var tallyingProcedure TallyingProcedure - keeper.ps.Get(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure) + keeper.paramStore.Get(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure) return tallyingProcedure } // nolint: errcheck func (keeper Keeper) setDepositProcedure(ctx sdk.Context, depositProcedure DepositProcedure) { - keeper.ps.Set(ctx, ParamStoreKeyDepositProcedure, &depositProcedure) + keeper.paramStore.Set(ctx, ParamStoreKeyDepositProcedure, &depositProcedure) } // nolint: errcheck func (keeper Keeper) setVotingProcedure(ctx sdk.Context, votingProcedure VotingProcedure) { - keeper.ps.Set(ctx, ParamStoreKeyVotingProcedure, &votingProcedure) + keeper.paramStore.Set(ctx, ParamStoreKeyVotingProcedure, &votingProcedure) } // nolint: errcheck func (keeper Keeper) setTallyingProcedure(ctx sdk.Context, tallyingProcedure TallyingProcedure) { - keeper.ps.Set(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure) + keeper.paramStore.Set(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure) } // ===================================================== diff --git a/x/params/store.go b/x/params/store.go index 19aa3038a..62c91da30 100644 --- a/x/params/store.go +++ b/x/params/store.go @@ -1,7 +1,6 @@ package params import ( - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/x/params/store" ) @@ -10,11 +9,5 @@ type ( Store = store.Store ReadOnlyStore = store.ReadOnlyStore ParamStruct = store.ParamStruct - KeyFieldPairs = store.KeyFieldPairs + KeyValuePairs = store.KeyValuePairs ) - -// UnmarshalParamsFromMap deserializes parameters from a given map. It returns -// an error upon failure. -func UnmarshalParamsFromMap(m map[string][]byte, cdc *codec.Codec, ps store.ParamStruct) error { - return store.UnmarshalParamsFromMap(m, cdc, ps) -} diff --git a/x/params/store/pair.go b/x/params/store/pair.go index 05350a5e8..fd1775fce 100644 --- a/x/params/store/pair.go +++ b/x/params/store/pair.go @@ -1,9 +1,5 @@ package store -import ( - "github.com/cosmos/cosmos-sdk/codec" -) - // Used for associating paramstore key and field of param structs type KeyFieldPair struct { Key []byte @@ -11,21 +7,9 @@ type KeyFieldPair struct { } // Slice of KeyFieldPair -type KeyFieldPairs []KeyFieldPair +type KeyValuePairs []KeyFieldPair // Interface for structs containing parameters for a module type ParamStruct interface { - KeyFieldPairs() KeyFieldPairs -} - -// Takes a map from key string to byte slice and -// unmarshalles it to ParamStruct -func UnmarshalParamsFromMap(m map[string][]byte, cdc *codec.Codec, ps ParamStruct) error { - for _, p := range ps.KeyFieldPairs() { - err := cdc.UnmarshalJSON(m[string(p.Key)], p.Field) - if err != nil { - return err - } - } - return nil + KeyValuePairs() KeyValuePairs } diff --git a/x/params/store/store.go b/x/params/store/store.go index 7fbc640a0..d29fedb01 100644 --- a/x/params/store/store.go +++ b/x/params/store/store.go @@ -8,6 +8,10 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +// Additional capicity to be allocated for Store.space +// 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 @@ -20,23 +24,30 @@ type Store struct { } // NewStore constructs a store with namestore -func NewStore(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, space string) Store { - return Store{ +func NewStore(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, space string) (res Store) { + res = Store{ cdc: cdc, key: key, tkey: tkey, - - space: []byte(space), } + + spacebz := []byte(space) + res.space = make([]byte, len(spacebz), len(spacebz)+extraKeyCap) + copy(res.space, spacebz) + return } -// Returns a KVStore identical with ctx,TransientStore(s.key).Prefix() +// Returns a KVStore identical with ctx.KVStore(s.key).Prefix() func (s Store) 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, '/')) } // Returns a KVStore identical with ctx.TransientStore(s.tkey).Prefix() func (s Store) 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, '/')) } @@ -123,14 +134,14 @@ func (s Store) SetRaw(ctx sdk.Context, key []byte, param []byte) { // Get to ParamStruct func (s Store) GetStruct(ctx sdk.Context, ps ParamStruct) { - for _, pair := range ps.KeyFieldPairs() { + for _, pair := range ps.KeyValuePairs() { s.Get(ctx, pair.Key, pair.Field) } } // Set from ParamStruct func (s Store) SetStruct(ctx sdk.Context, ps ParamStruct) { - for _, pair := range ps.KeyFieldPairs() { + 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, // since SetStruct is meant to be used in InitGenesis diff --git a/x/slashing/params.go b/x/slashing/params.go index 11f9406ca..f849fa6d6 100644 --- a/x/slashing/params.go +++ b/x/slashing/params.go @@ -28,15 +28,15 @@ type Params struct { MaxEvidenceAge time.Duration `json:"max-evidence-age"` SignedBlocksWindow int64 `json:"signed-blocks-window"` MinSignedPerWindow sdk.Dec `json:"min-signed-per-window"` - DoubleSignUnbondDuration time.Duration `json:"doublesign-unbond-duration"` + DoubleSignUnbondDuration time.Duration `json:"double-sign-unbond-duration"` DowntimeUnbondDuration time.Duration `json:"downtime-unbond-duration"` - SlashFractionDoubleSign sdk.Dec `json:"slash-fraction-doublesign"` + SlashFractionDoubleSign sdk.Dec `json:"slash-fraction-double-sign"` SlashFractionDowntime sdk.Dec `json:"slash-fraction-downtime"` } // Implements params.ParamStruct -func (p *Params) KeyFieldPairs() params.KeyFieldPairs { - return params.KeyFieldPairs{ +func (p *Params) KeyValuePairs() params.KeyValuePairs { + return params.KeyValuePairs{ {KeyMaxEvidenceAge, &p.MaxEvidenceAge}, {KeySignedBlocksWindow, &p.SignedBlocksWindow}, {KeyMinSignedPerWindow, &p.MinSignedPerWindow}, diff --git a/x/stake/types/params.go b/x/stake/types/params.go index ccf7272f8..ed224274a 100644 --- a/x/stake/types/params.go +++ b/x/stake/types/params.go @@ -25,6 +25,8 @@ var ( KeyBondDenom = []byte("BondDenom") ) +var _ params.ParamStruct = (*Params)(nil) + // Params defines the high level settings for staking type Params struct { InflationRateChange sdk.Dec `json:"inflation_rate_change"` // maximum annual change in inflation rate @@ -39,8 +41,8 @@ type Params struct { } // Implements params.ParamStruct -func (p *Params) KeyFieldPairs() params.KeyFieldPairs { - return params.KeyFieldPairs{ +func (p *Params) KeyValuePairs() params.KeyValuePairs { + return params.KeyValuePairs{ {KeyInflationRateChange, &p.InflationRateChange}, {KeyInflationMax, &p.InflationMax}, {KeyInflationMin, &p.InflationMin},