Skip to content

feat!: Add #[non_exhaustive] to ChangeSet structs #1980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 @@ -102,11 +102,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 @@ -482,6 +482,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 @@ -1021,6 +1021,7 @@ impl<K: core::fmt::Debug> std::error::Error for InsertDescriptorError<K> {}
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[must_use]
#[non_exhaustive]
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 @@ -408,6 +408,7 @@ impl LocalChain {
/// The [`ChangeSet`] represents changes to [`LocalChain`].
#[derive(Debug, Default, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[non_exhaustive]
pub struct ChangeSet {
/// 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 @@ -173,6 +173,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 @@ -1415,6 +1416,7 @@ impl<A: Anchor> TxGraph<A> {
))
)]
#[must_use]
#[non_exhaustive]
pub struct ChangeSet<A = ()> {
/// Added transactions.
pub txs: BTreeSet<Arc<Transaction>>,
Expand Down
63 changes: 30 additions & 33 deletions crates/chain/tests/test_indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ mod common;

use std::{collections::BTreeSet, sync::Arc};

use bdk_chain::indexed_tx_graph::ChangeSet;
use bdk_chain::{
indexed_tx_graph::{self, IndexedTxGraph},
indexer::keychain_txout::KeychainTxOutIndex,
local_chain::LocalChain,
tx_graph, Balance, CanonicalizationParams, ChainPosition, ConfirmationBlockTime, DescriptorExt,
Balance, CanonicalizationParams, ChainPosition, ConfirmationBlockTime, DescriptorExt,
SpkIterator,
};
use bdk_testenv::{
Expand Down Expand Up @@ -74,45 +75,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 changeset = ChangeSet::default();
changeset
.tx_graph
.txs
.extend(txs.iter().cloned().map(Arc::new));
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();
changeset.indexer = 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_changeset = indexed_tx_graph::ChangeSet::default();
initial_changeset.tx_graph = changeset.tx_graph;
initial_changeset.indexer = keychain_txout::ChangeSet::default();
initial_changeset.indexer.last_revealed = changeset.indexer.last_revealed;
initial_changeset.indexer.spk_cache = [(
descriptor.descriptor_id(),
SpkIterator::new_with_range(&descriptor, 0..=9 /*index of spk_1 */ + lookahead).collect(),
)]
.into();

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 @@ -80,14 +80,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 @@ -129,12 +127,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 @@ -629,16 +628,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