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 (
"bytes"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/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) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, GetUBDsByValIndexKey(valAddr))
i := 0
for ; ; i++ {
for {
if !iterator.Valid() {
break
}
@ -158,21 +156,11 @@ func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.Addre
if !iterator.Valid() {
break
}
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)
redelegationKey := GetREDKeyFromValSrcIndexKey(iterator.Key())
redelegationBytes := store.Get(redelegationKey)
var redelegation types.Redelegation
k.cdc.MustUnmarshalBinary(redelegationBytes, &redelegation)
redelegations = append(redelegations, redelegation)
iterator.Next()
}
iterator.Close()
@ -201,10 +189,9 @@ func (k Keeper) SetRedelegation(ctx sdk.Context, red types.Redelegation) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinary(red)
redKey := GetREDKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr)
fmt.Printf("debug redKey: %v\n", redKey)
store.Set(redKey, bz)
store.Set(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), redKey) //[]byte{})
store.Set(GetREDByValDstIndexKey(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), []byte{})
}
// remove a redelegation object and associated index

View File

@ -1,7 +1,6 @@
package keeper
import (
"fmt"
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -198,20 +197,14 @@ func TestGetRedelegationsFromValidator(t *testing.T) {
keeper.SetRedelegation(ctx, rd)
resBond, found := keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1])
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])
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")
}
// tests Get/Set/Remove/Has UnbondingDelegation
@ -248,7 +241,6 @@ func TestRedelegation(t *testing.T) {
// modify a records, save, and retrieve
rd.SharesSrc = sdk.NewRat(21)
rd.SharesDst = sdk.NewRat(21)
keeper.SetRedelegation(ctx, rd)
resBond, found = keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1])

View File

@ -2,7 +2,6 @@ package keeper
import (
"encoding/binary"
"fmt"
"github.com/tendermint/tendermint/crypto"
@ -54,9 +53,8 @@ func GetValidatorsBondedIndexKey(ownerAddr sdk.Address) []byte {
}
// rearrange the ValBondedIndexKey to get the ValidatorKey
func GetValKeyFromValBondedIndexKey(IndexKey []byte) []byte {
addr := IndexKey[1:] // remove prefix bytes
return GetValidatorKey(addr)
func GetAddressFromValBondedIndexKey(IndexKey []byte) []byte {
return IndexKey[1:] // remove prefix bytes
}
// 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.
// VALUE: none (key rearrangement used)
// VALUE: abci.Validator
// note records using these keys should never persist between blocks
func GetTendermintUpdatesKey(ownerAddr sdk.Address) []byte {
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.
@ -122,7 +115,13 @@ func GetDelegationsKey(delegatorAddr sdk.Address) []byte {
// get the key for an unbonding delegation by delegator and validator addr.
// VALUE: stake/types.UnbondingDelegation
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
@ -162,12 +161,12 @@ func GetUBDsByValIndexKey(validatorAddr sdk.Address) []byte {
func GetREDKey(delegatorAddr, validatorSrcAddr,
validatorDstAddr sdk.Address) []byte {
fmt.Println("KEY", delegatorAddr.Bytes())
key := make([]byte, len(delegatorAddr.Bytes()))
copy(key, delegatorAddr.Bytes())
return append(append(
GetREDsKey(key), validatorSrcAddr.Bytes()...),
GetREDsKey(key),
validatorSrcAddr.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)
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
address := iterator.Value()
address := GetAddressFromValBondedIndexKey(iterator.Key())
validator, found := k.GetValidator(ctx, address)
if !found {
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
func (k Keeper) SetValidatorBondedIndex(ctx sdk.Context, validator types.Validator) {
store := ctx.KVStore(k.storeKey)
store.Set(GetValidatorsBondedIndexKey(validator.Owner), validator.Owner)
store.Set(GetValidatorsBondedIndexKey(validator.Owner), []byte{})
}
// used in testing
@ -124,7 +124,7 @@ func (k Keeper) GetValidatorsBonded(ctx sdk.Context) (validators []types.Validat
if i > int(maxValidators-1) {
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)
if !found {
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)
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedIndexKey)
for ; iterator.Valid(); iterator.Next() {
ownerAddr := iterator.Value()
ownerAddr := GetAddressFromValBondedIndexKey(iterator.Key())
toKickOut[string(ownerAddr)] = 0 // set anything
}
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
bzVal := k.cdc.MustMarshalBinary(validator)
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
bzABCI := k.cdc.MustMarshalBinary(validator.ABCIValidator())

View File

@ -13,14 +13,16 @@ import (
type Delegation struct {
DelegatorAddr sdk.Address `json:"delegator_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
type DelegationValue struct {
Shares sdk.Rat `json:"shares"`
Height int64 `json:"height"` // Last height bond updated
}
//type DelegationValue struct {
//Shares sdk.Rat `json:"shares"`
//Height int64 `json:"height"` // Last height bond updated
//}
// two are equal
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
type UnbondingDelegation struct {
DelegatorAddr sdk.Address `json:"delegator_addr"` // delegator
ValidatorAddr sdk.Address `json:"validator_addr"` // validator unbonding from owner addr
UBDValue
DelegatorAddr sdk.Address `json:"delegator_addr"` // delegator
ValidatorAddr sdk.Address `json:"validator_addr"` // validator unbonding from owner addr
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
type UBDValue struct {
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
}
//type UBDValue struct {
//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
//}
// nolint
func (d UnbondingDelegation) Equal(d2 UnbondingDelegation) bool {
@ -110,18 +116,24 @@ type Redelegation struct {
DelegatorAddr sdk.Address `json:"delegator_addr"` // delegator
ValidatorSrcAddr sdk.Address `json:"validator_src_addr"` // validator redelegation source 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
type REDValue struct {
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
}
//type REDValue struct {
//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
//}
// nolint
func (d Redelegation) Equal(d2 Redelegation) bool {