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.

232 lines
9.9 KiB

8 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
8 years ago
8 years ago
  1. # Application BlockChain Interface (ABCI)
  2. [![CircleCI](https://circleci.com/gh/tendermint/abci.svg?style=svg)](https://circleci.com/gh/tendermint/abci)
  3. Blockchains are a system for multi-master state machine replication.
  4. **ABCI** is an interface that defines the boundary between the replication engine (the blockchain),
  5. and the state machine (the application).
  6. By using a socket protocol, we enable a consensus engine running in one process
  7. to manage an application state running in another.
  8. For more information on ABCI, motivations, and tutorials, please visit [our blog post](https://tendermint.com/blog/abci-the-application-blockchain-interface),
  9. and the more detailed [application developer's guide](https://tendermint.com/docs/guides/app-development).
  10. Previously, the ABCI was just referred to as TMSP.
  11. Other implementations:
  12. * [cpp-tmsp](https://github.com/mdyring/cpp-tmsp) by Martin Dyring-Andersen
  13. * [js-tmsp](https://github.com/tendermint/js-tmsp)
  14. * [jABCI](https://github.com/jTendermint/jabci) for Java
  15. # Specification
  16. The [primary specification](https://github.com/tendermint/abci/blob/master/types/types.proto) is made using Protocol Buffers.
  17. - The Protobuf file defining ABCI message types, and the optional GRPC interface. To build, run `make protoc`
  18. - See `protoc --help` and [the GRPC docs](https://www.grpc.io/docs) for examples and details of other languages.
  19. TendermintCore runs a client, and the ABCI application runs a server. There are three Golang implementation of ABCI client and server.
  20. 1. ABCI-socket: Asynchronous, ordered message passing over Unix or TCP sockets. Messages are serialized using Protobuf and length prefixed.
  21. 2. GRPC: Synchronous (slow) implementation using GRPC.
  22. 3. Golang in-process: If the ABCI appliation is written in Golang, it is possible to compile both TendermintCore and the application as one binary.
  23. ```golang
  24. // Applications
  25. type Application interface {
  26. // Latest state
  27. Info() ResponseInfo
  28. // Initialization
  29. SetOption(key string, value string) (log string)
  30. InitChain(validators []*Validator)
  31. // Apply a block
  32. BeginBlock(hash []byte, header *Header)
  33. DeliverTx(tx []byte) Result
  34. EndBlock(height uint64) ResponseEndBlock
  35. Commit() Result
  36. // Check validity
  37. CheckTx(tx []byte) Result
  38. // Query for state
  39. Query(query []byte) Result
  40. }
  41. type Result struct {
  42. Code CodeType
  43. Data []byte
  44. Log string // Can be non-deterministic
  45. }
  46. type ResponseInfo struct {
  47. Data string
  48. Version string
  49. LastBlockHeight uint64
  50. LastBlockAppHash []byte
  51. }
  52. type ResponseEndBlock struct {
  53. Diffs []*Validator
  54. }
  55. _TODO: merge information from https://tendermint.com/blog/tendermint-0-8-release_
  56. ## Message Types
  57. ABCI requests/responses are simple Protobuf messages. Check out the [schema file](https://github.com/tendermint/abci/blob/master/types/types.proto).
  58. #### DeliverTx
  59. * __Arguments__:
  60. * `Data ([]byte)`: The request transaction bytes
  61. * __Returns__:
  62. * `Code (uint32)`: Response code
  63. * `Data ([]byte)`: Result bytes, if any
  64. * `Log (string)`: Debug or error message
  65. * __Usage__:<br/>
  66. Append and run a transaction. If the transaction is valid, returns CodeType.OK
  67. #### CheckTx
  68. * __Arguments__:
  69. * `Data ([]byte)`: The request transaction bytes
  70. * __Returns__:
  71. * `Code (uint32)`: Response code
  72. * `Data ([]byte)`: Result bytes, if any
  73. * `Log (string)`: Debug or error message
  74. * __Usage__:<br/>
  75. Validate a mempool transaction, prior to broadcasting or proposing. This message should not mutate the main state, but application
  76. developers may want to keep a separate CheckTx state that gets reset upon Commit.
  77. CheckTx can happen interspersed with DeliverTx, but they happen on different connections - CheckTx from the mempool connection, and DeliverTx from the consensus connection. During Commit, the mempool is locked, so you can reset the mempool state to the latest state after running all those delivertxs, and then the mempool will re-run whatever txs it has against that latest mempool state.
  78. Transactions are first run through CheckTx before broadcast to peers in the mempool layer.
  79. You can make CheckTx semi-stateful and clear the state upon `Commit` or `BeginBlock`,
  80. to allow for dependent sequences of transactions in the same block.
  81. #### Commit
  82. * __Returns__:
  83. * `Data ([]byte)`: The Merkle root hash
  84. * `Log (string)`: Debug or error message
  85. * __Usage__:<br/>
  86. Return a Merkle root hash of the application state.
  87. #### Query
  88. * __Arguments__:
  89. * `Data ([]byte)`: Raw query bytes. Can be used with or in lieu of Path.
  90. * `Path (string)`: Path of request, like an HTTP GET path. Can be used with or in liue of Data.
  91. * Apps MUST interpret '/store' as a query by key on the underlying store. The key SHOULD be specified in the Data field.
  92. * Apps SHOULD allow queries over specific types like '/accounts/...' or '/votes/...'
  93. * `Height (uint64)`: The block height for which you want the query (default=0 returns data for the latest committed block). Note that this is the height of the block containing the application's Merkle root hash, which represents the state as it was after committing the block at Height-1
  94. * `Prove (bool)`: Return Merkle proof with response if possible
  95. * __Returns__:
  96. * `Code (uint32)`: Response code
  97. * `Key ([]byte)`: The key of the matching data
  98. * `Value ([]byte)`: The value of the matching data
  99. * `Proof ([]byte)`: Proof for the data, if requested
  100. * `Height (uint64)`: The block height from which data was derived. Note that this is the height of the block containing the application's Merkle root hash, which represents the state as it was after committing the block at Height-1
  101. * `Log (string)`: Debug or error message
  102. *Please note* The current implementation of go-merkle doesn't support querying proofs from past blocks, so for the present moment, any height other than 0 will return an error (recall height=0 defaults to latest block). Hopefully this will be improved soon(ish)
  103. #### Info
  104. * __Returns__:
  105. * `Data (string)`: Some arbitrary information
  106. * `Version (Version)`: Version information
  107. * `LastBlockHeight (uint64)`: Latest block for which the app has called Commit
  108. * `LastBlockAppHash ([]byte)`: Latest result of Commit
  109. * __Usage__:<br/>
  110. Return information about the application state. Used to sync the app with Tendermint on crash/restart.
  111. #### SetOption
  112. * __Arguments__:
  113. * `Key (string)`: Key to set
  114. * `Value (string)`: Value to set for key
  115. * __Returns__:
  116. * `Log (string)`: Debug or error message
  117. * __Usage__:<br/>
  118. Set application options. E.g. Key="mode", Value="mempool" for a mempool connection, or Key="mode", Value="consensus" for a consensus connection.
  119. Other options are application specific.
  120. #### InitChain
  121. * __Arguments__:
  122. * `Validators ([]Validator)`: Initial genesis validators
  123. * __Usage__:<br/>
  124. Called once upon genesis
  125. #### BeginBlock
  126. * __Arguments__:
  127. * `Hash ([]byte)`: The block's hash. This can be derived from the block header.
  128. * `Header (struct{})`: The block header
  129. * __Usage__:<br/>
  130. Signals the beginning of a new block. Called prior to any DeliverTxs. The header is expected to at least contain the Height.
  131. #### EndBlock
  132. * __Arguments__:
  133. * `Height (uint64)`: The block height that ended
  134. * __Returns__:
  135. * `Diffs ([]Validator)`: Changed validators with new voting powers (0 to remove)
  136. * __Usage__:<br/>
  137. Signals the end of a block. Called prior to each Commit after all transactions. Validator set is updated with the result.
  138. #### Echo
  139. * __Arguments__:
  140. * `Message (string)`: A string to echo back
  141. * __Returns__:
  142. * `Message (string)`: The input string
  143. * __Usage__:<br/>
  144. * Echo a string to test an abci client/server implementation
  145. #### Flush
  146. * __Usage__:<br/>
  147. * Signals that messages queued on the client should be flushed to the server. It is called periodically by the client implementation to ensure asynchronous requests are actually sent, and is called immediately to make a synchronous request, which returns when the Flush response comes back.
  148. # Implementations
  149. The ABCI is a client/server interface where the replication engine (blockchain) forms the client
  150. and the state machine (application) forms the server.
  151. As blocks are committed in the blockchain, they are forwarded to the application.
  152. This repository provides two implementations of an ABCI client & server: via socket and via GRPC.
  153. ## Socket
  154. ABCI is best implemented as a streaming protocol.
  155. The socket implementation provides for asynchronous, ordered message passing over unix or tcp.
  156. Messages are serialized using Protobuf3 and length-prefixed.
  157. Protobuf3 doesn't have an official length-prefix standard, so we use our own. The first byte represents the length of the big-endian encoded length.
  158. For example, if the Protobuf3 encoded ABCI message is `0xDEADBEEF` (4 bytes), the length-prefixed message is `0x0104DEADBEEF`. If the Protobuf3 encoded ABCI message is 65535 bytes long, the length-prefixed message would be like `0x02FFFF...`.
  159. ## GRPC
  160. GRPC is an rpc framework native to Protocol Buffers with support in many languages.
  161. Implementing the ABCI using GRPC can allow for faster prototyping, but is expected to be much slower than
  162. the ordered, asynchronous socket protocol.
  163. Note the length-prefixing used in the socket implementation does not apply for GRPC.
  164. # Tools and Apps
  165. The `abci-cli` tool wraps any ABCI client and can be used for probing/testing an ABCI application.
  166. See the [tutorial](https://tendermint.com/intro/getting-started/first-abci) for more details.
  167. Multiple example apps are included:
  168. - the `counter` application, which illustrates nonce checking in txs
  169. - the `dummy` application, which illustrates a simple key-value merkle tree
  170. - the `dummy --persistent` application, which augments the dummy with persistence and validator set changes
  171. # Build
  172. To build the protobuf code:
  173. ```
  174. make protoc
  175. ```
  176. See `protoc --help` and [the grpc docs](https://www.grpc.io/docs) for examples and details of other languages