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.

199 lines
5.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # Tendermint Encoding
  2. ## Binary Serialization (TMBIN)
  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. Zero is encoded as `0x00`. It is not length-prefixed.
  23. Examples:
  24. ```
  25. encode(uint(6)) == [0x01, 0x06]
  26. encode(uint(70000)) == [0x03, 0x01, 0x11, 0x70]
  27. encode(int(-6)) == [0xF1, 0x06]
  28. encode(int(-70000)) == [0xF3, 0x01, 0x11, 0x70]
  29. encode(int(0)) == [0x00]
  30. ```
  31. ### Strings
  32. An encoded string is a length prefix followed by the underlying bytes of the string.
  33. The length-prefix is itself encoded as an `int`.
  34. The empty string is encoded as `0x00`. It is not length-prefixed.
  35. Examples:
  36. ```
  37. encode("") == [0x00]
  38. encode("a") == [0x01, 0x01, 0x61]
  39. encode("hello") == [0x01, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F]
  40. encode("¥") == [0x01, 0x02, 0xC2, 0xA5]
  41. ```
  42. ### Arrays (fixed length)
  43. An encoded fix-lengthed array is the concatenation of the encoding of its elements.
  44. There is no length-prefix.
  45. Examples:
  46. ```
  47. encode([4]int8{1, 2, 3, 4}) == [0x01, 0x02, 0x03, 0x04]
  48. encode([4]int16{1, 2, 3, 4}) == [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04]
  49. encode([4]int{1, 2, 3, 4}) == [0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x04]
  50. encode([2]string{"abc", "efg"}) == [0x01, 0x03, 0x61, 0x62, 0x63, 0x01, 0x03, 0x65, 0x66, 0x67]
  51. ```
  52. ### Slices (variable length)
  53. An encoded variable-length array is a length prefix followed by the concatenation of the encoding of its elements.
  54. The length-prefix is itself encoded as an `int`.
  55. An empty slice is encoded as `0x00`. It is not length-prefixed.
  56. Examples:
  57. ```
  58. encode([]int8{}) == [0x00]
  59. encode([]int8{1, 2, 3, 4}) == [0x01, 0x04, 0x01, 0x02, 0x03, 0x04]
  60. encode([]int16{1, 2, 3, 4}) == [0x01, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04]
  61. encode([]int{1, 2, 3, 4}) == [0x01, 0x04, 0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x4]
  62. encode([]string{"abc", "efg"}) == [0x01, 0x02, 0x01, 0x03, 0x61, 0x62, 0x63, 0x01, 0x03, 0x65, 0x66, 0x67]
  63. ```
  64. ### BitArray
  65. BitArray is encoded as an `int` of the number of bits, and with an array of `uint64` to encode
  66. value of each array element.
  67. ```
  68. type BitArray struct {
  69. Bits int
  70. Elems []uint64
  71. }
  72. ```
  73. ### Time
  74. Time is encoded as an `int64` of the number of nanoseconds since January 1, 1970,
  75. rounded to the nearest millisecond.
  76. Times before then are invalid.
  77. Examples:
  78. ```
  79. encode(time.Time("Jan 1 00:00:00 UTC 1970")) == [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  80. encode(time.Time("Jan 1 00:00:01 UTC 1970")) == [0x00, 0x00, 0x00, 0x00, 0x3B, 0x9A, 0xCA, 0x00] // 1,000,000,000 ns
  81. encode(time.Time("Mon Jan 2 15:04:05 -0700 MST 2006")) == [0x0F, 0xC4, 0xBB, 0xC1, 0x53, 0x03, 0x12, 0x00]
  82. ```
  83. ### Structs
  84. An encoded struct is the concatenation of the encoding of its elements.
  85. There is no length-prefix.
  86. Examples:
  87. ```
  88. type MyStruct struct{
  89. A int
  90. B string
  91. C time.Time
  92. }
  93. encode(MyStruct{4, "hello", time.Time("Mon Jan 2 15:04:05 -0700 MST 2006")}) ==
  94. [0x01, 0x04, 0x01, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0F, 0xC4, 0xBB, 0xC1, 0x53, 0x03, 0x12, 0x00]
  95. ```
  96. ## Merkle Trees
  97. Simple Merkle trees are used in numerous places in Tendermint to compute a cryptographic digest of a data structure.
  98. RIPEMD160 is always used as the hashing function.
  99. The function `SimpleMerkleRoot` is a simple recursive function defined as follows:
  100. ```
  101. func SimpleMerkleRoot(hashes [][]byte) []byte{
  102. switch len(hashes) {
  103. case 0:
  104. return nil
  105. case 1:
  106. return hashes[0]
  107. default:
  108. left := SimpleMerkleRoot(hashes[:(len(hashes)+1)/2])
  109. right := SimpleMerkleRoot(hashes[(len(hashes)+1)/2:])
  110. return RIPEMD160(append(left, right))
  111. }
  112. }
  113. ```
  114. Note we abuse notion and call `SimpleMerkleRoot` with arguments of type `struct` or type `[]struct`.
  115. For `struct` arguments, we compute a `[][]byte` by sorting elements of the `struct` according to field name and then hashing them.
  116. For `[]struct` arguments, we compute a `[][]byte` by hashing the individual `struct` elements.
  117. ## JSON (TMJSON)
  118. Signed messages (eg. votes, proposals) in the consensus are encoded in TMJSON, rather than TMBIN.
  119. TMJSON is JSON where `[]byte` are encoded as uppercase hex, rather than base64.
  120. When signing, the elements of a message are sorted by key and the sorted message is embedded in an outer JSON that includes a `chain_id` field.
  121. We call this encoding the CanonicalSignBytes. For instance, CanonicalSignBytes for a vote would look like:
  122. ```
  123. {"chain_id":"my-chain-id","vote":{"block_id":{"hash":DEADBEEF,"parts":{"hash":BEEFDEAD,"total":3}},"height":3,"round":2,"timestamp":1234567890, "type":2}
  124. ```
  125. Note how the fields within each level are sorted.
  126. ## Other
  127. ### MakeParts
  128. TMBIN encode an object and slice it into parts.
  129. ```
  130. MakeParts(object, partSize)
  131. ```
  132. ### Part
  133. ```
  134. type Part struct {
  135. Index int
  136. Bytes byte[]
  137. Proof byte[]
  138. }
  139. ```