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.

185 lines
7.3 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/app-dev/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. PubKey pub_key
  38. int64 power
  39. }
  40. message PubKey {
  41. string type
  42. bytes data
  43. }
  44. ```
  45. The `pub_key` currently supports two types:
  46. - `type = "ed25519" and`data = <raw 32-byte public key>`
  47. - `type = "secp256k1" and `data = <33-byte OpenSSL compressed public key>`
  48. If the address is provided, it must match the address of the pubkey, as
  49. specified [here](/docs/spec/blockchain/encoding.md#Addresses)
  50. (Note: In the v0.19 series, the `pub_key` is the [Amino encoded public
  51. key](/docs/spec/blockchain/encoding.md#public-key-cryptography).
  52. For Ed25519 pubkeys, the Amino prefix is always "1624DE6220". For example, the 32-byte Ed25519 pubkey
  53. `76852933A4686A721442E931A8415F62F5F1AEDF4910F1F252FB393F74C40C85` would be
  54. Amino encoded as
  55. `1624DE622076852933A4686A721442E931A8415F62F5F1AEDF4910F1F252FB393F74C40C85`)
  56. (Note: In old versions of Tendermint (pre-v0.19.0), the pubkey is just prefixed with a
  57. single type byte, so for ED25519 we'd have `pub_key = 0x1 | pub`)
  58. The `power` is the new voting power for the validator, with the
  59. following rules:
  60. - power must be non-negative
  61. - if power is 0, the validator must already exist, and will be removed from the
  62. validator set
  63. - if power is non-0:
  64. - if the validator does not already exist, it will be added to the validator
  65. set with the given power
  66. - if the validator does already exist, its power will be adjusted to the given power
  67. ## InitChain Validator Updates
  68. ResponseInitChain has the option to return a list of validators.
  69. If the list is not empty, Tendermint will adopt it for the validator set.
  70. This way the application can determine the initial validator set for the
  71. blockchain.
  72. ResponseInitChain also includes ConsensusParams, but these are presently
  73. ignored.
  74. ## Query
  75. Query is a generic message type with lots of flexibility to enable diverse sets
  76. of queries from applications. Tendermint has no requirements from the Query
  77. message for normal operation - that is, the ABCI app developer need not implement Query functionality if they do not wish too.
  78. That said, Tendermint makes a number of queries to support some optional
  79. features. These are:
  80. ### Peer Filtering
  81. When Tendermint connects to a peer, it sends two queries to the ABCI application
  82. using the following paths, with no additional data:
  83. - `/p2p/filter/addr/<IP:PORT>`, where `<IP:PORT>` denote the IP address and
  84. the port of the connection
  85. - `p2p/filter/id/<ID>`, where `<ID>` is the peer node ID (ie. the
  86. pubkey.Address() for the peer's PubKey)
  87. If either of these queries return a non-zero ABCI code, Tendermint will refuse
  88. to connect to the peer.
  89. ## Info and the Handshake/Replay
  90. On startup, Tendermint calls Info on the Query connection to get the latest
  91. committed state of the app. The app MUST return information consistent with the
  92. last block it succesfully completed Commit for.
  93. If the app succesfully committed block H but not H+1, then `last_block_height = H` and `last_block_app_hash = <hash returned by Commit for block H>`. If the app
  94. failed during the Commit of block H, then `last_block_height = H-1` and
  95. `last_block_app_hash = <hash returned by Commit for block H-1, which is the hash in the header of block H>`.
  96. We now distinguish three heights, and describe how Tendermint syncs itself with
  97. the app.
  98. ```
  99. storeBlockHeight = height of the last block Tendermint saw a commit for
  100. stateBlockHeight = height of the last block for which Tendermint completed all
  101. block processing and saved all ABCI results to disk
  102. appBlockHeight = height of the last block for which ABCI app succesfully
  103. completely Commit
  104. ```
  105. Note we always have `storeBlockHeight >= stateBlockHeight` and `storeBlockHeight >= appBlockHeight`
  106. Note also we never call Commit on an ABCI app twice for the same height.
  107. The procedure is as follows.
  108. First, some simeple start conditions:
  109. If `appBlockHeight == 0`, then call InitChain.
  110. If `storeBlockHeight == 0`, we're done.
  111. Now, some sanity checks:
  112. If `storeBlockHeight < appBlockHeight`, error
  113. If `storeBlockHeight < stateBlockHeight`, panic
  114. If `storeBlockHeight > stateBlockHeight+1`, panic
  115. Now, the meat:
  116. If `storeBlockHeight == stateBlockHeight && appBlockHeight < storeBlockHeight`,
  117. replay all blocks in full from `appBlockHeight` to `storeBlockHeight`.
  118. This happens if we completed processing the block, but the app forgot its height.
  119. If `storeBlockHeight == stateBlockHeight && appBlockHeight == storeBlockHeight`, we're done
  120. This happens if we crashed at an opportune spot.
  121. If `storeBlockHeight == stateBlockHeight+1`
  122. This happens if we started processing the block but didn't finish.
  123. If `appBlockHeight < stateBlockHeight`
  124. replay all blocks in full from `appBlockHeight` to `storeBlockHeight-1`,
  125. and replay the block at `storeBlockHeight` using the WAL.
  126. This happens if the app forgot the last block it committed.
  127. If `appBlockHeight == stateBlockHeight`,
  128. replay the last block (storeBlockHeight) in full.
  129. This happens if we crashed before the app finished Commit
  130. If appBlockHeight == storeBlockHeight {
  131. update the state using the saved ABCI responses but dont run the block against the real app.
  132. This happens if we crashed after the app finished Commit but before Tendermint saved the state.