gitlab.com/gpdionisio/tendermint@v0.34.19-dev2/state/indexer/block/kv/util.go (about)

     1  package kv
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"github.com/google/orderedcode"
     9  
    10  	"github.com/tendermint/tendermint/libs/pubsub/query"
    11  	"github.com/tendermint/tendermint/types"
    12  )
    13  
    14  func intInSlice(a int, list []int) bool {
    15  	for _, b := range list {
    16  		if b == a {
    17  			return true
    18  		}
    19  	}
    20  
    21  	return false
    22  }
    23  
    24  func int64FromBytes(bz []byte) int64 {
    25  	v, _ := binary.Varint(bz)
    26  	return v
    27  }
    28  
    29  func int64ToBytes(i int64) []byte {
    30  	buf := make([]byte, binary.MaxVarintLen64)
    31  	n := binary.PutVarint(buf, i)
    32  	return buf[:n]
    33  }
    34  
    35  func heightKey(height int64) ([]byte, error) {
    36  	return orderedcode.Append(
    37  		nil,
    38  		types.BlockHeightKey,
    39  		height,
    40  	)
    41  }
    42  
    43  func eventKey(compositeKey, typ, eventValue string, height int64) ([]byte, error) {
    44  	return orderedcode.Append(
    45  		nil,
    46  		compositeKey,
    47  		eventValue,
    48  		height,
    49  		typ,
    50  	)
    51  }
    52  
    53  func parseValueFromPrimaryKey(key []byte) (string, error) {
    54  	var (
    55  		compositeKey string
    56  		height       int64
    57  	)
    58  
    59  	remaining, err := orderedcode.Parse(string(key), &compositeKey, &height)
    60  	if err != nil {
    61  		return "", fmt.Errorf("failed to parse event key: %w", err)
    62  	}
    63  
    64  	if len(remaining) != 0 {
    65  		return "", fmt.Errorf("unexpected remainder in key: %s", remaining)
    66  	}
    67  
    68  	return strconv.FormatInt(height, 10), nil
    69  }
    70  
    71  func parseValueFromEventKey(key []byte) (string, error) {
    72  	var (
    73  		compositeKey, typ, eventValue string
    74  		height                        int64
    75  	)
    76  
    77  	remaining, err := orderedcode.Parse(string(key), &compositeKey, &eventValue, &height, &typ)
    78  	if err != nil {
    79  		return "", fmt.Errorf("failed to parse event key: %w", err)
    80  	}
    81  
    82  	if len(remaining) != 0 {
    83  		return "", fmt.Errorf("unexpected remainder in key: %s", remaining)
    84  	}
    85  
    86  	return eventValue, nil
    87  }
    88  
    89  func lookForHeight(conditions []query.Condition) (int64, bool) {
    90  	for _, c := range conditions {
    91  		if c.CompositeKey == types.BlockHeightKey && c.Op == query.OpEqual {
    92  			return c.Operand.(int64), true
    93  		}
    94  	}
    95  
    96  	return 0, false
    97  }