/
epoch_cu.go
94 lines (79 loc) · 3.17 KB
/
epoch_cu.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
package types
import (
"encoding/binary"
"fmt"
"strconv"
"strings"
)
const (
UniqueEpochSessionPrefix = "UniqueEpochSession/"
ProviderEpochCuPrefix = "ProviderEpochCu/"
ProviderEpochComplainerCuPrefix = "ProviderEpochComplainerCu/"
ProviderConsumerEpochCuPrefix = "ProviderConsumerEpochCu/"
)
func EncodeBlock(block uint64) []byte {
encodedKey := make([]byte, 8)
binary.BigEndian.PutUint64(encodedKey[0:8], block)
return encodedKey
}
func DecodeBlock(encodedKey []byte) uint64 {
return binary.BigEndian.Uint64(encodedKey[0:8])
}
func UniqueEpochSessionKey(epoch uint64, provider string, chainID string, project string, sessionID uint64) []byte {
return append(EncodeBlock(epoch), []byte(strings.Join([]string{provider, chainID, project, strconv.FormatUint(sessionID, 10)}, " "))...)
}
func ProviderEpochCuKey(epoch uint64, provider string, chainID string) []byte {
return append(EncodeBlock(epoch), []byte(strings.Join([]string{provider, chainID}, " "))...)
}
func ProviderConsumerEpochCuKey(epoch uint64, provider string, project string, chainID string) []byte {
return append(EncodeBlock(epoch), []byte(strings.Join([]string{provider, project, chainID}, " "))...)
}
func DecodeUniqueEpochSessionKey(key string) (epoch uint64, provider string, chainID string, project string, sessionID uint64, err error) {
if len(key) < 8 {
return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad structure. key: %s", key)
}
split := strings.Split(key[8:], " ")
if len(split) != 4 {
return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad structure. key: %s", key)
}
epoch = DecodeBlock([]byte(key[:8]))
sessionID, err = strconv.ParseUint(split[3], 10, 64)
if err != nil {
return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad session ID. key: %s", key)
}
return epoch, split[0], split[1], split[2], sessionID, nil
}
func DecodeProviderEpochCuKey(key string) (epoch uint64, provider string, chainID string, err error) {
if len(key) < 8 {
return 0, "", "", fmt.Errorf("invalid ProviderEpochCu key: bad structure. key: %s", key)
}
split := strings.Split(key[8:], " ")
if len(split) != 2 {
return 0, "", "", fmt.Errorf("invalid ProviderEpochCu key: bad structure. key: %s", key)
}
epoch = DecodeBlock([]byte(key[:8]))
return epoch, split[0], split[1], nil
}
func DecodeProviderConsumerEpochCuKey(key string) (epoch uint64, provider string, project string, chainID string, err error) {
if len(key) < 8 {
return 0, "", "", "", fmt.Errorf("invalid ProviderConsumerEpochCu key: bad structure. key: %s", key)
}
split := strings.Split(key[8:], " ")
if len(split) != 3 {
return 0, "", "", "", fmt.Errorf("invalid ProviderConsumerEpochCu key: bad structure. key: %s", key)
}
epoch = DecodeBlock([]byte(key[:8]))
return epoch, split[0], split[1], split[2], nil
}
func UniqueEpochSessionKeyPrefix() []byte {
return []byte(UniqueEpochSessionPrefix)
}
func ProviderEpochCuKeyPrefix() []byte {
return []byte(ProviderEpochCuPrefix)
}
func ProviderEpochComplainerCuKeyPrefix() []byte {
return []byte(ProviderEpochComplainerCuPrefix)
}
func ProviderConsumerEpochCuKeyPrefix() []byte {
return []byte(ProviderConsumerEpochCuPrefix)
}