refactor!(chain): Unify ChangeSet nomenclature

This commit renames:

- indexed_tx_graph::IndexedAdditions -> indexed_tx_graph::ChangeSet
- indexed_tx_graph::IndexedAdditions::graph_additions -> indexed_tx_graph::ChangeSet::graph
- indexed_tx_graph::IndexedAdditions::index_additions -> indexed_tx_graph::ChangeSet::indexer
- tx_graph::Additions -> tx_graph::ChangeSet
- keychain::DerivationAdditions -> keychain::ChangeSet
- CanonicalTx::node -> CanonicalTx::tx_node
- CanonicalTx::observed_as -> CanonicalTx::chain_position
- LocalChangeSet -> WalletChangeSet
- LocalChangeSet::chain_changeset -> WalletChangeSet::chain
- LocalChangeSet::indexed_additions -> WalletChangeSet::indexed_tx_graph
- LocalUpdate -> WalletUpdate

This commit also changes the visibility of TxGraph::determine_changeset
to be pub(crate), and the method accepts a TxGraph instead of &TxGraph

This commit removes:
- `TxGraph::insert_txout_preview`
- `TxGraph::insert_tx_preview`
- `insert_anchor_preview`
- `insert_seen_at_preview`

Solves #1022
This commit is contained in:
Daniela Brozzoni
2023-08-07 17:43:17 +02:00
parent 0cd2348166
commit ea50b6a932
16 changed files with 329 additions and 379 deletions

View File

@@ -10,8 +10,8 @@ use bdk_chain::{
absolute, address, psbt::Prevouts, secp256k1::Secp256k1, sighash::SighashCache, Address,
Network, Sequence, Transaction, TxIn, TxOut,
},
indexed_tx_graph::{IndexedAdditions, IndexedTxGraph},
keychain::{DerivationAdditions, KeychainTxOutIndex},
indexed_tx_graph::{self, IndexedTxGraph},
keychain::{self, KeychainTxOutIndex},
miniscript::{
descriptor::{DescriptorSecretKey, KeyMap},
Descriptor, DescriptorPublicKey,
@@ -24,7 +24,7 @@ pub use clap;
use clap::{Parser, Subcommand};
pub type KeychainTxGraph<A> = IndexedTxGraph<A, KeychainTxOutIndex<Keychain>>;
pub type KeychainAdditions<A> = IndexedAdditions<A, DerivationAdditions<Keychain>>;
pub type KeychainChangeSet<A> = indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<Keychain>>;
pub type Database<'m, C> = Persist<Store<'m, C>, C>;
#[derive(Parser)]
@@ -186,7 +186,7 @@ pub fn run_address_cmd<A, C>(
cmd: AddressCmd,
) -> anyhow::Result<()>
where
C: Default + Append + DeserializeOwned + Serialize + From<KeychainAdditions<A>>,
C: Default + Append + DeserializeOwned + Serialize + From<KeychainChangeSet<A>>,
{
let index = &mut graph.index;
@@ -198,9 +198,9 @@ where
_ => unreachable!("only these two variants exist in match arm"),
};
let ((spk_i, spk), index_additions) = spk_chooser(index, &Keychain::External);
let ((spk_i, spk), index_changeset) = spk_chooser(index, &Keychain::External);
let db = &mut *db.lock().unwrap();
db.stage(C::from(KeychainAdditions::from(index_additions)));
db.stage(C::from(KeychainChangeSet::from(index_changeset)));
db.commit()?;
let addr = Address::from_script(spk, network).context("failed to derive address")?;
println!("[address @ {}] {}", spk_i, addr);
@@ -340,20 +340,20 @@ pub fn run_send_cmd<A: Anchor, O: ChainOracle, C>(
) -> anyhow::Result<()>
where
O::Error: std::error::Error + Send + Sync + 'static,
C: Default + Append + DeserializeOwned + Serialize + From<KeychainAdditions<A>>,
C: Default + Append + DeserializeOwned + Serialize + From<KeychainChangeSet<A>>,
{
let (transaction, change_index) = {
let graph = &mut *graph.lock().unwrap();
// take mutable ref to construct tx -- it is only open for a short time while building it.
let (tx, change_info) = create_tx(graph, chain, keymap, cs_algorithm, address, value)?;
if let Some((index_additions, (change_keychain, index))) = change_info {
if let Some((index_changeset, (change_keychain, index))) = change_info {
// We must first persist to disk the fact that we've got a new address from the
// change keychain so future scans will find the tx we're about to broadcast.
// If we're unable to persist this, then we don't want to broadcast.
{
let db = &mut *db.lock().unwrap();
db.stage(C::from(KeychainAdditions::from(index_additions)));
db.stage(C::from(KeychainChangeSet::from(index_changeset)));
db.commit()?;
}
@@ -371,12 +371,12 @@ where
Ok(_) => {
println!("Broadcasted Tx : {}", transaction.txid());
let keychain_additions = graph.lock().unwrap().insert_tx(&transaction, None, None);
let keychain_changeset = graph.lock().unwrap().insert_tx(&transaction, None, None);
// We know the tx is at least unconfirmed now. Note if persisting here fails,
// it's not a big deal since we can always find it again form
// blockchain.
db.lock().unwrap().stage(C::from(keychain_additions));
db.lock().unwrap().stage(C::from(keychain_changeset));
Ok(())
}
Err(e) => {
@@ -399,12 +399,12 @@ pub fn create_tx<A: Anchor, O: ChainOracle>(
value: u64,
) -> anyhow::Result<(
Transaction,
Option<(DerivationAdditions<Keychain>, (Keychain, u32))>,
Option<(keychain::ChangeSet<Keychain>, (Keychain, u32))>,
)>
where
O::Error: std::error::Error + Send + Sync + 'static,
{
let mut additions = DerivationAdditions::default();
let mut changeset = keychain::ChangeSet::default();
let assets = bdk_tmp_plan::Assets {
keys: keymap.iter().map(|(pk, _)| pk.clone()).collect(),
@@ -452,9 +452,9 @@ where
Keychain::External
};
let ((change_index, change_script), change_additions) =
let ((change_index, change_script), change_changeset) =
graph.index.next_unused_spk(&internal_keychain);
additions.append(change_additions);
changeset.append(change_changeset);
// Clone to drop the immutable reference.
let change_script = change_script.into();
@@ -594,7 +594,7 @@ where
}
let change_info = if selection_meta.drain_value.is_some() {
Some((additions, (internal_keychain, change_index)))
Some((changeset, (internal_keychain, change_index)))
} else {
None
};
@@ -645,7 +645,7 @@ pub fn handle_commands<S: clap::Subcommand, A: Anchor, O: ChainOracle, C>(
) -> anyhow::Result<()>
where
O::Error: std::error::Error + Send + Sync + 'static,
C: Default + Append + DeserializeOwned + Serialize + From<KeychainAdditions<A>>,
C: Default + Append + DeserializeOwned + Serialize + From<KeychainChangeSet<A>>,
{
match cmd {
Commands::ChainSpecific(_) => unreachable!("example code should handle this!"),

View File

@@ -6,8 +6,8 @@ use std::{
use bdk_chain::{
bitcoin::{Address, Network, OutPoint, ScriptBuf, Txid},
indexed_tx_graph::{IndexedAdditions, IndexedTxGraph},
keychain::LocalChangeSet,
indexed_tx_graph::{self, IndexedTxGraph},
keychain::WalletChangeSet,
local_chain::LocalChain,
Append, ConfirmationHeightAnchor,
};
@@ -60,7 +60,7 @@ pub struct ScanOptions {
pub batch_size: usize,
}
type ChangeSet = LocalChangeSet<Keychain, ConfirmationHeightAnchor>;
type ChangeSet = WalletChangeSet<Keychain, ConfirmationHeightAnchor>;
fn main() -> anyhow::Result<()> {
let (args, keymap, index, db, init_changeset) =
@@ -68,11 +68,11 @@ fn main() -> anyhow::Result<()> {
let graph = Mutex::new({
let mut graph = IndexedTxGraph::new(index);
graph.apply_additions(init_changeset.indexed_additions);
graph.apply_changeset(init_changeset.index_tx_graph);
graph
});
let chain = Mutex::new(LocalChain::from_changeset(init_changeset.chain_changeset));
let chain = Mutex::new(LocalChain::from_changeset(init_changeset.chain));
let electrum_url = match args.network {
Network::Bitcoin => "ssl://electrum.blockstream.info:50002",
@@ -234,8 +234,8 @@ fn main() -> anyhow::Result<()> {
let unconfirmed_txids = graph
.graph()
.list_chain_txs(&*chain, chain_tip)
.filter(|canonical_tx| !canonical_tx.observed_as.is_confirmed())
.map(|canonical_tx| canonical_tx.node.txid)
.filter(|canonical_tx| !canonical_tx.chain_position.is_confirmed())
.map(|canonical_tx| canonical_tx.tx_node.txid)
.collect::<Vec<Txid>>();
txids = Box::new(unconfirmed_txids.into_iter().inspect(|txid| {
@@ -275,24 +275,25 @@ fn main() -> anyhow::Result<()> {
let mut chain = chain.lock().unwrap();
let mut graph = graph.lock().unwrap();
let chain_changeset = chain.apply_update(final_update.chain)?;
let chain = chain.apply_update(final_update.chain)?;
let indexed_additions = {
let mut additions = IndexedAdditions::<ConfirmationHeightAnchor, _>::default();
let (_, index_additions) = graph
let index_tx_graph = {
let mut changeset =
indexed_tx_graph::ChangeSet::<ConfirmationHeightAnchor, _>::default();
let (_, indexer) = graph
.index
.reveal_to_target_multi(&final_update.last_active_indices);
additions.append(IndexedAdditions {
index_additions,
changeset.append(indexed_tx_graph::ChangeSet {
indexer,
..Default::default()
});
additions.append(graph.apply_update(final_update.graph));
additions
changeset.append(graph.apply_update(final_update.graph));
changeset
};
ChangeSet {
indexed_additions,
chain_changeset,
index_tx_graph,
chain,
}
};

View File

@@ -7,7 +7,7 @@ use std::{io::Write, str::FromStr};
use bdk::{
bitcoin::{Address, Network},
chain::keychain::LocalUpdate,
chain::keychain::WalletUpdate,
wallet::AddressIndex,
SignOptions, Wallet,
};
@@ -58,10 +58,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
client.update_tx_graph(keychain_spks, None, None, STOP_GAP, PARALLEL_REQUESTS)?;
let missing_heights = wallet.tx_graph().missing_heights(wallet.local_chain());
let chain_update = client.update_local_chain(prev_tip, missing_heights)?;
let update = LocalUpdate {
let update = WalletUpdate {
last_active_indices,
graph: update_graph,
..LocalUpdate::new(chain_update)
..WalletUpdate::new(chain_update)
};
wallet.apply_update(update)?;

View File

@@ -2,7 +2,7 @@ use std::{io::Write, str::FromStr};
use bdk::{
bitcoin::{Address, Network},
chain::keychain::LocalUpdate,
chain::keychain::WalletUpdate,
wallet::AddressIndex,
SignOptions, Wallet,
};
@@ -59,10 +59,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.await?;
let missing_heights = wallet.tx_graph().missing_heights(wallet.local_chain());
let chain_update = client.update_local_chain(prev_tip, missing_heights).await?;
let update = LocalUpdate {
let update = WalletUpdate {
last_active_indices,
graph: update_graph,
..LocalUpdate::new(chain_update)
..WalletUpdate::new(chain_update)
};
wallet.apply_update(update)?;
wallet.commit()?;