Skip to content

Commit

Permalink
Updated documentation and bumped up version
Browse files Browse the repository at this point in the history
  • Loading branch information
devashishdxt committed Nov 13, 2019
1 parent ff9f1bf commit f9e54b2
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 198 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "abci-rs"
version = "0.2.0"
version = "0.3.0"
authors = ["Devashish Dixit <[email protected]>"]
license = "MIT/Apache-2.0"
description = "A Rust crate for creating ABCI applications"
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Add `abci-rs` in your `Cargo.toml`'s `dependencies` section:

```toml
[dependencies]
abci-rs = "0.2"
abci-rs = "0.3"
```

Each ABCI application has to implement three core traits corresponding to all three ABCI connections, `Consensus`,
Expand All @@ -35,16 +35,16 @@ Each ABCI application has to implement three core traits corresponding to all th
> Note: Implementations of these traits are expected to be `Send + Sync` and methods take immutable reference of `self`.
So, internal mutability must be handled using thread safe (`Arc`, `Mutex`, etc.) constructs.

After implementing all three above mentioned `trait`s, you can create a `Server` object and use `run_sync()` or
`run_async()` function to start ABCI application.
After implementing all three above mentioned `trait`s, you can create a `Server` object and use `Server::run()` to start
ABCI application.

To know more, go to `examples/` to see a sample ABCI applications.
`Server::run()` is an `async` function and returns a `Future`. So, you'll need an executor to run `Future` returned from
`Server::run()`. `async-std` and `tokio` are two popular options. In `counter` example, we use `async-std`'s executor.

To know more, go to `examples/` to see a sample ABCI application.

### Features

- `async`: Enables ABCI Server with asynchronous IO
- Only supports **`tokio`** executor.
- **Disabled** by default.
- `uds`: Enables support for running ABCI server over Unix Domain Socket (UDS)
- Supported on **Unix** only.
- **Disabled** by default.
Expand Down
174 changes: 0 additions & 174 deletions examples/async_counter.rs

This file was deleted.

21 changes: 12 additions & 9 deletions examples/sync_counter.rs → examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
sync::{Arc, Mutex},
};

use abci::{run_sync, types::*, Consensus, Info, Mempool, Server};
use abci::{async_trait, types::*, Consensus, Info, Mempool, Server};

/// Simple counter
#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -31,12 +31,13 @@ impl ConsensusConnection {
}
}

#[async_trait]
impl Consensus for ConsensusConnection {
fn init_chain(&self, _init_chain_request: InitChainRequest) -> InitChainResponse {
async fn init_chain(&self, _init_chain_request: InitChainRequest) -> InitChainResponse {
Default::default()
}

fn begin_block(&self, _begin_block_request: BeginBlockRequest) -> BeginBlockResponse {
async fn begin_block(&self, _begin_block_request: BeginBlockRequest) -> BeginBlockResponse {
let committed_state = self.committed_state.lock().unwrap().clone();

let mut current_state = self.current_state.lock().unwrap();
Expand All @@ -45,7 +46,7 @@ impl Consensus for ConsensusConnection {
Default::default()
}

fn deliver_tx(&self, deliver_tx_request: DeliverTxRequest) -> Result<DeliverTxResponse> {
async fn deliver_tx(&self, deliver_tx_request: DeliverTxRequest) -> Result<DeliverTxResponse> {
let new_counter = parse_bytes_to_counter(&deliver_tx_request.tx)?;

let mut current_state_lock = self.current_state.lock().unwrap();
Expand All @@ -65,7 +66,7 @@ impl Consensus for ConsensusConnection {
Ok(Default::default())
}

fn end_block(&self, end_block_request: EndBlockRequest) -> EndBlockResponse {
async fn end_block(&self, end_block_request: EndBlockRequest) -> EndBlockResponse {
let mut current_state_lock = self.current_state.lock().unwrap();
let mut current_state = current_state_lock.as_mut().unwrap();

Expand All @@ -75,7 +76,7 @@ impl Consensus for ConsensusConnection {
Default::default()
}

fn commit(&self) -> CommitResponse {
async fn commit(&self) -> CommitResponse {
let current_state = self.current_state.lock().unwrap().as_ref().unwrap().clone();
let mut committed_state = self.committed_state.lock().unwrap();
*committed_state = current_state;
Expand All @@ -97,8 +98,9 @@ impl MempoolConnection {
}
}

#[async_trait]
impl Mempool for MempoolConnection {
fn check_tx(&self, check_tx_request: CheckTxRequest) -> Result<CheckTxResponse> {
async fn check_tx(&self, check_tx_request: CheckTxRequest) -> Result<CheckTxResponse> {
let new_counter = parse_bytes_to_counter(&check_tx_request.tx)?;

let state_lock = self.state.lock().unwrap();
Expand Down Expand Up @@ -127,8 +129,9 @@ impl InfoConnection {
}
}

#[async_trait]
impl Info for InfoConnection {
fn info(&self, _info_request: InfoRequest) -> InfoResponse {
async fn info(&self, _info_request: InfoRequest) -> InfoResponse {
let state = self.state.lock().unwrap();

InfoResponse {
Expand Down Expand Up @@ -169,5 +172,5 @@ fn main() -> std::io::Result<()> {

let server = Server::new(consensus, mempool, info);

run_sync(&server, "127.0.0.1:26658".parse::<SocketAddr>().unwrap())
async_std::task::block_on(server.run("127.0.0.1:26658".parse::<SocketAddr>().unwrap()))
}
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//!
//! ```toml
//! [dependencies]
//! abci-rs = "0.2"
//! abci-rs = "0.3"
//! ```
//!
//! Each ABCI application has to implement three core traits corresponding to all three ABCI connections, `Consensus`,
Expand All @@ -34,16 +34,16 @@
//! > Note: Implementations of these traits are expected to be `Send + Sync` and methods take immutable reference of `self`.
//! So, internal mutability must be handled using thread safe (`Arc`, `Mutex`, etc.) constructs.
//!
//! After implementing all three above mentioned `trait`s, you can create a `Server` object and use `run_sync()` or
//! `run_async()` function to start ABCI application.
//! After implementing all three above mentioned `trait`s, you can create a `Server` object and use `Server::run()`to start
//! ABCI application.
//!
//! To know more, go to `examples/` to see a sample ABCI applications.
//! `Server::run()` is an `async` function and returns a `Future`. So, you'll need an executor to run `Future` returned from
//! `Server::run()`. `async-std` and `tokio` are two popular options. In `counter` example, we use `async-std`'s executor.
//!
//! To know more, go to `examples/` to see a sample ABCI application.
//!
//! ### Features
//!
//! - `async`: Enables ABCI Server with asynchronous IO
//! - Only supports **`tokio`** executor.
//! - **Disabled** by default.
//! - `uds`: Enables support for running ABCI server over Unix Domain Socket (UDS)
//! - Supported on **Unix** only.
//! - **Disabled** by default.
Expand Down
2 changes: 2 additions & 0 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use self::abci::{Request, Response};
const BUFLEN: usize = 10;
const MSB: u8 = 0b1000_0000;

/// Decodes a `Request` from stream
pub async fn decode<R: Read + Unpin>(mut reader: R) -> Result<Option<Request>> {
let length: i64 = read_varint(&mut reader).await?;

Expand All @@ -31,6 +32,7 @@ pub async fn decode<R: Read + Unpin>(mut reader: R) -> Result<Option<Request>> {
.map_err(|e| Error::new(ErrorKind::InvalidData, e))
}

/// Encodes a `Response` to stream
pub async fn encode<W: Write + Unpin>(message: Response, mut writer: W) -> Result<()> {
write_varint(&mut writer, i64::from(message.compute_size())).await?;

Expand Down

0 comments on commit f9e54b2

Please sign in to comment.