-
Notifications
You must be signed in to change notification settings - Fork 5
/
keys.go
178 lines (135 loc) · 5.54 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package types
import (
"bytes"
time "time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
// ModuleName defines the module name.
ModuleName = "incentives"
// StoreKey defines the primary module store key.
StoreKey = ModuleName
// RouterKey is the message route for slashing.
RouterKey = ModuleName
// QuerierRoute defines the module's query routing key.
QuerierRoute = ModuleName
// MemStoreKey defines the in-memory store key.
MemStoreKey = "mem_capability"
// KeyPrefixTimestamp defines prefix key for timestamp iterator key.
KeyPrefixTimestamp = []byte{0x01}
// KeyLastGaugeID defines key for setting last gauge ID.
KeyLastGaugeID = []byte{0x02}
// KeyPrefixGauge defines prefix key for storing gauges.
KeyPrefixGauge = []byte{0x03}
// KeyPrefixGaugeIndex defines prefix key for storing reference key for all gauges.
KeyPrefixGaugeIndex = []byte{0x04}
// KeyPrefixGaugeIndexUpcoming defines prefix key for storing reference key for upcoming gauges.
KeyPrefixGaugeIndexUpcoming = []byte{0x04, 0x00}
// KeyPrefixGaugeIndexActive defines prefix key for storing reference key for active gauges.
KeyPrefixGaugeIndexActive = []byte{0x04, 0x01}
// KeyPrefixGaugeIndexFinished defines prefix key for storing reference key for finished gauges.
KeyPrefixGaugeIndexFinished = []byte{0x04, 0x02}
// KeyPrefixGaugeIndexByPair defines prefix key for storing indexes of gauge IDs by denomination.
KeyPrefixGaugeIndexByPair = []byte{0x05}
// KeyLastStakeID defines key to store stake ID used by last.
KeyLastStakeID = []byte{0x06}
// KeyPrefixStake defines prefix to store period stake by ID.
KeyPrefixStake = []byte{0x07}
// KeyPrefixStakeIndexAccount defines prefix for the iteration of stake IDs by account.
KeyPrefixStakeIndex = []byte{0x08}
// KeyPrefixStakeIndexAccount defines prefix for the iteration of stake IDs by account.
KeyPrefixStakeIndexAccount = []byte{0x09}
// KeyPrefixStakeIndexDenom defines prefix for the iteration of stake IDs by denom.
KeyPrefixStakeIndexDenom = []byte{0x0c}
// KeyPrefixStakeIndexPairTick defines prefix for the iteration of stake IDs by pairId and tick index.
KeyPrefixStakeIndexPairTick = []byte{0x0d}
// KeyPrefixStakeIndexAccountDenom defines prefix for the iteration of stake IDs by account, denomination.
KeyPrefixStakeIndexAccountDenom = []byte{0x0e}
// KeyPrefixStakeIndexTimestamp defines prefix for the iteration of stake IDs by timestamp.
KeyPrefixStakeIndexPairTimestamp = []byte{0x0f}
// // KeyPrefixStakeIndexAccountTimestamp defines prefix for the iteration of stake IDs by account and timestamp.
// KeyPrefixStakeIndexAccountTimestamp = []byte{0x10}
// // KeyPrefixStakeIndexDenomTimestamp defines prefix for the iteration of stake IDs by denom and timestamp.
// KeyPrefixStakeIndexDenomTimestamp = []byte{0x11}
// // KeyPrefixStakeIndexAccountDenomTimestamp defines prefix for the iteration of stake IDs by account, denomination and timestamp.
// KeyPrefixStakeIndexAccountDenomTimestamp = []byte{0x12}
// KeyndexSeparator defines separator between keys when combine, it should be one that is not used in denom expression.
KeyIndexSeparator = []byte{0xFF}
)
// stakeStoreKey returns action store key from ID.
func GetStakeStoreKey(id uint64) []byte {
return CombineKeys(KeyPrefixStake, sdk.Uint64ToBigEndian(id))
}
// combineKeys combine bytes array into a single bytes.
func CombineKeys(keys ...[]byte) []byte {
return bytes.Join(keys, KeyIndexSeparator)
}
// getTimeKey returns the key used for getting a set of period stakes
// where unstakeTime is after a specific time.
func GetTimeKey(timestamp time.Time) []byte {
timeBz := sdk.FormatTimeBytes(timestamp)
timeBzL := len(timeBz)
prefixL := len(KeyPrefixTimestamp)
bz := make([]byte, prefixL+8+timeBzL)
// copy the prefix
copy(bz[:prefixL], KeyPrefixTimestamp)
// 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)
return bz
}
func CombineStakes(pl1 Stakes, pl2 Stakes) Stakes {
return append(pl1, pl2...)
}
// gaugeStoreKey returns the combined byte array (store key) of the provided gauge ID's key prefix and the ID itself.
func GetKeyGaugeStore(id uint64) []byte {
return CombineKeys(KeyPrefixGauge, sdk.Uint64ToBigEndian(id))
}
// gaugePairStoreKey returns the combined byte array (store key) of the provided gauge denom key prefix and the denom itself.
func GetKeyGaugeIndexByPair(pairID string) []byte {
return CombineKeys(KeyPrefixGaugeIndexByPair, []byte(pairID))
}
func GetKeyStakeIndexByAccount(account sdk.AccAddress) []byte {
return CombineKeys(
KeyPrefixStakeIndexAccount,
account,
)
}
func GetKeyStakeIndexByDenom(denom string) []byte {
return CombineKeys(
KeyPrefixStakeIndexDenom,
[]byte(denom),
)
}
func GetKeyStakeIndexByAccountDenom(account sdk.AccAddress, denom string) []byte {
return CombineKeys(
KeyPrefixStakeIndexAccountDenom,
account,
[]byte(denom),
)
}
func GetKeyStakeIndexByTimestamp(pairID string, timestamp time.Time) []byte {
return CombineKeys(
KeyPrefixStakeIndexPairTimestamp,
[]byte(pairID),
GetTimeKey(timestamp),
)
}
func GetKeyStakeIndexByPairTick(pairID string, tickIndex int64) []byte {
return CombineKeys(
KeyPrefixStakeIndexPairTick,
[]byte(pairID),
TickIndexToBytes(tickIndex),
)
}
func TickIndexToBytes(tickIndex int64) []byte {
key := make([]byte, 9)
if tickIndex < 0 {
copy(key[1:], sdk.Uint64ToBigEndian(uint64(tickIndex)))
} else {
copy(key[:1], []byte{0x01})
copy(key[1:], sdk.Uint64ToBigEndian(uint64(tickIndex)))
}
return key
}