Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions crates/bitcoind_rpc/tests/test_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ pub fn test_sync_local_chain() -> anyhow::Result<()> {
assert_eq!(
local_chain.apply_update(emission.checkpoint,)?,
if exp_height == exp_hashes.len() - reorged_blocks.len() {
bdk_chain::local_chain::ChangeSet {
blocks: core::iter::once((height, Some(hash)))
.chain((height + 1..exp_hashes.len() as u32).map(|h| (h, None)))
.collect(),
}
bdk_chain::local_chain::ChangeSet::from(
core::iter::once((height, Some(hash)))
.chain((height + 1..exp_hashes.len() as u32).map(|h| (h, None))),
)
} else {
[(height, Some(hash))].into()
},
Expand Down
1 change: 1 addition & 0 deletions crates/chain/src/indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ impl<A, I> AsRef<TxGraph<A>> for IndexedTxGraph<A, I> {
))
)]
#[must_use]
#[non_exhaustive]
pub struct ChangeSet<A, IA> {
/// [`TxGraph`] changeset.
pub tx_graph: tx_graph::ChangeSet<A>,
Expand Down
1 change: 1 addition & 0 deletions crates/chain/src/indexer/keychain_txout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,7 @@ impl<K: core::fmt::Display + core::fmt::Debug> core::error::Error for InsertDesc
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[must_use]
#[non_exhaustive]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be interesting to either through doc comment or inside the book to add about the rational here?

pub struct ChangeSet {
/// Maps each `DescriptorId` to its last revealed derivation index.
pub last_revealed: BTreeMap<DescriptorId, u32>,
Expand Down
1 change: 1 addition & 0 deletions crates/chain/src/local_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ where
/// The [`ChangeSet`] represents changes to [`LocalChain`].
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[non_exhaustive]
pub struct ChangeSet<D = BlockHash> {
/// Changes to the [`LocalChain`] blocks.
///
Expand Down
2 changes: 2 additions & 0 deletions crates/chain/src/tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl<A: Anchor> From<TxUpdate<A>> for TxGraph<A> {
///
/// [module-level documentation]: crate::tx_graph
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct TxGraph<A = ConfirmationBlockTime> {
txs: HashMap<Txid, TxNodeInternal>,
spends: BTreeMap<OutPoint, HashSet<Txid>>,
Expand Down Expand Up @@ -1056,6 +1057,7 @@ impl<A: Anchor> TxGraph<A> {
))
)]
#[must_use]
#[non_exhaustive]
pub struct ChangeSet<A = ()> {
/// Added transactions.
pub txs: BTreeSet<Arc<Transaction>>,
Expand Down
60 changes: 28 additions & 32 deletions crates/chain/tests/test_indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,45 +261,41 @@ fn insert_relevant_txs() {

let txs = [tx_c, tx_b, tx_a];

let changeset = indexed_tx_graph::ChangeSet {
tx_graph: tx_graph::ChangeSet {
txs: txs.iter().cloned().map(Arc::new).collect(),
..Default::default()
},
indexer: keychain_txout::ChangeSet {
last_revealed: [(descriptor.descriptor_id(), 9_u32)].into(),
spk_cache: [(descriptor.descriptor_id(), {
let index_after_spk_1 = 9 /* index of spk_1 */ + 1;
SpkIterator::new_with_range(
&descriptor,
// This will also persist the staged spk cache inclusions from prev call to
// `.insert_descriptor`.
0..index_after_spk_1 + lookahead,
)
.collect()
})]
.into(),
},
};
let mut tx_graph_changeset = tx_graph::ChangeSet::default();
tx_graph_changeset.txs = txs.iter().cloned().map(Arc::new).collect();

let mut indexer_changeset = keychain_txout::ChangeSet::default();
indexer_changeset.last_revealed = [(descriptor.descriptor_id(), 9_u32)].into();
indexer_changeset.spk_cache = [(descriptor.descriptor_id(), {
let index_after_spk_1 = 9 /* index of spk_1 */ + 1;
SpkIterator::new_with_range(
&descriptor,
// This will also persist the staged spk cache inclusions from prev call to
// `.insert_descriptor`.
0..index_after_spk_1 + lookahead,
)
.collect()
})]
.into();

let changeset = indexed_tx_graph::ChangeSet::from((tx_graph_changeset, indexer_changeset));

assert_eq!(
graph.batch_insert_relevant(txs.iter().cloned().map(|tx| (tx, None))),
changeset,
);

// The initial changeset will also contain info about the keychain we added
let initial_changeset = indexed_tx_graph::ChangeSet {
tx_graph: changeset.tx_graph,
indexer: keychain_txout::ChangeSet {
last_revealed: changeset.indexer.last_revealed,
spk_cache: [(
descriptor.descriptor_id(),
SpkIterator::new_with_range(&descriptor, 0..=9 /* index of spk_1*/ + lookahead)
.collect(),
)]
.into(),
},
};
let mut initial_indexer_changeset = keychain_txout::ChangeSet::default();
initial_indexer_changeset.last_revealed = changeset.indexer.last_revealed;
initial_indexer_changeset.spk_cache = [(
descriptor.descriptor_id(),
SpkIterator::new_with_range(&descriptor, 0..=9 /* index of spk_1*/ + lookahead).collect(),
)]
.into();

let initial_changeset =
indexed_tx_graph::ChangeSet::from((changeset.tx_graph, initial_indexer_changeset));

assert_eq!(graph.initial_changeset(), initial_changeset);
}
Expand Down
38 changes: 16 additions & 22 deletions crates/chain/tests/test_keychain_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,12 @@ fn merge_changesets_check_last_revealed() {
rhs_di.insert(descriptor_ids[1], 5); // value more than lhs desc 1
lhs_di.insert(descriptor_ids[3], 4); // key doesn't exist in lhs

let mut lhs = ChangeSet {
last_revealed: lhs_di,
..Default::default()
};
let rhs = ChangeSet {
last_revealed: rhs_di,
..Default::default()
};
let mut lhs = ChangeSet::default();
lhs.last_revealed = lhs_di;

let mut rhs = ChangeSet::default();
rhs.last_revealed = rhs_di;

lhs.merge(rhs);

// Existing index doesn't update if the new index in `other` is lower than `self`.
Expand Down Expand Up @@ -138,12 +136,13 @@ fn test_set_all_derivation_indices() {
),
]
.into();

let mut expected_changeset = ChangeSet::default();
expected_changeset.last_revealed = last_revealed.clone();
expected_changeset.spk_cache = spk_cache.clone();
assert_eq!(
txout_index.reveal_to_target_multi(&derive_to),
ChangeSet {
last_revealed: last_revealed.clone(),
spk_cache: spk_cache.clone(),
}
expected_changeset
);
assert_eq!(txout_index.last_revealed_indices(), derive_to);
assert_eq!(
Expand Down Expand Up @@ -638,16 +637,11 @@ fn lookahead_to_target() {
#[test]
fn applying_changesets_one_by_one_vs_aggregate_must_have_same_result() {
let desc = parse_descriptor(DESCRIPTORS[0]);
let changesets: &[ChangeSet] = &[
ChangeSet {
last_revealed: [(desc.descriptor_id(), 10)].into(),
..Default::default()
},
ChangeSet {
last_revealed: [(desc.descriptor_id(), 12)].into(),
..Default::default()
},
];
let mut changeset_1 = ChangeSet::default();
changeset_1.last_revealed = [(desc.descriptor_id(), 10)].into();
let mut changeset_2 = ChangeSet::default();
changeset_2.last_revealed = [(desc.descriptor_id(), 12)].into();
let changesets: &[ChangeSet] = &[changeset_1, changeset_2];

let mut indexer_a = KeychainTxOutIndex::<TestKeychain>::new(0, true);
let _ = indexer_a
Expand Down
Loading
Loading