Skip to content

Commit bf0057f

Browse files
committed
refactor(persist_test_utils): Reuse ChangeSet construction helpers
1 parent 0ec828d commit bf0057f

1 file changed

Lines changed: 4 additions & 133 deletions

File tree

src/persist_test_utils.rs

Lines changed: 4 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//! Utilities for testing custom persistence backends for `bdk_wallet`
22
3-
// TODO:
4-
// - Extract reusable changeset helper functions
5-
63
use alloc::boxed::Box;
74
use core::fmt;
85
#[cfg(feature = "std")]
@@ -102,88 +99,10 @@ where
10299
}
103100

104101
// create changeset
105-
let descriptor: Descriptor<DescriptorPublicKey> = DESCRIPTORS[0].parse().unwrap();
106-
let change_descriptor: Descriptor<DescriptorPublicKey> = DESCRIPTORS[1].parse().unwrap();
107-
108-
let local_chain_changeset = local_chain::ChangeSet {
109-
blocks: [
110-
(910234, Some(hash!("B"))),
111-
(910233, Some(hash!("T"))),
112-
(910235, Some(hash!("C"))),
113-
]
114-
.into(),
115-
};
116-
117-
let tx1 = Arc::new(create_one_inp_one_out_tx(
118-
hash!("We_are_all_Satoshi"),
119-
30_000,
120-
));
121-
let tx2 = Arc::new(create_one_inp_one_out_tx(tx1.compute_txid(), 20_000));
122-
123-
let conf_anchor: ConfirmationBlockTime = ConfirmationBlockTime {
124-
block_id: block_id!(910234, "B"),
125-
confirmation_time: 1755317160,
126-
};
127-
128-
let outpoint = OutPoint::new(hash!("Rust"), 0);
129-
130-
let tx_graph_changeset = tx_graph::ChangeSet::<ConfirmationBlockTime> {
131-
txs: [tx1.clone()].into(),
132-
txouts: [
133-
(
134-
outpoint,
135-
TxOut {
136-
value: Amount::from_sat(1300),
137-
script_pubkey: spk_at_index(&descriptor, 4),
138-
},
139-
),
140-
(
141-
OutPoint::new(hash!("REDB"), 0),
142-
TxOut {
143-
value: Amount::from_sat(1400),
144-
script_pubkey: spk_at_index(&descriptor, 10),
145-
},
146-
),
147-
]
148-
.into(),
149-
anchors: [(conf_anchor, tx1.compute_txid())].into(),
150-
last_seen: [(tx1.compute_txid(), 1755317760)].into(),
151-
first_seen: [(tx1.compute_txid(), 1755317750)].into(),
152-
last_evicted: [(tx1.compute_txid(), 1755317760)].into(),
153-
};
154-
155-
let keychain_txout_changeset = keychain_txout::ChangeSet {
156-
last_revealed: [
157-
(descriptor.descriptor_id(), 12),
158-
(change_descriptor.descriptor_id(), 10),
159-
]
160-
.into(),
161-
spk_cache: [
162-
(
163-
descriptor.descriptor_id(),
164-
SpkIterator::new_with_range(&descriptor, 0..=37).collect(),
165-
),
166-
(
167-
change_descriptor.descriptor_id(),
168-
SpkIterator::new_with_range(&change_descriptor, 0..=35).collect(),
169-
),
170-
]
171-
.into(),
172-
};
173-
174-
let locked_outpoints_changeset = locked_outpoints::ChangeSet {
175-
outpoints: [(outpoint, true)].into(),
176-
};
102+
let tx1 = create_one_inp_one_out_tx(hash!("We_are_all_Satoshi"), 30_000);
103+
let tx2 = create_one_inp_one_out_tx(tx1.compute_txid(), 20_000);
177104

178-
let mut changeset = ChangeSet {
179-
descriptor: Some(descriptor.clone()),
180-
change_descriptor: Some(change_descriptor.clone()),
181-
network: Some(Network::Testnet),
182-
local_chain: local_chain_changeset,
183-
tx_graph: tx_graph_changeset,
184-
indexer: keychain_txout_changeset,
185-
locked_outpoints: locked_outpoints_changeset,
186-
};
105+
let mut changeset = get_changeset(tx1);
187106

188107
// persist and load
189108
WalletPersister::persist(&mut store, &changeset).map_err(E::persister)?;
@@ -198,55 +117,7 @@ where
198117
}
199118

200119
// create another changeset
201-
let local_chain_changeset = local_chain::ChangeSet {
202-
blocks: [(910236, Some(hash!("BDK")))].into(),
203-
};
204-
205-
let conf_anchor: ConfirmationBlockTime = ConfirmationBlockTime {
206-
block_id: block_id!(910236, "BDK"),
207-
confirmation_time: 1755317760,
208-
};
209-
210-
let outpoint = OutPoint::new(hash!("Bitcoin_fixes_things"), 1);
211-
212-
let tx_graph_changeset = tx_graph::ChangeSet::<ConfirmationBlockTime> {
213-
txs: [tx2.clone()].into(),
214-
txouts: [(
215-
outpoint,
216-
TxOut {
217-
value: Amount::from_sat(10000),
218-
script_pubkey: spk_at_index(&descriptor, 21),
219-
},
220-
)]
221-
.into(),
222-
anchors: [(conf_anchor, tx2.compute_txid())].into(),
223-
last_seen: [(tx2.compute_txid(), 1755317700)].into(),
224-
first_seen: [(tx2.compute_txid(), 1755317700)].into(),
225-
last_evicted: [(tx2.compute_txid(), 1755317760)].into(),
226-
};
227-
228-
let keychain_txout_changeset = keychain_txout::ChangeSet {
229-
last_revealed: [(descriptor.descriptor_id(), 14)].into(),
230-
spk_cache: [(
231-
descriptor.descriptor_id(),
232-
SpkIterator::new_with_range(&descriptor, 37..=39).collect(),
233-
)]
234-
.into(),
235-
};
236-
237-
let locked_outpoints_changeset = locked_outpoints::ChangeSet {
238-
outpoints: [(outpoint, true)].into(),
239-
};
240-
241-
let changeset_new = ChangeSet {
242-
descriptor: None,
243-
change_descriptor: None,
244-
network: None,
245-
local_chain: local_chain_changeset,
246-
tx_graph: tx_graph_changeset,
247-
indexer: keychain_txout_changeset,
248-
locked_outpoints: locked_outpoints_changeset,
249-
};
120+
let changeset_new = get_changeset_two(tx2);
250121

251122
// persist, load and check if same as merged
252123
WalletPersister::persist(&mut store, &changeset_new).map_err(E::persister)?;

0 commit comments

Comments
 (0)