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.

192 lines
7.4 KiB

  1. # Application Blockchain Interface (ABCI)
  2. ABCI is the interface between Tendermint (a state-machine replication engine)
  3. and an application (the actual state machine).
  4. The ABCI message types are defined in a [protobuf
  5. file](https://github.com/tendermint/tendermint/blob/develop/abci/types/types.proto).
  6. For full details on the ABCI message types and protocol, see the [ABCI
  7. specification](https://github.com/tendermint/tendermint/blob/develop/docs/abci-spec.md).
  8. Be sure to read the specification if you're trying to build an ABCI app!
  9. For additional details on server implementation, see the [ABCI
  10. readme](https://github.com/tendermint/tendermint/blob/develop/abci/README.md).
  11. Here we provide some more details around the use of ABCI by Tendermint and
  12. clarify common "gotchas".
  13. ## ABCI connections
  14. Tendermint opens 3 ABCI connections to the app: one for Consensus, one for
  15. Mempool, one for Queries.
  16. ## Async vs Sync
  17. The main ABCI server (ie. non-GRPC) provides ordered asynchronous messages.
  18. This is useful for DeliverTx and CheckTx, since it allows Tendermint to forward
  19. transactions to the app before it's finished processing previous ones.
  20. Thus, DeliverTx and CheckTx messages are sent asycnhronously, while all other
  21. messages are sent synchronously.
  22. ## CheckTx and Commit
  23. It is typical to hold three distinct states in an ABCI app: CheckTxState, DeliverTxState,
  24. QueryState. The QueryState contains the latest committed state for a block.
  25. The CheckTxState and DeliverTxState may be updated concurrently with one another.
  26. Before Commit is called, Tendermint locks and flushes the mempool so that no new changes will happen
  27. to CheckTxState. When Commit completes, it unlocks the mempool.
  28. Thus, during Commit, it is safe to reset the QueryState and the CheckTxState to the latest DeliverTxState
  29. (ie. the new state from executing all the txs in the block).
  30. Note, however, that it is not possible to send transactions to Tendermint during Commit - if your app
  31. tries to send a `/broadcast_tx` to Tendermint during Commit, it will deadlock.
  32. ## EndBlock Validator Updates
  33. Updates to the Tendermint validator set can be made by returning `Validator`
  34. objects in the `ResponseBeginBlock`:
  35. ```
  36. message Validator {
  37. bytes address = 1;
  38. PubKey pub_key = 2;
  39. int64 power = 3;
  40. }
  41. message PubKey {
  42. string type = 1;
  43. bytes data = 2;
  44. }
  45. ```
  46. The `pub_key` currently supports two types:
  47. - `type = "ed25519" and `data = <raw 32-byte public key>`
  48. - `type = "secp256k1" and `data = <33-byte OpenSSL compressed public key>`
  49. If the address is provided, it must match the address of the pubkey, as
  50. specified [here](/docs/spec/blockchain/encoding.md#Addresses)
  51. (Note: In the v0.19 series, the `pub_key` is the [Amino encoded public
  52. key](/docs/spec/blockchain/encoding.md#public-key-cryptography).
  53. For Ed25519 pubkeys, the Amino prefix is always "1624DE6220". For example, the 32-byte Ed25519 pubkey
  54. `76852933A4686A721442E931A8415F62F5F1AEDF4910F1F252FB393F74C40C85` would be
  55. Amino encoded as
  56. `1624DE622076852933A4686A721442E931A8415F62F5F1AEDF4910F1F252FB393F74C40C85`)
  57. (Note: In old versions of Tendermint (pre-v0.19.0), the pubkey is just prefixed with a
  58. single type byte, so for ED25519 we'd have `pub_key = 0x1 | pub`)
  59. The `power` is the new voting power for the validator, with the
  60. following rules:
  61. - power must be non-negative
  62. - if power is 0, the validator must already exist, and will be removed from the
  63. validator set
  64. - if power is non-0:
  65. - if the validator does not already exist, it will be added to the validator
  66. set with the given power
  67. - if the validator does already exist, its power will be adjusted to the given power
  68. ## InitChain Validator Updates
  69. ResponseInitChain has the option to return a list of validators.
  70. If the list is not empty, Tendermint will adopt it for the validator set.
  71. This way the application can determine the initial validator set for the
  72. blockchain.
  73. Note that if addressses are included in the returned validators, they must match
  74. the address of the public key.
  75. ResponseInitChain also includes ConsensusParams, but these are presently
  76. ignored.
  77. ## Query
  78. Query is a generic message type with lots of flexibility to enable diverse sets
  79. of queries from applications. Tendermint has no requirements from the Query
  80. message for normal operation - that is, the ABCI app developer need not implement Query functionality if they do not wish too.
  81. That said, Tendermint makes a number of queries to support some optional
  82. features. These are:
  83. ### Peer Filtering
  84. When Tendermint connects to a peer, it sends two queries to the ABCI application
  85. using the following paths, with no additional data:
  86. - `/p2p/filter/addr/<IP:PORT>`, where `<IP:PORT>` denote the IP address and
  87. the port of the connection
  88. - `p2p/filter/id/<ID>`, where `<ID>` is the peer node ID (ie. the
  89. pubkey.Address() for the peer's PubKey)
  90. If either of these queries return a non-zero ABCI code, Tendermint will refuse
  91. to connect to the peer.
  92. ## Info and the Handshake/Replay
  93. On startup, Tendermint calls Info on the Query connection to get the latest
  94. committed state of the app. The app MUST return information consistent with the
  95. last block it succesfully completed Commit for.
  96. If the app succesfully committed block H but not H+1, then `last_block_height =
  97. H` and `last_block_app_hash = <hash returned by Commit for block H>`. If the app
  98. failed during the Commit of block H, then `last_block_height = H-1` and
  99. `last_block_app_hash = <hash returned by Commit for block H-1, which is the hash
  100. in the header of block H>`.
  101. We now distinguish three heights, and describe how Tendermint syncs itself with
  102. the app.
  103. ```
  104. storeBlockHeight = height of the last block Tendermint saw a commit for
  105. stateBlockHeight = height of the last block for which Tendermint completed all
  106. block processing and saved all ABCI results to disk
  107. appBlockHeight = height of the last block for which ABCI app succesfully
  108. completely Commit
  109. ```
  110. Note we always have `storeBlockHeight >= stateBlockHeight` and `storeBlockHeight >= appBlockHeight`
  111. Note also we never call Commit on an ABCI app twice for the same height.
  112. The procedure is as follows.
  113. First, some simeple start conditions:
  114. If `appBlockHeight == 0`, then call InitChain.
  115. If `storeBlockHeight == 0`, we're done.
  116. Now, some sanity checks:
  117. If `storeBlockHeight < appBlockHeight`, error
  118. If `storeBlockHeight < stateBlockHeight`, panic
  119. If `storeBlockHeight > stateBlockHeight+1`, panic
  120. Now, the meat:
  121. If `storeBlockHeight == stateBlockHeight && appBlockHeight < storeBlockHeight`,
  122. replay all blocks in full from `appBlockHeight` to `storeBlockHeight`.
  123. This happens if we completed processing the block, but the app forgot its height.
  124. If `storeBlockHeight == stateBlockHeight && appBlockHeight == storeBlockHeight`, we're done
  125. This happens if we crashed at an opportune spot.
  126. If `storeBlockHeight == stateBlockHeight+1`
  127. This happens if we started processing the block but didn't finish.
  128. If `appBlockHeight < stateBlockHeight`
  129. replay all blocks in full from `appBlockHeight` to `storeBlockHeight-1`,
  130. and replay the block at `storeBlockHeight` using the WAL.
  131. This happens if the app forgot the last block it committed.
  132. If `appBlockHeight == stateBlockHeight`,
  133. replay the last block (storeBlockHeight) in full.
  134. This happens if we crashed before the app finished Commit
  135. If appBlockHeight == storeBlockHeight {
  136. update the state using the saved ABCI responses but dont run the block against the real app.
  137. This happens if we crashed after the app finished Commit but before Tendermint saved the state.