2018-06-26 19:00:12 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO remove some of these prefixes once have working multistore
|
|
|
|
|
|
|
|
//nolint
|
|
|
|
var (
|
|
|
|
// Keys for store prefixes
|
|
|
|
ParamKey = []byte{0x00} // key for parameters relating to staking
|
|
|
|
PoolKey = []byte{0x01} // key for the staking pools
|
|
|
|
ValidatorsKey = []byte{0x02} // prefix for each key to a validator
|
2018-09-24 21:09:31 -07:00
|
|
|
ValidatorsByConsAddrKey = []byte{0x03} // prefix for each key to a validator index, by pubkey
|
2018-06-26 19:00:12 -07:00
|
|
|
ValidatorsBondedIndexKey = []byte{0x04} // prefix for each key to a validator index, for bonded validators
|
|
|
|
ValidatorsByPowerIndexKey = []byte{0x05} // prefix for each key to a validator index, sorted by power
|
2018-10-03 09:37:06 -07:00
|
|
|
IntraTxCounterKey = []byte{0x06} // key for intra-block tx index
|
|
|
|
DelegationKey = []byte{0x07} // key for a delegation
|
|
|
|
UnbondingDelegationKey = []byte{0x08} // key for an unbonding-delegation
|
|
|
|
UnbondingDelegationByValIndexKey = []byte{0x09} // prefix for each key for an unbonding-delegation, by validator operator
|
|
|
|
RedelegationKey = []byte{0x0A} // key for a redelegation
|
|
|
|
RedelegationByValSrcIndexKey = []byte{0x0B} // prefix for each key for an redelegation, by source validator operator
|
|
|
|
RedelegationByValDstIndexKey = []byte{0x0C} // prefix for each key for an redelegation, by destination validator operator
|
2018-06-26 19:00:12 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const maxDigitsForAccount = 12 // ~220,000,000 atoms created at launch
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the key for the validator with address
|
2018-07-03 16:15:48 -07:00
|
|
|
// VALUE: stake/types.Validator
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetValidatorKey(operatorAddr sdk.ValAddress) []byte {
|
|
|
|
return append(ValidatorsKey, operatorAddr.Bytes()...)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the key for the validator with pubkey
|
2018-09-08 01:44:58 -07:00
|
|
|
// VALUE: validator operator address ([]byte)
|
2018-09-24 21:09:31 -07:00
|
|
|
func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte {
|
|
|
|
return append(ValidatorsByConsAddrKey, addr.Bytes()...)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-09-08 01:44:58 -07:00
|
|
|
// Get the validator operator address from ValBondedIndexKey
|
2018-07-03 16:15:48 -07:00
|
|
|
func GetAddressFromValBondedIndexKey(IndexKey []byte) []byte {
|
|
|
|
return IndexKey[1:] // remove prefix bytes
|
|
|
|
}
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// 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.
|
2018-09-08 01:44:58 -07:00
|
|
|
// VALUE: validator operator address ([]byte)
|
2018-07-03 16:15:48 -07:00
|
|
|
func GetValidatorsByPowerIndexKey(validator types.Validator, pool types.Pool) []byte {
|
2018-06-26 19:00:12 -07:00
|
|
|
// NOTE the address doesn't need to be stored because counter bytes must always be different
|
2018-10-03 23:42:03 -07:00
|
|
|
return getValidatorPowerRank(validator)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// get the bonded validator index key for an operator address
|
|
|
|
func GetBondedValidatorIndexKey(operator sdk.ValAddress) []byte {
|
|
|
|
return append(ValidatorsBondedIndexKey, operator...)
|
|
|
|
}
|
|
|
|
|
2018-07-03 16:15:48 -07:00
|
|
|
// get the power ranking of a validator
|
2018-07-06 16:53:07 -07:00
|
|
|
// NOTE the larger values are of higher value
|
2018-08-31 15:22:37 -07:00
|
|
|
// nolint: unparam
|
2018-10-03 23:42:03 -07:00
|
|
|
func getValidatorPowerRank(validator types.Validator) []byte {
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-07-13 13:46:14 -07:00
|
|
|
potentialPower := validator.Tokens
|
|
|
|
powerBytes := []byte(potentialPower.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first)
|
2018-10-03 23:42:03 -07:00
|
|
|
powerBytesLen := len(powerBytes)
|
|
|
|
// key is of format prefix || powerbytes || heightBytes || counterBytes
|
|
|
|
key := make([]byte, 1+powerBytesLen+8+2)
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-10-03 23:42:03 -07:00
|
|
|
key[0] = ValidatorsByPowerIndexKey[0]
|
|
|
|
copy(key[1:powerBytesLen+1], powerBytes)
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-10-03 23:42:03 -07:00
|
|
|
// include heightBytes height is inverted (older validators first)
|
|
|
|
binary.BigEndian.PutUint64(key[powerBytesLen+1:powerBytesLen+9], ^uint64(validator.BondHeight))
|
|
|
|
// include counterBytes, counter is inverted (first txns have priority)
|
|
|
|
binary.BigEndian.PutUint16(key[powerBytesLen+9:powerBytesLen+11], ^uint16(validator.BondIntraTxCounter))
|
|
|
|
|
|
|
|
return key
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
//______________________________________________________________________________
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the key for delegator bond with validator
|
2018-07-03 16:15:48 -07:00
|
|
|
// VALUE: stake/types.Delegation
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetDelegationKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte {
|
|
|
|
return append(GetDelegationsKey(delAddr), valAddr.Bytes()...)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the prefix for a delegator for all validators
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetDelegationsKey(delAddr sdk.AccAddress) []byte {
|
|
|
|
return append(DelegationKey, delAddr.Bytes()...)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
//______________________________________________________________________________
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the key for an unbonding delegation by delegator and validator addr
|
2018-07-03 16:15:48 -07:00
|
|
|
// VALUE: stake/types.UnbondingDelegation
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetUBDKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte {
|
2018-07-03 16:15:48 -07:00
|
|
|
return append(
|
2018-08-30 21:06:44 -07:00
|
|
|
GetUBDsKey(delAddr.Bytes()),
|
|
|
|
valAddr.Bytes()...)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the index-key for an unbonding delegation, stored by validator-index
|
2018-07-03 16:15:48 -07:00
|
|
|
// VALUE: none (key rearrangement used)
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte {
|
|
|
|
return append(GetUBDsByValIndexKey(valAddr), delAddr.Bytes()...)
|
2018-07-03 16:15:48 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// rearranges the ValIndexKey to get the UBDKey
|
2018-07-03 16:15:48 -07:00
|
|
|
func GetUBDKeyFromValIndexKey(IndexKey []byte) []byte {
|
|
|
|
addrs := IndexKey[1:] // remove prefix bytes
|
2018-07-04 14:07:06 -07:00
|
|
|
if len(addrs) != 2*sdk.AddrLen {
|
2018-07-03 16:15:48 -07:00
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
2018-07-04 14:07:06 -07:00
|
|
|
valAddr := addrs[:sdk.AddrLen]
|
|
|
|
delAddr := addrs[sdk.AddrLen:]
|
2018-07-03 16:15:48 -07:00
|
|
|
return GetUBDKey(delAddr, valAddr)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//______________
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the prefix for all unbonding delegations from a delegator
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetUBDsKey(delAddr sdk.AccAddress) []byte {
|
|
|
|
return append(UnbondingDelegationKey, delAddr.Bytes()...)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the prefix keyspace for the indexes of unbonding delegations for a validator
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetUBDsByValIndexKey(valAddr sdk.ValAddress) []byte {
|
|
|
|
return append(UnbondingDelegationByValIndexKey, valAddr.Bytes()...)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//________________________________________________________________________________
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the key for a redelegation
|
2018-07-03 16:15:48 -07:00
|
|
|
// VALUE: stake/types.RedelegationKey
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetREDKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte {
|
2018-10-03 23:42:03 -07:00
|
|
|
key := make([]byte, 1+sdk.AddrLen*3)
|
|
|
|
|
|
|
|
copy(key[0:sdk.AddrLen+1], GetREDsKey(delAddr.Bytes()))
|
|
|
|
copy(key[sdk.AddrLen+1:2*sdk.AddrLen+1], valSrcAddr.Bytes())
|
|
|
|
copy(key[2*sdk.AddrLen+1:3*sdk.AddrLen+1], valDstAddr.Bytes())
|
|
|
|
|
|
|
|
return key
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the index-key for a redelegation, stored by source-validator-index
|
2018-07-03 16:15:48 -07:00
|
|
|
// VALUE: none (key rearrangement used)
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetREDByValSrcIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte {
|
2018-10-03 23:42:03 -07:00
|
|
|
REDSFromValsSrcKey := GetREDsFromValSrcIndexKey(valSrcAddr)
|
|
|
|
offset := len(REDSFromValsSrcKey)
|
|
|
|
|
|
|
|
// key is of the form REDSFromValsSrcKey || delAddr || valDstAddr
|
|
|
|
key := make([]byte, len(REDSFromValsSrcKey)+2*sdk.AddrLen)
|
|
|
|
copy(key[0:offset], REDSFromValsSrcKey)
|
|
|
|
copy(key[offset:offset+sdk.AddrLen], delAddr.Bytes())
|
|
|
|
copy(key[offset+sdk.AddrLen:offset+2*sdk.AddrLen], valDstAddr.Bytes())
|
|
|
|
return key
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the index-key for a redelegation, stored by destination-validator-index
|
2018-07-03 16:15:48 -07:00
|
|
|
// VALUE: none (key rearrangement used)
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte {
|
2018-10-03 23:42:03 -07:00
|
|
|
REDSToValsDstKey := GetREDsToValDstIndexKey(valDstAddr)
|
|
|
|
offset := len(REDSToValsDstKey)
|
|
|
|
|
|
|
|
// key is of the form REDSToValsDstKey || delAddr || valSrcAddr
|
|
|
|
key := make([]byte, len(REDSToValsDstKey)+2*sdk.AddrLen)
|
|
|
|
copy(key[0:offset], REDSToValsDstKey)
|
|
|
|
copy(key[offset:offset+sdk.AddrLen], delAddr.Bytes())
|
|
|
|
copy(key[offset+sdk.AddrLen:offset+2*sdk.AddrLen], valSrcAddr.Bytes())
|
|
|
|
|
|
|
|
return key
|
2018-07-03 16:15:48 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// rearranges the ValSrcIndexKey to get the REDKey
|
2018-07-03 16:15:48 -07:00
|
|
|
func GetREDKeyFromValSrcIndexKey(IndexKey []byte) []byte {
|
|
|
|
addrs := IndexKey[1:] // remove prefix bytes
|
2018-07-04 14:07:06 -07:00
|
|
|
if len(addrs) != 3*sdk.AddrLen {
|
2018-07-03 16:15:48 -07:00
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
2018-07-04 14:07:06 -07:00
|
|
|
valSrcAddr := addrs[:sdk.AddrLen]
|
|
|
|
delAddr := addrs[sdk.AddrLen : 2*sdk.AddrLen]
|
|
|
|
valDstAddr := addrs[2*sdk.AddrLen:]
|
2018-07-03 16:15:48 -07:00
|
|
|
|
|
|
|
return GetREDKey(delAddr, valSrcAddr, valDstAddr)
|
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// rearranges the ValDstIndexKey to get the REDKey
|
2018-07-03 16:15:48 -07:00
|
|
|
func GetREDKeyFromValDstIndexKey(IndexKey []byte) []byte {
|
|
|
|
addrs := IndexKey[1:] // remove prefix bytes
|
2018-07-04 14:07:06 -07:00
|
|
|
if len(addrs) != 3*sdk.AddrLen {
|
2018-07-03 16:15:48 -07:00
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
2018-07-04 14:07:06 -07:00
|
|
|
valDstAddr := addrs[:sdk.AddrLen]
|
|
|
|
delAddr := addrs[sdk.AddrLen : 2*sdk.AddrLen]
|
|
|
|
valSrcAddr := addrs[2*sdk.AddrLen:]
|
2018-07-03 16:15:48 -07:00
|
|
|
return GetREDKey(delAddr, valSrcAddr, valDstAddr)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//______________
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the prefix keyspace for redelegations from a delegator
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetREDsKey(delAddr sdk.AccAddress) []byte {
|
|
|
|
return append(RedelegationKey, delAddr.Bytes()...)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the prefix keyspace for all redelegations redelegating away from a source validator
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetREDsFromValSrcIndexKey(valSrcAddr sdk.ValAddress) []byte {
|
|
|
|
return append(RedelegationByValSrcIndexKey, valSrcAddr.Bytes()...)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the prefix keyspace for all redelegations redelegating towards a destination validator
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetREDsToValDstIndexKey(valDstAddr sdk.ValAddress) []byte {
|
|
|
|
return append(RedelegationByValDstIndexKey, valDstAddr.Bytes()...)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// gets the prefix keyspace for all redelegations redelegating towards a destination validator
|
2018-06-26 19:00:12 -07:00
|
|
|
// from a particular delegator
|
2018-08-30 21:06:44 -07:00
|
|
|
func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddress) []byte {
|
2018-06-26 19:00:12 -07:00
|
|
|
return append(
|
2018-08-30 21:06:44 -07:00
|
|
|
GetREDsToValDstIndexKey(valDstAddr),
|
|
|
|
delAddr.Bytes()...)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|