bug somewhere here

This commit is contained in:
rigelrozanski 2018-07-02 18:16:47 -04:00
parent f1937bc0e7
commit ab4661f88b
7 changed files with 155 additions and 84 deletions

View File

@ -122,7 +122,7 @@ func GetCmdQueryDelegation(storeName string, cdc *wire.Codec) *cobra.Command {
return err return err
} }
key := stake.GetDelegationKey(delAddr, valAddr, cdc) key := stake.GetDelegationKey(delAddr, valAddr)
ctx := context.NewCoreContextFromViper() ctx := context.NewCoreContextFromViper()
res, err := ctx.QueryStore(key, storeName) res, err := ctx.QueryStore(key, storeName)
if err != nil { if err != nil {
@ -169,7 +169,7 @@ func GetCmdQueryDelegations(storeName string, cdc *wire.Codec) *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
key := stake.GetDelegationsKey(delegatorAddr, cdc) key := stake.GetDelegationsKey(delegatorAddr)
ctx := context.NewCoreContextFromViper() ctx := context.NewCoreContextFromViper()
resKVs, err := ctx.QuerySubspace(cdc, key, storeName) resKVs, err := ctx.QuerySubspace(cdc, key, storeName)
if err != nil { if err != nil {
@ -214,7 +214,7 @@ func GetCmdQueryUnbondingDelegation(storeName string, cdc *wire.Codec) *cobra.Co
return err return err
} }
key := stake.GetUBDKey(delAddr, valAddr, cdc) key := stake.GetUBDKey(delAddr, valAddr)
ctx := context.NewCoreContextFromViper() ctx := context.NewCoreContextFromViper()
res, err := ctx.QueryStore(key, storeName) res, err := ctx.QueryStore(key, storeName)
if err != nil { if err != nil {
@ -261,7 +261,7 @@ func GetCmdQueryUnbondingDelegations(storeName string, cdc *wire.Codec) *cobra.C
if err != nil { if err != nil {
return err return err
} }
key := stake.GetUBDsKey(delegatorAddr, cdc) key := stake.GetUBDsKey(delegatorAddr)
ctx := context.NewCoreContextFromViper() ctx := context.NewCoreContextFromViper()
resKVs, err := ctx.QuerySubspace(cdc, key, storeName) resKVs, err := ctx.QuerySubspace(cdc, key, storeName)
if err != nil { if err != nil {
@ -309,7 +309,7 @@ func GetCmdQueryRedelegation(storeName string, cdc *wire.Codec) *cobra.Command {
return err return err
} }
key := stake.GetREDKey(delAddr, valSrcAddr, valDstAddr, cdc) key := stake.GetREDKey(delAddr, valSrcAddr, valDstAddr)
ctx := context.NewCoreContextFromViper() ctx := context.NewCoreContextFromViper()
res, err := ctx.QueryStore(key, storeName) res, err := ctx.QueryStore(key, storeName)
if err != nil { if err != nil {
@ -356,7 +356,7 @@ func GetCmdQueryRedelegations(storeName string, cdc *wire.Codec) *cobra.Command
if err != nil { if err != nil {
return err return err
} }
key := stake.GetREDsKey(delegatorAddr, cdc) key := stake.GetREDsKey(delegatorAddr)
ctx := context.NewCoreContextFromViper() ctx := context.NewCoreContextFromViper()
resKVs, err := ctx.QuerySubspace(cdc, key, storeName) resKVs, err := ctx.QuerySubspace(cdc, key, storeName)
if err != nil { if err != nil {

View File

@ -238,7 +238,7 @@ func getShares(storeName string, cdc *wire.Codec, sharesAmountStr, sharesPercent
} }
// make a query to get the existing delegation shares // make a query to get the existing delegation shares
key := stake.GetDelegationKey(delegatorAddr, validatorAddr, cdc) key := stake.GetDelegationKey(delegatorAddr, validatorAddr)
ctx := context.NewCoreContextFromViper() ctx := context.NewCoreContextFromViper()
resQuery, err := ctx.QueryStore(key, storeName) resQuery, err := ctx.QueryStore(key, storeName)
if err != nil { if err != nil {

View File

@ -60,7 +60,7 @@ func delegationHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerF
return return
} }
key := stake.GetDelegationKey(delegatorAddr, validatorAddr, cdc) key := stake.GetDelegationKey(delegatorAddr, validatorAddr)
res, err := ctx.QueryStore(key, storeName) res, err := ctx.QueryStore(key, storeName)
if err != nil { if err != nil {
@ -117,7 +117,7 @@ func ubdHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc {
return return
} }
key := stake.GetUBDKey(delegatorAddr, validatorAddr, cdc) key := stake.GetUBDKey(delegatorAddr, validatorAddr)
res, err := ctx.QueryStore(key, storeName) res, err := ctx.QueryStore(key, storeName)
if err != nil { if err != nil {
@ -182,7 +182,7 @@ func redHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc {
return return
} }
key := stake.GetREDKey(delegatorAddr, validatorSrcAddr, validatorDstAddr, cdc) key := stake.GetREDKey(delegatorAddr, validatorSrcAddr, validatorDstAddr)
res, err := ctx.QueryStore(key, storeName) res, err := ctx.QueryStore(key, storeName)
if err != nil { if err != nil {

View File

@ -2,6 +2,7 @@ package keeper
import ( import (
"bytes" "bytes"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/stake/types" "github.com/cosmos/cosmos-sdk/x/stake/types"
@ -12,7 +13,7 @@ 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, k.cdc)) delegatorBytes := store.Get(GetDelegationKey(delegatorAddr, validatorAddr))
if delegatorBytes == nil { if delegatorBytes == nil {
return delegation, false return delegation, false
} }
@ -46,7 +47,7 @@ func (k Keeper) GetDelegations(ctx sdk.Context, delegator sdk.Address,
maxRetrieve int16) (delegations []types.Delegation) { maxRetrieve int16) (delegations []types.Delegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := GetDelegationsKey(delegator, k.cdc) delegatorPrefixKey := GetDelegationsKey(delegator)
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest
delegations = make([]types.Delegation, maxRetrieve) delegations = make([]types.Delegation, maxRetrieve)
@ -69,13 +70,13 @@ func (k Keeper) GetDelegations(ctx sdk.Context, delegator sdk.Address,
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 := k.cdc.MustMarshalBinary(delegation)
store.Set(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr, k.cdc), b) store.Set(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr), b)
} }
// remove the delegation // remove the delegation
func (k Keeper) RemoveDelegation(ctx sdk.Context, delegation types.Delegation) { func (k Keeper) RemoveDelegation(ctx sdk.Context, delegation types.Delegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
store.Delete(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr, k.cdc)) store.Delete(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr))
} }
//_____________________________________________________________________________________ //_____________________________________________________________________________________
@ -85,7 +86,7 @@ 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, k.cdc) ubdKey := GetUBDKey(DelegatorAddr, ValidatorAddr)
bz := store.Get(ubdKey) bz := store.Get(ubdKey)
if bz == nil { if bz == nil {
return ubd, false return ubd, false
@ -98,13 +99,13 @@ func (k Keeper) GetUnbondingDelegation(ctx sdk.Context,
// 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) (unbondingDelegations []types.UnbondingDelegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, GetUBDsByValIndexKey(valAddr, k.cdc)) iterator := sdk.KVStorePrefixIterator(store, GetUBDsByValIndexKey(valAddr))
i := 0 i := 0
for ; ; i++ { for ; ; i++ {
if !iterator.Valid() { if !iterator.Valid() {
break break
} }
unbondingKey := iterator.Value() unbondingKey := GetUBDKeyFromValIndexKey(iterator.Key())
unbondingBytes := store.Get(unbondingKey) unbondingBytes := store.Get(unbondingKey)
var unbondingDelegation types.UnbondingDelegation var unbondingDelegation types.UnbondingDelegation
k.cdc.MustUnmarshalBinary(unbondingBytes, &unbondingDelegation) k.cdc.MustUnmarshalBinary(unbondingBytes, &unbondingDelegation)
@ -119,17 +120,17 @@ func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sd
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 := k.cdc.MustMarshalBinary(ubd)
ubdKey := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr, k.cdc) ubdKey := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr)
store.Set(ubdKey, bz) store.Set(ubdKey, bz)
store.Set(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr, k.cdc), ubdKey) store.Set(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr), []byte{})
} }
// 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, k.cdc) ubdKey := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr)
store.Delete(ubdKey) store.Delete(ubdKey)
store.Delete(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr, k.cdc)) store.Delete(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr))
} }
//_____________________________________________________________________________________ //_____________________________________________________________________________________
@ -139,7 +140,7 @@ 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, k.cdc) redKey := GetREDKey(DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr)
bz := store.Get(redKey) bz := store.Get(redKey)
if bz == nil { if bz == nil {
return red, false return red, false
@ -152,13 +153,23 @@ func (k Keeper) GetRedelegation(ctx sdk.Context,
// 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) (redelegations []types.Redelegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, GetREDsFromValSrcIndexKey(valAddr, k.cdc)) iterator := sdk.KVStorePrefixIterator(store, GetREDsFromValSrcIndexKey(valAddr))
i := 0 i := 0
for ; ; i++ { for ; ; i++ {
fmt.Printf("debug i: %v\n", i)
if !iterator.Valid() { if !iterator.Valid() {
break break
} }
redelegationKey := iterator.Value() redelegationKeyOrig := iterator.Value()
redelegationBytes2 := store.Get(redelegationKeyOrig)
var redelegation2 types.Redelegation
k.cdc.MustUnmarshalBinary(redelegationBytes2, &redelegation2)
fmt.Printf("debug orig redelegation2: %v\n", redelegation2)
redelegationKey := GetREDKeyFromValSrcIndexKey(iterator.Key())
fmt.Printf("debug orig redelegationKey: %v\n", redelegationKeyOrig)
fmt.Printf("debug redelegationKey: %v\n", redelegationKey)
fmt.Printf("debug iteratorKey: %v\n", iterator.Key())
redelegationBytes := store.Get(redelegationKey) redelegationBytes := store.Get(redelegationKey)
var redelegation types.Redelegation var redelegation types.Redelegation
k.cdc.MustUnmarshalBinary(redelegationBytes, &redelegation) k.cdc.MustUnmarshalBinary(redelegationBytes, &redelegation)
@ -174,7 +185,7 @@ func (k Keeper) HasReceivingRedelegation(ctx sdk.Context,
DelegatorAddr, ValidatorDstAddr sdk.Address) bool { DelegatorAddr, ValidatorDstAddr sdk.Address) bool {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
prefix := GetREDsByDelToValDstIndexKey(DelegatorAddr, ValidatorDstAddr, k.cdc) prefix := GetREDsByDelToValDstIndexKey(DelegatorAddr, ValidatorDstAddr)
iterator := sdk.KVStorePrefixIterator(store, prefix) //smallest to largest iterator := sdk.KVStorePrefixIterator(store, prefix) //smallest to largest
found := false found := false
@ -190,19 +201,20 @@ func (k Keeper) HasReceivingRedelegation(ctx sdk.Context,
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 := k.cdc.MustMarshalBinary(red)
redKey := GetREDKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr, k.cdc) redKey := GetREDKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr)
fmt.Printf("debug redKey: %v\n", redKey)
store.Set(redKey, bz) store.Set(redKey, bz)
store.Set(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr, k.cdc), redKey) store.Set(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), redKey) //[]byte{})
store.Set(GetREDByValDstIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr, k.cdc), redKey) store.Set(GetREDByValDstIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), redKey) // []byte{})
} }
// remove a redelegation object and associated index // remove a redelegation object and associated index
func (k Keeper) RemoveRedelegation(ctx sdk.Context, red types.Redelegation) { func (k Keeper) RemoveRedelegation(ctx sdk.Context, red types.Redelegation) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
redKey := GetREDKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr, k.cdc) redKey := GetREDKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr)
store.Delete(redKey) store.Delete(redKey)
store.Delete(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr, k.cdc)) store.Delete(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr))
store.Delete(GetREDByValDstIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr, k.cdc)) store.Delete(GetREDByValDstIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr))
} }
//_____________________________________________________________________________________ //_____________________________________________________________________________________

View File

@ -1,6 +1,7 @@
package keeper package keeper
import ( import (
"fmt"
"testing" "testing"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -201,19 +202,38 @@ func TestRedelegation(t *testing.T) {
keeper.SetRedelegation(ctx, rd) keeper.SetRedelegation(ctx, rd)
resBond, found := keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) resBond, found := keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1])
require.True(t, found) require.True(t, found)
require.True(t, rd.Equal(resBond))
fmt.Println("zoo0")
redelegations := keeper.GetRedelegationsFromValidator(ctx, addrVals[0])
require.Equal(t, 1, len(redelegations))
require.True(t, redelegations[0].Equal(resBond))
fmt.Println("zoo1")
redelegations = keeper.GetRedelegationsFromValidator(ctx, addrVals[0])
require.Equal(t, 1, len(redelegations))
require.True(t, redelegations[0].Equal(resBond))
fmt.Println("zoo2")
// check if has the redelegation // check if has the redelegation
has = keeper.HasReceivingRedelegation(ctx, addrDels[0], addrVals[1]) has = keeper.HasReceivingRedelegation(ctx, addrDels[0], addrVals[1])
require.True(t, has) require.True(t, has)
fmt.Println("hoolahoop")
// modify a records, save, and retrieve // modify a records, save, and retrieve
rd.SharesSrc = sdk.NewRat(21) rd.SharesSrc = sdk.NewRat(21)
rd.SharesDst = sdk.NewRat(21) rd.SharesDst = sdk.NewRat(21)
keeper.SetRedelegation(ctx, rd) keeper.SetRedelegation(ctx, rd)
resBond, found = keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) resBond, found = keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1])
require.True(t, found) require.True(t, found)
require.True(t, rd.Equal(resBond)) require.True(t, rd.Equal(resBond))
fmt.Println("hippo2")
redelegations = keeper.GetRedelegationsFromValidator(ctx, addrVals[0])
require.Equal(t, 1, len(redelegations))
require.True(t, redelegations[0].Equal(resBond))
fmt.Println("zzzzzzzzzzzzzzzzzzebra")
// delete a record // delete a record
keeper.RemoveRedelegation(ctx, rd) keeper.RemoveRedelegation(ctx, rd)

View File

@ -6,7 +6,6 @@ import (
"github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto"
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/x/stake/types" "github.com/cosmos/cosmos-sdk/x/stake/types"
) )
@ -29,34 +28,39 @@ var (
UnbondingDelegationKey = []byte{0x0B} // key for an unbonding-delegation UnbondingDelegationKey = []byte{0x0B} // key for an unbonding-delegation
UnbondingDelegationByValIndexKey = []byte{0x0C} // prefix for each key for an unbonding-delegation, by validator owner UnbondingDelegationByValIndexKey = []byte{0x0C} // prefix for each key for an unbonding-delegation, by validator owner
RedelegationKey = []byte{0x0D} // key for a redelegation RedelegationKey = []byte{0x0D} // key for a redelegation
RedelegationByValSrcIndexKey = []byte{0x0E} // prefix for each key for an redelegation, by validator owner RedelegationByValSrcIndexKey = []byte{0x0E} // prefix for each key for an redelegation, by source validator owner
RedelegationByValDstIndexKey = []byte{0x0F} // prefix for each key for an redelegation, by validator owner RedelegationByValDstIndexKey = []byte{0x0F} // prefix for each key for an redelegation, by destination validator owner
) )
const maxDigitsForAccount = 12 // ~220,000,000 atoms created at launch const maxDigitsForAccount = 12 // ~220,000,000 atoms created at launch
// get the key for the validator with address. // get the key for the validator with address.
// the value at this key is of type stake/types.Validator // VALUE: stake/types.Validator
func GetValidatorKey(ownerAddr sdk.Address) []byte { func GetValidatorKey(ownerAddr sdk.Address) []byte {
return append(ValidatorsKey, ownerAddr.Bytes()...) return append(ValidatorsKey, ownerAddr.Bytes()...)
} }
// get the key for the validator with pubkey. // get the key for the validator with pubkey.
// the value at this key should the address for a stake/types.Validator // VALUE: validator owner address ([]byte)
func GetValidatorByPubKeyIndexKey(pubkey crypto.PubKey) []byte { func GetValidatorByPubKeyIndexKey(pubkey crypto.PubKey) []byte {
return append(ValidatorsByPubKeyIndexKey, pubkey.Bytes()...) return append(ValidatorsByPubKeyIndexKey, pubkey.Bytes()...)
} }
// get the key for the current validator group, ordered like tendermint. // get the key for the current validator group
// the value at this key is the address of the owner of a validator // VALUE: none (key rearrangement with GetValKeyFromValBondedIndexKey)
func GetValidatorsBondedIndexKey(ownerAddr sdk.Address) []byte { func GetValidatorsBondedIndexKey(ownerAddr sdk.Address) []byte {
return append(ValidatorsBondedIndexKey, ownerAddr.Bytes()...) return append(ValidatorsBondedIndexKey, ownerAddr.Bytes()...)
} }
// rearrange the ValBondedIndexKey to get the ValidatorKey
func GetValKeyFromValBondedIndexKey(IndexKey []byte) []byte {
addr := IndexKey[1:] // remove prefix bytes
return GetValidatorKey(addr)
}
// get the validator by power index. power index is the key used in the power-store, // get the validator by power index. power index is the key used in the power-store,
// and represents the relative power ranking of the validator. // and represents the relative power ranking of the validator.
// the value at this key is of type address, the address being the Address // VALUE: validator owner address ([]byte)
// of the corresponding validator.
func GetValidatorsByPowerIndexKey(validator types.Validator, pool types.Pool) []byte { func GetValidatorsByPowerIndexKey(validator types.Validator, pool types.Pool) []byte {
// NOTE the address doesn't need to be stored because counter bytes must always be different // NOTE the address doesn't need to be stored because counter bytes must always be different
return getValidatorPowerRank(validator, pool) return getValidatorPowerRank(validator, pool)
@ -68,7 +72,6 @@ func getValidatorPowerRank(validator types.Validator, pool types.Pool) []byte {
power := validator.EquivalentBondedShares(pool) power := validator.EquivalentBondedShares(pool)
powerBytes := []byte(power.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first) powerBytes := []byte(power.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first)
// TODO ensure that the key will be a readable string.. probably should add seperators and have
revokedBytes := make([]byte, 1) revokedBytes := make([]byte, 1)
if validator.Revoked { if validator.Revoked {
revokedBytes[0] = byte(0x01) revokedBytes[0] = byte(0x01)
@ -76,7 +79,6 @@ func getValidatorPowerRank(validator types.Validator, pool types.Pool) []byte {
revokedBytes[0] = byte(0x00) revokedBytes[0] = byte(0x00)
} }
// TODO ensure that the key will be a readable string.. probably should add separators and have
// heightBytes and counterBytes represent strings like powerBytes does // heightBytes and counterBytes represent strings like powerBytes does
heightBytes := make([]byte, binary.MaxVarintLen64) heightBytes := make([]byte, binary.MaxVarintLen64)
binary.BigEndian.PutUint64(heightBytes, ^uint64(validator.BondHeight)) // invert height (older validators first) binary.BigEndian.PutUint64(heightBytes, ^uint64(validator.BondHeight)) // invert height (older validators first)
@ -90,62 +92,77 @@ func getValidatorPowerRank(validator types.Validator, pool types.Pool) []byte {
} }
// get the key for the accumulated update validators. // get the key for the accumulated update validators.
// The value at this key is of type stake/types.Validator // VALUE: none (key rearrangement used)
func GetTendermintUpdatesKey(ownerAddr sdk.Address) []byte { func GetTendermintUpdatesKey(ownerAddr sdk.Address) []byte {
return append(TendermintUpdatesKey, ownerAddr.Bytes()...) return append(TendermintUpdatesKey, ownerAddr.Bytes()...)
} }
// rearrange the ValBondedIndexKey to get the ValidatorKey
func GetValKeyFromTUIndexKey(IndexKey []byte) []byte {
addr := IndexKey[1:] // remove prefix bytes
return GetValidatorKey(addr)
}
//________________________________________________________________________________ //________________________________________________________________________________
// get the key for delegator bond with validator. // get the key for delegator bond with validator.
// The value at this key is of type stake/types.Delegation // VALUE: stake/types.Delegation
func GetDelegationKey(delegatorAddr, validatorAddr sdk.Address, cdc *wire.Codec) []byte { func GetDelegationKey(delegatorAddr, validatorAddr sdk.Address) []byte {
return append(GetDelegationsKey(delegatorAddr, cdc), validatorAddr.Bytes()...) return append(GetDelegationsKey(delegatorAddr), validatorAddr.Bytes()...)
} }
// get the prefix for a delegator for all validators // get the prefix for a delegator for all validators
func GetDelegationsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte { func GetDelegationsKey(delegatorAddr sdk.Address) []byte {
res := cdc.MustMarshalBinary(&delegatorAddr) return append(DelegationKey, delegatorAddr.Bytes()...)
return append(DelegationKey, res...)
} }
//________________________________________________________________________________ //________________________________________________________________________________
// get the key for an unbonding delegation by delegator and validator addr. // get the key for an unbonding delegation by delegator and validator addr.
// The value at this key is of type stake/types.UnbondingDelegation // VALUE: stake/types.UnbondingDelegation
func GetUBDKey(delegatorAddr, validatorAddr sdk.Address, cdc *wire.Codec) []byte { func GetUBDKey(delegatorAddr, validatorAddr sdk.Address) []byte {
return append(GetUBDsKey(delegatorAddr, cdc), validatorAddr.Bytes()...) return append(GetUBDsKey(delegatorAddr), validatorAddr.Bytes()...)
} }
// get the index-key for an unbonding delegation, stored by validator-index // get the index-key for an unbonding delegation, stored by validator-index
// The value at this key is a key for the corresponding unbonding delegation. // VALUE: none (key rearrangement used)
func GetUBDByValIndexKey(delegatorAddr, validatorAddr sdk.Address, cdc *wire.Codec) []byte { func GetUBDByValIndexKey(delegatorAddr, validatorAddr sdk.Address) []byte {
return append(GetUBDsByValIndexKey(validatorAddr, cdc), delegatorAddr.Bytes()...) return append(GetUBDsByValIndexKey(validatorAddr), delegatorAddr.Bytes()...)
}
// rearrange the ValIndexKey to get the UBDKey
func GetUBDKeyFromValIndexKey(IndexKey []byte) []byte {
addrs := IndexKey[1:] // remove prefix bytes
split := len(addrs) / 2
if (len(addrs) % 2) != 0 {
panic("key length not even")
}
valAddr := addrs[:split]
delAddr := addrs[split:]
return GetUBDKey(delAddr, valAddr)
} }
//______________ //______________
// get the prefix for all unbonding delegations from a delegator // get the prefix for all unbonding delegations from a delegator
func GetUBDsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte { func GetUBDsKey(delegatorAddr sdk.Address) []byte {
res := cdc.MustMarshalBinary(&delegatorAddr) return append(UnbondingDelegationKey, delegatorAddr.Bytes()...)
return append(UnbondingDelegationKey, res...)
} }
// get the prefix keyspace for the indexes of unbonding delegations for a validator // get the prefix keyspace for the indexes of unbonding delegations for a validator
func GetUBDsByValIndexKey(validatorAddr sdk.Address, cdc *wire.Codec) []byte { func GetUBDsByValIndexKey(validatorAddr sdk.Address) []byte {
res := cdc.MustMarshalBinary(&validatorAddr) return append(UnbondingDelegationByValIndexKey, validatorAddr.Bytes()...)
return append(UnbondingDelegationByValIndexKey, res...)
} }
//________________________________________________________________________________ //________________________________________________________________________________
// get the key for a redelegation // get the key for a redelegation
// The value at this key is of type stake/types.RedelegationKey // VALUE: stake/types.RedelegationKey
func GetREDKey(delegatorAddr, validatorSrcAddr, func GetREDKey(delegatorAddr, validatorSrcAddr,
validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { validatorDstAddr sdk.Address) []byte {
return append( return append(
GetREDsKey(delegatorAddr, cdc), GetREDsKey(delegatorAddr),
append( append(
validatorSrcAddr.Bytes(), validatorSrcAddr.Bytes(),
validatorDstAddr.Bytes()...)..., validatorDstAddr.Bytes()...)...,
@ -153,12 +170,12 @@ func GetREDKey(delegatorAddr, validatorSrcAddr,
} }
// get the index-key for a redelegation, stored by source-validator-index // get the index-key for a redelegation, stored by source-validator-index
// The value at this key is a key for the corresponding redelegation. // VALUE: none (key rearrangement used)
func GetREDByValSrcIndexKey(delegatorAddr, validatorSrcAddr, func GetREDByValSrcIndexKey(delegatorAddr, validatorSrcAddr,
validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { validatorDstAddr sdk.Address) []byte {
return append( return append(
GetREDsFromValSrcIndexKey(validatorSrcAddr, cdc), GetREDsFromValSrcIndexKey(validatorSrcAddr),
append( append(
delegatorAddr.Bytes(), delegatorAddr.Bytes(),
validatorDstAddr.Bytes()...)..., validatorDstAddr.Bytes()...)...,
@ -166,44 +183,66 @@ func GetREDByValSrcIndexKey(delegatorAddr, validatorSrcAddr,
} }
// get the index-key for a redelegation, stored by destination-validator-index // get the index-key for a redelegation, stored by destination-validator-index
// The value at this key is a key for the corresponding redelegation. // VALUE: none (key rearrangement used)
func GetREDByValDstIndexKey(delegatorAddr, validatorSrcAddr, func GetREDByValDstIndexKey(delegatorAddr, validatorSrcAddr,
validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { validatorDstAddr sdk.Address) []byte {
return append( return append(
GetREDsToValDstIndexKey(validatorDstAddr, cdc), GetREDsToValDstIndexKey(validatorDstAddr),
append( append(
delegatorAddr.Bytes(), delegatorAddr.Bytes(),
validatorSrcAddr.Bytes()...)..., validatorSrcAddr.Bytes()...)...,
) )
} }
// rearrange the ValSrcIndexKey to get the REDKey
func GetREDKeyFromValSrcIndexKey(IndexKey []byte) []byte {
addrs := IndexKey[1:] // remove prefix bytes
split := len(addrs) / 3
if (len(addrs) % 3) != 0 {
panic("unexpected key length")
}
valSrcAddr := addrs[:split]
delAddr := addrs[split : 2*split]
valDstAddr := addrs[2*split:]
return GetREDKey(delAddr, valSrcAddr, valDstAddr)
}
// rearrange the ValDstIndexKey to get the REDKey
func GetREDKeyFromValDstIndexKey(IndexKey []byte) []byte {
addrs := IndexKey[1:] // remove prefix bytes
split := len(addrs) / 3
if (len(addrs) % 3) != 0 {
panic("unexpected key length")
}
valDstAddr := addrs[:split]
delAddr := addrs[split : 2*split]
valSrcAddr := addrs[2*split:]
return GetREDKey(delAddr, valSrcAddr, valDstAddr)
}
//______________ //______________
// get the prefix keyspace for redelegations from a delegator // get the prefix keyspace for redelegations from a delegator
func GetREDsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte { func GetREDsKey(delegatorAddr sdk.Address) []byte {
res := cdc.MustMarshalBinary(&delegatorAddr) return append(RedelegationKey, delegatorAddr.Bytes()...)
return append(RedelegationKey, res...)
} }
// get the prefix keyspace for all redelegations redelegating away from a source validator // get the prefix keyspace for all redelegations redelegating away from a source validator
func GetREDsFromValSrcIndexKey(validatorSrcAddr sdk.Address, cdc *wire.Codec) []byte { func GetREDsFromValSrcIndexKey(validatorSrcAddr sdk.Address) []byte {
res := cdc.MustMarshalBinary(&validatorSrcAddr) return append(RedelegationByValSrcIndexKey, validatorSrcAddr.Bytes()...)
return append(RedelegationByValSrcIndexKey, res...)
} }
// get the prefix keyspace for all redelegations redelegating towards a destination validator // get the prefix keyspace for all redelegations redelegating towards a destination validator
func GetREDsToValDstIndexKey(validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { func GetREDsToValDstIndexKey(validatorDstAddr sdk.Address) []byte {
res := cdc.MustMarshalBinary(&validatorDstAddr) return append(RedelegationByValDstIndexKey, validatorDstAddr.Bytes()...)
return append(RedelegationByValDstIndexKey, res...)
} }
// get the prefix keyspace for all redelegations redelegating towards a destination validator // get the prefix keyspace for all redelegations redelegating towards a destination validator
// from a particular delegator // from a particular delegator
func GetREDsByDelToValDstIndexKey(delegatorAddr sdk.Address, func GetREDsByDelToValDstIndexKey(delegatorAddr sdk.Address,
validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { validatorDstAddr sdk.Address) []byte {
return append( return append(
GetREDsToValDstIndexKey(validatorDstAddr, cdc), GetREDsToValDstIndexKey(validatorDstAddr),
delegatorAddr.Bytes()...) delegatorAddr.Bytes()...)
} }

View File

@ -87,7 +87,7 @@ func (k Keeper) Delegation(ctx sdk.Context, addrDel sdk.Address, addrVal sdk.Add
// iterate through the active validator set and perform the provided function // iterate through the active validator set and perform the provided function
func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.Address, fn func(index int64, delegation sdk.Delegation) (stop bool)) { func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.Address, fn func(index int64, delegation sdk.Delegation) (stop bool)) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
key := GetDelegationsKey(delAddr, k.cdc) key := GetDelegationsKey(delAddr)
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() {