-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_key.go
157 lines (134 loc) · 3.64 KB
/
util_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package kernel
import (
"encoding/binary"
"github.com/fletaio/common/util"
"github.com/fletaio/common"
"github.com/fletaio/common/hash"
)
var (
tagHeightHash = []byte{1, 0}
tagHeightHeader = []byte{1, 2}
tagHeightData = []byte{1, 3}
tagHashHeight = []byte{1, 4}
tagAccount = []byte{2, 0}
tagAccountName = []byte{2, 1}
tagAccountSeq = []byte{2, 2}
tagAccountBalance = []byte{2, 3}
tagAccountData = []byte{2, 4}
tagUTXO = []byte{3, 0}
tagCustomData = []byte{4, 0}
tagEvent = []byte{5, 0}
tagLockedBalance = []byte{6, 0}
tagLockedBalanceHeight = []byte{6, 1}
)
func toHeightDataKey(height uint32) []byte {
bs := make([]byte, 6)
copy(bs, tagHeightData)
binary.LittleEndian.PutUint32(bs[2:], height)
return bs
}
func toHeightHeaderKey(height uint32) []byte {
bs := make([]byte, 6)
copy(bs, tagHeightHeader)
binary.LittleEndian.PutUint32(bs[2:], height)
return bs
}
func toHeightHashKey(height uint32) []byte {
bs := make([]byte, 6)
copy(bs, tagHeightHash)
binary.LittleEndian.PutUint32(bs[2:], height)
return bs
}
func toHashHeightKey(h hash.Hash256) []byte {
bs := make([]byte, 34)
copy(bs, tagHashHeight)
copy(bs[2:], h[:])
return bs
}
func toAccountKey(addr common.Address) []byte {
bs := make([]byte, 2+common.AddressSize)
copy(bs, tagAccount)
copy(bs[2:], addr[:])
return bs
}
func toAccountNameKey(Name string) []byte {
bs := make([]byte, 2+len(Name))
copy(bs, tagAccountName)
copy(bs[2:], []byte(Name))
return bs
}
func toAccountSeqKey(addr common.Address) []byte {
bs := make([]byte, 2+common.AddressSize)
copy(bs, tagAccountSeq)
copy(bs[2:], addr[:])
return bs
}
func toAccountBalanceKey(addr common.Address) []byte {
bs := make([]byte, 2+common.AddressSize)
copy(bs, tagAccountBalance)
copy(bs[2:], addr[:])
return bs
}
func toAccountDataKey(key string) []byte {
bs := make([]byte, 2+len(key))
copy(bs, tagAccountData)
copy(bs[2:], []byte(key))
return bs
}
func toUTXOKey(id uint64) []byte {
bs := make([]byte, 10)
copy(bs, tagUTXO)
binary.LittleEndian.PutUint64(bs[2:], id)
return bs
}
func fromUTXOKey(bs []byte) uint64 {
return binary.LittleEndian.Uint64(bs[2:])
}
func toCustomData(key string) []byte {
bs := make([]byte, 2+len(key))
copy(bs, tagCustomData)
copy(bs[2:], []byte(key))
return bs
}
func toEventKey(id uint64) []byte {
bs := make([]byte, 10)
copy(bs, tagEvent)
binary.LittleEndian.PutUint64(bs[2:], id)
return bs
}
func toLockedBalancePrefix(Address common.Address) []byte {
bs := make([]byte, 2+common.AddressSize)
copy(bs, tagLockedBalance)
copy(bs[2:], Address[:])
return bs
}
func toLockedBalanceKey(Address common.Address, UnlockHeight uint32) []byte {
bs := make([]byte, 6+common.AddressSize)
copy(bs, tagLockedBalance)
copy(bs[2:], Address[:])
binary.LittleEndian.PutUint32(bs[2+common.AddressSize:], UnlockHeight)
return bs
}
func fromLockedBalanceKey(bs []byte) (common.Address, uint32) {
var addr common.Address
copy(addr[:], bs[2:])
return addr, util.BytesToUint32(bs[2+common.AddressSize:])
}
func toLockedBalanceHeightPrefix(Height uint32) []byte {
bs := make([]byte, 6)
copy(bs, tagLockedBalance)
binary.LittleEndian.PutUint32(bs[2:], Height)
return bs
}
func toLockedBalanceHeightKey(UnlockHeight uint32, Address common.Address) []byte {
bs := make([]byte, 6+common.AddressSize)
copy(bs, tagLockedBalance)
binary.LittleEndian.PutUint32(bs[2:], UnlockHeight)
copy(bs[6:], Address[:])
return bs
}
func fromLockedBalanceHeightKey(bs []byte) (common.Address, uint32) {
var addr common.Address
copy(addr[:], bs[6:])
return addr, util.BytesToUint32(bs[2:])
}