From 2e9e2835ff25a56f3d479c76760e25532bf6ac65 Mon Sep 17 00:00:00 2001 From: mossid Date: Sun, 6 May 2018 20:39:50 +0200 Subject: [PATCH] add delegation --- types/validator_set.go | 15 ++++++++++++++- x/stake/keeper.go | 15 ++++++++++++++- x/stake/types.go | 24 ++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/types/validator_set.go b/types/validator_set.go index 99592ce74a..af534747f5 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -25,6 +25,19 @@ type ValidatorSet interface { type ValidatorSetKeeper interface { ValidatorSet(Context) ValidatorSet - GetByAddress(Context, Address) Validator + Validator(Context, Address) Validator TotalPower(Context) Rat + DelegationSet(Context, Address) DelegationSet + Delegation(Context, Address, Address) Delegation +} + +type Delegation interface { + GetDelegator() Address + GetValidator() Address + GetDelegated() Rat +} + +type DelegationSet interface { + Iterate(func(int, Delegation)) + Size() int } diff --git a/x/stake/keeper.go b/x/stake/keeper.go index 7a810a351a..6e36588301 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -510,7 +510,7 @@ func (k Keeper) ValidatorSet(ctx sdk.Context) sdk.ValidatorSet { return ValidatorSet(vals) } -func (k Keeper) GetByAddress(ctx sdk.Context, addr sdk.Address) sdk.Validator { +func (k Keeper) Validator(ctx sdk.Context, addr sdk.Address) sdk.Validator { can, ok := k.GetCandidate(ctx, addr) if !ok { return nil @@ -525,3 +525,16 @@ func (k Keeper) TotalPower(ctx sdk.Context) sdk.Rat { pool := k.GetPool(ctx) return pool.BondedShares } + +func (k Keeper) Delegation(ctx sdk.Context, del sdk.Address, val sdk.Address) sdk.Delegation { + bond, ok := k.GetDelegatorBond(ctx, del, val) + if !ok { + return nil + } + return bond +} + +func (k Keeper) DelegationSet(ctx sdk.Context, del sdk.Address) sdk.DelegationSet { + bs := k.GetDelegatorBonds(ctx, del, 32767) + return DelegationSet(bs) +} diff --git a/x/stake/types.go b/x/stake/types.go index f9f6b4a23e..7bd03b38f9 100644 --- a/x/stake/types.go +++ b/x/stake/types.go @@ -355,3 +355,27 @@ func (b DelegatorBond) equal(b2 DelegatorBond) bool { b.Height == b2.Height && b.Shares.Equal(b2.Shares) } + +func (b DelegatorBond) GetDelegator() sdk.Address { + return b.DelegatorAddr +} + +func (b DelegatorBond) GetValidator() sdk.Address { + return b.CandidateAddr +} + +func (b DelegatorBond) GetDelegated() sdk.Rat { + return b.Shares +} + +type DelegationSet []DelegatorBond + +func (ds DelegationSet) Iterate(fn func(int, sdk.Delegation)) { + for i, d := range ds { + fn(i, d) + } +} + +func (ds DelegationSet) Size() int { + return len(ds) +}