From 0436d890c7dd9d273f6fa49226e7461e259bf714 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Tue, 24 Apr 2018 15:05:19 +0200 Subject: [PATCH] Add candidates & bonds to stake module genesis --- x/stake/handler.go | 12 +++++++++++- x/stake/keeper.go | 28 ++++++++++++++++++++++++++-- x/stake/types.go | 7 ++++--- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/x/stake/handler.go b/x/stake/handler.go index a04bdf862b..d7b7ef86ed 100644 --- a/x/stake/handler.go +++ b/x/stake/handler.go @@ -55,13 +55,23 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data GenesisState) { for _, candidate := range data.Candidates { k.setCandidate(ctx, candidate) } + for _, bond := range data.Bonds { + k.setDelegatorBond(ctx, bond) + } } // WriteGenesis - output genesis parameters func (k Keeper) WriteGenesis(ctx sdk.Context) GenesisState { pool := k.GetPool(ctx) params := k.GetParams(ctx) - return GenesisState{pool, params} + candidates := k.GetCandidates(ctx, -1) + bonds := k.GetBonds(ctx, -1) + return GenesisState{ + pool, + params, + candidates, + bonds, + } } //_____________________________________________________________________ diff --git a/x/stake/keeper.go b/x/stake/keeper.go index f1b4363140..d30d7b46e3 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -74,7 +74,7 @@ func (k Keeper) GetCandidate(ctx sdk.Context, addr sdk.Address) (candidate Candi return candidate, true } -// Get the set of all candidates, retrieve a maxRetrieve number of records +// Get the set of all candidates, retrieve a maxRetrieve number of records, -1 maxRetrieve = no limit func (k Keeper) GetCandidates(ctx sdk.Context, maxRetrieve int16) (candidates Candidates) { store := ctx.KVStore(k.storeKey) iterator := store.Iterator(subspace(CandidatesKey)) @@ -82,7 +82,7 @@ func (k Keeper) GetCandidates(ctx sdk.Context, maxRetrieve int16) (candidates Ca candidates = make([]Candidate, maxRetrieve) i := 0 for ; ; i++ { - if !iterator.Valid() || i > int(maxRetrieve-1) { + if !iterator.Valid() || (maxRetrieve >= 0 && i > int(maxRetrieve-1)) { iterator.Close() break } @@ -371,6 +371,30 @@ func (k Keeper) GetDelegatorBond(ctx sdk.Context, return bond, true } +// load all bonds, -1 maxRetrieve = no limit +func (k Keeper) GetBonds(ctx sdk.Context, maxRetrieve int16) (bonds []DelegatorBond) { + store := ctx.KVStore(k.storeKey) + iterator := store.Iterator(subspace(DelegatorBondKeyPrefix)) + + bonds = make([]DelegatorBond, maxRetrieve) + i := 0 + for ; ; i++ { + if !iterator.Valid() || (maxRetrieve >= 0 && i > int(maxRetrieve-1)) { + iterator.Close() + break + } + bondBytes := iterator.Value() + var bond DelegatorBond + err := k.cdc.UnmarshalBinary(bondBytes, &bond) + if err != nil { + panic(err) + } + bonds[i] = bond + iterator.Next() + } + return bonds[:i] // trim +} + // load all bonds of a delegator func (k Keeper) GetDelegatorBonds(ctx sdk.Context, delegator sdk.Address, maxRetrieve int16) (bonds []DelegatorBond) { store := ctx.KVStore(k.storeKey) diff --git a/x/stake/types.go b/x/stake/types.go index 634b51186b..6e15acd96d 100644 --- a/x/stake/types.go +++ b/x/stake/types.go @@ -9,9 +9,10 @@ import ( // GenesisState - all staking state that must be provided at genesis type GenesisState struct { - Pool Pool `json:"pool"` - Params Params `json:"params"` - Candidates []Candidate `json:"candidates"` + Pool Pool `json:"pool"` + Params Params `json:"params"` + Candidates []Candidate `json:"candidates"` + Bonds []DelegatorBond `json:"bonds"` } //_________________________________________________________________________