From 00f7918d48670bf4e308c0c613409c810518df78 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Tue, 13 Apr 2021 15:35:51 -0700 Subject: [PATCH] Add ABCI domain types. These types mirror the generated types in tendermint_proto, but have better naming. The documentation for each request type is adapted from the ABCI Methods and Types spec document. However, the same logical request may appear three times, as a struct with the request data, as a Request variant, and as a CategoryRequest variant. To avoid duplication, this PR uses the `#[doc = include_str!(...)]` functionality stabilized in Rust 1.54 to keep common definitions of the documentation. --- tendermint/src/abci.rs | 54 ++++ .../abci/doc/request-applysnapshotchunk.md | 21 ++ tendermint/src/abci/doc/request-beginblock.md | 6 + tendermint/src/abci/doc/request-checktx.md | 11 + tendermint/src/abci/doc/request-commit.md | 4 + tendermint/src/abci/doc/request-delivertx.md | 3 + tendermint/src/abci/doc/request-echo.md | 3 + tendermint/src/abci/doc/request-endblock.md | 5 + tendermint/src/abci/doc/request-flush.md | 3 + tendermint/src/abci/doc/request-info.md | 3 + tendermint/src/abci/doc/request-initchain.md | 3 + .../src/abci/doc/request-listsnapshots.md | 3 + .../src/abci/doc/request-loadsnapshotchunk.md | 3 + .../src/abci/doc/request-offersnapshot.md | 20 ++ tendermint/src/abci/doc/request-query.md | 3 + .../abci/doc/response-applysnapshotchunk.md | 7 + .../src/abci/doc/response-beginblock.md | 3 + tendermint/src/abci/doc/response-checktx.md | 3 + tendermint/src/abci/doc/response-commit.md | 3 + tendermint/src/abci/doc/response-delivertx.md | 4 + tendermint/src/abci/doc/response-echo.md | 3 + tendermint/src/abci/doc/response-endblock.md | 3 + tendermint/src/abci/doc/response-exception.md | 1 + tendermint/src/abci/doc/response-flush.md | 3 + tendermint/src/abci/doc/response-info.md | 3 + tendermint/src/abci/doc/response-initchain.md | 3 + .../src/abci/doc/response-listsnapshots.md | 3 + .../abci/doc/response-loadsnapshotchunk.md | 3 + .../src/abci/doc/response-offersnapshot.md | 7 + tendermint/src/abci/doc/response-query.md | 3 + tendermint/src/abci/event.rs | 182 +++++++++++ tendermint/src/abci/kind.rs | 23 ++ tendermint/src/abci/params.rs | 217 +++++++++++++ tendermint/src/abci/request.rs | 304 ++++++++++++++++++ .../src/abci/request/apply_snapshot_chunk.rs | 71 ++++ tendermint/src/abci/request/begin_block.rs | 75 +++++ tendermint/src/abci/request/check_tx.rs | 72 +++++ tendermint/src/abci/request/deliver_tx.rs | 35 ++ tendermint/src/abci/request/echo.rs | 37 +++ tendermint/src/abci/request/end_block.rs | 37 +++ tendermint/src/abci/request/info.rs | 49 +++ tendermint/src/abci/request/init_chain.rs | 71 ++++ .../src/abci/request/load_snapshot_chunk.rs | 45 +++ tendermint/src/abci/request/offer_snapshot.rs | 53 +++ tendermint/src/abci/request/query.rs | 64 ++++ tendermint/src/abci/response.rs | 289 +++++++++++++++++ .../src/abci/response/apply_snapshot_chunk.rs | 89 +++++ tendermint/src/abci/response/begin_block.rs | 44 +++ tendermint/src/abci/response/check_tx.rs | 94 ++++++ tendermint/src/abci/response/commit.rs | 46 +++ tendermint/src/abci/response/deliver_tx.rs | 82 +++++ tendermint/src/abci/response/echo.rs | 37 +++ tendermint/src/abci/response/end_block.rs | 66 ++++ tendermint/src/abci/response/exception.rs | 37 +++ tendermint/src/abci/response/info.rs | 56 ++++ tendermint/src/abci/response/init_chain.rs | 63 ++++ .../src/abci/response/list_snapshots.rs | 47 +++ .../src/abci/response/load_snapshot_chunk.rs | 42 +++ .../src/abci/response/offer_snapshot.rs | 64 ++++ tendermint/src/abci/response/query.rs | 83 +++++ tendermint/src/abci/types.rs | 304 ++++++++++++++++++ tendermint/src/lib.rs | 1 + 62 files changed, 2976 insertions(+) create mode 100644 tendermint/src/abci.rs create mode 100644 tendermint/src/abci/doc/request-applysnapshotchunk.md create mode 100644 tendermint/src/abci/doc/request-beginblock.md create mode 100644 tendermint/src/abci/doc/request-checktx.md create mode 100644 tendermint/src/abci/doc/request-commit.md create mode 100644 tendermint/src/abci/doc/request-delivertx.md create mode 100644 tendermint/src/abci/doc/request-echo.md create mode 100644 tendermint/src/abci/doc/request-endblock.md create mode 100644 tendermint/src/abci/doc/request-flush.md create mode 100644 tendermint/src/abci/doc/request-info.md create mode 100644 tendermint/src/abci/doc/request-initchain.md create mode 100644 tendermint/src/abci/doc/request-listsnapshots.md create mode 100644 tendermint/src/abci/doc/request-loadsnapshotchunk.md create mode 100644 tendermint/src/abci/doc/request-offersnapshot.md create mode 100644 tendermint/src/abci/doc/request-query.md create mode 100644 tendermint/src/abci/doc/response-applysnapshotchunk.md create mode 100644 tendermint/src/abci/doc/response-beginblock.md create mode 100644 tendermint/src/abci/doc/response-checktx.md create mode 100644 tendermint/src/abci/doc/response-commit.md create mode 100644 tendermint/src/abci/doc/response-delivertx.md create mode 100644 tendermint/src/abci/doc/response-echo.md create mode 100644 tendermint/src/abci/doc/response-endblock.md create mode 100644 tendermint/src/abci/doc/response-exception.md create mode 100644 tendermint/src/abci/doc/response-flush.md create mode 100644 tendermint/src/abci/doc/response-info.md create mode 100644 tendermint/src/abci/doc/response-initchain.md create mode 100644 tendermint/src/abci/doc/response-listsnapshots.md create mode 100644 tendermint/src/abci/doc/response-loadsnapshotchunk.md create mode 100644 tendermint/src/abci/doc/response-offersnapshot.md create mode 100644 tendermint/src/abci/doc/response-query.md create mode 100644 tendermint/src/abci/event.rs create mode 100644 tendermint/src/abci/kind.rs create mode 100644 tendermint/src/abci/params.rs create mode 100644 tendermint/src/abci/request.rs create mode 100644 tendermint/src/abci/request/apply_snapshot_chunk.rs create mode 100644 tendermint/src/abci/request/begin_block.rs create mode 100644 tendermint/src/abci/request/check_tx.rs create mode 100644 tendermint/src/abci/request/deliver_tx.rs create mode 100644 tendermint/src/abci/request/echo.rs create mode 100644 tendermint/src/abci/request/end_block.rs create mode 100644 tendermint/src/abci/request/info.rs create mode 100644 tendermint/src/abci/request/init_chain.rs create mode 100644 tendermint/src/abci/request/load_snapshot_chunk.rs create mode 100644 tendermint/src/abci/request/offer_snapshot.rs create mode 100644 tendermint/src/abci/request/query.rs create mode 100644 tendermint/src/abci/response.rs create mode 100644 tendermint/src/abci/response/apply_snapshot_chunk.rs create mode 100644 tendermint/src/abci/response/begin_block.rs create mode 100644 tendermint/src/abci/response/check_tx.rs create mode 100644 tendermint/src/abci/response/commit.rs create mode 100644 tendermint/src/abci/response/deliver_tx.rs create mode 100644 tendermint/src/abci/response/echo.rs create mode 100644 tendermint/src/abci/response/end_block.rs create mode 100644 tendermint/src/abci/response/exception.rs create mode 100644 tendermint/src/abci/response/info.rs create mode 100644 tendermint/src/abci/response/init_chain.rs create mode 100644 tendermint/src/abci/response/list_snapshots.rs create mode 100644 tendermint/src/abci/response/load_snapshot_chunk.rs create mode 100644 tendermint/src/abci/response/offer_snapshot.rs create mode 100644 tendermint/src/abci/response/query.rs create mode 100644 tendermint/src/abci/types.rs diff --git a/tendermint/src/abci.rs b/tendermint/src/abci.rs new file mode 100644 index 000000000..799cd28f5 --- /dev/null +++ b/tendermint/src/abci.rs @@ -0,0 +1,54 @@ +//! Application BlockChain Interface ([ABCI]) is the interface between Tendermint +//! (a consensus engine for Byzantine-fault-tolerant replication of a state +//! machine) and an application (the state machine to be replicated). +//! +//! Using ABCI involves writing an application driven by ABCI methods, exposing +//! that application as an ABCI server, and having Tendermint connect to the +//! server as an ABCI client. +//! +//! This module does not include an ABCI server implementation itself. Instead, +//! it provides a common set of Rust domain types that model the ABCI protocol, +//! which can be used by both ABCI applications and ABCI server implementations. +//! +//! One ABCI server implementation is provided by the [`tendermint_abci`][tmabci] +//! crate. +//! +//! Each ABCI method corresponds to a request/response pair. ABCI requests are +//! modeled by the [`Request`] enum, and responses are modeled by the +//! [`Response`] enum. As described in the [methods and types][mat] page, ABCI +//! methods are split into four categories. Tendermint opens one ABCI connection +//! for each category of messages. These categories are modeled by the +//! [`MethodKind`] enum and by per-category request and response enums: +//! +//! * [`ConsensusRequest`] / [`ConsensusResponse`] for [`MethodKind::Consensus`] methods; +//! * [`MempoolRequest`] / [`MempoolResponse`] for [`MethodKind::Mempool`] methods; +//! * [`InfoRequest`] / [`InfoResponse`] for [`MethodKind::Info`] methods; +//! * [`SnapshotRequest`] / [`SnapshotResponse`] for [`MethodKind::Snapshot`] methods. +//! +//! The domain types in this module have conversions to and from the Protobuf +//! types defined in the [`tendermint_proto`] crate. These conversions are +//! required for ABCI server implementations, which use the protobufs to +//! communicate with Tendermint, but should not be required for ABCI +//! applications, which should use the domain types in an interface defined by +//! their choice of ABCI server implementation. +//! +//! [ABCI]: https://docs.tendermint.com/master/spec/abci/ +//! [mat]: https://docs.tendermint.com/master/spec/abci/abci.html +//! [tmabci]: https://github.com/informalsystems/tendermint-rs/tree/master/abci + +mod kind; +mod event; + +pub mod params; +pub mod request; +pub mod response; +pub mod types; + +pub use event::{Event, EventAttribute, EventAttributeIndexExt}; + +#[doc(inline)] +pub use self::{ + kind::MethodKind, + request::{ConsensusRequest, InfoRequest, MempoolRequest, Request, SnapshotRequest}, + response::{ConsensusResponse, InfoResponse, MempoolResponse, Response, SnapshotResponse}, +}; diff --git a/tendermint/src/abci/doc/request-applysnapshotchunk.md b/tendermint/src/abci/doc/request-applysnapshotchunk.md new file mode 100644 index 000000000..fe1c35598 --- /dev/null +++ b/tendermint/src/abci/doc/request-applysnapshotchunk.md @@ -0,0 +1,21 @@ +Applies a snapshot chunk. + +The application can choose to refetch chunks and/or ban P2P peers as +appropriate. Tendermint will not do this unless instructed by the +application. + +The application may want to verify each chunk, e.g., by attaching chunk +hashes in [`Snapshot::metadata`] and/or incrementally verifying contents +against `app_hash`. + +When all chunks have been accepted, Tendermint will make an ABCI [`Info`] +request to verify that `last_block_app_hash` and `last_block_height` match +the expected values, and record the `app_version` in the node state. It then +switches to fast sync or consensus and joins the network. + +If Tendermint is unable to retrieve the next chunk after some time (e.g., +because no suitable peers are available), it will reject the snapshot and try +a different one via `OfferSnapshot`. The application should be prepared to +reset and accept it or abort as appropriate. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#applysnapshotchunk) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-beginblock.md b/tendermint/src/abci/doc/request-beginblock.md new file mode 100644 index 000000000..44b98920a --- /dev/null +++ b/tendermint/src/abci/doc/request-beginblock.md @@ -0,0 +1,6 @@ +Signals the beginning of a new block. + +Called prior to any [`DeliverTx`]s. The `header` contains the height, +timestamp, and more -- it exactly matches the Tendermint block header. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#beginblock) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-checktx.md b/tendermint/src/abci/doc/request-checktx.md new file mode 100644 index 000000000..7d97a6287 --- /dev/null +++ b/tendermint/src/abci/doc/request-checktx.md @@ -0,0 +1,11 @@ +Check whether a transaction should be included in the mempool. + +`CheckTx` is not involved in processing blocks, only in deciding whether a +transaction should be included in the mempool. Every node runs `CheckTx` +before adding a transaction to its local mempool. The transaction may come +from an external user or another node. `CheckTx` need not execute the +transaction in full, but can instead perform lightweight or statateful +validation (e.g., checking signatures or account balances) instead of more +expensive checks (like running code in a virtual machine). + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#checktx) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-commit.md b/tendermint/src/abci/doc/request-commit.md new file mode 100644 index 000000000..0013b8302 --- /dev/null +++ b/tendermint/src/abci/doc/request-commit.md @@ -0,0 +1,4 @@ +Signals the application that it can write the queued state transitions +from the block to its state. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#commit) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-delivertx.md b/tendermint/src/abci/doc/request-delivertx.md new file mode 100644 index 000000000..4d449cc56 --- /dev/null +++ b/tendermint/src/abci/doc/request-delivertx.md @@ -0,0 +1,3 @@ +Execute a transaction against the application state. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#delivertx) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-echo.md b/tendermint/src/abci/doc/request-echo.md new file mode 100644 index 000000000..92658169f --- /dev/null +++ b/tendermint/src/abci/doc/request-echo.md @@ -0,0 +1,3 @@ +Echoes a string to test an ABCI implementation. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#echo) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-endblock.md b/tendermint/src/abci/doc/request-endblock.md new file mode 100644 index 000000000..6e23b6d7c --- /dev/null +++ b/tendermint/src/abci/doc/request-endblock.md @@ -0,0 +1,5 @@ +Signals the end of a block. + +Called after all transactions, and prior to each `Commit`. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#endblock) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-flush.md b/tendermint/src/abci/doc/request-flush.md new file mode 100644 index 000000000..c556d8b2d --- /dev/null +++ b/tendermint/src/abci/doc/request-flush.md @@ -0,0 +1,3 @@ +Indicates that any pending requests should be completed and their responses flushed. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#flush) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-info.md b/tendermint/src/abci/doc/request-info.md new file mode 100644 index 000000000..471ec36c7 --- /dev/null +++ b/tendermint/src/abci/doc/request-info.md @@ -0,0 +1,3 @@ +Requests information about the application state. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#info) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-initchain.md b/tendermint/src/abci/doc/request-initchain.md new file mode 100644 index 000000000..49180f351 --- /dev/null +++ b/tendermint/src/abci/doc/request-initchain.md @@ -0,0 +1,3 @@ +Called on genesis to initialize chain state. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#initchain) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-listsnapshots.md b/tendermint/src/abci/doc/request-listsnapshots.md new file mode 100644 index 000000000..bc89accad --- /dev/null +++ b/tendermint/src/abci/doc/request-listsnapshots.md @@ -0,0 +1,3 @@ +Asks the application for a list of snapshots. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#listsnapshots) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-loadsnapshotchunk.md b/tendermint/src/abci/doc/request-loadsnapshotchunk.md new file mode 100644 index 000000000..70b686d41 --- /dev/null +++ b/tendermint/src/abci/doc/request-loadsnapshotchunk.md @@ -0,0 +1,3 @@ +Used during state sync to retrieve snapshot chunks from peers. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#loadsnapshotchunk) \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-offersnapshot.md b/tendermint/src/abci/doc/request-offersnapshot.md new file mode 100644 index 000000000..db0e60b17 --- /dev/null +++ b/tendermint/src/abci/doc/request-offersnapshot.md @@ -0,0 +1,20 @@ +Offers a list of snapshots to the application. + +`OfferSnapshot` is called when bootstrapping a node using state sync. The +application may accept or reject snapshots as appropriate. Upon accepting, +Tendermint will retrieve and apply snapshot chunks via +[`ApplySnapshotChunk`]. The application may also choose to reject a snapshot +in the chunk response, in which case it should be prepared to accept further +`OfferSnapshot` calls. + +Only `app_hash` can be trusted, as it has been verified by the light client. +Any other data can be spoofed by adversaries, so applications should employ +additional verification schemes to avoid denial-of-service attacks. The +verified `app_hash` is automatically checked against the restored application +at the end of snapshot restoration. + +See also the [`Snapshot`] data type and the [ABCI state sync documentation][ssd]. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#offersnapshot) + +[ssd]: https://docs.tendermint.com/master/spec/abci/apps.html#state-sync \ No newline at end of file diff --git a/tendermint/src/abci/doc/request-query.md b/tendermint/src/abci/doc/request-query.md new file mode 100644 index 000000000..5d061c54e --- /dev/null +++ b/tendermint/src/abci/doc/request-query.md @@ -0,0 +1,3 @@ +Queries for data from the application at current or past height. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#query) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-applysnapshotchunk.md b/tendermint/src/abci/doc/response-applysnapshotchunk.md new file mode 100644 index 000000000..bffabe7af --- /dev/null +++ b/tendermint/src/abci/doc/response-applysnapshotchunk.md @@ -0,0 +1,7 @@ +Returns the result of applying a snapshot chunk and associated data. + +The application can choose to refetch chunks and/or ban P2P peers as +appropriate. Tendermint will not do this unless instructed by the +application. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#applysnapshotchunk) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-beginblock.md b/tendermint/src/abci/doc/response-beginblock.md new file mode 100644 index 000000000..255efd098 --- /dev/null +++ b/tendermint/src/abci/doc/response-beginblock.md @@ -0,0 +1,3 @@ +Returns events that occurred when beginning a new block. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#beginblock) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-checktx.md b/tendermint/src/abci/doc/response-checktx.md new file mode 100644 index 000000000..cd31b1703 --- /dev/null +++ b/tendermint/src/abci/doc/response-checktx.md @@ -0,0 +1,3 @@ +Returns the result of checking a transaction for mempool inclusion. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#checktx) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-commit.md b/tendermint/src/abci/doc/response-commit.md new file mode 100644 index 000000000..822aab48d --- /dev/null +++ b/tendermint/src/abci/doc/response-commit.md @@ -0,0 +1,3 @@ +Returns the result of persisting the application state. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#commit) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-delivertx.md b/tendermint/src/abci/doc/response-delivertx.md new file mode 100644 index 000000000..cb83a6fd9 --- /dev/null +++ b/tendermint/src/abci/doc/response-delivertx.md @@ -0,0 +1,4 @@ +Returns events that occurred while executing a transaction against the +application state. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#delivertx) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-echo.md b/tendermint/src/abci/doc/response-echo.md new file mode 100644 index 000000000..92658169f --- /dev/null +++ b/tendermint/src/abci/doc/response-echo.md @@ -0,0 +1,3 @@ +Echoes a string to test an ABCI implementation. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#echo) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-endblock.md b/tendermint/src/abci/doc/response-endblock.md new file mode 100644 index 000000000..062cabb84 --- /dev/null +++ b/tendermint/src/abci/doc/response-endblock.md @@ -0,0 +1,3 @@ +Returns validator updates that occur after the end of a block. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#endblock) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-exception.md b/tendermint/src/abci/doc/response-exception.md new file mode 100644 index 000000000..5d8fb6c67 --- /dev/null +++ b/tendermint/src/abci/doc/response-exception.md @@ -0,0 +1 @@ +Returns an exception (undocumented, nondeterministic). \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-flush.md b/tendermint/src/abci/doc/response-flush.md new file mode 100644 index 000000000..6c411e1bf --- /dev/null +++ b/tendermint/src/abci/doc/response-flush.md @@ -0,0 +1,3 @@ +Indicates that all pending requests have been completed with their responses flushed. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#flush) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-info.md b/tendermint/src/abci/doc/response-info.md new file mode 100644 index 000000000..e0c64b1f5 --- /dev/null +++ b/tendermint/src/abci/doc/response-info.md @@ -0,0 +1,3 @@ +Returns information about the application state. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#info) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-initchain.md b/tendermint/src/abci/doc/response-initchain.md new file mode 100644 index 000000000..b7ea62de7 --- /dev/null +++ b/tendermint/src/abci/doc/response-initchain.md @@ -0,0 +1,3 @@ +Returned on genesis after initializing chain state. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#initchain) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-listsnapshots.md b/tendermint/src/abci/doc/response-listsnapshots.md new file mode 100644 index 000000000..48255b800 --- /dev/null +++ b/tendermint/src/abci/doc/response-listsnapshots.md @@ -0,0 +1,3 @@ +Returns a list of local state snapshots. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#listsnapshots) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-loadsnapshotchunk.md b/tendermint/src/abci/doc/response-loadsnapshotchunk.md new file mode 100644 index 000000000..2eaf1c614 --- /dev/null +++ b/tendermint/src/abci/doc/response-loadsnapshotchunk.md @@ -0,0 +1,3 @@ +Returns a snapshot chunk from the application. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#loadsnapshotchunk) \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-offersnapshot.md b/tendermint/src/abci/doc/response-offersnapshot.md new file mode 100644 index 000000000..0da7a66fa --- /dev/null +++ b/tendermint/src/abci/doc/response-offersnapshot.md @@ -0,0 +1,7 @@ +Returns the application's response to a snapshot offer. + +See also the [`Snapshot`] data type and the [ABCI state sync documentation][ssd]. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#offersnapshot) + +[ssd]: https://docs.tendermint.com/master/spec/abci/apps.html#state-sync \ No newline at end of file diff --git a/tendermint/src/abci/doc/response-query.md b/tendermint/src/abci/doc/response-query.md new file mode 100644 index 000000000..57eb3bf4a --- /dev/null +++ b/tendermint/src/abci/doc/response-query.md @@ -0,0 +1,3 @@ +Returns data queried from the application. + +[ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#query) \ No newline at end of file diff --git a/tendermint/src/abci/event.rs b/tendermint/src/abci/event.rs new file mode 100644 index 000000000..0bf904c76 --- /dev/null +++ b/tendermint/src/abci/event.rs @@ -0,0 +1,182 @@ +/// An event that occurred while processing a request. +/// +/// Application developers can attach additional information to +/// [`BeginBlock`](super::response::BeginBlock), +/// [`EndBlock`](super::response::EndBlock), +/// [`CheckTx`](super::response::CheckTx), and +/// [`DeliverTx`](super::response::DeliverTx) responses. Later, transactions may +/// be queried using these events. +/// +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#events) +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct Event { + /// The kind of event. + /// + /// Tendermint calls this the `type`, but we use `kind` to avoid confusion + /// with Rust types and follow Rust conventions. + pub kind: String, + /// A list of [`EventAttribute`]s describing the event. + pub attributes: Vec, +} + +impl Event { + /// Construct an event from generic data. + /// + /// The `From` impls on [`EventAttribute`] and the [`EventAttributeIndexExt`] + /// trait allow ergonomic event construction, as in this example: + /// + /// ``` + /// use tendermint::abci::{Event, EventAttributeIndexExt}; + /// + /// let event = Event::new( + /// "app", + /// vec![ + /// ("key1", "value1").index(), + /// ("key2", "value2").index(), + /// ("key3", "value3").no_index(), // will not be indexed + /// ], + /// ); + /// ``` + // XXX(hdevalence): remove vec! from example after https://github.com/rust-lang/rust/pull/65819 + pub fn new(kind: K, attributes: I) -> Self + where + K: Into, + I: IntoIterator, + I::Item: Into, + { + Self { + kind: kind.into(), + attributes: attributes.into_iter().map(Into::into).collect(), + } + } +} + +/// A key-value pair describing an [`Event`]. +/// +/// Generic methods are provided for more ergonomic attribute construction, see +/// [`Event::new`] for details. +/// +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#events) +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct EventAttribute { + /// The event key. + pub key: String, + /// The event value. + pub value: String, + /// Whether Tendermint's indexer should index this event. + /// + /// **This field is nondeterministic**. + pub index: bool, +} + +impl, V: Into> From<(K, V, bool)> for EventAttribute { + fn from((key, value, index): (K, V, bool)) -> Self { + EventAttribute { + key: key.into(), + value: value.into(), + index, + } + } +} + +/// Adds convenience methods to tuples for more ergonomic [`EventAttribute`] +/// construction. +/// +/// See [`Event::new`] for details. +#[allow(missing_docs)] +pub trait EventAttributeIndexExt: private::Sealed { + type Key; + type Value; + + /// Indicate that this key/value pair should be indexed by Tendermint. + fn index(self) -> (Self::Key, Self::Value, bool); + /// Indicate that this key/value pair should not be indexed by Tendermint. + fn no_index(self) -> (Self::Key, Self::Value, bool); +} + +impl, V: Into> EventAttributeIndexExt for (K, V) { + type Key = K; + type Value = V; + fn index(self) -> (K, V, bool) { + let (key, value) = self; + (key, value, true) + } + fn no_index(self) -> (K, V, bool) { + let (key, value) = self; + (key, value, false) + } +} + +mod private { + pub trait Sealed {} + + impl, V: Into> Sealed for (K, V) {} +} + +impl, V: Into> From<(K, V)> for EventAttribute { + fn from((key, value): (K, V)) -> Self { + (key, value, false).into() + } +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::{TryFrom, TryInto}; + +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::EventAttribute { + fn from(event: EventAttribute) -> Self { + Self { + key: event.key, + value: event.value, + index: event.index, + } + } +} + +impl TryFrom for EventAttribute { + type Error = crate::Error; + + fn try_from(event: pb::EventAttribute) -> Result { + Ok(Self { + key: event.key, + value: event.value, + index: event.index, + }) + } +} + +impl Protobuf for EventAttribute {} + +impl From for pb::Event { + fn from(event: Event) -> Self { + Self { + r#type: event.kind, + attributes: event.attributes.into_iter().map(Into::into).collect(), + } + } +} + +impl TryFrom for Event { + type Error = crate::Error; + + fn try_from(event: pb::Event) -> Result { + Ok(Self { + kind: event.r#type, + attributes: event + .attributes + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + }) + } +} + +impl Protobuf for Event {} diff --git a/tendermint/src/abci/kind.rs b/tendermint/src/abci/kind.rs new file mode 100644 index 000000000..065a01c9a --- /dev/null +++ b/tendermint/src/abci/kind.rs @@ -0,0 +1,23 @@ +/// A category of ABCI method. +/// +/// ABCI methods are split into four categories. Tendermint opens one ABCI +/// connection for each category and refers to these categories as *connections*, +/// but nothing actually restricts an ABCI connection from calling methods in +/// multiple categories. +/// +/// This enum breaks out the `Flush` method as a distinct category, since it is +/// used to control the execution of other methods. +pub enum MethodKind { + /// A consensus method, driven by the consensus protocol and responsible for + /// block execution. + Consensus, + /// A mempool method, used for validating new transactions before they're + /// shared or included in a block. + Mempool, + /// A snapshot method, used for serving and restoring state snapshots. + Snapshot, + /// An info method, used for initialization and user queries. + Info, + /// The flush method requests that all pending method requests are fully executed. + Flush, +} diff --git a/tendermint/src/abci/params.rs b/tendermint/src/abci/params.rs new file mode 100644 index 000000000..f1ac11d45 --- /dev/null +++ b/tendermint/src/abci/params.rs @@ -0,0 +1,217 @@ +//! Consensus-relevant parameters that can be adjusted by the ABCI app. + +use chrono::Duration; +use std::convert::{TryFrom, TryInto}; + +/// All consensus-relevant parameters that can be adjusted by the ABCI app. +/// +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#consensusparams) +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct ConsensusParams { + /// Parameters limiting the size of a block and time between consecutive blocks. + pub block: BlockParams, + /// Parameters limiting the validity of evidence of byzantine behaviour. + pub evidence: EvidenceParams, + /// Parameters limiting the types of public keys validators can use. + pub validator: ValidatorParams, + /// The ABCI application version. + pub version: VersionParams, +} +/// BlockParams contains limits on the block size. +/// +/// [Tendermint documentation](https://docs.tendermint.com/master/spec/core/data_structures.html#blockparams) +/// +/// XXX(hdevalence): this isn't ABCI-specific, should it live here? +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct BlockParams { + /// Max block size, in bytes. + pub max_bytes: i64, + /// Max sum of `gas_wanted` in a proposed block. + /// + /// Note: blocks that violate this may be committed if there are Byzantine + /// proposers. It's the application's responsibility to handle this when + /// processing a block. + pub max_gas: i64, +} + +/// EvidenceParams determine how we handle evidence of malfeasance. +/// +/// [Tendermint documentation](https://docs.tendermint.com/master/spec/core/data_structures.html#evidenceparams) +/// +/// XXX(hdevalence): this isn't ABCI-specific, should it live here? +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct EvidenceParams { + /// Max age of evidence, in blocks. + pub max_age_num_blocks: i64, + /// Max age of evidence, in time. + /// + /// It should correspond with an app's "unbonding period" or other similar + /// mechanism for handling [Nothing-At-Stake attacks][nas]. + /// + /// [nas]: https://github.com/ethereum/wiki/wiki/Proof-of-Stake-FAQ#what-is-the-nothing-at-stake-problem-and-how-can-it-be-fixed + pub max_age_duration: Duration, + /// This sets the maximum size of total evidence in bytes that can be + /// committed in a single block, and should fall comfortably under the max + /// block bytes. The default is 1048576 or 1MB. + pub max_bytes: i64, +} + +/// ValidatorParams restrict the public key types validators can use. +/// +/// [Tendermint documentation](https://docs.tendermint.com/master/spec/core/data_structures.html#validatorparams) +/// +/// XXX(hdevalence): this isn't ABCI-specific, should it live here? +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct ValidatorParams { + /// List of accepted public key types. + pub pub_key_types: Vec, +} + +/// (No description) +/// +/// [Tendermint documentation](https://docs.tendermint.com/master/spec/core/data_structures.html#versionparams) +/// +/// XXX(hdevalence): this isn't ABCI-specific, should it live here? +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct VersionParams { + /// The ABCI application version. + pub app_version: u64, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use tendermint_proto::types as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ConsensusParams { + fn from(params: ConsensusParams) -> Self { + Self { + block: Some(params.block.into()), + evidence: Some(params.evidence.into()), + validator: Some(params.validator.into()), + version: Some(params.version.into()), + } + } +} + +impl TryFrom for ConsensusParams { + type Error = crate::Error; + + fn try_from(params: pb::ConsensusParams) -> Result { + Ok(Self { + block: params.block.ok_or("missing block params")?.try_into()?, + evidence: params + .evidence + .ok_or("missing evidence params")? + .try_into()?, + validator: params + .validator + .ok_or("missing validator params")? + .try_into()?, + version: params.version.ok_or("missing version params")?.try_into()?, + }) + } +} + +impl Protobuf for ConsensusParams {} + +impl From for pb::BlockParams { + fn from(params: BlockParams) -> Self { + Self { + max_bytes: params.max_bytes, + max_gas: params.max_gas, + } + } +} + +impl TryFrom for BlockParams { + type Error = crate::Error; + + fn try_from(params: pb::BlockParams) -> Result { + if params.max_bytes == 0 { + Err("BlockParams::max_bytes must be greater than 0")? + } + if params.max_gas < -1 { + Err("BlockParams::max_gas must be greater than or equal to -1")? + } + + Ok(Self { + max_bytes: params.max_bytes, + max_gas: params.max_gas, + }) + } +} + +impl Protobuf for BlockParams {} + +impl From for pb::EvidenceParams { + fn from(params: EvidenceParams) -> Self { + Self { + max_age_num_blocks: params.max_age_num_blocks, + max_age_duration: Some(params.max_age_duration.into()), + max_bytes: params.max_bytes, + } + } +} + +impl TryFrom for EvidenceParams { + type Error = crate::Error; + + fn try_from(params: pb::EvidenceParams) -> Result { + Ok(Self { + max_age_num_blocks: params.max_age_num_blocks, + max_age_duration: params + .max_age_duration + .ok_or("missing max age duration")? + .into(), + max_bytes: params.max_bytes, + }) + } +} + +impl Protobuf for EvidenceParams {} + +impl From for pb::ValidatorParams { + fn from(params: ValidatorParams) -> Self { + Self { + pub_key_types: params.pub_key_types, + } + } +} + +impl TryFrom for ValidatorParams { + type Error = crate::Error; + + fn try_from(params: pb::ValidatorParams) -> Result { + Ok(Self { + pub_key_types: params.pub_key_types, + }) + } +} + +impl Protobuf for ValidatorParams {} + +impl From for pb::VersionParams { + fn from(params: VersionParams) -> Self { + Self { + app_version: params.app_version, + } + } +} + +impl TryFrom for VersionParams { + type Error = crate::Error; + + fn try_from(params: pb::VersionParams) -> Result { + Ok(Self { + app_version: params.app_version, + }) + } +} + +impl Protobuf for VersionParams {} diff --git a/tendermint/src/abci/request.rs b/tendermint/src/abci/request.rs new file mode 100644 index 000000000..243583478 --- /dev/null +++ b/tendermint/src/abci/request.rs @@ -0,0 +1,304 @@ +//! ABCI requests and request data. +//! +//! The [`Request`] enum records all possible ABCI requests. Requests that +//! contain data are modeled as a separate struct, to avoid duplication of field +//! definitions. + +// IMPORTANT NOTE ON DOCUMENTATION: +// +// The documentation for each request type is adapted from the ABCI Methods and +// Types spec document. However, the same logical request may appear three +// times, as a struct with the request data, as a Request variant, and as a +// CategoryRequest variant. +// +// To avoid duplication, this documentation is stored in the doc/ folder in +// individual .md files, which are pasted onto the relevant items using #[doc = +// include_str!(...)]. +// +// This is also why certain submodules have #[allow(unused)] imports to bring +// items into scope for doc links, rather than changing the doc links -- it +// allows the doc comments to be copied without editing. + +use std::convert::{TryFrom, TryInto}; + +use super::MethodKind; + +// bring into scope for doc links +#[allow(unused)] +use super::types::Snapshot; + +mod apply_snapshot_chunk; +mod begin_block; +mod check_tx; +mod deliver_tx; +mod echo; +mod end_block; +mod info; +mod init_chain; +mod load_snapshot_chunk; +mod offer_snapshot; +mod query; + +pub use apply_snapshot_chunk::ApplySnapshotChunk; +pub use begin_block::BeginBlock; +pub use check_tx::{CheckTx, CheckTxKind}; +pub use deliver_tx::DeliverTx; +pub use echo::Echo; +pub use end_block::EndBlock; +pub use info::Info; +pub use init_chain::InitChain; +pub use load_snapshot_chunk::LoadSnapshotChunk; +pub use offer_snapshot::OfferSnapshot; +pub use query::Query; + +/// All possible ABCI requests. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum Request { + #[doc = include_str!("doc/request-echo.md")] + Echo(Echo), + #[doc = include_str!("doc/request-flush.md")] + Flush, + #[doc = include_str!("doc/request-info.md")] + Info(Info), + #[doc = include_str!("doc/request-initchain.md")] + InitChain(InitChain), + #[doc = include_str!("doc/request-query.md")] + Query(Query), + #[doc = include_str!("doc/request-beginblock.md")] + BeginBlock(BeginBlock), + #[doc = include_str!("doc/request-checktx.md")] + CheckTx(CheckTx), + #[doc = include_str!("doc/request-delivertx.md")] + DeliverTx(DeliverTx), + #[doc = include_str!("doc/request-endblock.md")] + EndBlock(EndBlock), + #[doc = include_str!("doc/request-commit.md")] + Commit, + #[doc = include_str!("doc/request-listsnapshots.md")] + ListSnapshots, + #[doc = include_str!("doc/request-offersnapshot.md")] + OfferSnapshot(OfferSnapshot), + #[doc = include_str!("doc/request-loadsnapshotchunk.md")] + LoadSnapshotChunk(LoadSnapshotChunk), + #[doc = include_str!("doc/request-applysnapshotchunk.md")] + ApplySnapshotChunk(ApplySnapshotChunk), +} + +impl Request { + /// Get the method kind for this request. + pub fn kind(&self) -> MethodKind { + use Request::*; + match self { + Flush => MethodKind::Flush, + InitChain(_) => MethodKind::Consensus, + BeginBlock(_) => MethodKind::Consensus, + DeliverTx(_) => MethodKind::Consensus, + EndBlock(_) => MethodKind::Consensus, + Commit => MethodKind::Consensus, + CheckTx(_) => MethodKind::Mempool, + ListSnapshots => MethodKind::Snapshot, + OfferSnapshot(_) => MethodKind::Snapshot, + LoadSnapshotChunk(_) => MethodKind::Snapshot, + ApplySnapshotChunk(_) => MethodKind::Snapshot, + Info(_) => MethodKind::Info, + Query(_) => MethodKind::Info, + Echo(_) => MethodKind::Info, + } + } +} + +/// The consensus category of ABCI requests. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum ConsensusRequest { + #[doc = include_str!("doc/request-initchain.md")] + InitChain(InitChain), + #[doc = include_str!("doc/request-beginblock.md")] + BeginBlock(BeginBlock), + #[doc = include_str!("doc/request-delivertx.md")] + DeliverTx(DeliverTx), + #[doc = include_str!("doc/request-endblock.md")] + EndBlock(EndBlock), + #[doc = include_str!("doc/request-commit.md")] + Commit, +} + +impl From for Request { + fn from(req: ConsensusRequest) -> Self { + match req { + ConsensusRequest::InitChain(x) => Self::InitChain(x), + ConsensusRequest::BeginBlock(x) => Self::BeginBlock(x), + ConsensusRequest::DeliverTx(x) => Self::DeliverTx(x), + ConsensusRequest::EndBlock(x) => Self::EndBlock(x), + ConsensusRequest::Commit => Self::Commit, + } + } +} + +impl TryFrom for ConsensusRequest { + type Error = &'static str; + fn try_from(req: Request) -> Result { + match req { + Request::InitChain(x) => Ok(Self::InitChain(x)), + Request::BeginBlock(x) => Ok(Self::BeginBlock(x)), + Request::DeliverTx(x) => Ok(Self::DeliverTx(x)), + Request::EndBlock(x) => Ok(Self::EndBlock(x)), + Request::Commit => Ok(Self::Commit), + _ => Err("wrong request type"), + } + } +} + +/// The mempool category of ABCI requests. +#[derive(Clone, PartialEq, Debug)] +pub enum MempoolRequest { + #[doc = include_str!("doc/request-checktx.md")] + CheckTx(CheckTx), +} + +impl From for Request { + fn from(req: MempoolRequest) -> Self { + match req { + MempoolRequest::CheckTx(x) => Self::CheckTx(x), + } + } +} + +impl TryFrom for MempoolRequest { + type Error = &'static str; + fn try_from(req: Request) -> Result { + match req { + Request::CheckTx(x) => Ok(Self::CheckTx(x)), + _ => Err("wrong request type"), + } + } +} + +/// The info category of ABCI requests. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum InfoRequest { + #[doc = include_str!("doc/request-info.md")] + Info(Info), + #[doc = include_str!("doc/request-query.md")] + Query(Query), + #[doc = include_str!("doc/request-echo.md")] + Echo(Echo), +} + +impl From for Request { + fn from(req: InfoRequest) -> Self { + match req { + InfoRequest::Info(x) => Self::Info(x), + InfoRequest::Query(x) => Self::Query(x), + InfoRequest::Echo(x) => Self::Echo(x), + } + } +} + +impl TryFrom for InfoRequest { + type Error = &'static str; + fn try_from(req: Request) -> Result { + match req { + Request::Info(x) => Ok(Self::Info(x)), + Request::Query(x) => Ok(Self::Query(x)), + Request::Echo(x) => Ok(Self::Echo(x)), + _ => Err("wrong request type"), + } + } +} + +/// The snapshot category of ABCI requests. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum SnapshotRequest { + #[doc = include_str!("doc/request-listsnapshots.md")] + ListSnapshots, + #[doc = include_str!("doc/request-offersnapshot.md")] + OfferSnapshot(OfferSnapshot), + #[doc = include_str!("doc/request-loadsnapshotchunk.md")] + LoadSnapshotChunk(LoadSnapshotChunk), + #[doc = include_str!("doc/request-applysnapshotchunk.md")] + ApplySnapshotChunk(ApplySnapshotChunk), +} + +impl From for Request { + fn from(req: SnapshotRequest) -> Self { + match req { + SnapshotRequest::ListSnapshots => Self::ListSnapshots, + SnapshotRequest::OfferSnapshot(x) => Self::OfferSnapshot(x), + SnapshotRequest::LoadSnapshotChunk(x) => Self::LoadSnapshotChunk(x), + SnapshotRequest::ApplySnapshotChunk(x) => Self::ApplySnapshotChunk(x), + } + } +} + +impl TryFrom for SnapshotRequest { + type Error = &'static str; + fn try_from(req: Request) -> Result { + match req { + Request::ListSnapshots => Ok(Self::ListSnapshots), + Request::OfferSnapshot(x) => Ok(Self::OfferSnapshot(x)), + Request::LoadSnapshotChunk(x) => Ok(Self::LoadSnapshotChunk(x)), + Request::ApplySnapshotChunk(x) => Ok(Self::ApplySnapshotChunk(x)), + _ => Err("wrong request type"), + } + } +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::Request { + fn from(request: Request) -> pb::Request { + use pb::request::Value; + let value = match request { + Request::Echo(x) => Some(Value::Echo(x.into())), + Request::Flush => Some(Value::Flush(Default::default())), + Request::Info(x) => Some(Value::Info(x.into())), + Request::InitChain(x) => Some(Value::InitChain(x.into())), + Request::Query(x) => Some(Value::Query(x.into())), + Request::BeginBlock(x) => Some(Value::BeginBlock(x.into())), + Request::CheckTx(x) => Some(Value::CheckTx(x.into())), + Request::DeliverTx(x) => Some(Value::DeliverTx(x.into())), + Request::EndBlock(x) => Some(Value::EndBlock(x.into())), + Request::Commit => Some(Value::Commit(Default::default())), + Request::ListSnapshots => Some(Value::ListSnapshots(Default::default())), + Request::OfferSnapshot(x) => Some(Value::OfferSnapshot(x.into())), + Request::LoadSnapshotChunk(x) => Some(Value::LoadSnapshotChunk(x.into())), + Request::ApplySnapshotChunk(x) => Some(Value::ApplySnapshotChunk(x.into())), + }; + pb::Request { value } + } +} + +impl TryFrom for Request { + type Error = crate::Error; + + fn try_from(request: pb::Request) -> Result { + use pb::request::Value; + match request.value { + Some(Value::Echo(x)) => Ok(Request::Echo(x.try_into()?)), + Some(Value::Flush(pb::RequestFlush {})) => Ok(Request::Flush), + Some(Value::Info(x)) => Ok(Request::Info(x.try_into()?)), + Some(Value::InitChain(x)) => Ok(Request::InitChain(x.try_into()?)), + Some(Value::Query(x)) => Ok(Request::Query(x.try_into()?)), + Some(Value::BeginBlock(x)) => Ok(Request::BeginBlock(x.try_into()?)), + Some(Value::CheckTx(x)) => Ok(Request::CheckTx(x.try_into()?)), + Some(Value::DeliverTx(x)) => Ok(Request::DeliverTx(x.try_into()?)), + Some(Value::EndBlock(x)) => Ok(Request::EndBlock(x.try_into()?)), + Some(Value::Commit(pb::RequestCommit {})) => Ok(Request::Commit), + Some(Value::ListSnapshots(pb::RequestListSnapshots {})) => Ok(Request::ListSnapshots), + Some(Value::OfferSnapshot(x)) => Ok(Request::OfferSnapshot(x.try_into()?)), + Some(Value::LoadSnapshotChunk(x)) => Ok(Request::LoadSnapshotChunk(x.try_into()?)), + Some(Value::ApplySnapshotChunk(x)) => Ok(Request::ApplySnapshotChunk(x.try_into()?)), + None => Err("no request in proto".into()), + } + } +} + +impl Protobuf for Request {} diff --git a/tendermint/src/abci/request/apply_snapshot_chunk.rs b/tendermint/src/abci/request/apply_snapshot_chunk.rs new file mode 100644 index 000000000..eb7d4e113 --- /dev/null +++ b/tendermint/src/abci/request/apply_snapshot_chunk.rs @@ -0,0 +1,71 @@ +use bytes::Bytes; + +// bring into scope for doc links +#[allow(unused)] +use super::{super::types::Snapshot, Info, LoadSnapshotChunk}; + +/// Applies a snapshot chunk. +/// +/// The application can choose to refetch chunks and/or ban P2P peers as +/// appropriate. Tendermint will not do this unless instructed by the +/// application. +/// +/// The application may want to verify each chunk, e.g., by attaching chunk +/// hashes in [`Snapshot::metadata`] and/or incrementally verifying contents +/// against `app_hash`. +/// +/// When all chunks have been accepted, Tendermint will make an ABCI [`Info`] +/// request to verify that `last_block_app_hash` and `last_block_height` match +/// the expected values, and record the `app_version` in the node state. It then +/// switches to fast sync or consensus and joins the network. +/// +/// If Tendermint is unable to retrieve the next chunk after some time (e.g., +/// because no suitable peers are available), it will reject the snapshot and try +/// a different one via `OfferSnapshot`. The application should be prepared to +/// reset and accept it or abort as appropriate. +/// +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#applysnapshotchunk) +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct ApplySnapshotChunk { + /// The chunk index, starting from `0`. Tendermint applies chunks sequentially. + pub index: u32, + /// The binary chunk contents, as returned by [`LoadSnapshotChunk`]. + pub chunk: Bytes, + /// The P2P ID of the node who sent this chunk. + pub sender: String, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::RequestApplySnapshotChunk { + fn from(apply_snapshot_chunk: ApplySnapshotChunk) -> Self { + Self { + index: apply_snapshot_chunk.index, + chunk: apply_snapshot_chunk.chunk, + sender: apply_snapshot_chunk.sender, + } + } +} + +impl TryFrom for ApplySnapshotChunk { + type Error = crate::Error; + + fn try_from(apply_snapshot_chunk: pb::RequestApplySnapshotChunk) -> Result { + Ok(Self { + index: apply_snapshot_chunk.index, + chunk: apply_snapshot_chunk.chunk, + sender: apply_snapshot_chunk.sender, + }) + } +} + +impl Protobuf for ApplySnapshotChunk {} diff --git a/tendermint/src/abci/request/begin_block.rs b/tendermint/src/abci/request/begin_block.rs new file mode 100644 index 000000000..a139d2e5b --- /dev/null +++ b/tendermint/src/abci/request/begin_block.rs @@ -0,0 +1,75 @@ +use bytes::Bytes; + +use crate::block; + +use super::super::types::{Evidence, LastCommitInfo}; + +// bring into scope for doc links +#[allow(unused)] +use super::DeliverTx; + +#[doc = include_str!("../doc/request-beginblock.md")] +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct BeginBlock { + /// The block's hash. + /// + /// This can be derived from the block header. + pub hash: Bytes, + /// The block header. + pub header: block::Header, + /// Information about the last commit. + /// + /// This includes the round, the list of validators, and which validators + /// signed the last block. + pub last_commit_info: LastCommitInfo, + /// Evidence of validator misbehavior. + pub byzantine_validators: Vec, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::{TryFrom, TryInto}; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::RequestBeginBlock { + fn from(begin_block: BeginBlock) -> Self { + Self { + hash: begin_block.hash, + header: Some(begin_block.header.into()), + last_commit_info: Some(begin_block.last_commit_info.into()), + byzantine_validators: begin_block + .byzantine_validators + .into_iter() + .map(Into::into) + .collect(), + } + } +} + +impl TryFrom for BeginBlock { + type Error = crate::Error; + + fn try_from(begin_block: pb::RequestBeginBlock) -> Result { + Ok(Self { + hash: begin_block.hash, + header: begin_block.header.ok_or("missing header")?.try_into()?, + last_commit_info: begin_block + .last_commit_info + .ok_or("missing last commit info")? + .try_into()?, + byzantine_validators: begin_block + .byzantine_validators + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + }) + } +} + +impl Protobuf for BeginBlock {} diff --git a/tendermint/src/abci/request/check_tx.rs b/tendermint/src/abci/request/check_tx.rs new file mode 100644 index 000000000..101faa507 --- /dev/null +++ b/tendermint/src/abci/request/check_tx.rs @@ -0,0 +1,72 @@ +use bytes::Bytes; + +#[doc = include_str!("../doc/request-checktx.md")] +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct CheckTx { + /// The transaction bytes. + pub tx: Bytes, + /// The kind of check to perform. + /// + /// Note: this field is called `type` in the protobuf, but we call it `kind` + /// to avoid the Rust keyword. + pub kind: CheckTxKind, +} + +/// The possible kinds of [`CheckTx`] checks. +/// +/// Note: the +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#checktx) +/// calls this `CheckTxType`, but we follow the Rust convention and name it `CheckTxKind` +/// to avoid confusion with Rust types. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(i32)] +pub enum CheckTxKind { + /// A full check is required (the default). + New = 0, + /// Indicates that the mempool is initiating a recheck of the transaction. + Recheck = 1, +} + +impl Default for CheckTxKind { + fn default() -> Self { + CheckTxKind::New + } +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::RequestCheckTx { + fn from(check_tx: CheckTx) -> Self { + Self { + tx: check_tx.tx, + r#type: check_tx.kind as i32, + } + } +} + +impl TryFrom for CheckTx { + type Error = crate::Error; + + fn try_from(check_tx: pb::RequestCheckTx) -> Result { + let kind = match check_tx.r#type { + 0 => CheckTxKind::New, + 1 => CheckTxKind::Recheck, + _ => Err("unknown checktx type")?, + }; + Ok(Self { + tx: check_tx.tx, + kind, + }) + } +} + +impl Protobuf for CheckTx {} diff --git a/tendermint/src/abci/request/deliver_tx.rs b/tendermint/src/abci/request/deliver_tx.rs new file mode 100644 index 000000000..80e58f455 --- /dev/null +++ b/tendermint/src/abci/request/deliver_tx.rs @@ -0,0 +1,35 @@ +use bytes::Bytes; + +#[doc = include_str!("../doc/request-delivertx.md")] +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct DeliverTx { + /// The bytes of the transaction to execute. + pub tx: Bytes, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::RequestDeliverTx { + fn from(deliver_tx: DeliverTx) -> Self { + Self { tx: deliver_tx.tx } + } +} + +impl TryFrom for DeliverTx { + type Error = crate::Error; + + fn try_from(deliver_tx: pb::RequestDeliverTx) -> Result { + Ok(Self { tx: deliver_tx.tx }) + } +} + +impl Protobuf for DeliverTx {} diff --git a/tendermint/src/abci/request/echo.rs b/tendermint/src/abci/request/echo.rs new file mode 100644 index 000000000..c6bfb86df --- /dev/null +++ b/tendermint/src/abci/request/echo.rs @@ -0,0 +1,37 @@ +#[doc = include_str!("../doc/request-echo.md")] +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct Echo { + /// The message to send back. + pub message: String, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::RequestEcho { + fn from(echo: Echo) -> Self { + Self { + message: echo.message, + } + } +} + +impl TryFrom for Echo { + type Error = &'static str; + + fn try_from(echo: pb::RequestEcho) -> Result { + Ok(Self { + message: echo.message, + }) + } +} + +impl Protobuf for Echo {} diff --git a/tendermint/src/abci/request/end_block.rs b/tendermint/src/abci/request/end_block.rs new file mode 100644 index 000000000..ed32e7316 --- /dev/null +++ b/tendermint/src/abci/request/end_block.rs @@ -0,0 +1,37 @@ +#[doc = include_str!("../doc/request-endblock.md")] +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct EndBlock { + /// The height of the block just executed. + pub height: i64, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::RequestEndBlock { + fn from(end_block: EndBlock) -> Self { + Self { + height: end_block.height, + } + } +} + +impl TryFrom for EndBlock { + type Error = crate::Error; + + fn try_from(end_block: pb::RequestEndBlock) -> Result { + Ok(Self { + height: end_block.height, + }) + } +} + +impl Protobuf for EndBlock {} diff --git a/tendermint/src/abci/request/info.rs b/tendermint/src/abci/request/info.rs new file mode 100644 index 000000000..f3dfa14f9 --- /dev/null +++ b/tendermint/src/abci/request/info.rs @@ -0,0 +1,49 @@ +#[doc = include_str!("../doc/request-info.md")] +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct Info { + /// The Tendermint software semantic version. + pub version: String, + /// The Tendermint block protocol version. + pub block_version: u64, + /// The Tendermint p2p protocol version. + pub p2p_version: u64, + /// The Tendermint ABCI semantic version. + pub abci_version: String, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::RequestInfo { + fn from(info: Info) -> Self { + Self { + version: info.version, + block_version: info.block_version, + p2p_version: info.p2p_version, + abci_version: info.abci_version, + } + } +} + +impl TryFrom for Info { + type Error = &'static str; + + fn try_from(info: pb::RequestInfo) -> Result { + Ok(Self { + version: info.version, + block_version: info.block_version, + p2p_version: info.p2p_version, + abci_version: info.abci_version, + }) + } +} + +impl Protobuf for Info {} diff --git a/tendermint/src/abci/request/init_chain.rs b/tendermint/src/abci/request/init_chain.rs new file mode 100644 index 000000000..bedee4bae --- /dev/null +++ b/tendermint/src/abci/request/init_chain.rs @@ -0,0 +1,71 @@ +use bytes::Bytes; +use chrono::{DateTime, Utc}; + +use super::super::{params::ConsensusParams, types::ValidatorUpdate}; + +/// Called on genesis to initialize chain state. +/// +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#initchain) +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct InitChain { + /// The genesis time. + pub time: DateTime, + /// The ID of the blockchain. + pub chain_id: String, + /// Initial consensus-critical parameters. + pub consensus_params: ConsensusParams, + /// Initial genesis validators, sorted by voting power. + pub validators: Vec, + /// Serialized JSON bytes containing the initial application state. + pub app_state_bytes: Bytes, + /// Height of the initial block (typically `1`). + pub initial_height: i64, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::{TryFrom, TryInto}; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::RequestInitChain { + fn from(init_chain: InitChain) -> Self { + Self { + time: Some(init_chain.time.into()), + chain_id: init_chain.chain_id, + consensus_params: Some(init_chain.consensus_params.into()), + validators: init_chain.validators.into_iter().map(Into::into).collect(), + app_state_bytes: init_chain.app_state_bytes, + initial_height: init_chain.initial_height, + } + } +} + +impl TryFrom for InitChain { + type Error = crate::Error; + + fn try_from(init_chain: pb::RequestInitChain) -> Result { + Ok(Self { + time: init_chain.time.ok_or("missing genesis time")?.try_into()?, + chain_id: init_chain.chain_id, + consensus_params: init_chain + .consensus_params + .ok_or("missing consensus params")? + .try_into()?, + validators: init_chain + .validators + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + app_state_bytes: init_chain.app_state_bytes, + initial_height: init_chain.initial_height, + }) + } +} + +impl Protobuf for InitChain {} diff --git a/tendermint/src/abci/request/load_snapshot_chunk.rs b/tendermint/src/abci/request/load_snapshot_chunk.rs new file mode 100644 index 000000000..b4cead881 --- /dev/null +++ b/tendermint/src/abci/request/load_snapshot_chunk.rs @@ -0,0 +1,45 @@ +#[doc = include_str!("../doc/request-loadsnapshotchunk.md")] +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct LoadSnapshotChunk { + /// The height of the snapshot the chunks belong to. + pub height: u64, + /// An application-specific identifier of the format of the snapshot chunk. + pub format: u32, + /// The chunk index, starting from `0` for the initial chunk. + pub chunk: u32, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::RequestLoadSnapshotChunk { + fn from(load_snapshot_chunk: LoadSnapshotChunk) -> Self { + Self { + height: load_snapshot_chunk.height, + format: load_snapshot_chunk.format, + chunk: load_snapshot_chunk.chunk, + } + } +} + +impl TryFrom for LoadSnapshotChunk { + type Error = crate::Error; + + fn try_from(load_snapshot_chunk: pb::RequestLoadSnapshotChunk) -> Result { + Ok(Self { + height: load_snapshot_chunk.height, + format: load_snapshot_chunk.format, + chunk: load_snapshot_chunk.chunk, + }) + } +} + +impl Protobuf for LoadSnapshotChunk {} diff --git a/tendermint/src/abci/request/offer_snapshot.rs b/tendermint/src/abci/request/offer_snapshot.rs new file mode 100644 index 000000000..f97e603e3 --- /dev/null +++ b/tendermint/src/abci/request/offer_snapshot.rs @@ -0,0 +1,53 @@ +use bytes::Bytes; + +use super::super::types::Snapshot; + +// bring into scope for doc links +#[allow(unused)] +use super::ApplySnapshotChunk; + +#[doc = include_str!("../doc/request-offersnapshot.md")] +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct OfferSnapshot { + /// The snapshot offered for restoration. + pub snapshot: Snapshot, + /// The light client verified app hash for this height. + // XXX(hdevalence): replace with apphash + pub app_hash: Bytes, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::{TryFrom, TryInto}; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::RequestOfferSnapshot { + fn from(offer_snapshot: OfferSnapshot) -> Self { + Self { + snapshot: Some(offer_snapshot.snapshot.into()), + app_hash: offer_snapshot.app_hash, + } + } +} + +impl TryFrom for OfferSnapshot { + type Error = crate::Error; + + fn try_from(offer_snapshot: pb::RequestOfferSnapshot) -> Result { + Ok(Self { + snapshot: offer_snapshot + .snapshot + .ok_or("missing snapshot")? + .try_into()?, + app_hash: offer_snapshot.app_hash, + }) + } +} + +impl Protobuf for OfferSnapshot {} diff --git a/tendermint/src/abci/request/query.rs b/tendermint/src/abci/request/query.rs new file mode 100644 index 000000000..d209021f5 --- /dev/null +++ b/tendermint/src/abci/request/query.rs @@ -0,0 +1,64 @@ +use bytes::Bytes; + +#[doc = include_str!("../doc/request-query.md")] +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct Query { + /// Raw query bytes. + /// + /// Can be used with or in lieu of `path`. + pub data: Bytes, + /// Path of the request, like an HTTP `GET` path. + /// + /// Can be used with or in lieu of `data`. + /// + /// Applications MUST interpret `/store` as a query by key on the underlying + /// store. The key SHOULD be specified in the Data field. Applications SHOULD + /// allow queries over specific types like `/accounts/...` or `/votes/...`. + pub path: String, + /// The block height for which the query should be executed. + /// + /// The 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`. + pub height: i64, + /// Whether to return a Merkle proof with the response, if possible. + pub prove: bool, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::RequestQuery { + fn from(query: Query) -> Self { + Self { + data: query.data, + path: query.path, + height: query.height, + prove: query.prove, + } + } +} + +impl TryFrom for Query { + type Error = crate::Error; + + fn try_from(query: pb::RequestQuery) -> Result { + Ok(Self { + data: query.data, + path: query.path, + height: query.height, + prove: query.prove, + }) + } +} + +impl Protobuf for Query {} diff --git a/tendermint/src/abci/response.rs b/tendermint/src/abci/response.rs new file mode 100644 index 000000000..28fc9089b --- /dev/null +++ b/tendermint/src/abci/response.rs @@ -0,0 +1,289 @@ +//! ABCI responses and response data. +//! +//! The [`Response`] enum records all possible ABCI responses. Responses that +//! contain data are modeled as a separate struct, to avoid duplication of field +//! definitions. + +// IMPORTANT NOTE ON DOCUMENTATION: +// +// The documentation for each request type is adapted from the ABCI Methods and +// Types spec document. However, the same logical request may appear three +// times, as a struct with the request data, as a Request variant, and as a +// CategoryRequest variant. +// +// To avoid duplication, this documentation is stored in the doc/ folder in +// individual .md files, which are pasted onto the relevant items using #[doc = +// include_str!(...)]. +// +// This is also why certain submodules have #[allow(unused)] imports to bring +// items into scope for doc links, rather than changing the doc links -- it +// allows the doc comments to be copied without editing. + +use std::convert::{TryFrom, TryInto}; + +// bring into scope for doc links +#[allow(unused)] +use super::types::Snapshot; + +mod apply_snapshot_chunk; +mod begin_block; +mod check_tx; +mod commit; +mod deliver_tx; +mod echo; +mod end_block; +mod exception; +mod info; +mod init_chain; +mod list_snapshots; +mod load_snapshot_chunk; +mod offer_snapshot; +mod query; + +pub use apply_snapshot_chunk::{ApplySnapshotChunk, ApplySnapshotChunkResult}; +pub use begin_block::BeginBlock; +pub use check_tx::CheckTx; +pub use commit::Commit; +pub use deliver_tx::DeliverTx; +pub use echo::Echo; +pub use end_block::EndBlock; +pub use exception::Exception; +pub use info::Info; +pub use init_chain::InitChain; +pub use list_snapshots::ListSnapshots; +pub use load_snapshot_chunk::LoadSnapshotChunk; +pub use offer_snapshot::OfferSnapshot; +pub use query::Query; + +/// All possible ABCI responses. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum Response { + #[doc = include_str!("doc/response-exception.md")] + Exception(Exception), + #[doc = include_str!("doc/response-echo.md")] + Echo(Echo), + #[doc = include_str!("doc/response-flush.md")] + Flush, + #[doc = include_str!("doc/response-info.md")] + Info(Info), + #[doc = include_str!("doc/response-initchain.md")] + InitChain(InitChain), + #[doc = include_str!("doc/response-query.md")] + Query(Query), + #[doc = include_str!("doc/response-beginblock.md")] + BeginBlock(BeginBlock), + #[doc = include_str!("doc/response-checktx.md")] + CheckTx(CheckTx), + #[doc = include_str!("doc/response-delivertx.md")] + DeliverTx(DeliverTx), + #[doc = include_str!("doc/response-endblock.md")] + EndBlock(EndBlock), + #[doc = include_str!("doc/response-commit.md")] + Commit(Commit), + #[doc = include_str!("doc/response-listsnapshots.md")] + ListSnapshots(ListSnapshots), + #[doc = include_str!("doc/response-offersnapshot.md")] + OfferSnapshot(OfferSnapshot), + #[doc = include_str!("doc/response-loadsnapshotchunk.md")] + LoadSnapshotChunk(LoadSnapshotChunk), + #[doc = include_str!("doc/response-applysnapshotchunk.md")] + ApplySnapshotChunk(ApplySnapshotChunk), +} + +/// The consensus category of ABCI responses. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum ConsensusResponse { + #[doc = include_str!("doc/response-initchain.md")] + InitChain(InitChain), + #[doc = include_str!("doc/response-beginblock.md")] + BeginBlock(BeginBlock), + #[doc = include_str!("doc/response-delivertx.md")] + DeliverTx(DeliverTx), + #[doc = include_str!("doc/response-endblock.md")] + EndBlock(EndBlock), + #[doc = include_str!("doc/response-commit.md")] + Commit(Commit), +} + +impl From for Response { + fn from(req: ConsensusResponse) -> Self { + match req { + ConsensusResponse::InitChain(x) => Self::InitChain(x), + ConsensusResponse::BeginBlock(x) => Self::BeginBlock(x), + ConsensusResponse::DeliverTx(x) => Self::DeliverTx(x), + ConsensusResponse::EndBlock(x) => Self::EndBlock(x), + ConsensusResponse::Commit(x) => Self::Commit(x), + } + } +} + +impl TryFrom for ConsensusResponse { + type Error = &'static str; + fn try_from(req: Response) -> Result { + match req { + Response::InitChain(x) => Ok(Self::InitChain(x)), + Response::BeginBlock(x) => Ok(Self::BeginBlock(x)), + Response::DeliverTx(x) => Ok(Self::DeliverTx(x)), + Response::EndBlock(x) => Ok(Self::EndBlock(x)), + Response::Commit(x) => Ok(Self::Commit(x)), + _ => Err("wrong request type"), + } + } +} + +/// The mempool category of ABCI responses. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum MempoolResponse { + #[doc = include_str!("doc/response-checktx.md")] + CheckTx(CheckTx), +} + +impl From for Response { + fn from(req: MempoolResponse) -> Self { + match req { + MempoolResponse::CheckTx(x) => Self::CheckTx(x), + } + } +} + +impl TryFrom for MempoolResponse { + type Error = &'static str; + fn try_from(req: Response) -> Result { + match req { + Response::CheckTx(x) => Ok(Self::CheckTx(x)), + _ => Err("wrong request type"), + } + } +} + +/// The info category of ABCI responses. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum InfoResponse { + #[doc = include_str!("doc/response-echo.md")] + Echo(Echo), + #[doc = include_str!("doc/response-info.md")] + Info(Info), + #[doc = include_str!("doc/response-query.md")] + Query(Query), +} + +impl From for Response { + fn from(req: InfoResponse) -> Self { + match req { + InfoResponse::Echo(x) => Self::Echo(x), + InfoResponse::Info(x) => Self::Info(x), + InfoResponse::Query(x) => Self::Query(x), + } + } +} + +impl TryFrom for InfoResponse { + type Error = &'static str; + fn try_from(req: Response) -> Result { + match req { + Response::Echo(x) => Ok(Self::Echo(x)), + Response::Info(x) => Ok(Self::Info(x)), + Response::Query(x) => Ok(Self::Query(x)), + _ => Err("wrong request type"), + } + } +} + +/// The snapshot category of ABCI responses. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum SnapshotResponse { + #[doc = include_str!("doc/response-listsnapshots.md")] + ListSnapshots(ListSnapshots), + #[doc = include_str!("doc/response-offersnapshot.md")] + OfferSnapshot(OfferSnapshot), + #[doc = include_str!("doc/response-loadsnapshotchunk.md")] + LoadSnapshotChunk(LoadSnapshotChunk), + #[doc = include_str!("doc/response-applysnapshotchunk.md")] + ApplySnapshotChunk(ApplySnapshotChunk), +} + +impl From for Response { + fn from(req: SnapshotResponse) -> Self { + match req { + SnapshotResponse::ListSnapshots(x) => Self::ListSnapshots(x), + SnapshotResponse::OfferSnapshot(x) => Self::OfferSnapshot(x), + SnapshotResponse::LoadSnapshotChunk(x) => Self::LoadSnapshotChunk(x), + SnapshotResponse::ApplySnapshotChunk(x) => Self::ApplySnapshotChunk(x), + } + } +} + +impl TryFrom for SnapshotResponse { + type Error = &'static str; + fn try_from(req: Response) -> Result { + match req { + Response::ListSnapshots(x) => Ok(Self::ListSnapshots(x)), + Response::OfferSnapshot(x) => Ok(Self::OfferSnapshot(x)), + Response::LoadSnapshotChunk(x) => Ok(Self::LoadSnapshotChunk(x)), + Response::ApplySnapshotChunk(x) => Ok(Self::ApplySnapshotChunk(x)), + _ => Err("wrong request type"), + } + } +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::Response { + fn from(response: Response) -> pb::Response { + use pb::response::Value; + let value = match response { + Response::Exception(x) => Some(Value::Exception(x.into())), + Response::Echo(x) => Some(Value::Echo(x.into())), + Response::Flush => Some(Value::Flush(Default::default())), + Response::Info(x) => Some(Value::Info(x.into())), + Response::InitChain(x) => Some(Value::InitChain(x.into())), + Response::Query(x) => Some(Value::Query(x.into())), + Response::BeginBlock(x) => Some(Value::BeginBlock(x.into())), + Response::CheckTx(x) => Some(Value::CheckTx(x.into())), + Response::DeliverTx(x) => Some(Value::DeliverTx(x.into())), + Response::EndBlock(x) => Some(Value::EndBlock(x.into())), + Response::Commit(x) => Some(Value::Commit(x.into())), + Response::ListSnapshots(x) => Some(Value::ListSnapshots(x.into())), + Response::OfferSnapshot(x) => Some(Value::OfferSnapshot(x.into())), + Response::LoadSnapshotChunk(x) => Some(Value::LoadSnapshotChunk(x.into())), + Response::ApplySnapshotChunk(x) => Some(Value::ApplySnapshotChunk(x.into())), + }; + pb::Response { value } + } +} + +impl TryFrom for Response { + type Error = crate::Error; + + fn try_from(response: pb::Response) -> Result { + use pb::response::Value; + match response.value { + Some(Value::Exception(x)) => Ok(Response::Exception(x.try_into()?)), + Some(Value::Echo(x)) => Ok(Response::Echo(x.try_into()?)), + Some(Value::Flush(_)) => Ok(Response::Flush), + Some(Value::Info(x)) => Ok(Response::Info(x.try_into()?)), + Some(Value::InitChain(x)) => Ok(Response::InitChain(x.try_into()?)), + Some(Value::Query(x)) => Ok(Response::Query(x.try_into()?)), + Some(Value::BeginBlock(x)) => Ok(Response::BeginBlock(x.try_into()?)), + Some(Value::CheckTx(x)) => Ok(Response::CheckTx(x.try_into()?)), + Some(Value::DeliverTx(x)) => Ok(Response::DeliverTx(x.try_into()?)), + Some(Value::EndBlock(x)) => Ok(Response::EndBlock(x.try_into()?)), + Some(Value::Commit(x)) => Ok(Response::Commit(x.try_into()?)), + Some(Value::ListSnapshots(x)) => Ok(Response::ListSnapshots(x.try_into()?)), + Some(Value::OfferSnapshot(x)) => Ok(Response::OfferSnapshot(x.try_into()?)), + Some(Value::LoadSnapshotChunk(x)) => Ok(Response::LoadSnapshotChunk(x.try_into()?)), + Some(Value::ApplySnapshotChunk(x)) => Ok(Response::ApplySnapshotChunk(x.try_into()?)), + None => Err("no response in proto".into()), + } + } +} + +impl Protobuf for Response {} diff --git a/tendermint/src/abci/response/apply_snapshot_chunk.rs b/tendermint/src/abci/response/apply_snapshot_chunk.rs new file mode 100644 index 000000000..563a2637a --- /dev/null +++ b/tendermint/src/abci/response/apply_snapshot_chunk.rs @@ -0,0 +1,89 @@ +#[doc = include_str!("../doc/response-applysnapshotchunk.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct ApplySnapshotChunk { + /// The result of applying the snapshot chunk. + pub result: ApplySnapshotChunkResult, + /// Refetch and reapply the given chunks, regardless of `result`. + /// + /// Only the listed chunks will be refetched, and reapplied in sequential + /// order. + pub refetch_chunks: Vec, + /// Reject the given P2P senders, regardless of `result`. + /// + /// Any chunks already applied will not be refetched unless explicitly + /// requested, but queued chunks from these senders will be discarded, and + /// new chunks or other snapshots rejected. + pub reject_senders: Vec, +} + +/// The result of applying a snapshot chunk. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(i32)] +pub enum ApplySnapshotChunkResult { + /// Unknown result, abort all snapshot restoration. + Unknown = 0, + /// The chunk was accepted. + Accept = 1, + /// Abort snapshot restoration, and don't try any other snapshots. + Abort = 2, + /// Reapply this chunk, combine with + /// [`refetch_chunks`](ApplySnapshotChunk::refetch_chunks) and + /// [`reject_senders`](ApplySnapshotChunk::reject_senders) as appropriate. + Retry = 3, + /// Restart this snapshot from + /// [`OfferSnapshot`](super::super::request::OfferSnapshot), + /// reusing chunks unless instructed otherwise. + RetrySnapshot = 4, + /// Reject this snapshot, try a different one. + RejectSnapshot = 5, +} + +impl Default for ApplySnapshotChunkResult { + fn default() -> Self { + Self::Unknown + } +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseApplySnapshotChunk { + fn from(apply_snapshot_chunk: ApplySnapshotChunk) -> Self { + Self { + result: apply_snapshot_chunk.result as i32, + refetch_chunks: apply_snapshot_chunk.refetch_chunks, + reject_senders: apply_snapshot_chunk.reject_senders, + } + } +} + +impl TryFrom for ApplySnapshotChunk { + type Error = crate::Error; + + fn try_from(apply_snapshot_chunk: pb::ResponseApplySnapshotChunk) -> Result { + let result = match apply_snapshot_chunk.result { + 0 => ApplySnapshotChunkResult::Unknown, + 1 => ApplySnapshotChunkResult::Accept, + 2 => ApplySnapshotChunkResult::Abort, + 3 => ApplySnapshotChunkResult::Retry, + 4 => ApplySnapshotChunkResult::RetrySnapshot, + 5 => ApplySnapshotChunkResult::RejectSnapshot, + _ => Err("unknown snapshot chunk result")?, + }; + Ok(Self { + result, + refetch_chunks: apply_snapshot_chunk.refetch_chunks, + reject_senders: apply_snapshot_chunk.reject_senders, + }) + } +} + +impl Protobuf for ApplySnapshotChunk {} diff --git a/tendermint/src/abci/response/begin_block.rs b/tendermint/src/abci/response/begin_block.rs new file mode 100644 index 000000000..0382191c4 --- /dev/null +++ b/tendermint/src/abci/response/begin_block.rs @@ -0,0 +1,44 @@ +// XXX(hdevalence): fix this import +use super::super::event::Event; + +#[doc = include_str!("../doc/response-beginblock.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct BeginBlock { + /// Events that occurred while beginning the block. + pub events: Vec, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::{TryFrom, TryInto}; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseBeginBlock { + fn from(begin_block: BeginBlock) -> Self { + Self { + events: begin_block.events.into_iter().map(Into::into).collect(), + } + } +} + +impl TryFrom for BeginBlock { + type Error = crate::Error; + + fn try_from(begin_block: pb::ResponseBeginBlock) -> Result { + Ok(Self { + events: begin_block + .events + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + }) + } +} + +impl Protobuf for BeginBlock {} diff --git a/tendermint/src/abci/response/check_tx.rs b/tendermint/src/abci/response/check_tx.rs new file mode 100644 index 000000000..3097d7264 --- /dev/null +++ b/tendermint/src/abci/response/check_tx.rs @@ -0,0 +1,94 @@ +use bytes::Bytes; + +// XXX(hdevalence) fix import +use super::super::event::Event; + +#[doc = include_str!("../doc/response-checktx.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct CheckTx { + /// The response code. + /// + /// Transactions where `code != 0` will be rejected; these transactions will + /// not be broadcast to other nodes or included in a proposal block. + /// Tendermint attributes no other value to the response code. + pub code: u32, + /// Result bytes, if any. + pub data: Bytes, + /// The output of the application's logger. + /// + /// **May be non-deterministic**. + pub log: String, + /// Additional information. + /// + /// **May be non-deterministic**. + pub info: String, + /// Amount of gas requested for the transaction. + pub gas_wanted: i64, + /// Amount of gas consumed by the transaction. + pub gas_used: i64, + /// Events that occurred while checking the transaction. + pub events: Vec, + /// The namespace for the `code`. + pub codespace: String, + /// The transaction's sender (e.g. the signer). + pub sender: String, + /// The transaction's priority (for mempool ordering). + pub priority: i64, + /* mempool_error is contained in the proto, but skipped here: + * > mempool_error is set by Tendermint. + * > ABCI applictions creating a ResponseCheckTX should not set mempool_error. */ +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::{TryFrom, TryInto}; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseCheckTx { + fn from(check_tx: CheckTx) -> Self { + Self { + code: check_tx.code, + data: check_tx.data, + log: check_tx.log, + info: check_tx.info, + gas_wanted: check_tx.gas_wanted, + gas_used: check_tx.gas_used, + events: check_tx.events.into_iter().map(Into::into).collect(), + codespace: check_tx.codespace, + sender: check_tx.sender, + priority: check_tx.priority, + mempool_error: String::default(), + } + } +} + +impl TryFrom for CheckTx { + type Error = crate::Error; + + fn try_from(check_tx: pb::ResponseCheckTx) -> Result { + Ok(Self { + code: check_tx.code, + data: check_tx.data, + log: check_tx.log, + info: check_tx.info, + gas_wanted: check_tx.gas_wanted, + gas_used: check_tx.gas_used, + events: check_tx + .events + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + codespace: check_tx.codespace, + sender: check_tx.sender, + priority: check_tx.priority, + }) + } +} + +impl Protobuf for CheckTx {} diff --git a/tendermint/src/abci/response/commit.rs b/tendermint/src/abci/response/commit.rs new file mode 100644 index 000000000..088458869 --- /dev/null +++ b/tendermint/src/abci/response/commit.rs @@ -0,0 +1,46 @@ +use bytes::Bytes; + +#[doc = include_str!("../doc/response-commit.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct Commit { + /// The Merkle root hash of the application state + /// + /// XXX(hdevalence) - is this different from an app hash? + /// XXX(hdevalence) - rename to app_hash ? + pub data: Bytes, + /// Blocks below this height may be removed. + pub retain_height: i64, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseCommit { + fn from(commit: Commit) -> Self { + Self { + data: commit.data, + retain_height: commit.retain_height, + } + } +} + +impl TryFrom for Commit { + type Error = crate::Error; + + fn try_from(commit: pb::ResponseCommit) -> Result { + Ok(Self { + data: commit.data, + retain_height: commit.retain_height, + }) + } +} + +impl Protobuf for Commit {} diff --git a/tendermint/src/abci/response/deliver_tx.rs b/tendermint/src/abci/response/deliver_tx.rs new file mode 100644 index 000000000..06489569a --- /dev/null +++ b/tendermint/src/abci/response/deliver_tx.rs @@ -0,0 +1,82 @@ +use bytes::Bytes; + +// XXX(hdevalence): fix import +use super::super::event::Event; + +#[doc = include_str!("../doc/response-delivertx.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct DeliverTx { + /// The response code. + /// + /// This code should be `0` only if the transaction is fully valid. However, + /// invalid transactions included in a block will still be executed against + /// the application state. + pub code: u32, + /// Result bytes, if any. + pub data: Bytes, + /// The output of the application's logger. + /// + /// **May be non-deterministic**. + pub log: String, + /// Additional information. + /// + /// **May be non-deterministic**. + pub info: String, + /// Amount of gas requested for the transaction. + pub gas_wanted: i64, + /// Amount of gas consumed by the transaction. + pub gas_used: i64, + /// Events that occurred while executing the transaction. + pub events: Vec, + /// The namespace for the `code`. + pub codespace: String, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::{TryFrom, TryInto}; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseDeliverTx { + fn from(deliver_tx: DeliverTx) -> Self { + Self { + code: deliver_tx.code, + data: deliver_tx.data, + log: deliver_tx.log, + info: deliver_tx.info, + gas_wanted: deliver_tx.gas_wanted, + gas_used: deliver_tx.gas_used, + events: deliver_tx.events.into_iter().map(Into::into).collect(), + codespace: deliver_tx.codespace, + } + } +} + +impl TryFrom for DeliverTx { + type Error = crate::Error; + + fn try_from(deliver_tx: pb::ResponseDeliverTx) -> Result { + Ok(Self { + code: deliver_tx.code, + data: deliver_tx.data, + log: deliver_tx.log, + info: deliver_tx.info, + gas_wanted: deliver_tx.gas_wanted, + gas_used: deliver_tx.gas_used, + events: deliver_tx + .events + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + codespace: deliver_tx.codespace, + }) + } +} + +impl Protobuf for DeliverTx {} diff --git a/tendermint/src/abci/response/echo.rs b/tendermint/src/abci/response/echo.rs new file mode 100644 index 000000000..9be654796 --- /dev/null +++ b/tendermint/src/abci/response/echo.rs @@ -0,0 +1,37 @@ +#[doc = include_str!("../doc/response-echo.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct Echo { + /// The message sent in the request. + pub message: String, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseEcho { + fn from(echo: Echo) -> Self { + Self { + message: echo.message, + } + } +} + +impl TryFrom for Echo { + type Error = &'static str; + + fn try_from(echo: pb::ResponseEcho) -> Result { + Ok(Self { + message: echo.message, + }) + } +} + +impl Protobuf for Echo {} diff --git a/tendermint/src/abci/response/end_block.rs b/tendermint/src/abci/response/end_block.rs new file mode 100644 index 000000000..872186055 --- /dev/null +++ b/tendermint/src/abci/response/end_block.rs @@ -0,0 +1,66 @@ +// XXX(hdevalence) fix import +use super::super::event::Event; +use super::super::{params::ConsensusParams, types::ValidatorUpdate}; + +#[doc = include_str!("../doc/response-endblock.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct EndBlock { + /// Changes to the validator set, if any. + /// + /// Setting the voting power to 0 removes a validator. + pub validator_updates: Vec, + /// Changes to consensus parameters (optional). + pub consensus_param_updates: Option, + /// Events that occurred while ending the block. + pub events: Vec, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::{TryFrom, TryInto}; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseEndBlock { + fn from(end_block: EndBlock) -> Self { + Self { + validator_updates: end_block + .validator_updates + .into_iter() + .map(Into::into) + .collect(), + consensus_param_updates: end_block.consensus_param_updates.map(Into::into), + events: end_block.events.into_iter().map(Into::into).collect(), + } + } +} + +impl TryFrom for EndBlock { + type Error = crate::Error; + + fn try_from(end_block: pb::ResponseEndBlock) -> Result { + Ok(Self { + validator_updates: end_block + .validator_updates + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + consensus_param_updates: end_block + .consensus_param_updates + .map(TryInto::try_into) + .transpose()?, + events: end_block + .events + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + }) + } +} + +impl Protobuf for EndBlock {} diff --git a/tendermint/src/abci/response/exception.rs b/tendermint/src/abci/response/exception.rs new file mode 100644 index 000000000..08bb107b8 --- /dev/null +++ b/tendermint/src/abci/response/exception.rs @@ -0,0 +1,37 @@ +#[doc = include_str!("../doc/response-exception.md")] +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct Exception { + /// Undocumented. + pub error: String, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseException { + fn from(exception: Exception) -> Self { + Self { + error: exception.error, + } + } +} + +impl TryFrom for Exception { + type Error = &'static str; + + fn try_from(exception: pb::ResponseException) -> Result { + Ok(Self { + error: exception.error, + }) + } +} + +impl Protobuf for Exception {} diff --git a/tendermint/src/abci/response/info.rs b/tendermint/src/abci/response/info.rs new file mode 100644 index 000000000..84b05282c --- /dev/null +++ b/tendermint/src/abci/response/info.rs @@ -0,0 +1,56 @@ +use bytes::Bytes; + +#[doc = include_str!("../doc/response-info.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct Info { + /// Some arbitrary information. + pub data: String, + /// The application software semantic version. + pub version: String, + /// The application protocol version. + pub app_version: u64, + /// The latest block for which the app has called [`Commit`](super::super::Request::Commit). + pub last_block_height: i64, + /// The latest result of [`Commit`](super::super::Request::Commit). + // XXX(hdevalence): fix this, should be apphash? + pub last_block_app_hash: Bytes, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseInfo { + fn from(info: Info) -> Self { + Self { + data: info.data, + version: info.version, + app_version: info.app_version, + last_block_height: info.last_block_height, + last_block_app_hash: info.last_block_app_hash, + } + } +} + +impl TryFrom for Info { + type Error = &'static str; + + fn try_from(info: pb::ResponseInfo) -> Result { + Ok(Self { + data: info.data, + version: info.version, + app_version: info.app_version, + last_block_height: info.last_block_height, + last_block_app_hash: info.last_block_app_hash, + }) + } +} + +impl Protobuf for Info {} diff --git a/tendermint/src/abci/response/init_chain.rs b/tendermint/src/abci/response/init_chain.rs new file mode 100644 index 000000000..195a4f8b4 --- /dev/null +++ b/tendermint/src/abci/response/init_chain.rs @@ -0,0 +1,63 @@ +use bytes::Bytes; + +use super::super::{params::ConsensusParams, types::ValidatorUpdate}; + +#[doc = include_str!("../doc/response-initchain.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct InitChain { + /// Initial consensus-critical parameters (optional). + pub consensus_params: Option, + /// Initial validator set (optional). + /// + /// If this list is empty, the initial validator set will be the one given in + /// [`request::InitChain::validators`](super::super::request::InitChain::validators). + /// + /// If this list is nonempty, it will be the initial validator set, instead + /// of the one given in + /// [`request::InitChain::validators`](super::super::request::InitChain::validators). + pub validators: Vec, + /// Initial application hash. + pub app_hash: Bytes, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::{TryFrom, TryInto}; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseInitChain { + fn from(init_chain: InitChain) -> Self { + Self { + consensus_params: init_chain.consensus_params.map(Into::into), + validators: init_chain.validators.into_iter().map(Into::into).collect(), + app_hash: init_chain.app_hash, + } + } +} + +impl TryFrom for InitChain { + type Error = crate::Error; + + fn try_from(init_chain: pb::ResponseInitChain) -> Result { + Ok(Self { + consensus_params: init_chain + .consensus_params + .map(TryInto::try_into) + .transpose()?, + validators: init_chain + .validators + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + app_hash: init_chain.app_hash, + }) + } +} + +impl Protobuf for InitChain {} diff --git a/tendermint/src/abci/response/list_snapshots.rs b/tendermint/src/abci/response/list_snapshots.rs new file mode 100644 index 000000000..853b595e4 --- /dev/null +++ b/tendermint/src/abci/response/list_snapshots.rs @@ -0,0 +1,47 @@ +use super::super::types::Snapshot; + +#[doc = include_str!("../doc/response-listsnapshots.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct ListSnapshots { + /// A list of local state snapshots. + pub snapshots: Vec, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::{TryFrom, TryInto}; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseListSnapshots { + fn from(list_snapshots: ListSnapshots) -> Self { + Self { + snapshots: list_snapshots + .snapshots + .into_iter() + .map(Into::into) + .collect(), + } + } +} + +impl TryFrom for ListSnapshots { + type Error = crate::Error; + + fn try_from(list_snapshots: pb::ResponseListSnapshots) -> Result { + Ok(Self { + snapshots: list_snapshots + .snapshots + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + }) + } +} + +impl Protobuf for ListSnapshots {} diff --git a/tendermint/src/abci/response/load_snapshot_chunk.rs b/tendermint/src/abci/response/load_snapshot_chunk.rs new file mode 100644 index 000000000..32b8de563 --- /dev/null +++ b/tendermint/src/abci/response/load_snapshot_chunk.rs @@ -0,0 +1,42 @@ +use bytes::Bytes; + +#[doc = include_str!("../doc/response-loadsnapshotchunk.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct LoadSnapshotChunk { + /// The binary chunk contents, in an arbitrary format. + /// + /// Chunk messages cannot be larger than 16MB *including metadata*, so 10MB + /// is a good starting point. + pub chunk: Bytes, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseLoadSnapshotChunk { + fn from(load_snapshot_chunk: LoadSnapshotChunk) -> Self { + Self { + chunk: load_snapshot_chunk.chunk, + } + } +} + +impl TryFrom for LoadSnapshotChunk { + type Error = crate::Error; + + fn try_from(load_snapshot_chunk: pb::ResponseLoadSnapshotChunk) -> Result { + Ok(Self { + chunk: load_snapshot_chunk.chunk, + }) + } +} + +impl Protobuf for LoadSnapshotChunk {} diff --git a/tendermint/src/abci/response/offer_snapshot.rs b/tendermint/src/abci/response/offer_snapshot.rs new file mode 100644 index 000000000..1df7d6c57 --- /dev/null +++ b/tendermint/src/abci/response/offer_snapshot.rs @@ -0,0 +1,64 @@ +// bring into scope for doc links +#[allow(unused)] +use super::super::types::Snapshot; + +#[doc = include_str!("../doc/response-offersnapshot.md")] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(i32)] +pub enum OfferSnapshot { + /// Unknown result, abort all snapshot restoration + Unknown = 0, + /// Snapshot accepted, apply chunks + Accept = 1, + /// Abort all snapshot restoration + Abort = 2, + /// Reject this specific snapshot, try others + Reject = 3, + /// Reject all snapshots of this format, try others + RejectFormat = 4, + /// Reject all snapshots from the sender(s), try others + RejectSender = 5, +} + +impl Default for OfferSnapshot { + fn default() -> Self { + Self::Unknown + } +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::TryFrom; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseOfferSnapshot { + fn from(offer_snapshot: OfferSnapshot) -> Self { + Self { + result: offer_snapshot as i32, + } + } +} + +impl TryFrom for OfferSnapshot { + type Error = crate::Error; + + fn try_from(offer_snapshot: pb::ResponseOfferSnapshot) -> Result { + Ok(match offer_snapshot.result { + 0 => OfferSnapshot::Unknown, + 1 => OfferSnapshot::Accept, + 2 => OfferSnapshot::Abort, + 3 => OfferSnapshot::Reject, + 4 => OfferSnapshot::RejectFormat, + 5 => OfferSnapshot::RejectSender, + _ => Err("unknown offer snapshot result code")?, + }) + } +} + +impl Protobuf for OfferSnapshot {} diff --git a/tendermint/src/abci/response/query.rs b/tendermint/src/abci/response/query.rs new file mode 100644 index 000000000..6d9cf44c8 --- /dev/null +++ b/tendermint/src/abci/response/query.rs @@ -0,0 +1,83 @@ +use bytes::Bytes; + +/// XXX(hdevalence): hide merkle::proof and re-export its contents from merkle? +use crate::merkle::proof as merkle; + +#[doc = include_str!("../doc/response-query.md")] +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct Query { + /// The response code for the query. + pub code: u32, + /// The output of the application's logger. + /// + /// **May be non-deterministic**. + pub log: String, + /// Additional information. + /// + /// **May be non-deterministic**. + pub info: String, + /// The index of the key in the tree. + pub index: i64, + /// The key of the matching data. + pub key: Bytes, + /// The value of the matching data. + pub value: Bytes, + /// Serialized proof for the value data, if requested, to be verified against + /// the app hash for the given `height`. + pub proof: Option, + /// 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`. + pub height: i64, + /// The namespace for the `code`. + pub codespace: String, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use std::convert::{TryFrom, TryInto}; +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::ResponseQuery { + fn from(query: Query) -> Self { + Self { + code: query.code, + log: query.log, + info: query.info, + index: query.index, + key: query.key, + value: query.value, + proof_ops: query.proof.map(Into::into), + height: query.height, + codespace: query.codespace, + } + } +} + +impl TryFrom for Query { + type Error = crate::Error; + + fn try_from(query: pb::ResponseQuery) -> Result { + Ok(Self { + code: query.code, + log: query.log, + info: query.info, + index: query.index, + key: query.key, + value: query.value, + proof: query.proof_ops.map(TryInto::try_into).transpose()?, + height: query.height, + codespace: query.codespace, + }) + } +} + +impl Protobuf for Query {} diff --git a/tendermint/src/abci/types.rs b/tendermint/src/abci/types.rs new file mode 100644 index 000000000..f1c3ba462 --- /dev/null +++ b/tendermint/src/abci/types.rs @@ -0,0 +1,304 @@ +//! ABCI-specific data types used in requests and responses. +//! +//! These types have changes from the core data structures to better accomodate +//! ABCI applications. +//! +//! [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#data-types) + +use std::convert::{TryFrom, TryInto}; + +use bytes::Bytes; +use chrono::{DateTime, Utc}; + +use crate::PublicKey; + +/// A validator address with voting power. +/// +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#validator) +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct Validator { + /// The validator's address (the first 20 bytes of `SHA256(public_key)`). + pub address: [u8; 20], + /// The voting power of the validator. + pub power: i64, +} + +/// A change to the validator set. +/// +/// Used to inform Tendermint of changes to the validator set. +/// +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#validatorupdate) +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct ValidatorUpdate { + /// The validator's public key. + pub pub_key: PublicKey, + /// The validator's voting power. + pub power: i64, +} + +/// Information about a whether a validator signed the last block. +/// +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#voteinfo) +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct VoteInfo { + /// Identifies the validator. + pub validator: Validator, + /// Whether or not the validator signed the last block. + pub signed_last_block: bool, +} + +/// The possible kinds of [`Evidence`]. +/// +/// Note: the +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#evidencetype-2) +/// calls this `EvidenceType`, but we follow the Rust convention and name it `EvidenceKind` +/// to avoid confusion with Rust types. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(i32)] +pub enum EvidenceKind { + /// Unknown evidence type (proto default value). + Unknown = 0, + /// Evidence that the validator voted for two different blocks in the same + /// round of the same height. + DuplicateVote = 1, + /// Evidence that a validator attacked a light client. + LightClientAttack = 2, +} + +/// Evidence of validator misbehavior. +/// +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#evidence) +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct Evidence { + /// The kind of evidence. + /// + /// Note: this field is called `type` in the protobuf, but we call it `kind` + /// to avoid the Rust keyword. + pub kind: EvidenceKind, + /// The offending validator. + pub validator: Validator, + /// The height when the offense occurred. + pub height: i64, + /// The corresponding time when the offense occurred. + pub time: DateTime, + /// Total voting power of the validator set at `height`. + /// + /// This is included in case the ABCI application does not store historical + /// validators, cf. + /// [#4581](https://github.com/tendermint/tendermint/issues/4581) + pub total_voting_power: i64, +} + +/// Information on the last block commit. +/// +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#lastcommitinfo) +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct LastCommitInfo { + /// The commit round. + /// + /// Reflects the total number of rounds it took to come to consensus for the + /// current block. + pub round: i32, + /// The list of validator addresses in the last validator set, with their + /// voting power and whether or not they signed a vote. + pub votes: Vec, +} + +/// Used for state sync snapshots. +/// +/// When sent across the network, a `Snapshot` can be at most 4 MB. +/// +/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#snapshot) +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct Snapshot { + /// The height at which the snapshot was taken + pub height: u64, + /// The application-specific snapshot format identifier. + /// + /// This allows applications to version their snapshot data format and make + /// backwards-incompatible changes. Tendermint does not interpret this field. + pub format: u32, + /// The number of chunks in the snapshot. Must be at least 1. + pub chunks: u32, + /// An arbitrary snapshot hash. + /// + /// This hash must be equal only for identical snapshots across nodes. + /// Tendermint does not interpret the hash, only compares it with other + /// hashes. + pub hash: Bytes, + /// Arbitrary application metadata, e.g., chunk hashes or other verification data. + pub metadata: Bytes, +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +// XXX(hdevalence): these all use &'static str for now, this should be fixed +// to align with the crate's error-handling strategy. + +use tendermint_proto::abci as pb; +use tendermint_proto::Protobuf; + +impl From for pb::Validator { + fn from(v: Validator) -> Self { + Self { + address: Bytes::copy_from_slice(&v.address[..]), + power: v.power, + } + } +} + +impl TryFrom for Validator { + type Error = crate::Error; + + fn try_from(vu: pb::Validator) -> Result { + let address = if vu.address.len() == 20 { + let mut bytes = [0u8; 20]; + bytes.copy_from_slice(&vu.address); + bytes + } else { + return Err("wrong address length".into()); + }; + + Ok(Self { + address, + power: vu.power, + }) + } +} + +impl Protobuf for Validator {} + +impl From for pb::ValidatorUpdate { + fn from(vu: ValidatorUpdate) -> Self { + Self { + pub_key: Some(vu.pub_key.into()), + power: vu.power, + } + } +} + +impl TryFrom for ValidatorUpdate { + type Error = crate::Error; + + fn try_from(vu: pb::ValidatorUpdate) -> Result { + Ok(Self { + pub_key: vu.pub_key.ok_or("missing public key")?.try_into()?, + power: vu.power, + }) + } +} + +impl Protobuf for ValidatorUpdate {} + +impl From for pb::VoteInfo { + fn from(vi: VoteInfo) -> Self { + Self { + validator: Some(vi.validator.into()), + signed_last_block: vi.signed_last_block, + } + } +} + +impl TryFrom for VoteInfo { + type Error = crate::Error; + + fn try_from(vi: pb::VoteInfo) -> Result { + Ok(Self { + validator: vi.validator.ok_or("missing validator")?.try_into()?, + signed_last_block: vi.signed_last_block, + }) + } +} + +impl Protobuf for VoteInfo {} + +impl From for pb::Evidence { + fn from(evidence: Evidence) -> Self { + Self { + r#type: evidence.kind as i32, + validator: Some(evidence.validator.into()), + height: evidence.height, + time: Some(evidence.time.into()), + total_voting_power: evidence.total_voting_power, + } + } +} + +impl TryFrom for Evidence { + type Error = crate::Error; + + fn try_from(evidence: pb::Evidence) -> Result { + let kind = match evidence.r#type { + 0 => EvidenceKind::Unknown, + 1 => EvidenceKind::DuplicateVote, + 2 => EvidenceKind::LightClientAttack, + _ => Err("unknown evidence kind")?, + }; + + Ok(Self { + kind, + validator: evidence.validator.ok_or("missing validator")?.try_into()?, + height: evidence.height, + time: evidence.time.ok_or("missing time")?.into(), + total_voting_power: evidence.total_voting_power, + }) + } +} + +impl Protobuf for Evidence {} + +impl From for pb::LastCommitInfo { + fn from(lci: LastCommitInfo) -> Self { + Self { + round: lci.round, + votes: lci.votes.into_iter().map(Into::into).collect(), + } + } +} + +impl TryFrom for LastCommitInfo { + type Error = crate::Error; + + fn try_from(lci: pb::LastCommitInfo) -> Result { + Ok(Self { + round: lci.round, + votes: lci + .votes + .into_iter() + .map(TryInto::try_into) + .collect::>()?, + }) + } +} + +impl Protobuf for LastCommitInfo {} + +impl From for pb::Snapshot { + fn from(snapshot: Snapshot) -> Self { + Self { + height: snapshot.height, + format: snapshot.format, + chunks: snapshot.chunks, + hash: snapshot.hash, + metadata: snapshot.metadata, + } + } +} + +impl TryFrom for Snapshot { + type Error = crate::Error; + + fn try_from(snapshot: pb::Snapshot) -> Result { + Ok(Self { + height: snapshot.height, + format: snapshot.format, + chunks: snapshot.chunks, + hash: snapshot.hash, + metadata: snapshot.metadata, + }) + } +} + +impl Protobuf for Snapshot {} diff --git a/tendermint/src/lib.rs b/tendermint/src/lib.rs index 1e32b8074..aed2bd270 100644 --- a/tendermint/src/lib.rs +++ b/tendermint/src/lib.rs @@ -23,6 +23,7 @@ extern crate alloc; #[macro_use] pub mod error; +pub mod abci; pub mod account; pub mod block; pub mod chain;