-
Notifications
You must be signed in to change notification settings - Fork 198
/
keys.go
70 lines (54 loc) · 1.29 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
package types
import "encoding/binary"
func KeyPrefix(p string) []byte {
return []byte(p)
}
// FixationStore
const (
FixationVersionKey = "Entry_Version"
EntryIndexPrefix = "Entry_Index_"
EntryPrefix = "Entry_Value_"
)
var (
// use single byte to save storage.
// (value cannot be part of a valid index)
EntryIndexLive = []byte{1}
EntryIndexDead = []byte{2}
)
// TimerStore
type TimerType int
const (
BlockHeight TimerType = iota
BlockTime
)
const (
TimerVersionKey = "Timer_Version"
NextTimerPrefix = "Timer_Next_"
TimerPrefix = "Timer_Value_"
)
var NextTimerKey = []string{
"NextBlock", // for BlockHeight
"NextDate", // for BlockTime
}
var TimerTypePrefix = []string{
"Block_", // for BlockHeight
"Time_", // for BlockTime
}
// Common encoder/decode
func EncodeBlockAndKey(block uint64, key []byte) []byte {
encodedKey := make([]byte, 8+len(key))
binary.BigEndian.PutUint64(encodedKey[0:8], block)
copy(encodedKey[8:], key)
return encodedKey
}
func DecodeBlockAndKey(encodedKey []byte) (uint64, []byte) {
block := binary.BigEndian.Uint64(encodedKey[0:8])
return block, encodedKey[8:]
}
func EncodeKey(key uint64) []byte {
return EncodeBlockAndKey(key, []byte{})
}
func DecodeKey(encodedKey []byte) uint64 {
block, _ := DecodeBlockAndKey(encodedKey)
return block
}