1 use lightning::chain::WatchedOutput;
2 use bitcoin::{Txid, BlockHash, Transaction, BlockHeader, OutPoint};
4 use std::collections::{HashSet, HashMap};
7 // Represents the current state.
8 pub(crate) struct SyncState {
9 // Transactions that were previously processed, but must not be forgotten
10 // yet since they still need to be monitored for confirmation on-chain.
11 pub watched_transactions: HashSet<Txid>,
12 // Outputs that were previously processed, but must not be forgotten yet as
13 // as we still need to monitor any spends on-chain.
14 pub watched_outputs: HashMap<OutPoint, WatchedOutput>,
15 // The tip hash observed during our last sync.
16 pub last_sync_hash: Option<BlockHash>,
17 // Indicates whether we need to resync, e.g., after encountering an error.
18 pub pending_sync: bool,
22 pub fn new() -> Self {
24 watched_transactions: HashSet::new(),
25 watched_outputs: HashMap::new(),
33 // A queue that is to be filled by `Filter` and drained during the next syncing round.
34 pub(crate) struct FilterQueue {
35 // Transactions that were registered via the `Filter` interface and have to be processed.
36 pub transactions: HashSet<Txid>,
37 // Outputs that were registered via the `Filter` interface and have to be processed.
38 pub outputs: HashMap<OutPoint, WatchedOutput>,
42 pub fn new() -> Self {
44 transactions: HashSet::new(),
45 outputs: HashMap::new(),
49 // Processes the transaction and output queues and adds them to the given [`SyncState`].
51 // Returns `true` if new items had been registered.
52 pub fn process_queues(&mut self, sync_state: &mut SyncState) -> bool {
53 let mut pending_registrations = false;
55 if !self.transactions.is_empty() {
56 pending_registrations = true;
58 sync_state.watched_transactions.extend(self.transactions.drain());
61 if !self.outputs.is_empty() {
62 pending_registrations = true;
64 sync_state.watched_outputs.extend(self.outputs.drain());
70 pub(crate) struct ConfirmedTx {
72 pub block_header: BlockHeader,
73 pub block_height: u32,