-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
133 lines (103 loc) · 3.75 KB
/
key.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
package types
import (
sdk "github.com/ayher/anatha/types"
"strings"
"time"
)
const (
ModuleName = "hra"
StoreKey = ModuleName
RouterKey = ModuleName
QuerierRoute = ModuleName
Separator = ":"
)
// Keys for HRA store
// Items are stored with the following key: values
//
// - 0x10<Name_Bytes>: NameInfo
// - 0x11<Addr_Bytes><Separator><Name_Bytes>: boolean
// - 0x12<endTime_Bytes><Name_Bytes>: Name
// - 0x13<Addr_Bytes><Separator><BlockchainId_Bytes><Separator><AddressIndex_Bytes>: BlockchainAddress
// - 0x14<Addr_Bytes>: Int
// - 0x15<BlockchainId_Bytes>: boolean
var (
NameInfoByNameKeyPrefix = []byte{0x10}
StatusByAddressAndNameKeyPrefix = []byte{0x11}
ExpiredNameInfoQueueKeyPrefix = []byte{0x12}
AddressKeyPrefix = []byte{0x13}
CreditsKeyPrefix = []byte{0x14}
RegisteredBlockchainIdKeyPrefix = []byte{0x15}
StatusPresent = []byte{0x01}
StatusAbsent = []byte{0x00}
)
var lenTime = len(sdk.FormatTimeBytes(time.Now()))
func GetNameInfoByNameKey(name string) []byte {
return append(NameInfoByNameKeyPrefix, []byte(name)...)
}
func GetStatusByAddressAndNameKey(address sdk.AccAddress, name string) []byte {
key := append(StatusByAddressAndNameKeyPrefix, address...)
key = append(key, []byte(Separator)...)
key = append(key, []byte(name)...)
return key
}
// ExpiredNameInfoByTimeKey gets the inactive proposal queue key by endTime
func ExpiredNameInfoByTimeKey(endTime time.Time) []byte {
return append(ExpiredNameInfoQueueKeyPrefix, sdk.FormatTimeBytes(endTime)...)
}
func SplitExpiredNameInfoQueueKey(key []byte) (name string, endTime time.Time) {
return splitKeyWithTime(key)
}
func ExpiredNameInfoQueueKey(name string, endTime time.Time) []byte {
return append(ExpiredNameInfoByTimeKey(endTime), []byte(name)...)
}
func GetStatusByAddressAndNameIteratorKey(address sdk.AccAddress) []byte {
return append(StatusByAddressAndNameKeyPrefix, address...)
}
// Address
func GetAddressKey(address sdk.AccAddress, blockchainId string, index string) []byte {
// Craft KVStore key in format: AnathaAddress:BlockchainId:AddressIndex
key := append(AddressKeyPrefix,[]byte(address)...)
key = append(key, []byte(Separator)...)
key = append(key, []byte(blockchainId)...)
key = append(key, []byte(Separator)...)
key = append(key, []byte(index)...)
return key
}
func GetBlockchainAddressIteratorKey(address sdk.AccAddress) []byte {
return append(AddressKeyPrefix, address...)
}
func SplitBlockchainAddressKey(key []byte) (blockchainAddress BlockchainAddressInfo) {
parts := strings.Split(string(key[22:]), Separator) // prefix + address + first separator
return NewBlockchainAddressInfo(parts[0], parts[1], "")
}
func SplitBlockchainAddressRecordKey(key []byte) (blockchainAddressRecord BlockchainAddressRecordInfo) {
parts := strings.Split(string(key[22:]), Separator) // prefix + address + first separator
blockchainAddressInfo := NewBlockchainAddressInfo(parts[0], parts[1], "")
return NewBlockchainAddressRecordInfo(sdk.AccAddress(key[1:21]), blockchainAddressInfo)
}
func GetCreditsKey(address sdk.AccAddress) []byte {
return append(CreditsKeyPrefix, address...)
}
func GetRegisteredBlockchainIdKey(blockchainId string) []byte {
return append(RegisteredBlockchainIdKeyPrefix, []byte(blockchainId)...)
}
func GetRegisteredBlockchainIdIteratorKey() []byte {
return RegisteredBlockchainIdKeyPrefix
}
func SplitRegisteredBlockchainIdKey(key []byte) (string) {
return string(key[1:])
}
// private functions
func splitKeyWithTime(key []byte) (name string, 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)
}
name = string(key[1 + lenTime:])
return
}