From 8fad09a659903046bae29241bcdfbe89509b24f7 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Mon, 2 Apr 2018 15:48:10 +0200 Subject: [PATCH 1/3] Implement InitGenesis for x/stake (closes #737) --- x/stake/keeper.go | 18 +++++++++++++++--- x/stake/test_common.go | 33 +++++++++++++++++++++++++++++++++ x/stake/types.go | 28 +++++----------------------- 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/x/stake/keeper.go b/x/stake/keeper.go index 95eb85e76..1bd70feb7 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -2,6 +2,8 @@ package stake import ( "bytes" + "encoding/json" + "errors" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" @@ -28,6 +30,17 @@ func NewKeeper(ctx sdk.Context, cdc *wire.Codec, key sdk.StoreKey, ck bank.CoinK return keeper } +// InitGenesis - store genesis parameters +func (k Keeper) InitGenesis(ctx sdk.Context, data json.RawMessage) error { + var state GenesisState + if err := json.Unmarshal(data, &state); err != nil { + return err + } + k.setPool(ctx, state.Pool) + k.setParams(ctx, state.Params) + return nil +} + //_________________________________________________________________________ // get a single candidate @@ -343,8 +356,7 @@ func (k Keeper) GetParams(ctx sdk.Context) (params Params) { store := ctx.KVStore(k.storeKey) b := store.Get(ParamKey) if b == nil { - k.params = defaultParams() - return k.params + panic(errors.New("Stored params should not have been nil")) } err := k.cdc.UnmarshalBinary(b, ¶ms) @@ -374,7 +386,7 @@ func (k Keeper) GetPool(ctx sdk.Context) (gs Pool) { store := ctx.KVStore(k.storeKey) b := store.Get(PoolKey) if b == nil { - return initialPool() + panic(errors.New("Stored pool should not have been nil")) } err := k.cdc.UnmarshalBinary(b, &gs) if err != nil { diff --git a/x/stake/test_common.go b/x/stake/test_common.go index 03f9fe92c..c90c68b2c 100644 --- a/x/stake/test_common.go +++ b/x/stake/test_common.go @@ -2,6 +2,7 @@ package stake import ( "encoding/hex" + "encoding/json" "testing" "github.com/stretchr/testify/require" @@ -52,6 +53,31 @@ var ( emptyPubkey crypto.PubKey ) +// default params for testing +func defaultParams() Params { + return Params{ + InflationRateChange: sdk.NewRat(13, 100), + InflationMax: sdk.NewRat(20, 100), + InflationMin: sdk.NewRat(7, 100), + GoalBonded: sdk.NewRat(67, 100), + MaxValidators: 100, + BondDenom: "fermion", + } +} + +// initial pool for testing +func initialPool() Pool { + return Pool{ + TotalSupply: 0, + BondedShares: sdk.ZeroRat, + UnbondedShares: sdk.ZeroRat, + BondedPool: 0, + UnbondedPool: 0, + InflationLastTime: 0, + Inflation: sdk.NewRat(7, 100), + } +} + // XXX reference the common declaration of this function func subspace(prefix []byte) (start, end []byte) { end = make([]byte, len(prefix)) @@ -123,6 +149,13 @@ func createTestInput(t *testing.T, sender sdk.Address, isCheckTx bool, initCoins ) ck := bank.NewCoinKeeper(accountMapper) keeper := NewKeeper(ctx, cdc, keyStake, ck) + encoded, err := json.Marshal(GenesisState{initialPool(), defaultParams()}) + if err != nil { + panic(err) + } + if err = keeper.InitGenesis(ctx, encoded); err != nil { + panic(err) + } // fill all the addresses with some coins for _, addr := range addrs { diff --git a/x/stake/types.go b/x/stake/types.go index 4ba7c59d0..9dfeeca20 100644 --- a/x/stake/types.go +++ b/x/stake/types.go @@ -16,19 +16,6 @@ type Params struct { BondDenom string `json:"bond_denom"` // bondable coin denomination } -// XXX do we want to allow for default params even or do we want to enforce that you -// need to be explicit about defining all params in genesis? -func defaultParams() Params { - return Params{ - InflationRateChange: sdk.NewRat(13, 100), - InflationMax: sdk.NewRat(20, 100), - InflationMin: sdk.NewRat(7, 100), - GoalBonded: sdk.NewRat(67, 100), - MaxValidators: 100, - BondDenom: "fermion", - } -} - //_________________________________________________________________________ // Pool - dynamic parameters of the current state @@ -42,16 +29,11 @@ type Pool struct { Inflation sdk.Rat `json:"inflation"` // current annual inflation rate } -func initialPool() Pool { - return Pool{ - TotalSupply: 0, - BondedShares: sdk.ZeroRat, - UnbondedShares: sdk.ZeroRat, - BondedPool: 0, - UnbondedPool: 0, - InflationLastTime: 0, - Inflation: sdk.NewRat(7, 100), - } +// GenesisState - all staking state that must be provided at genesis + +type GenesisState struct { + Pool Pool `json:"pool"` + Params Params `json:"params"` } //_______________________________________________________________________________________________________ From cfb3ba75afd731a9d2b390d2841dcdabb95b477c Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Tue, 3 Apr 2018 20:50:31 +0200 Subject: [PATCH 2/3] Update InitGenesis tests for x/stake --- x/stake/keeper.go | 5 ++--- x/stake/keeper_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++ x/stake/test_common.go | 10 ++------- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/x/stake/keeper.go b/x/stake/keeper.go index 1bd70feb7..d377df21f 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -3,7 +3,6 @@ package stake import ( "bytes" "encoding/json" - "errors" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" @@ -356,7 +355,7 @@ func (k Keeper) GetParams(ctx sdk.Context) (params Params) { store := ctx.KVStore(k.storeKey) b := store.Get(ParamKey) if b == nil { - panic(errors.New("Stored params should not have been nil")) + panic("Stored params should not have been nil") } err := k.cdc.UnmarshalBinary(b, ¶ms) @@ -386,7 +385,7 @@ func (k Keeper) GetPool(ctx sdk.Context) (gs Pool) { store := ctx.KVStore(k.storeKey) b := store.Get(PoolKey) if b == nil { - panic(errors.New("Stored pool should not have been nil")) + panic("Stored pool should not have been nil") } err := k.cdc.UnmarshalBinary(b, &gs) if err != nil { diff --git a/x/stake/keeper_test.go b/x/stake/keeper_test.go index aa36cd585..d7886ba01 100644 --- a/x/stake/keeper_test.go +++ b/x/stake/keeper_test.go @@ -2,6 +2,7 @@ package stake import ( "bytes" + "encoding/json" "testing" sdk "github.com/cosmos/cosmos-sdk/types" @@ -573,3 +574,51 @@ func TestPool(t *testing.T) { resPool = keeper.GetPool(ctx) assert.Equal(t, expPool, resPool) } + +func TestInitGenesis(t *testing.T) { + ctx, _, keeper := createTestInput(t, nil, false, 0) + encoded := json.RawMessage(`{ + "params": { + "inflation_rate_change": { + "num": 13, + "denom": 100 + }, + "inflation_max": { + "num": 20, + "denom": 100 + }, + "inflation_min": { + "num": 7, + "denom": 100 + }, + "goal_bonded": { + "num": 67, + "denom": 100 + }, + "max_validators": 100, + "bond_denom": "fermion" + }, + "pool": { + "total_supply": 0, + "bonded_shares": { + "num": 0, + "denom": 1 + }, + "unbonded_shares": { + "num": 0, + "denom": 1 + }, + "bonded_pool": 0, + "unbonded_pool": 0, + "inflation_last_time": 0, + "inflation": { + "num": 7, + "denom": 100 + } + } + }`) + err := keeper.InitGenesis(ctx, encoded) + require.Nil(t, err) + require.Equal(t, keeper.GetPool(ctx), initialPool()) + require.Equal(t, keeper.GetParams(ctx), defaultParams()) +} diff --git a/x/stake/test_common.go b/x/stake/test_common.go index c90c68b2c..71b745475 100644 --- a/x/stake/test_common.go +++ b/x/stake/test_common.go @@ -2,7 +2,6 @@ package stake import ( "encoding/hex" - "encoding/json" "testing" "github.com/stretchr/testify/require" @@ -149,13 +148,8 @@ func createTestInput(t *testing.T, sender sdk.Address, isCheckTx bool, initCoins ) ck := bank.NewCoinKeeper(accountMapper) keeper := NewKeeper(ctx, cdc, keyStake, ck) - encoded, err := json.Marshal(GenesisState{initialPool(), defaultParams()}) - if err != nil { - panic(err) - } - if err = keeper.InitGenesis(ctx, encoded); err != nil { - panic(err) - } + keeper.setPool(ctx, initialPool()) + keeper.setParams(ctx, defaultParams()) // fill all the addresses with some coins for _, addr := range addrs { From a85fdcc23b7bc6c64562a2fc19e67e741fd54766 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 3 Apr 2018 15:47:26 -0400 Subject: [PATCH 3/3] cleanup TestInitGenesis --- x/stake/keeper_test.go | 60 ++++++++++++++---------------------------- x/stake/types.go | 1 - 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/x/stake/keeper_test.go b/x/stake/keeper_test.go index d7886ba01..9a0d0f30e 100644 --- a/x/stake/keeper_test.go +++ b/x/stake/keeper_test.go @@ -577,46 +577,26 @@ func TestPool(t *testing.T) { func TestInitGenesis(t *testing.T) { ctx, _, keeper := createTestInput(t, nil, false, 0) - encoded := json.RawMessage(`{ - "params": { - "inflation_rate_change": { - "num": 13, - "denom": 100 - }, - "inflation_max": { - "num": 20, - "denom": 100 - }, - "inflation_min": { - "num": 7, - "denom": 100 - }, - "goal_bonded": { - "num": 67, - "denom": 100 - }, - "max_validators": 100, - "bond_denom": "fermion" - }, - "pool": { - "total_supply": 0, - "bonded_shares": { - "num": 0, - "denom": 1 - }, - "unbonded_shares": { - "num": 0, - "denom": 1 - }, - "bonded_pool": 0, - "unbonded_pool": 0, - "inflation_last_time": 0, - "inflation": { - "num": 7, - "denom": 100 - } - } - }`) + jsonStr := `{ + "params": { + "inflation_rate_change": {"num": 13, "denom": 100}, + "inflation_max": {"num": 20, "denom": 100}, + "inflation_min": {"num": 7, "denom": 100}, + "goal_bonded": {"num": 67, "denom": 100}, + "max_validators": 100, + "bond_denom": "fermion" + }, + "pool": { + "total_supply": 0, + "bonded_shares": {"num": 0, "denom": 1}, + "unbonded_shares": {"num": 0, "denom": 1}, + "bonded_pool": 0, + "unbonded_pool": 0, + "inflation_last_time": 0, + "inflation": {"num": 7, "denom": 100} + } +}` + encoded := json.RawMessage(jsonStr) err := keeper.InitGenesis(ctx, encoded) require.Nil(t, err) require.Equal(t, keeper.GetPool(ctx), initialPool()) diff --git a/x/stake/types.go b/x/stake/types.go index 9dfeeca20..7e7fb9e75 100644 --- a/x/stake/types.go +++ b/x/stake/types.go @@ -30,7 +30,6 @@ type Pool struct { } // GenesisState - all staking state that must be provided at genesis - type GenesisState struct { Pool Pool `json:"pool"` Params Params `json:"params"`