github.com/aakash4dev/cometbft@v0.38.2/state/txindex/kv/utils.go (about) 1 package kv 2 3 import ( 4 "fmt" 5 "math/big" 6 7 idxutil "github.com/aakash4dev/cometbft/internal/indexer" 8 cmtsyntax "github.com/aakash4dev/cometbft/libs/pubsub/query/syntax" 9 "github.com/aakash4dev/cometbft/state/indexer" 10 "github.com/aakash4dev/cometbft/types" 11 "github.com/google/orderedcode" 12 ) 13 14 type HeightInfo struct { 15 heightRange indexer.QueryRange 16 height int64 17 heightEqIdx int 18 onlyHeightRange bool 19 onlyHeightEq bool 20 } 21 22 // IntInSlice returns true if a is found in the list. 23 func intInSlice(a int, list []int) bool { 24 for _, b := range list { 25 if b == a { 26 return true 27 } 28 } 29 return false 30 } 31 32 func ParseEventSeqFromEventKey(key []byte) (int64, error) { 33 var ( 34 compositeKey, typ, eventValue string 35 height int64 36 eventSeq int64 37 ) 38 39 remaining, err := orderedcode.Parse(string(key), &compositeKey, &eventValue, &height, &typ, &eventSeq) 40 if err != nil { 41 return 0, fmt.Errorf("failed to parse event key: %w", err) 42 } 43 44 if len(remaining) != 0 { 45 return 0, fmt.Errorf("unexpected remainder in key: %s", remaining) 46 } 47 48 return eventSeq, nil 49 } 50 51 func dedupHeight(conditions []cmtsyntax.Condition) (dedupConditions []cmtsyntax.Condition, heightInfo HeightInfo) { 52 heightInfo.heightEqIdx = -1 53 heightRangeExists := false 54 found := false 55 var heightCondition []cmtsyntax.Condition 56 heightInfo.onlyHeightEq = true 57 heightInfo.onlyHeightRange = true 58 for _, c := range conditions { 59 if c.Tag == types.TxHeightKey { 60 if c.Op == cmtsyntax.TEq { 61 if heightRangeExists || found { 62 continue 63 } 64 hFloat := c.Arg.Number() 65 if hFloat != nil { 66 h, _ := hFloat.Int64() 67 heightInfo.height = h 68 found = true 69 heightCondition = append(heightCondition, c) 70 } 71 } else { 72 heightInfo.onlyHeightEq = false 73 heightRangeExists = true 74 dedupConditions = append(dedupConditions, c) 75 } 76 } else { 77 heightInfo.onlyHeightRange = false 78 heightInfo.onlyHeightEq = false 79 dedupConditions = append(dedupConditions, c) 80 } 81 } 82 if !heightRangeExists && len(heightCondition) != 0 { 83 heightInfo.heightEqIdx = len(dedupConditions) 84 heightInfo.onlyHeightRange = false 85 dedupConditions = append(dedupConditions, heightCondition...) 86 } else { 87 // If we found a range make sure we set the height idx to -1 as the height equality 88 // will be removed 89 heightInfo.heightEqIdx = -1 90 heightInfo.height = 0 91 heightInfo.onlyHeightEq = false 92 } 93 return dedupConditions, heightInfo 94 } 95 96 func checkHeightConditions(heightInfo HeightInfo, keyHeight int64) (bool, error) { 97 if heightInfo.heightRange.Key != "" { 98 withinBounds, err := idxutil.CheckBounds(heightInfo.heightRange, big.NewInt(keyHeight)) 99 if err != nil || !withinBounds { 100 return false, err 101 } 102 } else { 103 if heightInfo.height != 0 && keyHeight != heightInfo.height { 104 return false, nil 105 } 106 } 107 return true, nil 108 }