OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / abci / README.md
1 # Application BlockChain Interface (ABCI)
2
3 [![CircleCI](https://circleci.com/gh/tendermint/abci.svg?style=svg)](https://circleci.com/gh/tendermint/abci)
4
5 Blockchains are a system for multi-master state machine replication.
6 **ABCI** is an interface that defines the boundary between the replication engine (the blockchain),
7 and the state machine (the application).
8 By using a socket protocol, we enable a consensus engine running in one process
9 to manage an application state running in another.
10
11 ## Install
12
13 ```
14 go get github.com/tendermint/abci
15 cd $GOPATH/src/github.com/tendermint/abci
16 glide install
17 go install ./cmd/...
18 ```
19
20 For background information on ABCI, motivations, and tendermint, please visit [the documentation](http://tendermint.readthedocs.io/en/master/).
21 The two guides to focus on are the `Application Development Guide` and `Using ABCI-CLI`.
22
23 Previously, the ABCI was referred to as TMSP.
24
25 The community has provided a number of addtional implementations, see the `Tendermint Ecosystem` in [the documentation](http://tendermint.readthedocs.io/en/master/).
26
27 ## Implementation
28
29 We provide three implementations of the ABCI in Go:
30
31 - ABCI-socket
32 - GRPC
33 - Golang in-process
34
35 ### Socket
36
37 ABCI is best implemented as a streaming protocol.
38 The socket implementation provides for asynchronous, ordered message passing over unix or tcp.
39 Messages are serialized using Protobuf3 and length-prefixed.
40 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.
41
42 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...`.
43
44 ### GRPC
45
46 GRPC is an rpc framework native to Protocol Buffers with support in many languages.
47 Implementing the ABCI using GRPC can allow for faster prototyping, but is expected to be much slower than
48 the ordered, asynchronous socket protocol.
49
50 Note the length-prefixing used in the socket implementation does not apply for GRPC.
51
52 ### In Process
53
54 The simplest implementation just uses function calls within Go.
55 This means ABCI applications written in Golang can be compiled with TendermintCore and run as a single binary.
56
57 ## Example Apps
58
59 The `abci-cli` tool wraps any ABCI client and can be used for probing/testing an ABCI application.
60 See [the documentation](http://tendermint.readthedocs.io/en/master/) for more details.
61
62 Multiple example apps are included:
63 - the `abci-cli counter` application, which illustrates nonce checking in txs
64 - the `abci-cli dummy` application, which illustrates a simple key-value merkle tree
65 - the `abci-cli dummy --persistent` application, which augments the dummy with persistence and validator set changes
66
67 ## Specification
68
69 The [primary specification](https://github.com/tendermint/abci/blob/master/types/types.proto) is made using Protocol Buffers.
70 To build it, run
71
72 ```
73 make protoc
74 ```
75
76 See `protoc --help` and [the Protocol Buffers site](https://developers.google.com/protocol-buffers/) for details on compiling for other languages.
77 Note we also include a [GRPC](http://www.grpc.io/docs) service definition.
78
79 For the specification as an interface in Go, see the [types/application.go file](https://github.com/tendermint/abci/blob/master/types/application.go).
80
81 ### Message Types
82
83 ABCI requests/responses are defined as simple Protobuf messages in [this schema file](https://github.com/tendermint/abci/blob/master/types/types.proto).
84 TendermintCore sends the requests, and the ABCI application sends the responses.
85 Here, we describe the requests and responses as function arguments and return values, and make some notes about usage:
86
87 #### DeliverTx
88   * __Arguments__:
89     * `Data ([]byte)`: The request transaction bytes
90   * __Returns__:
91     * `Code (uint32)`: Response code
92     * `Data ([]byte)`: Result bytes, if any
93     * `Log (string)`: Debug or error message
94   * __Usage__:<br/>
95     Append and run a transaction.  If the transaction is valid, returns CodeType.OK
96
97 #### CheckTx
98   * __Arguments__:
99     * `Data ([]byte)`: The request transaction bytes
100   * __Returns__:
101     * `Code (uint32)`: Response code
102     * `Data ([]byte)`: Result bytes, if any
103     * `Log (string)`: Debug or error message
104   * __Usage__:<br/>
105     Validate a mempool transaction, prior to broadcasting or proposing.  This message should not mutate the main state, but application
106     developers may want to keep a separate CheckTx state that gets reset upon Commit.
107
108     CheckTx can happen interspersed with DeliverTx, but they happen on different ABCI 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.
109
110     Transactions are first run through CheckTx before broadcast to peers in the mempool layer.
111     You can make CheckTx semi-stateful and clear the state upon `Commit` or `BeginBlock`,
112     to allow for dependent sequences of transactions in the same block.
113
114 #### Commit
115   * __Returns__:
116     * `Data ([]byte)`: The Merkle root hash
117     * `Log (string)`: Debug or error message
118   * __Usage__:<br/>
119     Return a Merkle root hash of the application state.
120
121 #### Query
122   * __Arguments__:
123     * `Data ([]byte)`: Raw query bytes.  Can be used with or in lieu of Path.
124     * `Path (string)`: Path of request, like an HTTP GET path.  Can be used with or in liue of Data.
125       * Apps MUST interpret '/store' as a query by key on the underlying store.  The key SHOULD be specified in the Data field.
126       * Apps SHOULD allow queries over specific types like '/accounts/...' or '/votes/...'
127     * `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
128     * `Prove (bool)`: Return Merkle proof with response if possible
129   * __Returns__:
130     * `Code (uint32)`: Response code
131     * `Key ([]byte)`: The key of the matching data
132     * `Value ([]byte)`: The value of the matching data
133     * `Proof ([]byte)`: Proof for the data, if requested
134     * `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
135     * `Log (string)`: Debug or error message
136   *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)
137
138 #### Info
139   * __Returns__:
140     * `Data (string)`: Some arbitrary information
141     * `Version (Version)`: Version information
142     * `LastBlockHeight (uint64)`: Latest block for which the app has called Commit
143     * `LastBlockAppHash ([]byte)`: Latest result of Commit
144
145   * __Usage__:<br/>
146     Return information about the application state. Used to sync the app with Tendermint on crash/restart.
147
148 #### SetOption
149   * __Arguments__:
150     * `Key (string)`: Key to set
151     * `Value (string)`: Value to set for key
152   * __Returns__:
153     * `Log (string)`: Debug or error message
154   * __Usage__:<br/>
155     Set application options.  E.g. Key="mode", Value="mempool" for a mempool connection, or Key="mode", Value="consensus" for a consensus connection.
156     Other options are application specific.
157
158 #### InitChain
159   * __Arguments__:
160     * `Validators ([]Validator)`: Initial genesis validators
161   * __Usage__:<br/>
162     Called once upon genesis
163
164 #### BeginBlock
165   * __Arguments__:
166     * `Hash ([]byte)`: The block's hash.  This can be derived from the block header.
167     * `Header (struct{})`: The block header
168   * __Usage__:<br/>
169     Signals the beginning of a new block. Called prior to any DeliverTxs. The header is expected to at least contain the Height.
170
171 #### EndBlock
172   * __Arguments__:
173     * `Height (uint64)`: The block height that ended
174   * __Returns__:
175     * `Diffs ([]Validator)`: Changed validators with new voting powers (0 to remove)
176   * __Usage__:<br/>
177     Signals the end of a block.  Called prior to each Commit after all transactions. Validator set is updated with the result.
178
179 #### Echo
180   * __Arguments__:
181     * `Message (string)`: A string to echo back
182   * __Returns__:
183     * `Message (string)`: The input string
184   * __Usage__:<br/>
185     * Echo a string to test an abci client/server implementation
186
187 #### Flush
188   * __Usage__:<br/>
189     * 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.