Crate nakamoto

source ·
Expand description

Nakamoto is a high-assurance Bitcoin light-client library.

The project is broken down into the following crates:

  • client: the core light-client library
  • p2p: the protocol implementation
  • chain: the block store and fork selection logic
  • common: common functionality used by all crates
  • net: networking backend

The client crate is intended to be the entry point for most users of the library, and is a good place to start, to see how everything fits together.

use std::{net, thread};

use nakamoto::client::{Client, Config, Error};
use nakamoto::client::network::{Network, Services};
use nakamoto::client::traits::Handle as _;

/// The network reactor we're going to use.
type Reactor = nakamoto::net::poll::Reactor<net::TcpStream>;

/// Run the light-client.
fn main() -> Result<(), Error> {
    let cfg = Config::new(Network::Testnet);

    // Create a client using the above network reactor.
    let client = Client::<Reactor>::new()?;
    let handle = client.handle();

    // Run the client on a different thread, to not block the main thread.
    thread::spawn(|| client.run(cfg).unwrap());

    // Wait for the client to be connected to a peer.
    handle.wait_for_peers(1, Services::default())?;

    // Ask the client to terminate.
    handle.shutdown()?;

    Ok(())
}

Re-exports

pub use nakamoto_chain as chain;
pub use nakamoto_client as client;
pub use nakamoto_common as common;
pub use nakamoto_p2p as p2p;

Modules