Skip to content

Commit 0d75a63

Browse files
authored
Merge pull request #889 from jkczyz/2021-04-electrum-trait
Define chain::Confirm trait for use by Electrum clients
2 parents 4f6a038 + 99e2283 commit 0d75a63

11 files changed

+296
-283
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
3030
use bitcoin::hash_types::{BlockHash, WPubkeyHash};
3131

3232
use lightning::chain;
33+
use lightning::chain::Confirm;
3334
use lightning::chain::chainmonitor;
3435
use lightning::chain::channelmonitor;
3536
use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, MonitorEvent};
@@ -428,11 +429,11 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
428429
let chain_hash = genesis_block(Network::Bitcoin).block_hash();
429430
let mut header = BlockHeader { version: 0x20000000, prev_blockhash: chain_hash, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
430431
let txdata: Vec<_> = channel_txn.iter().enumerate().map(|(i, tx)| (i + 1, tx)).collect();
431-
$node.transactions_confirmed(&header, 1, &txdata);
432+
$node.transactions_confirmed(&header, &txdata, 1);
432433
for _ in 2..100 {
433434
header = BlockHeader { version: 0x20000000, prev_blockhash: header.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
434435
}
435-
$node.update_best_block(&header, 99);
436+
$node.best_block_updated(&header, 99);
436437
} }
437438
}
438439

fuzz/src/full_stack.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
2727
use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
2828

2929
use lightning::chain;
30-
use lightning::chain::Listen;
30+
use lightning::chain::{Confirm, Listen};
3131
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
3232
use lightning::chain::chainmonitor;
3333
use lightning::chain::transaction::OutPoint;
@@ -207,9 +207,10 @@ impl<'a> MoneyLossDetector<'a> {
207207
self.blocks_connected += 1;
208208
let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height].0, merkle_root: Default::default(), time: self.blocks_connected, bits: 42, nonce: 42 };
209209
self.height += 1;
210-
self.manager.transactions_confirmed(&header, self.height as u32, &txdata);
211-
self.manager.update_best_block(&header, self.height as u32);
212-
(*self.monitor).block_connected(&header, &txdata, self.height as u32);
210+
self.manager.transactions_confirmed(&header, &txdata, self.height as u32);
211+
self.manager.best_block_updated(&header, self.height as u32);
212+
(*self.monitor).transactions_confirmed(&header, &txdata, self.height as u32);
213+
(*self.monitor).best_block_updated(&header, self.height as u32);
213214
if self.header_hashes.len() > self.height {
214215
self.header_hashes[self.height] = (header.block_hash(), self.blocks_connected);
215216
} else {

lightning/src/chain/chainmonitor.rs

Lines changed: 59 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -74,64 +74,14 @@ where C::Target: chain::Filter,
7474
P::Target: channelmonitor::Persist<ChannelSigner>,
7575
{
7676
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
77-
/// of a channel and reacting accordingly based on transactions in the connected block. See
77+
/// of a channel and reacting accordingly based on transactions in the given chain data. See
7878
/// [`ChannelMonitor::block_connected`] for details. Any HTLCs that were resolved on chain will
7979
/// be returned by [`chain::Watch::release_pending_monitor_events`].
8080
///
8181
/// Calls back to [`chain::Filter`] if any monitor indicated new outputs to watch. Subsequent
8282
/// calls must not exclude any transactions matching the new outputs nor any in-block
8383
/// descendants of such transactions. It is not necessary to re-fetch the block to obtain
8484
/// updated `txdata`.
85-
pub fn block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
86-
self.process_chain_data(header, txdata, |monitor, txdata| {
87-
monitor.block_connected(
88-
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
89-
});
90-
}
91-
92-
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
93-
/// of a channel and reacting accordingly to newly confirmed transactions. For details, see
94-
/// [`ChannelMonitor::transactions_confirmed`].
95-
///
96-
/// Used instead of [`block_connected`] by clients that are notified of transactions rather than
97-
/// blocks. May be called before or after [`update_best_block`] for transactions in the
98-
/// corresponding block. See [`update_best_block`] for further calling expectations.
99-
///
100-
/// [`block_connected`]: Self::block_connected
101-
/// [`update_best_block`]: Self::update_best_block
102-
pub fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
103-
self.process_chain_data(header, txdata, |monitor, txdata| {
104-
monitor.transactions_confirmed(
105-
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
106-
});
107-
}
108-
109-
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
110-
/// of a channel and reacting accordingly based on the new chain tip. For details, see
111-
/// [`ChannelMonitor::update_best_block`].
112-
///
113-
/// Used instead of [`block_connected`] by clients that are notified of transactions rather than
114-
/// blocks. May be called before or after [`transactions_confirmed`] for the corresponding
115-
/// block.
116-
///
117-
/// Must be called after new blocks become available for the most recent block. Intermediary
118-
/// blocks, however, may be safely skipped. In the event of a chain re-organization, this only
119-
/// needs to be called for the most recent block assuming `transaction_unconfirmed` is called
120-
/// for any affected transactions.
121-
///
122-
/// [`block_connected`]: Self::block_connected
123-
/// [`transactions_confirmed`]: Self::transactions_confirmed
124-
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
125-
pub fn update_best_block(&self, header: &BlockHeader, height: u32) {
126-
self.process_chain_data(header, &[], |monitor, txdata| {
127-
// While in practice there shouldn't be any recursive calls when given empty txdata,
128-
// it's still possible if a chain::Filter implementation returns a transaction.
129-
debug_assert!(txdata.is_empty());
130-
monitor.update_best_block(
131-
header, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
132-
});
133-
}
134-
13585
fn process_chain_data<FN>(&self, header: &BlockHeader, txdata: &TransactionData, process: FN)
13686
where
13787
FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<TransactionOutputs>
@@ -172,46 +122,6 @@ where C::Target: chain::Filter,
172122
}
173123
}
174124

175-
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
176-
/// of a channel based on the disconnected block. See [`ChannelMonitor::block_disconnected`] for
177-
/// details.
178-
pub fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
179-
let monitors = self.monitors.read().unwrap();
180-
for monitor in monitors.values() {
181-
monitor.block_disconnected(header, disconnected_height, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
182-
}
183-
}
184-
185-
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
186-
/// of a channel based on transactions unconfirmed as a result of a chain reorganization. See
187-
/// [`ChannelMonitor::transaction_unconfirmed`] for details.
188-
///
189-
/// Used instead of [`block_disconnected`] by clients that are notified of transactions rather
190-
/// than blocks. May be called before or after [`update_best_block`] for transactions in the
191-
/// corresponding block. See [`update_best_block`] for further calling expectations.
192-
///
193-
/// [`block_disconnected`]: Self::block_disconnected
194-
/// [`update_best_block`]: Self::update_best_block
195-
pub fn transaction_unconfirmed(&self, txid: &Txid) {
196-
let monitors = self.monitors.read().unwrap();
197-
for monitor in monitors.values() {
198-
monitor.transaction_unconfirmed(txid, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
199-
}
200-
}
201-
202-
/// Returns the set of txids that should be monitored for re-organization out of the chain.
203-
pub fn get_relevant_txids(&self) -> Vec<Txid> {
204-
let mut txids = Vec::new();
205-
let monitors = self.monitors.read().unwrap();
206-
for monitor in monitors.values() {
207-
txids.append(&mut monitor.get_relevant_txids());
208-
}
209-
210-
txids.sort_unstable();
211-
txids.dedup();
212-
txids
213-
}
214-
215125
/// Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
216126
///
217127
/// When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor
@@ -231,7 +141,7 @@ where C::Target: chain::Filter,
231141
}
232142
}
233143

234-
impl<ChannelSigner: Sign, C: Deref + Send + Sync, T: Deref + Send + Sync, F: Deref + Send + Sync, L: Deref + Send + Sync, P: Deref + Send + Sync>
144+
impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
235145
chain::Listen for ChainMonitor<ChannelSigner, C, T, F, L, P>
236146
where
237147
ChannelSigner: Sign,
@@ -242,12 +152,67 @@ where
242152
P::Target: channelmonitor::Persist<ChannelSigner>,
243153
{
244154
fn block_connected(&self, block: &Block, height: u32) {
155+
let header = &block.header;
245156
let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
246-
ChainMonitor::block_connected(self, &block.header, &txdata, height);
157+
self.process_chain_data(header, &txdata, |monitor, txdata| {
158+
monitor.block_connected(
159+
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
160+
});
247161
}
248162

249163
fn block_disconnected(&self, header: &BlockHeader, height: u32) {
250-
ChainMonitor::block_disconnected(self, header, height);
164+
let monitors = self.monitors.read().unwrap();
165+
for monitor in monitors.values() {
166+
monitor.block_disconnected(
167+
header, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
168+
}
169+
}
170+
}
171+
172+
impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
173+
chain::Confirm for ChainMonitor<ChannelSigner, C, T, F, L, P>
174+
where
175+
ChannelSigner: Sign,
176+
C::Target: chain::Filter,
177+
T::Target: BroadcasterInterface,
178+
F::Target: FeeEstimator,
179+
L::Target: Logger,
180+
P::Target: channelmonitor::Persist<ChannelSigner>,
181+
{
182+
fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
183+
self.process_chain_data(header, txdata, |monitor, txdata| {
184+
monitor.transactions_confirmed(
185+
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
186+
});
187+
}
188+
189+
fn transaction_unconfirmed(&self, txid: &Txid) {
190+
let monitors = self.monitors.read().unwrap();
191+
for monitor in monitors.values() {
192+
monitor.transaction_unconfirmed(txid, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
193+
}
194+
}
195+
196+
fn best_block_updated(&self, header: &BlockHeader, height: u32) {
197+
self.process_chain_data(header, &[], |monitor, txdata| {
198+
// While in practice there shouldn't be any recursive calls when given empty txdata,
199+
// it's still possible if a chain::Filter implementation returns a transaction.
200+
debug_assert!(txdata.is_empty());
201+
monitor.best_block_updated(
202+
header, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
203+
});
204+
}
205+
206+
fn get_relevant_txids(&self) -> Vec<Txid> {
207+
let mut txids = Vec::new();
208+
let monitors = self.monitors.read().unwrap();
209+
for monitor in monitors.values() {
210+
txids.append(&mut monitor.get_relevant_txids());
211+
}
212+
213+
txids.sort_unstable();
214+
txids.dedup();
215+
txids
251216
}
252217
}
253218

lightning/src/chain/channelmonitor.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,11 +1311,9 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
13111311
/// outputs to watch. See [`block_connected`] for details.
13121312
///
13131313
/// Used instead of [`block_connected`] by clients that are notified of transactions rather than
1314-
/// blocks. May be called before or after [`update_best_block`] for transactions in the
1315-
/// corresponding block. See [`update_best_block`] for further calling expectations.
1314+
/// blocks. See [`chain::Confirm`] for calling expectations.
13161315
///
13171316
/// [`block_connected`]: Self::block_connected
1318-
/// [`update_best_block`]: Self::update_best_block
13191317
pub fn transactions_confirmed<B: Deref, F: Deref, L: Deref>(
13201318
&self,
13211319
header: &BlockHeader,
@@ -1337,11 +1335,9 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
13371335
/// Processes a transaction that was reorganized out of the chain.
13381336
///
13391337
/// Used instead of [`block_disconnected`] by clients that are notified of transactions rather
1340-
/// than blocks. May be called before or after [`update_best_block`] for transactions in the
1341-
/// corresponding block. See [`update_best_block`] for further calling expectations.
1338+
/// than blocks. See [`chain::Confirm`] for calling expectations.
13421339
///
13431340
/// [`block_disconnected`]: Self::block_disconnected
1344-
/// [`update_best_block`]: Self::update_best_block
13451341
pub fn transaction_unconfirmed<B: Deref, F: Deref, L: Deref>(
13461342
&self,
13471343
txid: &Txid,
@@ -1361,18 +1357,10 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
13611357
/// [`block_connected`] for details.
13621358
///
13631359
/// Used instead of [`block_connected`] by clients that are notified of transactions rather than
1364-
/// blocks. May be called before or after [`transactions_confirmed`] for the corresponding
1365-
/// block.
1366-
///
1367-
/// Must be called after new blocks become available for the most recent block. Intermediary
1368-
/// blocks, however, may be safely skipped. In the event of a chain re-organization, this only
1369-
/// needs to be called for the most recent block assuming `transaction_unconfirmed` is called
1370-
/// for any affected transactions.
1360+
/// blocks. See [`chain::Confirm`] for calling expectations.
13711361
///
13721362
/// [`block_connected`]: Self::block_connected
1373-
/// [`transactions_confirmed`]: Self::transactions_confirmed
1374-
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
1375-
pub fn update_best_block<B: Deref, F: Deref, L: Deref>(
1363+
pub fn best_block_updated<B: Deref, F: Deref, L: Deref>(
13761364
&self,
13771365
header: &BlockHeader,
13781366
height: u32,
@@ -1385,7 +1373,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
13851373
F::Target: FeeEstimator,
13861374
L::Target: Logger,
13871375
{
1388-
self.inner.lock().unwrap().update_best_block(
1376+
self.inner.lock().unwrap().best_block_updated(
13891377
header, height, broadcaster, fee_estimator, logger)
13901378
}
13911379

@@ -2109,7 +2097,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21092097
self.transactions_confirmed(header, txdata, height, broadcaster, fee_estimator, logger)
21102098
}
21112099

2112-
fn update_best_block<B: Deref, F: Deref, L: Deref>(
2100+
fn best_block_updated<B: Deref, F: Deref, L: Deref>(
21132101
&mut self,
21142102
header: &BlockHeader,
21152103
height: u32,
@@ -2727,6 +2715,29 @@ where
27272715
}
27282716
}
27292717

2718+
impl<Signer: Sign, T: Deref, F: Deref, L: Deref> chain::Confirm for (ChannelMonitor<Signer>, T, F, L)
2719+
where
2720+
T::Target: BroadcasterInterface,
2721+
F::Target: FeeEstimator,
2722+
L::Target: Logger,
2723+
{
2724+
fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
2725+
self.0.transactions_confirmed(header, txdata, height, &*self.1, &*self.2, &*self.3);
2726+
}
2727+
2728+
fn transaction_unconfirmed(&self, txid: &Txid) {
2729+
self.0.transaction_unconfirmed(txid, &*self.1, &*self.2, &*self.3);
2730+
}
2731+
2732+
fn best_block_updated(&self, header: &BlockHeader, height: u32) {
2733+
self.0.best_block_updated(header, height, &*self.1, &*self.2, &*self.3);
2734+
}
2735+
2736+
fn get_relevant_txids(&self) -> Vec<Txid> {
2737+
self.0.get_relevant_txids()
2738+
}
2739+
}
2740+
27302741
const MAX_ALLOC_SIZE: usize = 64*1024;
27312742

27322743
impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>

0 commit comments

Comments
 (0)