From 94a3ce4c83afeaa5e3fcc069e889ffcf67b4f321 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Fri, 29 Jun 2018 10:45:11 -0700 Subject: [PATCH 01/15] docs: Explain the expected return type within the store (#1452) --- x/stake/keeper/key.go | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/x/stake/keeper/key.go b/x/stake/keeper/key.go index 667f5f681e..3be009e220 100644 --- a/x/stake/keeper/key.go +++ b/x/stake/keeper/key.go @@ -35,30 +35,35 @@ var ( 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 func GetValidatorKey(ownerAddr sdk.Address) []byte { 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 func GetValidatorByPubKeyIndexKey(pubkey crypto.PubKey) []byte { return append(ValidatorsByPubKeyIndexKey, pubkey.Bytes()...) } -// get the key for the current validator group, ordered like tendermint +// get the key for the current validator group, ordered like tendermint. +// the value at this key is the address of the owner of a validator func GetValidatorsBondedIndexKey(ownerAddr sdk.Address) []byte { return append(ValidatorsBondedIndexKey, ownerAddr.Bytes()...) } -// get the power which is the key for the validator 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. +// the value at this key is of type address, the address being the Address +// of the corresponding validator. 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 - return GetValidatorPowerRank(validator, pool) + return getValidatorPowerRank(validator, pool) } -// get the power of a validator -func GetValidatorPowerRank(validator types.Validator, pool types.Pool) []byte { +// get the power ranking of a validator +func getValidatorPowerRank(validator types.Validator, pool types.Pool) []byte { power := validator.EquivalentBondedShares(pool) powerBytes := []byte(power.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first) @@ -71,7 +76,7 @@ func GetValidatorPowerRank(validator types.Validator, pool types.Pool) []byte { revokedBytes[0] = byte(0x00) } - // TODO ensure that the key will be a readable string.. probably should add seperators and have + // 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 := make([]byte, binary.MaxVarintLen64) binary.BigEndian.PutUint64(heightBytes, ^uint64(validator.BondHeight)) // invert height (older validators first) @@ -84,14 +89,16 @@ func GetValidatorPowerRank(validator types.Validator, pool types.Pool) []byte { append(heightBytes, counterBytes...)...)...)...) } -// 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 func GetTendermintUpdatesKey(ownerAddr sdk.Address) []byte { return append(TendermintUpdatesKey, ownerAddr.Bytes()...) } //________________________________________________________________________________ -// 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 func GetDelegationKey(delegatorAddr, validatorAddr sdk.Address, cdc *wire.Codec) []byte { return append(GetDelegationsKey(delegatorAddr, cdc), validatorAddr.Bytes()...) } @@ -104,12 +111,14 @@ func GetDelegationsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte { //________________________________________________________________________________ -// get the key for an unbonding delegation +// get the key for an unbonding delegation by delegator and validator addr. +// The value at this key is of type stake/types.UnbondingDelegation func GetUBDKey(delegatorAddr, validatorAddr sdk.Address, cdc *wire.Codec) []byte { return append(GetUBDsKey(delegatorAddr, cdc), validatorAddr.Bytes()...) } // 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. func GetUBDByValIndexKey(delegatorAddr, validatorAddr sdk.Address, cdc *wire.Codec) []byte { return append(GetUBDsByValIndexKey(validatorAddr, cdc), delegatorAddr.Bytes()...) } @@ -122,7 +131,7 @@ func GetUBDsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte { return append(UnbondingDelegationKey, res...) } -// get the prefix keyspace for the indexs 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 { res := cdc.MustMarshalBinary(&validatorAddr) return append(UnbondingDelegationByValIndexKey, res...) @@ -131,6 +140,7 @@ func GetUBDsByValIndexKey(validatorAddr sdk.Address, cdc *wire.Codec) []byte { //________________________________________________________________________________ // get the key for a redelegation +// The value at this key is of type stake/types.RedelegationKey func GetREDKey(delegatorAddr, validatorSrcAddr, validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { @@ -143,6 +153,7 @@ func GetREDKey(delegatorAddr, validatorSrcAddr, } // get the index-key for a redelegation, stored by source-validator-index +// The value at this key is a key for the corresponding redelegation. func GetREDByValSrcIndexKey(delegatorAddr, validatorSrcAddr, validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { @@ -155,6 +166,7 @@ func GetREDByValSrcIndexKey(delegatorAddr, validatorSrcAddr, } // get the index-key for a redelegation, stored by destination-validator-index +// The value at this key is a key for the corresponding redelegation. func GetREDByValDstIndexKey(delegatorAddr, validatorSrcAddr, validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { From ab4661f88b0676f969e0cb4c9b58193158adfd68 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 2 Jul 2018 18:16:47 -0400 Subject: [PATCH 02/15] bug somewhere here --- x/stake/client/cli/query.go | 12 +-- x/stake/client/cli/tx.go | 2 +- x/stake/client/rest/query.go | 6 +- x/stake/keeper/delegation.go | 54 +++++++----- x/stake/keeper/delegation_test.go | 22 ++++- x/stake/keeper/key.go | 141 +++++++++++++++++++----------- x/stake/keeper/sdk_types.go | 2 +- 7 files changed, 155 insertions(+), 84 deletions(-) diff --git a/x/stake/client/cli/query.go b/x/stake/client/cli/query.go index c162717efd..661dce31d7 100644 --- a/x/stake/client/cli/query.go +++ b/x/stake/client/cli/query.go @@ -122,7 +122,7 @@ func GetCmdQueryDelegation(storeName string, cdc *wire.Codec) *cobra.Command { return err } - key := stake.GetDelegationKey(delAddr, valAddr, cdc) + key := stake.GetDelegationKey(delAddr, valAddr) ctx := context.NewCoreContextFromViper() res, err := ctx.QueryStore(key, storeName) if err != nil { @@ -169,7 +169,7 @@ func GetCmdQueryDelegations(storeName string, cdc *wire.Codec) *cobra.Command { if err != nil { return err } - key := stake.GetDelegationsKey(delegatorAddr, cdc) + key := stake.GetDelegationsKey(delegatorAddr) ctx := context.NewCoreContextFromViper() resKVs, err := ctx.QuerySubspace(cdc, key, storeName) if err != nil { @@ -214,7 +214,7 @@ func GetCmdQueryUnbondingDelegation(storeName string, cdc *wire.Codec) *cobra.Co return err } - key := stake.GetUBDKey(delAddr, valAddr, cdc) + key := stake.GetUBDKey(delAddr, valAddr) ctx := context.NewCoreContextFromViper() res, err := ctx.QueryStore(key, storeName) if err != nil { @@ -261,7 +261,7 @@ func GetCmdQueryUnbondingDelegations(storeName string, cdc *wire.Codec) *cobra.C if err != nil { return err } - key := stake.GetUBDsKey(delegatorAddr, cdc) + key := stake.GetUBDsKey(delegatorAddr) ctx := context.NewCoreContextFromViper() resKVs, err := ctx.QuerySubspace(cdc, key, storeName) if err != nil { @@ -309,7 +309,7 @@ func GetCmdQueryRedelegation(storeName string, cdc *wire.Codec) *cobra.Command { return err } - key := stake.GetREDKey(delAddr, valSrcAddr, valDstAddr, cdc) + key := stake.GetREDKey(delAddr, valSrcAddr, valDstAddr) ctx := context.NewCoreContextFromViper() res, err := ctx.QueryStore(key, storeName) if err != nil { @@ -356,7 +356,7 @@ func GetCmdQueryRedelegations(storeName string, cdc *wire.Codec) *cobra.Command if err != nil { return err } - key := stake.GetREDsKey(delegatorAddr, cdc) + key := stake.GetREDsKey(delegatorAddr) ctx := context.NewCoreContextFromViper() resKVs, err := ctx.QuerySubspace(cdc, key, storeName) if err != nil { diff --git a/x/stake/client/cli/tx.go b/x/stake/client/cli/tx.go index b0fa2e524a..068c19ce22 100644 --- a/x/stake/client/cli/tx.go +++ b/x/stake/client/cli/tx.go @@ -238,7 +238,7 @@ func getShares(storeName string, cdc *wire.Codec, sharesAmountStr, sharesPercent } // make a query to get the existing delegation shares - key := stake.GetDelegationKey(delegatorAddr, validatorAddr, cdc) + key := stake.GetDelegationKey(delegatorAddr, validatorAddr) ctx := context.NewCoreContextFromViper() resQuery, err := ctx.QueryStore(key, storeName) if err != nil { diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index afa9e3bf0c..12ef882e5e 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -60,7 +60,7 @@ func delegationHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerF return } - key := stake.GetDelegationKey(delegatorAddr, validatorAddr, cdc) + key := stake.GetDelegationKey(delegatorAddr, validatorAddr) res, err := ctx.QueryStore(key, storeName) if err != nil { @@ -117,7 +117,7 @@ func ubdHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc { return } - key := stake.GetUBDKey(delegatorAddr, validatorAddr, cdc) + key := stake.GetUBDKey(delegatorAddr, validatorAddr) res, err := ctx.QueryStore(key, storeName) if err != nil { @@ -182,7 +182,7 @@ func redHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc { return } - key := stake.GetREDKey(delegatorAddr, validatorSrcAddr, validatorDstAddr, cdc) + key := stake.GetREDKey(delegatorAddr, validatorSrcAddr, validatorDstAddr) res, err := ctx.QueryStore(key, storeName) if err != nil { diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index 514939e17f..8ede4b55e2 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -2,6 +2,7 @@ package keeper import ( "bytes" + "fmt" sdk "github.com/cosmos/cosmos-sdk/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) { store := ctx.KVStore(k.storeKey) - delegatorBytes := store.Get(GetDelegationKey(delegatorAddr, validatorAddr, k.cdc)) + delegatorBytes := store.Get(GetDelegationKey(delegatorAddr, validatorAddr)) if delegatorBytes == nil { return delegation, false } @@ -46,7 +47,7 @@ func (k Keeper) GetDelegations(ctx sdk.Context, delegator sdk.Address, maxRetrieve int16) (delegations []types.Delegation) { store := ctx.KVStore(k.storeKey) - delegatorPrefixKey := GetDelegationsKey(delegator, k.cdc) + delegatorPrefixKey := GetDelegationsKey(delegator) iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest 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) { store := ctx.KVStore(k.storeKey) 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 func (k Keeper) RemoveDelegation(ctx sdk.Context, delegation types.Delegation) { 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) { store := ctx.KVStore(k.storeKey) - ubdKey := GetUBDKey(DelegatorAddr, ValidatorAddr, k.cdc) + ubdKey := GetUBDKey(DelegatorAddr, ValidatorAddr) bz := store.Get(ubdKey) if bz == nil { return ubd, false @@ -98,13 +99,13 @@ func (k Keeper) GetUnbondingDelegation(ctx sdk.Context, // load all unbonding delegations from a particular validator func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sdk.Address) (unbondingDelegations []types.UnbondingDelegation) { store := ctx.KVStore(k.storeKey) - iterator := sdk.KVStorePrefixIterator(store, GetUBDsByValIndexKey(valAddr, k.cdc)) + iterator := sdk.KVStorePrefixIterator(store, GetUBDsByValIndexKey(valAddr)) i := 0 for ; ; i++ { if !iterator.Valid() { break } - unbondingKey := iterator.Value() + unbondingKey := GetUBDKeyFromValIndexKey(iterator.Key()) unbondingBytes := store.Get(unbondingKey) var unbondingDelegation types.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) { store := ctx.KVStore(k.storeKey) 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(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr, k.cdc), ubdKey) + store.Set(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr), []byte{}) } // remove the unbonding delegation object and associated index func (k Keeper) RemoveUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { store := ctx.KVStore(k.storeKey) - ubdKey := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr, k.cdc) + ubdKey := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) 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) { store := ctx.KVStore(k.storeKey) - redKey := GetREDKey(DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr, k.cdc) + redKey := GetREDKey(DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr) bz := store.Get(redKey) if bz == nil { return red, false @@ -152,13 +153,23 @@ func (k Keeper) GetRedelegation(ctx sdk.Context, // load all redelegations from a particular validator func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.Address) (redelegations []types.Redelegation) { store := ctx.KVStore(k.storeKey) - iterator := sdk.KVStorePrefixIterator(store, GetREDsFromValSrcIndexKey(valAddr, k.cdc)) + iterator := sdk.KVStorePrefixIterator(store, GetREDsFromValSrcIndexKey(valAddr)) i := 0 for ; ; i++ { + fmt.Printf("debug i: %v\n", i) if !iterator.Valid() { 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) var redelegation types.Redelegation k.cdc.MustUnmarshalBinary(redelegationBytes, &redelegation) @@ -174,7 +185,7 @@ func (k Keeper) HasReceivingRedelegation(ctx sdk.Context, DelegatorAddr, ValidatorDstAddr sdk.Address) bool { store := ctx.KVStore(k.storeKey) - prefix := GetREDsByDelToValDstIndexKey(DelegatorAddr, ValidatorDstAddr, k.cdc) + prefix := GetREDsByDelToValDstIndexKey(DelegatorAddr, ValidatorDstAddr) iterator := sdk.KVStorePrefixIterator(store, prefix) //smallest to largest found := false @@ -190,19 +201,20 @@ func (k Keeper) HasReceivingRedelegation(ctx sdk.Context, 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, k.cdc) + 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, k.cdc), redKey) - store.Set(GetREDByValDstIndexKey(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), redKey) // []byte{}) } // remove a redelegation object and associated index func (k Keeper) RemoveRedelegation(ctx sdk.Context, red types.Redelegation) { 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(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr, k.cdc)) - store.Delete(GetREDByValDstIndexKey(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)) } //_____________________________________________________________________________________ diff --git a/x/stake/keeper/delegation_test.go b/x/stake/keeper/delegation_test.go index eb318df4d3..4b5d12e095 100644 --- a/x/stake/keeper/delegation_test.go +++ b/x/stake/keeper/delegation_test.go @@ -1,6 +1,7 @@ package keeper import ( + "fmt" "testing" sdk "github.com/cosmos/cosmos-sdk/types" @@ -201,19 +202,38 @@ func TestRedelegation(t *testing.T) { keeper.SetRedelegation(ctx, rd) resBond, found := keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) 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 has = keeper.HasReceivingRedelegation(ctx, addrDels[0], addrVals[1]) require.True(t, has) + fmt.Println("hoolahoop") // 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]) require.True(t, found) 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 keeper.RemoveRedelegation(ctx, rd) diff --git a/x/stake/keeper/key.go b/x/stake/keeper/key.go index 3be009e220..c0f8c8b299 100644 --- a/x/stake/keeper/key.go +++ b/x/stake/keeper/key.go @@ -6,7 +6,6 @@ import ( "github.com/tendermint/tendermint/crypto" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/x/stake/types" ) @@ -29,34 +28,39 @@ var ( UnbondingDelegationKey = []byte{0x0B} // key for an unbonding-delegation UnbondingDelegationByValIndexKey = []byte{0x0C} // prefix for each key for an unbonding-delegation, by validator owner RedelegationKey = []byte{0x0D} // key for a redelegation - RedelegationByValSrcIndexKey = []byte{0x0E} // prefix for each key for an redelegation, by validator owner - RedelegationByValDstIndexKey = []byte{0x0F} // 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 destination validator owner ) const maxDigitsForAccount = 12 // ~220,000,000 atoms created at launch // 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 { return append(ValidatorsKey, ownerAddr.Bytes()...) } // 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 { return append(ValidatorsByPubKeyIndexKey, pubkey.Bytes()...) } -// get the key for the current validator group, ordered like tendermint. -// the value at this key is the address of the owner of a validator +// get the key for the current validator group +// VALUE: none (key rearrangement with GetValKeyFromValBondedIndexKey) func GetValidatorsBondedIndexKey(ownerAddr sdk.Address) []byte { 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, // and represents the relative power ranking of the validator. -// the value at this key is of type address, the address being the Address -// of the corresponding validator. +// VALUE: validator owner address ([]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 return getValidatorPowerRank(validator, pool) @@ -68,7 +72,6 @@ func getValidatorPowerRank(validator types.Validator, pool types.Pool) []byte { power := validator.EquivalentBondedShares(pool) 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) if validator.Revoked { revokedBytes[0] = byte(0x01) @@ -76,7 +79,6 @@ func getValidatorPowerRank(validator types.Validator, pool types.Pool) []byte { 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 := make([]byte, binary.MaxVarintLen64) 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. -// The value at this key is of type stake/types.Validator +// VALUE: none (key rearrangement used) 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. -// The value at this key is of type stake/types.Delegation -func GetDelegationKey(delegatorAddr, validatorAddr sdk.Address, cdc *wire.Codec) []byte { - return append(GetDelegationsKey(delegatorAddr, cdc), validatorAddr.Bytes()...) +// VALUE: stake/types.Delegation +func GetDelegationKey(delegatorAddr, validatorAddr sdk.Address) []byte { + return append(GetDelegationsKey(delegatorAddr), validatorAddr.Bytes()...) } // get the prefix for a delegator for all validators -func GetDelegationsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte { - res := cdc.MustMarshalBinary(&delegatorAddr) - return append(DelegationKey, res...) +func GetDelegationsKey(delegatorAddr sdk.Address) []byte { + return append(DelegationKey, delegatorAddr.Bytes()...) } //________________________________________________________________________________ // get the key for an unbonding delegation by delegator and validator addr. -// The value at this key is of type stake/types.UnbondingDelegation -func GetUBDKey(delegatorAddr, validatorAddr sdk.Address, cdc *wire.Codec) []byte { - return append(GetUBDsKey(delegatorAddr, cdc), validatorAddr.Bytes()...) +// VALUE: stake/types.UnbondingDelegation +func GetUBDKey(delegatorAddr, validatorAddr sdk.Address) []byte { + return append(GetUBDsKey(delegatorAddr), validatorAddr.Bytes()...) } // 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. -func GetUBDByValIndexKey(delegatorAddr, validatorAddr sdk.Address, cdc *wire.Codec) []byte { - return append(GetUBDsByValIndexKey(validatorAddr, cdc), delegatorAddr.Bytes()...) +// VALUE: none (key rearrangement used) +func GetUBDByValIndexKey(delegatorAddr, validatorAddr sdk.Address) []byte { + 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 -func GetUBDsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte { - res := cdc.MustMarshalBinary(&delegatorAddr) - return append(UnbondingDelegationKey, res...) +func GetUBDsKey(delegatorAddr sdk.Address) []byte { + return append(UnbondingDelegationKey, delegatorAddr.Bytes()...) } // get the prefix keyspace for the indexes of unbonding delegations for a validator -func GetUBDsByValIndexKey(validatorAddr sdk.Address, cdc *wire.Codec) []byte { - res := cdc.MustMarshalBinary(&validatorAddr) - return append(UnbondingDelegationByValIndexKey, res...) +func GetUBDsByValIndexKey(validatorAddr sdk.Address) []byte { + return append(UnbondingDelegationByValIndexKey, validatorAddr.Bytes()...) } //________________________________________________________________________________ // 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, - validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { + validatorDstAddr sdk.Address) []byte { return append( - GetREDsKey(delegatorAddr, cdc), + GetREDsKey(delegatorAddr), append( validatorSrcAddr.Bytes(), validatorDstAddr.Bytes()...)..., @@ -153,12 +170,12 @@ func GetREDKey(delegatorAddr, validatorSrcAddr, } // 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, - validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { + validatorDstAddr sdk.Address) []byte { return append( - GetREDsFromValSrcIndexKey(validatorSrcAddr, cdc), + GetREDsFromValSrcIndexKey(validatorSrcAddr), append( delegatorAddr.Bytes(), validatorDstAddr.Bytes()...)..., @@ -166,44 +183,66 @@ func GetREDByValSrcIndexKey(delegatorAddr, validatorSrcAddr, } // 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, - validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { + validatorDstAddr sdk.Address) []byte { return append( - GetREDsToValDstIndexKey(validatorDstAddr, cdc), + GetREDsToValDstIndexKey(validatorDstAddr), append( delegatorAddr.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 -func GetREDsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte { - res := cdc.MustMarshalBinary(&delegatorAddr) - return append(RedelegationKey, res...) +func GetREDsKey(delegatorAddr sdk.Address) []byte { + return append(RedelegationKey, delegatorAddr.Bytes()...) } // get the prefix keyspace for all redelegations redelegating away from a source validator -func GetREDsFromValSrcIndexKey(validatorSrcAddr sdk.Address, cdc *wire.Codec) []byte { - res := cdc.MustMarshalBinary(&validatorSrcAddr) - return append(RedelegationByValSrcIndexKey, res...) +func GetREDsFromValSrcIndexKey(validatorSrcAddr sdk.Address) []byte { + return append(RedelegationByValSrcIndexKey, validatorSrcAddr.Bytes()...) } // get the prefix keyspace for all redelegations redelegating towards a destination validator -func GetREDsToValDstIndexKey(validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { - res := cdc.MustMarshalBinary(&validatorDstAddr) - return append(RedelegationByValDstIndexKey, res...) +func GetREDsToValDstIndexKey(validatorDstAddr sdk.Address) []byte { + return append(RedelegationByValDstIndexKey, validatorDstAddr.Bytes()...) } // get the prefix keyspace for all redelegations redelegating towards a destination validator // from a particular delegator func GetREDsByDelToValDstIndexKey(delegatorAddr sdk.Address, - validatorDstAddr sdk.Address, cdc *wire.Codec) []byte { - + validatorDstAddr sdk.Address) []byte { return append( - GetREDsToValDstIndexKey(validatorDstAddr, cdc), + GetREDsToValDstIndexKey(validatorDstAddr), delegatorAddr.Bytes()...) } diff --git a/x/stake/keeper/sdk_types.go b/x/stake/keeper/sdk_types.go index 55ad658a2d..1e4782c86c 100644 --- a/x/stake/keeper/sdk_types.go +++ b/x/stake/keeper/sdk_types.go @@ -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 func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.Address, fn func(index int64, delegation sdk.Delegation) (stop bool)) { store := ctx.KVStore(k.storeKey) - key := GetDelegationsKey(delAddr, k.cdc) + key := GetDelegationsKey(delAddr) iterator := sdk.KVStorePrefixIterator(store, key) i := int64(0) for ; iterator.Valid(); iterator.Next() { From b3d6a59c619a9779a2bb3b9d39c7ae857e710da1 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 2 Jul 2018 18:58:52 -0400 Subject: [PATCH 03/15] ... --- x/stake/keeper/delegation.go | 20 +++++++------- x/stake/keeper/delegation_test.go | 44 ++++++++++++++++++++++++------- x/stake/keeper/key.go | 4 +++ 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index 8ede4b55e2..0fe4406c30 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -154,26 +154,24 @@ func (k Keeper) GetRedelegation(ctx sdk.Context, func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.Address) (redelegations []types.Redelegation) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, GetREDsFromValSrcIndexKey(valAddr)) - i := 0 - for ; ; i++ { - fmt.Printf("debug i: %v\n", i) + for { if !iterator.Valid() { break } - 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.Println("called") + //redelegationKey := iterator.Value() + iKey := iterator.Key() + 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) - fmt.Printf("debug iteratorKey: %v\n", iterator.Key()) + redelegationBytes := store.Get(redelegationKey) var redelegation types.Redelegation k.cdc.MustUnmarshalBinary(redelegationBytes, &redelegation) redelegations = append(redelegations, redelegation) + iterator.Next() } iterator.Close() diff --git a/x/stake/keeper/delegation_test.go b/x/stake/keeper/delegation_test.go index 4b5d12e095..90cfb8f37b 100644 --- a/x/stake/keeper/delegation_test.go +++ b/x/stake/keeper/delegation_test.go @@ -180,6 +180,40 @@ func TestUnbondDelegation(t *testing.T) { require.Equal(t, int64(4), pool.BondedTokens) } +// tests Get/Set/Remove/Has UnbondingDelegation +func TestGetRedelegationsFromValidator(t *testing.T) { + ctx, _, keeper := CreateTestInput(t, false, 0) + + rd := types.Redelegation{ + DelegatorAddr: addrDels[0], + ValidatorSrcAddr: addrVals[0], + ValidatorDstAddr: addrVals[1], + CreationHeight: 0, + MinTime: 0, + SharesSrc: sdk.NewRat(5), + SharesDst: sdk.NewRat(5), + } + + // set and retrieve a record + 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 func TestRedelegation(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) @@ -203,21 +237,13 @@ func TestRedelegation(t *testing.T) { resBond, found := keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) require.True(t, found) - 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 has = keeper.HasReceivingRedelegation(ctx, addrDels[0], addrVals[1]) require.True(t, has) - fmt.Println("hoolahoop") // modify a records, save, and retrieve rd.SharesSrc = sdk.NewRat(21) @@ -228,12 +254,10 @@ func TestRedelegation(t *testing.T) { resBond, found = keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) require.True(t, found) 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 keeper.RemoveRedelegation(ctx, rd) diff --git a/x/stake/keeper/key.go b/x/stake/keeper/key.go index c0f8c8b299..248ee9db0f 100644 --- a/x/stake/keeper/key.go +++ b/x/stake/keeper/key.go @@ -2,6 +2,7 @@ package keeper import ( "encoding/binary" + "fmt" "github.com/tendermint/tendermint/crypto" @@ -205,6 +206,9 @@ func GetREDKeyFromValSrcIndexKey(IndexKey []byte) []byte { valSrcAddr := addrs[:split] delAddr := addrs[split : 2*split] valDstAddr := addrs[2*split:] + fmt.Printf("debug delAddr: %v\n", delAddr) + fmt.Printf("debug valSrcAddr: %v\n", valSrcAddr) + fmt.Printf("debug valDstAddr: %v\n", valDstAddr) return GetREDKey(delAddr, valSrcAddr, valDstAddr) } From 2355e5ae6e1ad4d65ffa4434d5bcd1ad3461bf1d Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 2 Jul 2018 20:05:52 -0400 Subject: [PATCH 04/15] ... --- x/stake/keeper/key.go | 2 +- x/stake/types/delegation.go | 43 +++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/x/stake/keeper/key.go b/x/stake/keeper/key.go index 248ee9db0f..4b9eb56626 100644 --- a/x/stake/keeper/key.go +++ b/x/stake/keeper/key.go @@ -206,7 +206,7 @@ func GetREDKeyFromValSrcIndexKey(IndexKey []byte) []byte { valSrcAddr := addrs[:split] delAddr := addrs[split : 2*split] valDstAddr := addrs[2*split:] - fmt.Printf("debug delAddr: %v\n", delAddr) + fmt.Printf("debug delAddr: %v\n", delAddr) fmt.Printf("debug valSrcAddr: %v\n", valSrcAddr) fmt.Printf("debug valDstAddr: %v\n", valDstAddr) return GetREDKey(delAddr, valSrcAddr, valDstAddr) diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index eb38a50a7a..c945468fd8 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -13,8 +13,13 @@ import ( type Delegation struct { DelegatorAddr sdk.Address `json:"delegator_addr"` ValidatorAddr sdk.Address `json:"validator_addr"` - 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 } // two are equal @@ -57,12 +62,17 @@ 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 - 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 + DelegatorAddr sdk.Address `json:"delegator_addr"` // delegator + ValidatorAddr sdk.Address `json:"validator_addr"` // validator unbonding from owner addr + 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 } // nolint @@ -100,12 +110,17 @@ 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 - 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 } // nolint From aa243565ce70893f5cfa5328e51d5c08a518f96b Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 2 Jul 2018 20:09:57 -0400 Subject: [PATCH 05/15] fix appending over key --- x/stake/keeper/delegation.go | 1 + x/stake/keeper/key.go | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index 0fe4406c30..ded603abdd 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -162,6 +162,7 @@ func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.Addre 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) diff --git a/x/stake/keeper/key.go b/x/stake/keeper/key.go index 248ee9db0f..b831de5f23 100644 --- a/x/stake/keeper/key.go +++ b/x/stake/keeper/key.go @@ -162,12 +162,13 @@ func GetUBDsByValIndexKey(validatorAddr sdk.Address) []byte { func GetREDKey(delegatorAddr, validatorSrcAddr, validatorDstAddr sdk.Address) []byte { - return append( - GetREDsKey(delegatorAddr), - append( - validatorSrcAddr.Bytes(), - validatorDstAddr.Bytes()...)..., - ) + fmt.Println("KEY", delegatorAddr.Bytes()) + key := make([]byte, len(delegatorAddr.Bytes())) + copy(key, delegatorAddr.Bytes()) + + return append(append( + GetREDsKey(key), validatorSrcAddr.Bytes()...), + validatorDstAddr.Bytes()...) } // get the index-key for a redelegation, stored by source-validator-index @@ -206,10 +207,18 @@ func GetREDKeyFromValSrcIndexKey(IndexKey []byte) []byte { valSrcAddr := addrs[:split] delAddr := addrs[split : 2*split] valDstAddr := addrs[2*split:] - fmt.Printf("debug delAddr: %v\n", delAddr) + fmt.Printf("debug indexKey: %v\n", IndexKey) fmt.Printf("debug valSrcAddr: %v\n", valSrcAddr) + fmt.Printf("debug delAddr: %v\n", delAddr) fmt.Printf("debug valDstAddr: %v\n", valDstAddr) - return GetREDKey(delAddr, valSrcAddr, valDstAddr) + redKey := GetREDKey(delAddr, valSrcAddr, valDstAddr) + fmt.Println("------") + fmt.Printf("debug indexKey: %v\n", IndexKey) + fmt.Printf("debug valSrcAddr: %v\n", valSrcAddr) + fmt.Printf("debug delAddr: %v\n", delAddr) + fmt.Printf("debug valDstAddr: %v\n", valDstAddr) + fmt.Println("") + return redKey } // rearrange the ValDstIndexKey to get the REDKey From 6f3b4c18056fe32520edf985e3d5d492c54780c7 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 2 Jul 2018 20:58:22 -0400 Subject: [PATCH 06/15] keys cleanup --- x/stake/keeper/delegation.go | 21 +++-------- x/stake/keeper/delegation_test.go | 8 ----- x/stake/keeper/key.go | 27 +++++++------- x/stake/keeper/sdk_types.go | 2 +- x/stake/keeper/validator.go | 8 ++--- x/stake/types/delegation.go | 58 +++++++++++++++++++------------ 6 files changed, 57 insertions(+), 67 deletions(-) diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index ded603abdd..bb8170a6a3 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -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 diff --git a/x/stake/keeper/delegation_test.go b/x/stake/keeper/delegation_test.go index 90cfb8f37b..a7cb566f1d 100644 --- a/x/stake/keeper/delegation_test.go +++ b/x/stake/keeper/delegation_test.go @@ -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]) diff --git a/x/stake/keeper/key.go b/x/stake/keeper/key.go index 50d08e4255..6924a10579 100644 --- a/x/stake/keeper/key.go +++ b/x/stake/keeper/key.go @@ -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()...) } diff --git a/x/stake/keeper/sdk_types.go b/x/stake/keeper/sdk_types.go index 1e4782c86c..bedbc15591 100644 --- a/x/stake/keeper/sdk_types.go +++ b/x/stake/keeper/sdk_types.go @@ -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)) diff --git a/x/stake/keeper/validator.go b/x/stake/keeper/validator.go index 0d556f7dc4..c2711788be 100644 --- a/x/stake/keeper/validator.go +++ b/x/stake/keeper/validator.go @@ -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()) diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index c945468fd8..daaeecef1e 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -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 { From aec264d92302d2f22e9122c60ccd29290d0e4a40 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 2 Jul 2018 21:06:05 -0400 Subject: [PATCH 07/15] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b261e7d84b..4dea6d7f8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ BREAKING CHANGES * Add REST endpoint to unrevoke a validator previously revoked for downtime * Add REST endpoint to retrieve liveness signing information for a validator * [types] renamed rational.Evaluate to rational.Round{Int64, Int} +* [stake] most index keys nolonger hold a value - inputs are rearranged to form the desired key FEATURES * [gaiacli] You can now attach a simple text-only memo to any transaction, with the `--memo` flag From 271fbb2db3a5946f20ba34891bdedc77a9b15e15 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 2 Jul 2018 21:16:32 -0400 Subject: [PATCH 08/15] remove some junk --- x/stake/types/delegation.go | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index daaeecef1e..eb38a50a7a 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -15,15 +15,8 @@ type Delegation struct { ValidatorAddr sdk.Address `json:"validator_addr"` 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 -//} - // two are equal func (d Delegation) Equal(d2 Delegation) bool { return bytes.Equal(d.DelegatorAddr, d2.DelegatorAddr) && @@ -70,17 +63,8 @@ type UnbondingDelegation struct { 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 -//} - // nolint func (d UnbondingDelegation) Equal(d2 UnbondingDelegation) bool { bz1 := MsgCdc.MustMarshalBinary(&d) @@ -122,19 +106,8 @@ type Redelegation struct { 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 -//} - // nolint func (d Redelegation) Equal(d2 Redelegation) bool { bz1 := MsgCdc.MustMarshalBinary(&d) From 4f0c7d8746e3e0d4315888dac72869034ed336a0 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 3 Jul 2018 13:37:17 -0400 Subject: [PATCH 09/15] address bucky comments - rearrange appends --- x/stake/keeper/delegation_test.go | 4 +++- x/stake/keeper/key.go | 39 ++++++++++++------------------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/x/stake/keeper/delegation_test.go b/x/stake/keeper/delegation_test.go index a7cb566f1d..c0a3ee8c57 100644 --- a/x/stake/keeper/delegation_test.go +++ b/x/stake/keeper/delegation_test.go @@ -179,7 +179,7 @@ func TestUnbondDelegation(t *testing.T) { require.Equal(t, int64(4), pool.BondedTokens) } -// tests Get/Set/Remove/Has UnbondingDelegation +// Make sure that that the retrieving the delegations doesn't affect the state func TestGetRedelegationsFromValidator(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) @@ -198,10 +198,12 @@ func TestGetRedelegationsFromValidator(t *testing.T) { resBond, found := keeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1]) require.True(t, found) + // get the redelegations one time redelegations := keeper.GetRedelegationsFromValidator(ctx, addrVals[0]) require.Equal(t, 1, len(redelegations)) require.True(t, redelegations[0].Equal(resBond)) + // get the redelegations a second time, should be exactly the same redelegations = keeper.GetRedelegationsFromValidator(ctx, addrVals[0]) require.Equal(t, 1, len(redelegations)) require.True(t, redelegations[0].Equal(resBond)) diff --git a/x/stake/keeper/key.go b/x/stake/keeper/key.go index 6924a10579..4d1ad0baea 100644 --- a/x/stake/keeper/key.go +++ b/x/stake/keeper/key.go @@ -52,7 +52,7 @@ func GetValidatorsBondedIndexKey(ownerAddr sdk.Address) []byte { return append(ValidatorsBondedIndexKey, ownerAddr.Bytes()...) } -// rearrange the ValBondedIndexKey to get the ValidatorKey +// Get the validator owner address from ValBondedIndexKey func GetAddressFromValBondedIndexKey(IndexKey []byte) []byte { return IndexKey[1:] // remove prefix bytes } @@ -84,10 +84,12 @@ func getValidatorPowerRank(validator types.Validator, pool types.Pool) []byte { counterBytes := make([]byte, 2) binary.BigEndian.PutUint16(counterBytes, ^uint16(validator.BondIntraTxCounter)) // invert counter (first txns have priority) - return append(ValidatorsByPowerIndexKey, - append(revokedBytes, - append(powerBytes, - append(heightBytes, counterBytes...)...)...)...) + return append(append(append(append( + ValidatorsByPowerIndexKey, + revokedBytes...), + powerBytes...), + heightBytes...), + counterBytes...) } // get the key for the accumulated update validators. @@ -115,12 +117,8 @@ 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 { - - key := make([]byte, len(delegatorAddr.Bytes())) - copy(key, delegatorAddr.Bytes()) - return append( - GetUBDsKey(key), + GetUBDsKey(delegatorAddr.Bytes()), validatorAddr.Bytes()...) } @@ -161,11 +159,8 @@ func GetUBDsByValIndexKey(validatorAddr sdk.Address) []byte { func GetREDKey(delegatorAddr, validatorSrcAddr, validatorDstAddr sdk.Address) []byte { - key := make([]byte, len(delegatorAddr.Bytes())) - copy(key, delegatorAddr.Bytes()) - return append(append( - GetREDsKey(key), + GetREDsKey(delegatorAddr.Bytes()), validatorSrcAddr.Bytes()...), validatorDstAddr.Bytes()...) } @@ -175,12 +170,10 @@ func GetREDKey(delegatorAddr, validatorSrcAddr, func GetREDByValSrcIndexKey(delegatorAddr, validatorSrcAddr, validatorDstAddr sdk.Address) []byte { - return append( + return append(append( GetREDsFromValSrcIndexKey(validatorSrcAddr), - append( - delegatorAddr.Bytes(), - validatorDstAddr.Bytes()...)..., - ) + delegatorAddr.Bytes()...), + validatorDstAddr.Bytes()...) } // get the index-key for a redelegation, stored by destination-validator-index @@ -188,12 +181,10 @@ func GetREDByValSrcIndexKey(delegatorAddr, validatorSrcAddr, func GetREDByValDstIndexKey(delegatorAddr, validatorSrcAddr, validatorDstAddr sdk.Address) []byte { - return append( + return append(append( GetREDsToValDstIndexKey(validatorDstAddr), - append( - delegatorAddr.Bytes(), - validatorSrcAddr.Bytes()...)..., - ) + delegatorAddr.Bytes()...), + validatorSrcAddr.Bytes()...) } // rearrange the ValSrcIndexKey to get the REDKey From ab964da10568b7ff1872dc754160c87312e940bf Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 3 Jul 2018 15:03:35 -0400 Subject: [PATCH 10/15] marshal/unmarshal delegation --- x/stake/types/delegation.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index eb38a50a7a..705dcb6cd9 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -5,6 +5,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" ) // Delegation represents the bond with tokens held by an account. It is @@ -17,6 +18,41 @@ type Delegation struct { Height int64 `json:"height"` // Last height bond updated } +type delegationValue struct { + Shares sdk.Rat + Height int64 +} + +// return the delegation without fields contained within the key for the store +func MarshalDelegation(cdc *wire.Codec, delegation Delegation) []byte { + val := delegationValue{ + delegation.Shares, + delegation.Height, + } + return cdc.MustMarshalBinary(val) +} + +// return the delegation without fields contained within the key for the store +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 { + panic("key length not even") + } + delAddr := sdk.Address{addrs[:split]} + valAddr := sdk.Address{addrs[split:]} + + return Delegation{ + DelegatorAddr: delAddr, + ValidatorAddr: valAddr, + Shares: storeValue.Shares, + Height: storeValue.Height, + } +} + // two are equal func (d Delegation) Equal(d2 Delegation) bool { return bytes.Equal(d.DelegatorAddr, d2.DelegatorAddr) && From 199c1e81ebda231ae344e086f27654772be93284 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 3 Jul 2018 23:44:54 -0400 Subject: [PATCH 11/15] delegations new key format ported --- x/stake/client/cli/query.go | 7 +++---- x/stake/client/rest/query.go | 10 +++------- x/stake/keeper/delegation.go | 17 +++++++---------- x/stake/keeper/sdk_types.go | 4 +--- x/stake/types/delegation.go | 9 ++++----- 5 files changed, 18 insertions(+), 29 deletions(-) diff --git a/x/stake/client/cli/query.go b/x/stake/client/cli/query.go index 5a22f2c416..e66a3bb4d5 100644 --- a/x/stake/client/cli/query.go +++ b/x/stake/client/cli/query.go @@ -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) } diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 12ef882e5e..b19cea72b7 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -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 { diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index bb8170a6a3..9c56b85125 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -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) } diff --git a/x/stake/keeper/sdk_types.go b/x/stake/keeper/sdk_types.go index bedbc15591..9e45982b46 100644 --- a/x/stake/keeper/sdk_types.go +++ b/x/stake/keeper/sdk_types.go @@ -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 diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index 705dcb6cd9..b6d35fb560 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -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, From 04921b9ebd640b3b5f3c9d6dabcc73d438976c88 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 4 Jul 2018 00:29:02 -0400 Subject: [PATCH 12/15] ubd and red more limited values --- x/stake/client/cli/query.go | 24 ++++------ x/stake/client/rest/query.go | 16 +------ x/stake/keeper/delegation.go | 60 ++++++++++++------------ x/stake/types/delegation.go | 90 +++++++++++++++++++++++++++++++++++- 4 files changed, 130 insertions(+), 60 deletions(-) diff --git a/x/stake/client/cli/query.go b/x/stake/client/cli/query.go index e66a3bb4d5..d50b0d14db 100644 --- a/x/stake/client/cli/query.go +++ b/x/stake/client/cli/query.go @@ -75,9 +75,9 @@ func GetCmdQueryValidators(storeName string, cdc *wire.Codec) *cobra.Command { // parse out the validators var validators []stake.Validator - for _, KV := range resKVs { + for _, kv := range resKVs { var validator stake.Validator - cdc.MustUnmarshalBinary(KV.Value, &validator) + cdc.MustUnmarshalBinary(kv.Value, &validator) validators = append(validators, validator) } @@ -178,8 +178,8 @@ func GetCmdQueryDelegations(storeName string, cdc *wire.Codec) *cobra.Command { // parse out the validators var delegations []stake.Delegation - for _, KV := range resKVs { - delegation := types.UnmarshalDelegation(cdc, KV.Key, KV.Value) + for _, kv := range resKVs { + delegation := types.UnmarshalDelegation(cdc, kv.Key, kv.Value) delegations = append(delegations, delegation) } @@ -221,7 +221,7 @@ func GetCmdQueryUnbondingDelegation(storeName string, cdc *wire.Codec) *cobra.Co } // parse out the unbonding delegation - ubd := new(stake.UnbondingDelegation) + ubd := types.UnmarshalUBD(cdc, key, res) switch viper.Get(cli.OutputFlag) { case "text": @@ -231,7 +231,6 @@ func GetCmdQueryUnbondingDelegation(storeName string, cdc *wire.Codec) *cobra.Co } fmt.Println(resp) case "json": - cdc.MustUnmarshalBinary(res, ubd) output, err := wire.MarshalJSONIndent(cdc, ubd) if err != nil { return err @@ -269,9 +268,8 @@ func GetCmdQueryUnbondingDelegations(storeName string, cdc *wire.Codec) *cobra.C // parse out the validators var ubds []stake.UnbondingDelegation - for _, KV := range resKVs { - var ubd stake.UnbondingDelegation - cdc.MustUnmarshalBinary(KV.Value, &ubd) + for _, kv := range resKVs { + ubd := types.UnmarshalUBD(cdc, kv.Key, kv.Value) ubds = append(ubds, ubd) } @@ -316,7 +314,7 @@ func GetCmdQueryRedelegation(storeName string, cdc *wire.Codec) *cobra.Command { } // parse out the unbonding delegation - red := new(stake.Redelegation) + red := types.UnmarshalRED(cdc, key, res) switch viper.Get(cli.OutputFlag) { case "text": @@ -326,7 +324,6 @@ func GetCmdQueryRedelegation(storeName string, cdc *wire.Codec) *cobra.Command { } fmt.Println(resp) case "json": - cdc.MustUnmarshalBinary(res, red) output, err := wire.MarshalJSONIndent(cdc, red) if err != nil { return err @@ -364,9 +361,8 @@ func GetCmdQueryRedelegations(storeName string, cdc *wire.Codec) *cobra.Command // parse out the validators var reds []stake.Redelegation - for _, KV := range resKVs { - var red stake.Redelegation - cdc.MustUnmarshalBinary(KV.Value, &red) + for _, kv := range resKVs { + red := types.UnmarshalRED(cdc, kv.Key, kv.Value) reds = append(reds, red) } diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index b19cea72b7..457806de08 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -128,13 +128,7 @@ func ubdHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc { return } - var ubd stake.UnbondingDelegation - err = cdc.UnmarshalBinary(res, &ubd) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf("couldn't decode unbonding-delegation. Error: %s", err.Error()))) - return - } + ubd := types.UnmarshalUBD(cdc, key, res) output, err := cdc.MarshalJSON(ubd) if err != nil { @@ -193,13 +187,7 @@ func redHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc { return } - var red stake.Redelegation - err = cdc.UnmarshalBinary(res, &red) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf("couldn't decode redelegation. Error: %s", err.Error()))) - return - } + red := types.UnmarshalRED(cdc, key, res) output, err := cdc.MarshalJSON(red) if err != nil { diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index 9c56b85125..3d949a9a0a 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -82,49 +82,48 @@ func (k Keeper) GetUnbondingDelegation(ctx sdk.Context, DelegatorAddr, ValidatorAddr sdk.Address) (ubd types.UnbondingDelegation, found bool) { store := ctx.KVStore(k.storeKey) - ubdKey := GetUBDKey(DelegatorAddr, ValidatorAddr) - bz := store.Get(ubdKey) - if bz == nil { + key := GetUBDKey(DelegatorAddr, ValidatorAddr) + value := store.Get(key) + if value == nil { return ubd, false } - k.cdc.MustUnmarshalBinary(bz, &ubd) + ubd = types.UnmarshalUBD(k.cdc, key, value) return ubd, true } // 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) (ubds []types.UnbondingDelegation) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, GetUBDsByValIndexKey(valAddr)) for { if !iterator.Valid() { break } - unbondingKey := GetUBDKeyFromValIndexKey(iterator.Key()) - unbondingBytes := store.Get(unbondingKey) - var unbondingDelegation types.UnbondingDelegation - k.cdc.MustUnmarshalBinary(unbondingBytes, &unbondingDelegation) - unbondingDelegations = append(unbondingDelegations, unbondingDelegation) + key := GetUBDKeyFromValIndexKey(iterator.Key()) + value := store.Get(key) + ubd := types.UnmarshalUBD(k.cdc, key, value) + ubds = append(ubds, ubd) iterator.Next() } iterator.Close() - return unbondingDelegations + return ubds } // set the unbonding delegation and associated index func (k Keeper) SetUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinary(ubd) - ubdKey := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) - store.Set(ubdKey, bz) - store.Set(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr), []byte{}) + bz := types.MarshalUBD(k.cdc, ubd) + key := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) + store.Set(key, bz) + store.Set(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr), []byte{}) // index, store empty bytes } // remove the unbonding delegation object and associated index func (k Keeper) RemoveUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { store := ctx.KVStore(k.storeKey) - ubdKey := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) - store.Delete(ubdKey) + key := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) + store.Delete(key) store.Delete(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr)) } @@ -135,33 +134,32 @@ func (k Keeper) GetRedelegation(ctx sdk.Context, DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr sdk.Address) (red types.Redelegation, found bool) { store := ctx.KVStore(k.storeKey) - redKey := GetREDKey(DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr) - bz := store.Get(redKey) - if bz == nil { + key := GetREDKey(DelegatorAddr, ValidatorSrcAddr, ValidatorDstAddr) + value := store.Get(key) + if value == nil { return red, false } - k.cdc.MustUnmarshalBinary(bz, &red) + red = types.UnmarshalRED(k.cdc, key, value) return red, true } // 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) (reds []types.Redelegation) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, GetREDsFromValSrcIndexKey(valAddr)) for { if !iterator.Valid() { break } - redelegationKey := GetREDKeyFromValSrcIndexKey(iterator.Key()) - redelegationBytes := store.Get(redelegationKey) - var redelegation types.Redelegation - k.cdc.MustUnmarshalBinary(redelegationBytes, &redelegation) - redelegations = append(redelegations, redelegation) + key := GetREDKeyFromValSrcIndexKey(iterator.Key()) + value := store.Get(key) + red := types.UnmarshalRED(k.cdc, key, value) + reds = append(reds, red) iterator.Next() } iterator.Close() - return redelegations + return reds } // has a redelegation @@ -184,9 +182,9 @@ func (k Keeper) HasReceivingRedelegation(ctx sdk.Context, // set a redelegation and associated index 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) - store.Set(redKey, bz) + bz := types.MarshalRED(k.cdc, red) + key := GetREDKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr) + store.Set(key, bz) store.Set(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), []byte{}) store.Set(GetREDByValDstIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), []byte{}) } diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index b6d35fb560..0a3c9cf4fc 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -39,7 +39,7 @@ func UnmarshalDelegation(cdc *wire.Codec, key, value []byte) Delegation { addrs := key[1:] // remove prefix bytes if len(addrs) != 40 { - panic("key length not even") + panic("unexpected key length") } delAddr := sdk.Address(addrs[:20]) valAddr := sdk.Address(addrs[20:]) @@ -100,6 +100,46 @@ type UnbondingDelegation struct { Balance sdk.Coin `json:"balance"` // atoms to receive at completion } +type ubdValue struct { + CreationHeight int64 + MinTime int64 + InitialBalance sdk.Coin + Balance sdk.Coin +} + +// return the unbonding delegation without fields contained within the key for the store +func MarshalUBD(cdc *wire.Codec, ubd UnbondingDelegation) []byte { + val := ubdValue{ + ubd.CreationHeight, + ubd.MinTime, + ubd.InitialBalance, + ubd.Balance, + } + return cdc.MustMarshalBinary(val) +} + +// return the unbonding delegation without fields contained within the key for the store +func UnmarshalUBD(cdc *wire.Codec, key, value []byte) UnbondingDelegation { + var storeValue ubdValue + cdc.MustUnmarshalBinary(value, &storeValue) + + addrs := key[1:] // remove prefix bytes + if len(addrs) != 40 { + panic("unexpected key length") + } + delAddr := sdk.Address(addrs[:20]) + valAddr := sdk.Address(addrs[20:]) + + return UnbondingDelegation{ + DelegatorAddr: delAddr, + ValidatorAddr: valAddr, + CreationHeight: storeValue.CreationHeight, + MinTime: storeValue.MinTime, + InitialBalance: storeValue.InitialBalance, + Balance: storeValue.Balance, + } +} + // nolint func (d UnbondingDelegation) Equal(d2 UnbondingDelegation) bool { bz1 := MsgCdc.MustMarshalBinary(&d) @@ -143,6 +183,54 @@ type Redelegation struct { SharesDst sdk.Rat `json:"shares_dst"` // amount of destination shares redelegating } +type redValue struct { + CreationHeight int64 + MinTime int64 + InitialBalance sdk.Coin + Balance sdk.Coin + SharesSrc sdk.Rat + SharesDst sdk.Rat +} + +// return the unbonding delegation without fields contained within the key for the store +func MarshalRED(cdc *wire.Codec, red Redelegation) []byte { + val := redValue{ + red.CreationHeight, + red.MinTime, + red.InitialBalance, + red.Balance, + red.SharesSrc, + red.SharesDst, + } + return cdc.MustMarshalBinary(val) +} + +// return the unbonding delegation without fields contained within the key for the store +func UnmarshalRED(cdc *wire.Codec, key, value []byte) Redelegation { + var storeValue redValue + cdc.MustUnmarshalBinary(value, &storeValue) + + addrs := key[1:] // remove prefix bytes + if len(addrs) != 60 { + panic("unexpected key length") + } + delAddr := sdk.Address(addrs[:20]) + valSrcAddr := sdk.Address(addrs[20:40]) + valDstAddr := sdk.Address(addrs[40:60]) + + return Redelegation{ + DelegatorAddr: delAddr, + ValidatorSrcAddr: valSrcAddr, + ValidatorDstAddr: valDstAddr, + CreationHeight: storeValue.CreationHeight, + MinTime: storeValue.MinTime, + InitialBalance: storeValue.InitialBalance, + Balance: storeValue.Balance, + SharesSrc: storeValue.SharesSrc, + SharesDst: storeValue.SharesDst, + } +} + // nolint func (d Redelegation) Equal(d2 Redelegation) bool { bz1 := MsgCdc.MustMarshalBinary(&d) From a2f7b582df0789a0a0993be32afb5df4e047a232 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 4 Jul 2018 01:32:49 -0400 Subject: [PATCH 13/15] validators smaller values stored --- x/slashing/handler_test.go | 3 +- x/slashing/test_common.go | 10 +++--- x/stake/client/cli/query.go | 8 ++--- x/stake/client/rest/query.go | 15 ++++----- x/stake/keeper/sdk_types.go | 5 ++- x/stake/keeper/validator.go | 30 +++++++---------- x/stake/types/delegation.go | 6 ++-- x/stake/types/validator.go | 65 ++++++++++++++++++++++++++++++++++++ 8 files changed, 100 insertions(+), 42 deletions(-) diff --git a/x/slashing/handler_test.go b/x/slashing/handler_test.go index c2c5d9166f..d5a6b15dbb 100644 --- a/x/slashing/handler_test.go +++ b/x/slashing/handler_test.go @@ -15,7 +15,8 @@ func TestCannotUnrevokeUnlessRevoked(t *testing.T) { slh := NewHandler(keeper) amtInt := int64(100) addr, val, amt := addrs[0], pks[0], sdk.NewInt(amtInt) - got := stake.NewHandler(sk)(ctx, newTestMsgCreateValidator(addr, val, amt)) + msg := newTestMsgCreateValidator(addr, val, amt) + got := stake.NewHandler(sk)(ctx, msg) require.True(t, got.IsOK()) stake.EndBlocker(ctx, sk) require.Equal(t, ck.GetCoins(ctx, addr), sdk.Coins{{sk.GetParams(ctx).BondDenom, initCoins.Sub(amt)}}) diff --git a/x/slashing/test_common.go b/x/slashing/test_common.go index edf119bc7f..77b553317a 100644 --- a/x/slashing/test_common.go +++ b/x/slashing/test_common.go @@ -23,16 +23,16 @@ import ( // TODO remove dependencies on staking (should only refer to validator set type from sdk) var ( - addrs = []sdk.Address{ - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6160"), - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6161"), - testAddr("A58856F0FD53BF058B4909A21AEC019107BA6162"), - } pks = []crypto.PubKey{ newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB50"), newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"), newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"), } + addrs = []sdk.Address{ + pks[0].Address(), + pks[1].Address(), + pks[2].Address(), + } initCoins sdk.Int = sdk.NewInt(200) ) diff --git a/x/stake/client/cli/query.go b/x/stake/client/cli/query.go index d50b0d14db..ee495e96e0 100644 --- a/x/stake/client/cli/query.go +++ b/x/stake/client/cli/query.go @@ -32,8 +32,8 @@ func GetCmdQueryValidator(storeName string, cdc *wire.Codec) *cobra.Command { if err != nil { return err } - validator := new(stake.Validator) - cdc.MustUnmarshalBinary(res, validator) + + validator := types.UnmarshalValidator(cdc, addr, res) switch viper.Get(cli.OutputFlag) { case "text": @@ -76,8 +76,8 @@ func GetCmdQueryValidators(storeName string, cdc *wire.Codec) *cobra.Command { // parse out the validators var validators []stake.Validator for _, kv := range resKVs { - var validator stake.Validator - cdc.MustUnmarshalBinary(kv.Value, &validator) + addr := kv.Key[1:] + validator := types.UnmarshalValidator(cdc, addr, kv.Value) validators = append(validators, validator) } diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 457806de08..49051c73ef 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -275,15 +275,14 @@ func validatorsHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerF // parse out the validators validators := make([]StakeValidatorOutput, len(kvs)) for i, kv := range kvs { - var validator stake.Validator - var bech32Validator StakeValidatorOutput - err = cdc.UnmarshalBinary(kv.Value, &validator) - if err == nil { - bech32Validator, err = bech32StakeValidatorOutput(validator) - } + + addr := kv.Key[1:] + validator := types.UnmarshalValidator(cdc, addr, kv.Value) + + bech32Validator, err := bech32StakeValidatorOutput(validator) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf("couldn't decode validator. Error: %s", err.Error()))) + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) return } validators[i] = bech32Validator diff --git a/x/stake/keeper/sdk_types.go b/x/stake/keeper/sdk_types.go index 9e45982b46..0fe465892c 100644 --- a/x/stake/keeper/sdk_types.go +++ b/x/stake/keeper/sdk_types.go @@ -16,9 +16,8 @@ func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validato iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey) i := int64(0) for ; iterator.Valid(); iterator.Next() { - bz := iterator.Value() - var validator types.Validator - k.cdc.MustUnmarshalBinary(bz, &validator) + addr := iterator.Key()[1:] + validator := types.UnmarshalValidator(k.cdc, addr, iterator.Value()) stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to? if stop { break diff --git a/x/stake/keeper/validator.go b/x/stake/keeper/validator.go index c2711788be..ad70958a3c 100644 --- a/x/stake/keeper/validator.go +++ b/x/stake/keeper/validator.go @@ -14,11 +14,12 @@ import ( // get a single validator func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.Address) (validator types.Validator, found bool) { store := ctx.KVStore(k.storeKey) - b := store.Get(GetValidatorKey(addr)) - if b == nil { + value := store.Get(GetValidatorKey(addr)) + if value == nil { return validator, false } - k.cdc.MustUnmarshalBinary(b, &validator) + fmt.Printf("debug addr: %v\n", addr) + validator = types.UnmarshalValidator(k.cdc, addr, value) return validator, true } @@ -35,15 +36,13 @@ func (k Keeper) GetValidatorByPubKey(ctx sdk.Context, pubkey crypto.PubKey) (val // set the main record holding validator details func (k Keeper) SetValidator(ctx sdk.Context, validator types.Validator) { store := ctx.KVStore(k.storeKey) - // set main store - bz := k.cdc.MustMarshalBinary(validator) + bz := types.MarshalValidator(k.cdc, validator) store.Set(GetValidatorKey(validator.Owner), bz) } // validator index func (k Keeper) SetValidatorByPubKeyIndex(ctx sdk.Context, validator types.Validator) { store := ctx.KVStore(k.storeKey) - // set pointer by pubkey store.Set(GetValidatorByPubKeyIndexKey(validator.PubKey), validator.Owner) } @@ -75,9 +74,8 @@ func (k Keeper) GetAllValidators(ctx sdk.Context) (validators []types.Validator) if !iterator.Valid() { break } - bz := iterator.Value() - var validator types.Validator - k.cdc.MustUnmarshalBinary(bz, &validator) + addr := iterator.Key()[1:] + validator := types.UnmarshalValidator(k.cdc, addr, iterator.Value()) validators = append(validators, validator) iterator.Next() } @@ -96,9 +94,8 @@ func (k Keeper) GetValidators(ctx sdk.Context, maxRetrieve int16) (validators [] if !iterator.Valid() || i > int(maxRetrieve-1) { break } - bz := iterator.Value() - var validator types.Validator - k.cdc.MustUnmarshalBinary(bz, &validator) + addr := iterator.Key()[1:] + validator := types.UnmarshalValidator(k.cdc, addr, iterator.Value()) validators[i] = validator iterator.Next() } @@ -205,8 +202,7 @@ func (k Keeper) UpdateValidator(ctx sdk.Context, validator types.Validator) type // always update the main list ordered by owner address before exiting defer func() { - bz := k.cdc.MustMarshalBinary(validator) - store.Set(GetValidatorKey(ownerAddr), bz) + k.SetValidator(ctx, validator) }() // retrieve the old validator record @@ -441,8 +437,7 @@ func (k Keeper) unbondValidator(ctx sdk.Context, validator types.Validator) type k.SetPool(ctx, pool) // save the now unbonded validator record - bzVal := k.cdc.MustMarshalBinary(validator) - store.Set(GetValidatorKey(validator.Owner), bzVal) + k.SetValidator(ctx, validator) // add to accumulated changes for tendermint bzABCI := k.cdc.MustMarshalBinary(validator.ABCIValidatorZero()) @@ -469,8 +464,7 @@ func (k Keeper) bondValidator(ctx sdk.Context, validator types.Validator) types. k.SetPool(ctx, pool) // save the now bonded validator record to the three referenced stores - bzVal := k.cdc.MustMarshalBinary(validator) - store.Set(GetValidatorKey(validator.Owner), bzVal) + k.SetValidator(ctx, validator) store.Set(GetValidatorsBondedIndexKey(validator.Owner), []byte{}) // add to accumulated changes for tendermint diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index 0a3c9cf4fc..34aa323772 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -118,7 +118,7 @@ func MarshalUBD(cdc *wire.Codec, ubd UnbondingDelegation) []byte { return cdc.MustMarshalBinary(val) } -// return the unbonding delegation without fields contained within the key for the store +// unmarshal a unbonding delegation from a store key and value func UnmarshalUBD(cdc *wire.Codec, key, value []byte) UnbondingDelegation { var storeValue ubdValue cdc.MustUnmarshalBinary(value, &storeValue) @@ -192,7 +192,7 @@ type redValue struct { SharesDst sdk.Rat } -// return the unbonding delegation without fields contained within the key for the store +// return the redelegation without fields contained within the key for the store func MarshalRED(cdc *wire.Codec, red Redelegation) []byte { val := redValue{ red.CreationHeight, @@ -205,7 +205,7 @@ func MarshalRED(cdc *wire.Codec, red Redelegation) []byte { return cdc.MustMarshalBinary(val) } -// return the unbonding delegation without fields contained within the key for the store +// unmarshal a redelegation from a store key and value func UnmarshalRED(cdc *wire.Codec, key, value []byte) Redelegation { var storeValue redValue cdc.MustUnmarshalBinary(value, &storeValue) diff --git a/x/stake/types/validator.go b/x/stake/types/validator.go index c2c19439be..a2a04b8659 100644 --- a/x/stake/types/validator.go +++ b/x/stake/types/validator.go @@ -9,6 +9,7 @@ import ( tmtypes "github.com/tendermint/tendermint/types" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" ) // Validator defines the total amount of bond shares and their exchange rate to @@ -60,6 +61,70 @@ func NewValidator(owner sdk.Address, pubKey crypto.PubKey, description Descripti } } +// what's kept in the store value +type validatorValue struct { + PubKey crypto.PubKey + Revoked bool + PoolShares PoolShares + DelegatorShares sdk.Rat + Description Description + BondHeight int64 + BondIntraTxCounter int16 + ProposerRewardPool sdk.Coins + Commission sdk.Rat + CommissionMax sdk.Rat + CommissionChangeRate sdk.Rat + CommissionChangeToday sdk.Rat + PrevBondedShares sdk.Rat +} + +// return the redelegation without fields contained within the key for the store +func MarshalValidator(cdc *wire.Codec, validator Validator) []byte { + val := validatorValue{ + PubKey: validator.PubKey, + Revoked: validator.Revoked, + PoolShares: validator.PoolShares, + DelegatorShares: validator.DelegatorShares, + Description: validator.Description, + BondHeight: validator.BondHeight, + BondIntraTxCounter: validator.BondIntraTxCounter, + ProposerRewardPool: validator.ProposerRewardPool, + Commission: validator.Commission, + CommissionMax: validator.CommissionMax, + CommissionChangeRate: validator.CommissionChangeRate, + CommissionChangeToday: validator.CommissionChangeToday, + PrevBondedShares: validator.PrevBondedShares, + } + return cdc.MustMarshalBinary(val) +} + +// unmarshal a redelegation from a store key and value +func UnmarshalValidator(cdc *wire.Codec, ownerAddr, value []byte) Validator { + var storeValue validatorValue + cdc.MustUnmarshalBinary(value, &storeValue) + + if len(ownerAddr) != 20 { + panic("unexpected address length") + } + + return Validator{ + Owner: ownerAddr, + PubKey: storeValue.PubKey, + Revoked: storeValue.Revoked, + PoolShares: storeValue.PoolShares, + DelegatorShares: storeValue.DelegatorShares, + Description: storeValue.Description, + BondHeight: storeValue.BondHeight, + BondIntraTxCounter: storeValue.BondIntraTxCounter, + ProposerRewardPool: storeValue.ProposerRewardPool, + Commission: storeValue.Commission, + CommissionMax: storeValue.CommissionMax, + CommissionChangeRate: storeValue.CommissionChangeRate, + CommissionChangeToday: storeValue.CommissionChangeToday, + PrevBondedShares: storeValue.PrevBondedShares, + } +} + // only the vitals - does not check bond height of IntraTxCounter func (v Validator) Equal(c2 Validator) bool { return v.PubKey.Equals(c2.PubKey) && From 319358c587d709ecfb6a1bf38c7fdfa3b2d4e39b Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 4 Jul 2018 14:34:55 -0400 Subject: [PATCH 14/15] changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56bad46c37..b356641404 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,8 +37,9 @@ BREAKING CHANGES * Add REST endpoint to unrevoke a validator previously revoked for downtime * Add REST endpoint to retrieve liveness signing information for a validator * [types] renamed rational.Evaluate to rational.Round{Int64, Int} -* [stake] most index keys nolonger hold a value - inputs are rearranged to form the desired key +* [x/stake] most index keys nolonger hold a value - inputs are rearranged to form the desired key * [lcd] Switch key creation output to return bech32 +* [x/stake] store-value for delegation, validator, ubd, and red do not hold duplicate information contained store-key FEATURES * [gaiacli] You can now attach a simple text-only memo to any transaction, with the `--memo` flag From aed3a873669d9c84f7087be78e7320e0a2dd7baf Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 4 Jul 2018 17:07:06 -0400 Subject: [PATCH 15/15] address cwgoes comments --- types/account.go | 6 ++- x/stake/client/cli/query.go | 16 +++---- x/stake/client/rest/query.go | 28 +++++++++-- x/stake/keeper/delegation.go | 20 ++++---- x/stake/keeper/key.go | 22 ++++----- x/stake/keeper/sdk_types.go | 4 +- x/stake/keeper/validator.go | 9 ++-- x/stake/types/delegation.go | 90 ++++++++++++++++++++++++++---------- x/stake/types/validator.go | 25 ++++++++-- 9 files changed, 149 insertions(+), 71 deletions(-) diff --git a/types/account.go b/types/account.go index ce872d8b54..0cdc75fe2d 100644 --- a/types/account.go +++ b/types/account.go @@ -13,8 +13,12 @@ import ( //Address is a go crypto-style Address type Address = cmn.HexBytes -// Bech32 prefixes +// nolint const ( + // expected address length + AddrLen = 20 + + // Bech32 prefixes Bech32PrefixAccAddr = "cosmosaccaddr" Bech32PrefixAccPub = "cosmosaccpub" Bech32PrefixValAddr = "cosmosvaladdr" diff --git a/x/stake/client/cli/query.go b/x/stake/client/cli/query.go index ee495e96e0..39c7771f0a 100644 --- a/x/stake/client/cli/query.go +++ b/x/stake/client/cli/query.go @@ -33,7 +33,7 @@ func GetCmdQueryValidator(storeName string, cdc *wire.Codec) *cobra.Command { return err } - validator := types.UnmarshalValidator(cdc, addr, res) + validator := types.MustUnmarshalValidator(cdc, addr, res) switch viper.Get(cli.OutputFlag) { case "text": @@ -77,7 +77,7 @@ func GetCmdQueryValidators(storeName string, cdc *wire.Codec) *cobra.Command { var validators []stake.Validator for _, kv := range resKVs { addr := kv.Key[1:] - validator := types.UnmarshalValidator(cdc, addr, kv.Value) + validator := types.MustUnmarshalValidator(cdc, addr, kv.Value) validators = append(validators, validator) } @@ -131,7 +131,7 @@ func GetCmdQueryDelegation(storeName string, cdc *wire.Codec) *cobra.Command { } // parse out the delegation - delegation := types.UnmarshalDelegation(cdc, key, res) + delegation := types.MustUnmarshalDelegation(cdc, key, res) switch viper.Get(cli.OutputFlag) { case "text": @@ -179,7 +179,7 @@ func GetCmdQueryDelegations(storeName string, cdc *wire.Codec) *cobra.Command { // parse out the validators var delegations []stake.Delegation for _, kv := range resKVs { - delegation := types.UnmarshalDelegation(cdc, kv.Key, kv.Value) + delegation := types.MustUnmarshalDelegation(cdc, kv.Key, kv.Value) delegations = append(delegations, delegation) } @@ -221,7 +221,7 @@ func GetCmdQueryUnbondingDelegation(storeName string, cdc *wire.Codec) *cobra.Co } // parse out the unbonding delegation - ubd := types.UnmarshalUBD(cdc, key, res) + ubd := types.MustUnmarshalUBD(cdc, key, res) switch viper.Get(cli.OutputFlag) { case "text": @@ -269,7 +269,7 @@ func GetCmdQueryUnbondingDelegations(storeName string, cdc *wire.Codec) *cobra.C // parse out the validators var ubds []stake.UnbondingDelegation for _, kv := range resKVs { - ubd := types.UnmarshalUBD(cdc, kv.Key, kv.Value) + ubd := types.MustUnmarshalUBD(cdc, kv.Key, kv.Value) ubds = append(ubds, ubd) } @@ -314,7 +314,7 @@ func GetCmdQueryRedelegation(storeName string, cdc *wire.Codec) *cobra.Command { } // parse out the unbonding delegation - red := types.UnmarshalRED(cdc, key, res) + red := types.MustUnmarshalRED(cdc, key, res) switch viper.Get(cli.OutputFlag) { case "text": @@ -362,7 +362,7 @@ func GetCmdQueryRedelegations(storeName string, cdc *wire.Codec) *cobra.Command // parse out the validators var reds []stake.Redelegation for _, kv := range resKVs { - red := types.UnmarshalRED(cdc, kv.Key, kv.Value) + red := types.MustUnmarshalRED(cdc, kv.Key, kv.Value) reds = append(reds, red) } diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 49051c73ef..090445f0a8 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -77,7 +77,12 @@ func delegationHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerF return } - delegation := types.UnmarshalDelegation(cdc, key, res) + delegation, err := types.UnmarshalDelegation(cdc, key, res) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) + return + } output, err := cdc.MarshalJSON(delegation) if err != nil { @@ -128,7 +133,12 @@ func ubdHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc { return } - ubd := types.UnmarshalUBD(cdc, key, res) + ubd, err := types.UnmarshalUBD(cdc, key, res) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(fmt.Sprintf("couldn't query unbonding-delegation. Error: %s", err.Error()))) + return + } output, err := cdc.MarshalJSON(ubd) if err != nil { @@ -187,7 +197,12 @@ func redHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc { return } - red := types.UnmarshalRED(cdc, key, res) + red, err := types.UnmarshalRED(cdc, key, res) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(fmt.Sprintf("couldn't query unbonding-delegation. Error: %s", err.Error()))) + return + } output, err := cdc.MarshalJSON(red) if err != nil { @@ -277,7 +292,12 @@ func validatorsHandlerFn(ctx context.CoreContext, cdc *wire.Codec) http.HandlerF for i, kv := range kvs { addr := kv.Key[1:] - validator := types.UnmarshalValidator(cdc, addr, kv.Value) + validator, err := types.UnmarshalValidator(cdc, addr, kv.Value) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(fmt.Sprintf("couldn't query unbonding-delegation. Error: %s", err.Error()))) + return + } bech32Validator, err := bech32StakeValidatorOutput(validator) if err != nil { diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index 3d949a9a0a..353154ce22 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -18,7 +18,7 @@ func (k Keeper) GetDelegation(ctx sdk.Context, return delegation, false } - delegation = types.UnmarshalDelegation(k.cdc, key, value) + delegation = types.MustUnmarshalDelegation(k.cdc, key, value) return delegation, true } @@ -32,7 +32,7 @@ func (k Keeper) GetAllDelegations(ctx sdk.Context) (delegations []types.Delegati if !iterator.Valid() { break } - delegation := types.UnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value()) + delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value()) delegations = append(delegations, delegation) iterator.Next() } @@ -54,7 +54,7 @@ func (k Keeper) GetDelegations(ctx sdk.Context, delegator sdk.Address, if !iterator.Valid() || i > int(maxRetrieve-1) { break } - delegation := types.UnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value()) + delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value()) delegations[i] = delegation iterator.Next() } @@ -65,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 := types.MarshalDelegation(k.cdc, delegation) + b := types.MustMarshalDelegation(k.cdc, delegation) store.Set(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr), b) } @@ -88,7 +88,7 @@ func (k Keeper) GetUnbondingDelegation(ctx sdk.Context, return ubd, false } - ubd = types.UnmarshalUBD(k.cdc, key, value) + ubd = types.MustUnmarshalUBD(k.cdc, key, value) return ubd, true } @@ -102,7 +102,7 @@ func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sd } key := GetUBDKeyFromValIndexKey(iterator.Key()) value := store.Get(key) - ubd := types.UnmarshalUBD(k.cdc, key, value) + ubd := types.MustUnmarshalUBD(k.cdc, key, value) ubds = append(ubds, ubd) iterator.Next() } @@ -113,7 +113,7 @@ func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sd // set the unbonding delegation and associated index func (k Keeper) SetUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { store := ctx.KVStore(k.storeKey) - bz := types.MarshalUBD(k.cdc, ubd) + bz := types.MustMarshalUBD(k.cdc, ubd) key := GetUBDKey(ubd.DelegatorAddr, ubd.ValidatorAddr) store.Set(key, bz) store.Set(GetUBDByValIndexKey(ubd.DelegatorAddr, ubd.ValidatorAddr), []byte{}) // index, store empty bytes @@ -140,7 +140,7 @@ func (k Keeper) GetRedelegation(ctx sdk.Context, return red, false } - red = types.UnmarshalRED(k.cdc, key, value) + red = types.MustUnmarshalRED(k.cdc, key, value) return red, true } @@ -154,7 +154,7 @@ func (k Keeper) GetRedelegationsFromValidator(ctx sdk.Context, valAddr sdk.Addre } key := GetREDKeyFromValSrcIndexKey(iterator.Key()) value := store.Get(key) - red := types.UnmarshalRED(k.cdc, key, value) + red := types.MustUnmarshalRED(k.cdc, key, value) reds = append(reds, red) iterator.Next() } @@ -182,7 +182,7 @@ func (k Keeper) HasReceivingRedelegation(ctx sdk.Context, // set a redelegation and associated index func (k Keeper) SetRedelegation(ctx sdk.Context, red types.Redelegation) { store := ctx.KVStore(k.storeKey) - bz := types.MarshalRED(k.cdc, red) + bz := types.MustMarshalRED(k.cdc, red) key := GetREDKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr) store.Set(key, bz) store.Set(GetREDByValSrcIndexKey(red.DelegatorAddr, red.ValidatorSrcAddr, red.ValidatorDstAddr), []byte{}) diff --git a/x/stake/keeper/key.go b/x/stake/keeper/key.go index cfe8ded82b..32c54f5196 100644 --- a/x/stake/keeper/key.go +++ b/x/stake/keeper/key.go @@ -131,11 +131,11 @@ func GetUBDByValIndexKey(delegatorAddr, validatorAddr sdk.Address) []byte { // rearrange the ValIndexKey to get the UBDKey func GetUBDKeyFromValIndexKey(IndexKey []byte) []byte { addrs := IndexKey[1:] // remove prefix bytes - if len(addrs) != 40 { + if len(addrs) != 2*sdk.AddrLen { panic("unexpected key length") } - valAddr := addrs[:20] - delAddr := addrs[20:] + valAddr := addrs[:sdk.AddrLen] + delAddr := addrs[sdk.AddrLen:] return GetUBDKey(delAddr, valAddr) } @@ -189,12 +189,12 @@ func GetREDByValDstIndexKey(delegatorAddr, validatorSrcAddr, // rearrange the ValSrcIndexKey to get the REDKey func GetREDKeyFromValSrcIndexKey(IndexKey []byte) []byte { addrs := IndexKey[1:] // remove prefix bytes - if len(addrs) != 60 { + if len(addrs) != 3*sdk.AddrLen { panic("unexpected key length") } - valSrcAddr := addrs[:20] - delAddr := addrs[20:40] - valDstAddr := addrs[40:] + valSrcAddr := addrs[:sdk.AddrLen] + delAddr := addrs[sdk.AddrLen : 2*sdk.AddrLen] + valDstAddr := addrs[2*sdk.AddrLen:] return GetREDKey(delAddr, valSrcAddr, valDstAddr) } @@ -202,12 +202,12 @@ func GetREDKeyFromValSrcIndexKey(IndexKey []byte) []byte { // rearrange the ValDstIndexKey to get the REDKey func GetREDKeyFromValDstIndexKey(IndexKey []byte) []byte { addrs := IndexKey[1:] // remove prefix bytes - if len(addrs) != 60 { + if len(addrs) != 3*sdk.AddrLen { panic("unexpected key length") } - valDstAddr := addrs[:20] - delAddr := addrs[20:40] - valSrcAddr := addrs[40:] + valDstAddr := addrs[:sdk.AddrLen] + delAddr := addrs[sdk.AddrLen : 2*sdk.AddrLen] + valSrcAddr := addrs[2*sdk.AddrLen:] return GetREDKey(delAddr, valSrcAddr, valDstAddr) } diff --git a/x/stake/keeper/sdk_types.go b/x/stake/keeper/sdk_types.go index 0fe465892c..b8be3e34f8 100644 --- a/x/stake/keeper/sdk_types.go +++ b/x/stake/keeper/sdk_types.go @@ -17,7 +17,7 @@ func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validato i := int64(0) for ; iterator.Valid(); iterator.Next() { addr := iterator.Key()[1:] - validator := types.UnmarshalValidator(k.cdc, addr, iterator.Value()) + validator := types.MustUnmarshalValidator(k.cdc, addr, iterator.Value()) stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to? if stop { break @@ -90,7 +90,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() { - delegation := types.UnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value()) + delegation := types.MustUnmarshalDelegation(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 diff --git a/x/stake/keeper/validator.go b/x/stake/keeper/validator.go index ad70958a3c..164c500492 100644 --- a/x/stake/keeper/validator.go +++ b/x/stake/keeper/validator.go @@ -18,8 +18,7 @@ func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.Address) (validator types if value == nil { return validator, false } - fmt.Printf("debug addr: %v\n", addr) - validator = types.UnmarshalValidator(k.cdc, addr, value) + validator = types.MustUnmarshalValidator(k.cdc, addr, value) return validator, true } @@ -36,7 +35,7 @@ func (k Keeper) GetValidatorByPubKey(ctx sdk.Context, pubkey crypto.PubKey) (val // set the main record holding validator details func (k Keeper) SetValidator(ctx sdk.Context, validator types.Validator) { store := ctx.KVStore(k.storeKey) - bz := types.MarshalValidator(k.cdc, validator) + bz := types.MustMarshalValidator(k.cdc, validator) store.Set(GetValidatorKey(validator.Owner), bz) } @@ -75,7 +74,7 @@ func (k Keeper) GetAllValidators(ctx sdk.Context) (validators []types.Validator) break } addr := iterator.Key()[1:] - validator := types.UnmarshalValidator(k.cdc, addr, iterator.Value()) + validator := types.MustUnmarshalValidator(k.cdc, addr, iterator.Value()) validators = append(validators, validator) iterator.Next() } @@ -95,7 +94,7 @@ func (k Keeper) GetValidators(ctx sdk.Context, maxRetrieve int16) (validators [] break } addr := iterator.Key()[1:] - validator := types.UnmarshalValidator(k.cdc, addr, iterator.Value()) + validator := types.MustUnmarshalValidator(k.cdc, addr, iterator.Value()) validators[i] = validator iterator.Next() } diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index 7b9d45c0d8..f7d31a6407 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -2,6 +2,7 @@ package types import ( "bytes" + "errors" "fmt" sdk "github.com/cosmos/cosmos-sdk/types" @@ -24,7 +25,7 @@ type delegationValue struct { } // return the delegation without fields contained within the key for the store -func MarshalDelegation(cdc *wire.Codec, delegation Delegation) []byte { +func MustMarshalDelegation(cdc *wire.Codec, delegation Delegation) []byte { val := delegationValue{ delegation.Shares, delegation.Height, @@ -33,23 +34,36 @@ func MarshalDelegation(cdc *wire.Codec, delegation Delegation) []byte { } // return the delegation without fields contained within the key for the store -func UnmarshalDelegation(cdc *wire.Codec, key, value []byte) Delegation { +func MustUnmarshalDelegation(cdc *wire.Codec, key, value []byte) Delegation { + delegation, err := UnmarshalDelegation(cdc, key, value) + if err != nil { + panic(err) + } + return delegation +} + +// return the delegation without fields contained within the key for the store +func UnmarshalDelegation(cdc *wire.Codec, key, value []byte) (delegation Delegation, err error) { var storeValue delegationValue - cdc.MustUnmarshalBinary(value, &storeValue) + err = cdc.UnmarshalBinary(value, &storeValue) + if err != nil { + return + } addrs := key[1:] // remove prefix bytes - if len(addrs) != 40 { - panic("unexpected key length") + if len(addrs) != 2*sdk.AddrLen { + err = errors.New("unexpected key length") + return } - delAddr := sdk.Address(addrs[:20]) - valAddr := sdk.Address(addrs[20:]) + delAddr := sdk.Address(addrs[:sdk.AddrLen]) + valAddr := sdk.Address(addrs[sdk.AddrLen:]) return Delegation{ DelegatorAddr: delAddr, ValidatorAddr: valAddr, Shares: storeValue.Shares, Height: storeValue.Height, - } + }, nil } // nolint @@ -109,7 +123,7 @@ type ubdValue struct { } // return the unbonding delegation without fields contained within the key for the store -func MarshalUBD(cdc *wire.Codec, ubd UnbondingDelegation) []byte { +func MustMarshalUBD(cdc *wire.Codec, ubd UnbondingDelegation) []byte { val := ubdValue{ ubd.CreationHeight, ubd.MinTime, @@ -120,16 +134,29 @@ func MarshalUBD(cdc *wire.Codec, ubd UnbondingDelegation) []byte { } // unmarshal a unbonding delegation from a store key and value -func UnmarshalUBD(cdc *wire.Codec, key, value []byte) UnbondingDelegation { +func MustUnmarshalUBD(cdc *wire.Codec, key, value []byte) UnbondingDelegation { + ubd, err := UnmarshalUBD(cdc, key, value) + if err != nil { + panic(err) + } + return ubd +} + +// unmarshal a unbonding delegation from a store key and value +func UnmarshalUBD(cdc *wire.Codec, key, value []byte) (ubd UnbondingDelegation, err error) { var storeValue ubdValue - cdc.MustUnmarshalBinary(value, &storeValue) + err = cdc.UnmarshalBinary(value, &storeValue) + if err != nil { + return + } addrs := key[1:] // remove prefix bytes - if len(addrs) != 40 { - panic("unexpected key length") + if len(addrs) != 2*sdk.AddrLen { + err = errors.New("unexpected key length") + return } - delAddr := sdk.Address(addrs[:20]) - valAddr := sdk.Address(addrs[20:]) + delAddr := sdk.Address(addrs[:sdk.AddrLen]) + valAddr := sdk.Address(addrs[sdk.AddrLen:]) return UnbondingDelegation{ DelegatorAddr: delAddr, @@ -138,7 +165,7 @@ func UnmarshalUBD(cdc *wire.Codec, key, value []byte) UnbondingDelegation { MinTime: storeValue.MinTime, InitialBalance: storeValue.InitialBalance, Balance: storeValue.Balance, - } + }, nil } // nolint @@ -196,7 +223,7 @@ type redValue struct { } // return the redelegation without fields contained within the key for the store -func MarshalRED(cdc *wire.Codec, red Redelegation) []byte { +func MustMarshalRED(cdc *wire.Codec, red Redelegation) []byte { val := redValue{ red.CreationHeight, red.MinTime, @@ -209,17 +236,30 @@ func MarshalRED(cdc *wire.Codec, red Redelegation) []byte { } // unmarshal a redelegation from a store key and value -func UnmarshalRED(cdc *wire.Codec, key, value []byte) Redelegation { +func MustUnmarshalRED(cdc *wire.Codec, key, value []byte) Redelegation { + red, err := UnmarshalRED(cdc, key, value) + if err != nil { + panic(err) + } + return red +} + +// unmarshal a redelegation from a store key and value +func UnmarshalRED(cdc *wire.Codec, key, value []byte) (red Redelegation, err error) { var storeValue redValue - cdc.MustUnmarshalBinary(value, &storeValue) + err = cdc.UnmarshalBinary(value, &storeValue) + if err != nil { + return + } addrs := key[1:] // remove prefix bytes - if len(addrs) != 60 { - panic("unexpected key length") + if len(addrs) != 3*sdk.AddrLen { + err = errors.New("unexpected key length") + return } - delAddr := sdk.Address(addrs[:20]) - valSrcAddr := sdk.Address(addrs[20:40]) - valDstAddr := sdk.Address(addrs[40:60]) + delAddr := sdk.Address(addrs[:sdk.AddrLen]) + valSrcAddr := sdk.Address(addrs[sdk.AddrLen : 2*sdk.AddrLen]) + valDstAddr := sdk.Address(addrs[2*sdk.AddrLen:]) return Redelegation{ DelegatorAddr: delAddr, @@ -231,7 +271,7 @@ func UnmarshalRED(cdc *wire.Codec, key, value []byte) Redelegation { Balance: storeValue.Balance, SharesSrc: storeValue.SharesSrc, SharesDst: storeValue.SharesDst, - } + }, nil } // nolint diff --git a/x/stake/types/validator.go b/x/stake/types/validator.go index 80ee70355c..480416d015 100644 --- a/x/stake/types/validator.go +++ b/x/stake/types/validator.go @@ -2,6 +2,7 @@ package types import ( "bytes" + "errors" "fmt" abci "github.com/tendermint/tendermint/abci/types" @@ -81,7 +82,7 @@ type validatorValue struct { } // return the redelegation without fields contained within the key for the store -func MarshalValidator(cdc *wire.Codec, validator Validator) []byte { +func MustMarshalValidator(cdc *wire.Codec, validator Validator) []byte { val := validatorValue{ PubKey: validator.PubKey, Revoked: validator.Revoked, @@ -101,12 +102,26 @@ func MarshalValidator(cdc *wire.Codec, validator Validator) []byte { } // unmarshal a redelegation from a store key and value -func UnmarshalValidator(cdc *wire.Codec, ownerAddr, value []byte) Validator { +func MustUnmarshalValidator(cdc *wire.Codec, ownerAddr, value []byte) Validator { + validator, err := UnmarshalValidator(cdc, ownerAddr, value) + if err != nil { + panic(err) + } + + return validator +} + +// unmarshal a redelegation from a store key and value +func UnmarshalValidator(cdc *wire.Codec, ownerAddr, value []byte) (validator Validator, err error) { var storeValue validatorValue - cdc.MustUnmarshalBinary(value, &storeValue) + err = cdc.UnmarshalBinary(value, &storeValue) + if err != nil { + return + } if len(ownerAddr) != 20 { - panic("unexpected address length") + err = errors.New("unexpected address length") + return } return Validator{ @@ -124,7 +139,7 @@ func UnmarshalValidator(cdc *wire.Codec, ownerAddr, value []byte) Validator { CommissionChangeRate: storeValue.CommissionChangeRate, CommissionChangeToday: storeValue.CommissionChangeToday, PrevBondedShares: storeValue.PrevBondedShares, - } + }, nil } // only the vitals - does not check bond height of IntraTxCounter