delegations new key format ported
This commit is contained in:
parent
ab964da105
commit
199c1e81eb
|
@ -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
|
||||||
|
@ -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.UnmarshalDelegation(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
|
||||||
|
@ -179,8 +179,7 @@ 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.UnmarshalDelegation(cdc, KV.Key, KV.Value)
|
||||||
cdc.MustUnmarshalBinary(KV.Value, &delegation)
|
|
||||||
delegations = append(delegations, delegation)
|
delegations = append(delegations, delegation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,13 +77,7 @@ func delegationHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerF
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var delegation stake.Delegation
|
delegation := types.UnmarshalDelegation(cdc, key, res)
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
output, err := cdc.MarshalJSON(delegation)
|
output, err := cdc.MarshalJSON(delegation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -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.UnmarshalDelegation(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.UnmarshalDelegation(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.UnmarshalDelegation(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.MarshalDelegation(k.cdc, delegation)
|
||||||
store.Set(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr), b)
|
store.Set(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr), b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -91,9 +91,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.UnmarshalDelegation(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
|
||||||
|
|
|
@ -37,13 +37,12 @@ func UnmarshalDelegation(cdc *wire.Codec, key, value []byte) Delegation {
|
||||||
var storeValue delegationValue
|
var storeValue delegationValue
|
||||||
cdc.MustUnmarshalBinary(value, &storeValue)
|
cdc.MustUnmarshalBinary(value, &storeValue)
|
||||||
|
|
||||||
addrs := IndexKey[1:] // remove prefix bytes
|
addrs := key[1:] // remove prefix bytes
|
||||||
split := len(addrs) / 2
|
if len(addrs) != 40 {
|
||||||
if (len(addrs) % 2) != 0 {
|
|
||||||
panic("key length not even")
|
panic("key length not even")
|
||||||
}
|
}
|
||||||
delAddr := sdk.Address{addrs[:split]}
|
delAddr := sdk.Address(addrs[:20])
|
||||||
valAddr := sdk.Address{addrs[split:]}
|
valAddr := sdk.Address(addrs[20:])
|
||||||
|
|
||||||
return Delegation{
|
return Delegation{
|
||||||
DelegatorAddr: delAddr,
|
DelegatorAddr: delAddr,
|
||||||
|
|
Loading…
Reference in New Issue