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.

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