Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,37 @@ Utility to run a regtest [electrs](https://github.com/romanz/electrs/) process c
useful in integration testing environment.

```rust
let bitcoind = bitcoind::BitcoinD::new("/usr/local/bin/bitcoind").unwrap();
let electrsd = electrsd::ElectrsD::new("/usr/local/bin/electrs", bitcoind).unwrap();
let header = electrsd.client.block_headers_subscribe().unwrap();
assert_eq!(header.height, 0);
let bitcoind_path = "/usr/local/bin/bitcoind";
if std::path::Path::new(&bitcoind_path).exists() {
use crate::electrsd::electrum_client::ElectrumApi;
let mut bitcoind_conf = electrsd::corepc_node::Conf::default();
bitcoind_conf.p2p = electrsd::corepc_node::P2P::Yes;
let bitcoind = corepc_node::Node::with_conf(bitcoind_path, &bitcoind_conf).unwrap();
let electrsd = electrsd::ElectrsD::new("/usr/local/bin/electrs", &bitcoind).unwrap();
let header = electrsd.client.block_headers_subscribe_raw().unwrap();
assert_eq!(header.height, 0);
}
```

## Automatic binaries download

In your project Cargo.toml, activate the following features

```yml
electrsd = { version= "0.23", features = ["corepc-node_23_1", "electrs_0_9_1"] }
electrsd = { version= "0.36", features = ["corepc-node_23_1", "electrs_0_9_1"] }
```

Then use it:

```rust
let bitcoind_exe = bitcoind::downloaded_exe_path().expect("bitcoind version feature must be enabled");
let bitcoind = bitcoind::BitcoinD::new(bitcoind_exe).unwrap();
let electrs_exe = electrsd::downloaded_exe_path().expect("electrs version feature must be enabled");
let electrsd = electrsd::ElectrsD::new(electrs_exe, bitcoind).unwrap();
if let Ok(bitcoind_exe) = corepc_node::downloaded_exe_path() {
let mut bitcoind_conf = electrsd::corepc_node::Conf::default();
bitcoind_conf.p2p = electrsd::corepc_node::P2P::Yes;
let bitcoind = corepc_node::Node::with_conf(bitcoind_exe, &bitcoind_conf).unwrap();
if let Some(electrs_exe) = electrsd::downloaded_exe_path() {
let electrsd = electrsd::ElectrsD::new(electrs_exe, &bitcoind).unwrap();
}
}
```

When the `ELECTRSD_DOWNLOAD_ENDPOINT`/`BITCOIND_DOWNLOAD_ENDPOINT` environment variables are set,
Expand All @@ -39,8 +49,13 @@ When you don't use the auto-download feature you have the following options:
- provide the `electrs` executable via the `ELECTRS_EXEC` env var

```rust
if let Ok(exe_path) = electrsd::exe_path() {
let electrsd = electrsd::electrsD::new(exe_path).unwrap();
if let Ok(bitcoind_path) = corepc_node::exe_path() {
let mut bitcoind_conf = electrsd::corepc_node::Conf::default();
bitcoind_conf.p2p = electrsd::corepc_node::P2P::Yes;
let bitcoind = corepc_node::Node::with_conf(bitcoind_path, &bitcoind_conf).unwrap();
if let Ok(exe_path) = electrsd::exe_path() {
let electrsd = electrsd::ElectrsD::new(exe_path, &bitcoind).unwrap();
}
}
```

Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,13 @@ pub fn exe_path() -> anyhow::Result<String> {
.map(|p| p.display().to_string())
}

// Test the code in the readme
#[doc = include_str!("../README.md")]
#[allow(dead_code)]
fn readme() {
// dummy function
}

#[cfg(test)]
mod test {
use crate::exe_path;
Expand Down
Loading