2018-03-22 09:00:45 -07:00
|
|
|
package stake
|
|
|
|
|
|
|
|
import (
|
2018-04-06 04:11:26 -07:00
|
|
|
"encoding/binary"
|
|
|
|
|
2018-03-22 09:00:45 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
2018-05-03 10:38:02 -07:00
|
|
|
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
|
2018-03-22 14:47:57 -07:00
|
|
|
ParamKey = []byte{0x00} // key for global parameters relating to staking
|
|
|
|
PoolKey = []byte{0x01} // key for global parameters relating to staking
|
|
|
|
CandidatesKey = []byte{0x02} // prefix for each key to a candidate
|
|
|
|
ValidatorsKey = []byte{0x03} // prefix for each key to a validator
|
|
|
|
AccUpdateValidatorsKey = []byte{0x04} // prefix for each key to a validator which is being updated
|
2018-05-09 18:39:14 -07:00
|
|
|
CurrentValidatorsKey = []byte{0x05} // prefix for each key to the last updated validator group
|
2018-04-02 11:37:35 -07:00
|
|
|
ToKickOutValidatorsKey = []byte{0x06} // prefix for each key to the last updated validator group
|
2018-05-09 18:39:14 -07:00
|
|
|
DelegationKeyPrefix = []byte{0x07} // prefix for each key to a delegator's bond
|
2018-05-03 23:00:30 -07:00
|
|
|
IntraTxCounterKey = []byte{0x08} // key for block-local tx index
|
2018-03-22 09:00:45 -07:00
|
|
|
)
|
|
|
|
|
2018-03-27 17:17:11 -07:00
|
|
|
const maxDigitsForAccount = 12 // ~220,000,000 atoms created at launch
|
|
|
|
|
2018-03-22 14:47:57 -07:00
|
|
|
// get the key for the candidate with address
|
2018-03-22 09:00:45 -07:00
|
|
|
func GetCandidateKey(addr sdk.Address) []byte {
|
2018-03-22 14:47:57 -07:00
|
|
|
return append(CandidatesKey, 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
|
2018-05-04 16:15:24 -07:00
|
|
|
func GetValidatorKey(validator Validator) []byte {
|
|
|
|
powerBytes := []byte(validator.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
|
|
|
|
// heightBytes and counterBytes represent strings like powerBytes does
|
2018-04-09 04:12:08 -07:00
|
|
|
heightBytes := make([]byte, binary.MaxVarintLen64)
|
2018-05-04 16:15:24 -07:00
|
|
|
binary.BigEndian.PutUint64(heightBytes, ^uint64(validator.Height)) // invert height (older validators first)
|
2018-04-19 05:09:12 -07:00
|
|
|
counterBytes := make([]byte, 2)
|
2018-05-04 16:15:24 -07:00
|
|
|
binary.BigEndian.PutUint16(counterBytes, ^uint16(validator.Counter)) // invert counter (first txns have priority)
|
|
|
|
return append(ValidatorsKey,
|
|
|
|
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
|
|
|
|
func GetAccUpdateValidatorKey(addr sdk.Address) []byte {
|
|
|
|
return append(AccUpdateValidatorsKey, 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
|
|
|
|
func GetCurrentValidatorsKey(pk crypto.PubKey) []byte {
|
2018-05-03 10:38:02 -07:00
|
|
|
addr := pk.Address()
|
2018-05-09 18:39:14 -07:00
|
|
|
return append(CurrentValidatorsKey, addr.Bytes()...)
|
2018-04-02 11:37:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the key for the accumulated update validators
|
|
|
|
func GetToKickOutValidatorKey(addr sdk.Address) []byte {
|
|
|
|
return append(ToKickOutValidatorsKey, addr.Bytes()...)
|
|
|
|
}
|
|
|
|
|
2018-03-22 14:47:57 -07:00
|
|
|
// get the key for delegator bond with candidate
|
2018-05-09 18:39:14 -07:00
|
|
|
func GetDelegationKey(delegatorAddr, candidateAddr sdk.Address, cdc *wire.Codec) []byte {
|
|
|
|
return append(GetDelegationsKey(delegatorAddr, cdc), candidateAddr.Bytes()...)
|
2018-03-22 09:00:45 -07:00
|
|
|
}
|
|
|
|
|
2018-03-22 14:47:57 -07:00
|
|
|
// get the prefix for a delegator for all candidates
|
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 18:39:14 -07:00
|
|
|
return append(DelegationKeyPrefix, 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
|
|
|
}
|