apply requests in progress

This commit is contained in:
mossid 2018-10-06 22:50:58 +09:00
parent 6978d283bf
commit 7d49675600
8 changed files with 85 additions and 66 deletions

View File

@ -88,12 +88,41 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
// add handlers // add handlers
app.bankKeeper = bank.NewBaseKeeper(app.accountMapper) 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.paramsKeeper = params.NewKeeper(
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Substore(slashing.DefaultParamspace), app.RegisterCodespace(slashing.DefaultCodespace)) app.cdc,
app.stakeKeeper = app.stakeKeeper.WithHooks(app.slashingKeeper.Hooks()) app.keyParams, app.tkeyParams,
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.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 // register message routes
app.Router(). app.Router().

View File

@ -119,7 +119,7 @@ func TestPrefixStoreIterate(t *testing.T) {
pIter.Close() pIter.Close()
} }
func mutateFirstByte(bz []byte) { func incFirstByte(bz []byte) {
if bz[0] == byte(255) { if bz[0] == byte(255) {
bz[0] = byte(0) bz[0] = byte(0)
return return
@ -133,15 +133,15 @@ func TestCloneAppend(t *testing.T) {
bz := cloneAppend(kvp.key, kvp.value) bz := cloneAppend(kvp.key, kvp.value)
require.Equal(t, bz, append(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...)) require.NotEqual(t, bz, append(kvp.key, kvp.value...))
bz = cloneAppend(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...)) require.NotEqual(t, bz, append(kvp.key, kvp.value...))
bz = cloneAppend(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...)) require.NotEqual(t, bz, append(kvp.key, kvp.value...))
} }
} }

View File

@ -22,10 +22,10 @@ var (
// Governance Keeper // Governance Keeper
type Keeper struct { type Keeper struct {
// The reference to the Param Keeper to get and set Global Params // 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 // 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 // The reference to the CoinKeeper to modify balances
ck bank.Keeper ck bank.Keeper
@ -51,16 +51,16 @@ type Keeper struct {
// - depositing funds into proposals, and activating upon sufficient funds being deposited // - depositing funds into proposals, and activating upon sufficient funds being deposited
// - users voting on proposals, with weight proportional to stake in the system // - users voting on proposals, with weight proportional to stake in the system
// - and tallying the result of the vote. // - 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{ return Keeper{
storeKey: key, storeKey: key,
pk: pk, paramsKeeper: paramsKeeper,
ps: ps, paramStore: paramStore,
ck: ck, ck: ck,
ds: ds, ds: ds,
vs: ds.GetValidatorSet(), vs: ds.GetValidatorSet(),
cdc: cdc, cdc: cdc,
codespace: codespace, codespace: codespace,
} }
} }
@ -219,7 +219,7 @@ func (keeper Keeper) activateVotingPeriod(ctx sdk.Context, proposal Proposal) {
// nolint: errcheck // nolint: errcheck
func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure { func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure {
var depositProcedure DepositProcedure var depositProcedure DepositProcedure
keeper.ps.Get(ctx, ParamStoreKeyDepositProcedure, &depositProcedure) keeper.paramStore.Get(ctx, ParamStoreKeyDepositProcedure, &depositProcedure)
return depositProcedure return depositProcedure
} }
@ -227,7 +227,7 @@ func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure {
// nolint: errcheck // nolint: errcheck
func (keeper Keeper) GetVotingProcedure(ctx sdk.Context) VotingProcedure { func (keeper Keeper) GetVotingProcedure(ctx sdk.Context) VotingProcedure {
var votingProcedure VotingProcedure var votingProcedure VotingProcedure
keeper.ps.Get(ctx, ParamStoreKeyVotingProcedure, &votingProcedure) keeper.paramStore.Get(ctx, ParamStoreKeyVotingProcedure, &votingProcedure)
return votingProcedure return votingProcedure
} }
@ -235,23 +235,23 @@ func (keeper Keeper) GetVotingProcedure(ctx sdk.Context) VotingProcedure {
// nolint: errcheck // nolint: errcheck
func (keeper Keeper) GetTallyingProcedure(ctx sdk.Context) TallyingProcedure { func (keeper Keeper) GetTallyingProcedure(ctx sdk.Context) TallyingProcedure {
var tallyingProcedure TallyingProcedure var tallyingProcedure TallyingProcedure
keeper.ps.Get(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure) keeper.paramStore.Get(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure)
return tallyingProcedure return tallyingProcedure
} }
// nolint: errcheck // nolint: errcheck
func (keeper Keeper) setDepositProcedure(ctx sdk.Context, depositProcedure DepositProcedure) { func (keeper Keeper) setDepositProcedure(ctx sdk.Context, depositProcedure DepositProcedure) {
keeper.ps.Set(ctx, ParamStoreKeyDepositProcedure, &depositProcedure) keeper.paramStore.Set(ctx, ParamStoreKeyDepositProcedure, &depositProcedure)
} }
// nolint: errcheck // nolint: errcheck
func (keeper Keeper) setVotingProcedure(ctx sdk.Context, votingProcedure VotingProcedure) { func (keeper Keeper) setVotingProcedure(ctx sdk.Context, votingProcedure VotingProcedure) {
keeper.ps.Set(ctx, ParamStoreKeyVotingProcedure, &votingProcedure) keeper.paramStore.Set(ctx, ParamStoreKeyVotingProcedure, &votingProcedure)
} }
// nolint: errcheck // nolint: errcheck
func (keeper Keeper) setTallyingProcedure(ctx sdk.Context, tallyingProcedure TallyingProcedure) { func (keeper Keeper) setTallyingProcedure(ctx sdk.Context, tallyingProcedure TallyingProcedure) {
keeper.ps.Set(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure) keeper.paramStore.Set(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure)
} }
// ===================================================== // =====================================================

View File

@ -1,7 +1,6 @@
package params package params
import ( import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/params/store" "github.com/cosmos/cosmos-sdk/x/params/store"
) )
@ -10,11 +9,5 @@ type (
Store = store.Store Store = store.Store
ReadOnlyStore = store.ReadOnlyStore ReadOnlyStore = store.ReadOnlyStore
ParamStruct = store.ParamStruct 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)
}

View File

@ -1,9 +1,5 @@
package store package store
import (
"github.com/cosmos/cosmos-sdk/codec"
)
// Used for associating paramstore key and field of param structs // Used for associating paramstore key and field of param structs
type KeyFieldPair struct { type KeyFieldPair struct {
Key []byte Key []byte
@ -11,21 +7,9 @@ type KeyFieldPair struct {
} }
// Slice of KeyFieldPair // Slice of KeyFieldPair
type KeyFieldPairs []KeyFieldPair type KeyValuePairs []KeyFieldPair
// Interface for structs containing parameters for a module // Interface for structs containing parameters for a module
type ParamStruct interface { type ParamStruct interface {
KeyFieldPairs() KeyFieldPairs KeyValuePairs() KeyValuePairs
}
// 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
} }

View File

@ -8,6 +8,10 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" 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 // Individual parameter store for each keeper
// Transient store persists for a block, so we use it for // Transient store persists for a block, so we use it for
// recording whether the parameter has been changed or not // recording whether the parameter has been changed or not
@ -20,23 +24,30 @@ type Store struct {
} }
// NewStore constructs a store with namestore // NewStore constructs a store with namestore
func NewStore(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, space string) Store { func NewStore(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, space string) (res Store) {
return Store{ res = Store{
cdc: cdc, cdc: cdc,
key: key, key: key,
tkey: tkey, 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 { 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, '/')) return ctx.KVStore(s.key).Prefix(append(s.space, '/'))
} }
// Returns a KVStore identical with ctx.TransientStore(s.tkey).Prefix() // Returns a KVStore identical with ctx.TransientStore(s.tkey).Prefix()
func (s Store) transientStore(ctx sdk.Context) sdk.KVStore { 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, '/')) 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 // Get to ParamStruct
func (s Store) GetStruct(ctx sdk.Context, ps 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) s.Get(ctx, pair.Key, pair.Field)
} }
} }
// Set from ParamStruct // Set from ParamStruct
func (s Store) SetStruct(ctx sdk.Context, ps 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. // pair.Field is a pointer to the field, so indirecting the ptr.
// go-amino automatically handles it but just for sure, // go-amino automatically handles it but just for sure,
// since SetStruct is meant to be used in InitGenesis // since SetStruct is meant to be used in InitGenesis

View File

@ -28,15 +28,15 @@ type Params struct {
MaxEvidenceAge time.Duration `json:"max-evidence-age"` MaxEvidenceAge time.Duration `json:"max-evidence-age"`
SignedBlocksWindow int64 `json:"signed-blocks-window"` SignedBlocksWindow int64 `json:"signed-blocks-window"`
MinSignedPerWindow sdk.Dec `json:"min-signed-per-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"` 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"` SlashFractionDowntime sdk.Dec `json:"slash-fraction-downtime"`
} }
// Implements params.ParamStruct // Implements params.ParamStruct
func (p *Params) KeyFieldPairs() params.KeyFieldPairs { func (p *Params) KeyValuePairs() params.KeyValuePairs {
return params.KeyFieldPairs{ return params.KeyValuePairs{
{KeyMaxEvidenceAge, &p.MaxEvidenceAge}, {KeyMaxEvidenceAge, &p.MaxEvidenceAge},
{KeySignedBlocksWindow, &p.SignedBlocksWindow}, {KeySignedBlocksWindow, &p.SignedBlocksWindow},
{KeyMinSignedPerWindow, &p.MinSignedPerWindow}, {KeyMinSignedPerWindow, &p.MinSignedPerWindow},

View File

@ -25,6 +25,8 @@ var (
KeyBondDenom = []byte("BondDenom") KeyBondDenom = []byte("BondDenom")
) )
var _ params.ParamStruct = (*Params)(nil)
// Params defines the high level settings for staking // Params defines the high level settings for staking
type Params struct { type Params struct {
InflationRateChange sdk.Dec `json:"inflation_rate_change"` // maximum annual change in inflation rate InflationRateChange sdk.Dec `json:"inflation_rate_change"` // maximum annual change in inflation rate
@ -39,8 +41,8 @@ type Params struct {
} }
// Implements params.ParamStruct // Implements params.ParamStruct
func (p *Params) KeyFieldPairs() params.KeyFieldPairs { func (p *Params) KeyValuePairs() params.KeyValuePairs {
return params.KeyFieldPairs{ return params.KeyValuePairs{
{KeyInflationRateChange, &p.InflationRateChange}, {KeyInflationRateChange, &p.InflationRateChange},
{KeyInflationMax, &p.InflationMax}, {KeyInflationMax, &p.InflationMax},
{KeyInflationMin, &p.InflationMin}, {KeyInflationMin, &p.InflationMin},