keys cleanup

This commit is contained in:
rigelrozanski 2018-07-02 20:58:22 -04:00
parent 96f2d2983b
commit 6f3b4c1805
6 changed files with 57 additions and 67 deletions

View File

@ -2,7 +2,6 @@ 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"
@ -100,8 +99,7 @@ func (k Keeper) GetUnbondingDelegation(ctx sdk.Context,
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)) iterator := sdk.KVStorePrefixIterator(store, GetUBDsByValIndexKey(valAddr))
i := 0 for {
for ; ; i++ {
if !iterator.Valid() { if !iterator.Valid() {
break break
} }
@ -158,21 +156,11 @@ func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.Addre
if !iterator.Valid() { if !iterator.Valid() {
break break
} }
redelegationKey := GetREDKeyFromValSrcIndexKey(iterator.Key())
fmt.Println("called")
//redelegationKey := iterator.Value()
iKey := iterator.Key()
fmt.Println("LEN IT", len(iKey))
redelegationKey := GetREDKeyFromValSrcIndexKey(iKey)
fmt.Printf("debug iteratorValue: %v\n", iterator.Value())
fmt.Printf("debug iteratorKey: %v\n", iKey)
fmt.Printf("debug redelegationKey: %v\n", redelegationKey)
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)
redelegations = append(redelegations, redelegation) redelegations = append(redelegations, redelegation)
iterator.Next() iterator.Next()
} }
iterator.Close() iterator.Close()
@ -201,10 +189,9 @@ 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) 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), redKey) //[]byte{}) store.Set(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), []byte{})
store.Set(GetREDByValDstIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), redKey) // []byte{}) store.Set(GetREDByValDstIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), []byte{})
} }
// remove a redelegation object and associated index // remove a redelegation object and associated index

View File

@ -1,7 +1,6 @@
package keeper package keeper
import ( import (
"fmt"
"testing" "testing"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -198,20 +197,14 @@ func TestGetRedelegationsFromValidator(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)
fmt.Printf("debug addrDels[0]: %v\n", addrDels[0])
fmt.Printf("debug addrVals[0]: %v\n", addrVals[0])
fmt.Printf("debug addrVals[1]: %v\n", addrVals[1])
fmt.Println("zoo0")
redelegations := keeper.GetRedelegationsFromValidator(ctx, addrVals[0]) redelegations := keeper.GetRedelegationsFromValidator(ctx, addrVals[0])
require.Equal(t, 1, len(redelegations)) require.Equal(t, 1, len(redelegations))
require.True(t, redelegations[0].Equal(resBond)) require.True(t, redelegations[0].Equal(resBond))
fmt.Println("zoo1")
redelegations = keeper.GetRedelegationsFromValidator(ctx, addrVals[0]) redelegations = keeper.GetRedelegationsFromValidator(ctx, addrVals[0])
require.Equal(t, 1, len(redelegations)) require.Equal(t, 1, len(redelegations))
require.True(t, redelegations[0].Equal(resBond)) require.True(t, redelegations[0].Equal(resBond))
fmt.Println("zoo2")
} }
// tests Get/Set/Remove/Has UnbondingDelegation // tests Get/Set/Remove/Has UnbondingDelegation
@ -248,7 +241,6 @@ func TestRedelegation(t *testing.T) {
// 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])

View File

@ -2,7 +2,6 @@ package keeper
import ( import (
"encoding/binary" "encoding/binary"
"fmt"
"github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto"
@ -54,9 +53,8 @@ func GetValidatorsBondedIndexKey(ownerAddr sdk.Address) []byte {
} }
// rearrange the ValBondedIndexKey to get the ValidatorKey // rearrange the ValBondedIndexKey to get the ValidatorKey
func GetValKeyFromValBondedIndexKey(IndexKey []byte) []byte { func GetAddressFromValBondedIndexKey(IndexKey []byte) []byte {
addr := IndexKey[1:] // remove prefix bytes return 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,
@ -93,17 +91,12 @@ 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.
// VALUE: none (key rearrangement used) // VALUE: abci.Validator
// note records using these keys should never persist between blocks
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.
@ -122,7 +115,13 @@ func GetDelegationsKey(delegatorAddr sdk.Address) []byte {
// get the key for an unbonding delegation by delegator and validator addr. // get the key for an unbonding delegation by delegator and validator addr.
// VALUE: stake/types.UnbondingDelegation // VALUE: stake/types.UnbondingDelegation
func GetUBDKey(delegatorAddr, validatorAddr sdk.Address) []byte { func GetUBDKey(delegatorAddr, validatorAddr sdk.Address) []byte {
return append(GetUBDsKey(delegatorAddr), validatorAddr.Bytes()...)
key := make([]byte, len(delegatorAddr.Bytes()))
copy(key, delegatorAddr.Bytes())
return append(
GetUBDsKey(key),
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
@ -162,12 +161,12 @@ func GetUBDsByValIndexKey(validatorAddr sdk.Address) []byte {
func GetREDKey(delegatorAddr, validatorSrcAddr, func GetREDKey(delegatorAddr, validatorSrcAddr,
validatorDstAddr sdk.Address) []byte { validatorDstAddr sdk.Address) []byte {
fmt.Println("KEY", delegatorAddr.Bytes())
key := make([]byte, len(delegatorAddr.Bytes())) key := make([]byte, len(delegatorAddr.Bytes()))
copy(key, delegatorAddr.Bytes()) copy(key, delegatorAddr.Bytes())
return append(append( return append(append(
GetREDsKey(key), validatorSrcAddr.Bytes()...), GetREDsKey(key),
validatorSrcAddr.Bytes()...),
validatorDstAddr.Bytes()...) validatorDstAddr.Bytes()...)
} }

View File

@ -34,7 +34,7 @@ func (k Keeper) IterateValidatorsBonded(ctx sdk.Context, fn func(index int64, va
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedIndexKey) iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedIndexKey)
i := int64(0) i := int64(0)
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
address := iterator.Value() address := GetAddressFromValBondedIndexKey(iterator.Key())
validator, found := k.GetValidator(ctx, address) validator, found := k.GetValidator(ctx, address)
if !found { if !found {
panic(fmt.Sprintf("validator record not found for address: %v\n", address)) panic(fmt.Sprintf("validator record not found for address: %v\n", address))

View File

@ -56,7 +56,7 @@ func (k Keeper) SetValidatorByPowerIndex(ctx sdk.Context, validator types.Valida
// validator index // validator index
func (k Keeper) SetValidatorBondedIndex(ctx sdk.Context, validator types.Validator) { func (k Keeper) SetValidatorBondedIndex(ctx sdk.Context, validator types.Validator) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
store.Set(GetValidatorsBondedIndexKey(validator.Owner), validator.Owner) store.Set(GetValidatorsBondedIndexKey(validator.Owner), []byte{})
} }
// used in testing // used in testing
@ -124,7 +124,7 @@ func (k Keeper) GetValidatorsBonded(ctx sdk.Context) (validators []types.Validat
if i > int(maxValidators-1) { if i > int(maxValidators-1) {
panic("maxValidators is less than the number of records in ValidatorsBonded Store, store should have been updated") panic("maxValidators is less than the number of records in ValidatorsBonded Store, store should have been updated")
} }
address := iterator.Value() address := GetAddressFromValBondedIndexKey(iterator.Key())
validator, found := k.GetValidator(ctx, address) validator, found := k.GetValidator(ctx, address)
if !found { if !found {
panic(fmt.Sprintf("validator record not found for address: %v\n", address)) panic(fmt.Sprintf("validator record not found for address: %v\n", address))
@ -362,7 +362,7 @@ func (k Keeper) UpdateBondedValidatorsFull(ctx sdk.Context) {
toKickOut := make(map[string]byte) toKickOut := make(map[string]byte)
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedIndexKey) iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedIndexKey)
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
ownerAddr := iterator.Value() ownerAddr := GetAddressFromValBondedIndexKey(iterator.Key())
toKickOut[string(ownerAddr)] = 0 // set anything toKickOut[string(ownerAddr)] = 0 // set anything
} }
iterator.Close() iterator.Close()
@ -471,7 +471,7 @@ func (k Keeper) bondValidator(ctx sdk.Context, validator types.Validator) types.
// save the now bonded validator record to the three referenced stores // save the now bonded validator record to the three referenced stores
bzVal := k.cdc.MustMarshalBinary(validator) bzVal := k.cdc.MustMarshalBinary(validator)
store.Set(GetValidatorKey(validator.Owner), bzVal) store.Set(GetValidatorKey(validator.Owner), bzVal)
store.Set(GetValidatorsBondedIndexKey(validator.Owner), validator.Owner) store.Set(GetValidatorsBondedIndexKey(validator.Owner), []byte{})
// add to accumulated changes for tendermint // add to accumulated changes for tendermint
bzABCI := k.cdc.MustMarshalBinary(validator.ABCIValidator()) bzABCI := k.cdc.MustMarshalBinary(validator.ABCIValidator())

View File

@ -13,14 +13,16 @@ import (
type Delegation struct { type Delegation struct {
DelegatorAddr sdk.Address `json:"delegator_addr"` DelegatorAddr sdk.Address `json:"delegator_addr"`
ValidatorAddr sdk.Address `json:"validator_addr"` ValidatorAddr sdk.Address `json:"validator_addr"`
DelegationValue Shares sdk.Rat `json:"shares"`
Height int64 `json:"height"` // Last height bond updated
//DelegationValue
} }
// delegation store value // delegation store value
type DelegationValue struct { //type DelegationValue struct {
Shares sdk.Rat `json:"shares"` //Shares sdk.Rat `json:"shares"`
Height int64 `json:"height"` // Last height bond updated //Height int64 `json:"height"` // Last height bond updated
} //}
// two are equal // two are equal
func (d Delegation) Equal(d2 Delegation) bool { func (d Delegation) Equal(d2 Delegation) bool {
@ -62,18 +64,22 @@ func (d Delegation) HumanReadableString() (string, error) {
// element stored to represent the passive unbonding queue // element stored to represent the passive unbonding queue
type UnbondingDelegation struct { type UnbondingDelegation struct {
DelegatorAddr sdk.Address `json:"delegator_addr"` // delegator DelegatorAddr sdk.Address `json:"delegator_addr"` // delegator
ValidatorAddr sdk.Address `json:"validator_addr"` // validator unbonding from owner addr ValidatorAddr sdk.Address `json:"validator_addr"` // validator unbonding from owner addr
UBDValue CreationHeight int64 `json:"creation_height"` // height which the unbonding took place
MinTime int64 `json:"min_time"` // unix time for unbonding completion
InitialBalance sdk.Coin `json:"initial_balance"` // atoms initially scheduled to receive at completion
Balance sdk.Coin `json:"balance"` // atoms to receive at completion
//UBDValue
} }
// UBD store value // UBD store value
type UBDValue struct { //type UBDValue struct {
CreationHeight int64 `json:"creation_height"` // height which the unbonding took place //CreationHeight int64 `json:"creation_height"` // height which the unbonding took place
MinTime int64 `json:"min_time"` // unix time for unbonding completion //MinTime int64 `json:"min_time"` // unix time for unbonding completion
InitialBalance sdk.Coin `json:"initial_balance"` // atoms initially scheduled to receive at completion //InitialBalance sdk.Coin `json:"initial_balance"` // atoms initially scheduled to receive at completion
Balance sdk.Coin `json:"balance"` // atoms to receive at completion //Balance sdk.Coin `json:"balance"` // atoms to receive at completion
} //}
// nolint // nolint
func (d UnbondingDelegation) Equal(d2 UnbondingDelegation) bool { func (d UnbondingDelegation) Equal(d2 UnbondingDelegation) bool {
@ -110,18 +116,24 @@ type Redelegation struct {
DelegatorAddr sdk.Address `json:"delegator_addr"` // delegator DelegatorAddr sdk.Address `json:"delegator_addr"` // delegator
ValidatorSrcAddr sdk.Address `json:"validator_src_addr"` // validator redelegation source owner addr ValidatorSrcAddr sdk.Address `json:"validator_src_addr"` // validator redelegation source owner addr
ValidatorDstAddr sdk.Address `json:"validator_dst_addr"` // validator redelegation destination owner addr ValidatorDstAddr sdk.Address `json:"validator_dst_addr"` // validator redelegation destination owner addr
REDValue CreationHeight int64 `json:"creation_height"` // height which the redelegation took place
MinTime int64 `json:"min_time"` // unix time for redelegation completion
InitialBalance sdk.Coin `json:"initial_balance"` // initial balance when redelegation started
Balance sdk.Coin `json:"balance"` // current balance
SharesSrc sdk.Rat `json:"shares_src"` // amount of source shares redelegating
SharesDst sdk.Rat `json:"shares_dst"` // amount of destination shares redelegating
//REDValue
} }
// Redelegation store value // Redelegation store value
type REDValue struct { //type REDValue struct {
CreationHeight int64 `json:"creation_height"` // height which the redelegation took place //CreationHeight int64 `json:"creation_height"` // height which the redelegation took place
MinTime int64 `json:"min_time"` // unix time for redelegation completion //MinTime int64 `json:"min_time"` // unix time for redelegation completion
InitialBalance sdk.Coin `json:"initial_balance"` // initial balance when redelegation started //InitialBalance sdk.Coin `json:"initial_balance"` // initial balance when redelegation started
Balance sdk.Coin `json:"balance"` // current balance //Balance sdk.Coin `json:"balance"` // current balance
SharesSrc sdk.Rat `json:"shares_src"` // amount of source shares redelegating //SharesSrc sdk.Rat `json:"shares_src"` // amount of source shares redelegating
SharesDst sdk.Rat `json:"shares_dst"` // amount of destination shares redelegating //SharesDst sdk.Rat `json:"shares_dst"` // amount of destination shares redelegating
} //}
// nolint // nolint
func (d Redelegation) Equal(d2 Redelegation) bool { func (d Redelegation) Equal(d2 Redelegation) bool {