validators smaller values stored
This commit is contained in:
parent
04921b9ebd
commit
a2f7b582df
|
@ -15,7 +15,8 @@ func TestCannotUnrevokeUnlessRevoked(t *testing.T) {
|
||||||
slh := NewHandler(keeper)
|
slh := NewHandler(keeper)
|
||||||
amtInt := int64(100)
|
amtInt := int64(100)
|
||||||
addr, val, amt := addrs[0], pks[0], sdk.NewInt(amtInt)
|
addr, val, amt := addrs[0], pks[0], sdk.NewInt(amtInt)
|
||||||
got := stake.NewHandler(sk)(ctx, newTestMsgCreateValidator(addr, val, amt))
|
msg := newTestMsgCreateValidator(addr, val, amt)
|
||||||
|
got := stake.NewHandler(sk)(ctx, msg)
|
||||||
require.True(t, got.IsOK())
|
require.True(t, got.IsOK())
|
||||||
stake.EndBlocker(ctx, sk)
|
stake.EndBlocker(ctx, sk)
|
||||||
require.Equal(t, ck.GetCoins(ctx, addr), sdk.Coins{{sk.GetParams(ctx).BondDenom, initCoins.Sub(amt)}})
|
require.Equal(t, ck.GetCoins(ctx, addr), sdk.Coins{{sk.GetParams(ctx).BondDenom, initCoins.Sub(amt)}})
|
||||||
|
|
|
@ -23,16 +23,16 @@ import (
|
||||||
// TODO remove dependencies on staking (should only refer to validator set type from sdk)
|
// TODO remove dependencies on staking (should only refer to validator set type from sdk)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
addrs = []sdk.Address{
|
|
||||||
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6160"),
|
|
||||||
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6161"),
|
|
||||||
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6162"),
|
|
||||||
}
|
|
||||||
pks = []crypto.PubKey{
|
pks = []crypto.PubKey{
|
||||||
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB50"),
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB50"),
|
||||||
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"),
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"),
|
||||||
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"),
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"),
|
||||||
}
|
}
|
||||||
|
addrs = []sdk.Address{
|
||||||
|
pks[0].Address(),
|
||||||
|
pks[1].Address(),
|
||||||
|
pks[2].Address(),
|
||||||
|
}
|
||||||
initCoins sdk.Int = sdk.NewInt(200)
|
initCoins sdk.Int = sdk.NewInt(200)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -32,8 +32,8 @@ func GetCmdQueryValidator(storeName string, cdc *wire.Codec) *cobra.Command {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
validator := new(stake.Validator)
|
|
||||||
cdc.MustUnmarshalBinary(res, validator)
|
validator := types.UnmarshalValidator(cdc, addr, res)
|
||||||
|
|
||||||
switch viper.Get(cli.OutputFlag) {
|
switch viper.Get(cli.OutputFlag) {
|
||||||
case "text":
|
case "text":
|
||||||
|
@ -76,8 +76,8 @@ func GetCmdQueryValidators(storeName string, cdc *wire.Codec) *cobra.Command {
|
||||||
// parse out the validators
|
// parse out the validators
|
||||||
var validators []stake.Validator
|
var validators []stake.Validator
|
||||||
for _, kv := range resKVs {
|
for _, kv := range resKVs {
|
||||||
var validator stake.Validator
|
addr := kv.Key[1:]
|
||||||
cdc.MustUnmarshalBinary(kv.Value, &validator)
|
validator := types.UnmarshalValidator(cdc, addr, kv.Value)
|
||||||
validators = append(validators, validator)
|
validators = append(validators, validator)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -275,15 +275,14 @@ func validatorsHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerF
|
||||||
// parse out the validators
|
// parse out the validators
|
||||||
validators := make([]StakeValidatorOutput, len(kvs))
|
validators := make([]StakeValidatorOutput, len(kvs))
|
||||||
for i, kv := range kvs {
|
for i, kv := range kvs {
|
||||||
var validator stake.Validator
|
|
||||||
var bech32Validator StakeValidatorOutput
|
addr := kv.Key[1:]
|
||||||
err = cdc.UnmarshalBinary(kv.Value, &validator)
|
validator := types.UnmarshalValidator(cdc, addr, kv.Value)
|
||||||
if err == nil {
|
|
||||||
bech32Validator, err = bech32StakeValidatorOutput(validator)
|
bech32Validator, err := bech32StakeValidatorOutput(validator)
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte(fmt.Sprintf("couldn't decode validator. Error: %s", err.Error())))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
validators[i] = bech32Validator
|
validators[i] = bech32Validator
|
||||||
|
|
|
@ -16,9 +16,8 @@ func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validato
|
||||||
iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)
|
iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)
|
||||||
i := int64(0)
|
i := int64(0)
|
||||||
for ; iterator.Valid(); iterator.Next() {
|
for ; iterator.Valid(); iterator.Next() {
|
||||||
bz := iterator.Value()
|
addr := iterator.Key()[1:]
|
||||||
var validator types.Validator
|
validator := types.UnmarshalValidator(k.cdc, addr, iterator.Value())
|
||||||
k.cdc.MustUnmarshalBinary(bz, &validator)
|
|
||||||
stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to?
|
stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to?
|
||||||
if stop {
|
if stop {
|
||||||
break
|
break
|
||||||
|
|
|
@ -14,11 +14,12 @@ import (
|
||||||
// get a single validator
|
// get a single validator
|
||||||
func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.Address) (validator types.Validator, found bool) {
|
func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.Address) (validator types.Validator, found bool) {
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
b := store.Get(GetValidatorKey(addr))
|
value := store.Get(GetValidatorKey(addr))
|
||||||
if b == nil {
|
if value == nil {
|
||||||
return validator, false
|
return validator, false
|
||||||
}
|
}
|
||||||
k.cdc.MustUnmarshalBinary(b, &validator)
|
fmt.Printf("debug addr: %v\n", addr)
|
||||||
|
validator = types.UnmarshalValidator(k.cdc, addr, value)
|
||||||
return validator, true
|
return validator, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,15 +36,13 @@ func (k Keeper) GetValidatorByPubKey(ctx sdk.Context, pubkey crypto.PubKey) (val
|
||||||
// set the main record holding validator details
|
// set the main record holding validator details
|
||||||
func (k Keeper) SetValidator(ctx sdk.Context, validator types.Validator) {
|
func (k Keeper) SetValidator(ctx sdk.Context, validator types.Validator) {
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
// set main store
|
bz := types.MarshalValidator(k.cdc, validator)
|
||||||
bz := k.cdc.MustMarshalBinary(validator)
|
|
||||||
store.Set(GetValidatorKey(validator.Owner), bz)
|
store.Set(GetValidatorKey(validator.Owner), bz)
|
||||||
}
|
}
|
||||||
|
|
||||||
// validator index
|
// validator index
|
||||||
func (k Keeper) SetValidatorByPubKeyIndex(ctx sdk.Context, validator types.Validator) {
|
func (k Keeper) SetValidatorByPubKeyIndex(ctx sdk.Context, validator types.Validator) {
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
// set pointer by pubkey
|
|
||||||
store.Set(GetValidatorByPubKeyIndexKey(validator.PubKey), validator.Owner)
|
store.Set(GetValidatorByPubKeyIndexKey(validator.PubKey), validator.Owner)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,9 +74,8 @@ func (k Keeper) GetAllValidators(ctx sdk.Context) (validators []types.Validator)
|
||||||
if !iterator.Valid() {
|
if !iterator.Valid() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
bz := iterator.Value()
|
addr := iterator.Key()[1:]
|
||||||
var validator types.Validator
|
validator := types.UnmarshalValidator(k.cdc, addr, iterator.Value())
|
||||||
k.cdc.MustUnmarshalBinary(bz, &validator)
|
|
||||||
validators = append(validators, validator)
|
validators = append(validators, validator)
|
||||||
iterator.Next()
|
iterator.Next()
|
||||||
}
|
}
|
||||||
|
@ -96,9 +94,8 @@ func (k Keeper) GetValidators(ctx sdk.Context, maxRetrieve int16) (validators []
|
||||||
if !iterator.Valid() || i > int(maxRetrieve-1) {
|
if !iterator.Valid() || i > int(maxRetrieve-1) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
bz := iterator.Value()
|
addr := iterator.Key()[1:]
|
||||||
var validator types.Validator
|
validator := types.UnmarshalValidator(k.cdc, addr, iterator.Value())
|
||||||
k.cdc.MustUnmarshalBinary(bz, &validator)
|
|
||||||
validators[i] = validator
|
validators[i] = validator
|
||||||
iterator.Next()
|
iterator.Next()
|
||||||
}
|
}
|
||||||
|
@ -205,8 +202,7 @@ func (k Keeper) UpdateValidator(ctx sdk.Context, validator types.Validator) type
|
||||||
|
|
||||||
// always update the main list ordered by owner address before exiting
|
// always update the main list ordered by owner address before exiting
|
||||||
defer func() {
|
defer func() {
|
||||||
bz := k.cdc.MustMarshalBinary(validator)
|
k.SetValidator(ctx, validator)
|
||||||
store.Set(GetValidatorKey(ownerAddr), bz)
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// retrieve the old validator record
|
// retrieve the old validator record
|
||||||
|
@ -441,8 +437,7 @@ func (k Keeper) unbondValidator(ctx sdk.Context, validator types.Validator) type
|
||||||
k.SetPool(ctx, pool)
|
k.SetPool(ctx, pool)
|
||||||
|
|
||||||
// save the now unbonded validator record
|
// save the now unbonded validator record
|
||||||
bzVal := k.cdc.MustMarshalBinary(validator)
|
k.SetValidator(ctx, validator)
|
||||||
store.Set(GetValidatorKey(validator.Owner), bzVal)
|
|
||||||
|
|
||||||
// add to accumulated changes for tendermint
|
// add to accumulated changes for tendermint
|
||||||
bzABCI := k.cdc.MustMarshalBinary(validator.ABCIValidatorZero())
|
bzABCI := k.cdc.MustMarshalBinary(validator.ABCIValidatorZero())
|
||||||
|
@ -469,8 +464,7 @@ func (k Keeper) bondValidator(ctx sdk.Context, validator types.Validator) types.
|
||||||
k.SetPool(ctx, pool)
|
k.SetPool(ctx, pool)
|
||||||
|
|
||||||
// save the now bonded validator record to the three referenced stores
|
// save the now bonded validator record to the three referenced stores
|
||||||
bzVal := k.cdc.MustMarshalBinary(validator)
|
k.SetValidator(ctx, validator)
|
||||||
store.Set(GetValidatorKey(validator.Owner), bzVal)
|
|
||||||
store.Set(GetValidatorsBondedIndexKey(validator.Owner), []byte{})
|
store.Set(GetValidatorsBondedIndexKey(validator.Owner), []byte{})
|
||||||
|
|
||||||
// add to accumulated changes for tendermint
|
// add to accumulated changes for tendermint
|
||||||
|
|
|
@ -118,7 +118,7 @@ func MarshalUBD(cdc *wire.Codec, ubd UnbondingDelegation) []byte {
|
||||||
return cdc.MustMarshalBinary(val)
|
return cdc.MustMarshalBinary(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the unbonding delegation without fields contained within the key for the store
|
// unmarshal a unbonding delegation from a store key and value
|
||||||
func UnmarshalUBD(cdc *wire.Codec, key, value []byte) UnbondingDelegation {
|
func UnmarshalUBD(cdc *wire.Codec, key, value []byte) UnbondingDelegation {
|
||||||
var storeValue ubdValue
|
var storeValue ubdValue
|
||||||
cdc.MustUnmarshalBinary(value, &storeValue)
|
cdc.MustUnmarshalBinary(value, &storeValue)
|
||||||
|
@ -192,7 +192,7 @@ type redValue struct {
|
||||||
SharesDst sdk.Rat
|
SharesDst sdk.Rat
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the unbonding delegation without fields contained within the key for the store
|
// return the redelegation without fields contained within the key for the store
|
||||||
func MarshalRED(cdc *wire.Codec, red Redelegation) []byte {
|
func MarshalRED(cdc *wire.Codec, red Redelegation) []byte {
|
||||||
val := redValue{
|
val := redValue{
|
||||||
red.CreationHeight,
|
red.CreationHeight,
|
||||||
|
@ -205,7 +205,7 @@ func MarshalRED(cdc *wire.Codec, red Redelegation) []byte {
|
||||||
return cdc.MustMarshalBinary(val)
|
return cdc.MustMarshalBinary(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the unbonding delegation without fields contained within the key for the store
|
// unmarshal a redelegation from a store key and value
|
||||||
func UnmarshalRED(cdc *wire.Codec, key, value []byte) Redelegation {
|
func UnmarshalRED(cdc *wire.Codec, key, value []byte) Redelegation {
|
||||||
var storeValue redValue
|
var storeValue redValue
|
||||||
cdc.MustUnmarshalBinary(value, &storeValue)
|
cdc.MustUnmarshalBinary(value, &storeValue)
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
tmtypes "github.com/tendermint/tendermint/types"
|
tmtypes "github.com/tendermint/tendermint/types"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Validator defines the total amount of bond shares and their exchange rate to
|
// Validator defines the total amount of bond shares and their exchange rate to
|
||||||
|
@ -60,6 +61,70 @@ func NewValidator(owner sdk.Address, pubKey crypto.PubKey, description Descripti
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// what's kept in the store value
|
||||||
|
type validatorValue struct {
|
||||||
|
PubKey crypto.PubKey
|
||||||
|
Revoked bool
|
||||||
|
PoolShares PoolShares
|
||||||
|
DelegatorShares sdk.Rat
|
||||||
|
Description Description
|
||||||
|
BondHeight int64
|
||||||
|
BondIntraTxCounter int16
|
||||||
|
ProposerRewardPool sdk.Coins
|
||||||
|
Commission sdk.Rat
|
||||||
|
CommissionMax sdk.Rat
|
||||||
|
CommissionChangeRate sdk.Rat
|
||||||
|
CommissionChangeToday sdk.Rat
|
||||||
|
PrevBondedShares sdk.Rat
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the redelegation without fields contained within the key for the store
|
||||||
|
func MarshalValidator(cdc *wire.Codec, validator Validator) []byte {
|
||||||
|
val := validatorValue{
|
||||||
|
PubKey: validator.PubKey,
|
||||||
|
Revoked: validator.Revoked,
|
||||||
|
PoolShares: validator.PoolShares,
|
||||||
|
DelegatorShares: validator.DelegatorShares,
|
||||||
|
Description: validator.Description,
|
||||||
|
BondHeight: validator.BondHeight,
|
||||||
|
BondIntraTxCounter: validator.BondIntraTxCounter,
|
||||||
|
ProposerRewardPool: validator.ProposerRewardPool,
|
||||||
|
Commission: validator.Commission,
|
||||||
|
CommissionMax: validator.CommissionMax,
|
||||||
|
CommissionChangeRate: validator.CommissionChangeRate,
|
||||||
|
CommissionChangeToday: validator.CommissionChangeToday,
|
||||||
|
PrevBondedShares: validator.PrevBondedShares,
|
||||||
|
}
|
||||||
|
return cdc.MustMarshalBinary(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
// unmarshal a redelegation from a store key and value
|
||||||
|
func UnmarshalValidator(cdc *wire.Codec, ownerAddr, value []byte) Validator {
|
||||||
|
var storeValue validatorValue
|
||||||
|
cdc.MustUnmarshalBinary(value, &storeValue)
|
||||||
|
|
||||||
|
if len(ownerAddr) != 20 {
|
||||||
|
panic("unexpected address length")
|
||||||
|
}
|
||||||
|
|
||||||
|
return Validator{
|
||||||
|
Owner: ownerAddr,
|
||||||
|
PubKey: storeValue.PubKey,
|
||||||
|
Revoked: storeValue.Revoked,
|
||||||
|
PoolShares: storeValue.PoolShares,
|
||||||
|
DelegatorShares: storeValue.DelegatorShares,
|
||||||
|
Description: storeValue.Description,
|
||||||
|
BondHeight: storeValue.BondHeight,
|
||||||
|
BondIntraTxCounter: storeValue.BondIntraTxCounter,
|
||||||
|
ProposerRewardPool: storeValue.ProposerRewardPool,
|
||||||
|
Commission: storeValue.Commission,
|
||||||
|
CommissionMax: storeValue.CommissionMax,
|
||||||
|
CommissionChangeRate: storeValue.CommissionChangeRate,
|
||||||
|
CommissionChangeToday: storeValue.CommissionChangeToday,
|
||||||
|
PrevBondedShares: storeValue.PrevBondedShares,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// only the vitals - does not check bond height of IntraTxCounter
|
// only the vitals - does not check bond height of IntraTxCounter
|
||||||
func (v Validator) Equal(c2 Validator) bool {
|
func (v Validator) Equal(c2 Validator) bool {
|
||||||
return v.PubKey.Equals(c2.PubKey) &&
|
return v.PubKey.Equals(c2.PubKey) &&
|
||||||
|
|
Loading…
Reference in New Issue