From 3d64b49358534b61b891ad6a7f752fac63125ef3 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Tue, 6 Jul 2021 16:11:21 +0530 Subject: [PATCH 01/10] check store keys length before accessing --- x/authz/keeper/keys.go | 10 ++++++++++ x/distribution/migrations/v040/keys.go | 8 ++++++++ x/distribution/types/keys.go | 11 +++++++++++ x/gov/types/keys.go | 7 +++++++ 4 files changed, 36 insertions(+) diff --git a/x/authz/keeper/keys.go b/x/authz/keeper/keys.go index 3bdd02138efb..c2bbe4a00016 100644 --- a/x/authz/keeper/keys.go +++ b/x/authz/keeper/keys.go @@ -1,6 +1,8 @@ package keeper import ( + "fmt" + "github.com/cosmos/cosmos-sdk/internal/conv" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" @@ -38,10 +40,18 @@ func grantStoreKey(grantee sdk.AccAddress, granter sdk.AccAddress, msgType strin func addressesFromGrantStoreKey(key []byte) (granterAddr, granteeAddr sdk.AccAddress) { // key is of format: // 0x01 + assertKeyAtLeastLength(key, 2) granterAddrLen := key[1] // remove prefix key granterAddr = sdk.AccAddress(key[2 : 2+granterAddrLen]) + assertKeyAtLeastLength(key, int(3+granterAddrLen)) granteeAddrLen := int(key[2+granterAddrLen]) granteeAddr = sdk.AccAddress(key[3+granterAddrLen : 3+granterAddrLen+byte(granteeAddrLen)]) return granterAddr, granteeAddr } + +func assertKeyAtLeastLength(bz []byte, length int) { + if len(bz) < length { + panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) + } +} diff --git a/x/distribution/migrations/v040/keys.go b/x/distribution/migrations/v040/keys.go index c6978a9e31de..ea5235d004e3 100644 --- a/x/distribution/migrations/v040/keys.go +++ b/x/distribution/migrations/v040/keys.go @@ -4,6 +4,7 @@ package v040 import ( "encoding/binary" + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" v040auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v040" @@ -130,6 +131,7 @@ func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, he } valAddr = sdk.ValAddress(addr) startB := 1 + v040auth.AddrLen + assertKeyAtLeastLength(key, startB+9) b := key[startB : startB+8] // the next 8 bytes represent the height height = binary.BigEndian.Uint64(b) return @@ -194,3 +196,9 @@ func GetValidatorSlashEventKey(v sdk.ValAddress, height, period uint64) []byte { prefix := GetValidatorSlashEventKeyPrefix(v, height) return append(prefix, periodBz...) } + +func assertKeyAtLeastLength(bz []byte, length int) { + if len(bz) < length { + panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) + } +} diff --git a/x/distribution/types/keys.go b/x/distribution/types/keys.go index d28ba2d6b7d7..b34b07ee3efd 100644 --- a/x/distribution/types/keys.go +++ b/x/distribution/types/keys.go @@ -2,6 +2,7 @@ package types import ( "encoding/binary" + fmt "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" @@ -86,6 +87,7 @@ func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delAddr sdk.AccAddress) { // key is in the format: // 0x04 + assertKeyAtLeastLength(key, 2) valAddrLen := int(key[1]) valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) delAddrLen := int(key[2+valAddrLen]) @@ -101,6 +103,7 @@ func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delA func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) { // key is in the format: // 0x05 + assertKeyAtLeastLength(key, 2) valAddrLen := int(key[1]) valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) b := key[2+valAddrLen:] @@ -143,9 +146,11 @@ func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddres func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) { // key is in the format: // 0x08: ValidatorSlashEvent + assertKeyAtLeastLength(key, 2) valAddrLen := int(key[1]) valAddr = key[2 : 2+valAddrLen] startB := 2 + valAddrLen + assertKeyAtLeastLength(key, startB+9) b := key[startB : startB+8] // the next 8 bytes represent the height height = binary.BigEndian.Uint64(b) return @@ -212,3 +217,9 @@ func GetValidatorSlashEventKey(v sdk.ValAddress, height, period uint64) []byte { return append(prefix, periodBz...) } + +func assertKeyAtLeastLength(bz []byte, length int) { + if len(bz) < length { + panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) + } +} diff --git a/x/gov/types/keys.go b/x/gov/types/keys.go index fe98bcbf81c4..46e004791e38 100644 --- a/x/gov/types/keys.go +++ b/x/gov/types/keys.go @@ -157,7 +157,14 @@ func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) { func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) { // Both Vote and Deposit store keys are of format: // + assertKeyAtLeastLength(key, 10) proposalID = GetProposalIDFromBytes(key[1:9]) addr = sdk.AccAddress(key[10:]) return } + +func assertKeyAtLeastLength(bz []byte, length int) { + if len(bz) < length { + panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) + } +} From f9f8ccc78979dd6b61a27d9705faa4c0f73da290 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Tue, 6 Jul 2021 20:11:06 +0530 Subject: [PATCH 02/10] check store keys length in all modules --- x/bank/migrations/v040/keys.go | 7 +++++++ x/distribution/migrations/v040/keys.go | 7 +++++++ x/distribution/types/keys.go | 7 +++++++ x/slashing/migrations/v040/keys.go | 8 ++++++++ x/staking/migrations/v040/keys.go | 9 +++++++++ x/staking/types/keys.go | 19 +++++++++++++++++++ 6 files changed, 57 insertions(+) diff --git a/x/bank/migrations/v040/keys.go b/x/bank/migrations/v040/keys.go index a5674f397438..cae34580b0d9 100644 --- a/x/bank/migrations/v040/keys.go +++ b/x/bank/migrations/v040/keys.go @@ -40,6 +40,7 @@ func DenomMetadataKey(denom string) []byte { // store. The key must not contain the perfix BalancesPrefix as the prefix store // iterator discards the actual prefix. func AddressFromBalancesStore(key []byte) sdk.AccAddress { + assertKeyAtLeastLength(key, 1+v040auth.AddrLen) addr := key[:v040auth.AddrLen] if len(addr) != v040auth.AddrLen { panic(fmt.Sprintf("unexpected account address key length; got: %d, expected: %d", len(addr), v040auth.AddrLen)) @@ -47,3 +48,9 @@ func AddressFromBalancesStore(key []byte) sdk.AccAddress { return sdk.AccAddress(addr) } + +func assertKeyAtLeastLength(bz []byte, length int) { + if len(bz) < length { + panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) + } +} diff --git a/x/distribution/migrations/v040/keys.go b/x/distribution/migrations/v040/keys.go index ea5235d004e3..916e84a7262c 100644 --- a/x/distribution/migrations/v040/keys.go +++ b/x/distribution/migrations/v040/keys.go @@ -59,6 +59,7 @@ var ( // gets an address from a validator's outstanding rewards key func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) { + assertKeyAtLeastLength(key, 2) addr := key[1:] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -68,6 +69,7 @@ func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) // gets an address from a delegator's withdraw info key func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { + assertKeyAtLeastLength(key, 2) addr := key[1:] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -77,6 +79,7 @@ func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { // gets the addresses from a delegator starting info key func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delAddr sdk.AccAddress) { + assertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -92,6 +95,7 @@ func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delA // gets the address & period from a validator's historical rewards key func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) { + assertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -107,6 +111,7 @@ func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddr // gets the address from a validator's current rewards key func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { + assertKeyAtLeastLength(key, 2) addr := key[1:] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -116,6 +121,7 @@ func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { // gets the address from a validator's accumulated commission key func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddress) { + assertKeyAtLeastLength(key, 2) addr := key[1:] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -125,6 +131,7 @@ func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddres // gets the height from a validator's slash event key func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) { + assertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] if len(addr) != v040auth.AddrLen { panic("unexpected key length") diff --git a/x/distribution/types/keys.go b/x/distribution/types/keys.go index b34b07ee3efd..4030ba971b09 100644 --- a/x/distribution/types/keys.go +++ b/x/distribution/types/keys.go @@ -61,6 +61,7 @@ func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) // 0x02 // Remove prefix and address length. + assertKeyAtLeastLength(key, 3) addr := key[2:] if len(addr) != int(key[1]) { panic("unexpected key length") @@ -75,6 +76,7 @@ func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { // 0x03 // Remove prefix and address length. + assertKeyAtLeastLength(key, 3) addr := key[2:] if len(addr) != int(key[1]) { panic("unexpected key length") @@ -90,6 +92,7 @@ func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delA assertKeyAtLeastLength(key, 2) valAddrLen := int(key[1]) valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) + assertKeyAtLeastLength(key, 3+valAddrLen) delAddrLen := int(key[2+valAddrLen]) delAddr = sdk.AccAddress(key[3+valAddrLen:]) if len(delAddr.Bytes()) != delAddrLen { @@ -106,6 +109,7 @@ func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddr assertKeyAtLeastLength(key, 2) valAddrLen := int(key[1]) valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) + assertKeyAtLeastLength(key, 3+valAddrLen) b := key[2+valAddrLen:] if len(b) != 8 { panic("unexpected key length") @@ -120,6 +124,7 @@ func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { // 0x06: ValidatorCurrentRewards // Remove prefix and address length. + assertKeyAtLeastLength(key, 3) addr := key[2:] if len(addr) != int(key[1]) { panic("unexpected key length") @@ -134,6 +139,7 @@ func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddres // 0x07: ValidatorCurrentRewards // Remove prefix and address length. + assertKeyAtLeastLength(key, 3) addr := key[2:] if len(addr) != int(key[1]) { panic("unexpected key length") @@ -148,6 +154,7 @@ func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, he // 0x08: ValidatorSlashEvent assertKeyAtLeastLength(key, 2) valAddrLen := int(key[1]) + assertKeyAtLeastLength(key, 3+valAddrLen) valAddr = key[2 : 2+valAddrLen] startB := 2 + valAddrLen assertKeyAtLeastLength(key, startB+9) diff --git a/x/slashing/migrations/v040/keys.go b/x/slashing/migrations/v040/keys.go index fe4cb52c5b83..771f2ccd0d6b 100644 --- a/x/slashing/migrations/v040/keys.go +++ b/x/slashing/migrations/v040/keys.go @@ -4,6 +4,7 @@ package v040 import ( "encoding/binary" + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" v040auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v040" @@ -44,6 +45,7 @@ func ValidatorSigningInfoKey(v sdk.ConsAddress) []byte { // ValidatorSigningInfoAddress - extract the address from a validator signing info key func ValidatorSigningInfoAddress(key []byte) (v sdk.ConsAddress) { + assertKeyAtLeastLength(key, 2) addr := key[1:] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -67,3 +69,9 @@ func ValidatorMissedBlockBitArrayKey(v sdk.ConsAddress, i int64) []byte { func AddrPubkeyRelationKey(address []byte) []byte { return append(AddrPubkeyRelationKeyPrefix, address...) } + +func assertKeyAtLeastLength(bz []byte, length int) { + if len(bz) < length { + panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) + } +} diff --git a/x/staking/migrations/v040/keys.go b/x/staking/migrations/v040/keys.go index e35ae83a3065..7b6e9896ecec 100644 --- a/x/staking/migrations/v040/keys.go +++ b/x/staking/migrations/v040/keys.go @@ -66,6 +66,7 @@ func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte { // Get the validator operator address from LastValidatorPowerKey func AddressFromLastValidatorPowerKey(key []byte) []byte { + assertKeyAtLeastLength(key, 2) return key[1:] // remove prefix bytes } @@ -196,11 +197,13 @@ func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte // rearranges the ValIndexKey to get the UBDKey func GetUBDKeyFromValIndexKey(indexKey []byte) []byte { + assertKeyAtLeastLength(indexKey, 2) addrs := indexKey[1:] // remove prefix bytes if len(addrs) != 2*v040auth.AddrLen { panic("unexpected key length") } + assertKeyAtLeastLength(addrs, v040auth.AddrLen+1) valAddr := addrs[:v040auth.AddrLen] delAddr := addrs[v040auth.AddrLen:] @@ -328,3 +331,9 @@ func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddr func GetHistoricalInfoKey(height int64) []byte { return append(HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) } + +func assertKeyAtLeastLength(bz []byte, length int) { + if len(bz) < length { + panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) + } +} diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 584dba5684c3..ae0feed34378 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -63,11 +63,13 @@ func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte { // AddressFromValidatorsKey creates the validator operator address from ValidatorsKey func AddressFromValidatorsKey(key []byte) []byte { + assertKeyAtLeastLength(key, 3) return key[2:] // remove prefix bytes and address length } // AddressFromLastValidatorPowerKey creates the validator operator address from LastValidatorPowerKey func AddressFromLastValidatorPowerKey(key []byte) []byte { + assertKeyAtLeastLength(key, 3) return key[2:] // remove prefix bytes and address length } @@ -196,10 +198,13 @@ func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte // GetUBDKeyFromValIndexKey rearranges the ValIndexKey to get the UBDKey func GetUBDKeyFromValIndexKey(indexKey []byte) []byte { + assertKeyAtLeastLength(indexKey, 2) addrs := indexKey[1:] // remove prefix bytes valAddrLen := addrs[0] + assertKeyAtLeastLength(addrs, 2+int(valAddrLen)) valAddr := addrs[1 : 1+valAddrLen] + assertKeyAtLeastLength(addrs, 3+int(valAddrLen)) delAddr := addrs[valAddrLen+2:] return GetUBDKey(delAddr, valAddr) @@ -273,12 +278,16 @@ func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.V // GetREDKeyFromValSrcIndexKey rearranges the ValSrcIndexKey to get the REDKey func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { // note that first byte is prefix byte, which we remove + assertKeyAtLeastLength(indexKey, 2) addrs := indexKey[1:] valSrcAddrLen := addrs[0] + assertKeyAtLeastLength(addrs, int(valSrcAddrLen)+2) valSrcAddr := addrs[1 : valSrcAddrLen+1] delAddrLen := addrs[valSrcAddrLen+1] + assertKeyAtLeastLength(addrs, int(valSrcAddrLen)+int(delAddrLen)+2) delAddr := addrs[valSrcAddrLen+2 : valSrcAddrLen+2+delAddrLen] + assertKeyAtLeastLength(addrs, int(valSrcAddrLen)+int(delAddrLen)+4) valDstAddr := addrs[valSrcAddrLen+delAddrLen+3:] return GetREDKey(delAddr, valSrcAddr, valDstAddr) @@ -287,12 +296,16 @@ func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { // GetREDKeyFromValDstIndexKey rearranges the ValDstIndexKey to get the REDKey func GetREDKeyFromValDstIndexKey(indexKey []byte) []byte { // note that first byte is prefix byte, which we remove + assertKeyAtLeastLength(indexKey, 2) addrs := indexKey[1:] valDstAddrLen := addrs[0] + assertKeyAtLeastLength(addrs, int(valDstAddrLen)+2) valDstAddr := addrs[1 : valDstAddrLen+1] delAddrLen := addrs[valDstAddrLen+1] + assertKeyAtLeastLength(addrs, int(valDstAddrLen)+int(delAddrLen)+3) delAddr := addrs[valDstAddrLen+2 : valDstAddrLen+2+delAddrLen] + assertKeyAtLeastLength(addrs, int(valDstAddrLen)+int(delAddrLen)+4) valSrcAddr := addrs[valDstAddrLen+delAddrLen+3:] return GetREDKey(delAddr, valSrcAddr, valDstAddr) @@ -333,3 +346,9 @@ func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddr func GetHistoricalInfoKey(height int64) []byte { return append(HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) } + +func assertKeyAtLeastLength(bz []byte, length int) { + if len(bz) < length { + panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) + } +} From ea2dc7fe06a24249c578d6977ec66468606a7dc8 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 7 Jul 2021 10:05:35 +0530 Subject: [PATCH 03/10] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 190615fb26fb..ed058a733366 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* [\#9639](https://github.com/cosmos/cosmos-sdk/pull/9639) Check store keys length before accessing them by making sure that `key` is of length `m+1` (for `a := key[n:m]`) * (types) [\#9627](https://github.com/cosmos/cosmos-sdk/pull/9627) Fix nil pointer panic on `NewBigIntFromInt` * (x/genutil) [\#9574](https://github.com/cosmos/cosmos-sdk/pull/9575) Actually use the `gentx` client tx flags (like `--keyring-dir`) * (x/distribution) [\#9599](https://github.com/cosmos/cosmos-sdk/pull/9599) Withdraw rewards event now includes a value attribute even if there are 0 rewards (due to situations like 100% commission). From c913d0ab7a788ba23b3b9504e2a90c6ea1f1fa15 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 7 Jul 2021 14:49:06 +0530 Subject: [PATCH 04/10] refactoring --- types/kv/helpers.go | 9 +++++++ x/authz/keeper/keys.go | 13 +++------- x/bank/migrations/v040/keys.go | 9 ++----- x/distribution/migrations/v040/keys.go | 24 +++++++------------ x/distribution/types/keys.go | 30 ++++++++++------------- x/gov/types/keys.go | 9 ++----- x/slashing/migrations/v040/keys.go | 10 ++------ x/staking/migrations/v040/keys.go | 13 ++++------ x/staking/types/keys.go | 33 +++++++++++--------------- 9 files changed, 57 insertions(+), 93 deletions(-) create mode 100644 types/kv/helpers.go diff --git a/types/kv/helpers.go b/types/kv/helpers.go new file mode 100644 index 000000000000..9158c4d162d3 --- /dev/null +++ b/types/kv/helpers.go @@ -0,0 +1,9 @@ +package kv + +import "fmt" + +func AssertKeyAtLeastLength(bz []byte, length int) { + if len(bz) < length { + panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) + } +} diff --git a/x/authz/keeper/keys.go b/x/authz/keeper/keys.go index c2bbe4a00016..d5c3b2964400 100644 --- a/x/authz/keeper/keys.go +++ b/x/authz/keeper/keys.go @@ -1,11 +1,10 @@ package keeper import ( - "fmt" - "github.com/cosmos/cosmos-sdk/internal/conv" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" + "github.com/cosmos/cosmos-sdk/types/kv" "github.com/cosmos/cosmos-sdk/x/authz" ) @@ -40,18 +39,12 @@ func grantStoreKey(grantee sdk.AccAddress, granter sdk.AccAddress, msgType strin func addressesFromGrantStoreKey(key []byte) (granterAddr, granteeAddr sdk.AccAddress) { // key is of format: // 0x01 - assertKeyAtLeastLength(key, 2) + kv.AssertKeyAtLeastLength(key, 2) granterAddrLen := key[1] // remove prefix key granterAddr = sdk.AccAddress(key[2 : 2+granterAddrLen]) - assertKeyAtLeastLength(key, int(3+granterAddrLen)) + kv.AssertKeyAtLeastLength(key, int(3+granterAddrLen)) granteeAddrLen := int(key[2+granterAddrLen]) granteeAddr = sdk.AccAddress(key[3+granterAddrLen : 3+granterAddrLen+byte(granteeAddrLen)]) return granterAddr, granteeAddr } - -func assertKeyAtLeastLength(bz []byte, length int) { - if len(bz) < length { - panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) - } -} diff --git a/x/bank/migrations/v040/keys.go b/x/bank/migrations/v040/keys.go index cae34580b0d9..582f9dc13b7e 100644 --- a/x/bank/migrations/v040/keys.go +++ b/x/bank/migrations/v040/keys.go @@ -6,6 +6,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/kv" v040auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v040" ) @@ -40,7 +41,7 @@ func DenomMetadataKey(denom string) []byte { // store. The key must not contain the perfix BalancesPrefix as the prefix store // iterator discards the actual prefix. func AddressFromBalancesStore(key []byte) sdk.AccAddress { - assertKeyAtLeastLength(key, 1+v040auth.AddrLen) + kv.AssertKeyAtLeastLength(key, 1+v040auth.AddrLen) addr := key[:v040auth.AddrLen] if len(addr) != v040auth.AddrLen { panic(fmt.Sprintf("unexpected account address key length; got: %d, expected: %d", len(addr), v040auth.AddrLen)) @@ -48,9 +49,3 @@ func AddressFromBalancesStore(key []byte) sdk.AccAddress { return sdk.AccAddress(addr) } - -func assertKeyAtLeastLength(bz []byte, length int) { - if len(bz) < length { - panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) - } -} diff --git a/x/distribution/migrations/v040/keys.go b/x/distribution/migrations/v040/keys.go index 916e84a7262c..b93b44c0795f 100644 --- a/x/distribution/migrations/v040/keys.go +++ b/x/distribution/migrations/v040/keys.go @@ -4,9 +4,9 @@ package v040 import ( "encoding/binary" - "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/kv" v040auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v040" ) @@ -59,7 +59,7 @@ var ( // gets an address from a validator's outstanding rewards key func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) { - assertKeyAtLeastLength(key, 2) + kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -69,7 +69,7 @@ func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) // gets an address from a delegator's withdraw info key func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { - assertKeyAtLeastLength(key, 2) + kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -79,7 +79,7 @@ func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { // gets the addresses from a delegator starting info key func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delAddr sdk.AccAddress) { - assertKeyAtLeastLength(key, 2+v040auth.AddrLen) + kv.AssertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -95,7 +95,7 @@ func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delA // gets the address & period from a validator's historical rewards key func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) { - assertKeyAtLeastLength(key, 2+v040auth.AddrLen) + kv.AssertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -111,7 +111,7 @@ func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddr // gets the address from a validator's current rewards key func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { - assertKeyAtLeastLength(key, 2) + kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -121,7 +121,7 @@ func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { // gets the address from a validator's accumulated commission key func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddress) { - assertKeyAtLeastLength(key, 2) + kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -131,14 +131,14 @@ func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddres // gets the height from a validator's slash event key func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) { - assertKeyAtLeastLength(key, 2+v040auth.AddrLen) + kv.AssertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] if len(addr) != v040auth.AddrLen { panic("unexpected key length") } valAddr = sdk.ValAddress(addr) startB := 1 + v040auth.AddrLen - assertKeyAtLeastLength(key, startB+9) + kv.AssertKeyAtLeastLength(key, startB+9) b := key[startB : startB+8] // the next 8 bytes represent the height height = binary.BigEndian.Uint64(b) return @@ -203,9 +203,3 @@ func GetValidatorSlashEventKey(v sdk.ValAddress, height, period uint64) []byte { prefix := GetValidatorSlashEventKeyPrefix(v, height) return append(prefix, periodBz...) } - -func assertKeyAtLeastLength(bz []byte, length int) { - if len(bz) < length { - panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) - } -} diff --git a/x/distribution/types/keys.go b/x/distribution/types/keys.go index 4030ba971b09..985d36b0575a 100644 --- a/x/distribution/types/keys.go +++ b/x/distribution/types/keys.go @@ -2,10 +2,10 @@ package types import ( "encoding/binary" - fmt "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" + "github.com/cosmos/cosmos-sdk/types/kv" ) const ( @@ -61,7 +61,7 @@ func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) // 0x02 // Remove prefix and address length. - assertKeyAtLeastLength(key, 3) + kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] if len(addr) != int(key[1]) { panic("unexpected key length") @@ -76,7 +76,7 @@ func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { // 0x03 // Remove prefix and address length. - assertKeyAtLeastLength(key, 3) + kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] if len(addr) != int(key[1]) { panic("unexpected key length") @@ -89,10 +89,10 @@ func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delAddr sdk.AccAddress) { // key is in the format: // 0x04 - assertKeyAtLeastLength(key, 2) + kv.AssertKeyAtLeastLength(key, 2) valAddrLen := int(key[1]) valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) - assertKeyAtLeastLength(key, 3+valAddrLen) + kv.AssertKeyAtLeastLength(key, 3+valAddrLen) delAddrLen := int(key[2+valAddrLen]) delAddr = sdk.AccAddress(key[3+valAddrLen:]) if len(delAddr.Bytes()) != delAddrLen { @@ -106,10 +106,10 @@ func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delA func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) { // key is in the format: // 0x05 - assertKeyAtLeastLength(key, 2) + kv.AssertKeyAtLeastLength(key, 2) valAddrLen := int(key[1]) valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) - assertKeyAtLeastLength(key, 3+valAddrLen) + kv.AssertKeyAtLeastLength(key, 3+valAddrLen) b := key[2+valAddrLen:] if len(b) != 8 { panic("unexpected key length") @@ -124,7 +124,7 @@ func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { // 0x06: ValidatorCurrentRewards // Remove prefix and address length. - assertKeyAtLeastLength(key, 3) + kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] if len(addr) != int(key[1]) { panic("unexpected key length") @@ -139,7 +139,7 @@ func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddres // 0x07: ValidatorCurrentRewards // Remove prefix and address length. - assertKeyAtLeastLength(key, 3) + kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] if len(addr) != int(key[1]) { panic("unexpected key length") @@ -152,12 +152,12 @@ func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddres func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) { // key is in the format: // 0x08: ValidatorSlashEvent - assertKeyAtLeastLength(key, 2) + kv.AssertKeyAtLeastLength(key, 2) valAddrLen := int(key[1]) - assertKeyAtLeastLength(key, 3+valAddrLen) + kv.AssertKeyAtLeastLength(key, 3+valAddrLen) valAddr = key[2 : 2+valAddrLen] startB := 2 + valAddrLen - assertKeyAtLeastLength(key, startB+9) + kv.AssertKeyAtLeastLength(key, startB+9) b := key[startB : startB+8] // the next 8 bytes represent the height height = binary.BigEndian.Uint64(b) return @@ -224,9 +224,3 @@ func GetValidatorSlashEventKey(v sdk.ValAddress, height, period uint64) []byte { return append(prefix, periodBz...) } - -func assertKeyAtLeastLength(bz []byte, length int) { - if len(bz) < length { - panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) - } -} diff --git a/x/gov/types/keys.go b/x/gov/types/keys.go index 46e004791e38..93f180033670 100644 --- a/x/gov/types/keys.go +++ b/x/gov/types/keys.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" + "github.com/cosmos/cosmos-sdk/types/kv" ) const ( @@ -157,14 +158,8 @@ func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) { func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) { // Both Vote and Deposit store keys are of format: // - assertKeyAtLeastLength(key, 10) + kv.AssertKeyAtLeastLength(key, 10) proposalID = GetProposalIDFromBytes(key[1:9]) addr = sdk.AccAddress(key[10:]) return } - -func assertKeyAtLeastLength(bz []byte, length int) { - if len(bz) < length { - panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) - } -} diff --git a/x/slashing/migrations/v040/keys.go b/x/slashing/migrations/v040/keys.go index 771f2ccd0d6b..c18785961bd8 100644 --- a/x/slashing/migrations/v040/keys.go +++ b/x/slashing/migrations/v040/keys.go @@ -4,9 +4,9 @@ package v040 import ( "encoding/binary" - "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/kv" v040auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v040" ) @@ -45,7 +45,7 @@ func ValidatorSigningInfoKey(v sdk.ConsAddress) []byte { // ValidatorSigningInfoAddress - extract the address from a validator signing info key func ValidatorSigningInfoAddress(key []byte) (v sdk.ConsAddress) { - assertKeyAtLeastLength(key, 2) + kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] if len(addr) != v040auth.AddrLen { panic("unexpected key length") @@ -69,9 +69,3 @@ func ValidatorMissedBlockBitArrayKey(v sdk.ConsAddress, i int64) []byte { func AddrPubkeyRelationKey(address []byte) []byte { return append(AddrPubkeyRelationKeyPrefix, address...) } - -func assertKeyAtLeastLength(bz []byte, length int) { - if len(bz) < length { - panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) - } -} diff --git a/x/staking/migrations/v040/keys.go b/x/staking/migrations/v040/keys.go index 7b6e9896ecec..80c573df1230 100644 --- a/x/staking/migrations/v040/keys.go +++ b/x/staking/migrations/v040/keys.go @@ -10,6 +10,7 @@ import ( "time" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/kv" v040auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v040" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -66,7 +67,7 @@ func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte { // Get the validator operator address from LastValidatorPowerKey func AddressFromLastValidatorPowerKey(key []byte) []byte { - assertKeyAtLeastLength(key, 2) + kv.AssertKeyAtLeastLength(key, 2) return key[1:] // remove prefix bytes } @@ -197,13 +198,13 @@ func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte // rearranges the ValIndexKey to get the UBDKey func GetUBDKeyFromValIndexKey(indexKey []byte) []byte { - assertKeyAtLeastLength(indexKey, 2) + kv.AssertKeyAtLeastLength(indexKey, 2) addrs := indexKey[1:] // remove prefix bytes if len(addrs) != 2*v040auth.AddrLen { panic("unexpected key length") } - assertKeyAtLeastLength(addrs, v040auth.AddrLen+1) + kv.AssertKeyAtLeastLength(addrs, v040auth.AddrLen+1) valAddr := addrs[:v040auth.AddrLen] delAddr := addrs[v040auth.AddrLen:] @@ -331,9 +332,3 @@ func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddr func GetHistoricalInfoKey(height int64) []byte { return append(HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) } - -func assertKeyAtLeastLength(bz []byte, length int) { - if len(bz) < length { - panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) - } -} diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index ae0feed34378..74d73bf19c7e 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -9,6 +9,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" + "github.com/cosmos/cosmos-sdk/types/kv" ) const ( @@ -63,13 +64,13 @@ func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte { // AddressFromValidatorsKey creates the validator operator address from ValidatorsKey func AddressFromValidatorsKey(key []byte) []byte { - assertKeyAtLeastLength(key, 3) + kv.AssertKeyAtLeastLength(key, 3) return key[2:] // remove prefix bytes and address length } // AddressFromLastValidatorPowerKey creates the validator operator address from LastValidatorPowerKey func AddressFromLastValidatorPowerKey(key []byte) []byte { - assertKeyAtLeastLength(key, 3) + kv.AssertKeyAtLeastLength(key, 3) return key[2:] // remove prefix bytes and address length } @@ -198,13 +199,13 @@ func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte // GetUBDKeyFromValIndexKey rearranges the ValIndexKey to get the UBDKey func GetUBDKeyFromValIndexKey(indexKey []byte) []byte { - assertKeyAtLeastLength(indexKey, 2) + kv.AssertKeyAtLeastLength(indexKey, 2) addrs := indexKey[1:] // remove prefix bytes valAddrLen := addrs[0] - assertKeyAtLeastLength(addrs, 2+int(valAddrLen)) + kv.AssertKeyAtLeastLength(addrs, 2+int(valAddrLen)) valAddr := addrs[1 : 1+valAddrLen] - assertKeyAtLeastLength(addrs, 3+int(valAddrLen)) + kv.AssertKeyAtLeastLength(addrs, 3+int(valAddrLen)) delAddr := addrs[valAddrLen+2:] return GetUBDKey(delAddr, valAddr) @@ -278,16 +279,16 @@ func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.V // GetREDKeyFromValSrcIndexKey rearranges the ValSrcIndexKey to get the REDKey func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { // note that first byte is prefix byte, which we remove - assertKeyAtLeastLength(indexKey, 2) + kv.AssertKeyAtLeastLength(indexKey, 2) addrs := indexKey[1:] valSrcAddrLen := addrs[0] - assertKeyAtLeastLength(addrs, int(valSrcAddrLen)+2) + kv.AssertKeyAtLeastLength(addrs, int(valSrcAddrLen)+2) valSrcAddr := addrs[1 : valSrcAddrLen+1] delAddrLen := addrs[valSrcAddrLen+1] - assertKeyAtLeastLength(addrs, int(valSrcAddrLen)+int(delAddrLen)+2) + kv.AssertKeyAtLeastLength(addrs, int(valSrcAddrLen)+int(delAddrLen)+2) delAddr := addrs[valSrcAddrLen+2 : valSrcAddrLen+2+delAddrLen] - assertKeyAtLeastLength(addrs, int(valSrcAddrLen)+int(delAddrLen)+4) + kv.AssertKeyAtLeastLength(addrs, int(valSrcAddrLen)+int(delAddrLen)+4) valDstAddr := addrs[valSrcAddrLen+delAddrLen+3:] return GetREDKey(delAddr, valSrcAddr, valDstAddr) @@ -296,16 +297,16 @@ func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { // GetREDKeyFromValDstIndexKey rearranges the ValDstIndexKey to get the REDKey func GetREDKeyFromValDstIndexKey(indexKey []byte) []byte { // note that first byte is prefix byte, which we remove - assertKeyAtLeastLength(indexKey, 2) + kv.AssertKeyAtLeastLength(indexKey, 2) addrs := indexKey[1:] valDstAddrLen := addrs[0] - assertKeyAtLeastLength(addrs, int(valDstAddrLen)+2) + kv.AssertKeyAtLeastLength(addrs, int(valDstAddrLen)+2) valDstAddr := addrs[1 : valDstAddrLen+1] delAddrLen := addrs[valDstAddrLen+1] - assertKeyAtLeastLength(addrs, int(valDstAddrLen)+int(delAddrLen)+3) + kv.AssertKeyAtLeastLength(addrs, int(valDstAddrLen)+int(delAddrLen)+3) delAddr := addrs[valDstAddrLen+2 : valDstAddrLen+2+delAddrLen] - assertKeyAtLeastLength(addrs, int(valDstAddrLen)+int(delAddrLen)+4) + kv.AssertKeyAtLeastLength(addrs, int(valDstAddrLen)+int(delAddrLen)+4) valSrcAddr := addrs[valDstAddrLen+delAddrLen+3:] return GetREDKey(delAddr, valSrcAddr, valDstAddr) @@ -346,9 +347,3 @@ func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddr func GetHistoricalInfoKey(height int64) []byte { return append(HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) } - -func assertKeyAtLeastLength(bz []byte, length int) { - if len(bz) < length { - panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) - } -} From d5271f0a50ef1b93fbbb4681cdb07988b931a1e9 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 7 Jul 2021 19:42:33 +0530 Subject: [PATCH 05/10] address review comments --- CHANGELOG.md | 4 +- types/kv/helpers.go | 7 +++ x/authz/keeper/keys.go | 3 +- x/bank/migrations/v040/keys.go | 10 ++-- x/distribution/migrations/v040/keys.go | 63 +++++++++++++++----------- x/distribution/types/keys.go | 47 +++++++++++-------- x/gov/migrations/v040/keys.go | 24 ++++++---- x/gov/types/keys.go | 16 ++++--- x/slashing/migrations/v040/keys.go | 7 +-- x/slashing/types/keys.go | 2 + x/staking/migrations/v040/keys.go | 28 +++++++----- 11 files changed, 123 insertions(+), 88 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50ba1dec765d..d8714bd05522 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,11 +70,11 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements -* (cli) [\#9593](https://github.com/cosmos/cosmos-sdk/pull/9593) Check if chain-id is blank before verifying signatures in multisign and error. +* (cli) [\#9593](https://github.com/cosmos/cosmos-sdk/pull/9593) Check if chain-id is blank before verifying signatures in multisign xxand error. ### Bug Fixes -* [\#9639](https://github.com/cosmos/cosmos-sdk/pull/9639) Check store keys length before accessing them by making sure that `key` is of length `m+1` (for `a := key[n:m]`) +* [\#9639](https://github.com/cosmos/cosmos-sdk/pull/9639) Check store keys length before accessing them by making sure that `key` is of length `m+1` (for `key[n:m]`) * (types) [\#9627](https://github.com/cosmos/cosmos-sdk/pull/9627) Fix nil pointer panic on `NewBigIntFromInt` * (x/genutil) [\#9574](https://github.com/cosmos/cosmos-sdk/pull/9575) Actually use the `gentx` client tx flags (like `--keyring-dir`) * (x/distribution) [\#9599](https://github.com/cosmos/cosmos-sdk/pull/9599) Withdraw rewards event now includes a value attribute even if there are 0 rewards (due to situations like 100% commission). diff --git a/types/kv/helpers.go b/types/kv/helpers.go index 9158c4d162d3..3f8a9e233e3e 100644 --- a/types/kv/helpers.go +++ b/types/kv/helpers.go @@ -7,3 +7,10 @@ func AssertKeyAtLeastLength(bz []byte, length int) { panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) } } + +func AssertKeyLength(addr []byte, length int) { + if len(addr) != length { + // panic(fmt.Sprintf("unexpected key length\nexpected key of length %d", length)) + panic(fmt.Sprintf("unexpected key length; got: %d, expected: %d", len(addr), length)) + } +} diff --git a/x/authz/keeper/keys.go b/x/authz/keeper/keys.go index d5c3b2964400..76ac447e37aa 100644 --- a/x/authz/keeper/keys.go +++ b/x/authz/keeper/keys.go @@ -41,9 +41,10 @@ func addressesFromGrantStoreKey(key []byte) (granterAddr, granteeAddr sdk.AccAdd // 0x01 kv.AssertKeyAtLeastLength(key, 2) granterAddrLen := key[1] // remove prefix key - granterAddr = sdk.AccAddress(key[2 : 2+granterAddrLen]) kv.AssertKeyAtLeastLength(key, int(3+granterAddrLen)) + granterAddr = sdk.AccAddress(key[2 : 2+granterAddrLen]) granteeAddrLen := int(key[2+granterAddrLen]) + kv.AssertKeyAtLeastLength(key, 4+int(granterAddrLen+byte(granteeAddrLen))) granteeAddr = sdk.AccAddress(key[3+granterAddrLen : 3+granterAddrLen+byte(granteeAddrLen)]) return granterAddr, granteeAddr diff --git a/x/bank/migrations/v040/keys.go b/x/bank/migrations/v040/keys.go index 582f9dc13b7e..e8b510e95905 100644 --- a/x/bank/migrations/v040/keys.go +++ b/x/bank/migrations/v040/keys.go @@ -3,8 +3,6 @@ package v040 import ( - "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" v040auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v040" @@ -43,9 +41,9 @@ func DenomMetadataKey(denom string) []byte { func AddressFromBalancesStore(key []byte) sdk.AccAddress { kv.AssertKeyAtLeastLength(key, 1+v040auth.AddrLen) addr := key[:v040auth.AddrLen] - if len(addr) != v040auth.AddrLen { - panic(fmt.Sprintf("unexpected account address key length; got: %d, expected: %d", len(addr), v040auth.AddrLen)) - } - + kv.AssertKeyLength(addr, v040auth.AddrLen) + // if len(addr) != v040auth.AddrLen { + // panic(fmt.Sprintf("unexpected account address key length; got: %d, expected: %d", len(addr), v040auth.AddrLen)) + // } return sdk.AccAddress(addr) } diff --git a/x/distribution/migrations/v040/keys.go b/x/distribution/migrations/v040/keys.go index b93b44c0795f..3f33220b3f12 100644 --- a/x/distribution/migrations/v040/keys.go +++ b/x/distribution/migrations/v040/keys.go @@ -61,9 +61,10 @@ var ( func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] - if len(addr) != v040auth.AddrLen { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, v040auth.AddrLen) + // if len(addr) != v040auth.AddrLen { + // panic("unexpected key length") + // } return sdk.ValAddress(addr) } @@ -71,9 +72,10 @@ func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] - if len(addr) != v040auth.AddrLen { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, v040auth.AddrLen) + // if len(addr) != v040auth.AddrLen { + // panic("unexpected key length") + // } return sdk.AccAddress(addr) } @@ -81,14 +83,16 @@ func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delAddr sdk.AccAddress) { kv.AssertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] - if len(addr) != v040auth.AddrLen { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, v040auth.AddrLen) + // if len(addr) != v040auth.AddrLen { + // panic("unexpected key length") + // } valAddr = sdk.ValAddress(addr) addr = key[1+v040auth.AddrLen:] - if len(addr) != v040auth.AddrLen { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, v040auth.AddrLen) + // if len(addr) != v040auth.AddrLen { + // panic("unexpected key length") + // } delAddr = sdk.AccAddress(addr) return } @@ -97,14 +101,16 @@ func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delA func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) { kv.AssertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] - if len(addr) != v040auth.AddrLen { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, v040auth.AddrLen) + // if len(addr) != v040auth.AddrLen { + // panic("unexpected key length") + // } valAddr = sdk.ValAddress(addr) b := key[1+v040auth.AddrLen:] - if len(b) != 8 { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, 8) + // if len(b) != 8 { + // panic("unexpected key length") + // } period = binary.LittleEndian.Uint64(b) return } @@ -113,9 +119,10 @@ func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddr func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] - if len(addr) != v040auth.AddrLen { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, v040auth.AddrLen) + // if len(addr) != v040auth.AddrLen { + // panic("unexpected key length") + // } return sdk.ValAddress(addr) } @@ -123,9 +130,10 @@ func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] - if len(addr) != v040auth.AddrLen { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, v040auth.AddrLen) + // if len(addr) != v040auth.AddrLen { + // panic("unexpected key length") + // } return sdk.ValAddress(addr) } @@ -133,9 +141,10 @@ func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddres func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) { kv.AssertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] - if len(addr) != v040auth.AddrLen { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, v040auth.AddrLen) + // if len(addr) != v040auth.AddrLen { + // panic("unexpected key length") + // } valAddr = sdk.ValAddress(addr) startB := 1 + v040auth.AddrLen kv.AssertKeyAtLeastLength(key, startB+9) diff --git a/x/distribution/types/keys.go b/x/distribution/types/keys.go index 985d36b0575a..03c7d1668706 100644 --- a/x/distribution/types/keys.go +++ b/x/distribution/types/keys.go @@ -63,9 +63,10 @@ func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) // Remove prefix and address length. kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] - if len(addr) != int(key[1]) { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, int(key[1])) + // if len(addr) != int(key[1]) { + // panic("unexpected key length") + // } return sdk.ValAddress(addr) } @@ -78,9 +79,10 @@ func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { // Remove prefix and address length. kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] - if len(addr) != int(key[1]) { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, int(key[1])) + // if len(addr) != int(key[1]) { + // panic("unexpected key length") + // } return sdk.AccAddress(addr) } @@ -91,13 +93,15 @@ func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delA // 0x04 kv.AssertKeyAtLeastLength(key, 2) valAddrLen := int(key[1]) - valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) kv.AssertKeyAtLeastLength(key, 3+valAddrLen) + valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) delAddrLen := int(key[2+valAddrLen]) + kv.AssertKeyAtLeastLength(key, 4+valAddrLen) delAddr = sdk.AccAddress(key[3+valAddrLen:]) - if len(delAddr.Bytes()) != delAddrLen { - panic("unexpected key length") - } + kv.AssertKeyLength(delAddr.Bytes(), delAddrLen) + // if len(delAddr.Bytes()) != delAddrLen { + // panic("unexpected key length") + // } return } @@ -108,12 +112,13 @@ func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddr // 0x05 kv.AssertKeyAtLeastLength(key, 2) valAddrLen := int(key[1]) - valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) kv.AssertKeyAtLeastLength(key, 3+valAddrLen) + valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) b := key[2+valAddrLen:] - if len(b) != 8 { - panic("unexpected key length") - } + kv.AssertKeyLength(b, 8) + // if len(b) != 8 { + // panic("unexpected key length") + // } period = binary.LittleEndian.Uint64(b) return } @@ -126,9 +131,10 @@ func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { // Remove prefix and address length. kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] - if len(addr) != int(key[1]) { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, int(key[1])) + // if len(addr) != int(key[1]) { + // panic("unexpected key length") + // } return sdk.ValAddress(addr) } @@ -141,9 +147,10 @@ func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddres // Remove prefix and address length. kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] - if len(addr) != int(key[1]) { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, int(key[1])) + // if len(addr) != int(key[1]) { + // panic("unexpected key length") + // } return sdk.ValAddress(addr) } diff --git a/x/gov/migrations/v040/keys.go b/x/gov/migrations/v040/keys.go index 8fe2e4413d8f..9c311fde3ea5 100644 --- a/x/gov/migrations/v040/keys.go +++ b/x/gov/migrations/v040/keys.go @@ -4,10 +4,10 @@ package v040 import ( "encoding/binary" - "fmt" "time" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/kv" v040auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v040" ) @@ -113,9 +113,10 @@ func VoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte { // 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:]))) - } + kv.AssertKeyLength(key[1:], 8) + // if len(key[1:]) != 8 { + // panic(fmt.Sprintf("unexpected key length (%d ≠ 8)", len(key[1:]))) + // } return GetProposalIDFromBytes(key[1:]) } @@ -143,9 +144,10 @@ func SplitKeyVote(key []byte) (proposalID uint64, voterAddr sdk.AccAddress) { // 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)) - } + kv.AssertKeyLength(key[1:], 8+lenTime) + // 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 { @@ -157,10 +159,12 @@ func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) { } func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) { - if len(key[1:]) != 8+v040auth.AddrLen { - panic(fmt.Sprintf("unexpected key length (%d ≠ %d)", len(key), 8+v040auth.AddrLen)) - } + kv.AssertKeyLength(key[1:], 8+v040auth.AddrLen) + // if len(key[1:]) != 8+v040auth.AddrLen { + // panic(fmt.Sprintf("unexpected key length (%d ≠ %d)", len(key), 8+v040auth.AddrLen)) + // } + kv.AssertKeyAtLeastLength(key, 10) proposalID = GetProposalIDFromBytes(key[1:9]) addr = sdk.AccAddress(key[9:]) return diff --git a/x/gov/types/keys.go b/x/gov/types/keys.go index 93f180033670..6145f29104b5 100644 --- a/x/gov/types/keys.go +++ b/x/gov/types/keys.go @@ -2,7 +2,6 @@ package types import ( "encoding/binary" - "fmt" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -112,9 +111,10 @@ func VoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte { // 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:]))) - } + kv.AssertKeyLength(key[1:], 8) + // if len(key[1:]) != 8 { + // panic(fmt.Sprintf("unexpected key length (%d ≠ 8)", len(key[1:]))) + // } return GetProposalIDFromBytes(key[1:]) } @@ -142,9 +142,10 @@ func SplitKeyVote(key []byte) (proposalID uint64, voterAddr sdk.AccAddress) { // 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)) - } + kv.AssertKeyLength(key[1:], 8+lenTime) + // 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 { @@ -160,6 +161,7 @@ func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) { // kv.AssertKeyAtLeastLength(key, 10) proposalID = GetProposalIDFromBytes(key[1:9]) + kv.AssertKeyAtLeastLength(key, 11) addr = sdk.AccAddress(key[10:]) return } diff --git a/x/slashing/migrations/v040/keys.go b/x/slashing/migrations/v040/keys.go index c18785961bd8..0ee7e3e9cfee 100644 --- a/x/slashing/migrations/v040/keys.go +++ b/x/slashing/migrations/v040/keys.go @@ -47,9 +47,10 @@ func ValidatorSigningInfoKey(v sdk.ConsAddress) []byte { func ValidatorSigningInfoAddress(key []byte) (v sdk.ConsAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] - if len(addr) != v040auth.AddrLen { - panic("unexpected key length") - } + kv.AssertKeyLength(addr, v040auth.AddrLen) + // if len(addr) != v040auth.AddrLen { + // panic("unexpected key length") + // } return sdk.ConsAddress(addr) } diff --git a/x/slashing/types/keys.go b/x/slashing/types/keys.go index f0049760f0a0..09c978c9a4d5 100644 --- a/x/slashing/types/keys.go +++ b/x/slashing/types/keys.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" + "github.com/cosmos/cosmos-sdk/types/kv" ) const ( @@ -43,6 +44,7 @@ func ValidatorSigningInfoKey(v sdk.ConsAddress) []byte { // ValidatorSigningInfoAddress - extract the address from a validator signing info key func ValidatorSigningInfoAddress(key []byte) (v sdk.ConsAddress) { // Remove prefix and address length. + kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] return sdk.ConsAddress(addr) diff --git a/x/staking/migrations/v040/keys.go b/x/staking/migrations/v040/keys.go index 80c573df1230..e846468fa0fe 100644 --- a/x/staking/migrations/v040/keys.go +++ b/x/staking/migrations/v040/keys.go @@ -114,9 +114,10 @@ func GetLastValidatorPowerKey(operator sdk.ValAddress) []byte { // parse the validators operator address from power rank key func ParseValidatorPowerRankKey(key []byte) (operAddr []byte) { powerBytesLen := 8 - if len(key) != 1+powerBytesLen+v040auth.AddrLen { - panic("Invalid validator power rank key length") - } + kv.AssertKeyLength(key, 1+powerBytesLen+v040auth.AddrLen) + // if len(key) != 1+powerBytesLen+v040auth.AddrLen { + // panic("Invalid validator power rank key length") + // } operAddr = sdk.CopyBytes(key[powerBytesLen+1:]) @@ -200,9 +201,10 @@ func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte func GetUBDKeyFromValIndexKey(indexKey []byte) []byte { kv.AssertKeyAtLeastLength(indexKey, 2) addrs := indexKey[1:] // remove prefix bytes - if len(addrs) != 2*v040auth.AddrLen { - panic("unexpected key length") - } + kv.AssertKeyLength(addrs, 2*v040auth.AddrLen) + // if len(addrs) != 2*v040auth.AddrLen { + // panic("unexpected key length") + // } kv.AssertKeyAtLeastLength(addrs, v040auth.AddrLen+1) valAddr := addrs[:v040auth.AddrLen] @@ -272,9 +274,10 @@ func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.V // GetREDKeyFromValSrcIndexKey rearranges the ValSrcIndexKey to get the REDKey func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { // note that first byte is prefix byte - if len(indexKey) != 3*v040auth.AddrLen+1 { - panic("unexpected key length") - } + kv.AssertKeyLength(indexKey, 3*v040auth.AddrLen+1) + // if len(indexKey) != 3*v040auth.AddrLen+1 { + // panic("unexpected key length") + // } valSrcAddr := indexKey[1 : v040auth.AddrLen+1] delAddr := indexKey[v040auth.AddrLen+1 : 2*v040auth.AddrLen+1] @@ -286,9 +289,10 @@ func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { // GetREDKeyFromValDstIndexKey rearranges the ValDstIndexKey to get the REDKey func GetREDKeyFromValDstIndexKey(indexKey []byte) []byte { // note that first byte is prefix byte - if len(indexKey) != 3*v040auth.AddrLen+1 { - panic("unexpected key length") - } + kv.AssertKeyLength(indexKey, 3*v040auth.AddrLen+1) + // if len(indexKey) != 3*v040auth.AddrLen+1 { + // panic("unexpected key length") + // } valDstAddr := indexKey[1 : v040auth.AddrLen+1] delAddr := indexKey[v040auth.AddrLen+1 : 2*v040auth.AddrLen+1] From 2a4e1d6376a2799d9733706cdc1d3bf39869b86d Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 7 Jul 2021 20:13:44 +0530 Subject: [PATCH 06/10] remove commented lines --- x/bank/migrations/v040/keys.go | 3 --- x/distribution/migrations/v040/keys.go | 27 -------------------------- x/distribution/types/keys.go | 18 ----------------- x/gov/migrations/v040/keys.go | 9 --------- x/gov/types/keys.go | 6 ------ x/slashing/migrations/v040/keys.go | 3 --- x/staking/migrations/v040/keys.go | 12 ------------ 7 files changed, 78 deletions(-) diff --git a/x/bank/migrations/v040/keys.go b/x/bank/migrations/v040/keys.go index e8b510e95905..38bb49a0032d 100644 --- a/x/bank/migrations/v040/keys.go +++ b/x/bank/migrations/v040/keys.go @@ -42,8 +42,5 @@ func AddressFromBalancesStore(key []byte) sdk.AccAddress { kv.AssertKeyAtLeastLength(key, 1+v040auth.AddrLen) addr := key[:v040auth.AddrLen] kv.AssertKeyLength(addr, v040auth.AddrLen) - // if len(addr) != v040auth.AddrLen { - // panic(fmt.Sprintf("unexpected account address key length; got: %d, expected: %d", len(addr), v040auth.AddrLen)) - // } return sdk.AccAddress(addr) } diff --git a/x/distribution/migrations/v040/keys.go b/x/distribution/migrations/v040/keys.go index 3f33220b3f12..db8b1548a343 100644 --- a/x/distribution/migrations/v040/keys.go +++ b/x/distribution/migrations/v040/keys.go @@ -62,9 +62,6 @@ func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] kv.AssertKeyLength(addr, v040auth.AddrLen) - // if len(addr) != v040auth.AddrLen { - // panic("unexpected key length") - // } return sdk.ValAddress(addr) } @@ -73,9 +70,6 @@ func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] kv.AssertKeyLength(addr, v040auth.AddrLen) - // if len(addr) != v040auth.AddrLen { - // panic("unexpected key length") - // } return sdk.AccAddress(addr) } @@ -84,15 +78,9 @@ func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delA kv.AssertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] kv.AssertKeyLength(addr, v040auth.AddrLen) - // if len(addr) != v040auth.AddrLen { - // panic("unexpected key length") - // } valAddr = sdk.ValAddress(addr) addr = key[1+v040auth.AddrLen:] kv.AssertKeyLength(addr, v040auth.AddrLen) - // if len(addr) != v040auth.AddrLen { - // panic("unexpected key length") - // } delAddr = sdk.AccAddress(addr) return } @@ -102,15 +90,9 @@ func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddr kv.AssertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] kv.AssertKeyLength(addr, v040auth.AddrLen) - // if len(addr) != v040auth.AddrLen { - // panic("unexpected key length") - // } valAddr = sdk.ValAddress(addr) b := key[1+v040auth.AddrLen:] kv.AssertKeyLength(addr, 8) - // if len(b) != 8 { - // panic("unexpected key length") - // } period = binary.LittleEndian.Uint64(b) return } @@ -120,9 +102,6 @@ func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] kv.AssertKeyLength(addr, v040auth.AddrLen) - // if len(addr) != v040auth.AddrLen { - // panic("unexpected key length") - // } return sdk.ValAddress(addr) } @@ -131,9 +110,6 @@ func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddres kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] kv.AssertKeyLength(addr, v040auth.AddrLen) - // if len(addr) != v040auth.AddrLen { - // panic("unexpected key length") - // } return sdk.ValAddress(addr) } @@ -142,9 +118,6 @@ func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, he kv.AssertKeyAtLeastLength(key, 2+v040auth.AddrLen) addr := key[1 : 1+v040auth.AddrLen] kv.AssertKeyLength(addr, v040auth.AddrLen) - // if len(addr) != v040auth.AddrLen { - // panic("unexpected key length") - // } valAddr = sdk.ValAddress(addr) startB := 1 + v040auth.AddrLen kv.AssertKeyAtLeastLength(key, startB+9) diff --git a/x/distribution/types/keys.go b/x/distribution/types/keys.go index 03c7d1668706..e0458459d080 100644 --- a/x/distribution/types/keys.go +++ b/x/distribution/types/keys.go @@ -64,9 +64,6 @@ func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] kv.AssertKeyLength(addr, int(key[1])) - // if len(addr) != int(key[1]) { - // panic("unexpected key length") - // } return sdk.ValAddress(addr) } @@ -80,9 +77,6 @@ func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] kv.AssertKeyLength(addr, int(key[1])) - // if len(addr) != int(key[1]) { - // panic("unexpected key length") - // } return sdk.AccAddress(addr) } @@ -99,9 +93,6 @@ func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delA kv.AssertKeyAtLeastLength(key, 4+valAddrLen) delAddr = sdk.AccAddress(key[3+valAddrLen:]) kv.AssertKeyLength(delAddr.Bytes(), delAddrLen) - // if len(delAddr.Bytes()) != delAddrLen { - // panic("unexpected key length") - // } return } @@ -116,9 +107,6 @@ func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddr valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) b := key[2+valAddrLen:] kv.AssertKeyLength(b, 8) - // if len(b) != 8 { - // panic("unexpected key length") - // } period = binary.LittleEndian.Uint64(b) return } @@ -132,9 +120,6 @@ func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] kv.AssertKeyLength(addr, int(key[1])) - // if len(addr) != int(key[1]) { - // panic("unexpected key length") - // } return sdk.ValAddress(addr) } @@ -148,9 +133,6 @@ func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddres kv.AssertKeyAtLeastLength(key, 3) addr := key[2:] kv.AssertKeyLength(addr, int(key[1])) - // if len(addr) != int(key[1]) { - // panic("unexpected key length") - // } return sdk.ValAddress(addr) } diff --git a/x/gov/migrations/v040/keys.go b/x/gov/migrations/v040/keys.go index 9c311fde3ea5..5b8a6536cafa 100644 --- a/x/gov/migrations/v040/keys.go +++ b/x/gov/migrations/v040/keys.go @@ -114,9 +114,6 @@ func VoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte { // SplitProposalKey split the proposal key and returns the proposal id func SplitProposalKey(key []byte) (proposalID uint64) { kv.AssertKeyLength(key[1:], 8) - // if len(key[1:]) != 8 { - // panic(fmt.Sprintf("unexpected key length (%d ≠ 8)", len(key[1:]))) - // } return GetProposalIDFromBytes(key[1:]) } @@ -145,9 +142,6 @@ func SplitKeyVote(key []byte) (proposalID uint64, voterAddr sdk.AccAddress) { func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) { kv.AssertKeyLength(key[1:], 8+lenTime) - // 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 { @@ -160,9 +154,6 @@ func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) { func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) { kv.AssertKeyLength(key[1:], 8+v040auth.AddrLen) - // if len(key[1:]) != 8+v040auth.AddrLen { - // panic(fmt.Sprintf("unexpected key length (%d ≠ %d)", len(key), 8+v040auth.AddrLen)) - // } kv.AssertKeyAtLeastLength(key, 10) proposalID = GetProposalIDFromBytes(key[1:9]) diff --git a/x/gov/types/keys.go b/x/gov/types/keys.go index 6145f29104b5..7f58d8fdfbac 100644 --- a/x/gov/types/keys.go +++ b/x/gov/types/keys.go @@ -112,9 +112,6 @@ func VoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte { // SplitProposalKey split the proposal key and returns the proposal id func SplitProposalKey(key []byte) (proposalID uint64) { kv.AssertKeyLength(key[1:], 8) - // if len(key[1:]) != 8 { - // panic(fmt.Sprintf("unexpected key length (%d ≠ 8)", len(key[1:]))) - // } return GetProposalIDFromBytes(key[1:]) } @@ -143,9 +140,6 @@ func SplitKeyVote(key []byte) (proposalID uint64, voterAddr sdk.AccAddress) { func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) { kv.AssertKeyLength(key[1:], 8+lenTime) - // 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 { diff --git a/x/slashing/migrations/v040/keys.go b/x/slashing/migrations/v040/keys.go index 0ee7e3e9cfee..367ead65e5cc 100644 --- a/x/slashing/migrations/v040/keys.go +++ b/x/slashing/migrations/v040/keys.go @@ -48,9 +48,6 @@ func ValidatorSigningInfoAddress(key []byte) (v sdk.ConsAddress) { kv.AssertKeyAtLeastLength(key, 2) addr := key[1:] kv.AssertKeyLength(addr, v040auth.AddrLen) - // if len(addr) != v040auth.AddrLen { - // panic("unexpected key length") - // } return sdk.ConsAddress(addr) } diff --git a/x/staking/migrations/v040/keys.go b/x/staking/migrations/v040/keys.go index e846468fa0fe..27e2f9eae02b 100644 --- a/x/staking/migrations/v040/keys.go +++ b/x/staking/migrations/v040/keys.go @@ -115,9 +115,6 @@ func GetLastValidatorPowerKey(operator sdk.ValAddress) []byte { func ParseValidatorPowerRankKey(key []byte) (operAddr []byte) { powerBytesLen := 8 kv.AssertKeyLength(key, 1+powerBytesLen+v040auth.AddrLen) - // if len(key) != 1+powerBytesLen+v040auth.AddrLen { - // panic("Invalid validator power rank key length") - // } operAddr = sdk.CopyBytes(key[powerBytesLen+1:]) @@ -202,9 +199,6 @@ func GetUBDKeyFromValIndexKey(indexKey []byte) []byte { kv.AssertKeyAtLeastLength(indexKey, 2) addrs := indexKey[1:] // remove prefix bytes kv.AssertKeyLength(addrs, 2*v040auth.AddrLen) - // if len(addrs) != 2*v040auth.AddrLen { - // panic("unexpected key length") - // } kv.AssertKeyAtLeastLength(addrs, v040auth.AddrLen+1) valAddr := addrs[:v040auth.AddrLen] @@ -275,9 +269,6 @@ func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.V func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { // note that first byte is prefix byte kv.AssertKeyLength(indexKey, 3*v040auth.AddrLen+1) - // if len(indexKey) != 3*v040auth.AddrLen+1 { - // panic("unexpected key length") - // } valSrcAddr := indexKey[1 : v040auth.AddrLen+1] delAddr := indexKey[v040auth.AddrLen+1 : 2*v040auth.AddrLen+1] @@ -290,9 +281,6 @@ func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { func GetREDKeyFromValDstIndexKey(indexKey []byte) []byte { // note that first byte is prefix byte kv.AssertKeyLength(indexKey, 3*v040auth.AddrLen+1) - // if len(indexKey) != 3*v040auth.AddrLen+1 { - // panic("unexpected key length") - // } valDstAddr := indexKey[1 : v040auth.AddrLen+1] delAddr := indexKey[v040auth.AddrLen+1 : 2*v040auth.AddrLen+1] From 95978821776c21fa66e4797b1d8c0d59448c2482 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 7 Jul 2021 20:23:37 +0530 Subject: [PATCH 07/10] small fixes --- CHANGELOG.md | 2 +- types/kv/helpers.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8714bd05522..837de46c86a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,7 +70,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements -* (cli) [\#9593](https://github.com/cosmos/cosmos-sdk/pull/9593) Check if chain-id is blank before verifying signatures in multisign xxand error. +* (cli) [\#9593](https://github.com/cosmos/cosmos-sdk/pull/9593) Check if chain-id is blank before verifying signatures in multisign and error. ### Bug Fixes diff --git a/types/kv/helpers.go b/types/kv/helpers.go index 3f8a9e233e3e..2eabb093b54a 100644 --- a/types/kv/helpers.go +++ b/types/kv/helpers.go @@ -10,7 +10,6 @@ func AssertKeyAtLeastLength(bz []byte, length int) { func AssertKeyLength(addr []byte, length int) { if len(addr) != length { - // panic(fmt.Sprintf("unexpected key length\nexpected key of length %d", length)) panic(fmt.Sprintf("unexpected key length; got: %d, expected: %d", len(addr), length)) } } From cd8e279fc2728a4517177a7542633ccbc331a1fb Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 8 Jul 2021 15:29:53 +0530 Subject: [PATCH 08/10] address review comments --- types/kv/helpers.go | 6 +++--- x/bank/types/key.go | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/types/kv/helpers.go b/types/kv/helpers.go index 2eabb093b54a..582c9f31077a 100644 --- a/types/kv/helpers.go +++ b/types/kv/helpers.go @@ -8,8 +8,8 @@ func AssertKeyAtLeastLength(bz []byte, length int) { } } -func AssertKeyLength(addr []byte, length int) { - if len(addr) != length { - panic(fmt.Sprintf("unexpected key length; got: %d, expected: %d", len(addr), length)) +func AssertKeyLength(bz []byte, length int) { + if len(bz) != length { + panic(fmt.Sprintf("unexpected key length; got: %d, expected: %d", len(bz), length)) } } diff --git a/x/bank/types/key.go b/x/bank/types/key.go index 5d7f52baa531..13d0471ff75e 100644 --- a/x/bank/types/key.go +++ b/x/bank/types/key.go @@ -3,6 +3,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" + "github.com/cosmos/cosmos-sdk/types/kv" ) const ( @@ -43,8 +44,10 @@ func AddressFromBalancesStore(key []byte) (sdk.AccAddress, error) { if len(key) == 0 { return nil, ErrInvalidKey } + kv.AssertKeyAtLeastLength(key, 1) addrLen := key[0] bound := int(addrLen) + kv.AssertKeyAtLeastLength(key, bound+2) if len(key)-1 < bound { return nil, ErrInvalidKey } From c2b75898ba02085ff0d3b72f689aa416f6000942 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 8 Jul 2021 16:10:50 +0530 Subject: [PATCH 09/10] fix failing tests --- x/bank/types/key.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/bank/types/key.go b/x/bank/types/key.go index 13d0471ff75e..e91a38970e8b 100644 --- a/x/bank/types/key.go +++ b/x/bank/types/key.go @@ -47,7 +47,6 @@ func AddressFromBalancesStore(key []byte) (sdk.AccAddress, error) { kv.AssertKeyAtLeastLength(key, 1) addrLen := key[0] bound := int(addrLen) - kv.AssertKeyAtLeastLength(key, bound+2) if len(key)-1 < bound { return nil, ErrInvalidKey } From 8b6e32e7c6c706227ab2a1529767623a88208bcc Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 8 Jul 2021 16:36:41 +0530 Subject: [PATCH 10/10] add godocs --- types/kv/helpers.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/kv/helpers.go b/types/kv/helpers.go index 582c9f31077a..5bccea122eb5 100644 --- a/types/kv/helpers.go +++ b/types/kv/helpers.go @@ -2,12 +2,14 @@ package kv import "fmt" +// AssertKeyAtLeastLength panics when store key length is less than the given length. func AssertKeyAtLeastLength(bz []byte, length int) { if len(bz) < length { panic(fmt.Sprintf("expected key of length at least %d, got %d", length, len(bz))) } } +// AssertKeyLength panics when store key length is not equal to the given length. func AssertKeyLength(bz []byte, length int) { if len(bz) != length { panic(fmt.Sprintf("unexpected key length; got: %d, expected: %d", len(bz), length))