forked from dymensionxyz/dymint
/
util.go
96 lines (78 loc) · 1.87 KB
/
util.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
package kv
import (
"encoding/binary"
"fmt"
"strconv"
"github.com/google/orderedcode"
"github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/types"
)
func intInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func int64FromBytes(bz []byte) int64 {
v, _ := binary.Varint(bz)
return v
}
func int64ToBytes(i int64) []byte {
buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutVarint(buf, i)
return buf[:n]
}
func heightKey(height int64) ([]byte, error) {
return orderedcode.Append(
nil,
types.BlockHeightKey,
height,
)
}
func eventKey(compositeKey, typ, eventValue string, height int64) ([]byte, error) {
return orderedcode.Append(
nil,
compositeKey,
eventValue,
height,
typ,
)
}
func parseValueFromPrimaryKey(key []byte) (string, error) {
var (
compositeKey string
height int64
)
remaining, err := orderedcode.Parse(string(key), &compositeKey, &height)
if err != nil {
return "", fmt.Errorf("failed to parse event key: %w", err)
}
if len(remaining) != 0 {
return "", fmt.Errorf("unexpected remainder in key: %s", remaining)
}
return strconv.FormatInt(height, 10), nil
}
func parseValueFromEventKey(key []byte) (string, error) {
var (
compositeKey, typ, eventValue string
height int64
)
remaining, err := orderedcode.Parse(string(key), &compositeKey, &eventValue, &height, &typ)
if err != nil {
return "", fmt.Errorf("failed to parse event key: %w", err)
}
if len(remaining) != 0 {
return "", fmt.Errorf("unexpected remainder in key: %s", remaining)
}
return eventValue, nil
}
func lookForHeight(conditions []query.Condition) (int64, bool) {
for _, c := range conditions {
if c.CompositeKey == types.BlockHeightKey && c.Op == query.OpEqual {
return c.Operand.(int64), true
}
}
return 0, false
}