Add candidates & bonds to stake module genesis

This commit is contained in:
Christopher Goes 2018-04-24 15:05:19 +02:00 committed by rigelrozanski
parent f71191e402
commit 0436d890c7
3 changed files with 41 additions and 6 deletions

View File

@ -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,
}
}
//_____________________________________________________________________

View File

@ -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)

View File

@ -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"`
}
//_________________________________________________________________________