From 37ce8d51c697f15329e3a6c6872000bfbf1fa6fa Mon Sep 17 00:00:00 2001 From: mossid Date: Mon, 10 Sep 2018 20:59:05 +0900 Subject: [PATCH] apply requests --- cmd/gaia/app/app_test.go | 2 +- cmd/gaia/app/genesis.go | 2 +- cmd/gaia/app/sim_test.go | 2 +- x/params/keeper.go | 18 +++++++++++------- x/params/space/doc.go | 13 +++++++++++++ x/params/space/space.go | 2 +- x/slashing/genesis.go | 4 ++-- x/slashing/handler_test.go | 4 ++-- x/slashing/hooks_test.go | 4 ++-- x/slashing/keeper_test.go | 6 +++--- x/slashing/params.go | 2 +- x/slashing/signing_info_test.go | 4 ++-- x/slashing/slashing_period_test.go | 4 ++-- x/slashing/tick_test.go | 2 +- 14 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 x/params/space/doc.go diff --git a/cmd/gaia/app/app_test.go b/cmd/gaia/app/app_test.go index fcb2fb408e..d16cba40ea 100644 --- a/cmd/gaia/app/app_test.go +++ b/cmd/gaia/app/app_test.go @@ -24,7 +24,7 @@ func setGenesis(gapp *GaiaApp, accs ...*auth.BaseAccount) error { genesisState := GenesisState{ Accounts: genaccs, StakeData: stake.DefaultGenesisState(), - SlashingData: slashing.HubDefaultGenesisState(), + SlashingData: slashing.DefaultGenesisState(), } stateBytes, err := codec.MarshalJSONIndent(gapp.cdc, genesisState) diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index d2edd7f25b..99cffc3434 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -172,7 +172,7 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat // start with the default staking genesis state stakeData := stake.DefaultGenesisState() - slashingData := slashing.HubDefaultGenesisState() + slashingData := slashing.DefaultGenesisState() // get genesis flag account information genaccs := make([]GenesisAccount, len(appGenTxs)) diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index f6a21ba57d..c6ede65a45 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -77,7 +77,7 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage { genesis := GenesisState{ Accounts: genesisAccounts, StakeData: stakeGenesis, - SlashingData: slashing.HubDefaultGenesisState(), + SlashingData: slashing.DefaultGenesisState(), GovData: govGenesis, } diff --git a/x/params/keeper.go b/x/params/keeper.go index 2130fac5f6..97b17f9a03 100644 --- a/x/params/keeper.go +++ b/x/params/keeper.go @@ -30,17 +30,12 @@ func NewKeeper(cdc *codec.Codec, key *sdk.KVStoreKey, tkey *sdk.TransientStoreKe } // Allocate substore used for keepers -func (k Keeper) Subspace(space string) Space { - _, ok := k.spaces[space] +func (k Keeper) Subspace(spacename string) Space { + _, ok := k.spaces[spacename] if ok { panic("subspace already occupied") } - return k.UnsafeSubspace(space) -} - -// Get substore without checking existing allocation -func (k Keeper) UnsafeSubspace(spacename string) Space { if spacename == "" { panic("cannot use empty string for subspace") } @@ -51,3 +46,12 @@ func (k Keeper) UnsafeSubspace(spacename string) Space { return space } + +// Get existing subspace from keeper +func (k Keeper) GetSubspace(spacename string) (Space, bool) { + space, ok := k.spaces[spacename] + if !ok { + return Space{}, false + } + return *space, ok +} diff --git a/x/params/space/doc.go b/x/params/space/doc.go new file mode 100644 index 0000000000..c02b69e7e9 --- /dev/null +++ b/x/params/space/doc.go @@ -0,0 +1,13 @@ +package space + +/* +To prevent namespace collision between comsumer modules, we define type +"space". A Space can only be generated by the keeper, and the keeper checks +the existence of the space having the same name before generating the +space. + +Consumer modules must take a space(via Keeper.Subspace), not the keeper +itself. This isolates each modules from the others and make them modify theparameters safely. Keeper can be treated as master permission for all +subspaces(via Keeper.GetSubspace), so should be passed to proper modules +(ex. gov) +*/ diff --git a/x/params/space/space.go b/x/params/space/space.go index e0e764c47a..89947a808d 100644 --- a/x/params/space/space.go +++ b/x/params/space/space.go @@ -30,7 +30,7 @@ func NewSpace(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, space strin key: key, tkey: tkey, - space: []byte(space + "/"), + space: append([]byte(space), '/'), } } diff --git a/x/slashing/genesis.go b/x/slashing/genesis.go index 44e61f8c7f..cc259eb501 100644 --- a/x/slashing/genesis.go +++ b/x/slashing/genesis.go @@ -11,9 +11,9 @@ type GenesisState struct { } // HubDefaultGenesisState - default GenesisState used by Cosmos Hub -func HubDefaultGenesisState() GenesisState { +func DefaultGenesisState() GenesisState { return GenesisState{ - Params: HubDefaultParams(), + Params: DefaultParams(), } } diff --git a/x/slashing/handler_test.go b/x/slashing/handler_test.go index 90874d7759..55a2a6a430 100644 --- a/x/slashing/handler_test.go +++ b/x/slashing/handler_test.go @@ -12,7 +12,7 @@ import ( func TestCannotUnjailUnlessJailed(t *testing.T) { // initial setup - ctx, ck, sk, _, keeper := createTestInput(t, HubDefaultParams()) + ctx, ck, sk, _, keeper := createTestInput(t, DefaultParams()) slh := NewHandler(keeper) amtInt := int64(100) addr, val, amt := addrs[0], pks[0], sdk.NewInt(amtInt) @@ -30,7 +30,7 @@ func TestCannotUnjailUnlessJailed(t *testing.T) { } func TestJailedValidatorDelegations(t *testing.T) { - ctx, _, stakeKeeper, _, slashingKeeper := createTestInput(t, HubDefaultParams()) + ctx, _, stakeKeeper, _, slashingKeeper := createTestInput(t, DefaultParams()) stakeParams := stakeKeeper.GetParams(ctx) stakeParams.UnbondingTime = 0 diff --git a/x/slashing/hooks_test.go b/x/slashing/hooks_test.go index c297185267..951e3637f3 100644 --- a/x/slashing/hooks_test.go +++ b/x/slashing/hooks_test.go @@ -9,7 +9,7 @@ import ( ) func TestHookOnValidatorBonded(t *testing.T) { - ctx, _, _, _, keeper := createTestInput(t, HubDefaultParams()) + ctx, _, _, _, keeper := createTestInput(t, DefaultParams()) addr := sdk.ConsAddress(addrs[0]) keeper.onValidatorBonded(ctx, addr) period := keeper.getValidatorSlashingPeriodForHeight(ctx, addr, ctx.BlockHeight()) @@ -17,7 +17,7 @@ func TestHookOnValidatorBonded(t *testing.T) { } func TestHookOnValidatorBeginUnbonding(t *testing.T) { - ctx, _, _, _, keeper := createTestInput(t, HubDefaultParams()) + ctx, _, _, _, keeper := createTestInput(t, DefaultParams()) addr := sdk.ConsAddress(addrs[0]) keeper.onValidatorBonded(ctx, addr) keeper.onValidatorBeginUnbonding(ctx, addr) diff --git a/x/slashing/keeper_test.go b/x/slashing/keeper_test.go index 1d22eee642..69ecf41cd1 100644 --- a/x/slashing/keeper_test.go +++ b/x/slashing/keeper_test.go @@ -13,7 +13,7 @@ import ( // Have to change these parameters for tests // lest the tests take forever func keeperTestParams() Params { - params := HubDefaultParams() + params := DefaultParams() params.SignedBlocksWindow = 1000 params.DowntimeUnbondDuration = 60 * 60 params.DoubleSignUnbondDuration = 60 * 60 @@ -70,7 +70,7 @@ func TestHandleDoubleSign(t *testing.T) { func TestSlashingPeriodCap(t *testing.T) { // initial setup - ctx, ck, sk, _, keeper := createTestInput(t, HubDefaultParams()) + ctx, ck, sk, _, keeper := createTestInput(t, DefaultParams()) sk = sk.WithHooks(keeper.Hooks()) amtInt := int64(100) addr, amt := addrs[0], sdk.NewInt(amtInt) @@ -298,7 +298,7 @@ func TestHandleNewValidator(t *testing.T) { func TestHandleAlreadyJailed(t *testing.T) { // initial setup - ctx, _, sk, _, keeper := createTestInput(t, HubDefaultParams()) + ctx, _, sk, _, keeper := createTestInput(t, DefaultParams()) amtInt := int64(100) addr, val, amt := addrs[0], pks[0], sdk.NewInt(amtInt) sh := stake.NewHandler(sk) diff --git a/x/slashing/params.go b/x/slashing/params.go index 94d2265bad..6f6755d33d 100644 --- a/x/slashing/params.go +++ b/x/slashing/params.go @@ -45,7 +45,7 @@ type Params struct { } // Default parameters used by Cosmos Hub -func HubDefaultParams() Params { +func DefaultParams() Params { return Params{ // defaultMaxEvidenceAge = 60 * 60 * 24 * 7 * 3 // TODO Temporarily set to 2 minutes for testnets. diff --git a/x/slashing/signing_info_test.go b/x/slashing/signing_info_test.go index 897902d4e5..123457d5d5 100644 --- a/x/slashing/signing_info_test.go +++ b/x/slashing/signing_info_test.go @@ -10,7 +10,7 @@ import ( ) func TestGetSetValidatorSigningInfo(t *testing.T) { - ctx, _, _, _, keeper := createTestInput(t, HubDefaultParams()) + ctx, _, _, _, keeper := createTestInput(t, DefaultParams()) info, found := keeper.getValidatorSigningInfo(ctx, sdk.ConsAddress(addrs[0])) require.False(t, found) newInfo := ValidatorSigningInfo{ @@ -29,7 +29,7 @@ func TestGetSetValidatorSigningInfo(t *testing.T) { } func TestGetSetValidatorSigningBitArray(t *testing.T) { - ctx, _, _, _, keeper := createTestInput(t, HubDefaultParams()) + ctx, _, _, _, keeper := createTestInput(t, DefaultParams()) signed := keeper.getValidatorSigningBitArray(ctx, sdk.ConsAddress(addrs[0]), 0) require.False(t, signed) // treat empty key as unsigned keeper.setValidatorSigningBitArray(ctx, sdk.ConsAddress(addrs[0]), 0, true) diff --git a/x/slashing/slashing_period_test.go b/x/slashing/slashing_period_test.go index ac9c58313d..bd12ef1273 100644 --- a/x/slashing/slashing_period_test.go +++ b/x/slashing/slashing_period_test.go @@ -9,7 +9,7 @@ import ( ) func TestGetSetValidatorSlashingPeriod(t *testing.T) { - ctx, _, _, _, keeper := createTestInput(t, HubDefaultParams()) + ctx, _, _, _, keeper := createTestInput(t, DefaultParams()) addr := sdk.ConsAddress(addrs[0]) height := int64(5) require.Panics(t, func() { keeper.getValidatorSlashingPeriodForHeight(ctx, addr, height) }) @@ -60,7 +60,7 @@ func TestGetSetValidatorSlashingPeriod(t *testing.T) { } func TestValidatorSlashingPeriodCap(t *testing.T) { - ctx, _, _, _, keeper := createTestInput(t, HubDefaultParams()) + ctx, _, _, _, keeper := createTestInput(t, DefaultParams()) addr := sdk.ConsAddress(addrs[0]) height := int64(5) newPeriod := ValidatorSlashingPeriod{ diff --git a/x/slashing/tick_test.go b/x/slashing/tick_test.go index 7687d68cbc..085ce9eba8 100644 --- a/x/slashing/tick_test.go +++ b/x/slashing/tick_test.go @@ -13,7 +13,7 @@ import ( ) func TestBeginBlocker(t *testing.T) { - ctx, ck, sk, _, keeper := createTestInput(t, HubDefaultParams()) + ctx, ck, sk, _, keeper := createTestInput(t, DefaultParams()) addr, pk, amt := addrs[2], pks[2], sdk.NewInt(100) // bond the validator