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