Skip to content

Commit 012be1e

Browse files
committed
f Move common types to dedicated file
This will become useful when we introduce other client types, e.g. Electrum.
1 parent 500412a commit 012be1e

File tree

3 files changed

+79
-70
lines changed

3 files changed

+79
-70
lines changed

lightning-transaction-sync/src/esplora.rs

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::error::{TxSyncError, InternalError};
2+
use crate::types::{SyncState, FilterQueue, ConfirmedTx};
23

34
use lightning::util::logger::Logger;
45
use lightning::{log_error, log_given_level, log_info, log_internal, log_debug, log_trace};
56
use lightning::chain::WatchedOutput;
67
use lightning::chain::{Confirm, Filter};
78

8-
use bitcoin::{BlockHash, BlockHeader, Script, Transaction, Txid};
9+
use bitcoin::{BlockHash, Script, Txid};
910

1011
use esplora_client::Builder;
1112
#[cfg(feature = "async-interface")]
@@ -327,68 +328,6 @@ where
327328
}
328329
}
329330

330-
// Represents the current state.
331-
struct SyncState {
332-
// Transactions that were previously processed, but must not be forgotten
333-
// yet since they still need to be monitored for confirmation on-chain.
334-
watched_transactions: HashSet<Txid>,
335-
// Outputs that were previously processed, but must not be forgotten yet as
336-
// as we still need to monitor any spends on-chain.
337-
watched_outputs: HashSet<WatchedOutput>,
338-
// The tip hash observed during our last sync.
339-
last_sync_hash: Option<BlockHash>,
340-
// Indicates whether we need to resync, e.g., after encountering an error.
341-
pending_sync: bool,
342-
}
343-
344-
impl SyncState {
345-
fn new() -> Self {
346-
Self {
347-
watched_transactions: HashSet::new(),
348-
watched_outputs: HashSet::new(),
349-
last_sync_hash: None,
350-
pending_sync: false,
351-
}
352-
}
353-
}
354-
355-
// A queue that is to be filled by `Filter` and drained during the next syncing round.
356-
struct FilterQueue {
357-
// Transactions that were registered via the `Filter` interface and have to be processed.
358-
transactions: HashSet<Txid>,
359-
// Outputs that were registered via the `Filter` interface and have to be processed.
360-
outputs: HashSet<WatchedOutput>,
361-
}
362-
363-
impl FilterQueue {
364-
fn new() -> Self {
365-
Self {
366-
transactions: HashSet::new(),
367-
outputs: HashSet::new(),
368-
}
369-
}
370-
371-
// Processes the transaction and output queues and adds them to the given [`SyncState`].
372-
//
373-
// Returns `true` if new items had been registered.
374-
fn process_queues(&mut self, sync_state: &mut SyncState) -> bool {
375-
let mut pending_registrations = false;
376-
377-
if !self.transactions.is_empty() {
378-
pending_registrations = true;
379-
380-
sync_state.watched_transactions.extend(self.transactions.drain());
381-
}
382-
383-
if !self.outputs.is_empty() {
384-
pending_registrations = true;
385-
386-
sync_state.watched_outputs.extend(self.outputs.drain());
387-
}
388-
pending_registrations
389-
}
390-
}
391-
392331
#[cfg(feature = "async-interface")]
393332
type MutexType<I> = futures::lock::Mutex<I>;
394333
#[cfg(not(feature = "async-interface"))]
@@ -401,13 +340,6 @@ pub type EsploraClientType = AsyncClient;
401340
pub type EsploraClientType = BlockingClient;
402341

403342

404-
struct ConfirmedTx {
405-
tx: Transaction,
406-
block_header: BlockHeader,
407-
block_height: u32,
408-
pos: usize,
409-
}
410-
411343
impl<L: Deref> Filter for EsploraSyncClient<L>
412344
where
413345
L::Target: Logger,

lightning-transaction-sync/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ extern crate bdk_macros;
6868
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
6969
mod esplora;
7070

71+
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
72+
mod types;
73+
7174
#[cfg(test)]
7275
mod tests;
7376

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use lightning::chain::WatchedOutput;
2+
use bitcoin::{Txid, BlockHash, Transaction, BlockHeader};
3+
use std::collections::HashSet;
4+
5+
6+
// Represents the current state.
7+
pub(crate) struct SyncState {
8+
// Transactions that were previously processed, but must not be forgotten
9+
// yet since they still need to be monitored for confirmation on-chain.
10+
pub watched_transactions: HashSet<Txid>,
11+
// Outputs that were previously processed, but must not be forgotten yet as
12+
// as we still need to monitor any spends on-chain.
13+
pub watched_outputs: HashSet<WatchedOutput>,
14+
// The tip hash observed during our last sync.
15+
pub last_sync_hash: Option<BlockHash>,
16+
// Indicates whether we need to resync, e.g., after encountering an error.
17+
pub pending_sync: bool,
18+
}
19+
20+
impl SyncState {
21+
pub fn new() -> Self {
22+
Self {
23+
watched_transactions: HashSet::new(),
24+
watched_outputs: HashSet::new(),
25+
last_sync_hash: None,
26+
pending_sync: false,
27+
}
28+
}
29+
}
30+
31+
32+
// A queue that is to be filled by `Filter` and drained during the next syncing round.
33+
pub(crate) struct FilterQueue {
34+
// Transactions that were registered via the `Filter` interface and have to be processed.
35+
pub transactions: HashSet<Txid>,
36+
// Outputs that were registered via the `Filter` interface and have to be processed.
37+
pub outputs: HashSet<WatchedOutput>,
38+
}
39+
40+
impl FilterQueue {
41+
pub fn new() -> Self {
42+
Self {
43+
transactions: HashSet::new(),
44+
outputs: HashSet::new(),
45+
}
46+
}
47+
48+
// Processes the transaction and output queues and adds them to the given [`SyncState`].
49+
//
50+
// Returns `true` if new items had been registered.
51+
pub fn process_queues(&mut self, sync_state: &mut SyncState) -> bool {
52+
let mut pending_registrations = false;
53+
54+
if !self.transactions.is_empty() {
55+
pending_registrations = true;
56+
57+
sync_state.watched_transactions.extend(self.transactions.drain());
58+
}
59+
60+
if !self.outputs.is_empty() {
61+
pending_registrations = true;
62+
63+
sync_state.watched_outputs.extend(self.outputs.drain());
64+
}
65+
pending_registrations
66+
}
67+
}
68+
69+
pub(crate) struct ConfirmedTx {
70+
pub tx: Transaction,
71+
pub block_header: BlockHeader,
72+
pub block_height: u32,
73+
pub pos: usize,
74+
}

0 commit comments

Comments
 (0)