Skip to content

Commit 0121e0f

Browse files
committed
docs(wallet): expand docs for apply_evicted_txs
1 parent 4d3116a commit 0121e0f

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

wallet/src/wallet/mod.rs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,13 +2457,57 @@ impl Wallet {
24572457
self.stage.merge(indexed_graph_changeset.into());
24582458
}
24592459

2460-
/// Apply evictions of the given txids with their associated timestamps.
2460+
/// Apply evictions of the given transaction IDs with their associated timestamps.
24612461
///
2462-
/// This means that any pending unconfirmed tx in this set will no longer be canonical by
2463-
/// default. Note that an evicted tx can become canonical if it is later seen again or
2464-
/// observed on-chain.
2462+
/// This function is used to mark specific unconfirmed transactions as evicted from the mempool.
2463+
/// Eviction means that these transactions are no longer considered canonical by default, which
2464+
/// implies that they are not part of the wallet's [`transactions`] set. This can happen, for
2465+
/// example, when a transaction is dropped from the mempool due to low fees or conflicts
2466+
/// with another transaction.
24652467
///
2466-
/// This stages the changes which need to be persisted.
2468+
/// Only transactions that are currently unconfirmed and canonical will be considered for
2469+
/// eviction. Transactions that are not relevant to the wallet are ignored. Note that an
2470+
/// evicted transaction can become canonical again if it is later observed on-chain or seen in
2471+
/// the mempool with a higher priority (e.g., due to a fee bump).
2472+
///
2473+
/// ### Parameters
2474+
///
2475+
/// - `evicted_txs`: An iterator of `(Txid, u64)` tuples, where:
2476+
/// - `Txid`: The transaction ID of the transaction to be evicted.
2477+
/// - `u64`: The timestamp indicating when the transaction was evicted from the mempool. This
2478+
/// will usually correspond to the time of the latest chain sync. See docs for
2479+
/// [`start_sync_with_revealed_spks`].
2480+
///
2481+
/// ### Example
2482+
///
2483+
/// ```rust,no_run
2484+
/// # use bitcoin::hashes::Hash;
2485+
/// use std::time::{SystemTime, UNIX_EPOCH};
2486+
/// # let mut wallet = bdk_wallet::doctest_wallet!();
2487+
/// # let txid_to_evict = bitcoin::Txid::all_zeros();
2488+
///
2489+
/// let timestamp = SystemTime::now()
2490+
/// .duration_since(UNIX_EPOCH)
2491+
/// .unwrap()
2492+
/// .as_secs();
2493+
///
2494+
/// wallet.apply_evicted_txs(vec![(txid_to_evict, timestamp)]);
2495+
/// ```
2496+
///
2497+
/// ### Note
2498+
///
2499+
/// - This function is particularly useful when syncing with a blockchain backend that provides
2500+
/// mempool eviction notifications.
2501+
/// - The changes are staged in the wallet's internal state and must be persisted to ensure they
2502+
/// are retained across wallet restarts. Use [`Wallet::take_staged`] to retrieve the staged
2503+
/// changes and persist them to your database of choice.
2504+
/// - Evicted transactions are removed from the wallet's canonical transaction set, but the data
2505+
/// remains in the wallet's internal transaction graph for historical purposes.
2506+
/// - Ensure that the timestamps provided are accurate and monotonically increasing, as they
2507+
/// influence the wallet's canonicalization logic.
2508+
///
2509+
/// [`transactions`]: Wallet::transactions
2510+
/// [`start_sync_with_revealed_spks`]: Wallet::start_sync_with_revealed_spks
24672511
pub fn apply_evicted_txs(&mut self, evicted_txs: impl IntoIterator<Item = (Txid, u64)>) {
24682512
let chain = &self.chain;
24692513
let canon_txids: Vec<Txid> = self
@@ -2671,7 +2715,7 @@ macro_rules! floating_rate {
26712715
/// Macro for getting a wallet for use in a doctest
26722716
macro_rules! doctest_wallet {
26732717
() => {{
2674-
use $crate::bitcoin::{BlockHash, Transaction, absolute, TxOut, Network, hashes::Hash};
2718+
use $crate::bitcoin::{transaction, Amount, BlockHash, Transaction, absolute, TxOut, Network, hashes::Hash};
26752719
use $crate::chain::{ConfirmationBlockTime, BlockId, TxGraph, tx_graph};
26762720
use $crate::{Update, KeychainKind, Wallet};
26772721
use $crate::test_utils::*;

0 commit comments

Comments
 (0)