github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/state/txindex/kv/utils.go (about)

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