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-05-12 15:22:59 -07:00
|
|
|
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
|
2018-05-12 15:22:59 -07:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2018-03-27 17:17:11 -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
|
2018-05-12 15:22:59 -07:00
|
|
|
func GetValidatorsBondedByPowerKey(validator Validator, pool Pool) []byte {
|
|
|
|
|
2018-05-12 15:54:50 -07:00
|
|
|
power := validator.EquivalentBondedShares(pool)
|
2018-05-12 15:22:59 -07:00
|
|
|
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)
|
2018-04-19 05:09:12 -07:00
|
|
|
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 {
|
2018-05-03 10:38:02 -07:00
|
|
|
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
|
|
|
}
|