Merge PR #1548: No keys fields in state value

This commit is contained in:
Christopher Goes 2018-07-05 00:39:06 +02:00 committed by GitHub
commit 08625633c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 359 additions and 127 deletions

View File

@ -37,8 +37,9 @@ BREAKING CHANGES
* Add REST endpoint to unrevoke a validator previously revoked for downtime * Add REST endpoint to unrevoke a validator previously revoked for downtime
* Add REST endpoint to retrieve liveness signing information for a validator * Add REST endpoint to retrieve liveness signing information for a validator
* [types] renamed rational.Evaluate to rational.Round{Int64, Int} * [types] renamed rational.Evaluate to rational.Round{Int64, Int}
* [stake] most index keys nolonger hold a value - inputs are rearranged to form the desired key * [x/stake] most index keys nolonger hold a value - inputs are rearranged to form the desired key
* [lcd] Switch key creation output to return bech32 * [lcd] Switch key creation output to return bech32
* [x/stake] store-value for delegation, validator, ubd, and red do not hold duplicate information contained store-key
FEATURES FEATURES
* [gaiacli] You can now attach a simple text-only memo to any transaction, with the `--memo` flag * [gaiacli] You can now attach a simple text-only memo to any transaction, with the `--memo` flag

View File

@ -13,8 +13,12 @@ import (
//Address is a go crypto-style Address //Address is a go crypto-style Address
type Address = cmn.HexBytes type Address = cmn.HexBytes
// Bech32 prefixes // nolint
const ( const (
// expected address length
AddrLen = 20
// Bech32 prefixes
Bech32PrefixAccAddr = "cosmosaccaddr" Bech32PrefixAccAddr = "cosmosaccaddr"
Bech32PrefixAccPub = "cosmosaccpub" Bech32PrefixAccPub = "cosmosaccpub"
Bech32PrefixValAddr = "cosmosvaladdr" Bech32PrefixValAddr = "cosmosvaladdr"

View File

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

View File

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

View File

@ -11,6 +11,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/stake" "github.com/cosmos/cosmos-sdk/x/stake"
"github.com/cosmos/cosmos-sdk/x/stake/types"
) )
// get the command to query a validator // get the command to query a validator
@ -31,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.MustUnmarshalValidator(cdc, addr, res)
switch viper.Get(cli.OutputFlag) { switch viper.Get(cli.OutputFlag) {
case "text": case "text":
@ -74,9 +75,9 @@ 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.MustUnmarshalValidator(cdc, addr, kv.Value)
validators = append(validators, validator) validators = append(validators, validator)
} }
@ -130,7 +131,7 @@ func GetCmdQueryDelegation(storeName string, cdc *wire.Codec) *cobra.Command {
} }
// parse out the delegation // parse out the delegation
delegation := new(stake.Delegation) delegation := types.MustUnmarshalDelegation(cdc, key, res)
switch viper.Get(cli.OutputFlag) { switch viper.Get(cli.OutputFlag) {
case "text": case "text":
@ -140,7 +141,6 @@ func GetCmdQueryDelegation(storeName string, cdc *wire.Codec) *cobra.Command {
} }
fmt.Println(resp) fmt.Println(resp)
case "json": case "json":
cdc.MustUnmarshalBinary(res, delegation)
output, err := wire.MarshalJSONIndent(cdc, delegation) output, err := wire.MarshalJSONIndent(cdc, delegation)
if err != nil { if err != nil {
return err return err
@ -178,9 +178,8 @@ func GetCmdQueryDelegations(storeName string, cdc *wire.Codec) *cobra.Command {
// parse out the validators // parse out the validators
var delegations []stake.Delegation var delegations []stake.Delegation
for _, KV := range resKVs { for _, kv := range resKVs {
var delegation stake.Delegation delegation := types.MustUnmarshalDelegation(cdc, kv.Key, kv.Value)
cdc.MustUnmarshalBinary(KV.Value, &delegation)
delegations = append(delegations, delegation) delegations = append(delegations, delegation)
} }
@ -222,7 +221,7 @@ func GetCmdQueryUnbondingDelegation(storeName string, cdc *wire.Codec) *cobra.Co
} }
// parse out the unbonding delegation // parse out the unbonding delegation
ubd := new(stake.UnbondingDelegation) ubd := types.MustUnmarshalUBD(cdc, key, res)
switch viper.Get(cli.OutputFlag) { switch viper.Get(cli.OutputFlag) {
case "text": case "text":
@ -232,7 +231,6 @@ func GetCmdQueryUnbondingDelegation(storeName string, cdc *wire.Codec) *cobra.Co
} }
fmt.Println(resp) fmt.Println(resp)
case "json": case "json":
cdc.MustUnmarshalBinary(res, ubd)
output, err := wire.MarshalJSONIndent(cdc, ubd) output, err := wire.MarshalJSONIndent(cdc, ubd)
if err != nil { if err != nil {
return err return err
@ -270,9 +268,8 @@ func GetCmdQueryUnbondingDelegations(storeName string, cdc *wire.Codec) *cobra.C
// parse out the validators // parse out the validators
var ubds []stake.UnbondingDelegation var ubds []stake.UnbondingDelegation
for _, KV := range resKVs { for _, kv := range resKVs {
var ubd stake.UnbondingDelegation ubd := types.MustUnmarshalUBD(cdc, kv.Key, kv.Value)
cdc.MustUnmarshalBinary(KV.Value, &ubd)
ubds = append(ubds, ubd) ubds = append(ubds, ubd)
} }
@ -317,7 +314,7 @@ func GetCmdQueryRedelegation(storeName string, cdc *wire.Codec) *cobra.Command {
} }
// parse out the unbonding delegation // parse out the unbonding delegation
red := new(stake.Redelegation) red := types.MustUnmarshalRED(cdc, key, res)
switch viper.Get(cli.OutputFlag) { switch viper.Get(cli.OutputFlag) {
case "text": case "text":
@ -327,7 +324,6 @@ func GetCmdQueryRedelegation(storeName string, cdc *wire.Codec) *cobra.Command {
} }
fmt.Println(resp) fmt.Println(resp)
case "json": case "json":
cdc.MustUnmarshalBinary(res, red)
output, err := wire.MarshalJSONIndent(cdc, red) output, err := wire.MarshalJSONIndent(cdc, red)
if err != nil { if err != nil {
return err return err
@ -365,9 +361,8 @@ func GetCmdQueryRedelegations(storeName string, cdc *wire.Codec) *cobra.Command
// parse out the validators // parse out the validators
var reds []stake.Redelegation var reds []stake.Redelegation
for _, KV := range resKVs { for _, kv := range resKVs {
var red stake.Redelegation red := types.MustUnmarshalRED(cdc, kv.Key, kv.Value)
cdc.MustUnmarshalBinary(KV.Value, &red)
reds = append(reds, red) reds = append(reds, red)
} }

View File

@ -9,7 +9,9 @@ import (
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/stake" "github.com/cosmos/cosmos-sdk/x/stake"
"github.com/cosmos/cosmos-sdk/x/stake/types"
) )
const storeName = "stake" const storeName = "stake"
@ -75,11 +77,10 @@ func delegationHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerF
return return
} }
var delegation stake.Delegation delegation, err := types.UnmarshalDelegation(cdc, key, res)
err = cdc.UnmarshalBinary(res, &delegation)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("couldn't decode delegation. Error: %s", err.Error()))) w.Write([]byte(err.Error()))
return return
} }
@ -132,11 +133,10 @@ func ubdHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc {
return return
} }
var ubd stake.UnbondingDelegation ubd, err := types.UnmarshalUBD(cdc, key, res)
err = cdc.UnmarshalBinary(res, &ubd)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("couldn't decode unbonding-delegation. Error: %s", err.Error()))) w.Write([]byte(fmt.Sprintf("couldn't query unbonding-delegation. Error: %s", err.Error())))
return return
} }
@ -197,11 +197,10 @@ func redHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc {
return return
} }
var red stake.Redelegation red, err := types.UnmarshalRED(cdc, key, res)
err = cdc.UnmarshalBinary(res, &red)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("couldn't decode redelegation. Error: %s", err.Error()))) w.Write([]byte(fmt.Sprintf("couldn't query unbonding-delegation. Error: %s", err.Error())))
return return
} }
@ -291,15 +290,19 @@ 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, err := types.UnmarshalValidator(cdc, addr, kv.Value)
if err == nil {
bech32Validator, err = bech32StakeValidatorOutput(validator)
}
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("couldn't decode validator. Error: %s", err.Error()))) w.Write([]byte(fmt.Sprintf("couldn't query unbonding-delegation. Error: %s", err.Error())))
return
}
bech32Validator, err := bech32StakeValidatorOutput(validator)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return return
} }
validators[i] = bech32Validator validators[i] = bech32Validator

View File

@ -12,12 +12,13 @@ func (k Keeper) GetDelegation(ctx sdk.Context,
delegatorAddr, validatorAddr sdk.Address) (delegation types.Delegation, found bool) { delegatorAddr, validatorAddr sdk.Address) (delegation types.Delegation, found bool) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
delegatorBytes := store.Get(GetDelegationKey(delegatorAddr, validatorAddr)) key := GetDelegationKey(delegatorAddr, validatorAddr)
if delegatorBytes == nil { value := store.Get(key)
if value == nil {
return delegation, false return delegation, false
} }
k.cdc.MustUnmarshalBinary(delegatorBytes, &delegation) delegation = types.MustUnmarshalDelegation(k.cdc, key, value)
return delegation, true return delegation, true
} }
@ -31,9 +32,7 @@ func (k Keeper) GetAllDelegations(ctx sdk.Context) (delegations []types.Delegati
if !iterator.Valid() { if !iterator.Valid() {
break break
} }
bondBytes := iterator.Value() delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value())
var delegation types.Delegation
k.cdc.MustUnmarshalBinary(bondBytes, &delegation)
delegations = append(delegations, delegation) delegations = append(delegations, delegation)
iterator.Next() iterator.Next()
} }
@ -55,9 +54,7 @@ func (k Keeper) GetDelegations(ctx sdk.Context, delegator sdk.Address,
if !iterator.Valid() || i > int(maxRetrieve-1) { if !iterator.Valid() || i > int(maxRetrieve-1) {
break break
} }
bondBytes := iterator.Value() delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value())
var delegation types.Delegation
k.cdc.MustUnmarshalBinary(bondBytes, &delegation)
delegations[i] = delegation delegations[i] = delegation
iterator.Next() iterator.Next()
} }
@ -68,7 +65,7 @@ func (k Keeper) GetDelegations(ctx sdk.Context, delegator sdk.Address,
// set the delegation // set the delegation
func (k Keeper) SetDelegation(ctx sdk.Context, delegation types.Delegation) { func (k Keeper) SetDelegation(ctx sdk.Context, delegation types.Delegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinary(delegation) b := types.MustMarshalDelegation(k.cdc, delegation)
store.Set(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr), b) store.Set(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr), b)
} }
@ -85,49 +82,48 @@ func (k Keeper) GetUnbondingDelegation(ctx sdk.Context,
DelegatorAddr, ValidatorAddr sdk.Address) (ubd types.UnbondingDelegation, found bool) { DelegatorAddr, ValidatorAddr sdk.Address) (ubd types.UnbondingDelegation, found bool) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
ubdKey := GetUBDKey(DelegatorAddr, ValidatorAddr) key := GetUBDKey(DelegatorAddr, ValidatorAddr)
bz := store.Get(ubdKey) value := store.Get(key)
if bz == nil { if value == nil {
return ubd, false return ubd, false
} }
k.cdc.MustUnmarshalBinary(bz, &ubd) ubd = types.MustUnmarshalUBD(k.cdc, key, value)
return ubd, true return ubd, true
} }
// load all unbonding delegations from a particular validator // load all unbonding delegations from a particular validator
func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sdk.Address) (unbondingDelegations []types.UnbondingDelegation) { func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sdk.Address) (ubds []types.UnbondingDelegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, GetUBDsByValIndexKey(valAddr)) iterator := sdk.KVStorePrefixIterator(store, GetUBDsByValIndexKey(valAddr))
for { for {
if !iterator.Valid() { if !iterator.Valid() {
break break
} }
unbondingKey := GetUBDKeyFromValIndexKey(iterator.Key()) key := GetUBDKeyFromValIndexKey(iterator.Key())
unbondingBytes := store.Get(unbondingKey) value := store.Get(key)
var unbondingDelegation types.UnbondingDelegation ubd := types.MustUnmarshalUBD(k.cdc, key, value)
k.cdc.MustUnmarshalBinary(unbondingBytes, &unbondingDelegation) ubds = append(ubds, ubd)
unbondingDelegations = append(unbondingDelegations, unbondingDelegation)
iterator.Next() iterator.Next()
} }
iterator.Close() iterator.Close()
return unbondingDelegations return ubds
} }
// set the unbonding delegation and associated index // set the unbonding delegation and associated index
func (k Keeper) SetUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { func (k Keeper) SetUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinary(ubd) bz := types.MustMarshalUBD(k.cdc, ubd)
ubdKey := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) key := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr)
store.Set(ubdKey, bz) store.Set(key, bz)
store.Set(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr), []byte{}) store.Set(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr), []byte{}) // index, store empty bytes
} }
// remove the unbonding delegation object and associated index // remove the unbonding delegation object and associated index
func (k Keeper) RemoveUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { func (k Keeper) RemoveUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
ubdKey := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) key := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr)
store.Delete(ubdKey) store.Delete(key)
store.Delete(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr)) store.Delete(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr))
} }
@ -138,33 +134,32 @@ func (k Keeper) GetRedelegation(ctx sdk.Context,
DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr sdk.Address) (red types.Redelegation, found bool) { DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr sdk.Address) (red types.Redelegation, found bool) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
redKey := GetREDKey(DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr) key := GetREDKey(DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr)
bz := store.Get(redKey) value := store.Get(key)
if bz == nil { if value == nil {
return red, false return red, false
} }
k.cdc.MustUnmarshalBinary(bz, &red) red = types.MustUnmarshalRED(k.cdc, key, value)
return red, true return red, true
} }
// load all redelegations from a particular validator // load all redelegations from a particular validator
func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.Address) (redelegations []types.Redelegation) { func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.Address) (reds []types.Redelegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, GetREDsFromValSrcIndexKey(valAddr)) iterator := sdk.KVStorePrefixIterator(store, GetREDsFromValSrcIndexKey(valAddr))
for { for {
if !iterator.Valid() { if !iterator.Valid() {
break break
} }
redelegationKey := GetREDKeyFromValSrcIndexKey(iterator.Key()) key := GetREDKeyFromValSrcIndexKey(iterator.Key())
redelegationBytes := store.Get(redelegationKey) value := store.Get(key)
var redelegation types.Redelegation red := types.MustUnmarshalRED(k.cdc, key, value)
k.cdc.MustUnmarshalBinary(redelegationBytes, &redelegation) reds = append(reds, red)
redelegations = append(redelegations, redelegation)
iterator.Next() iterator.Next()
} }
iterator.Close() iterator.Close()
return redelegations return reds
} }
// has a redelegation // has a redelegation
@ -187,9 +182,9 @@ func (k Keeper) HasReceivingRedelegation(ctx sdk.Context,
// set a redelegation and associated index // set a redelegation and associated index
func (k Keeper) SetRedelegation(ctx sdk.Context, red types.Redelegation) { func (k Keeper) SetRedelegation(ctx sdk.Context, red types.Redelegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinary(red) bz := types.MustMarshalRED(k.cdc, red)
redKey := GetREDKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr) key := GetREDKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr)
store.Set(redKey, bz) store.Set(key, bz)
store.Set(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), []byte{}) store.Set(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), []byte{})
store.Set(GetREDByValDstIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), []byte{}) store.Set(GetREDByValDstIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), []byte{})
} }

View File

@ -131,11 +131,11 @@ func GetUBDByValIndexKey(delegatorAddr, validatorAddr sdk.Address) []byte {
// rearrange the ValIndexKey to get the UBDKey // rearrange the ValIndexKey to get the UBDKey
func GetUBDKeyFromValIndexKey(IndexKey []byte) []byte { func GetUBDKeyFromValIndexKey(IndexKey []byte) []byte {
addrs := IndexKey[1:] // remove prefix bytes addrs := IndexKey[1:] // remove prefix bytes
if len(addrs) != 40 { if len(addrs) != 2*sdk.AddrLen {
panic("unexpected key length") panic("unexpected key length")
} }
valAddr := addrs[:20] valAddr := addrs[:sdk.AddrLen]
delAddr := addrs[20:] delAddr := addrs[sdk.AddrLen:]
return GetUBDKey(delAddr, valAddr) return GetUBDKey(delAddr, valAddr)
} }
@ -189,12 +189,12 @@ func GetREDByValDstIndexKey(delegatorAddr, validatorSrcAddr,
// rearrange the ValSrcIndexKey to get the REDKey // rearrange the ValSrcIndexKey to get the REDKey
func GetREDKeyFromValSrcIndexKey(IndexKey []byte) []byte { func GetREDKeyFromValSrcIndexKey(IndexKey []byte) []byte {
addrs := IndexKey[1:] // remove prefix bytes addrs := IndexKey[1:] // remove prefix bytes
if len(addrs) != 60 { if len(addrs) != 3*sdk.AddrLen {
panic("unexpected key length") panic("unexpected key length")
} }
valSrcAddr := addrs[:20] valSrcAddr := addrs[:sdk.AddrLen]
delAddr := addrs[20:40] delAddr := addrs[sdk.AddrLen : 2*sdk.AddrLen]
valDstAddr := addrs[40:] valDstAddr := addrs[2*sdk.AddrLen:]
return GetREDKey(delAddr, valSrcAddr, valDstAddr) return GetREDKey(delAddr, valSrcAddr, valDstAddr)
} }
@ -202,12 +202,12 @@ func GetREDKeyFromValSrcIndexKey(IndexKey []byte) []byte {
// rearrange the ValDstIndexKey to get the REDKey // rearrange the ValDstIndexKey to get the REDKey
func GetREDKeyFromValDstIndexKey(IndexKey []byte) []byte { func GetREDKeyFromValDstIndexKey(IndexKey []byte) []byte {
addrs := IndexKey[1:] // remove prefix bytes addrs := IndexKey[1:] // remove prefix bytes
if len(addrs) != 60 { if len(addrs) != 3*sdk.AddrLen {
panic("unexpected key length") panic("unexpected key length")
} }
valDstAddr := addrs[:20] valDstAddr := addrs[:sdk.AddrLen]
delAddr := addrs[20:40] delAddr := addrs[sdk.AddrLen : 2*sdk.AddrLen]
valSrcAddr := addrs[40:] valSrcAddr := addrs[2*sdk.AddrLen:]
return GetREDKey(delAddr, valSrcAddr, valDstAddr) return GetREDKey(delAddr, valSrcAddr, valDstAddr)
} }

View File

@ -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.MustUnmarshalValidator(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
@ -91,9 +90,7 @@ func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.Address, fn func
iterator := sdk.KVStorePrefixIterator(store, key) iterator := sdk.KVStorePrefixIterator(store, key)
i := int64(0) i := int64(0)
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
bz := iterator.Value() delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value())
var delegation types.Delegation
k.cdc.MustUnmarshalBinary(bz, &delegation)
stop := fn(i, delegation) // XXX is this safe will the fields be able to get written to? stop := fn(i, delegation) // XXX is this safe will the fields be able to get written to?
if stop { if stop {
break break

View File

@ -14,11 +14,11 @@ 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) validator = types.MustUnmarshalValidator(k.cdc, addr, value)
return validator, true return validator, true
} }
@ -35,15 +35,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.MustMarshalValidator(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 +73,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.MustUnmarshalValidator(k.cdc, addr, iterator.Value())
k.cdc.MustUnmarshalBinary(bz, &validator)
validators = append(validators, validator) validators = append(validators, validator)
iterator.Next() iterator.Next()
} }
@ -96,9 +93,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.MustUnmarshalValidator(k.cdc, addr, iterator.Value())
k.cdc.MustUnmarshalBinary(bz, &validator)
validators[i] = validator validators[i] = validator
iterator.Next() iterator.Next()
} }
@ -205,8 +201,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 +436,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 +463,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

View File

@ -2,9 +2,11 @@ package types
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
) )
// Delegation represents the bond with tokens held by an account. It is // Delegation represents the bond with tokens held by an account. It is
@ -17,7 +19,54 @@ type Delegation struct {
Height int64 `json:"height"` // Last height bond updated Height int64 `json:"height"` // Last height bond updated
} }
// Equal returns a boolean determining if two Delegation types are identical. type delegationValue struct {
Shares sdk.Rat
Height int64
}
// return the delegation without fields contained within the key for the store
func MustMarshalDelegation(cdc *wire.Codec, delegation Delegation) []byte {
val := delegationValue{
delegation.Shares,
delegation.Height,
}
return cdc.MustMarshalBinary(val)
}
// return the delegation without fields contained within the key for the store
func MustUnmarshalDelegation(cdc *wire.Codec, key, value []byte) Delegation {
delegation, err := UnmarshalDelegation(cdc, key, value)
if err != nil {
panic(err)
}
return delegation
}
// return the delegation without fields contained within the key for the store
func UnmarshalDelegation(cdc *wire.Codec, key, value []byte) (delegation Delegation, err error) {
var storeValue delegationValue
err = cdc.UnmarshalBinary(value, &storeValue)
if err != nil {
return
}
addrs := key[1:] // remove prefix bytes
if len(addrs) != 2*sdk.AddrLen {
err = errors.New("unexpected key length")
return
}
delAddr := sdk.Address(addrs[:sdk.AddrLen])
valAddr := sdk.Address(addrs[sdk.AddrLen:])
return Delegation{
DelegatorAddr: delAddr,
ValidatorAddr: valAddr,
Shares: storeValue.Shares,
Height: storeValue.Height,
}, nil
}
// nolint
func (d Delegation) Equal(d2 Delegation) bool { func (d Delegation) Equal(d2 Delegation) bool {
return bytes.Equal(d.DelegatorAddr, d2.DelegatorAddr) && return bytes.Equal(d.DelegatorAddr, d2.DelegatorAddr) &&
bytes.Equal(d.ValidatorAddr, d2.ValidatorAddr) && bytes.Equal(d.ValidatorAddr, d2.ValidatorAddr) &&
@ -66,8 +115,60 @@ type UnbondingDelegation struct {
Balance sdk.Coin `json:"balance"` // atoms to receive at completion Balance sdk.Coin `json:"balance"` // atoms to receive at completion
} }
// Equal returns a boolean determining if two UnbondingDelegation types are type ubdValue struct {
// identical. CreationHeight int64
MinTime int64
InitialBalance sdk.Coin
Balance sdk.Coin
}
// return the unbonding delegation without fields contained within the key for the store
func MustMarshalUBD(cdc *wire.Codec, ubd UnbondingDelegation) []byte {
val := ubdValue{
ubd.CreationHeight,
ubd.MinTime,
ubd.InitialBalance,
ubd.Balance,
}
return cdc.MustMarshalBinary(val)
}
// unmarshal a unbonding delegation from a store key and value
func MustUnmarshalUBD(cdc *wire.Codec, key, value []byte) UnbondingDelegation {
ubd, err := UnmarshalUBD(cdc, key, value)
if err != nil {
panic(err)
}
return ubd
}
// unmarshal a unbonding delegation from a store key and value
func UnmarshalUBD(cdc *wire.Codec, key, value []byte) (ubd UnbondingDelegation, err error) {
var storeValue ubdValue
err = cdc.UnmarshalBinary(value, &storeValue)
if err != nil {
return
}
addrs := key[1:] // remove prefix bytes
if len(addrs) != 2*sdk.AddrLen {
err = errors.New("unexpected key length")
return
}
delAddr := sdk.Address(addrs[:sdk.AddrLen])
valAddr := sdk.Address(addrs[sdk.AddrLen:])
return UnbondingDelegation{
DelegatorAddr: delAddr,
ValidatorAddr: valAddr,
CreationHeight: storeValue.CreationHeight,
MinTime: storeValue.MinTime,
InitialBalance: storeValue.InitialBalance,
Balance: storeValue.Balance,
}, nil
}
// nolint
func (d UnbondingDelegation) Equal(d2 UnbondingDelegation) bool { func (d UnbondingDelegation) Equal(d2 UnbondingDelegation) bool {
bz1 := MsgCdc.MustMarshalBinary(&d) bz1 := MsgCdc.MustMarshalBinary(&d)
bz2 := MsgCdc.MustMarshalBinary(&d2) bz2 := MsgCdc.MustMarshalBinary(&d2)
@ -112,7 +213,68 @@ type Redelegation struct {
SharesDst sdk.Rat `json:"shares_dst"` // amount of destination shares redelegating SharesDst sdk.Rat `json:"shares_dst"` // amount of destination shares redelegating
} }
// Equal returns a boolean determining if two Redelegation types are identical. type redValue struct {
CreationHeight int64
MinTime int64
InitialBalance sdk.Coin
Balance sdk.Coin
SharesSrc sdk.Rat
SharesDst sdk.Rat
}
// return the redelegation without fields contained within the key for the store
func MustMarshalRED(cdc *wire.Codec, red Redelegation) []byte {
val := redValue{
red.CreationHeight,
red.MinTime,
red.InitialBalance,
red.Balance,
red.SharesSrc,
red.SharesDst,
}
return cdc.MustMarshalBinary(val)
}
// unmarshal a redelegation from a store key and value
func MustUnmarshalRED(cdc *wire.Codec, key, value []byte) Redelegation {
red, err := UnmarshalRED(cdc, key, value)
if err != nil {
panic(err)
}
return red
}
// unmarshal a redelegation from a store key and value
func UnmarshalRED(cdc *wire.Codec, key, value []byte) (red Redelegation, err error) {
var storeValue redValue
err = cdc.UnmarshalBinary(value, &storeValue)
if err != nil {
return
}
addrs := key[1:] // remove prefix bytes
if len(addrs) != 3*sdk.AddrLen {
err = errors.New("unexpected key length")
return
}
delAddr := sdk.Address(addrs[:sdk.AddrLen])
valSrcAddr := sdk.Address(addrs[sdk.AddrLen : 2*sdk.AddrLen])
valDstAddr := sdk.Address(addrs[2*sdk.AddrLen:])
return Redelegation{
DelegatorAddr: delAddr,
ValidatorSrcAddr: valSrcAddr,
ValidatorDstAddr: valDstAddr,
CreationHeight: storeValue.CreationHeight,
MinTime: storeValue.MinTime,
InitialBalance: storeValue.InitialBalance,
Balance: storeValue.Balance,
SharesSrc: storeValue.SharesSrc,
SharesDst: storeValue.SharesDst,
}, nil
}
// nolint
func (d Redelegation) Equal(d2 Redelegation) bool { func (d Redelegation) Equal(d2 Redelegation) bool {
bz1 := MsgCdc.MustMarshalBinary(&d) bz1 := MsgCdc.MustMarshalBinary(&d)
bz2 := MsgCdc.MustMarshalBinary(&d2) bz2 := MsgCdc.MustMarshalBinary(&d2)

View File

@ -2,12 +2,15 @@ package types
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types" tmtypes "github.com/tendermint/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
) )
const doNotModifyDescVal = "[do-not-modify]" const doNotModifyDescVal = "[do-not-modify]"
@ -61,7 +64,85 @@ func NewValidator(owner sdk.Address, pubKey crypto.PubKey, description Descripti
} }
} }
// Equal returns a boolean reflecting if two given validators are identical. // 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 MustMarshalValidator(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 MustUnmarshalValidator(cdc *wire.Codec, ownerAddr, value []byte) Validator {
validator, err := UnmarshalValidator(cdc, ownerAddr, value)
if err != nil {
panic(err)
}
return validator
}
// unmarshal a redelegation from a store key and value
func UnmarshalValidator(cdc *wire.Codec, ownerAddr, value []byte) (validator Validator, err error) {
var storeValue validatorValue
err = cdc.UnmarshalBinary(value, &storeValue)
if err != nil {
return
}
if len(ownerAddr) != 20 {
err = errors.New("unexpected address length")
return
}
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,
}, nil
}
// 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) &&
bytes.Equal(v.Owner, c2.Owner) && bytes.Equal(v.Owner, c2.Owner) &&