Skip to main content

BitcoinD

Struct BitcoinD 

Source
pub struct BitcoinD { /* private fields */ }
Expand description

A running bitcoind regtest node.

The node is started in BitcoinD::from_bin (or one of its siblings) and stopped — and its temporary files removed — when this value is dropped.

§Wallet

A wallet named "wallet" is created (or loaded) automatically on startup. All RPC helpers that require a wallet (generate, new_address, …) use this wallet.

§Networking

Both the RPC and P2P ports are chosen from the OS’s ephemeral range at startup. Use rpc_socket and get_p2p_socket to discover them after construction.

Implementations§

Source§

impl BitcoinD

Source

pub fn new() -> Result<BitcoinD, Error>

Start a BitcoinD node using the binary located by get_bitcoind_path, with the default BitcoinDConf.

If the binary is not cached under target/bin/, it will fetch one from bitcoincore.org per build.rs.

Source

pub fn new_with_conf(conf: &BitcoinDConf<'_>) -> Result<BitcoinD, Error>

Start a BitcoinD node using the binary located by get_bitcoind_path, with a custom BitcoinDConf.

If the binary is not cached under target/bin/, it will fetch one from bitcoincore.org per build.rs.

Source

pub fn from_bin<P: AsRef<Path>>(bitcoind_bin: P) -> Result<BitcoinD, Error>

Create a BitcoinD instance running the binary at Path with the default BitcoinDConf.

Source

pub fn from_bin_with_conf<P: AsRef<Path>>( bitcoind_bin: P, conf: &BitcoinDConf<'_>, ) -> Result<BitcoinD, Error>

Create a BitcoinD instance running the binary at Path with a custom BitcoinDConf. The method retries up to BitcoinDConf::max_retries times. On each attempt it:

  1. Picks fresh ephemeral RPC and P2P ports.
  2. Spawns bitcoind with those ports and a fresh data directory.
  3. Waits for the cookie file to appear (up to 5 s).
  4. Creates or loads the default wallet and builds an RPC client.
  5. Waits for the node to become responsive (up to 5 s).

Returns an error if all attempts are exhausted.

Source

pub fn stop(&mut self) -> Result<ExitStatus, Error>

Send stop via RPC and wait for the process to exit.

Calling this method is not required in normal usage because Drop kills the process automatically. It is provided for cases where you need the exit status or want to ensure the node has fully shut down before proceeding.

Source

pub fn get_pid(&self) -> u32

Get BitcoinD’s PID process.

Source

pub fn get_working_directory(&self) -> PathBuf

Get BitcoinD’s data directory.

Source

pub fn get_p2p_socket(&self) -> SocketAddr

Get BitcoinD’s P2P SocketAddr.

Pass this to BitcoinD::add_peer on another node to connect the two.

Source

pub fn get_rpc_client(&self) -> &Client

Get a reference to BitcoinD’s RPC Client.

Source

pub fn rpc_socket(&self) -> SocketAddr

Get BitcoinD’s JSON-RPC SocketAddr.

Source

pub fn cookie_file(&self) -> &Path

Get the Path to BitcoinD’s cookie file.

Source

pub fn get_height(&self) -> Result<u32, Error>

Get the current chain height.

Source

pub fn add_peer(&self, socket: SocketAddr) -> Result<(), Error>

Connect this BitcoinD to another BitcoinD at socket and wait until the connection is established (up to 5 seconds with exponential back-off).

Returns an error if the peer does not appear in getpeerinfo within the timeout.

Source

pub fn get_peer_count(&self) -> Result<u32, Error>

Get BitcoinD’s peer count.

Source

pub fn generate(&self, count: u32) -> Result<Vec<String>, Error>

Generate count blocks.

Returns a the block hashes as a Vec<String>.

Trait Implementations§

Source§

impl Debug for BitcoinD

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for BitcoinD

Source§

fn drop(&mut self)

Gracefully stops the node (if it was started with a persistent directory) and kills the process.

Errors from stop and kill are silently discarded so that Drop never panics.

Source§

impl Node for BitcoinD

Source§

fn get_name() -> &'static str

A human-readable name for the Node.
Source§

fn get_height(&self) -> Result<u32, Error>

Get the Node’s current chain height.
Source§

fn poll_interval() -> Duration

How long to sleep between get_height RPC calls. Read more
Source§

fn wait_timeout() -> Duration

How long wait_for_height will poll before giving up. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.