-
Notifications
You must be signed in to change notification settings - Fork 45
/
keys.go
117 lines (93 loc) · 4.44 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
package types
// DONTCOVER
import (
"encoding/binary"
poststypes "github.com/desmos-labs/desmos/v4/x/posts/types"
subspacestypes "github.com/desmos-labs/desmos/v4/x/subspaces/types"
)
const (
ModuleName = "reports"
RouterKey = ModuleName
StoreKey = ModuleName
QuerierRoute = ModuleName
ActionCreateReport = "create_report"
ActionDeleteReport = "delete_report"
ActionSupportStandardReason = "support_standard_reason"
ActionAddReason = "add_reason"
ActionRemoveReason = "remove_reason"
)
var (
NextReportIDPrefix = []byte{0x01}
ReportPrefix = []byte{0x02}
PostsReportsPrefix = []byte{0x03}
UsersReportsPrefix = []byte{0x04}
NextReasonIDPrefix = []byte{0x10}
ReasonPrefix = []byte{0x11}
)
// GetReportIDBytes returns the byte representation of the reportID
func GetReportIDBytes(reportID uint64) (reportIDBz []byte) {
reportIDBz = make([]byte, 8)
binary.BigEndian.PutUint64(reportIDBz, reportID)
return reportIDBz
}
// GetReportIDFromBytes returns reportID in uint64 format from a byte array
func GetReportIDFromBytes(bz []byte) (reportID uint64) {
return binary.BigEndian.Uint64(bz)
}
// NextReportIDStoreKey returns the key used to store the next report id for the given subspace
func NextReportIDStoreKey(subspaceID uint64) []byte {
return append(NextReportIDPrefix, subspacestypes.GetSubspaceIDBytes(subspaceID)...)
}
// SubspaceReportsPrefix returns the store prefix used to store all the reports related to the given subspace
func SubspaceReportsPrefix(subspaceID uint64) []byte {
return append(ReportPrefix, subspacestypes.GetSubspaceIDBytes(subspaceID)...)
}
// ReportStoreKey returns the key used to store the report with the given subspace id and report id
func ReportStoreKey(subspaceID uint64, reportID uint64) []byte {
return append(SubspaceReportsPrefix(subspaceID), GetReportIDBytes(reportID)...)
}
// PostReportsPrefix returns the prefix used to store the references of the reports for the given post
func PostReportsPrefix(subspaceID uint64, postID uint64) []byte {
postsReportsSuffix := append(subspacestypes.GetSubspaceIDBytes(subspaceID), poststypes.GetPostIDBytes(postID)...)
return append(PostsReportsPrefix, postsReportsSuffix...)
}
// PostReportStoreKey returns the key used to store the reference to a report for the post from the given reporter
func PostReportStoreKey(subspaceID uint64, postID uint64, reporter string) []byte {
return append(PostReportsPrefix(subspaceID, postID), GetUserAddressBytes(reporter)...)
}
// GetUserAddressBytes returns the byte representation of the given user address
func GetUserAddressBytes(address string) []byte {
return []byte(address)
}
// UserReportsPrefix returns the prefix used to store the reports from the given reporter
func UserReportsPrefix(subspaceID uint64, user string) []byte {
userReportsSuffix := append(subspacestypes.GetSubspaceIDBytes(subspaceID), GetUserAddressBytes(user)...)
return append(UsersReportsPrefix, userReportsSuffix...)
}
// UserReportStoreKey returns the key used to store the report for the given user having the given id
func UserReportStoreKey(subspaceID uint64, user string, reporter string) []byte {
return append(UserReportsPrefix(subspaceID, user), GetUserAddressBytes(reporter)...)
}
// --------------------------------------------------------------------------------------------------------------------
// GetReasonIDBytes returns the byte representation of the reasonID
func GetReasonIDBytes(reasonID uint32) (reasonIDBz []byte) {
reasonIDBz = make([]byte, 4)
binary.BigEndian.PutUint32(reasonIDBz, reasonID)
return reasonIDBz
}
// GetReasonIDFromBytes returns reasonID in uint32 format from a byte array
func GetReasonIDFromBytes(bz []byte) (reasonID uint32) {
return binary.BigEndian.Uint32(bz)
}
// NextReasonIDStoreKey returns the key used to store the next reason id for the given subspace
func NextReasonIDStoreKey(subspaceID uint64) []byte {
return append(NextReasonIDPrefix, subspacestypes.GetSubspaceIDBytes(subspaceID)...)
}
// SubspaceReasonsPrefix returns the store prefix used to store all the reports for the given subspace
func SubspaceReasonsPrefix(subspaceID uint64) []byte {
return append(ReasonPrefix, subspacestypes.GetSubspaceIDBytes(subspaceID)...)
}
// ReasonStoreKey returns the key used to store the reason with the given subspace id and reason id
func ReasonStoreKey(subspaceID uint64, reasonID uint32) []byte {
return append(SubspaceReasonsPrefix(subspaceID), GetReasonIDBytes(reasonID)...)
}