Skip to content

Commit

Permalink
Implement InitGenesis for x/stake (closes #737)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Apr 2, 2018
1 parent f1ebd67 commit 382aa7c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
19 changes: 16 additions & 3 deletions x/stake/keeper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package stake

import (
"encoding/json"
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/bank"
Expand All @@ -26,6 +29,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
Expand Down Expand Up @@ -279,8 +293,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, &params)
Expand Down Expand Up @@ -310,7 +323,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 {
Expand Down
33 changes: 33 additions & 0 deletions x/stake/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stake

import (
"encoding/hex"
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 5 additions & 23 deletions x/stake/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
}

//_______________________________________________________________________________________________________
Expand Down

0 comments on commit 382aa7c

Please sign in to comment.