Merge pull request #772 from cosmos/cwgoes/staking-initgenesis

Implement InitGenesis for staking
This commit is contained in:
Rigel 2018-04-03 21:51:13 +02:00 committed by GitHub
commit 9fc9db00fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 26 deletions

View File

@ -2,6 +2,7 @@ package stake
import ( import (
"bytes" "bytes"
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/wire"
@ -28,6 +29,17 @@ func NewKeeper(ctx sdk.Context, cdc *wire.Codec, key sdk.StoreKey, ck bank.CoinK
return keeper 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 // get a single candidate
@ -343,8 +355,7 @@ func (k Keeper) GetParams(ctx sdk.Context) (params Params) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
b := store.Get(ParamKey) b := store.Get(ParamKey)
if b == nil { if b == nil {
k.params = defaultParams() panic("Stored params should not have been nil")
return k.params
} }
err := k.cdc.UnmarshalBinary(b, &params) err := k.cdc.UnmarshalBinary(b, &params)
@ -374,7 +385,7 @@ func (k Keeper) GetPool(ctx sdk.Context) (gs Pool) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
b := store.Get(PoolKey) b := store.Get(PoolKey)
if b == nil { if b == nil {
return initialPool() panic("Stored pool should not have been nil")
} }
err := k.cdc.UnmarshalBinary(b, &gs) err := k.cdc.UnmarshalBinary(b, &gs)
if err != nil { if err != nil {

View File

@ -2,6 +2,7 @@ package stake
import ( import (
"bytes" "bytes"
"encoding/json"
"testing" "testing"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -573,3 +574,31 @@ func TestPool(t *testing.T) {
resPool = keeper.GetPool(ctx) resPool = keeper.GetPool(ctx)
assert.Equal(t, expPool, resPool) assert.Equal(t, expPool, resPool)
} }
func TestInitGenesis(t *testing.T) {
ctx, _, keeper := createTestInput(t, nil, false, 0)
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())
require.Equal(t, keeper.GetParams(ctx), defaultParams())
}

View File

@ -52,6 +52,31 @@ var (
emptyPubkey crypto.PubKey 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 // XXX reference the common declaration of this function
func subspace(prefix []byte) (start, end []byte) { func subspace(prefix []byte) (start, end []byte) {
end = make([]byte, len(prefix)) end = make([]byte, len(prefix))
@ -123,6 +148,8 @@ func createTestInput(t *testing.T, sender sdk.Address, isCheckTx bool, initCoins
) )
ck := bank.NewCoinKeeper(accountMapper) ck := bank.NewCoinKeeper(accountMapper)
keeper := NewKeeper(ctx, cdc, keyStake, ck) keeper := NewKeeper(ctx, cdc, keyStake, ck)
keeper.setPool(ctx, initialPool())
keeper.setParams(ctx, defaultParams())
// fill all the addresses with some coins // fill all the addresses with some coins
for _, addr := range addrs { for _, addr := range addrs {

View File

@ -16,19 +16,6 @@ type Params struct {
BondDenom string `json:"bond_denom"` // bondable coin denomination 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 // Pool - dynamic parameters of the current state
@ -42,16 +29,10 @@ type Pool struct {
Inflation sdk.Rat `json:"inflation"` // current annual inflation rate Inflation sdk.Rat `json:"inflation"` // current annual inflation rate
} }
func initialPool() Pool { // GenesisState - all staking state that must be provided at genesis
return Pool{ type GenesisState struct {
TotalSupply: 0, Pool Pool `json:"pool"`
BondedShares: sdk.ZeroRat, Params Params `json:"params"`
UnbondedShares: sdk.ZeroRat,
BondedPool: 0,
UnbondedPool: 0,
InflationLastTime: 0,
Inflation: sdk.NewRat(7, 100),
}
} }
//_______________________________________________________________________________________________________ //_______________________________________________________________________________________________________