/
keys.go
210 lines (155 loc) · 6.59 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
199
200
201
202
203
204
205
206
207
208
209
210
package types
import (
"fmt"
"time"
hubtypes "github.com/sentinel-official/hub/v12/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)
const (
ModuleName = "subscription"
Day = 24 * time.Hour
)
var (
CountKey = []byte{0x00}
SubscriptionKeyPrefix = []byte{0x10}
SubscriptionForInactiveAtKeyPrefix = []byte{0x11}
SubscriptionForAccountKeyPrefix = []byte{0x12}
SubscriptionForNodeKeyPrefix = []byte{0x13}
SubscriptionForPlanKeyPrefix = []byte{0x14}
AllocationKeyPrefix = []byte{0x20}
PayoutKeyPrefix = []byte{0x30}
PayoutForNextAtKeyPrefix = []byte{0x31}
PayoutForAccountKeyPrefix = []byte{0x32}
PayoutForNodeKeyPrefix = []byte{0x33}
PayoutForAccountByNodeKeyPrefix = []byte{0x34}
)
func SubscriptionKey(id uint64) []byte {
return append(SubscriptionKeyPrefix, sdk.Uint64ToBigEndian(id)...)
}
func GetSubscriptionForAccountKeyPrefix(addr sdk.AccAddress) []byte {
return append(SubscriptionForAccountKeyPrefix, address.MustLengthPrefix(addr.Bytes())...)
}
func SubscriptionForAccountKey(addr sdk.AccAddress, id uint64) []byte {
return append(GetSubscriptionForAccountKeyPrefix(addr), sdk.Uint64ToBigEndian(id)...)
}
func GetSubscriptionForNodeKeyPrefix(addr hubtypes.NodeAddress) []byte {
return append(SubscriptionForNodeKeyPrefix, address.MustLengthPrefix(addr.Bytes())...)
}
func SubscriptionForNodeKey(addr hubtypes.NodeAddress, id uint64) []byte {
return append(GetSubscriptionForNodeKeyPrefix(addr), sdk.Uint64ToBigEndian(id)...)
}
func GetSubscriptionForPlanKeyPrefix(id uint64) []byte {
return append(SubscriptionForPlanKeyPrefix, sdk.Uint64ToBigEndian(id)...)
}
func SubscriptionForPlanKey(planID, subscriptionID uint64) []byte {
return append(GetSubscriptionForPlanKeyPrefix(planID), sdk.Uint64ToBigEndian(subscriptionID)...)
}
func GetSubscriptionForInactiveAtKeyPrefix(at time.Time) []byte {
return append(SubscriptionForInactiveAtKeyPrefix, sdk.FormatTimeBytes(at)...)
}
func SubscriptionForInactiveAtKey(at time.Time, id uint64) []byte {
return append(GetSubscriptionForInactiveAtKeyPrefix(at), sdk.Uint64ToBigEndian(id)...)
}
func GetAllocationForSubscriptionKeyPrefix(id uint64) []byte {
return append(AllocationKeyPrefix, sdk.Uint64ToBigEndian(id)...)
}
func AllocationKey(id uint64, addr sdk.AccAddress) []byte {
return append(GetAllocationForSubscriptionKeyPrefix(id), address.MustLengthPrefix(addr.Bytes())...)
}
func PayoutKey(id uint64) []byte {
return append(PayoutKeyPrefix, sdk.Uint64ToBigEndian(id)...)
}
func GetPayoutForNextAtKeyPrefix(at time.Time) []byte {
return append(PayoutForNextAtKeyPrefix, sdk.FormatTimeBytes(at)...)
}
func PayoutForNextAtKey(at time.Time, id uint64) []byte {
return append(GetPayoutForNextAtKeyPrefix(at), sdk.Uint64ToBigEndian(id)...)
}
func GetPayoutForAccountKeyPrefix(addr sdk.AccAddress) []byte {
return append(PayoutForAccountKeyPrefix, address.MustLengthPrefix(addr.Bytes())...)
}
func PayoutForAccountKey(addr sdk.AccAddress, id uint64) []byte {
return append(GetPayoutForAccountKeyPrefix(addr), sdk.Uint64ToBigEndian(id)...)
}
func GetPayoutForNodeKeyPrefix(addr hubtypes.NodeAddress) []byte {
return append(PayoutForNodeKeyPrefix, address.MustLengthPrefix(addr.Bytes())...)
}
func PayoutForNodeKey(addr hubtypes.NodeAddress, id uint64) []byte {
return append(GetPayoutForNodeKeyPrefix(addr), sdk.Uint64ToBigEndian(id)...)
}
func GetPayoutForAccountByNodeKeyPrefix(accAddr sdk.AccAddress, nodeAddr hubtypes.NodeAddress) (key []byte) {
return append(append(PayoutForAccountByNodeKeyPrefix, address.MustLengthPrefix(accAddr.Bytes())...), address.MustLengthPrefix(nodeAddr.Bytes())...)
}
func PayoutForAccountByNodeKey(accAddr sdk.AccAddress, nodeAddr hubtypes.NodeAddress, id uint64) []byte {
return append(GetPayoutForAccountByNodeKeyPrefix(accAddr, nodeAddr), sdk.Uint64ToBigEndian(id)...)
}
func AccAddrFromSubscriptionForAccountKey(key []byte) sdk.AccAddress {
// prefix (1 byte) | addrLen (1 byte) | addr (addrLen bytes) | id (8 bytes)
addrLen := int(key[1])
if len(key) != 10+addrLen {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 10+addrLen))
}
return key[2 : 2+addrLen]
}
func IDFromSubscriptionForAccountKey(key []byte) uint64 {
// prefix (1 byte) | addrLen (1 byte) | addr (addrLen bytes) | id (8 bytes)
addrLen := int(key[1])
if len(key) != 10+addrLen {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 10+addrLen))
}
return sdk.BigEndianToUint64(key[2+addrLen:])
}
func IDFromSubscriptionForNodeKey(key []byte) uint64 {
// prefix (1 byte) | addrLen (1 byte) | addr (addrLen bytes) | id (8 bytes)
addrLen := int(key[1])
if len(key) != 10+addrLen {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 10+addrLen))
}
return sdk.BigEndianToUint64(key[2+addrLen:])
}
func IDFromSubscriptionForPlanKey(key []byte) uint64 {
// prefix (1 byte) | planID (8 bytes) | subscriptionID (8 bytes)
if len(key) != 17 {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 17))
}
return sdk.BigEndianToUint64(key[9:])
}
func IDFromSubscriptionForInactiveAtKey(key []byte) uint64 {
// prefix (1 byte) | at (29 bytes) | id (8 bytes)
if len(key) != 38 {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 38))
}
return sdk.BigEndianToUint64(key[30:])
}
func IDFromPayoutForAccountKey(key []byte) uint64 {
// prefix (1 byte) | addrLen(1 byte) | addr (addrLen bytes) | id (8 bytes)
addrLen := int(key[1])
if len(key) != 10+addrLen {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 10+addrLen))
}
return sdk.BigEndianToUint64(key[2+addrLen:])
}
func IDFromPayoutForNodeKey(key []byte) uint64 {
// prefix (1 byte) | addrLen(1 byte) | addr (addrLen bytes) | id (8 bytes)
addrLen := int(key[1])
if len(key) != 10+addrLen {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 10+addrLen))
}
return sdk.BigEndianToUint64(key[2+addrLen:])
}
func IDFromPayoutForAccountByNodeKey(key []byte) uint64 {
// prefix (1 byte) | accAddrLen(1 byte) | accAddr (accAddrLen bytes) | nodeAddrLen(1 byte) | nodeAddr (nodeAddrLen bytes) | id (8 bytes)
accAddrLen, nodeAddrLen := int(key[1]), int(key[2+int(key[1])])
if len(key) != 11+accAddrLen+nodeAddrLen {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 11+accAddrLen+nodeAddrLen))
}
return sdk.BigEndianToUint64(key[3+accAddrLen+nodeAddrLen:])
}
func IDFromPayoutForNextAtKey(key []byte) uint64 {
// prefix (1 byte) | at (29 bytes) | id (8 bytes)
if len(key) != 38 {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 38))
}
return sdk.BigEndianToUint64(key[30:])
}