From f3ca3e5e2afcd5850cf7c39c0c4643ebb29460f6 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 23 Feb 2021 11:28:46 +0100 Subject: [PATCH] Remove 042 keys files --- x/bank/legacy/v042/keys.go | 52 ---- x/bank/legacy/v042/store.go | 3 +- x/bank/legacy/v042/store_test.go | 3 +- x/distribution/legacy/v042/keys.go | 216 --------------- x/distribution/legacy/v042/store_test.go | 19 +- x/gov/legacy/v042/keys.go | 165 ----------- x/gov/legacy/v042/store_test.go | 13 +- x/slashing/legacy/v042/keys.go | 69 ----- x/slashing/legacy/v042/store_test.go | 7 +- x/staking/legacy/v042/keys.go | 338 ----------------------- x/staking/legacy/v042/store_test.go | 31 ++- 11 files changed, 41 insertions(+), 875 deletions(-) delete mode 100644 x/bank/legacy/v042/keys.go delete mode 100644 x/distribution/legacy/v042/keys.go delete mode 100644 x/gov/legacy/v042/keys.go delete mode 100644 x/slashing/legacy/v042/keys.go delete mode 100644 x/staking/legacy/v042/keys.go diff --git a/x/bank/legacy/v042/keys.go b/x/bank/legacy/v042/keys.go deleted file mode 100644 index fc9aae8c0de4..000000000000 --- a/x/bank/legacy/v042/keys.go +++ /dev/null @@ -1,52 +0,0 @@ -// Package v042 is copy-pasted from: -// https://github.com/cosmos/cosmos-sdk/blob/bddbc131fd232a0c4c3acf402eb5880109c96281/x/bank/types/key.go -package v042 - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/address" -) - -const ( - // ModuleName defines the module name - ModuleName = "bank" - - // StoreKey defines the primary module store key - StoreKey = ModuleName - - // RouterKey defines the module's message routing key - RouterKey = ModuleName - - // QuerierRoute defines the module's query routing key - QuerierRoute = ModuleName -) - -// KVStore keys -var ( - // BalancesPrefix is the for the account balances store. We use a byte - // (instead of say `[]]byte("balances")` to save some disk space). - BalancesPrefix = []byte{0x02} - SupplyKey = []byte{0x00} - DenomMetadataPrefix = []byte{0x1} -) - -// DenomMetadataKey returns the denomination metadata key. -func DenomMetadataKey(denom string) []byte { - d := []byte(denom) - return append(DenomMetadataPrefix, d...) -} - -// AddressFromBalancesStore returns an account address from a balances prefix -// store. The key must not contain the perfix BalancesPrefix as the prefix store -// iterator discards the actual prefix. -func AddressFromBalancesStore(key []byte) sdk.AccAddress { - addrLen := key[0] - addr := key[1 : addrLen+1] - - return sdk.AccAddress(addr) -} - -// CreateAccountBalancesPrefix creates the prefix for an account's balances. -func CreateAccountBalancesPrefix(addr []byte) []byte { - return append(BalancesPrefix, address.MustLengthPrefix(addr)...) -} diff --git a/x/bank/legacy/v042/store.go b/x/bank/legacy/v042/store.go index 9295d8d2ff77..894305b6c8f7 100644 --- a/x/bank/legacy/v042/store.go +++ b/x/bank/legacy/v042/store.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" v040auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v040" v040bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v040" + "github.com/cosmos/cosmos-sdk/x/bank/types" ) // MigrateStore performs in-place store migrations from v0.40 to v0.42. The @@ -27,7 +28,7 @@ func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey) error { for ; oldStoreIter.Valid(); oldStoreIter.Next() { addr := v040bank.AddressFromBalancesStore(oldStoreIter.Key()) denom := oldStoreIter.Key()[v040auth.AddrLen:] - newStoreKey := append(CreateAccountBalancesPrefix(addr), denom...) + newStoreKey := append(types.CreateAccountBalancesPrefix(addr), denom...) // Set new key on store. Values don't change. store.Set(newStoreKey, oldStoreIter.Value()) diff --git a/x/bank/legacy/v042/store_test.go b/x/bank/legacy/v042/store_test.go index e1b6a7987a04..7481232f5c59 100644 --- a/x/bank/legacy/v042/store_test.go +++ b/x/bank/legacy/v042/store_test.go @@ -10,6 +10,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" v040bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v040" v042bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v042" + "github.com/cosmos/cosmos-sdk/x/bank/types" ) func TestStoreMigration(t *testing.T) { @@ -27,7 +28,7 @@ func TestStoreMigration(t *testing.T) { err := v042bank.MigrateStore(ctx, bankKey) require.NoError(t, err) - newKey := append(v042bank.CreateAccountBalancesPrefix(addr), denom...) + newKey := append(types.CreateAccountBalancesPrefix(addr), denom...) // -7 because we replaced "balances" with 0x02, // +1 because we added length-prefix to address. require.Equal(t, len(oldKey)-7+1, len(newKey)) diff --git a/x/distribution/legacy/v042/keys.go b/x/distribution/legacy/v042/keys.go deleted file mode 100644 index 5d11677abc51..000000000000 --- a/x/distribution/legacy/v042/keys.go +++ /dev/null @@ -1,216 +0,0 @@ -// Package v042 is copy-pasted from: -// https://github.com/cosmos/cosmos-sdk/blob/bddbc131fd232a0c4c3acf402eb5880109c96281/x/distribution/types/keys.go -package v042 - -import ( - "encoding/binary" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/address" -) - -const ( - // ModuleName is the module name constant used in many places - ModuleName = "distribution" - - // StoreKey is the store key string for distribution - StoreKey = ModuleName - - // RouterKey is the message route for distribution - RouterKey = ModuleName - - // QuerierRoute is the querier route for distribution - QuerierRoute = ModuleName -) - -// Keys for distribution store -// Items are stored with the following key: values -// -// - 0x00: FeePol -// -// - 0x01: sdk.ConsAddress -// -// - 0x02: ValidatorOutstandingRewards -// -// - 0x03: sdk.AccAddress -// -// - 0x04: DelegatorStartingInfo -// -// - 0x05: ValidatorHistoricalRewards -// -// - 0x06: ValidatorCurrentRewards -// -// - 0x07: ValidatorCurrentRewards -// -// - 0x08: ValidatorSlashEvent -var ( - FeePoolKey = []byte{0x00} // key for global distribution state - ProposerKey = []byte{0x01} // key for the proposer operator address - ValidatorOutstandingRewardsPrefix = []byte{0x02} // key for outstanding rewards - - DelegatorWithdrawAddrPrefix = []byte{0x03} // key for delegator withdraw address - DelegatorStartingInfoPrefix = []byte{0x04} // key for delegator starting info - ValidatorHistoricalRewardsPrefix = []byte{0x05} // key for historical validators rewards / stake - ValidatorCurrentRewardsPrefix = []byte{0x06} // key for current validator rewards - ValidatorAccumulatedCommissionPrefix = []byte{0x07} // key for accumulated validator commission - ValidatorSlashEventPrefix = []byte{0x08} // key for validator slash fraction -) - -// GetValidatorOutstandingRewardsAddress creates an address from a validator's outstanding rewards key. -func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) { - // key is in the format: - // 0x02 - - // Remove prefix and address length. - addr := key[2:] - if len(addr) != int(key[1]) { - panic("unexpected key length") - } - - return sdk.ValAddress(addr) -} - -// GetDelegatorWithdrawInfoAddress creates an address from a delegator's withdraw info key. -func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { - // key is in the format: - // 0x03 - - // Remove prefix and address length. - addr := key[2:] - if len(addr) != int(key[1]) { - panic("unexpected key length") - } - - return sdk.AccAddress(addr) -} - -// GetDelegatorStartingInfoAddresses creates the addresses from a delegator starting info key. -func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delAddr sdk.AccAddress) { - // key is in the format: - // 0x04 - valAddrLen := int(key[1]) - valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) - delAddrLen := int(key[2+valAddrLen]) - delAddr = sdk.AccAddress(key[3+valAddrLen:]) - if len(delAddr.Bytes()) != delAddrLen { - panic("unexpected key length") - } - - return -} - -// GetValidatorHistoricalRewardsAddressPeriod creates the address & period from a validator's historical rewards key. -func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) { - // key is in the format: - // 0x05 - valAddrLen := int(key[1]) - valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) - b := key[2+valAddrLen:] - if len(b) != 8 { - panic("unexpected key length") - } - period = binary.LittleEndian.Uint64(b) - return -} - -// GetValidatorCurrentRewardsAddress creates the address from a validator's current rewards key. -func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { - // key is in the format: - // 0x06: ValidatorCurrentRewards - - // Remove prefix and address length. - addr := key[2:] - if len(addr) != int(key[1]) { - panic("unexpected key length") - } - - return sdk.ValAddress(addr) -} - -// GetValidatorAccumulatedCommissionAddress creates the address from a validator's accumulated commission key. -func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddress) { - // key is in the format: - // 0x07: ValidatorCurrentRewards - - // Remove prefix and address length. - addr := key[2:] - if len(addr) != int(key[1]) { - panic("unexpected key length") - } - - return sdk.ValAddress(addr) -} - -// GetValidatorSlashEventAddressHeight creates the height from a validator's slash event key. -func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) { - // key is in the format: - // 0x08: ValidatorSlashEvent - valAddrLen := int(key[1]) - valAddr = key[2 : 2+valAddrLen] - startB := 2 + valAddrLen - b := key[startB : startB+8] // the next 8 bytes represent the height - height = binary.BigEndian.Uint64(b) - return -} - -// GetValidatorOutstandingRewardsKey creates the outstanding rewards key for a validator. -func GetValidatorOutstandingRewardsKey(valAddr sdk.ValAddress) []byte { - return append(ValidatorOutstandingRewardsPrefix, address.MustLengthPrefix(valAddr.Bytes())...) -} - -// GetDelegatorWithdrawAddrKey creates the key for a delegator's withdraw addr. -func GetDelegatorWithdrawAddrKey(delAddr sdk.AccAddress) []byte { - return append(DelegatorWithdrawAddrPrefix, address.MustLengthPrefix(delAddr.Bytes())...) -} - -// GetDelegatorStartingInfoKey creates the key for a delegator's starting info. -func GetDelegatorStartingInfoKey(v sdk.ValAddress, d sdk.AccAddress) []byte { - return append(append(DelegatorStartingInfoPrefix, address.MustLengthPrefix(v.Bytes())...), address.MustLengthPrefix(d.Bytes())...) -} - -// GetValidatorHistoricalRewardsPrefix creates the prefix key for a validator's historical rewards. -func GetValidatorHistoricalRewardsPrefix(v sdk.ValAddress) []byte { - return append(ValidatorHistoricalRewardsPrefix, address.MustLengthPrefix(v.Bytes())...) -} - -// GetValidatorHistoricalRewardsKey creates the key for a validator's historical rewards. -func GetValidatorHistoricalRewardsKey(v sdk.ValAddress, k uint64) []byte { - b := make([]byte, 8) - binary.LittleEndian.PutUint64(b, k) - return append(append(ValidatorHistoricalRewardsPrefix, address.MustLengthPrefix(v.Bytes())...), b...) -} - -// GetValidatorCurrentRewardsKey creates the key for a validator's current rewards. -func GetValidatorCurrentRewardsKey(v sdk.ValAddress) []byte { - return append(ValidatorCurrentRewardsPrefix, address.MustLengthPrefix(v.Bytes())...) -} - -// GetValidatorAccumulatedCommissionKey creates the key for a validator's current commission. -func GetValidatorAccumulatedCommissionKey(v sdk.ValAddress) []byte { - return append(ValidatorAccumulatedCommissionPrefix, address.MustLengthPrefix(v.Bytes())...) -} - -// GetValidatorSlashEventPrefix creates the prefix key for a validator's slash fractions. -func GetValidatorSlashEventPrefix(v sdk.ValAddress) []byte { - return append(ValidatorSlashEventPrefix, address.MustLengthPrefix(v.Bytes())...) -} - -// GetValidatorSlashEventKeyPrefix creates the prefix key for a validator's slash fraction (ValidatorSlashEventPrefix + height). -func GetValidatorSlashEventKeyPrefix(v sdk.ValAddress, height uint64) []byte { - heightBz := make([]byte, 8) - binary.BigEndian.PutUint64(heightBz, height) - - return append( - ValidatorSlashEventPrefix, - append(address.MustLengthPrefix(v.Bytes()), heightBz...)..., - ) -} - -// GetValidatorSlashEventKey creates the key for a validator's slash fraction. -func GetValidatorSlashEventKey(v sdk.ValAddress, height, period uint64) []byte { - periodBz := make([]byte, 8) - binary.BigEndian.PutUint64(periodBz, period) - prefix := GetValidatorSlashEventKeyPrefix(v, height) - - return append(prefix, periodBz...) -} diff --git a/x/distribution/legacy/v042/store_test.go b/x/distribution/legacy/v042/store_test.go index 992294e3aaf6..2a4a5df75bb1 100644 --- a/x/distribution/legacy/v042/store_test.go +++ b/x/distribution/legacy/v042/store_test.go @@ -11,6 +11,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" v040distribution "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v040" v042distribution "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v042" + "github.com/cosmos/cosmos-sdk/x/distribution/types" ) func TestStoreMigration(t *testing.T) { @@ -32,47 +33,47 @@ func TestStoreMigration(t *testing.T) { { "FeePoolKey", v040distribution.FeePoolKey, - v042distribution.FeePoolKey, + types.FeePoolKey, }, { "ProposerKey", v040distribution.ProposerKey, - v042distribution.ProposerKey, + types.ProposerKey, }, { "ValidatorOutstandingRewards", v040distribution.GetValidatorOutstandingRewardsKey(valAddr), - v042distribution.GetValidatorOutstandingRewardsKey(valAddr), + types.GetValidatorOutstandingRewardsKey(valAddr), }, { "DelegatorWithdrawAddr", v040distribution.GetDelegatorWithdrawAddrKey(addr2), - v042distribution.GetDelegatorWithdrawAddrKey(addr2), + types.GetDelegatorWithdrawAddrKey(addr2), }, { "DelegatorStartingInfo", v040distribution.GetDelegatorStartingInfoKey(valAddr, addr2), - v042distribution.GetDelegatorStartingInfoKey(valAddr, addr2), + types.GetDelegatorStartingInfoKey(valAddr, addr2), }, { "ValidatorHistoricalRewards", v040distribution.GetValidatorHistoricalRewardsKey(valAddr, 6), - v042distribution.GetValidatorHistoricalRewardsKey(valAddr, 6), + types.GetValidatorHistoricalRewardsKey(valAddr, 6), }, { "ValidatorCurrentRewards", v040distribution.GetValidatorCurrentRewardsKey(valAddr), - v042distribution.GetValidatorCurrentRewardsKey(valAddr), + types.GetValidatorCurrentRewardsKey(valAddr), }, { "ValidatorAccumulatedCommission", v040distribution.GetValidatorAccumulatedCommissionKey(valAddr), - v042distribution.GetValidatorAccumulatedCommissionKey(valAddr), + types.GetValidatorAccumulatedCommissionKey(valAddr), }, { "ValidatorSlashEvent", v040distribution.GetValidatorSlashEventKey(valAddr, 6, 8), - v042distribution.GetValidatorSlashEventKey(valAddr, 6, 8), + types.GetValidatorSlashEventKey(valAddr, 6, 8), }, } diff --git a/x/gov/legacy/v042/keys.go b/x/gov/legacy/v042/keys.go deleted file mode 100644 index 1075d90d63e9..000000000000 --- a/x/gov/legacy/v042/keys.go +++ /dev/null @@ -1,165 +0,0 @@ -// Package v042 is copy-pasted from: -// https://github.com/cosmos/cosmos-sdk/blob/bddbc131fd232a0c4c3acf402eb5880109c96281/x/gov/types/keys.go -package v042 - -import ( - "encoding/binary" - "fmt" - "time" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/address" -) - -const ( - // ModuleName is the name of the module - ModuleName = "gov" - - // StoreKey is the store key string for gov - StoreKey = ModuleName - - // RouterKey is the message route for gov - RouterKey = ModuleName - - // QuerierRoute is the querier route for gov - QuerierRoute = ModuleName -) - -// Keys for governance store -// Items are stored with the following key: values -// -// - 0x00: Proposal -// -// - 0x01: activeProposalID -// -// - 0x02: inactiveProposalID -// -// - 0x03: nextProposalID -// -// - 0x10: Deposit -// -// - 0x20: Voter -var ( - ProposalsKeyPrefix = []byte{0x00} - ActiveProposalQueuePrefix = []byte{0x01} - InactiveProposalQueuePrefix = []byte{0x02} - ProposalIDKey = []byte{0x03} - - DepositsKeyPrefix = []byte{0x10} - - VotesKeyPrefix = []byte{0x20} -) - -var lenTime = len(sdk.FormatTimeBytes(time.Now())) - -// GetProposalIDBytes returns the byte representation of the proposalID -func GetProposalIDBytes(proposalID uint64) (proposalIDBz []byte) { - proposalIDBz = make([]byte, 8) - binary.BigEndian.PutUint64(proposalIDBz, proposalID) - return -} - -// GetProposalIDFromBytes returns proposalID in uint64 format from a byte array -func GetProposalIDFromBytes(bz []byte) (proposalID uint64) { - return binary.BigEndian.Uint64(bz) -} - -// ProposalKey gets a specific proposal from the store -func ProposalKey(proposalID uint64) []byte { - return append(ProposalsKeyPrefix, GetProposalIDBytes(proposalID)...) -} - -// ActiveProposalByTimeKey gets the active proposal queue key by endTime -func ActiveProposalByTimeKey(endTime time.Time) []byte { - return append(ActiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...) -} - -// ActiveProposalQueueKey returns the key for a proposalID in the activeProposalQueue -func ActiveProposalQueueKey(proposalID uint64, endTime time.Time) []byte { - return append(ActiveProposalByTimeKey(endTime), GetProposalIDBytes(proposalID)...) -} - -// InactiveProposalByTimeKey gets the inactive proposal queue key by endTime -func InactiveProposalByTimeKey(endTime time.Time) []byte { - return append(InactiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...) -} - -// InactiveProposalQueueKey returns the key for a proposalID in the inactiveProposalQueue -func InactiveProposalQueueKey(proposalID uint64, endTime time.Time) []byte { - return append(InactiveProposalByTimeKey(endTime), GetProposalIDBytes(proposalID)...) -} - -// DepositsKey gets the first part of the deposits key based on the proposalID -func DepositsKey(proposalID uint64) []byte { - return append(DepositsKeyPrefix, GetProposalIDBytes(proposalID)...) -} - -// DepositKey key of a specific deposit from the store -func DepositKey(proposalID uint64, depositorAddr sdk.AccAddress) []byte { - return append(DepositsKey(proposalID), address.MustLengthPrefix(depositorAddr.Bytes())...) -} - -// VotesKey gets the first part of the votes key based on the proposalID -func VotesKey(proposalID uint64) []byte { - return append(VotesKeyPrefix, GetProposalIDBytes(proposalID)...) -} - -// VoteKey key of a specific vote from the store -func VoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte { - return append(VotesKey(proposalID), address.MustLengthPrefix(voterAddr.Bytes())...) -} - -// Split keys function; used for iterators - -// SplitProposalKey split the proposal key and returns the proposal id -func SplitProposalKey(key []byte) (proposalID uint64) { - if len(key[1:]) != 8 { - panic(fmt.Sprintf("unexpected key length (%d ≠ 8)", len(key[1:]))) - } - - return GetProposalIDFromBytes(key[1:]) -} - -// SplitActiveProposalQueueKey split the active proposal key and returns the proposal id and endTime -func SplitActiveProposalQueueKey(key []byte) (proposalID uint64, endTime time.Time) { - return splitKeyWithTime(key) -} - -// SplitInactiveProposalQueueKey split the inactive proposal key and returns the proposal id and endTime -func SplitInactiveProposalQueueKey(key []byte) (proposalID uint64, endTime time.Time) { - return splitKeyWithTime(key) -} - -// SplitKeyDeposit split the deposits key and returns the proposal id and depositor address -func SplitKeyDeposit(key []byte) (proposalID uint64, depositorAddr sdk.AccAddress) { - return splitKeyWithAddress(key) -} - -// SplitKeyVote split the votes key and returns the proposal id and voter address -func SplitKeyVote(key []byte) (proposalID uint64, voterAddr sdk.AccAddress) { - return splitKeyWithAddress(key) -} - -// private functions - -func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) { - if len(key[1:]) != 8+lenTime { - panic(fmt.Sprintf("unexpected key length (%d ≠ %d)", len(key[1:]), lenTime+8)) - } - - endTime, err := sdk.ParseTimeBytes(key[1 : 1+lenTime]) - if err != nil { - panic(err) - } - - proposalID = GetProposalIDFromBytes(key[1+lenTime:]) - return -} - -func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) { - // Both Vote and Deposit store keys are of format: - // - proposalID = GetProposalIDFromBytes(key[1:9]) - addr = sdk.AccAddress(key[10:]) - return -} diff --git a/x/gov/legacy/v042/store_test.go b/x/gov/legacy/v042/store_test.go index 89edef75e0fa..6bcb056a9c99 100644 --- a/x/gov/legacy/v042/store_test.go +++ b/x/gov/legacy/v042/store_test.go @@ -12,6 +12,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" v040gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v040" v042gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v042" + "github.com/cosmos/cosmos-sdk/x/gov/types" ) func TestStoreMigration(t *testing.T) { @@ -33,32 +34,32 @@ func TestStoreMigration(t *testing.T) { { "ProposalKey", v040gov.ProposalKey(proposalID), - v042gov.ProposalKey(proposalID), + types.ProposalKey(proposalID), }, { "ActiveProposalQueue", v040gov.ActiveProposalQueueKey(proposalID, now), - v042gov.ActiveProposalQueueKey(proposalID, now), + types.ActiveProposalQueueKey(proposalID, now), }, { "InactiveProposalQueue", v040gov.InactiveProposalQueueKey(proposalID, now), - v042gov.InactiveProposalQueueKey(proposalID, now), + types.InactiveProposalQueueKey(proposalID, now), }, { "ProposalIDKey", v040gov.ProposalIDKey, - v042gov.ProposalIDKey, + types.ProposalIDKey, }, { "DepositKey", v040gov.DepositKey(proposalID, addr1), - v042gov.DepositKey(proposalID, addr1), + types.DepositKey(proposalID, addr1), }, { "VotesKeyPrefix", v040gov.VoteKey(proposalID, addr1), - v042gov.VoteKey(proposalID, addr1), + types.VoteKey(proposalID, addr1), }, } diff --git a/x/slashing/legacy/v042/keys.go b/x/slashing/legacy/v042/keys.go deleted file mode 100644 index 992f9aad1dc4..000000000000 --- a/x/slashing/legacy/v042/keys.go +++ /dev/null @@ -1,69 +0,0 @@ -// Package v042 is copy-pasted from: -// https://github.com/cosmos/cosmos-sdk/blob/bddbc131fd232a0c4c3acf402eb5880109c96281/x/slashing/types/keys.go -package v042 - -import ( - "encoding/binary" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/address" -) - -const ( - // ModuleName is the name of the module - ModuleName = "slashing" - - // StoreKey is the store key string for slashing - StoreKey = ModuleName - - // RouterKey is the message route for slashing - RouterKey = ModuleName - - // QuerierRoute is the querier route for slashing - QuerierRoute = ModuleName -) - -// Keys for slashing store -// Items are stored with the following key: values -// -// - 0x01: ValidatorSigningInfo -// -// - 0x02: bool -// -// - 0x03: cryptotypes.PubKey -var ( - ValidatorSigningInfoKeyPrefix = []byte{0x01} // Prefix for signing info - ValidatorMissedBlockBitArrayKeyPrefix = []byte{0x02} // Prefix for missed block bit array - AddrPubkeyRelationKeyPrefix = []byte{0x03} // Prefix for address-pubkey relation -) - -// ValidatorSigningInfoKey - stored by *Consensus* address (not operator address) -func ValidatorSigningInfoKey(v sdk.ConsAddress) []byte { - return append(ValidatorSigningInfoKeyPrefix, address.MustLengthPrefix(v.Bytes())...) -} - -// ValidatorSigningInfoAddress - extract the address from a validator signing info key -func ValidatorSigningInfoAddress(key []byte) (v sdk.ConsAddress) { - // Remove prefix and address length. - addr := key[2:] - - return sdk.ConsAddress(addr) -} - -// ValidatorMissedBlockBitArrayPrefixKey - stored by *Consensus* address (not operator address) -func ValidatorMissedBlockBitArrayPrefixKey(v sdk.ConsAddress) []byte { - return append(ValidatorMissedBlockBitArrayKeyPrefix, address.MustLengthPrefix(v.Bytes())...) -} - -// ValidatorMissedBlockBitArrayKey - stored by *Consensus* address (not operator address) -func ValidatorMissedBlockBitArrayKey(v sdk.ConsAddress, i int64) []byte { - b := make([]byte, 8) - binary.LittleEndian.PutUint64(b, uint64(i)) - - return append(ValidatorMissedBlockBitArrayPrefixKey(v), b...) -} - -// AddrPubkeyRelationKey gets pubkey relation key used to get the pubkey from the address -func AddrPubkeyRelationKey(addr []byte) []byte { - return append(AddrPubkeyRelationKeyPrefix, address.MustLengthPrefix(addr)...) -} diff --git a/x/slashing/legacy/v042/store_test.go b/x/slashing/legacy/v042/store_test.go index be08c864c0a6..60bf43a27b84 100644 --- a/x/slashing/legacy/v042/store_test.go +++ b/x/slashing/legacy/v042/store_test.go @@ -11,6 +11,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" v040slashing "github.com/cosmos/cosmos-sdk/x/slashing/legacy/v040" v042slashing "github.com/cosmos/cosmos-sdk/x/slashing/legacy/v042" + "github.com/cosmos/cosmos-sdk/x/slashing/types" ) func TestStoreMigration(t *testing.T) { @@ -31,17 +32,17 @@ func TestStoreMigration(t *testing.T) { { "ValidatorSigningInfoKey", v040slashing.ValidatorSigningInfoKey(consAddr), - v042slashing.ValidatorSigningInfoKey(consAddr), + types.ValidatorSigningInfoKey(consAddr), }, { "ValidatorMissedBlockBitArrayKey", v040slashing.ValidatorMissedBlockBitArrayKey(consAddr, 2), - v042slashing.ValidatorMissedBlockBitArrayKey(consAddr, 2), + types.ValidatorMissedBlockBitArrayKey(consAddr, 2), }, { "AddrPubkeyRelationKey", v040slashing.AddrPubkeyRelationKey(consAddr), - v042slashing.AddrPubkeyRelationKey(consAddr), + types.AddrPubkeyRelationKey(consAddr), }, } diff --git a/x/staking/legacy/v042/keys.go b/x/staking/legacy/v042/keys.go deleted file mode 100644 index d8eb838e8320..000000000000 --- a/x/staking/legacy/v042/keys.go +++ /dev/null @@ -1,338 +0,0 @@ -// Package v042 is copy-pasted from: -// https://github.com/cosmos/cosmos-sdk/blob/bddbc131fd232a0c4c3acf402eb5880109c96281/x/staking/types/keys.go -package v042 - -import ( - "bytes" - "encoding/binary" - "fmt" - "strconv" - "time" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/address" - "github.com/cosmos/cosmos-sdk/x/staking/types" -) - -const ( - // ModuleName is the name of the staking module - ModuleName = "staking" - - // StoreKey is the string store representation - StoreKey = ModuleName - - // QuerierRoute is the querier route for the staking module - QuerierRoute = ModuleName - - // RouterKey is the msg router key for the staking module - RouterKey = ModuleName -) - -var ( - // Keys for store prefixes - // Last* values are constant during a block. - LastValidatorPowerKey = []byte{0x11} // prefix for each key to a validator index, for bonded validators - LastTotalPowerKey = []byte{0x12} // prefix for the total power - - ValidatorsKey = []byte{0x21} // prefix for each key to a validator - ValidatorsByConsAddrKey = []byte{0x22} // prefix for each key to a validator index, by pubkey - ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power - - DelegationKey = []byte{0x31} // key for a delegation - UnbondingDelegationKey = []byte{0x32} // key for an unbonding-delegation - UnbondingDelegationByValIndexKey = []byte{0x33} // prefix for each key for an unbonding-delegation, by validator operator - RedelegationKey = []byte{0x34} // key for a redelegation - RedelegationByValSrcIndexKey = []byte{0x35} // prefix for each key for an redelegation, by source validator operator - RedelegationByValDstIndexKey = []byte{0x36} // prefix for each key for an redelegation, by destination validator operator - - UnbondingQueueKey = []byte{0x41} // prefix for the timestamps in unbonding queue - RedelegationQueueKey = []byte{0x42} // prefix for the timestamps in redelegations queue - ValidatorQueueKey = []byte{0x43} // prefix for the timestamps in validator queue - - HistoricalInfoKey = []byte{0x50} // prefix for the historical info -) - -// GetValidatorKey creates the key for the validator with address -// VALUE: staking/Validator -func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { - return append(ValidatorsKey, address.MustLengthPrefix(operatorAddr)...) -} - -// GetValidatorByConsAddrKey creates the key for the validator with pubkey -// VALUE: validator operator address ([]byte) -func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte { - return append(ValidatorsByConsAddrKey, address.MustLengthPrefix(addr)...) -} - -// AddressFromValidatorsKey creates the validator operator address from ValidatorsKey -func AddressFromValidatorsKey(key []byte) []byte { - return key[2:] // remove prefix bytes and address length -} - -// AddressFromLastValidatorPowerKey creates the validator operator address from LastValidatorPowerKey -func AddressFromLastValidatorPowerKey(key []byte) []byte { - return key[2:] // remove prefix bytes and address length -} - -// GetValidatorsByPowerIndexKey creates the validator by power index. -// Power index is the key used in the power-store, and represents the relative -// power ranking of the validator. -// VALUE: validator operator address ([]byte) -func GetValidatorsByPowerIndexKey(validator types.Validator) []byte { - // NOTE the address doesn't need to be stored because counter bytes must always be different - // NOTE the larger values are of higher value - - consensusPower := sdk.TokensToConsensusPower(validator.Tokens) - consensusPowerBytes := make([]byte, 8) - binary.BigEndian.PutUint64(consensusPowerBytes, uint64(consensusPower)) - - powerBytes := consensusPowerBytes - powerBytesLen := len(powerBytes) // 8 - - addr, err := sdk.ValAddressFromBech32(validator.OperatorAddress) - if err != nil { - panic(err) - } - operAddrInvr := sdk.CopyBytes(addr) - addrLen := len(operAddrInvr) - - for i, b := range operAddrInvr { - operAddrInvr[i] = ^b - } - - // key is of format prefix || powerbytes || addrLen (1byte) || addrBytes - key := make([]byte, 1+powerBytesLen+1+addrLen) - - key[0] = ValidatorsByPowerIndexKey[0] - copy(key[1:powerBytesLen+1], powerBytes) - key[powerBytesLen+1] = byte(addrLen) - copy(key[powerBytesLen+2:], operAddrInvr) - - return key -} - -// GetLastValidatorPowerKey creates the bonded validator index key for an operator address -func GetLastValidatorPowerKey(operator sdk.ValAddress) []byte { - return append(LastValidatorPowerKey, address.MustLengthPrefix(operator)...) -} - -// ParseValidatorPowerRankKey parses the validators operator address from power rank key -func ParseValidatorPowerRankKey(key []byte) (operAddr []byte) { - powerBytesLen := 8 - - // key is of format prefix (1 byte) || powerbytes || addrLen (1byte) || addrBytes - operAddr = sdk.CopyBytes(key[powerBytesLen+2:]) - - for i, b := range operAddr { - operAddr[i] = ^b - } - - return operAddr -} - -// GetValidatorQueueKey returns the prefix key used for getting a set of unbonding -// validators whose unbonding completion occurs at the given time and height. -func GetValidatorQueueKey(timestamp time.Time, height int64) []byte { - heightBz := sdk.Uint64ToBigEndian(uint64(height)) - timeBz := sdk.FormatTimeBytes(timestamp) - timeBzL := len(timeBz) - prefixL := len(ValidatorQueueKey) - - bz := make([]byte, prefixL+8+timeBzL+8) - - // copy the prefix - copy(bz[:prefixL], ValidatorQueueKey) - - // copy the encoded time bytes length - copy(bz[prefixL:prefixL+8], sdk.Uint64ToBigEndian(uint64(timeBzL))) - - // copy the encoded time bytes - copy(bz[prefixL+8:prefixL+8+timeBzL], timeBz) - - // copy the encoded height - copy(bz[prefixL+8+timeBzL:], heightBz) - - return bz -} - -// ParseValidatorQueueKey returns the encoded time and height from a key created -// from GetValidatorQueueKey. -func ParseValidatorQueueKey(bz []byte) (time.Time, int64, error) { - prefixL := len(ValidatorQueueKey) - if prefix := bz[:prefixL]; !bytes.Equal(prefix, ValidatorQueueKey) { - return time.Time{}, 0, fmt.Errorf("invalid prefix; expected: %X, got: %X", ValidatorQueueKey, prefix) - } - - timeBzL := sdk.BigEndianToUint64(bz[prefixL : prefixL+8]) - ts, err := sdk.ParseTimeBytes(bz[prefixL+8 : prefixL+8+int(timeBzL)]) - if err != nil { - return time.Time{}, 0, err - } - - height := sdk.BigEndianToUint64(bz[prefixL+8+int(timeBzL):]) - - return ts, int64(height), nil -} - -// GetDelegationKey creates the key for delegator bond with validator -// VALUE: staking/Delegation -func GetDelegationKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { - return append(GetDelegationsKey(delAddr), address.MustLengthPrefix(valAddr)...) -} - -// GetDelegationsKey creates the prefix for a delegator for all validators -func GetDelegationsKey(delAddr sdk.AccAddress) []byte { - return append(DelegationKey, address.MustLengthPrefix(delAddr)...) -} - -// GetUBDKey creates the key for an unbonding delegation by delegator and validator addr -// VALUE: staking/UnbondingDelegation -func GetUBDKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { - return append(GetUBDsKey(delAddr.Bytes()), address.MustLengthPrefix(valAddr)...) -} - -// GetUBDByValIndexKey creates the index-key for an unbonding delegation, stored by validator-index -// VALUE: none (key rearrangement used) -func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { - return append(GetUBDsByValIndexKey(valAddr), address.MustLengthPrefix(delAddr)...) -} - -// GetUBDKeyFromValIndexKey rearranges the ValIndexKey to get the UBDKey -func GetUBDKeyFromValIndexKey(indexKey []byte) []byte { - addrs := indexKey[1:] // remove prefix bytes - - valAddrLen := addrs[0] - valAddr := addrs[1 : 1+valAddrLen] - delAddr := addrs[valAddrLen+2:] - - return GetUBDKey(delAddr, valAddr) -} - -// GetUBDsKey creates the prefix for all unbonding delegations from a delegator -func GetUBDsKey(delAddr sdk.AccAddress) []byte { - return append(UnbondingDelegationKey, address.MustLengthPrefix(delAddr)...) -} - -// GetUBDsByValIndexKey creates the prefix keyspace for the indexes of unbonding delegations for a validator -func GetUBDsByValIndexKey(valAddr sdk.ValAddress) []byte { - return append(UnbondingDelegationByValIndexKey, address.MustLengthPrefix(valAddr)...) -} - -// GetUnbondingDelegationTimeKey creates the prefix for all unbonding delegations from a delegator -func GetUnbondingDelegationTimeKey(timestamp time.Time) []byte { - bz := sdk.FormatTimeBytes(timestamp) - return append(UnbondingQueueKey, bz...) -} - -// GetREDKey returns a key prefix for indexing a redelegation from a delegator -// and source validator to a destination validator. -func GetREDKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { - // key is of the form GetREDsKey || valSrcAddrLen (1 byte) || valSrcAddr || valDstAddrLen (1 byte) || valDstAddr - key := make([]byte, 1+3+len(delAddr)+len(valSrcAddr)+len(valDstAddr)) - - copy(key[0:2+len(delAddr)], GetREDsKey(delAddr.Bytes())) - key[2+len(delAddr)] = byte(len(valSrcAddr)) - copy(key[3+len(delAddr):3+len(delAddr)+len(valSrcAddr)], valSrcAddr.Bytes()) - key[3+len(delAddr)+len(valSrcAddr)] = byte(len(valDstAddr)) - copy(key[4+len(delAddr)+len(valSrcAddr):], valDstAddr.Bytes()) - - return key -} - -// GetREDByValSrcIndexKey creates the index-key for a redelegation, stored by source-validator-index -// VALUE: none (key rearrangement used) -func GetREDByValSrcIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { - REDSFromValsSrcKey := GetREDsFromValSrcIndexKey(valSrcAddr) - offset := len(REDSFromValsSrcKey) - - // key is of the form REDSFromValsSrcKey || delAddrLen (1 byte) || delAddr || valDstAddrLen (1 byte) || valDstAddr - key := make([]byte, offset+2+len(delAddr)+len(valDstAddr)) - copy(key[0:offset], REDSFromValsSrcKey) - key[offset] = byte(len(delAddr)) - copy(key[offset+1:offset+1+len(delAddr)], delAddr.Bytes()) - key[offset+1+len(delAddr)] = byte(len(valDstAddr)) - copy(key[offset+2+len(delAddr):], valDstAddr.Bytes()) - - return key -} - -// GetREDByValDstIndexKey creates the index-key for a redelegation, stored by destination-validator-index -// VALUE: none (key rearrangement used) -func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { - REDSToValsDstKey := GetREDsToValDstIndexKey(valDstAddr) - offset := len(REDSToValsDstKey) - - // key is of the form REDSToValsDstKey || delAddrLen (1 byte) || delAddr || valSrcAddrLen (1 byte) || valSrcAddr - key := make([]byte, offset+2+len(delAddr)+len(valSrcAddr)) - copy(key[0:offset], REDSToValsDstKey) - key[offset] = byte(len(delAddr)) - copy(key[offset+1:offset+1+len(delAddr)], delAddr.Bytes()) - key[offset+1+len(delAddr)] = byte(len(valSrcAddr)) - copy(key[offset+2+len(delAddr):], valSrcAddr.Bytes()) - - return key -} - -// GetREDKeyFromValSrcIndexKey rearranges the ValSrcIndexKey to get the REDKey -func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { - // note that first byte is prefix byte, which we remove - addrs := indexKey[1:] - - valSrcAddrLen := addrs[0] - valSrcAddr := addrs[1 : valSrcAddrLen+1] - delAddrLen := addrs[valSrcAddrLen+1] - delAddr := addrs[valSrcAddrLen+2 : valSrcAddrLen+2+delAddrLen] - valDstAddr := addrs[valSrcAddrLen+delAddrLen+3:] - - return GetREDKey(delAddr, valSrcAddr, valDstAddr) -} - -// GetREDKeyFromValDstIndexKey rearranges the ValDstIndexKey to get the REDKey -func GetREDKeyFromValDstIndexKey(indexKey []byte) []byte { - // note that first byte is prefix byte, which we remove - addrs := indexKey[1:] - - valDstAddrLen := addrs[0] - valDstAddr := addrs[1 : valDstAddrLen+1] - delAddrLen := addrs[valDstAddrLen+1] - delAddr := addrs[valDstAddrLen+2 : valDstAddrLen+2+delAddrLen] - valSrcAddr := addrs[valDstAddrLen+delAddrLen+3:] - - return GetREDKey(delAddr, valSrcAddr, valDstAddr) -} - -// GetRedelegationTimeKey returns a key prefix for indexing an unbonding -// redelegation based on a completion time. -func GetRedelegationTimeKey(timestamp time.Time) []byte { - bz := sdk.FormatTimeBytes(timestamp) - return append(RedelegationQueueKey, bz...) -} - -// GetREDsKey returns a key prefix for indexing a redelegation from a delegator -// address. -func GetREDsKey(delAddr sdk.AccAddress) []byte { - return append(RedelegationKey, address.MustLengthPrefix(delAddr)...) -} - -// GetREDsFromValSrcIndexKey returns a key prefix for indexing a redelegation to -// a source validator. -func GetREDsFromValSrcIndexKey(valSrcAddr sdk.ValAddress) []byte { - return append(RedelegationByValSrcIndexKey, address.MustLengthPrefix(valSrcAddr)...) -} - -// GetREDsToValDstIndexKey returns a key prefix for indexing a redelegation to a -// destination (target) validator. -func GetREDsToValDstIndexKey(valDstAddr sdk.ValAddress) []byte { - return append(RedelegationByValDstIndexKey, address.MustLengthPrefix(valDstAddr)...) -} - -// GetREDsByDelToValDstIndexKey returns a key prefix for indexing a redelegation -// from an address to a source validator. -func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddress) []byte { - return append(GetREDsToValDstIndexKey(valDstAddr), address.MustLengthPrefix(delAddr)...) -} - -// GetHistoricalInfoKey returns a key prefix for indexing HistoricalInfo objects. -func GetHistoricalInfoKey(height int64) []byte { - return append(HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) -} diff --git a/x/staking/legacy/v042/store_test.go b/x/staking/legacy/v042/store_test.go index be082306aed0..963ef02ed9c9 100644 --- a/x/staking/legacy/v042/store_test.go +++ b/x/staking/legacy/v042/store_test.go @@ -13,6 +13,7 @@ import ( v040staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v040" v042staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v042" "github.com/cosmos/cosmos-sdk/x/staking/teststaking" + "github.com/cosmos/cosmos-sdk/x/staking/types" ) func TestStoreMigration(t *testing.T) { @@ -40,77 +41,77 @@ func TestStoreMigration(t *testing.T) { { "LastValidatorPowerKey", v040staking.GetLastValidatorPowerKey(valAddr1), - v042staking.GetLastValidatorPowerKey(valAddr1), + types.GetLastValidatorPowerKey(valAddr1), }, { "LastTotalPowerKey", v040staking.LastTotalPowerKey, - v042staking.LastTotalPowerKey, + types.LastTotalPowerKey, }, { "ValidatorsKey", v040staking.GetValidatorKey(valAddr1), - v042staking.GetValidatorKey(valAddr1), + types.GetValidatorKey(valAddr1), }, { "ValidatorsByConsAddrKey", v040staking.GetValidatorByConsAddrKey(consAddr), - v042staking.GetValidatorByConsAddrKey(consAddr), + types.GetValidatorByConsAddrKey(consAddr), }, { "ValidatorsByPowerIndexKey", v040staking.GetValidatorsByPowerIndexKey(val), - v042staking.GetValidatorsByPowerIndexKey(val), + types.GetValidatorsByPowerIndexKey(val), }, { "DelegationKey", v040staking.GetDelegationKey(addr4, valAddr1), - v042staking.GetDelegationKey(addr4, valAddr1), + types.GetDelegationKey(addr4, valAddr1), }, { "UnbondingDelegationKey", v040staking.GetUBDKey(addr4, valAddr1), - v042staking.GetUBDKey(addr4, valAddr1), + types.GetUBDKey(addr4, valAddr1), }, { "UnbondingDelegationByValIndexKey", v040staking.GetUBDByValIndexKey(addr4, valAddr1), - v042staking.GetUBDByValIndexKey(addr4, valAddr1), + types.GetUBDByValIndexKey(addr4, valAddr1), }, { "RedelegationKey", v040staking.GetREDKey(addr4, valAddr1, valAddr2), - v042staking.GetREDKey(addr4, valAddr1, valAddr2), + types.GetREDKey(addr4, valAddr1, valAddr2), }, { "RedelegationByValSrcIndexKey", v040staking.GetREDByValSrcIndexKey(addr4, valAddr1, valAddr2), - v042staking.GetREDByValSrcIndexKey(addr4, valAddr1, valAddr2), + types.GetREDByValSrcIndexKey(addr4, valAddr1, valAddr2), }, { "RedelegationByValDstIndexKey", v040staking.GetREDByValDstIndexKey(addr4, valAddr1, valAddr2), - v042staking.GetREDByValDstIndexKey(addr4, valAddr1, valAddr2), + types.GetREDByValDstIndexKey(addr4, valAddr1, valAddr2), }, { "UnbondingQueueKey", v040staking.GetUnbondingDelegationTimeKey(now), - v042staking.GetUnbondingDelegationTimeKey(now), + types.GetUnbondingDelegationTimeKey(now), }, { "RedelegationQueueKey", v040staking.GetRedelegationTimeKey(now), - v042staking.GetRedelegationTimeKey(now), + types.GetRedelegationTimeKey(now), }, { "ValidatorQueueKey", v040staking.GetValidatorQueueKey(now, 4), - v042staking.GetValidatorQueueKey(now, 4), + types.GetValidatorQueueKey(now, 4), }, { "HistoricalInfoKey", v040staking.GetHistoricalInfoKey(4), - v042staking.GetHistoricalInfoKey(4), + types.GetHistoricalInfoKey(4), }, }