|
| 1 | +package query |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "fmt" |
| 6 | + "hash/crc32" |
| 7 | + |
| 8 | + "github.com/akash-network/node/util/validation" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + ErrInvalidPaginationKey = fmt.Errorf("pagination: invalid key") |
| 13 | +) |
| 14 | + |
| 15 | +// DecodePaginationKey parses the pagination key and returns the states, prefix and key to be used by the FilteredPaginate |
| 16 | +func DecodePaginationKey(key []byte) ([]byte, []byte, []byte, []byte, error) { |
| 17 | + if len(key) < 5 { |
| 18 | + return nil, nil, nil, nil, fmt.Errorf("%w: invalid key length", ErrInvalidPaginationKey) |
| 19 | + } |
| 20 | + |
| 21 | + expectedChecksum := binary.BigEndian.Uint32(key) |
| 22 | + |
| 23 | + key = key[4:] |
| 24 | + |
| 25 | + checksum := crc32.ChecksumIEEE(key) |
| 26 | + |
| 27 | + if expectedChecksum != checksum { |
| 28 | + return nil, nil, nil, nil, fmt.Errorf("%w: invalid checksum, 0x%08x != 0x%08x", ErrInvalidPaginationKey, expectedChecksum, checksum) |
| 29 | + } |
| 30 | + |
| 31 | + statesC := int(key[0]) |
| 32 | + key = key[1:] |
| 33 | + |
| 34 | + if len(key) < statesC { |
| 35 | + return nil, nil, nil, nil, fmt.Errorf("%w: invalid state length", ErrInvalidPaginationKey) |
| 36 | + } |
| 37 | + |
| 38 | + states := make([]byte, 0, statesC) |
| 39 | + for _, state := range key[:statesC] { |
| 40 | + states = append(states, state) |
| 41 | + } |
| 42 | + |
| 43 | + key = key[len(states):] |
| 44 | + |
| 45 | + if len(key) < 1 { |
| 46 | + return nil, nil, nil, nil, fmt.Errorf("%w: invalid state length", ErrInvalidPaginationKey) |
| 47 | + } |
| 48 | + |
| 49 | + prefixLength := int(key[0]) |
| 50 | + |
| 51 | + key = key[1:] |
| 52 | + |
| 53 | + if len(key) < prefixLength { |
| 54 | + return nil, nil, nil, nil, fmt.Errorf("%w: invalid state length", ErrInvalidPaginationKey) |
| 55 | + } |
| 56 | + |
| 57 | + prefix := key[:prefixLength] |
| 58 | + |
| 59 | + key = key[prefixLength:] |
| 60 | + |
| 61 | + if len(key) == 0 { |
| 62 | + return nil, nil, nil, nil, fmt.Errorf("%w: invalid state length", ErrInvalidPaginationKey) |
| 63 | + } |
| 64 | + |
| 65 | + keyLength := int(key[0]) |
| 66 | + key = key[1:] |
| 67 | + |
| 68 | + if len(key) < keyLength { |
| 69 | + return nil, nil, nil, nil, fmt.Errorf("%w: invalid state length", ErrInvalidPaginationKey) |
| 70 | + } |
| 71 | + |
| 72 | + pkey := key[:keyLength] |
| 73 | + |
| 74 | + key = key[keyLength:] |
| 75 | + var unsolicited []byte |
| 76 | + |
| 77 | + if len(key) > 0 { |
| 78 | + keyLength = int(key[0]) |
| 79 | + key = key[1:] |
| 80 | + |
| 81 | + if len(key) != keyLength { |
| 82 | + return nil, nil, nil, nil, fmt.Errorf("%w: invalid state length", ErrInvalidPaginationKey) |
| 83 | + } |
| 84 | + |
| 85 | + unsolicited = key |
| 86 | + } |
| 87 | + |
| 88 | + return states, prefix, pkey, unsolicited, nil |
| 89 | +} |
| 90 | + |
| 91 | +func EncodePaginationKey(states, prefix, key, unsolicited []byte) ([]byte, error) { |
| 92 | + if len(states) == 0 { |
| 93 | + return nil, fmt.Errorf("%w: states cannot be empty", ErrInvalidPaginationKey) |
| 94 | + } |
| 95 | + |
| 96 | + if len(prefix) == 0 { |
| 97 | + return nil, fmt.Errorf("%w: prefix cannot be empty", ErrInvalidPaginationKey) |
| 98 | + } |
| 99 | + |
| 100 | + if len(key) == 0 { |
| 101 | + return nil, fmt.Errorf("%w: key cannot be empty", ErrInvalidPaginationKey) |
| 102 | + } |
| 103 | + |
| 104 | + // 4 bytes for checksum |
| 105 | + // 1 byte for states count |
| 106 | + // len(states) bytes for states |
| 107 | + // 1 byte for prefix length |
| 108 | + // len(prefix) bytes for prefix |
| 109 | + // 1 byte for key length |
| 110 | + // len(key) bytes for key |
| 111 | + encLen := 4 + 1 + len(states) + 1 + len(prefix) + 1 + len(key) |
| 112 | + |
| 113 | + if len(unsolicited) > 0 { |
| 114 | + encLen += 1 + len(unsolicited) |
| 115 | + } |
| 116 | + |
| 117 | + buf := make([]byte, encLen) |
| 118 | + |
| 119 | + data := buf[4:] |
| 120 | + |
| 121 | + tmp := validation.MustEncodeWithLengthPrefix(states) |
| 122 | + copy(data, tmp) |
| 123 | + |
| 124 | + offset := len(tmp) |
| 125 | + tmp = validation.MustEncodeWithLengthPrefix(prefix) |
| 126 | + |
| 127 | + copy(data[offset:], tmp) |
| 128 | + offset += len(tmp) |
| 129 | + |
| 130 | + tmp = validation.MustEncodeWithLengthPrefix(key) |
| 131 | + copy(data[offset:], tmp) |
| 132 | + |
| 133 | + if len(unsolicited) > 0 { |
| 134 | + offset += len(tmp) |
| 135 | + tmp = validation.MustEncodeWithLengthPrefix(unsolicited) |
| 136 | + copy(data[offset:], tmp) |
| 137 | + } |
| 138 | + |
| 139 | + checksum := crc32.ChecksumIEEE(data) |
| 140 | + binary.BigEndian.PutUint32(buf, checksum) |
| 141 | + |
| 142 | + return buf, nil |
| 143 | +} |
0 commit comments