-
Notifications
You must be signed in to change notification settings - Fork 44
/
keys.go
203 lines (162 loc) · 7.89 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
package types
import (
"encoding/binary"
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
subspacetypes "github.com/desmos-labs/desmos/v5/x/subspaces/types"
)
// DONTCOVER
const (
ModuleName = "posts"
RouterKey = ModuleName
StoreKey = ModuleName
QuerierRoute = ModuleName
ActionCreatePost = "create_post"
ActionEditPost = "edit_post"
ActionAddPostAttachment = "add_post_attachment"
ActionRemovePostAttachment = "remove_post_attachment"
ActionDeletePost = "delete_post"
ActionAnswerPoll = "answer_poll"
ActionUpdateParams = "update_params"
DoNotModify = "[do-not-modify]"
)
var (
NextPostIDPrefix = []byte{0x00}
PostPrefix = []byte{0x01}
PostSectionPrefix = []byte{0x02}
NextAttachmentIDPrefix = []byte{0x10}
AttachmentPrefix = []byte{0x11}
UserAnswerPrefix = []byte{0x20}
ActivePollQueuePrefix = []byte{0x21}
ParamsKey = []byte{0x30}
)
// GetPostIDBytes returns the byte representation of the postID
func GetPostIDBytes(postID uint64) (postIDBz []byte) {
postIDBz = make([]byte, 8)
binary.BigEndian.PutUint64(postIDBz, postID)
return
}
// GetPostIDFromBytes returns postID in uint64 format from a byte array
func GetPostIDFromBytes(bz []byte) (postID uint64) {
return binary.BigEndian.Uint64(bz)
}
// GetSubspacePostIDBytes returns the byte representation of the subspaceID merged with the postID
func GetSubspacePostIDBytes(subspaceID uint64, postID uint64) []byte {
return append(subspacetypes.GetSubspaceIDBytes(subspaceID), GetPostIDBytes(postID)...)
}
// NextPostIDStoreKey returns the key uses to store the next post id for the given subspace
func NextPostIDStoreKey(subspaceID uint64) []byte {
return append(NextPostIDPrefix, subspacetypes.GetSubspaceIDBytes(subspaceID)...)
}
// SubspacePostsPrefix returns the store prefix used to store all the posts related to the given subspace
func SubspacePostsPrefix(subspaceID uint64) []byte {
return append(PostPrefix, subspacetypes.GetSubspaceIDBytes(subspaceID)...)
}
// PostStoreKey returns the key for a specific post
func PostStoreKey(subspaceID uint64, postID uint64) []byte {
return append(SubspacePostsPrefix(subspaceID), GetPostIDBytes(postID)...)
}
// SubspaceSectionsPrefix returns the prefix used to store all the section references for the given subspace
func SubspaceSectionsPrefix(subspaceID uint64) []byte {
return append(PostSectionPrefix, subspacetypes.GetSubspaceIDBytes(subspaceID)...)
}
// SectionPostsPrefix returns the prefix used to store all the section references for the given section
func SectionPostsPrefix(subspaceID uint64, sectionID uint32) []byte {
return append(SubspaceSectionsPrefix(subspaceID), subspacetypes.GetSectionIDBytes(sectionID)...)
}
// PostSectionStoreKey returns the key used to store the section reference for the given post
func PostSectionStoreKey(subspaceID uint64, sectionID uint32, postID uint64) []byte {
return append(SectionPostsPrefix(subspaceID, sectionID), GetPostIDBytes(postID)...)
}
var (
PostSectionPrefixLen = len(PostSectionPrefix)
SubspaceIDLen = len(subspacetypes.GetSubspaceIDBytes(1))
SectionIDLen = len(subspacetypes.GetSectionIDBytes(1))
PostIDLen = len(GetPostIDBytes(1))
)
func SplitPostSectionStoreKey(key []byte) (subspaceID uint64, sectionID uint32, postID uint64) {
expectedLen := PostSectionPrefixLen + SubspaceIDLen + SectionIDLen + PostIDLen
if len(key) != expectedLen {
panic(fmt.Errorf("invalid key length; expected %d but got %d", expectedLen, len(key)))
}
key = key[PostSectionPrefixLen:]
subspaceID = subspacetypes.GetSubspaceIDFromBytes(key[:SubspaceIDLen])
sectionID = subspacetypes.GetSectionIDFromBytes(key[SubspaceIDLen : SubspaceIDLen+SectionIDLen])
postID = GetPostIDFromBytes(key[SubspaceIDLen+SectionIDLen:])
return subspaceID, sectionID, postID
}
// --------------------------------------------------------------------------------------------------------------------
// GetAttachmentIDBytes returns the byte representation of the attachmentID
func GetAttachmentIDBytes(attachmentID uint32) (attachmentIDBz []byte) {
attachmentIDBz = make([]byte, 4)
binary.BigEndian.PutUint32(attachmentIDBz, attachmentID)
return
}
// GetAttachmentIDFromBytes returns the attachmentID in uint32 format from a byte array
func GetAttachmentIDFromBytes(bz []byte) (attachmentID uint32) {
return binary.BigEndian.Uint32(bz)
}
// NextAttachmentIDStoreKey returns the store key that is used to store the attachment id to be used next for the given post
func NextAttachmentIDStoreKey(subspaceID uint64, postID uint64) []byte {
return append(NextAttachmentIDPrefix, GetSubspacePostIDBytes(subspaceID, postID)...)
}
// PostAttachmentsPrefix returns the store prefix used to store all the given post attachments
func PostAttachmentsPrefix(subspaceID uint64, postID uint64) []byte {
return append(AttachmentPrefix, GetSubspacePostIDBytes(subspaceID, postID)...)
}
// AttachmentStoreKey returns the store key that is used to store the attachment having the given id
func AttachmentStoreKey(subspaceID uint64, postID uint64, attachmentID uint32) []byte {
return append(PostAttachmentsPrefix(subspaceID, postID), GetAttachmentIDBytes(attachmentID)...)
}
// --------------------------------------------------------------------------------------------------------------------
var (
lenTime = len(sdk.FormatTimeBytes(time.Now()))
lenPrefix = len(ActivePollQueuePrefix)
lenSubspaceID = 64 / 8
lenPostID = 64 / 8
)
// ActivePollByTimeKey gets the active poll queue key by endTime
func ActivePollByTimeKey(endTime time.Time) []byte {
return append(ActivePollQueuePrefix, sdk.FormatTimeBytes(endTime)...)
}
// ActivePollQueueKey returns the key for a pollID in the activePollQueue
func ActivePollQueueKey(subspaceID uint64, postID uint64, pollID uint32, endTime time.Time) []byte {
return append(ActivePollByTimeKey(endTime), GetPollIDBytes(subspaceID, postID, pollID)...)
}
// SplitActivePollQueueKey split the active poll key and returns the poll id and endTime
func SplitActivePollQueueKey(key []byte) (subspaceID uint64, postID uint64, pollID uint32, endTime time.Time) {
if len(key[lenPrefix:]) != 20+lenTime {
panic(fmt.Errorf("unexpected key length (%d ≠ %d)", len(key[1:]), lenTime+8))
}
endTime, err := sdk.ParseTimeBytes(key[lenPrefix : lenPrefix+lenTime])
if err != nil {
panic(err)
}
subspaceID = subspacetypes.GetSubspaceIDFromBytes(key[lenPrefix+lenTime : lenPrefix+lenTime+lenSubspaceID])
postID = GetPostIDFromBytes(key[lenPrefix+lenTime+lenSubspaceID : lenPrefix+lenTime+lenSubspaceID+lenPostID])
pollID = GetAttachmentIDFromBytes(key[lenPrefix+lenTime+lenSubspaceID+lenPostID:])
return subspaceID, postID, pollID, endTime
}
// GetPollIDBytes returns the byte representation of the provided pollID
func GetPollIDBytes(subspaceID uint64, postID uint64, pollID uint32) []byte {
return append(GetSubspacePostIDBytes(subspaceID, postID), GetAttachmentIDBytes(pollID)...)
}
// GetPollIDFromBytes returns the pollID in uint32 format from a byte array
func GetPollIDFromBytes(bz []byte) (subspaceID uint64, postID uint64, pollID uint32) {
if len(bz) != 20 {
panic(fmt.Errorf("unexpected key length (%d ≠ %d", len(bz), 20))
}
subspaceID = subspacetypes.GetSubspaceIDFromBytes(bz[:lenSubspaceID])
postID = GetPostIDFromBytes(bz[lenSubspaceID : lenSubspaceID+lenPostID])
pollID = GetAttachmentIDFromBytes(bz[lenSubspaceID+lenPostID:])
return subspaceID, postID, pollID
}
// PollAnswersPrefix returns the store prefix used to store the polls associated with the given post
func PollAnswersPrefix(subspaceID uint64, postID uint64, pollID uint32) []byte {
return append(UserAnswerPrefix, GetPollIDBytes(subspaceID, postID, pollID)...)
}
// PollAnswerStoreKey returns the store key used to store the poll answer for the given user
func PollAnswerStoreKey(subspaceID uint64, postID uint64, pollID uint32, user string) []byte {
return append(PollAnswersPrefix(subspaceID, postID, pollID), []byte(user)...)
}