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