Uniswap Sniper Bot  1.0
rlp.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <cstring>
5 #include <string>
6 
7 #include "utils.hpp"
8 
9 using namespace Utils;
10 
17 namespace RLP {
21  struct Item {
23  std::size_t length;
24  };
25 
34  inline std::size_t encodeLength(std::size_t length, std::size_t offset, Buffer output) {
35  if(length < 56) {
36  *output = length + offset;
37  return 1;
38  }
39 
40  std::size_t bytesLength = intToBuffer(length, output + 1);
41 
42  // Why 55?
43  // Documentation outlines:
44  // If a string is more than 55 bytes long, the RLP encoding consists of a single byte with value 0xb7 [...]
45  // If the total payload of a list is more than 55 bytes long, the RLP encoding consists of a single byte with value 0xf7 [...]
46  // When encoding string, we specify offset of 128 (0x80), add 55 and receive 183 (0xb7)
47  // When encoding total payload, we specify offset of 192 (0xc0), add 55 and receive 247 (0xf7)
48  *output = bytesLength + offset + 55;
49  return bytesLength + 1;
50  }
51 
59  inline std::size_t encodeItem(Item *input, Buffer output) {
60  // Empty item encoding returns 0x80
61  if(input->length == 0) {
62  *output = 0x80;
63  return 1;
64  }
65 
66  // For a single byte whose value is in the [0x00, 0x7f] range, that byte is its own RLP encoding
67  if(input->length == 1 && *(input->buffer) < 0x80){
68  *output = *(input->buffer);
69  return 1;
70  }
71 
72  std::size_t encodedLengthLength = encodeLength(input->length, 0x80, output);
73  memcpy(output + encodedLengthLength, input->buffer, input->length);
74 
75  return encodedLengthLength + input->length;
76  }
77 
86  inline std::size_t encodeList(Item input[], std::size_t inputLength, Buffer output) {
87  std::size_t payloadLength = 0;
88  for(std::size_t i = 0; i < inputLength; i++) {
89  // Shift output buffer by 9 as it is maximum encodeLength length
90  payloadLength += encodeItem(input + i, output + payloadLength + 9);
91  }
92 
93  // Encode payload length
94  std::size_t encodedLengthLength = encodeLength(payloadLength, 0xc0, output);
95 
96  // Move payload to the encoded length
97  memmove(output + encodedLengthLength, output + 9, payloadLength);
98 
99  return encodedLengthLength + payloadLength;
100  }
101 }
Functions for Recursive Length Prefix Encoding. It is the main encoding method used to serialize obje...
Definition: rlp.hpp:17
std::size_t encodeList(Item input[], std::size_t inputLength, Buffer output)
Encodes list of items.
Definition: rlp.hpp:86
std::size_t encodeItem(Item *input, Buffer output)
Encodes single item.
Definition: rlp.hpp:59
std::size_t encodeLength(std::size_t length, std::size_t offset, Buffer output)
Encodes length.
Definition: rlp.hpp:34
Namespace holding all converters and other utilities.
Definition: utils.hpp:10
std::size_t intToBuffer(std::uint64_t x, Buffer output)
Converts integer to buffer.
Definition: utils.hpp:122
Byte * Buffer
Definition: utils.hpp:12
Struct holding single item data - its byte representation and length.
Definition: rlp.hpp:21
Buffer buffer
Definition: rlp.hpp:22
std::size_t length
Definition: rlp.hpp:23