From 04921b9ebd640b3b5f3c9d6dabcc73d438976c88 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 4 Jul 2018 00:29:02 -0400 Subject: [PATCH] ubd and red more limited values --- x/stake/client/cli/query.go | 24 ++++------ x/stake/client/rest/query.go | 16 +------ x/stake/keeper/delegation.go | 60 ++++++++++++------------ x/stake/types/delegation.go | 90 +++++++++++++++++++++++++++++++++++- 4 files changed, 130 insertions(+), 60 deletions(-) diff --git a/x/stake/client/cli/query.go b/x/stake/client/cli/query.go index e66a3bb4d5..d50b0d14db 100644 --- a/x/stake/client/cli/query.go +++ b/x/stake/client/cli/query.go @@ -75,9 +75,9 @@ func GetCmdQueryValidators(storeName string, cdc *wire.Codec) *cobra.Command { // parse out the validators var validators []stake.Validator - for _, KV := range resKVs { + for _, kv := range resKVs { var validator stake.Validator - cdc.MustUnmarshalBinary(KV.Value, &validator) + cdc.MustUnmarshalBinary(kv.Value, &validator) validators = append(validators, validator) } @@ -178,8 +178,8 @@ func GetCmdQueryDelegations(storeName string, cdc *wire.Codec) *cobra.Command { // parse out the validators var delegations []stake.Delegation - for _, KV := range resKVs { - delegation := types.UnmarshalDelegation(cdc, KV.Key, KV.Value) + for _, kv := range resKVs { + delegation := types.UnmarshalDelegation(cdc, kv.Key, kv.Value) delegations = append(delegations, delegation) } @@ -221,7 +221,7 @@ func GetCmdQueryUnbondingDelegation(storeName string, cdc *wire.Codec) *cobra.Co } // parse out the unbonding delegation - ubd := new(stake.UnbondingDelegation) + ubd := types.UnmarshalUBD(cdc, key, res) switch viper.Get(cli.OutputFlag) { case "text": @@ -231,7 +231,6 @@ func GetCmdQueryUnbondingDelegation(storeName string, cdc *wire.Codec) *cobra.Co } fmt.Println(resp) case "json": - cdc.MustUnmarshalBinary(res, ubd) output, err := wire.MarshalJSONIndent(cdc, ubd) if err != nil { return err @@ -269,9 +268,8 @@ func GetCmdQueryUnbondingDelegations(storeName string, cdc *wire.Codec) *cobra.C // parse out the validators var ubds []stake.UnbondingDelegation - for _, KV := range resKVs { - var ubd stake.UnbondingDelegation - cdc.MustUnmarshalBinary(KV.Value, &ubd) + for _, kv := range resKVs { + ubd := types.UnmarshalUBD(cdc, kv.Key, kv.Value) ubds = append(ubds, ubd) } @@ -316,7 +314,7 @@ func GetCmdQueryRedelegation(storeName string, cdc *wire.Codec) *cobra.Command { } // parse out the unbonding delegation - red := new(stake.Redelegation) + red := types.UnmarshalRED(cdc, key, res) switch viper.Get(cli.OutputFlag) { case "text": @@ -326,7 +324,6 @@ func GetCmdQueryRedelegation(storeName string, cdc *wire.Codec) *cobra.Command { } fmt.Println(resp) case "json": - cdc.MustUnmarshalBinary(res, red) output, err := wire.MarshalJSONIndent(cdc, red) if err != nil { return err @@ -364,9 +361,8 @@ func GetCmdQueryRedelegations(storeName string, cdc *wire.Codec) *cobra.Command // parse out the validators var reds []stake.Redelegation - for _, KV := range resKVs { - var red stake.Redelegation - cdc.MustUnmarshalBinary(KV.Value, &red) + for _, kv := range resKVs { + red := types.UnmarshalRED(cdc, kv.Key, kv.Value) reds = append(reds, red) } diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index b19cea72b7..457806de08 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -128,13 +128,7 @@ func ubdHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc { return } - var ubd stake.UnbondingDelegation - err = cdc.UnmarshalBinary(res, &ubd) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf("couldn't decode unbonding-delegation. Error: %s", err.Error()))) - return - } + ubd := types.UnmarshalUBD(cdc, key, res) output, err := cdc.MarshalJSON(ubd) if err != nil { @@ -193,13 +187,7 @@ func redHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc { return } - var red stake.Redelegation - err = cdc.UnmarshalBinary(res, &red) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf("couldn't decode redelegation. Error: %s", err.Error()))) - return - } + red := types.UnmarshalRED(cdc, key, res) output, err := cdc.MarshalJSON(red) if err != nil { diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index 9c56b85125..3d949a9a0a 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -82,49 +82,48 @@ func (k Keeper) GetUnbondingDelegation(ctx sdk.Context, DelegatorAddr, ValidatorAddr sdk.Address) (ubd types.UnbondingDelegation, found bool) { store := ctx.KVStore(k.storeKey) - ubdKey := GetUBDKey(DelegatorAddr, ValidatorAddr) - bz := store.Get(ubdKey) - if bz == nil { + key := GetUBDKey(DelegatorAddr, ValidatorAddr) + value := store.Get(key) + if value == nil { return ubd, false } - k.cdc.MustUnmarshalBinary(bz, &ubd) + ubd = types.UnmarshalUBD(k.cdc, key, value) return ubd, true } // 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) iterator := sdk.KVStorePrefixIterator(store, GetUBDsByValIndexKey(valAddr)) for { if !iterator.Valid() { break } - unbondingKey := GetUBDKeyFromValIndexKey(iterator.Key()) - unbondingBytes := store.Get(unbondingKey) - var unbondingDelegation types.UnbondingDelegation - k.cdc.MustUnmarshalBinary(unbondingBytes, &unbondingDelegation) - unbondingDelegations = append(unbondingDelegations, unbondingDelegation) + key := GetUBDKeyFromValIndexKey(iterator.Key()) + value := store.Get(key) + ubd := types.UnmarshalUBD(k.cdc, key, value) + ubds = append(ubds, ubd) iterator.Next() } iterator.Close() - return unbondingDelegations + return ubds } // set the unbonding delegation and associated index func (k Keeper) SetUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(ubd) - ubdKey := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) - store.Set(ubdKey, bz) - store.Set(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr), []byte{}) + bz := types.MarshalUBD(k.cdc, ubd) + key := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) + store.Set(key, bz) + store.Set(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr), []byte{}) // index, store empty bytes } // remove the unbonding delegation object and associated index func (k Keeper) RemoveUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { store := ctx.KVStore(k.storeKey) - ubdKey := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) - store.Delete(ubdKey) + key := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) + store.Delete(key) store.Delete(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr)) } @@ -135,33 +134,32 @@ func (k Keeper) GetRedelegation(ctx sdk.Context, DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr sdk.Address) (red types.Redelegation, found bool) { store := ctx.KVStore(k.storeKey) - redKey := GetREDKey(DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr) - bz := store.Get(redKey) - if bz == nil { + key := GetREDKey(DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr) + value := store.Get(key) + if value == nil { return red, false } - k.cdc.MustUnmarshalBinary(bz, &red) + red = types.UnmarshalRED(k.cdc, key, value) return red, true } // 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) iterator := sdk.KVStorePrefixIterator(store, GetREDsFromValSrcIndexKey(valAddr)) for { if !iterator.Valid() { break } - redelegationKey := GetREDKeyFromValSrcIndexKey(iterator.Key()) - redelegationBytes := store.Get(redelegationKey) - var redelegation types.Redelegation - k.cdc.MustUnmarshalBinary(redelegationBytes, &redelegation) - redelegations = append(redelegations, redelegation) + key := GetREDKeyFromValSrcIndexKey(iterator.Key()) + value := store.Get(key) + red := types.UnmarshalRED(k.cdc, key, value) + reds = append(reds, red) iterator.Next() } iterator.Close() - return redelegations + return reds } // has a redelegation @@ -184,9 +182,9 @@ func (k Keeper) HasReceivingRedelegation(ctx sdk.Context, // set a redelegation and associated index func (k Keeper) SetRedelegation(ctx sdk.Context, red types.Redelegation) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(red) - redKey := GetREDKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr) - store.Set(redKey, bz) + bz := types.MarshalRED(k.cdc, red) + key := GetREDKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr) + store.Set(key, bz) store.Set(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), []byte{}) store.Set(GetREDByValDstIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), []byte{}) } diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index b6d35fb560..0a3c9cf4fc 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -39,7 +39,7 @@ func UnmarshalDelegation(cdc *wire.Codec, key, value []byte) Delegation { addrs := key[1:] // remove prefix bytes if len(addrs) != 40 { - panic("key length not even") + panic("unexpected key length") } delAddr := sdk.Address(addrs[:20]) valAddr := sdk.Address(addrs[20:]) @@ -100,6 +100,46 @@ type UnbondingDelegation struct { Balance sdk.Coin `json:"balance"` // atoms to receive at completion } +type ubdValue struct { + CreationHeight int64 + MinTime int64 + InitialBalance sdk.Coin + Balance sdk.Coin +} + +// return the unbonding delegation without fields contained within the key for the store +func MarshalUBD(cdc *wire.Codec, ubd UnbondingDelegation) []byte { + val := ubdValue{ + ubd.CreationHeight, + ubd.MinTime, + ubd.InitialBalance, + ubd.Balance, + } + return cdc.MustMarshalBinary(val) +} + +// return the unbonding delegation without fields contained within the key for the store +func UnmarshalUBD(cdc *wire.Codec, key, value []byte) UnbondingDelegation { + var storeValue ubdValue + cdc.MustUnmarshalBinary(value, &storeValue) + + addrs := key[1:] // remove prefix bytes + if len(addrs) != 40 { + panic("unexpected key length") + } + delAddr := sdk.Address(addrs[:20]) + valAddr := sdk.Address(addrs[20:]) + + return UnbondingDelegation{ + DelegatorAddr: delAddr, + ValidatorAddr: valAddr, + CreationHeight: storeValue.CreationHeight, + MinTime: storeValue.MinTime, + InitialBalance: storeValue.InitialBalance, + Balance: storeValue.Balance, + } +} + // nolint func (d UnbondingDelegation) Equal(d2 UnbondingDelegation) bool { bz1 := MsgCdc.MustMarshalBinary(&d) @@ -143,6 +183,54 @@ type Redelegation struct { SharesDst sdk.Rat `json:"shares_dst"` // amount of destination shares redelegating } +type redValue struct { + CreationHeight int64 + MinTime int64 + InitialBalance sdk.Coin + Balance sdk.Coin + SharesSrc sdk.Rat + SharesDst sdk.Rat +} + +// return the unbonding delegation without fields contained within the key for the store +func MarshalRED(cdc *wire.Codec, red Redelegation) []byte { + val := redValue{ + red.CreationHeight, + red.MinTime, + red.InitialBalance, + red.Balance, + red.SharesSrc, + red.SharesDst, + } + return cdc.MustMarshalBinary(val) +} + +// return the unbonding delegation without fields contained within the key for the store +func UnmarshalRED(cdc *wire.Codec, key, value []byte) Redelegation { + var storeValue redValue + cdc.MustUnmarshalBinary(value, &storeValue) + + addrs := key[1:] // remove prefix bytes + if len(addrs) != 60 { + panic("unexpected key length") + } + delAddr := sdk.Address(addrs[:20]) + valSrcAddr := sdk.Address(addrs[20:40]) + valDstAddr := sdk.Address(addrs[40:60]) + + 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, + } +} + // nolint func (d Redelegation) Equal(d2 Redelegation) bool { bz1 := MsgCdc.MustMarshalBinary(&d)