Skip to content

Commit a5d076f

Browse files
committed
chore(core)!: s/tx_graph::Update/TxUpdate/
We shouldn't refer to `tx_graph` in `bdk_core`. TxGraph happens to consume `Update` but it doesn't conceptually own the definition of an update to transactions. I tried to also remove a lot of references to "graph" where they shouldn't be. "graph" is a very general kind of data structure so we should avoid referring to it where it's not relevant. `tx_update` is much better than `graph_update`.
1 parent dafb9aa commit a5d076f

File tree

20 files changed

+195
-196
lines changed

20 files changed

+195
-196
lines changed

crates/chain/src/indexed_tx_graph.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ where
9090

9191
/// Apply an `update` directly.
9292
///
93-
/// `update` is a [`tx_graph::Update<A>`] and the resultant changes is returned as [`ChangeSet`].
93+
/// `update` is a [`tx_graph::TxUpdate<A>`] and the resultant changes is returned as [`ChangeSet`].
9494
#[cfg(feature = "std")]
9595
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
96-
pub fn apply_update(&mut self, update: tx_graph::Update<A>) -> ChangeSet<A, I::ChangeSet> {
96+
pub fn apply_update(&mut self, update: tx_graph::TxUpdate<A>) -> ChangeSet<A, I::ChangeSet> {
9797
let tx_graph = self.graph.apply_update(update);
9898
let indexer = self.index_tx_graph_changeset(&tx_graph);
9999
ChangeSet { tx_graph, indexer }
@@ -114,7 +114,7 @@ where
114114
/// set to the current time.
115115
pub fn apply_update_at(
116116
&mut self,
117-
update: tx_graph::Update<A>,
117+
update: tx_graph::TxUpdate<A>,
118118
seen_at: Option<u64>,
119119
) -> ChangeSet<A, I::ChangeSet> {
120120
let tx_graph = self.graph.apply_update_at(update, seen_at);

crates/chain/src/tx_graph.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
//! # let tx_b = tx_from_hex(RAW_TX_2);
7979
//! let mut graph: TxGraph = TxGraph::default();
8080
//!
81-
//! let mut update = tx_graph::Update::default();
81+
//! let mut update = tx_graph::TxUpdate::default();
8282
//! update.txs.push(Arc::new(tx_a));
8383
//! update.txs.push(Arc::new(tx_b));
8484
//!
@@ -98,15 +98,15 @@ use crate::{Anchor, Balance, ChainOracle, ChainPosition, FullTxOut, Merge};
9898
use alloc::collections::vec_deque::VecDeque;
9999
use alloc::sync::Arc;
100100
use alloc::vec::Vec;
101-
pub use bdk_core::tx_graph::Update;
101+
pub use bdk_core::TxUpdate;
102102
use bitcoin::{Amount, OutPoint, ScriptBuf, SignedAmount, Transaction, TxOut, Txid};
103103
use core::fmt::{self, Formatter};
104104
use core::{
105105
convert::Infallible,
106106
ops::{Deref, RangeInclusive},
107107
};
108108

109-
impl<A> From<TxGraph<A>> for Update<A> {
109+
impl<A> From<TxGraph<A>> for TxUpdate<A> {
110110
fn from(graph: TxGraph<A>) -> Self {
111111
Self {
112112
txs: graph.full_txs().map(|tx_node| tx_node.tx).collect(),
@@ -120,8 +120,8 @@ impl<A> From<TxGraph<A>> for Update<A> {
120120
}
121121
}
122122

123-
impl<A: Ord + Clone> From<Update<A>> for TxGraph<A> {
124-
fn from(update: Update<A>) -> Self {
123+
impl<A: Ord + Clone> From<TxUpdate<A>> for TxGraph<A> {
124+
fn from(update: TxUpdate<A>) -> Self {
125125
let mut graph = TxGraph::<A>::default();
126126
let _ = graph.apply_update_at(update, None);
127127
graph
@@ -655,7 +655,7 @@ impl<A: Clone + Ord> TxGraph<A> {
655655
/// exist in `update` but not in `self`).
656656
#[cfg(feature = "std")]
657657
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
658-
pub fn apply_update(&mut self, update: Update<A>) -> ChangeSet<A> {
658+
pub fn apply_update(&mut self, update: TxUpdate<A>) -> ChangeSet<A> {
659659
use std::time::*;
660660
let now = SystemTime::now()
661661
.duration_since(UNIX_EPOCH)
@@ -676,7 +676,7 @@ impl<A: Clone + Ord> TxGraph<A> {
676676
///
677677
/// Use [`apply_update`](TxGraph::apply_update) to have the `seen_at` value automatically set
678678
/// to the current time.
679-
pub fn apply_update_at(&mut self, update: Update<A>, seen_at: Option<u64>) -> ChangeSet<A> {
679+
pub fn apply_update_at(&mut self, update: TxUpdate<A>, seen_at: Option<u64>) -> ChangeSet<A> {
680680
let mut changeset = ChangeSet::<A>::default();
681681
let mut unanchored_txs = HashSet::<Txid>::new();
682682
for tx in update.txs {

crates/chain/tests/test_tx_graph.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn insert_txouts() {
8888

8989
// Make the update graph
9090
let update = {
91-
let mut update = tx_graph::Update::default();
91+
let mut update = tx_graph::TxUpdate::default();
9292
for (outpoint, txout) in &update_ops {
9393
// Insert partials transactions.
9494
update.txouts.insert(*outpoint, txout.clone());
@@ -1137,12 +1137,12 @@ fn call_map_anchors_with_non_deterministic_anchor() {
11371137
);
11381138
}
11391139

1140-
/// Tests `From` impls for conversion between [`TxGraph`] and [`tx_graph::Update`].
1140+
/// Tests `From` impls for conversion between [`TxGraph`] and [`tx_graph::TxUpdate`].
11411141
#[test]
11421142
fn tx_graph_update_conversion() {
1143-
use tx_graph::Update;
1143+
use tx_graph::TxUpdate;
11441144

1145-
type TestCase = (&'static str, Update<ConfirmationBlockTime>);
1145+
type TestCase = (&'static str, TxUpdate<ConfirmationBlockTime>);
11461146

11471147
fn make_tx(v: i32) -> Transaction {
11481148
Transaction {
@@ -1161,24 +1161,24 @@ fn tx_graph_update_conversion() {
11611161
}
11621162

11631163
let test_cases: &[TestCase] = &[
1164-
("empty_update", Update::default()),
1164+
("empty_update", TxUpdate::default()),
11651165
(
11661166
"single_tx",
1167-
Update {
1167+
TxUpdate {
11681168
txs: vec![make_tx(0).into()],
11691169
..Default::default()
11701170
},
11711171
),
11721172
(
11731173
"two_txs",
1174-
Update {
1174+
TxUpdate {
11751175
txs: vec![make_tx(0).into(), make_tx(1).into()],
11761176
..Default::default()
11771177
},
11781178
),
11791179
(
11801180
"with_floating_txouts",
1181-
Update {
1181+
TxUpdate {
11821182
txs: vec![make_tx(0).into(), make_tx(1).into()],
11831183
txouts: [
11841184
(OutPoint::new(h!("a"), 0), make_txout(0)),
@@ -1191,7 +1191,7 @@ fn tx_graph_update_conversion() {
11911191
),
11921192
(
11931193
"with_anchors",
1194-
Update {
1194+
TxUpdate {
11951195
txs: vec![make_tx(0).into(), make_tx(1).into()],
11961196
txouts: [
11971197
(OutPoint::new(h!("a"), 0), make_txout(0)),
@@ -1209,7 +1209,7 @@ fn tx_graph_update_conversion() {
12091209
),
12101210
(
12111211
"with_seen_ats",
1212-
Update {
1212+
TxUpdate {
12131213
txs: vec![make_tx(0).into(), make_tx(1).into()],
12141214
txouts: [
12151215
(OutPoint::new(h!("a"), 0), make_txout(0)),
@@ -1230,7 +1230,7 @@ fn tx_graph_update_conversion() {
12301230
for (test_name, update) in test_cases {
12311231
let mut tx_graph = TxGraph::<ConfirmationBlockTime>::default();
12321232
let _ = tx_graph.apply_update_at(update.clone(), None);
1233-
let update_from_tx_graph: Update<ConfirmationBlockTime> = tx_graph.into();
1233+
let update_from_tx_graph: TxUpdate<ConfirmationBlockTime> = tx_graph.into();
12341234

12351235
assert_eq!(
12361236
update
File renamed without changes.

crates/core/src/lib.rs

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -59,55 +59,13 @@ pub type Indexed<T> = (u32, T);
5959
/// A tuple of keychain `K`, derivation index (`u32`) and a `T` associated with them.
6060
pub type KeychainIndexed<K, T> = ((K, u32), T);
6161

62-
mod chain_data;
63-
pub use chain_data::*;
62+
mod block_id;
63+
pub use block_id::*;
6464

6565
mod checkpoint;
6666
pub use checkpoint::*;
6767

68-
pub mod spk_client;
69-
70-
/// Core structures for [`TxGraph`].
71-
///
72-
/// [`TxGraph`]: https://docs.rs/bdk_chain/latest/bdk_chain/tx_graph/struct.TxGraph.html
73-
pub mod tx_graph {
74-
use crate::collections::{BTreeMap, BTreeSet, HashMap};
75-
use alloc::{sync::Arc, vec::Vec};
76-
use bitcoin::{OutPoint, Transaction, TxOut, Txid};
77-
78-
/// Data object used to update a [`TxGraph`].
79-
///
80-
/// [`TxGraph`]: https://docs.rs/bdk_chain/latest/bdk_chain/tx_graph/struct.TxGraph.html
81-
#[derive(Debug, Clone)]
82-
pub struct Update<A = ()> {
83-
/// Full transactions.
84-
pub txs: Vec<Arc<Transaction>>,
85-
/// Floating txouts.
86-
pub txouts: BTreeMap<OutPoint, TxOut>,
87-
/// Transaction anchors.
88-
pub anchors: BTreeSet<(A, Txid)>,
89-
/// Seen at times for transactions.
90-
pub seen_ats: HashMap<Txid, u64>,
91-
}
68+
mod tx_update;
69+
pub use tx_update::*;
9270

93-
impl<A> Default for Update<A> {
94-
fn default() -> Self {
95-
Self {
96-
txs: Default::default(),
97-
txouts: Default::default(),
98-
anchors: Default::default(),
99-
seen_ats: Default::default(),
100-
}
101-
}
102-
}
103-
104-
impl<A: Ord> Update<A> {
105-
/// Extend this update with `other`.
106-
pub fn extend(&mut self, other: Update<A>) {
107-
self.txs.extend(other.txs);
108-
self.txouts.extend(other.txouts);
109-
self.anchors.extend(other.anchors);
110-
self.seen_ats.extend(other.seen_ats);
111-
}
112-
}
113-
}
71+
pub mod spk_client;

crates/core/src/spk_client.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,16 @@ impl<I> SyncRequest<I> {
323323
#[must_use]
324324
#[derive(Debug)]
325325
pub struct SyncResult<A = ConfirmationBlockTime> {
326-
/// The update to apply to the receiving
327-
/// [`TxGraph`](../../bdk_chain/tx_graph/struct.TxGraph.html).
328-
pub graph_update: crate::tx_graph::Update<A>,
329-
/// The update to apply to the receiving
330-
/// [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html).
326+
/// Relevant transaction data discovered during the scan.
327+
pub tx_update: crate::TxUpdate<A>,
328+
/// Changes to the chain discovered during the scan.
331329
pub chain_update: Option<CheckPoint>,
332330
}
333331

334332
impl<A> Default for SyncResult<A> {
335333
fn default() -> Self {
336334
Self {
337-
graph_update: Default::default(),
335+
tx_update: Default::default(),
338336
chain_update: Default::default(),
339337
}
340338
}
@@ -462,19 +460,19 @@ impl<K: Ord + Clone> FullScanRequest<K> {
462460
#[must_use]
463461
#[derive(Debug)]
464462
pub struct FullScanResult<K, A = ConfirmationBlockTime> {
465-
/// The update to apply to the receiving
466-
/// [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html).
467-
pub graph_update: crate::tx_graph::Update<A>,
468-
/// The update to apply to the receiving [`TxGraph`](../../bdk_chain/tx_graph/struct.TxGraph.html).
469-
pub chain_update: Option<CheckPoint>,
470-
/// Last active indices for the corresponding keychains (`K`).
463+
/// Relevant transaction data discovered during the scan.
464+
pub tx_update: crate::TxUpdate<A>,
465+
/// Last active indices for the corresponding keychains (`K`). An index is active if it had a
466+
/// transaction associated with the script pubkey at that index.
471467
pub last_active_indices: BTreeMap<K, u32>,
468+
/// Changes to the chain discovered during the scan.
469+
pub chain_update: Option<CheckPoint>,
472470
}
473471

474472
impl<K, A> Default for FullScanResult<K, A> {
475473
fn default() -> Self {
476474
Self {
477-
graph_update: Default::default(),
475+
tx_update: Default::default(),
478476
chain_update: Default::default(),
479477
last_active_indices: Default::default(),
480478
}

crates/core/src/tx_update.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::collections::{BTreeMap, BTreeSet, HashMap};
2+
use alloc::{sync::Arc, vec::Vec};
3+
use bitcoin::{OutPoint, Transaction, TxOut, Txid};
4+
5+
/// Data object used to communicate updates about relevant transactions from some chain data soruce
6+
/// to the core model (usually a `bdk_chain::TxGraph`).
7+
#[derive(Debug, Clone)]
8+
pub struct TxUpdate<A = ()> {
9+
/// Full transactions. These are transactions that were determined to be relevant to the wallet
10+
/// given the request.
11+
pub txs: Vec<Arc<Transaction>>,
12+
/// Floating txouts. These are `TxOut`s that exist but the whole transaction wasn't included in
13+
/// `txs` since only knowing about the output is important. These are often used to help determine
14+
/// the fee of a wallet transaction.
15+
pub txouts: BTreeMap<OutPoint, TxOut>,
16+
/// Transaction anchors. Anchors tells us a position in the chain where a transaction was
17+
/// confirmed.
18+
pub anchors: BTreeSet<(A, Txid)>,
19+
/// Seen at times for transactions. This records when a transaction was most recently seen in
20+
/// the user's mempool for the sake of tie-breaking other conflicting transactions.
21+
pub seen_ats: HashMap<Txid, u64>,
22+
}
23+
24+
impl<A> Default for TxUpdate<A> {
25+
fn default() -> Self {
26+
Self {
27+
txs: Default::default(),
28+
txouts: Default::default(),
29+
anchors: Default::default(),
30+
seen_ats: Default::default(),
31+
}
32+
}
33+
}
34+
35+
impl<A: Ord> TxUpdate<A> {
36+
/// Extend this update with `other`.
37+
pub fn extend(&mut self, other: TxUpdate<A>) {
38+
self.txs.extend(other.txs);
39+
self.txouts.extend(other.txouts);
40+
self.anchors.extend(other.anchors);
41+
self.seen_ats.extend(other.seen_ats);
42+
}
43+
}

0 commit comments

Comments
 (0)