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 15, 2024
1 parent d99b3ef commit 6748db3
Show file tree
Hide file tree
Showing 16 changed files with 620 additions and 2,932 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,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
86 changes: 34 additions & 52 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,11 +12,8 @@ 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, CombinedChangeSet, ConfirmationBlockTime, Merge,
};
use example_cli::{
anyhow,
Expand All @@ -36,10 +33,8 @@ 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<Keychain>>,
);
/// ChangeSet
type ChangeSet = CombinedChangeSet<Keychain, ConfirmationBlockTime>;

#[derive(Debug)]
enum Emission {
Expand Down Expand Up @@ -111,52 +106,28 @@ 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, ConfirmationBlockTime>(
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 +162,11 @@ 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(ChangeSet {
chain: chain_changeset,
indexed_tx_graph: graph_changeset,
..Default::default()
});

// commit staged db changes in intervals
if last_db_commit.elapsed() >= DB_COMMIT_DELAY {
Expand Down Expand Up @@ -220,7 +195,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 +210,10 @@ fn main() -> anyhow::Result<()> {
);
{
let db = &mut *db.lock().unwrap();
db_stage.merge((local_chain::ChangeSet::default(), graph_changeset));
db_stage.merge(ChangeSet {
indexed_tx_graph: graph_changeset,
..Default::default()
});
if let Some(changeset) = db_stage.take() {
db.append_changeset(&changeset)?;
}
Expand Down Expand Up @@ -300,7 +278,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 +299,11 @@ fn main() -> anyhow::Result<()> {
continue;
}
};
db_stage.merge(changeset);
db_stage.merge(ChangeSet {
chain: chain_changeset,
indexed_tx_graph: graph_changeset,
..Default::default()
});

if last_db_commit.elapsed() >= DB_COMMIT_DELAY {
let db = &mut *db.lock().unwrap();
Expand All @@ -348,7 +330,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
7 changes: 3 additions & 4 deletions example-crates/example_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ edition = "2021"

[dependencies]
bdk_chain = { path = "../../crates/chain", features = ["serde", "miniscript"]}
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 6748db3

Please sign in to comment.