-
Notifications
You must be signed in to change notification settings - Fork 40
/
keys.go
123 lines (91 loc) · 3.69 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
118
119
120
121
122
123
package types
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
hubtypes "github.com/sentinel-official/hub/v12/types"
)
const (
ModuleName = "session"
)
var (
CountKey = []byte{0x00}
SessionKeyPrefix = []byte{0x10}
SessionForInactiveAtKeyPrefix = []byte{0x11}
SessionForAccountKeyPrefix = []byte{0x12}
SessionForNodeKeyPrefix = []byte{0x13}
SessionForSubscriptionKeyPrefix = []byte{0x14}
SessionForAllocationKeyPrefix = []byte{0x15}
)
func SessionKey(id uint64) []byte {
return append(SessionKeyPrefix, sdk.Uint64ToBigEndian(id)...)
}
func GetSessionForAccountKeyPrefix(addr sdk.AccAddress) []byte {
return append(SessionForAccountKeyPrefix, address.MustLengthPrefix(addr.Bytes())...)
}
func SessionForAccountKey(addr sdk.AccAddress, id uint64) []byte {
return append(GetSessionForAccountKeyPrefix(addr), sdk.Uint64ToBigEndian(id)...)
}
func GetSessionForNodeKeyPrefix(addr hubtypes.NodeAddress) []byte {
return append(SessionForNodeKeyPrefix, address.MustLengthPrefix(addr.Bytes())...)
}
func SessionForNodeKey(addr hubtypes.NodeAddress, id uint64) []byte {
return append(GetSessionForNodeKeyPrefix(addr), sdk.Uint64ToBigEndian(id)...)
}
func GetSessionForSubscriptionKeyPrefix(id uint64) []byte {
return append(SessionForSubscriptionKeyPrefix, sdk.Uint64ToBigEndian(id)...)
}
func SessionForSubscriptionKey(subscriptionID, sessionID uint64) []byte {
return append(GetSessionForSubscriptionKeyPrefix(subscriptionID), sdk.Uint64ToBigEndian(sessionID)...)
}
func GetSessionForAllocationKeyPrefix(id uint64, addr sdk.AccAddress) []byte {
return append(SessionForAllocationKeyPrefix, append(sdk.Uint64ToBigEndian(id), address.MustLengthPrefix(addr)...)...)
}
func SessionForAllocationKey(subscriptionID uint64, addr sdk.AccAddress, sessionID uint64) []byte {
return append(GetSessionForAllocationKeyPrefix(subscriptionID, addr), sdk.Uint64ToBigEndian(sessionID)...)
}
func GetSessionForInactiveAtKeyPrefix(at time.Time) []byte {
return append(SessionForInactiveAtKeyPrefix, sdk.FormatTimeBytes(at)...)
}
func SessionForInactiveAtKey(at time.Time, id uint64) []byte {
return append(GetSessionForInactiveAtKeyPrefix(at), sdk.Uint64ToBigEndian(id)...)
}
func IDFromSessionForAccountKey(key []byte) uint64 {
// prefix (1 byte) | addrLen (1 byte) | addr (addrLen bytes) | id (8 bytes)
addrLen := int(key[1])
if len(key) != 10+addrLen {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 10+addrLen))
}
return sdk.BigEndianToUint64(key[2+addrLen:])
}
func IDFromSessionForNodeKey(key []byte) uint64 {
// prefix (1 byte) | addrLen (1 byte) | addr (addrLen bytes) | id (8 bytes)
addrLen := int(key[1])
if len(key) != 10+addrLen {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 10+addrLen))
}
return sdk.BigEndianToUint64(key[2+addrLen:])
}
func IDFromSessionForSubscriptionKey(key []byte) uint64 {
// prefix (1 byte) | subscriptionID (8 bytes) | sessionID (8 bytes)
if len(key) != 17 {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 17))
}
return sdk.BigEndianToUint64(key[9:])
}
func IDFromSessionForAllocationKey(key []byte) uint64 {
// prefix (1 byte) | subscriptionID (8 bytes) | addrLen (1 byte) | addr (addrLen bytes) | id (8 bytes)
addrLen := int(key[9])
if len(key) != 18+addrLen {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 18+addrLen))
}
return sdk.BigEndianToUint64(key[10+addrLen:])
}
func IDFromSessionForInactiveAtKey(key []byte) uint64 {
// prefix (1 byte) | at (29 bytes) | session (8 bytes)
if len(key) != 38 {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 38))
}
return sdk.BigEndianToUint64(key[30:])
}