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.

217 lines
7.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
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
8 years ago
9 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. As a [Go interface](https://github.com/tendermint/abci/blob/master/types/application.go), it might look like:
  18. ```golang
  19. // Applications
  20. type Application interface {
  21. // Latest state
  22. Info() ResponseInfo
  23. // Initialization
  24. SetOption(key string, value string) (log string)
  25. InitChain(validators []*Validator)
  26. // Apply a block
  27. BeginBlock(hash []byte, header *Header)
  28. DeliverTx(tx []byte) Result
  29. EndBlock(height uint64) ResponseEndBlock
  30. Commit() Result
  31. // Check validity
  32. CheckTx(tx []byte) Result
  33. // Query for state
  34. Query(query []byte) Result
  35. }
  36. type Result struct {
  37. Code CodeType
  38. Data []byte
  39. Log string // Can be non-deterministic
  40. }
  41. type ResponseInfo struct {
  42. Data string
  43. Version string
  44. LastBlockHeight uint64
  45. LastBlockAppHash []byte
  46. }
  47. type ResponseEndBlock struct {
  48. Diffs []*Validator
  49. }
  50. ```
  51. ## Message Types
  52. ABCI requests/responses are simple Protobuf messages. Check out the [schema file](https://github.com/tendermint/abci/blob/master/types/types.proto).
  53. #### DeliverTx
  54. * __Arguments__:
  55. * `Data ([]byte)`: The request transaction bytes
  56. * __Returns__:
  57. * `Code (uint32)`: Response code
  58. * `Data ([]byte)`: Result bytes, if any
  59. * `Log (string)`: Debug or error message
  60. * __Usage__:<br/>
  61. Append and run a transaction. If the transaction is valid, returns CodeType.OK
  62. #### CheckTx
  63. * __Arguments__:
  64. * `Data ([]byte)`: The request transaction bytes
  65. * __Returns__:
  66. * `Code (uint32)`: Response code
  67. * `Data ([]byte)`: Result bytes, if any
  68. * `Log (string)`: Debug or error message
  69. * __Usage__:<br/>
  70. Validate a mempool transaction, prior to broadcasting or proposing. This message should not mutate the main state, but application
  71. developers may want to keep a separate CheckTx state that gets reset upon Commit.
  72. 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 stte
  73. Transactions are first run through CheckTx before broadcast to peers in the mempool layer.
  74. You can make CheckTx semi-stateful and clear the state upon `Commit` or `BeginBlock`,
  75. to allow for dependent sequences of transactions in the same block.
  76. #### Commit
  77. * __Returns__:
  78. * `Data ([]byte)`: The Merkle root hash
  79. * `Log (string)`: Debug or error message
  80. * __Usage__:<br/>
  81. Return a Merkle root hash of the application state.
  82. #### Query
  83. * __Arguments__:
  84. * `Data ([]byte)`: The query request bytes
  85. * __Returns__:
  86. * `Code (uint32)`: Response code
  87. * `Data ([]byte)`: The query response bytes
  88. * `Log (string)`: Debug or error message
  89. #### Info
  90. * __Returns__:
  91. * `Data (string)`: Some arbitrary information
  92. * `Version (Version)`: Version information
  93. * `LastBlockHeight (uint64)`: Latest block for which the app has called Commit
  94. * `LastBlockAppHash ([]byte)`: Latest result of Commit
  95. * __Usage__:<br/>
  96. Return information about the application state. Used to sync the app with Tendermint on crash/restart.
  97. #### SetOption
  98. * __Arguments__:
  99. * `Key (string)`: Key to set
  100. * `Value (string)`: Value to set for key
  101. * __Returns__:
  102. * `Log (string)`: Debug or error message
  103. * __Usage__:<br/>
  104. Set application options. E.g. Key="mode", Value="mempool" for a mempool connection, or Key="mode", Value="consensus" for a consensus connection.
  105. Other options are application specific.
  106. #### InitChain
  107. * __Arguments__:
  108. * `Validators ([]Validator)`: Initial genesis validators
  109. * __Usage__:<br/>
  110. Called once upon genesis
  111. #### BeginBlock
  112. * __Arguments__:
  113. * `Hash ([]byte)`: The block height that is starting
  114. * `Header (struct{})`: The block header
  115. * __Usage__:<br/>
  116. Signals the beginning of a new block. Called prior to any DeliverTxs. The header is expected to at least contain the Height.
  117. #### EndBlock
  118. * __Arguments__:
  119. * `Height (uint64)`: The block height that ended
  120. * __Returns__:
  121. * `Diffs ([]Validator)`: Changed validators with new voting powers (0 to remove)
  122. * __Usage__:<br/>
  123. Signals the end of a block. Called prior to each Commit after all transactions. Validator set is updated with the result.
  124. #### Echo
  125. * __Arguments__:
  126. * `Message (string)`: A string to echo back
  127. * __Returns__:
  128. * `Message (string)`: The input string
  129. * __Usage__:<br/>
  130. * Echo a string to test an abci client/server implementation
  131. #### Flush
  132. * __Usage__:<br/>
  133. * 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.
  134. # Implementations
  135. The ABCI is a client/server interface where the replication engine (blockchain) forms the client
  136. and the state machine (application) forms the server.
  137. As blocks are committed in the blockchain, they are forwarded to the application.
  138. This repository provides two implementations of an ABCI client & server: via socket and via GRPC.
  139. ## Socket
  140. ABCI is best implemented as a streaming protocol.
  141. The socket implementation provides for asynchronous, ordered message passing over unix or tcp.
  142. Messages are serialized using Protobuf3 and length-prefixed.
  143. 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.
  144. 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...`.
  145. ## GRPC
  146. GRPC is an rpc framework native to Protocol Buffers with support in many languages.
  147. Implementing the ABCI using GRPC can allow for faster prototyping, but is expected to be much slower than
  148. the ordered, asynchronous socket protocol.
  149. Note the length-prefixing used in the socket implementation does not apply for GRPC.
  150. # Tools and Apps
  151. The `abci-cli` tool wraps any ABCI client and can be used for probing/testing an ABCI application.
  152. See the [tutorial](https://tendermint.com/intro/getting-started/first-abci) for more details.
  153. Multiple example apps are included:
  154. - the `counter` application, which illustrates nonce checking in txs
  155. - the `dummy` application, which illustrates a simple key-value merkle tree
  156. - the `dummy --persistent` application, which augments the dummy with persistence and validator set changes
  157. # Build
  158. To build the protobuf code:
  159. ```
  160. make protoc
  161. ```
  162. See `protoc --help` and [the grpc docs](https://www.grpc.io/docs) for examples and details of other languages