You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.6 KiB

7 years ago
  1. # Tendermint Encoding
  2. ## Serialization
  3. Tendermint aims to encode data structures in a manner similar to how the corresponding Go structs are laid out in memory.
  4. Variable length items are length-prefixed.
  5. While the encoding was inspired by Go, it is easily implemented in other languages as well given its intuitive design.
  6. ### Fixed Length Integers
  7. Fixed length integers are encoded in Big-Endian using the specified number of bytes.
  8. So `uint8` and `int8` use one byte, `uint16` and `int16` use two bytes,
  9. `uint32` and `int32` use 3 bytes, and `uint64` and `int64` use 4 bytes.
  10. Negative integers are encoded via twos-complement.
  11. Examples:
  12. ```
  13. encode(uint8(6)) == [0x06]
  14. encode(uint32(6)) == [0x00, 0x00, 0x00, 0x06]
  15. encode(int8(-6)) == [0xFA]
  16. encode(int32(-6)) == [0xFF, 0xFF, 0xFF, 0xFA]
  17. ```
  18. ### Variable Length Integers
  19. Variable length integers are encoded as length-prefixed Big-Endian integers.
  20. The length-prefix consists of a single byte and corresponds to the length of the encoded integer.
  21. Negative integers are encoded by flipping the leading bit of the length-prefix to a `1`.
  22. Examples:
  23. ```
  24. encode(uint(6)) == [0x01, 0x06]
  25. encode(uint(70000)) == [0x03, 0x01, 0x11, 0x70]
  26. encode(int(-6)) == [0xF1, 0x06]
  27. encode(int(-70000)) == [0xF3, 0x01, 0x11, 0x70]
  28. ```
  29. ### Strings
  30. An encoded string is a length prefix followed by the underlying bytes of the string.
  31. The length-prefix is itself encoded as an `int`.
  32. Examples:
  33. ```
  34. encode("a") == [0x01, 0x01, 0x61]
  35. encode("hello") == [0x01, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F]
  36. encode("¥") == [0x01, 0x02, 0xC2, 0xA5]
  37. ```
  38. ### Arrays (fixed length)
  39. An encoded fix-lengthed array is the concatenation of the encoding of its elements.
  40. There is no length-prefix.
  41. Examples:
  42. ```
  43. encode([4]int8{1, 2, 3, 4}) == [0x01, 0x02, 0x03, 0x04]
  44. encode([4]int16{1, 2, 3, 4}) == [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04]
  45. encode([4]int{1, 2, 3, 4}) == [0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x04]
  46. encode([2]string{"abc", "efg"}) == [0x01, 0x03, 0x61, 0x62, 0x63, 0x01, 0x03, 0x65, 0x66, 0x67]
  47. ```
  48. ### Slices (variable length)
  49. An encoded variable-length array is a length prefix followed by the concatenation of the encoding of its elements.
  50. The length-prefix is itself encoded as an `int`.
  51. Examples:
  52. ```
  53. encode([]int8{1, 2, 3, 4}) == [0x01, 0x04, 0x01, 0x02, 0x03, 0x04]
  54. encode([]int16{1, 2, 3, 4}) == [0x01, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04]
  55. encode([]int{1, 2, 3, 4}) == [0x01, 0x04, 0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x4]
  56. encode([]string{"abc", "efg"}) == [0x01, 0x02, 0x01, 0x03, 0x61, 0x62, 0x63, 0x01, 0x03, 0x65, 0x66, 0x67]
  57. ```
  58. ### Time
  59. Time is encoded as an `int64` of the number of nanoseconds since January 1, 1970,
  60. rounded to the nearest millisecond.
  61. Times before then are invalid.
  62. Examples:
  63. ```
  64. encode(time.Time("Jan 1 00:00:00 UTC 1970")) == [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  65. encode(time.Time("Jan 1 00:00:01 UTC 1970")) == [0x00, 0x00, 0x00, 0x00, 0x3B, 0x9A, 0xCA, 0x00] // 1,000,000,000 ns
  66. encode(time.Time("Mon Jan 2 15:04:05 -0700 MST 2006")) == [0x0F, 0xC4, 0xBB, 0xC1, 0x53, 0x03, 0x12, 0x00]
  67. ```
  68. ### Structs
  69. An encoded struct is the concatenation of the encoding of its elements.
  70. There is no length-prefix.
  71. Examples:
  72. ```
  73. type MyStruct struct{
  74. A int
  75. B string
  76. C time.Time
  77. }
  78. encode(MyStruct{4, "hello", time.Time("Mon Jan 2 15:04:05 -0700 MST 2006")}) ==
  79. [0x01, 0x04, 0x01, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0F, 0xC4, 0xBB, 0xC1, 0x53, 0x03, 0x12, 0x00]
  80. ```
  81. ## Merkle Trees
  82. SimpleMerkleRoot
  83. MakeBlockParts