delegations new key format ported

This commit is contained in:
rigelrozanski 2018-07-03 23:44:54 -04:00
parent ab964da105
commit 199c1e81eb
5 changed files with 18 additions and 29 deletions

View File

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

View File

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

View File

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

View File

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

View File

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