diff --git a/types/validator_set.go b/types/validator_set.go new file mode 100644 index 0000000000..ab12e1c8b1 --- /dev/null +++ b/types/validator_set.go @@ -0,0 +1,26 @@ +package types + +import ( + abci "github.com/tendermint/abci/types" + "github.com/tendermint/go-crypto" + + "github.com/cosmos/cosmos-sdk/wire" +) + +var cdc = wire.NewCodec() + +func init() { + crypto.RegisterAmino(cdc) +} + +type Validator = abci.Validator + +type ValidatorSetKeeper interface { + Hash(Context) []byte + GetValidators(Context) []*Validator + Size(Context) int + IsValidator(Context, Address) bool + GetByAddress(Context, Address) (int, *Validator) + GetByIndex(Context, int) *Validator + TotalPower(Context) Rat +} diff --git a/x/stake/keeper.go b/x/stake/keeper.go index 605f675db0..df0de8ec86 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -493,3 +493,49 @@ func (k Keeper) setPool(ctx sdk.Context, p Pool) { store.Set(PoolKey, b) k.pool = Pool{} //clear the cache } + +//__________________________________________________________________________ + +// Implements sdk.ValidatorSetKeeper + +func (k Keeper) Hash() []byte { + return nil +} + +func (k Keeper) Size(ctx sdk.Context) int { + return len(k.GetValidators(ctx)) +} + +func (k Keeper) IsValidator(ctx sdk.Context, addr sdk.Address) bool { + for _, v := range k.GetValidators(ctx) { + if bytes.Equal(v.Address, addr) { + return true + } + } + return false +} + +func (k Keeper) GetByAddress(ctx sdk.Context, addr sdk.Address) (int, *sdk.Validator) { + for i, v := range k.GetValidators(ctx) { + if bytes.Equal(v.Address, addr) { + val := v.abciValidator(k.cdc) + return i, &val + } + } + return -1, nil +} + +func (k Keeper) GetByIndex(ctx sdk.Context, index int) *sdk.Validator { + valset := k.GetValidators(ctx) + + if index < 0 || index >= len(valset) { + return nil + } + + val := valset[index].abciValidator(k.cdc) + return &val +} + +func (k Keeper) TotalPower() sdk.Rat { + return sdk.ZeroRat +} diff --git a/x/stake/types.go b/x/stake/types.go index 741783fa1d..ffe2ca4fb4 100644 --- a/x/stake/types.go +++ b/x/stake/types.go @@ -2,9 +2,9 @@ package stake import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/wire" - abci "github.com/tendermint/abci/types" crypto "github.com/tendermint/go-crypto" + + "github.com/cosmos/cosmos-sdk/wire" ) // GenesisState - all staking state that must be provided at genesis @@ -159,8 +159,8 @@ type Validator struct { } // abci validator from stake validator type -func (v Validator) abciValidator(cdc *wire.Codec) abci.Validator { - return abci.Validator{ +func (v Validator) abciValidator(cdc *wire.Codec) sdk.Validator { + return sdk.Validator{ PubKey: v.PubKey.Bytes(), Power: v.Power.Evaluate(), } @@ -168,8 +168,8 @@ func (v Validator) abciValidator(cdc *wire.Codec) abci.Validator { // abci validator from stake validator type // with zero power used for validator updates -func (v Validator) abciValidatorZero(cdc *wire.Codec) abci.Validator { - return abci.Validator{ +func (v Validator) abciValidatorZero(cdc *wire.Codec) sdk.Validator { + return sdk.Validator{ PubKey: v.PubKey.Bytes(), Power: 0, }