Skip to content

Commit 714a7de

Browse files
committed
standardize keys prefix, remove append
1 parent 422a21c commit 714a7de

File tree

2 files changed

+61
-19
lines changed

2 files changed

+61
-19
lines changed

x/feeds/types/keys.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@ const (
2323

2424
// Constants for keys
2525
var (
26-
GlobalStoreKeyPrefix = []byte{0x00}
26+
// global store keys
27+
ReferenceSourceConfigStoreKey = []byte{0x00}
28+
CurrentFeedsStoreKey = []byte{0x01}
2729

28-
ReferenceSourceConfigStoreKey = append(GlobalStoreKeyPrefix, []byte("ReferenceSourceConfig")...)
29-
CurrentFeedsStoreKey = append(GlobalStoreKeyPrefix, []byte("CurrentFeeds")...)
30+
// store prefixes
31+
ValidatorPriceListStoreKeyPrefix = []byte{0x10}
32+
PriceStoreKeyPrefix = []byte{0x11}
33+
DelegatorSignalsStoreKeyPrefix = []byte{0x12}
34+
SignalTotalPowerStoreKeyPrefix = []byte{0x13}
3035

31-
ValidatorPriceListStoreKeyPrefix = []byte{0x01}
32-
PriceStoreKeyPrefix = []byte{0x02}
33-
DelegatorSignalsStoreKeyPrefix = []byte{0x03}
34-
SignalTotalPowerStoreKeyPrefix = []byte{0x04}
36+
// index prefixes
37+
SignalTotalPowerByPowerIndexKeyPrefix = []byte{0x80}
3538

36-
ParamsKey = []byte{0x10}
37-
38-
SignalTotalPowerByPowerIndexKeyPrefix = []byte{0x20}
39+
// params store keys
40+
ParamsKey = []byte{0x90}
3941
)
4042

4143
// DelegatorSignalsStoreKey creates a key for storing delegator signals

x/feeds/types/keys_test.go

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,63 @@ import (
88
"github.com/stretchr/testify/require"
99
)
1010

11+
func TestValidatorPriceListStoreKey(t *testing.T) {
12+
// Prefix: 0x10
13+
expect, _ := hex.DecodeString("100a31303030303030303031")
14+
require.Equal(t, expect, ValidatorPriceListStoreKey(sdk.ValAddress("1000000001")))
15+
16+
// Test with empty validator address
17+
emptyVal := sdk.ValAddress{}
18+
expectEmpty, _ := hex.DecodeString("10")
19+
require.Equal(t, expectEmpty, ValidatorPriceListStoreKey(emptyVal))
20+
}
21+
22+
func TestPriceStoreKey(t *testing.T) {
23+
// Prefix: 0x11
24+
expect, _ := hex.DecodeString("1142414e44")
25+
require.Equal(t, expect, PriceStoreKey("BAND"))
26+
27+
// Test with empty string
28+
expectEmpty, _ := hex.DecodeString("11")
29+
require.Equal(t, expectEmpty, PriceStoreKey(""))
30+
}
31+
1132
func TestDelegatorSignalsStoreKey(t *testing.T) {
33+
// Prefix: 0x12
1234
acc, _ := sdk.AccAddressFromHexUnsafe("b80f2a5df7d5710b15622d1a9f1e3830ded5bda8")
13-
expect, _ := hex.DecodeString("0314b80f2a5df7d5710b15622d1a9f1e3830ded5bda8")
35+
expect, _ := hex.DecodeString("1214b80f2a5df7d5710b15622d1a9f1e3830ded5bda8")
1436
require.Equal(t, expect, DelegatorSignalsStoreKey(acc))
37+
38+
// Test with empty address
39+
emptyAcc := sdk.AccAddress{}
40+
expectEmpty, _ := hex.DecodeString("12")
41+
require.Equal(t, expectEmpty, DelegatorSignalsStoreKey(emptyAcc))
1542
}
1643

1744
func TestSignalTotalPowerStoreKey(t *testing.T) {
18-
expect, _ := hex.DecodeString("0442414e44")
45+
// Prefix: 0x13
46+
expect, _ := hex.DecodeString("1342414e44")
1947
require.Equal(t, expect, SignalTotalPowerStoreKey("BAND"))
20-
}
2148

22-
func TestValidatorPriceListStoreKey(t *testing.T) {
23-
expect, _ := hex.DecodeString("010a31303030303030303031")
24-
require.Equal(t, expect, ValidatorPriceListStoreKey(sdk.ValAddress("1000000001")))
49+
// Test with empty string
50+
expectEmpty, _ := hex.DecodeString("13")
51+
require.Equal(t, expectEmpty, SignalTotalPowerStoreKey(""))
2552
}
2653

27-
func TestPriceStoreKey(t *testing.T) {
28-
expect, _ := hex.DecodeString("0242414e44")
29-
require.Equal(t, expect, PriceStoreKey("BAND"))
54+
func TestSignalTotalPowerByPowerIndexKey(t *testing.T) {
55+
// Prefix: 0x80
56+
// Test with signal ID "BAND" and power 100
57+
expect, _ := hex.DecodeString(
58+
"80000000000000006404bdbeb1bb",
59+
) // Signal ID "BAND" with power 100, Signal ID bytes negated
60+
require.Equal(t, expect, SignalTotalPowerByPowerIndexKey("BAND", 100))
61+
62+
// Test with empty signal ID and zero power
63+
expectEmpty, _ := hex.DecodeString("80000000000000000000")
64+
require.Equal(t, expectEmpty, SignalTotalPowerByPowerIndexKey("", 0))
65+
66+
// Test with empty signal ID and large power
67+
largePower := int64(9223372036854775807) // Max int64
68+
expectLargePower, _ := hex.DecodeString("807fffffffffffffff00")
69+
require.Equal(t, expectLargePower, SignalTotalPowerByPowerIndexKey("", largePower))
3070
}

0 commit comments

Comments
 (0)