diff --git a/x/stake/client/cli/query.go b/x/stake/client/cli/query.go index 5a22f2c416..e66a3bb4d5 100644 --- a/x/stake/client/cli/query.go +++ b/x/stake/client/cli/query.go @@ -11,6 +11,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/x/stake" + "github.com/cosmos/cosmos-sdk/x/stake/types" ) // get the command to query a validator @@ -130,7 +131,7 @@ func GetCmdQueryDelegation(storeName string, cdc *wire.Codec) *cobra.Command { } // parse out the delegation - delegation := new(stake.Delegation) + delegation := types.UnmarshalDelegation(cdc, key, res) switch viper.Get(cli.OutputFlag) { case "text": @@ -140,7 +141,6 @@ func GetCmdQueryDelegation(storeName string, cdc *wire.Codec) *cobra.Command { } fmt.Println(resp) case "json": - cdc.MustUnmarshalBinary(res, delegation) output, err := wire.MarshalJSONIndent(cdc, delegation) if err != nil { return err @@ -179,8 +179,7 @@ func GetCmdQueryDelegations(storeName string, cdc *wire.Codec) *cobra.Command { // parse out the validators var delegations []stake.Delegation for _, KV := range resKVs { - var delegation stake.Delegation - cdc.MustUnmarshalBinary(KV.Value, &delegation) + delegation := types.UnmarshalDelegation(cdc, KV.Key, KV.Value) delegations = append(delegations, delegation) } diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 12ef882e5e..b19cea72b7 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -9,7 +9,9 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/stake" + "github.com/cosmos/cosmos-sdk/x/stake/types" ) const storeName = "stake" @@ -75,13 +77,7 @@ func delegationHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerF return } - var delegation stake.Delegation - err = cdc.UnmarshalBinary(res, &delegation) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf("couldn't decode delegation. Error: %s", err.Error()))) - return - } + delegation := types.UnmarshalDelegation(cdc, key, res) output, err := cdc.MarshalJSON(delegation) if err != nil { diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index bb8170a6a3..9c56b85125 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -12,12 +12,13 @@ func (k Keeper) GetDelegation(ctx sdk.Context, delegatorAddr, validatorAddr sdk.Address) (delegation types.Delegation, found bool) { store := ctx.KVStore(k.storeKey) - delegatorBytes := store.Get(GetDelegationKey(delegatorAddr, validatorAddr)) - if delegatorBytes == nil { + key := GetDelegationKey(delegatorAddr, validatorAddr) + value := store.Get(key) + if value == nil { return delegation, false } - k.cdc.MustUnmarshalBinary(delegatorBytes, &delegation) + delegation = types.UnmarshalDelegation(k.cdc, key, value) return delegation, true } @@ -31,9 +32,7 @@ func (k Keeper) GetAllDelegations(ctx sdk.Context) (delegations []types.Delegati if !iterator.Valid() { break } - bondBytes := iterator.Value() - var delegation types.Delegation - k.cdc.MustUnmarshalBinary(bondBytes, &delegation) + delegation := types.UnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value()) delegations = append(delegations, delegation) iterator.Next() } @@ -55,9 +54,7 @@ func (k Keeper) GetDelegations(ctx sdk.Context, delegator sdk.Address, if !iterator.Valid() || i > int(maxRetrieve-1) { break } - bondBytes := iterator.Value() - var delegation types.Delegation - k.cdc.MustUnmarshalBinary(bondBytes, &delegation) + delegation := types.UnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value()) delegations[i] = delegation iterator.Next() } @@ -68,7 +65,7 @@ func (k Keeper) GetDelegations(ctx sdk.Context, delegator sdk.Address, // set the delegation func (k Keeper) SetDelegation(ctx sdk.Context, delegation types.Delegation) { store := ctx.KVStore(k.storeKey) - b := k.cdc.MustMarshalBinary(delegation) + b := types.MarshalDelegation(k.cdc, delegation) store.Set(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr), b) } diff --git a/x/stake/keeper/sdk_types.go b/x/stake/keeper/sdk_types.go index bedbc15591..9e45982b46 100644 --- a/x/stake/keeper/sdk_types.go +++ b/x/stake/keeper/sdk_types.go @@ -91,9 +91,7 @@ func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.Address, fn func iterator := sdk.KVStorePrefixIterator(store, key) i := int64(0) for ; iterator.Valid(); iterator.Next() { - bz := iterator.Value() - var delegation types.Delegation - k.cdc.MustUnmarshalBinary(bz, &delegation) + delegation := types.UnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value()) stop := fn(i, delegation) // XXX is this safe will the fields be able to get written to? if stop { break diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index 705dcb6cd9..b6d35fb560 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -37,13 +37,12 @@ func UnmarshalDelegation(cdc *wire.Codec, key, value []byte) Delegation { var storeValue delegationValue cdc.MustUnmarshalBinary(value, &storeValue) - addrs := IndexKey[1:] // remove prefix bytes - split := len(addrs) / 2 - if (len(addrs) % 2) != 0 { + addrs := key[1:] // remove prefix bytes + if len(addrs) != 40 { panic("key length not even") } - delAddr := sdk.Address{addrs[:split]} - valAddr := sdk.Address{addrs[split:]} + delAddr := sdk.Address(addrs[:20]) + valAddr := sdk.Address(addrs[20:]) return Delegation{ DelegatorAddr: delAddr,