Skip to content

Commit 8eef350

Browse files
committed
Merge #1453: refactor(electrum) put the tx cache in electrum
2d2656a feat(electrum): re-export `transaction_broadcast` method (志宇) 53fa350 refactor(electrum)!: put the tx cache in electrum (LLFourn) Pull request description: Previously there was a `TxCache` that you passed in as part of the sync request. There are lots of downsides to this: 1. If the user forgets to do this you cache nothing 2. where are you meant to keep this cache? The example shows it being recreated every time which seems very suboptimal. 3. More API and documentation surface area. Instead just do a plain old simple cache inside the electrum client. This way at least you only download transactions once. You can pre-populate the cache with a method also and I did this in the examples. * [x] This pull request breaks the existing API ACKs for top commit: evanlinjin: self-ACK 2d2656a notmandatory: ACK 2d2656a Tree-SHA512: 6c29fd4f99ea5bd66234d5cdaf4b157a192ddd3baacc91076e402d8df0de7010bc482e24895e85fcb2f805ec6d1ce6cdb7654f8f552c90ba75ed35f80a00b856
2 parents 363d9f4 + 2d2656a commit 8eef350

File tree

8 files changed

+308
-352
lines changed

8 files changed

+308
-352
lines changed

crates/chain/src/indexed_tx_graph.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,9 @@ pub trait Indexer {
352352
/// Determines whether the transaction should be included in the index.
353353
fn is_tx_relevant(&self, tx: &Transaction) -> bool;
354354
}
355+
356+
impl<A, I> AsRef<TxGraph<A>> for IndexedTxGraph<A, I> {
357+
fn as_ref(&self) -> &TxGraph<A> {
358+
&self.graph
359+
}
360+
}

crates/chain/src/spk_client.rs

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
//! Helper types for spk-based blockchain clients.
22
33
use crate::{
4-
collections::{BTreeMap, HashMap},
5-
local_chain::CheckPoint,
6-
ConfirmationTimeHeightAnchor, TxGraph,
4+
collections::BTreeMap, local_chain::CheckPoint, ConfirmationTimeHeightAnchor, TxGraph,
75
};
8-
use alloc::{boxed::Box, sync::Arc, vec::Vec};
9-
use bitcoin::{OutPoint, Script, ScriptBuf, Transaction, Txid};
6+
use alloc::{boxed::Box, vec::Vec};
7+
use bitcoin::{OutPoint, Script, ScriptBuf, Txid};
108
use core::{fmt::Debug, marker::PhantomData, ops::RangeBounds};
119

12-
/// A cache of [`Arc`]-wrapped full transactions, identified by their [`Txid`]s.
13-
///
14-
/// This is used by the chain-source to avoid re-fetching full transactions.
15-
pub type TxCache = HashMap<Txid, Arc<Transaction>>;
16-
1710
/// Data required to perform a spk-based blockchain client sync.
1811
///
1912
/// A client sync fetches relevant chain data for a known list of scripts, transaction ids and
@@ -24,8 +17,6 @@ pub struct SyncRequest {
2417
///
2518
/// [`LocalChain::tip`]: crate::local_chain::LocalChain::tip
2619
pub chain_tip: CheckPoint,
27-
/// Cache of full transactions, so the chain-source can avoid re-fetching.
28-
pub tx_cache: TxCache,
2920
/// Transactions that spend from or to these indexed script pubkeys.
3021
pub spks: Box<dyn ExactSizeIterator<Item = ScriptBuf> + Send>,
3122
/// Transactions with these txids.
@@ -39,36 +30,12 @@ impl SyncRequest {
3930
pub fn from_chain_tip(cp: CheckPoint) -> Self {
4031
Self {
4132
chain_tip: cp,
42-
tx_cache: TxCache::new(),
4333
spks: Box::new(core::iter::empty()),
4434
txids: Box::new(core::iter::empty()),
4535
outpoints: Box::new(core::iter::empty()),
4636
}
4737
}
4838

49-
/// Add to the [`TxCache`] held by the request.
50-
///
51-
/// This consumes the [`SyncRequest`] and returns the updated one.
52-
#[must_use]
53-
pub fn cache_txs<T>(mut self, full_txs: impl IntoIterator<Item = (Txid, T)>) -> Self
54-
where
55-
T: Into<Arc<Transaction>>,
56-
{
57-
self.tx_cache = full_txs
58-
.into_iter()
59-
.map(|(txid, tx)| (txid, tx.into()))
60-
.collect();
61-
self
62-
}
63-
64-
/// Add all transactions from [`TxGraph`] into the [`TxCache`].
65-
///
66-
/// This consumes the [`SyncRequest`] and returns the updated one.
67-
#[must_use]
68-
pub fn cache_graph_txs<A>(self, graph: &TxGraph<A>) -> Self {
69-
self.cache_txs(graph.full_txs().map(|tx_node| (tx_node.txid, tx_node.tx)))
70-
}
71-
7239
/// Set the [`Script`]s that will be synced against.
7340
///
7441
/// This consumes the [`SyncRequest`] and returns the updated one.
@@ -227,8 +194,6 @@ pub struct FullScanRequest<K> {
227194
///
228195
/// [`LocalChain::tip`]: crate::local_chain::LocalChain::tip
229196
pub chain_tip: CheckPoint,
230-
/// Cache of full transactions, so the chain-source can avoid re-fetching.
231-
pub tx_cache: TxCache,
232197
/// Iterators of script pubkeys indexed by the keychain index.
233198
pub spks_by_keychain: BTreeMap<K, Box<dyn Iterator<Item = (u32, ScriptBuf)> + Send>>,
234199
}
@@ -239,34 +204,10 @@ impl<K: Ord + Clone> FullScanRequest<K> {
239204
pub fn from_chain_tip(chain_tip: CheckPoint) -> Self {
240205
Self {
241206
chain_tip,
242-
tx_cache: TxCache::new(),
243207
spks_by_keychain: BTreeMap::new(),
244208
}
245209
}
246210

247-
/// Add to the [`TxCache`] held by the request.
248-
///
249-
/// This consumes the [`SyncRequest`] and returns the updated one.
250-
#[must_use]
251-
pub fn cache_txs<T>(mut self, full_txs: impl IntoIterator<Item = (Txid, T)>) -> Self
252-
where
253-
T: Into<Arc<Transaction>>,
254-
{
255-
self.tx_cache = full_txs
256-
.into_iter()
257-
.map(|(txid, tx)| (txid, tx.into()))
258-
.collect();
259-
self
260-
}
261-
262-
/// Add all transactions from [`TxGraph`] into the [`TxCache`].
263-
///
264-
/// This consumes the [`SyncRequest`] and returns the updated one.
265-
#[must_use]
266-
pub fn cache_graph_txs<A>(self, graph: &TxGraph<A>) -> Self {
267-
self.cache_txs(graph.full_txs().map(|tx_node| (tx_node.txid, tx_node.tx)))
268-
}
269-
270211
/// Construct a new [`FullScanRequest`] from a given `chain_tip` and `index`.
271212
///
272213
/// Unbounded script pubkey iterators for each keychain (`K`) are extracted using

0 commit comments

Comments
 (0)