Skip to content

Commit

Permalink
example: Update example_cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal committed Jul 24, 2024
1 parent 17a9850 commit 083f709
Show file tree
Hide file tree
Showing 16 changed files with 632 additions and 2,942 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ members = [
"example-crates/wallet_esplora_blocking",
"example-crates/wallet_esplora_async",
"example-crates/wallet_rpc",
"nursery/tmp_plan",
"nursery/coin_select"
]

[workspace.package]
Expand Down
76 changes: 22 additions & 54 deletions example-crates/example_bitcoind_rpc_polling/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{
path::PathBuf,
sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex,
Arc,
},
time::{Duration, Instant},
};
Expand All @@ -12,16 +12,13 @@ use bdk_bitcoind_rpc::{
Emitter,
};
use bdk_chain::{
bitcoin::{constants::genesis_block, Block, Transaction},
indexed_tx_graph,
indexer::keychain_txout,
local_chain::{self, LocalChain},
ConfirmationBlockTime, IndexedTxGraph, Merge,
bitcoin::{Block, Transaction},
local_chain, Merge,
};
use example_cli::{
anyhow,
clap::{self, Args, Subcommand},
Keychain,
ChangeSet, Keychain,
};

const DB_MAGIC: &[u8] = b"bdk_example_rpc";
Expand All @@ -36,11 +33,6 @@ const MEMPOOL_EMIT_DELAY: Duration = Duration::from_secs(30);
/// Delay for committing to persistence.
const DB_COMMIT_DELAY: Duration = Duration::from_secs(60);

type ChangeSet = (
local_chain::ChangeSet,
indexed_tx_graph::ChangeSet<ConfirmationBlockTime, keychain_txout::ChangeSet>,
);

#[derive(Debug)]
enum Emission {
Block(bdk_bitcoind_rpc::BlockEvent<Block>),
Expand Down Expand Up @@ -111,52 +103,26 @@ enum RpcCommands {

fn main() -> anyhow::Result<()> {
let start = Instant::now();

let example_cli::Init {
args,
keymap,
index,
graph,
chain,
db,
init_changeset,
} = example_cli::init::<RpcCommands, RpcArgs, ChangeSet>(DB_MAGIC, DB_PATH)?;
println!(
"[{:>10}s] loaded initial changeset from db",
start.elapsed().as_secs_f32()
);
let (init_chain_changeset, init_graph_changeset) = init_changeset;

let graph = Mutex::new({
let mut graph = IndexedTxGraph::new(index);
graph.apply_changeset(init_graph_changeset);
graph
});
println!(
"[{:>10}s] loaded indexed tx graph from changeset",
start.elapsed().as_secs_f32()
);

let chain = Mutex::new(if init_chain_changeset.is_empty() {
let genesis_hash = genesis_block(args.network).block_hash();
let (chain, chain_changeset) = LocalChain::from_genesis_hash(genesis_hash);
let mut db = db.lock().unwrap();
db.append_changeset(&(chain_changeset, Default::default()))?;
chain
} else {
LocalChain::from_changeset(init_chain_changeset)?
});
println!(
"[{:>10}s] loaded local chain from changeset",
start.elapsed().as_secs_f32()
);
network,
} = match example_cli::init_or_load::<RpcCommands, RpcArgs>(DB_MAGIC, DB_PATH)? {
Some(init) => init,
None => return Ok(()),
};

let rpc_cmd = match args.command {
example_cli::Commands::ChainSpecific(rpc_cmd) => rpc_cmd,
general_cmd => {
return example_cli::handle_commands(
&graph,
&db,
&chain,
&keymap,
args.network,
&db,
network,
|rpc_args, tx| {
let client = rpc_args.new_client()?;
client.send_raw_transaction(tx)?;
Expand Down Expand Up @@ -191,7 +157,8 @@ fn main() -> anyhow::Result<()> {
.apply_update(emission.checkpoint)
.expect("must always apply as we receive blocks in order from emitter");
let graph_changeset = graph.apply_block_relevant(&emission.block, height);
db_stage.merge((chain_changeset, graph_changeset));
db_stage.merge(chain_changeset.into());
db_stage.merge(graph_changeset.into());

// commit staged db changes in intervals
if last_db_commit.elapsed() >= DB_COMMIT_DELAY {
Expand Down Expand Up @@ -220,7 +187,7 @@ fn main() -> anyhow::Result<()> {
)
};
println!(
"[{:>10}s] synced to {} @ {} | total: {} sats",
"[{:>10}s] synced to {} @ {} | total: {}",
start.elapsed().as_secs_f32(),
synced_to.hash(),
synced_to.height(),
Expand All @@ -235,7 +202,7 @@ fn main() -> anyhow::Result<()> {
);
{
let db = &mut *db.lock().unwrap();
db_stage.merge((local_chain::ChangeSet::default(), graph_changeset));
db_stage.merge(graph_changeset.into());
if let Some(changeset) = db_stage.take() {
db.append_changeset(&changeset)?;
}
Expand Down Expand Up @@ -300,7 +267,7 @@ fn main() -> anyhow::Result<()> {
let mut graph = graph.lock().unwrap();
let mut chain = chain.lock().unwrap();

let changeset = match emission {
let (chain_changeset, graph_changeset) = match emission {
Emission::Block(block_emission) => {
let height = block_emission.block_height();
let chain_changeset = chain
Expand All @@ -321,7 +288,8 @@ fn main() -> anyhow::Result<()> {
continue;
}
};
db_stage.merge(changeset);
db_stage.merge(chain_changeset.into());
db_stage.merge(graph_changeset.into());

if last_db_commit.elapsed() >= DB_COMMIT_DELAY {
let db = &mut *db.lock().unwrap();
Expand All @@ -348,7 +316,7 @@ fn main() -> anyhow::Result<()> {
)
};
println!(
"[{:>10}s] synced to {} @ {} / {} | total: {} sats",
"[{:>10}s] synced to {} @ {} / {} | total: {}",
start.elapsed().as_secs_f32(),
synced_to.hash(),
synced_to.height(),
Expand Down
8 changes: 4 additions & 4 deletions example-crates/example_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ edition = "2021"

[dependencies]
bdk_chain = { path = "../../crates/chain", features = ["serde", "miniscript"]}
bdk_wallet = { path = "../../crates/wallet" }
bdk_coin_select = "0.3.0"
bdk_file_store = { path = "../../crates/file_store" }
bdk_tmp_plan = { path = "../../nursery/tmp_plan" }
bdk_coin_select = { path = "../../nursery/coin_select" }

clap = { version = "3.2.23", features = ["derive", "env"] }
anyhow = "1"
clap = { version = "3.2.23", features = ["derive", "env"] }
serde = { version = "1", features = ["derive"] }
serde_json = { version = "^1.0" }
serde_json = "1.0"
Loading

0 comments on commit 083f709

Please sign in to comment.