Skip to content

Commit 71230c9

Browse files
committed
Replace WatchEvent usage with get_outputs_to_watch
Outputs to watch are tracked by ChannelMonitor as of 73dce20. Instead of determining new outputs to watch independently using ChainWatchedUtil, do so by comparing against outputs already tracked. Thus, ChainWatchedUtil and WatchEvent are no longer needed.
1 parent 9e14256 commit 71230c9

File tree

5 files changed

+73
-226
lines changed

5 files changed

+73
-226
lines changed

lightning/src/chain/chaininterface.rs

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
//! disconnections, transaction broadcasting, and feerate information requests.
1515
1616
use bitcoin::blockdata::transaction::Transaction;
17-
use bitcoin::blockdata::script::Script;
18-
use bitcoin::hash_types::Txid;
19-
20-
use std::collections::HashSet;
2117

2218
/// An interface to send a transaction to the Bitcoin network.
2319
pub trait BroadcasterInterface: Sync + Send {
@@ -55,91 +51,3 @@ pub trait FeeEstimator: Sync + Send {
5551

5652
/// Minimum relay fee as required by bitcoin network mempool policy.
5753
pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000;
58-
59-
/// Utility for tracking registered txn/outpoints and checking for matches
60-
#[cfg_attr(test, derive(PartialEq))]
61-
pub struct ChainWatchedUtil {
62-
watch_all: bool,
63-
64-
// We are more conservative in matching during testing to ensure everything matches *exactly*,
65-
// even though during normal runtime we take more optimized match approaches...
66-
#[cfg(test)]
67-
watched_txn: HashSet<(Txid, Script)>,
68-
#[cfg(not(test))]
69-
watched_txn: HashSet<Script>,
70-
71-
watched_outpoints: HashSet<(Txid, u32)>,
72-
}
73-
74-
impl ChainWatchedUtil {
75-
/// Constructs an empty (watches nothing) ChainWatchedUtil
76-
pub fn new() -> Self {
77-
Self {
78-
watch_all: false,
79-
watched_txn: HashSet::new(),
80-
watched_outpoints: HashSet::new(),
81-
}
82-
}
83-
84-
/// Registers a tx for monitoring, returning true if it was a new tx and false if we'd already
85-
/// been watching for it.
86-
pub fn register_tx(&mut self, txid: &Txid, script_pub_key: &Script) -> bool {
87-
if self.watch_all { return false; }
88-
#[cfg(test)]
89-
{
90-
self.watched_txn.insert((txid.clone(), script_pub_key.clone()))
91-
}
92-
#[cfg(not(test))]
93-
{
94-
let _tx_unused = txid; // It's used in cfg(test), though
95-
self.watched_txn.insert(script_pub_key.clone())
96-
}
97-
}
98-
99-
/// Registers an outpoint for monitoring, returning true if it was a new outpoint and false if
100-
/// we'd already been watching for it
101-
pub fn register_outpoint(&mut self, outpoint: (Txid, u32), _script_pub_key: &Script) -> bool {
102-
if self.watch_all { return false; }
103-
self.watched_outpoints.insert(outpoint)
104-
}
105-
106-
/// Sets us to match all transactions, returning true if this is a new setting and false if
107-
/// we'd already been set to match everything.
108-
pub fn watch_all(&mut self) -> bool {
109-
if self.watch_all { return false; }
110-
self.watch_all = true;
111-
true
112-
}
113-
114-
/// Checks if a given transaction matches the current filter.
115-
pub fn does_match_tx(&self, tx: &Transaction) -> bool {
116-
if self.watch_all {
117-
return true;
118-
}
119-
for out in tx.output.iter() {
120-
#[cfg(test)]
121-
for &(ref txid, ref script) in self.watched_txn.iter() {
122-
if *script == out.script_pubkey {
123-
if tx.txid() == *txid {
124-
return true;
125-
}
126-
}
127-
}
128-
#[cfg(not(test))]
129-
for script in self.watched_txn.iter() {
130-
if *script == out.script_pubkey {
131-
return true;
132-
}
133-
}
134-
}
135-
for input in tx.input.iter() {
136-
for outpoint in self.watched_outpoints.iter() {
137-
let &(outpoint_hash, outpoint_index) = outpoint;
138-
if outpoint_hash == input.previous_output.txid && outpoint_index == input.previous_output.vout {
139-
return true;
140-
}
141-
}
142-
}
143-
false
144-
}
145-
}

lightning/src/chain/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ pub trait Watch: Send + Sync {
7373
///
7474
/// Implementations are responsible for watching the chain for the funding transaction along
7575
/// with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
76-
/// calling [`block_connected`] and [`block_disconnected`] on the monitor and including all such
77-
/// transactions that meet this criteria.
76+
/// calling [`block_connected`] and [`block_disconnected`] on the monitor.
7877
///
7978
/// [`get_outputs_to_watch`]: ../ln/channelmonitor/struct.ChannelMonitor.html#method.get_outputs_to_watch
8079
/// [`block_connected`]: ../ln/channelmonitor/struct.ChannelMonitor.html#method.block_connected
@@ -119,9 +118,9 @@ pub trait Watch: Send + Sync {
119118
pub trait Filter: Send + Sync {
120119
/// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
121120
/// a spending condition.
122-
fn register_tx(&self, txid: Txid, script_pubkey: Script);
121+
fn register_tx(&self, txid: &Txid, script_pubkey: &Script);
123122

124123
/// Registers interest in spends of a transaction output identified by `outpoint` having
125124
/// `script_pubkey` as the spending condition.
126-
fn register_output(&self, outpoint: OutPoint, script_pubkey: Script);
125+
fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script);
127126
}

0 commit comments

Comments
 (0)