Skip to content

Commit

Permalink
index keys
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski committed Jun 5, 2018
1 parent d69d179 commit 0efb0a4
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 46 deletions.
8 changes: 4 additions & 4 deletions x/stake/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@ func handleMsgEditValidator(ctx sdk.Context, msg types.MsgEditValidator, k keepe
}

// replace all editable fields (clients should autofill existing values)
d, err := validator.Description.UpdateDescription(msg.Description)
description, err := validator.Description.UpdateDescription(msg.Description)
if err != nil {
return err.Result()
}
validator.Description = d
validator.Description = description

k.UpdateValidator(ctx, validator)
tags := sdk.NewTags(
"action", []byte("editValidator"),
"validator", msg.ValidatorAddr.Bytes(),
"moniker", []byte(msg.Description.Moniker),
"identity", []byte(msg.Description.Identity),
"moniker", []byte(description.Moniker),
"identity", []byte(description.Identity),
)
return sdk.Result{
Tags: tags,
Expand Down
29 changes: 28 additions & 1 deletion x/stake/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,33 @@ func (k PrivlegedKeeper) Delegate(ctx sdk.Context, delegatorAddr sdk.Address,
k.SetPool(ctx, pool)
k.SetDelegation(ctx, bond)
k.UpdateValidator(ctx, validator)
tags := sdk.NewTags("action", []byte("delegate"), "delegator", delegatorAddr.Bytes(), "validator", validator.Owner.Bytes())
tags := sdk.NewTags(
"action", []byte("delegate"),
"delegator", delegatorAddr.Bytes(),
"validator", validator.Owner.Bytes(),
)
return tags, nil
}

//_____________________________________________________________________________________

// load a delegator bond
func (k Keeper) GetUnbondingDelegation(ctx sdk.Context,
delegationKey []byte) (bond types.UnbondingDelegation, found bool) {

store := ctx.KVStore(k.storeKey)
delegatorBytes := store.Get(delegationKey)
if delegatorBytes == nil {
return bond, false
}

k.cdc.MustUnmarshalBinary(delegatorBytes, &bond)
return bond, true
}

// set the delegation
func (k PrivlegedKeeper) SetUnbondingDelegation(ctx sdk.Context, bond types.UnbondingDelegation) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinary(bond)
store.Set(GetDelegationKey(bond.DelegatorAddr, bond.ValidatorAddr, k.cdc), b)
}
2 changes: 1 addition & 1 deletion x/stake/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (k PrivlegedKeeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
k.SetValidatorByPubKeyIndex(ctx, validator)
k.SetValidatorByPowerIndex(ctx, validator, data.Pool)
if validator.Status() == sdk.Bonded {
store.Set(GetValidatorsBondedKey(validator.PubKey), validator.Owner)
store.Set(GetValidatorsBondedIndexKey(validator.PubKey), validator.Owner)
}
}
for _, bond := range data.Bonds {
Expand Down
26 changes: 16 additions & 10 deletions x/stake/keeper/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ var (
PoolKey = []byte{0x01} // key for the staking pools
ValidatorsKey = []byte{0x02} // prefix for each key to a validator
ValidatorsByPubKeyIndexKey = []byte{0x03} // prefix for each key to a validator by pubkey
ValidatorsBondedKey = []byte{0x04} // prefix for each key to bonded/actively validating validators
ValidatorsByPowerKey = []byte{0x05} // prefix for each key to a validator sorted by power
ValidatorCliffKey = []byte{0x06} // key for block-local tx index
ValidatorsBondedIndexKey = []byte{0x04} // prefix for each key to bonded/actively validating validators
ValidatorsByPowerIndexKey = []byte{0x05} // prefix for each key to a validator sorted by power
ValidatorCliffIndexKey = []byte{0x06} // key for block-local tx index
ValidatorPowerCliffKey = []byte{0x07} // key for block-local tx index
TendermintUpdatesKey = []byte{0x08} // prefix for each key to a validator which is being updated
DelegationKey = []byte{0x09} // prefix for each key to a delegator's bond
Expand All @@ -41,13 +41,18 @@ func GetValidatorByPubKeyIndexKey(pubkey crypto.PubKey) []byte {
}

// get the key for the current validator group, ordered like tendermint
func GetValidatorsBondedKey(pk crypto.PubKey) []byte {
func GetValidatorsBondedIndexKey(pk crypto.PubKey) []byte {
addr := pk.Address()
return append(ValidatorsBondedKey, addr.Bytes()...)
return append(ValidatorsBondedIndexKey, addr.Bytes()...)
}

// get the key for the validator used in the power-store
func GetValidatorsByPowerKey(validator types.Validator, pool types.Pool) []byte {
// get the power which is the key for the validator used in the power-store
func GetValidatorsByPowerIndexKey(validator types.Validator, pool types.Pool) []byte {
return GetValidatorsByPower(validator, pool)
}

// get the power of a validator
func GetValidatorsByPower(validator types.Validator, pool types.Pool) []byte {

power := validator.EquivalentBondedShares(pool)
powerBytes := []byte(power.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first)
Expand All @@ -58,10 +63,11 @@ func GetValidatorsByPowerKey(validator types.Validator, pool types.Pool) []byte
binary.BigEndian.PutUint64(heightBytes, ^uint64(validator.BondHeight)) // invert height (older validators first)
counterBytes := make([]byte, 2)
binary.BigEndian.PutUint16(counterBytes, ^uint16(validator.BondIntraTxCounter)) // invert counter (first txns have priority)
return append(ValidatorsByPowerKey,

// NOTE the address doesn't need to be stored because counter bytes must always be different
return append(ValidatorsByPowerIndexKey,
append(powerBytes,
append(heightBytes,
append(counterBytes, validator.Owner.Bytes()...)...)...)...) // TODO don't technically need to store owner
append(heightBytes, counterBytes...)...)...)
}

// get the key for the accumulated update validators
Expand Down
2 changes: 1 addition & 1 deletion x/stake/keeper/sdk_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validato
// iterate through the active validator set and perform the provided function
func (k Keeper) IterateValidatorsBonded(ctx sdk.Context, fn func(index int64, validator sdk.Validator) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey)
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedIndexKey)
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
address := iterator.Value()
Expand Down
38 changes: 19 additions & 19 deletions x/stake/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (k PrivlegedKeeper) SetValidatorByPubKeyIndex(ctx sdk.Context, validator ty
// validator index
func (k PrivlegedKeeper) SetValidatorByPowerIndex(ctx sdk.Context, validator types.Validator, pool types.Pool) {
store := ctx.KVStore(k.storeKey)
store.Set(GetValidatorsByPowerKey(validator, pool), validator.Owner)
store.Set(GetValidatorsByPowerIndexKey(validator, pool), validator.Owner)
}

// Get the set of all validators with no limits, used during genesis dump
Expand Down Expand Up @@ -107,7 +107,7 @@ func (k Keeper) GetValidatorsBonded(ctx sdk.Context) (validators []types.Validat
maxValidators := k.GetParams(ctx).MaxValidators
validators = make([]types.Validator, maxValidators)

iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey)
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedIndexKey)
i := 0
for ; iterator.Valid(); iterator.Next() {

Expand All @@ -133,7 +133,7 @@ func (k Keeper) GetValidatorsByPower(ctx sdk.Context) []types.Validator {
store := ctx.KVStore(k.storeKey)
maxValidators := k.GetParams(ctx).MaxValidators
validators := make([]types.Validator, maxValidators)
iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest
iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerIndexKey) // largest to smallest
i := 0
for {
if !iterator.Valid() || i > int(maxValidators-1) {
Expand Down Expand Up @@ -230,9 +230,9 @@ func (k PrivlegedKeeper) UpdateValidator(ctx sdk.Context, validator types.Valida

// update the list ordered by voting power
if oldFound {
store.Delete(GetValidatorsByPowerKey(oldValidator, pool))
store.Delete(GetValidatorsByPowerIndexKey(oldValidator, pool))
}
valPower := GetValidatorsByPowerKey(validator, pool)
valPower := GetValidatorsByPowerIndexKey(validator, pool)
store.Set(valPower, validator.Owner)

// efficiency case:
Expand Down Expand Up @@ -266,9 +266,9 @@ func (k PrivlegedKeeper) UpdateValidator(ctx sdk.Context, validator types.Valida
// updatedValidatorAddr term.
//
// The correct subset is retrieved by iterating through an index of the
// validators sorted by power, stored using the ValidatorsByPowerKey.
// validators sorted by power, stored using the ValidatorsByPowerIndexKey.
// Simultaneously the current validator records are updated in store with the
// ValidatorsBondedKey. This store is used to determine if a validator is a
// ValidatorsBondedIndexKey. This store is used to determine if a validator is a
// validator without needing to iterate over the subspace as we do in
// GetValidators.
//
Expand All @@ -281,7 +281,7 @@ func (k PrivlegedKeeper) UpdateBondedValidators(ctx sdk.Context, store sdk.KVSto

// add the actual validator power sorted store
maxValidators := k.GetParams(ctx).MaxValidators
iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest
iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerIndexKey) // largest to smallest
bondedValidatorsCount := 0
var validator types.Validator
for {
Expand Down Expand Up @@ -346,7 +346,7 @@ func (k PrivlegedKeeper) UpdateBondedValidators(ctx sdk.Context, store sdk.KVSto
func (k PrivlegedKeeper) UpdateBondedValidatorsFull(ctx sdk.Context, store sdk.KVStore) {
// clear the current validators store, add to the ToKickOut temp store
toKickOut := make(map[string]byte)
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedKey)
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedIndexKey)
for ; iterator.Valid(); iterator.Next() {
ownerAddr := iterator.Value()
toKickOut[string(ownerAddr)] = 0 // set anything
Expand All @@ -355,7 +355,7 @@ func (k PrivlegedKeeper) UpdateBondedValidatorsFull(ctx sdk.Context, store sdk.K

// add the actual validator power sorted store
maxValidators := k.GetParams(ctx).MaxValidators
iterator = sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerKey) // largest to smallest
iterator = sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerIndexKey) // largest to smallest
bondedValidatorsCount := 0
var validator types.Validator
for {
Expand Down Expand Up @@ -433,7 +433,7 @@ func (k PrivlegedKeeper) unbondValidator(ctx sdk.Context, store sdk.KVStore, val
store.Set(GetTendermintUpdatesKey(validator.Owner), bzABCI)

// also remove from the Bonded types.Validators Store
store.Delete(GetValidatorsBondedKey(validator.PubKey))
store.Delete(GetValidatorsBondedIndexKey(validator.PubKey))
return validator
}

Expand All @@ -453,7 +453,7 @@ func (k PrivlegedKeeper) bondValidator(ctx sdk.Context, store sdk.KVStore, valid
// save the now bonded validator record to the three referenced stores
bzVal := k.cdc.MustMarshalBinary(validator)
store.Set(GetValidatorKey(validator.Owner), bzVal)
store.Set(GetValidatorsBondedKey(validator.PubKey), validator.Owner)
store.Set(GetValidatorsBondedIndexKey(validator.PubKey), validator.Owner)

// add to accumulated changes for tendermint
bzABCI := k.cdc.MustMarshalBinary(validator.ABCIValidator(k.cdc))
Expand All @@ -476,14 +476,14 @@ func (k PrivlegedKeeper) RemoveValidator(ctx sdk.Context, address sdk.Address) {
pool := k.getPool(store)
store.Delete(GetValidatorKey(address))
store.Delete(GetValidatorByPubKeyIndexKey(validator.PubKey))
store.Delete(GetValidatorsByPowerKey(validator, pool))
store.Delete(GetValidatorsByPowerIndexKey(validator, pool))

// delete from the current and power weighted validator groups if the validator
// is bonded - and add validator with zero power to the validator updates
if store.Get(GetValidatorsBondedKey(validator.PubKey)) == nil {
if store.Get(GetValidatorsBondedIndexKey(validator.PubKey)) == nil {
return
}
store.Delete(GetValidatorsBondedKey(validator.PubKey))
store.Delete(GetValidatorsBondedIndexKey(validator.PubKey))

bz := k.cdc.MustMarshalBinary(validator.ABCIValidatorZero(k.cdc))
store.Set(GetTendermintUpdatesKey(address), bz)
Expand All @@ -494,7 +494,7 @@ func (k PrivlegedKeeper) RemoveValidator(ctx sdk.Context, address sdk.Address) {
// get the current validator on the cliff
func (k Keeper) getCliffValidator(ctx sdk.Context) []byte {
store := ctx.KVStore(k.storeKey)
return store.Get(ValidatorCliffKey)
return store.Get(ValidatorCliffIndexKey)
}

// get the current power of the validator on the cliff
Expand All @@ -506,14 +506,14 @@ func (k Keeper) getCliffValidatorPower(ctx sdk.Context) []byte {
// set the current validator and power of the validator on the cliff
func (k PrivlegedKeeper) setCliffValidator(ctx sdk.Context, validator types.Validator, pool types.Pool) {
store := ctx.KVStore(k.storeKey)
bz := GetValidatorsByPowerKey(validator, pool)
bz := GetValidatorsByPowerIndexKey(validator, pool)
store.Set(ValidatorPowerCliffKey, bz)
store.Set(ValidatorCliffKey, validator.Owner)
store.Set(ValidatorCliffIndexKey, validator.Owner)
}

// clear the current validator and power of the validator on the cliff
func (k PrivlegedKeeper) clearCliffValidator(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
store.Delete(ValidatorPowerCliffKey)
store.Delete(ValidatorCliffKey)
store.Delete(ValidatorCliffIndexKey)
}
10 changes: 5 additions & 5 deletions x/stake/types.go → x/stake/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ var (
NewPrivlegedKeeper = keeper.NewPrivlegedKeeper
GetValidatorKey = keeper.GetValidatorKey
GetValidatorByPubKeyIndexKey = keeper.GetValidatorByPubKeyIndexKey
GetValidatorsBondedKey = keeper.GetValidatorsBondedKey
GetValidatorsByPowerKey = keeper.GetValidatorsByPowerKey
GetValidatorsBondedIndexKey = keeper.GetValidatorsBondedIndexKey
GetValidatorsByPowerIndexKey = keeper.GetValidatorsByPowerIndexKey
GetTendermintUpdatesKey = keeper.GetTendermintUpdatesKey
GetDelegationKey = keeper.GetDelegationKey
GetDelegationsKey = keeper.GetDelegationsKey
ParamKey = keeper.ParamKey
PoolKey = keeper.PoolKey
ValidatorsKey = keeper.ValidatorsKey
ValidatorsByPubKeyIndexKey = keeper.ValidatorsByPubKeyIndexKey
ValidatorsBondedKey = keeper.ValidatorsBondedKey
ValidatorsByPowerKey = keeper.ValidatorsByPowerKey
ValidatorCliffKey = keeper.ValidatorCliffKey
ValidatorsBondedIndexKey = keeper.ValidatorsBondedIndexKey
ValidatorsByPowerIndexKey = keeper.ValidatorsByPowerIndexKey
ValidatorCliffIndexKey = keeper.ValidatorCliffIndexKey
ValidatorPowerCliffKey = keeper.ValidatorPowerCliffKey
TendermintUpdatesKey = keeper.TendermintUpdatesKey
DelegationKey = keeper.DelegationKey
Expand Down
11 changes: 6 additions & 5 deletions x/stake/types/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ func (b Delegation) HumanReadableString() (string, error) {

// element stored to represent the passive unbonding queue
type UnbondingDelegation struct {
DelegationKey sdk.Address // key of the delegation
InitTime int64 // unix time at unbonding initation
InitHeight int64 // block height at unbonding initation
ExpectedTokens sdk.Coins // the value in Atoms of the amount of shares which are unbonding
StartSlashRatio sdk.Rat // validator slash ratio at unbonding initiation
DelegatorAddr sdk.Address `json:"delegator_addr"` // delegator
ValidatorAddr sdk.Address `json:"validator_addr"` // validator unbonding from owner addr
InitTime int64 `json:"init_time"` // unix time at unbonding initation
InitHeight int64 `json:"init_height"` // block height at unbonding initation
ExpectedTokens sdk.Coins `json:"expected_tokens"` // the value in Atoms of the amount of shares which are unbonding
StartSlashRatio sdk.Rat `json:"start_slash_ratio"` // validator slash ratio at unbonding initiation
}

//__________________________________________________________________
Expand Down

0 comments on commit 0efb0a4

Please sign in to comment.