-
Notifications
You must be signed in to change notification settings - Fork 147
/
util.go
139 lines (125 loc) · 3.81 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
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
package kv
import (
"encoding/binary"
"fmt"
"math/big"
"strconv"
"strings"
"github.com/cometbft/cometbft/libs/pubsub/query/syntax"
"github.com/cometbft/cometbft/types"
"github.com/rollkit/rollkit/state"
"github.com/rollkit/rollkit/state/indexer"
"github.com/rollkit/rollkit/store"
)
type HeightInfo struct {
heightRange indexer.QueryRange
height int64
heightEqIdx int
onlyHeightRange bool
onlyHeightEq bool
}
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) string {
return store.GenerateKey([]string{types.BlockHeightKey, strconv.FormatInt(height, 10)})
}
func eventKey(compositeKey, typ, eventValue string, height int64) string {
return store.GenerateKey([]string{compositeKey, eventValue, strconv.FormatInt(height, 10), typ})
}
func parseValueFromPrimaryKey(key string) string {
parts := strings.SplitN(key, "/", 3)
return parts[2]
// height, err := strconv.ParseFloat(parts[2], 64)
// if err != nil {
// return 0, fmt.Errorf("failed to parse event key: %w", err)
// }
// return height, nil
}
func parseHeightFromEventKey(key string) (int64, error) {
parts := strings.SplitN(key, "/", 5)
height, err := strconv.ParseInt(parts[2], 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse event key: %w", err)
}
return height, nil
}
func parseValueFromEventKey(key string) string {
parts := strings.SplitN(key, "/", 5)
return parts[2]
}
// Remove all occurrences of height equality queries except one. While we are traversing the conditions, check whether the only condition in
// addition to match events is the height equality or height range query. At the same time, if we do have a height range condition
// ignore the height equality condition. If a height equality exists, place the condition index in the query and the desired height
// into the heightInfo struct
func dedupHeight(conditions []syntax.Condition) (dedupConditions []syntax.Condition, heightInfo HeightInfo, found bool) {
heightInfo.heightEqIdx = -1
heightRangeExists := false
var heightCondition []syntax.Condition
heightInfo.onlyHeightEq = true
heightInfo.onlyHeightRange = true
for _, c := range conditions {
if c.Tag == types.BlockHeightKey {
if c.Op == syntax.TEq {
if found || heightRangeExists {
continue
}
hFloat := c.Arg.Number()
if hFloat != nil {
h, _ := hFloat.Int64()
heightInfo.height = h
heightCondition = append(heightCondition, c)
found = true
}
} else {
heightInfo.onlyHeightEq = false
heightRangeExists = true
dedupConditions = append(dedupConditions, c)
}
} else {
heightInfo.onlyHeightRange = false
heightInfo.onlyHeightEq = false
dedupConditions = append(dedupConditions, c)
}
}
if !heightRangeExists && len(heightCondition) != 0 {
heightInfo.heightEqIdx = len(dedupConditions)
heightInfo.onlyHeightRange = false
dedupConditions = append(dedupConditions, heightCondition...)
} else {
// If we found a range make sure we set the hegiht idx to -1 as the height equality
// will be removed
heightInfo.heightEqIdx = -1
heightInfo.height = 0
heightInfo.onlyHeightEq = false
found = false
}
return dedupConditions, heightInfo, found
}
func checkHeightConditions(heightInfo HeightInfo, keyHeight int64) (bool, error) {
if heightInfo.heightRange.Key != "" {
withinBounds, err := state.CheckBounds(heightInfo.heightRange, big.NewInt(keyHeight))
if err != nil || !withinBounds {
return false, err
}
} else {
if heightInfo.height != 0 && keyHeight != heightInfo.height {
return false, nil
}
}
return true, nil
}