|
@ -2,9 +2,11 @@ |
|
|
|
|
|
|
|
|
## Binary Serialization (TMBIN) |
|
|
## Binary Serialization (TMBIN) |
|
|
|
|
|
|
|
|
Tendermint aims to encode data structures in a manner similar to how the corresponding Go structs are laid out in memory. |
|
|
|
|
|
|
|
|
Tendermint aims to encode data structures in a manner similar to how the corresponding Go structs |
|
|
|
|
|
are laid out in memory. |
|
|
Variable length items are length-prefixed. |
|
|
Variable length items are length-prefixed. |
|
|
While the encoding was inspired by Go, it is easily implemented in other languages as well given its intuitive design. |
|
|
|
|
|
|
|
|
While the encoding was inspired by Go, it is easily implemented in other languages as well given its |
|
|
|
|
|
intuitive design. |
|
|
|
|
|
|
|
|
XXX: This is changing to use real varints and 4-byte-prefixes. |
|
|
XXX: This is changing to use real varints and 4-byte-prefixes. |
|
|
See https://github.com/tendermint/go-wire/tree/sdk2. |
|
|
See https://github.com/tendermint/go-wire/tree/sdk2. |
|
@ -19,7 +21,7 @@ Negative integers are encoded via twos-complement. |
|
|
|
|
|
|
|
|
Examples: |
|
|
Examples: |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```go |
|
|
encode(uint8(6)) == [0x06] |
|
|
encode(uint8(6)) == [0x06] |
|
|
encode(uint32(6)) == [0x00, 0x00, 0x00, 0x06] |
|
|
encode(uint32(6)) == [0x00, 0x00, 0x00, 0x06] |
|
|
|
|
|
|
|
@ -36,10 +38,9 @@ Negative integers are encoded by flipping the leading bit of the length-prefix t |
|
|
|
|
|
|
|
|
Zero is encoded as `0x00`. It is not length-prefixed. |
|
|
Zero is encoded as `0x00`. It is not length-prefixed. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Examples: |
|
|
Examples: |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```go |
|
|
encode(uint(6)) == [0x01, 0x06] |
|
|
encode(uint(6)) == [0x01, 0x06] |
|
|
encode(uint(70000)) == [0x03, 0x01, 0x11, 0x70] |
|
|
encode(uint(70000)) == [0x03, 0x01, 0x11, 0x70] |
|
|
|
|
|
|
|
@ -58,7 +59,7 @@ The empty string is encoded as `0x00`. It is not length-prefixed. |
|
|
|
|
|
|
|
|
Examples: |
|
|
Examples: |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```go |
|
|
encode("") == [0x00] |
|
|
encode("") == [0x00] |
|
|
encode("a") == [0x01, 0x01, 0x61] |
|
|
encode("a") == [0x01, 0x01, 0x61] |
|
|
encode("hello") == [0x01, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F] |
|
|
encode("hello") == [0x01, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F] |
|
@ -72,7 +73,7 @@ There is no length-prefix. |
|
|
|
|
|
|
|
|
Examples: |
|
|
Examples: |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```go |
|
|
encode([4]int8{1, 2, 3, 4}) == [0x01, 0x02, 0x03, 0x04] |
|
|
encode([4]int8{1, 2, 3, 4}) == [0x01, 0x02, 0x03, 0x04] |
|
|
encode([4]int16{1, 2, 3, 4}) == [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04] |
|
|
encode([4]int16{1, 2, 3, 4}) == [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04] |
|
|
encode([4]int{1, 2, 3, 4}) == [0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x04] |
|
|
encode([4]int{1, 2, 3, 4}) == [0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x04] |
|
@ -81,14 +82,15 @@ encode([2]string{"abc", "efg"}) == [0x01, 0x03, 0x61, 0x62, 0x63, 0x01, 0x03, 0x |
|
|
|
|
|
|
|
|
### Slices (variable length) |
|
|
### Slices (variable length) |
|
|
|
|
|
|
|
|
An encoded variable-length array is a length prefix followed by the concatenation of the encoding of its elements. |
|
|
|
|
|
|
|
|
An encoded variable-length array is a length prefix followed by the concatenation of the encoding of |
|
|
|
|
|
its elements. |
|
|
The length-prefix is itself encoded as an `int`. |
|
|
The length-prefix is itself encoded as an `int`. |
|
|
|
|
|
|
|
|
An empty slice is encoded as `0x00`. It is not length-prefixed. |
|
|
An empty slice is encoded as `0x00`. It is not length-prefixed. |
|
|
|
|
|
|
|
|
Examples: |
|
|
Examples: |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```go |
|
|
encode([]int8{}) == [0x00] |
|
|
encode([]int8{}) == [0x00] |
|
|
encode([]int8{1, 2, 3, 4}) == [0x01, 0x04, 0x01, 0x02, 0x03, 0x04] |
|
|
encode([]int8{1, 2, 3, 4}) == [0x01, 0x04, 0x01, 0x02, 0x03, 0x04] |
|
|
encode([]int16{1, 2, 3, 4}) == [0x01, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04] |
|
|
encode([]int16{1, 2, 3, 4}) == [0x01, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04] |
|
@ -97,10 +99,11 @@ encode([]string{"abc", "efg"}) == [0x01, 0x02, 0x01, 0x03, 0x61, 0x62, 0x63, 0x |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### BitArray |
|
|
### BitArray |
|
|
|
|
|
|
|
|
BitArray is encoded as an `int` of the number of bits, and with an array of `uint64` to encode |
|
|
BitArray is encoded as an `int` of the number of bits, and with an array of `uint64` to encode |
|
|
value of each array element. |
|
|
value of each array element. |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```go |
|
|
type BitArray struct { |
|
|
type BitArray struct { |
|
|
Bits int |
|
|
Bits int |
|
|
Elems []uint64 |
|
|
Elems []uint64 |
|
@ -116,7 +119,7 @@ Times before then are invalid. |
|
|
|
|
|
|
|
|
Examples: |
|
|
Examples: |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```go |
|
|
encode(time.Time("Jan 1 00:00:00 UTC 1970")) == [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] |
|
|
encode(time.Time("Jan 1 00:00:00 UTC 1970")) == [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] |
|
|
encode(time.Time("Jan 1 00:00:01 UTC 1970")) == [0x00, 0x00, 0x00, 0x00, 0x3B, 0x9A, 0xCA, 0x00] // 1,000,000,000 ns |
|
|
encode(time.Time("Jan 1 00:00:01 UTC 1970")) == [0x00, 0x00, 0x00, 0x00, 0x3B, 0x9A, 0xCA, 0x00] // 1,000,000,000 ns |
|
|
encode(time.Time("Mon Jan 2 15:04:05 -0700 MST 2006")) == [0x0F, 0xC4, 0xBB, 0xC1, 0x53, 0x03, 0x12, 0x00] |
|
|
encode(time.Time("Mon Jan 2 15:04:05 -0700 MST 2006")) == [0x0F, 0xC4, 0xBB, 0xC1, 0x53, 0x03, 0x12, 0x00] |
|
@ -129,7 +132,7 @@ There is no length-prefix. |
|
|
|
|
|
|
|
|
Examples: |
|
|
Examples: |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```go |
|
|
type MyStruct struct{ |
|
|
type MyStruct struct{ |
|
|
A int |
|
|
A int |
|
|
B string |
|
|
B string |
|
@ -139,7 +142,6 @@ encode(MyStruct{4, "hello", time.Time("Mon Jan 2 15:04:05 -0700 MST 2006")}) == |
|
|
[0x01, 0x04, 0x01, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0F, 0xC4, 0xBB, 0xC1, 0x53, 0x03, 0x12, 0x00] |
|
|
[0x01, 0x04, 0x01, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0F, 0xC4, 0xBB, 0xC1, 0x53, 0x03, 0x12, 0x00] |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Merkle Trees |
|
|
## Merkle Trees |
|
|
|
|
|
|
|
|
Simple Merkle trees are used in numerous places in Tendermint to compute a cryptographic digest of a data structure. |
|
|
Simple Merkle trees are used in numerous places in Tendermint to compute a cryptographic digest of a data structure. |
|
@ -148,23 +150,24 @@ RIPEMD160 is always used as the hashing function. |
|
|
|
|
|
|
|
|
The function `SimpleMerkleRoot` is a simple recursive function defined as follows: |
|
|
The function `SimpleMerkleRoot` is a simple recursive function defined as follows: |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```go |
|
|
func SimpleMerkleRoot(hashes [][]byte) []byte{ |
|
|
func SimpleMerkleRoot(hashes [][]byte) []byte{ |
|
|
switch len(hashes) { |
|
|
|
|
|
case 0: |
|
|
|
|
|
return nil |
|
|
|
|
|
case 1: |
|
|
|
|
|
return hashes[0] |
|
|
|
|
|
default: |
|
|
|
|
|
left := SimpleMerkleRoot(hashes[:(len(hashes)+1)/2]) |
|
|
|
|
|
right := SimpleMerkleRoot(hashes[(len(hashes)+1)/2:]) |
|
|
|
|
|
return RIPEMD160(append(left, right)) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
switch len(hashes) { |
|
|
|
|
|
case 0: |
|
|
|
|
|
return nil |
|
|
|
|
|
case 1: |
|
|
|
|
|
return hashes[0] |
|
|
|
|
|
default: |
|
|
|
|
|
left := SimpleMerkleRoot(hashes[:(len(hashes)+1)/2]) |
|
|
|
|
|
right := SimpleMerkleRoot(hashes[(len(hashes)+1)/2:]) |
|
|
|
|
|
return RIPEMD160(append(left, right)) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
Note we abuse notion and call `SimpleMerkleRoot` with arguments of type `struct` or type `[]struct`. |
|
|
Note we abuse notion and call `SimpleMerkleRoot` with arguments of type `struct` or type `[]struct`. |
|
|
For `struct` arguments, we compute a `[][]byte` by sorting elements of the `struct` according to field name and then hashing them. |
|
|
|
|
|
|
|
|
For `struct` arguments, we compute a `[][]byte` by sorting elements of the `struct` according to |
|
|
|
|
|
field name and then hashing them. |
|
|
For `[]struct` arguments, we compute a `[][]byte` by hashing the individual `struct` elements. |
|
|
For `[]struct` arguments, we compute a `[][]byte` by hashing the individual `struct` elements. |
|
|
|
|
|
|
|
|
## JSON (TMJSON) |
|
|
## JSON (TMJSON) |
|
@ -172,10 +175,12 @@ For `[]struct` arguments, we compute a `[][]byte` by hashing the individual `str |
|
|
Signed messages (eg. votes, proposals) in the consensus are encoded in TMJSON, rather than TMBIN. |
|
|
Signed messages (eg. votes, proposals) in the consensus are encoded in TMJSON, rather than TMBIN. |
|
|
TMJSON is JSON where `[]byte` are encoded as uppercase hex, rather than base64. |
|
|
TMJSON is JSON where `[]byte` are encoded as uppercase hex, rather than base64. |
|
|
|
|
|
|
|
|
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. |
|
|
|
|
|
We call this encoding the CanonicalSignBytes. For instance, CanonicalSignBytes for a vote would look like: |
|
|
|
|
|
|
|
|
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. |
|
|
|
|
|
We call this encoding the CanonicalSignBytes. For instance, CanonicalSignBytes for a vote would look |
|
|
|
|
|
like: |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```json |
|
|
{"chain_id":"my-chain-id","vote":{"block_id":{"hash":DEADBEEF,"parts":{"hash":BEEFDEAD,"total":3}},"height":3,"round":2,"timestamp":1234567890, "type":2} |
|
|
{"chain_id":"my-chain-id","vote":{"block_id":{"hash":DEADBEEF,"parts":{"hash":BEEFDEAD,"total":3}},"height":3,"round":2,"timestamp":1234567890, "type":2} |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
@ -187,16 +192,16 @@ Note how the fields within each level are sorted. |
|
|
|
|
|
|
|
|
TMBIN encode an object and slice it into parts. |
|
|
TMBIN encode an object and slice it into parts. |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```go |
|
|
MakeParts(object, partSize) |
|
|
MakeParts(object, partSize) |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### Part |
|
|
### Part |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```go |
|
|
type Part struct { |
|
|
type Part struct { |
|
|
Index int |
|
|
|
|
|
Bytes byte[] |
|
|
|
|
|
Proof byte[] |
|
|
|
|
|
|
|
|
Index int |
|
|
|
|
|
Bytes byte[] |
|
|
|
|
|
Proof byte[] |
|
|
} |
|
|
} |
|
|
``` |
|
|
``` |