/
keys.go
199 lines (157 loc) · 6.17 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package types
import (
"encoding/binary"
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
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
// DefaultParamspace default name for parameter store
DefaultParamspace = ModuleName
)
// Keys for governance store
// Items are stored with the following key: values
//
// - 0x00<proposalID_Bytes>: Proposal
//
// - 0x01<endTime_Bytes><proposalID_Bytes>: activeProposalID
//
// - 0x02<endTime_Bytes><proposalID_Bytes>: inactiveProposalID
//
// - 0x03: nextProposalID
//
// - 0x10<proposalID_Bytes><depositorAddr_Bytes>: Deposit
//
// - 0x20<proposalID_Bytes><voterAddr_Bytes>: Voter
var (
ProposalsKeyPrefix = []byte{0x00}
ActiveProposalQueuePrefix = []byte{0x01}
InactiveProposalQueuePrefix = []byte{0x02}
ProposalIDKey = []byte{0x03}
DepositsKeyPrefix = []byte{0x10}
VotesKeyPrefix = []byte{0x20}
// PrefixWaitingProposalQueue defines the prefix of waiting proposal queue
PrefixWaitingProposalQueue = []byte{0x30}
)
// WaitingProposalByBlockHeightKey gets the waiting proposal queue key by block height
func WaitingProposalByBlockHeightKey(blockHeight uint64) []byte {
return append(PrefixWaitingProposalQueue, sdk.Uint64ToBigEndian(blockHeight)...)
}
// WaitingProposalQueueKey returns the key for a proposalID in the WaitingProposalQueue
func WaitingProposalQueueKey(proposalID uint64, blockHeight uint64) []byte {
bz := make([]byte, 8)
binary.LittleEndian.PutUint64(bz, proposalID)
return append(WaitingProposalByBlockHeightKey(blockHeight), bz...)
}
// SplitWaitingProposalQueueKey split the active proposal key and returns the proposal id and endTime
func SplitWaitingProposalQueueKey(key []byte) (proposalID uint64, height uint64) {
return splitKeyWithHeight(key)
}
func splitKeyWithHeight(key []byte) (proposalID uint64, height uint64) {
// 16 is sum of proposalID length and height length
if len(key[1:]) != 16 {
panic(fmt.Sprintf("unexpected key length (%d ≠ %d)", len(key[1:]), 16))
}
height = binary.BigEndian.Uint64(key[1 : 1+8])
proposalID = binary.LittleEndian.Uint64(key[1+8:])
return
}
var lenTime = len(sdk.FormatTimeBytes(time.Now()))
// ProposalKey gets a specific proposal from the store
func ProposalKey(proposalID uint64) []byte {
bz := make([]byte, 8)
binary.LittleEndian.PutUint64(bz, proposalID)
return append(ProposalsKeyPrefix, bz...)
}
// 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 {
bz := make([]byte, 8)
binary.LittleEndian.PutUint64(bz, proposalID)
return append(ActiveProposalByTimeKey(endTime), bz...)
}
// 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 {
bz := make([]byte, 8)
binary.LittleEndian.PutUint64(bz, proposalID)
return append(InactiveProposalByTimeKey(endTime), bz...)
}
// DepositsKey gets the first part of the deposits key based on the proposalID
func DepositsKey(proposalID uint64) []byte {
bz := make([]byte, 8)
binary.LittleEndian.PutUint64(bz, proposalID)
return append(DepositsKeyPrefix, bz...)
}
// DepositKey key of a specific deposit from the store
func DepositKey(proposalID uint64, depositorAddr sdk.AccAddress) []byte {
return append(DepositsKey(proposalID), depositorAddr.Bytes()...)
}
// VotesKey gets the first part of the votes key based on the proposalID
func VotesKey(proposalID uint64) []byte {
bz := make([]byte, 8)
binary.LittleEndian.PutUint64(bz, proposalID)
return append(VotesKeyPrefix, bz...)
}
// VoteKey key of a specific vote from the store
func VoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte {
return append(VotesKey(proposalID), 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 binary.LittleEndian.Uint64(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 = binary.LittleEndian.Uint64(key[1+lenTime:])
return
}
func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) {
if len(key[1:]) != 8+sdk.AddrLen {
panic(fmt.Sprintf("unexpected key length (%d ≠ %d)", len(key), 8+sdk.AddrLen))
}
proposalID = binary.LittleEndian.Uint64(key[1:9])
addr = sdk.AccAddress(key[9:])
return
}