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.

172 lines
5.8 KiB

  1. Wire Protocol
  2. =============
  3. The `Tendermint wire protocol <https://github.com/tendermint/go-wire>`__
  4. encodes data in `c-style binary <#binary>`__ and `JSON <#json>`__ form.
  5. Supported types
  6. ---------------
  7. - Primitive types
  8. - ``uint8`` (aka ``byte``), ``uint16``, ``uint32``, ``uint64``
  9. - ``int8``, ``int16``, ``int32``, ``int64``
  10. - ``uint``, ``int``: variable length (un)signed integers
  11. - ``string``, ``[]byte``
  12. - ``time``
  13. - Derived types
  14. - structs
  15. - var-length arrays of a particular type
  16. - fixed-length arrays of a particular type
  17. - interfaces: registered union types preceded by a ``type byte``
  18. - pointers
  19. Binary
  20. ------
  21. **Fixed-length primitive types** are encoded with 1,2,3, or 4 big-endian
  22. bytes. - ``uint8`` (aka ``byte``), ``uint16``, ``uint32``, ``uint64``:
  23. takes 1,2,3, and 4 bytes respectively - ``int8``, ``int16``, ``int32``,
  24. ``int64``: takes 1,2,3, and 4 bytes respectively - ``time``: ``int64``
  25. representation of nanoseconds since epoch
  26. **Variable-length integers** are encoded with a single leading byte
  27. representing the length of the following big-endian bytes. For signed
  28. negative integers, the most significant bit of the leading byte is a 1.
  29. - ``uint``: 1-byte length prefixed variable-size (0 ~ 255 bytes)
  30. unsigned integers
  31. - ``int``: 1-byte length prefixed variable-size (0 ~ 127 bytes) signed
  32. integers
  33. NOTE: While the number 0 (zero) is encoded with a single byte ``x00``,
  34. the number 1 (one) takes two bytes to represent: ``x0101``. This isn't
  35. the most efficient representation, but the rules are easier to remember.
  36. +---------------+----------------+----------------+
  37. | number | binary | binary ``int`` |
  38. | | ``uint`` | |
  39. +===============+================+================+
  40. | 0 | ``x00`` | ``x00`` |
  41. +---------------+----------------+----------------+
  42. | 1 | ``x0101`` | ``x0101`` |
  43. +---------------+----------------+----------------+
  44. | 2 | ``x0102`` | ``x0102`` |
  45. +---------------+----------------+----------------+
  46. | 256 | ``x020100`` | ``x020100`` |
  47. +---------------+----------------+----------------+
  48. | 2^(127\ *8)-1 | ``x800100...`` | overflow |
  49. | \| | | |
  50. | ``x7FFFFF...` | | |
  51. | ` | | |
  52. | \| | | |
  53. | ``x7FFFFF...` | | |
  54. | ` | | |
  55. | \| \| | | |
  56. | 2^(127*\ 8) | | |
  57. +---------------+----------------+----------------+
  58. | 2^(255\*8)-1 |
  59. | \| |
  60. | ``xFFFFFF...` |
  61. | ` |
  62. | \| overflow |
  63. | \| \| -1 \| |
  64. | n/a \| |
  65. | ``x8101`` \| |
  66. | \| -2 \| n/a |
  67. | \| ``x8102`` |
  68. | \| \| -256 \| |
  69. | n/a \| |
  70. | ``x820100`` |
  71. | \| |
  72. +---------------+----------------+----------------+
  73. **Structures** are encoded by encoding the field values in order of
  74. declaration.
  75. .. code:: go
  76. type Foo struct {
  77. MyString string
  78. MyUint32 uint32
  79. }
  80. var foo = Foo{"626172", math.MaxUint32}
  81. /* The binary representation of foo:
  82. 0103626172FFFFFFFF
  83. 0103: `int` encoded length of string, here 3
  84. 626172: 3 bytes of string "bar"
  85. FFFFFFFF: 4 bytes of uint32 MaxUint32
  86. */
  87. **Variable-length arrays** are encoded with a leading ``int`` denoting
  88. the length of the array followed by the binary representation of the
  89. items. **Fixed-length arrays** are similar but aren't preceded by the
  90. leading ``int``.
  91. .. code:: go
  92. foos := []Foo{foo, foo}
  93. /* The binary representation of foos:
  94. 01020103626172FFFFFFFF0103626172FFFFFFFF
  95. 0102: `int` encoded length of array, here 2
  96. 0103626172FFFFFFFF: the first `foo`
  97. 0103626172FFFFFFFF: the second `foo`
  98. */
  99. foos := [2]Foo{foo, foo} // fixed-length array
  100. /* The binary representation of foos:
  101. 0103626172FFFFFFFF0103626172FFFFFFFF
  102. 0103626172FFFFFFFF: the first `foo`
  103. 0103626172FFFFFFFF: the second `foo`
  104. */
  105. **Interfaces** can represent one of any number of concrete types. The
  106. concrete types of an interface must first be declared with their
  107. corresponding ``type byte``. An interface is then encoded with the
  108. leading ``type byte``, then the binary encoding of the underlying
  109. concrete type.
  110. NOTE: The byte ``x00`` is reserved for the ``nil`` interface value and
  111. ``nil`` pointer values.
  112. .. code:: go
  113. type Animal interface{}
  114. type Dog uint32
  115. type Cat string
  116. RegisterInterface(
  117. struct{ Animal }{}, // Convenience for referencing the 'Animal' interface
  118. ConcreteType{Dog(0), 0x01}, // Register the byte 0x01 to denote a Dog
  119. ConcreteType{Cat(""), 0x02}, // Register the byte 0x02 to denote a Cat
  120. )
  121. var animal Animal = Dog(02)
  122. /* The binary representation of animal:
  123. 010102
  124. 01: the type byte for a `Dog`
  125. 0102: the bytes of Dog(02)
  126. */
  127. **Pointers** are encoded with a single leading byte ``x00`` for ``nil``
  128. pointers, otherwise encoded with a leading byte ``x01`` followed by the
  129. binary encoding of the value pointed to.
  130. NOTE: It's easy to convert pointer types into interface types, since the
  131. ``type byte`` ``x00`` is always ``nil``.
  132. JSON
  133. ----
  134. The JSON codec is compatible with the ```binary`` <#binary>`__ codec,
  135. and is fairly intuitive if you're already familiar with golang's JSON
  136. encoding. Some quirks are noted below:
  137. - variable-length and fixed-length bytes are encoded as uppercase
  138. hexadecimal strings
  139. - interface values are encoded as an array of two items:
  140. ``[type_byte, concrete_value]``
  141. - times are encoded as rfc2822 strings