1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! # Tendermint ABCI library for Rust
//!
//! This library provides an application Trait and TCP server for implementing Tendemint ABCI
//! application in Rust.  The Application Trait provides default implementations for each callback
//! to simplify development.
//!
//! ## Example
//!
//! Here's a simple example that communicates with Tendermint. Defaults callbacks are handled by
//! the Trait.  The app doesn't do any actual processing on a transaction.
//!
//! ```rust,no_run
//! struct EmptyApp;
//!
//! impl abci::Application for EmptyApp {}
//!
//! fn main() {
//!     abci::run_local(EmptyApp);
//! }
//!```
//!
use std::net::SocketAddr;

extern crate bytes;
extern crate integer_encoding;
extern crate mockstream;
extern crate protobuf;

mod messages;
mod server;
mod stream;

pub use messages::abci::*;
pub use messages::merkle::*;
pub use messages::types::*;

use server::serve;

/// Main Trait for an ABCI application. Provides generic responses for all callbacks
/// Override desired callbacks as needed.  Tendermint makes 3 TCP connections to the
/// application and does so in a synchonized manner.
pub trait Application {
    /// Query Connection: Called on startup from Tendermint.  The application should normally
    /// return the last know state so Tendermint can determine if it needs to replay blocks
    /// to the application.
    fn info(&mut self, _req: &RequestInfo) -> ResponseInfo {
        ResponseInfo::new()
    }

    /// Query Connection: Set options on the application (rarely used)
    fn set_option(&mut self, _req: &RequestSetOption) -> ResponseSetOption {
        ResponseSetOption::new()
    }

    /// Query Connection: Query your application. This usually resolves through a merkle tree holding
    /// the state of the app.
    fn query(&mut self, _req: &RequestQuery) -> ResponseQuery {
        ResponseQuery::new()
    }

    /// Mempool Connection:  Used to validate incoming transactions.  If the application reponds
    /// with a non-zero value, the transaction is added to Tendermint's mempool for processing
    /// on the deliver_tx call below.
    fn check_tx(&mut self, _req: &RequestCheckTx) -> ResponseCheckTx {
        ResponseCheckTx::new()
    }

    /// Consensus Connection:  Called once on startup. Usually used to establish initial (genesis)
    /// state.
    fn init_chain(&mut self, _req: &RequestInitChain) -> ResponseInitChain {
        ResponseInitChain::new()
    }

    /// Consensus Connection: Called at the start of processing a block of transactions
    /// The flow is:
    /// begin_block()
    ///   deliver_tx()  for each transaction in the block
    /// end_block()
    /// commit()
    fn begin_block(&mut self, _req: &RequestBeginBlock) -> ResponseBeginBlock {
        ResponseBeginBlock::new()
    }

    /// Consensus Connection: Actually processing the transaction, performing some form of a
    /// state transistion.
    fn deliver_tx(&mut self, _p: &RequestDeliverTx) -> ResponseDeliverTx {
        ResponseDeliverTx::new()
    }

    /// Consensus Connection: Called at the end of the block.  Often used to update the validator set.
    fn end_block(&mut self, _req: &RequestEndBlock) -> ResponseEndBlock {
        ResponseEndBlock::new()
    }

    /// Consensus Connection: Commit the block with the latest state from the application.
    fn commit(&mut self, _req: &RequestCommit) -> ResponseCommit {
        ResponseCommit::new()
    }
}

/// Setup the app and start the server using localhost and default tendermint port 26658
pub fn run_local<A>(app: A)
where
    A: Application + 'static + Send + Sync,
{
    let addr = "127.0.0.1:26658".parse().unwrap();
    run(addr, app);
}

/// Setup the application and start the server. Use this fn when setting different ip:port.
pub fn run<A>(listen_addr: SocketAddr, app: A)
where
    A: Application + 'static + Send + Sync,
{
    serve(app, listen_addr).unwrap();
}