Skip to content

Commit e9263f9

Browse files
evanlinjinLagginTimes
authored andcommitted
feat(core): Add is_empty methods to TxUpdate and {}_Response types
1 parent 5e49d3e commit e9263f9

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

crates/core/src/spk_client.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,13 @@ impl<A> Default for SyncResponse<A> {
403403
}
404404
}
405405

406+
impl<A> SyncResponse<A> {
407+
/// Returns true if the `SyncResponse` is empty.
408+
pub fn is_empty(&self) -> bool {
409+
self.tx_update.is_empty() && self.chain_update.is_none()
410+
}
411+
}
412+
406413
/// Builds a [`FullScanRequest`].
407414
///
408415
/// Construct with [`FullScanRequest::builder`].
@@ -560,6 +567,15 @@ impl<K, A> Default for FullScanResponse<K, A> {
560567
}
561568
}
562569

570+
impl<K, A> FullScanResponse<K, A> {
571+
/// Returns true if the `FullScanResponse` is empty.
572+
pub fn is_empty(&self) -> bool {
573+
self.tx_update.is_empty()
574+
&& self.last_active_indices.is_empty()
575+
&& self.chain_update.is_none()
576+
}
577+
}
578+
563579
struct KeychainSpkIter<'r, K> {
564580
keychain: K,
565581
spks: Option<&'r mut Box<dyn Iterator<Item = Indexed<ScriptBuf>> + Send>>,

crates/core/src/tx_update.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ impl<A> Default for TxUpdate<A> {
6464
}
6565
}
6666

67+
impl<A> TxUpdate<A> {
68+
/// Returns true if the `TxUpdate` contains no elements in any of its fields.
69+
pub fn is_empty(&self) -> bool {
70+
self.txs.is_empty()
71+
&& self.txouts.is_empty()
72+
&& self.anchors.is_empty()
73+
&& self.seen_ats.is_empty()
74+
&& self.evicted_ats.is_empty()
75+
}
76+
}
77+
6778
impl<A: Ord> TxUpdate<A> {
6879
/// Transforms the [`TxUpdate`] to have `anchors` (`A`) of another type (`A2`).
6980
///

crates/core/tests/test_spk_client.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use bdk_core::spk_client::{FullScanResponse, SyncResponse};
2+
3+
#[test]
4+
fn test_empty() {
5+
assert!(
6+
FullScanResponse::<(), ()>::default().is_empty(),
7+
"Default `FullScanResponse` must be empty"
8+
);
9+
assert!(
10+
SyncResponse::<()>::default().is_empty(),
11+
"Default `SyncResponse` must be empty"
12+
);
13+
}

crates/core/tests/test_tx_update.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use bdk_core::TxUpdate;
2+
3+
#[test]
4+
fn test_empty() {
5+
assert!(
6+
TxUpdate::<()>::default().is_empty(),
7+
"Default `TxUpdate` must be empty"
8+
);
9+
}

0 commit comments

Comments
 (0)