cosmos-sdk/x/stake/keeper_keys.go

82 lines
3.1 KiB
Go
Raw Normal View History

2018-03-22 09:00:45 -07:00
package stake
import (
"encoding/binary"
2018-03-22 09:00:45 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
crypto "github.com/tendermint/go-crypto"
2018-03-22 09:00:45 -07:00
)
2018-03-22 14:47:57 -07:00
// TODO remove some of these prefixes once have working multistore
2018-03-22 09:00:45 -07:00
//nolint
var (
// Keys for store prefixes
ParamKey = []byte{0x00} // key for global parameters relating to staking
PoolKey = []byte{0x01} // key for global parameters relating to staking
ValidatorsKey = []byte{0x02} // prefix for each key to a validator
2018-05-17 07:35:51 -07:00
ValidatorsByPowerKey = []byte{0x03} // prefix for each key to a validator sorted by power
ValidatorsBondedKey = []byte{0x04} // prefix for each key to bonded/actively validating validators
TendermintUpdatesKey = []byte{0x05} // prefix for each key to a validator which is being updated
DelegationKey = []byte{0x06} // prefix for each key to a delegator's bond
IntraTxCounterKey = []byte{0x07} // key for block-local tx index
2018-03-22 09:00:45 -07:00
)
const maxDigitsForAccount = 12 // ~220,000,000 atoms created at launch
2018-05-09 21:01:58 -07:00
// get the key for the validator with address
func GetValidatorKey(addr sdk.Address) []byte {
return append(ValidatorsKey, addr.Bytes()...)
2018-03-22 09:00:45 -07:00
}
2018-03-22 14:47:57 -07:00
// get the key for the validator used in the power-store
func GetValidatorsBondedByPowerKey(validator Validator, pool Pool) []byte {
power := validator.EquivalentBondedShares(pool)
powerBytes := []byte(power.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first)
2018-05-04 16:15:24 -07:00
// TODO ensure that the key will be a readable string.. probably should add seperators and have
// heightBytes and counterBytes represent strings like powerBytes does
2018-04-09 04:12:08 -07:00
heightBytes := make([]byte, binary.MaxVarintLen64)
2018-05-10 16:02:35 -07:00
binary.BigEndian.PutUint64(heightBytes, ^uint64(validator.BondHeight)) // invert height (older validators first)
counterBytes := make([]byte, 2)
2018-05-10 16:02:35 -07:00
binary.BigEndian.PutUint16(counterBytes, ^uint16(validator.BondIntraTxCounter)) // invert counter (first txns have priority)
2018-05-09 20:28:00 -07:00
return append(ValidatorsByPowerKey,
2018-05-04 16:15:24 -07:00
append(powerBytes,
append(heightBytes,
append(counterBytes, validator.Address.Bytes()...)...)...)...)
2018-03-22 14:47:57 -07:00
}
// get the key for the accumulated update validators
2018-05-10 18:38:57 -07:00
func GetTendermintUpdatesKey(addr sdk.Address) []byte {
return append(TendermintUpdatesKey, addr.Bytes()...)
2018-03-22 09:00:45 -07:00
}
2018-05-09 18:39:14 -07:00
// get the key for the current validator group, ordered like tendermint
2018-05-06 15:18:52 -07:00
func GetValidatorsBondedKey(pk crypto.PubKey) []byte {
addr := pk.Address()
2018-05-09 20:28:00 -07:00
return append(ValidatorsBondedKey, addr.Bytes()...)
2018-04-02 11:37:35 -07:00
}
2018-05-09 21:01:58 -07:00
// get the key for delegator bond with validator
func GetDelegationKey(delegatorAddr, validatorAddr sdk.Address, cdc *wire.Codec) []byte {
return append(GetDelegationsKey(delegatorAddr, cdc), validatorAddr.Bytes()...)
2018-03-22 09:00:45 -07:00
}
2018-05-09 21:01:58 -07:00
// get the prefix for a delegator for all validators
2018-05-09 18:39:14 -07:00
func GetDelegationsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte {
2018-03-22 09:00:45 -07:00
res, err := cdc.MarshalBinary(&delegatorAddr)
if err != nil {
panic(err)
}
2018-05-09 20:28:00 -07:00
return append(DelegationKey, res...)
2018-03-22 09:00:45 -07:00
}
2018-05-03 23:00:30 -07:00
2018-05-09 18:39:14 -07:00
//______________________________________________________________
// remove the prefix byte from a key, possibly revealing and address
func AddrFromKey(key []byte) sdk.Address {
return key[1:]
2018-05-03 23:00:30 -07:00
}