Skip to content

Commit

Permalink
Merge #1495: example: Update example_cli and retire old nursery crates
Browse files Browse the repository at this point in the history
771f6b9 example: Update example_cli (valued mammal)

Pull request description:

  - Adds two commands `init` and `generate`. Loading database doesn't require descriptors
  - Replaces `send` command with `psbt` (new, sign, and extract). Supports tap key spend
  - Uses `bdk-coin-select` and miniscript `plan` module

  fixes #1469
  partially addresses #1462

  ### Notes to the reviewers

  Note the `example_cli` lib now defines the `ChangeSet` and `Anchor` type whereas before these were generic

  ### Changelog notice

  * Improvements to `example_cli` that include generating descriptors and creating PSBTs

  ### Checklists

  #### All Submissions:

  * [x] I've signed all my commits
  * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md)
  * [x] I ran `cargo fmt` and `cargo clippy` before committing

ACKs for top commit:
  evanlinjin:
    ACK 771f6b9

Tree-SHA512: 93b9e38ec428c3ed5920e7632381dcf50e28a01ceb8e1438c87aa459563600cd79636bcf1179eefa763adce98239a60ad2894dfb2ba0f2700cc64e57ede75010
  • Loading branch information
evanlinjin committed Aug 12, 2024
2 parents 4545458 + 771f6b9 commit 98c4959
Show file tree
Hide file tree
Showing 16 changed files with 688 additions and 2,943 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
89 changes: 35 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,12 @@ 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 {
local_chain: chain_changeset,
tx_graph: graph_changeset.tx_graph,
indexer: graph_changeset.indexer,
..Default::default()
});

// commit staged db changes in intervals
if last_db_commit.elapsed() >= DB_COMMIT_DELAY {
Expand Down Expand Up @@ -220,7 +191,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 +206,11 @@ fn main() -> anyhow::Result<()> {
);
{
let db = &mut *db.lock().unwrap();
db_stage.merge((local_chain::ChangeSet::default(), graph_changeset));
db_stage.merge(ChangeSet {
tx_graph: graph_changeset.tx_graph,
indexer: graph_changeset.indexer,
..Default::default()
});
if let Some(changeset) = db_stage.take() {
db.append_changeset(&changeset)?;
}
Expand Down Expand Up @@ -300,7 +275,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 +296,13 @@ fn main() -> anyhow::Result<()> {
continue;
}
};
db_stage.merge(changeset);

db_stage.merge(ChangeSet {
local_chain: chain_changeset,
tx_graph: graph_changeset.tx_graph,
indexer: graph_changeset.indexer,
..Default::default()
});

if last_db_commit.elapsed() >= DB_COMMIT_DELAY {
let db = &mut *db.lock().unwrap();
Expand All @@ -348,7 +329,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 98c4959

Please sign in to comment.