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.

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