1

I am trying to understand what this means from the RLP white paper wiki "The only purpose of RLP is to encode structure; encoding specific data types (eg. strings, floats) is left up to higher-order protocols;"

reference: https://github.com/ethereum/wiki/wiki/RLP

My understanding from this is that RLP encodes just the structure and does not deal with the data, and the interpretation of the data type is left to a custom function? can somebody give me an example of this input is the structure below using RLP encoding:

e:g: ["cat",[[]],"pig",[""],"sheep"]

Thanks in advance, also If there is any video reference just for RLP would be greatly helpful.

1

Taken from the documentation, you have the following axioms:

if a string is 0-55 bytes long, the RLP encoding consists of a single byte with value 0x80 plus the length of the string followed by the string. The range of the first byte is thus [0x80, 0xb7].

And

If the total payload of a list (i.e. the combined length of all its items being RLP encoded) is 0-55 bytes long, the RLP encoding consists of a single byte with value 0xc0 plus the length of the list followed by the concatenation of the RLP encodings of the items. The range of the first byte is thus [0xc0, 0xf7].

Hence, ["cat",[[]],"pig",[""],"sheep"] would be encoded with the following logic:

You would encore cat like 0x83, 'c', 'a', 't' "cat" is a string, hence you start from 0x80 and add the length of the string (3) so it gives us 0x83, then the characters of the string.

[[]] would be 0xc1, 0xc0 This array contains ONE empty array.

As 0xc1 is the length of the array (the array contain one element, here [] ), and 0xc0 is the length of the second array (empty element)

"pig" is same as "cat", a string of 3 characters so: 0x83, 'p', 'i', 'g'

[""] is an array of ONE element being an empty string (0 characters) so it would encode as 0xc1, 0x80 (0xc0 + 1 , 0x80 + 0)

"sheep" is a 5 characters string, so it would encode as: 0x85, 's', 'h', 'e', 'e', 'p' (0x80 + 5, and each letter of the string)

The whole array contains 18 bytes of value, so it would encode as:

[ 0xd2, 0x83, 'c', 'a', 't', 0xc1, 0xc0, 0x83, 'p', 'i', 'g', 0xc1, 0x80, 0x85, 's', 'h', 'e', 'e', 'p'] (0xd2 is 0xc0 + 0x12, 0x12 being 18 in decimal)

For clarity purpose, characters have not been encoded in hexadecimal, but the real encoding will look like this:

[0xd2, 0x83, 0x63, 0x61, 0x74, 0xc1, 0xc0, 0x83, 0x70, 0x69, 0x67, 0xc1, 0x80, 0x85, 0x73, 0x68, 0x65, 0x65, 0x70]

  • Thank you so much for answering I will go through the answer again and ensure I understand. one more e:g to decode to clear up please, e:g : [ 978,12, ["cat"], {"dog"} ] – drao Mar 8 '18 at 14:35
  • 978 is 0x3D2 so would encode as [0x82, 0x03, 0xd2] , 12 is just one byte so [0x0c], cat and dog are 3 letter words in a single-element array, so it encode as above, so the final encoding is [0xce, 0x82, 0x03, 0xD2, 0x0c, 0xc4, 0x83, "c", "a", "t", 0xc4, 0x83, "d", "o", "g"] (0xce is 0xc0+ #NUMBER_OF_ELEMENT, which is 15 in decimal, which is E in hexa) – Betcheg Mar 9 '18 at 8:48
  • In the above example, does have RLP have encoding rule for {}, looks like it converted the {} as a list. for e.g how will RLP encode [ {"dog"} ]. I think RLP will only understand, Lists, Strings, Numbers. – drao Apr 3 '18 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.