This repository has been archived by the owner on Apr 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
108 lines (98 loc) · 2.79 KB
/
utils.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
package kv
import (
"fmt"
"math/big"
idxutil "github.com/cometbft/cometbft/internal/indexer"
cmtsyntax "github.com/cometbft/cometbft/libs/pubsub/query/syntax"
"github.com/cometbft/cometbft/state/indexer"
"github.com/cometbft/cometbft/types"
"github.com/google/orderedcode"
)
type HeightInfo struct {
heightRange indexer.QueryRange
height int64
heightEqIdx int
onlyHeightRange bool
onlyHeightEq bool
}
// IntInSlice returns true if a is found in the list.
func intInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func ParseEventSeqFromEventKey(key []byte) (int64, error) {
var (
compositeKey, typ, eventValue string
height int64
eventSeq int64
)
remaining, err := orderedcode.Parse(string(key), &compositeKey, &eventValue, &height, &typ, &eventSeq)
if err != nil {
return 0, fmt.Errorf("failed to parse event key: %w", err)
}
if len(remaining) != 0 {
return 0, fmt.Errorf("unexpected remainder in key: %s", remaining)
}
return eventSeq, nil
}
func dedupHeight(conditions []cmtsyntax.Condition) (dedupConditions []cmtsyntax.Condition, heightInfo HeightInfo) {
heightInfo.heightEqIdx = -1
heightRangeExists := false
found := false
var heightCondition []cmtsyntax.Condition
heightInfo.onlyHeightEq = true
heightInfo.onlyHeightRange = true
for _, c := range conditions {
if c.Tag == types.TxHeightKey {
if c.Op == cmtsyntax.TEq {
if heightRangeExists || found {
continue
}
hFloat := c.Arg.Number()
if hFloat != nil {
h, _ := hFloat.Int64()
heightInfo.height = h
found = true
heightCondition = append(heightCondition, c)
}
} 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 height idx to -1 as the height equality
// will be removed
heightInfo.heightEqIdx = -1
heightInfo.height = 0
heightInfo.onlyHeightEq = false
}
return dedupConditions, heightInfo
}
func checkHeightConditions(heightInfo HeightInfo, keyHeight int64) (bool, error) {
if heightInfo.heightRange.Key != "" {
withinBounds, err := idxutil.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
}