tendermint/README.md

233 lines
9.9 KiB
Markdown
Raw Normal View History

2017-01-13 00:09:28 -08:00
# Application BlockChain Interface (ABCI)
2015-11-02 07:39:53 -08:00
2017-01-12 12:47:55 -08:00
[![CircleCI](https://circleci.com/gh/tendermint/abci.svg?style=svg)](https://circleci.com/gh/tendermint/abci)
2016-07-06 14:01:20 -07:00
2017-01-13 00:09:28 -08:00
Blockchains are a system for multi-master state machine replication.
**ABCI** is an interface that defines the boundary between the replication engine (the blockchain),
2017-01-23 20:14:14 -08:00
and the state machine (the application).
2017-01-13 00:09:28 -08:00
By using a socket protocol, we enable a consensus engine running in one process
to manage an application state running in another.
2015-11-02 07:39:53 -08:00
2017-02-06 15:10:20 -08:00
For more information on ABCI, motivations, and tutorials, please visit [our blog post](https://tendermint.com/blog/abci-the-application-blockchain-interface),
and the more detailed [application developer's guide](https://tendermint.com/docs/guides/app-development).
2017-01-13 00:09:28 -08:00
Previously, the ABCI was just referred to as TMSP.
2016-04-15 15:01:05 -07:00
Other implementations:
2017-01-13 00:09:28 -08:00
* [cpp-tmsp](https://github.com/mdyring/cpp-tmsp) by Martin Dyring-Andersen
* [js-tmsp](https://github.com/tendermint/js-tmsp)
2017-02-19 07:46:57 -08:00
* [jABCI](https://github.com/jTendermint/jabci) for Java
2016-04-15 15:01:05 -07:00
2017-01-13 00:09:28 -08:00
# Specification
2017-01-13 00:09:28 -08:00
The [primary specification](https://github.com/tendermint/abci/blob/master/types/types.proto) is made using Protocol Buffers.
2017-01-27 22:27:32 -08:00
- The Protobuf file defining ABCI message types, and the optional GRPC interface. To build, run `make protoc`
- See `protoc --help` and [the GRPC docs](https://www.grpc.io/docs) for examples and details of other languages.
2017-01-27 22:27:32 -08:00
TendermintCore runs a client, and the ABCI application runs a server. There are three Golang implementation of ABCI client and server.
2017-01-27 22:27:32 -08:00
1. ABCI-socket: Asynchronous, ordered message passing over Unix or TCP sockets. Messages are serialized using Protobuf and length prefixed.
2. GRPC: Synchronous (slow) implementation using GRPC.
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.
2017-02-17 22:36:14 -08:00
```golang
2017-01-13 00:09:28 -08:00
// Applications
type Application interface {
2017-01-13 00:09:28 -08:00
// Latest state
Info() ResponseInfo
2017-01-13 00:09:28 -08:00
// Initialization
SetOption(key string, value string) (log string)
InitChain(validators []*Validator)
2017-01-13 00:09:28 -08:00
// Apply a block
BeginBlock(hash []byte, header *Header)
DeliverTx(tx []byte) Result
EndBlock(height uint64) ResponseEndBlock
Commit() Result
2016-05-04 13:37:22 -07:00
2017-01-13 00:09:28 -08:00
// Check validity
CheckTx(tx []byte) Result
2016-05-04 13:37:22 -07:00
2017-01-13 00:09:28 -08:00
// Query for state
Query(query []byte) Result
}
type Result struct {
Code CodeType
Data []byte
Log string // Can be non-deterministic
}
type ResponseInfo struct {
Data string
Version string
LastBlockHeight uint64
LastBlockAppHash []byte
}
2016-05-04 13:37:22 -07:00
2017-01-13 00:09:28 -08:00
type ResponseEndBlock struct {
Diffs []*Validator
}
2016-07-23 16:05:46 -07:00
2017-01-27 22:27:32 -08:00
_TODO: merge information from https://tendermint.com/blog/tendermint-0-8-release_
2017-01-13 00:09:28 -08:00
## Message Types
2015-11-02 07:39:53 -08:00
2017-01-12 12:47:55 -08:00
ABCI requests/responses are simple Protobuf messages. Check out the [schema file](https://github.com/tendermint/abci/blob/master/types/types.proto).
2016-02-14 12:51:49 -08:00
2017-01-12 12:27:08 -08:00
#### DeliverTx
2015-11-02 07:39:53 -08:00
* __Arguments__:
2016-02-14 12:51:49 -08:00
* `Data ([]byte)`: The request transaction bytes
2015-11-02 07:39:53 -08:00
* __Returns__:
* `Code (uint32)`: Response code
2016-02-14 12:51:49 -08:00
* `Data ([]byte)`: Result bytes, if any
* `Log (string)`: Debug or error message
2015-11-02 07:39:53 -08:00
* __Usage__:<br/>
2016-02-14 12:51:49 -08:00
Append and run a transaction. If the transaction is valid, returns CodeType.OK
2015-11-02 07:39:53 -08:00
#### CheckTx
* __Arguments__:
2016-02-14 12:51:49 -08:00
* `Data ([]byte)`: The request transaction bytes
2015-11-02 07:39:53 -08:00
* __Returns__:
* `Code (uint32)`: Response code
2016-02-14 12:51:49 -08:00
* `Data ([]byte)`: Result bytes, if any
* `Log (string)`: Debug or error message
2015-11-02 07:39:53 -08:00
* __Usage__:<br/>
2016-10-28 12:06:40 -07:00
Validate a mempool transaction, prior to broadcasting or proposing. This message should not mutate the main state, but application
developers may want to keep a separate CheckTx state that gets reset upon Commit.
2017-02-20 04:49:52 -08:00
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.
2016-10-28 12:06:40 -07:00
2016-02-14 12:51:49 -08:00
Transactions are first run through CheckTx before broadcast to peers in the mempool layer.
2016-03-26 22:35:23 -07:00
You can make CheckTx semi-stateful and clear the state upon `Commit` or `BeginBlock`,
to allow for dependent sequences of transactions in the same block.
2015-11-02 07:39:53 -08:00
#### Commit
2015-11-02 07:39:53 -08:00
* __Returns__:
2016-02-14 12:51:49 -08:00
* `Data ([]byte)`: The Merkle root hash
* `Log (string)`: Debug or error message
2015-11-02 07:39:53 -08:00
* __Usage__:<br/>
2016-02-14 12:51:49 -08:00
Return a Merkle root hash of the application state.
#### Query
* __Arguments__:
* `Data ([]byte)`: Raw query bytes. Can be used with or in lieu of Path.
* `Path (string)`: Path of request, like an HTTP GET path. Can be used with or in liue of Data.
* Apps MUST interpret '/store' as a query by key on the underlying store. The key SHOULD be specified in the Data field.
* Apps SHOULD allow queries over specific types like '/accounts/...' or '/votes/...'
2017-02-14 13:53:21 -08:00
* `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
* `Prove (bool)`: Return Merkle proof with response if possible
2016-02-14 12:51:49 -08:00
* __Returns__:
* `Code (uint32)`: Response code
* `Key ([]byte)`: The key of the matching data
* `Value ([]byte)`: The value of the matching data
* `Proof ([]byte)`: Proof for the data, if requested
2017-02-14 13:53:21 -08:00
* `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
2016-02-14 12:51:49 -08:00
* `Log (string)`: Debug or error message
2017-02-06 16:20:33 -08:00
*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)
2015-11-02 07:39:53 -08:00
#### Info
* __Returns__:
2017-01-13 00:09:28 -08:00
* `Data (string)`: Some arbitrary information
* `Version (Version)`: Version information
* `LastBlockHeight (uint64)`: Latest block for which the app has called Commit
* `LastBlockAppHash ([]byte)`: Latest result of Commit
* __Usage__:<br/>
2017-01-13 00:09:28 -08:00
Return information about the application state. Used to sync the app with Tendermint on crash/restart.
2015-11-27 10:14:46 -08:00
#### SetOption
* __Arguments__:
2016-02-14 12:51:49 -08:00
* `Key (string)`: Key to set
* `Value (string)`: Value to set for key
2015-11-27 10:14:46 -08:00
* __Returns__:
2016-02-14 12:51:49 -08:00
* `Log (string)`: Debug or error message
2015-11-27 10:14:46 -08:00
* __Usage__:<br/>
Set application options. E.g. Key="mode", Value="mempool" for a mempool connection, or Key="mode", Value="consensus" for a consensus connection.
Other options are application specific.
#### InitChain
2016-02-28 18:53:24 -08:00
* __Arguments__:
* `Validators ([]Validator)`: Initial genesis validators
* __Usage__:<br/>
Called once upon genesis
2016-02-28 18:53:24 -08:00
2016-03-26 22:35:23 -07:00
#### BeginBlock
* __Arguments__:
2017-02-12 18:38:10 -08:00
* `Hash ([]byte)`: The block's hash. This can be derived from the block header.
2017-01-13 00:09:28 -08:00
* `Header (struct{})`: The block header
2016-03-26 22:35:23 -07:00
* __Usage__:<br/>
2017-01-13 00:09:28 -08:00
Signals the beginning of a new block. Called prior to any DeliverTxs. The header is expected to at least contain the Height.
2016-03-26 22:35:23 -07:00
#### EndBlock
2016-03-06 17:57:47 -08:00
* __Arguments__:
* `Height (uint64)`: The block height that ended
2016-02-28 18:53:24 -08:00
* __Returns__:
2017-01-13 00:09:28 -08:00
* `Diffs ([]Validator)`: Changed validators with new voting powers (0 to remove)
2016-02-28 18:53:24 -08:00
* __Usage__:<br/>
2017-01-13 00:09:28 -08:00
Signals the end of a block. Called prior to each Commit after all transactions. Validator set is updated with the result.
2017-02-06 15:10:20 -08:00
#### Echo
* __Arguments__:
* `Message (string)`: A string to echo back
* __Returns__:
* `Message (string)`: The input string
* __Usage__:<br/>
* Echo a string to test an abci client/server implementation
#### Flush
* __Usage__:<br/>
* 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.
2017-01-13 00:09:28 -08:00
# Implementations
The ABCI is a client/server interface where the replication engine (blockchain) forms the client
and the state machine (application) forms the server.
As blocks are committed in the blockchain, they are forwarded to the application.
2017-01-23 20:14:14 -08:00
This repository provides two implementations of an ABCI client & server: via socket and via GRPC.
2017-01-13 00:09:28 -08:00
2017-01-23 20:14:14 -08:00
## Socket
2017-01-13 00:09:28 -08:00
ABCI is best implemented as a streaming protocol.
2017-01-23 20:14:14 -08:00
The socket implementation provides for asynchronous, ordered message passing over unix or tcp.
2017-01-13 00:09:28 -08:00
Messages are serialized using Protobuf3 and length-prefixed.
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.
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...`.
2016-02-28 18:53:24 -08:00
2017-01-13 00:09:28 -08:00
## GRPC
2016-03-26 22:35:23 -07:00
2017-01-13 00:09:28 -08:00
GRPC is an rpc framework native to Protocol Buffers with support in many languages.
Implementing the ABCI using GRPC can allow for faster prototyping, but is expected to be much slower than
2017-01-23 20:14:14 -08:00
the ordered, asynchronous socket protocol.
2017-01-23 20:14:14 -08:00
Note the length-prefixing used in the socket implementation does not apply for GRPC.
2016-02-28 18:53:24 -08:00
2017-01-13 00:09:28 -08:00
# Tools and Apps
2016-02-28 18:53:24 -08:00
2017-01-13 00:09:28 -08:00
The `abci-cli` tool wraps any ABCI client and can be used for probing/testing an ABCI application.
2017-02-06 15:10:20 -08:00
See the [tutorial](https://tendermint.com/intro/getting-started/first-abci) for more details.
2016-02-14 12:51:49 -08:00
2017-01-13 00:09:28 -08:00
Multiple example apps are included:
- the `counter` application, which illustrates nonce checking in txs
- the `dummy` application, which illustrates a simple key-value merkle tree
- the `dummy --persistent` application, which augments the dummy with persistence and validator set changes
2016-02-14 12:51:49 -08:00
2016-01-23 20:49:15 -08:00
2017-01-13 00:09:28 -08:00
# Build
2016-01-23 20:49:15 -08:00
2017-01-13 00:09:28 -08:00
To build the protobuf code:
2017-01-13 00:09:28 -08:00
```
make protoc
```
2017-01-13 00:09:28 -08:00
See `protoc --help` and [the grpc docs](https://www.grpc.io/docs) for examples and details of other languages