Example WriteGenesis implementation

This commit is contained in:
Christopher Goes 2018-04-23 14:51:35 +02:00 committed by rigelrozanski
parent ef88ca4c21
commit ff66629b68
3 changed files with 21 additions and 11 deletions

View File

@ -36,13 +36,29 @@ func NewKeeper(key sdk.StoreKey, config Config, ck bank.Keeper, codespace sdk.Co
return Keeper{key, config, ck, codespace}
}
// Init Genessis for the POW module
// InitGenesis for the POW module
func (k Keeper) InitGenesis(ctx sdk.Context, genesis Genesis) error {
k.SetLastDifficulty(ctx, genesis.Difficulty)
k.SetLastCount(ctx, genesis.Count)
return nil
}
// WriteGenesis for the PoW module
func (k Keeper) WriteGenesis(ctx sdk.Context) Genesis {
difficulty, err := k.GetLastDifficulty(ctx)
if err != nil {
panic(err)
}
count, err := k.GetLastCount(ctx)
if err != nil {
panic(err)
}
return Genesis{
difficulty,
count,
}
}
var lastDifficultyKey = []byte("lastDifficultyKey")
// get the last mining difficulty

View File

@ -40,6 +40,10 @@ func TestPowKeeperGetSet(t *testing.T) {
err := keeper.InitGenesis(ctx, Genesis{uint64(1), uint64(0)})
assert.Nil(t, err)
genesis := keeper.WriteGenesis(ctx)
assert.Nil(t, err)
assert.Equal(t, genesis, Genesis{uint64(1), uint64(0)})
res, err := keeper.GetLastDifficulty(ctx)
assert.Nil(t, err)
assert.Equal(t, res, uint64(1))

View File

@ -1,10 +0,0 @@
package types
import (
"encoding/json"
)
// Run only once on chain initialization, should write genesis state to store
// or throw an error if some required information was not provided, in which case
// the application will panic.
type InitGenesis func(ctx Context, data json.RawMessage) error