Skip to content

Commit 23c4c8b

Browse files
committed
Implement chain::Confirm for relevant structs
1 parent 6b12dd2 commit 23c4c8b

File tree

8 files changed

+144
-184
lines changed

8 files changed

+144
-184
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: 3 additions & 3 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,8 +207,8 @@ 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);
210+
self.manager.transactions_confirmed(&header, &txdata, self.height as u32);
211+
self.manager.best_block_updated(&header, self.height as u32);
212212
(*self.monitor).block_connected(&header, &txdata, self.height as u32);
213213
if self.header_hashes.len() > self.height {
214214
self.header_hashes[self.height] = (header.block_hash(), self.blocks_connected);

lightning/src/chain/chainmonitor.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ where C::Target: chain::Filter,
9494
/// [`ChannelMonitor::transactions_confirmed`].
9595
///
9696
/// 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.
97+
/// blocks. May be called before or after [`best_block_updated`] for transactions in the
98+
/// corresponding block. See [`best_block_updated`] for further calling expectations.
9999
///
100100
/// [`block_connected`]: Self::block_connected
101-
/// [`update_best_block`]: Self::update_best_block
101+
/// [`best_block_updated`]: Self::best_block_updated
102102
pub fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
103103
self.process_chain_data(header, txdata, |monitor, txdata| {
104104
monitor.transactions_confirmed(
@@ -108,7 +108,7 @@ where C::Target: chain::Filter,
108108

109109
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
110110
/// of a channel and reacting accordingly based on the new chain tip. For details, see
111-
/// [`ChannelMonitor::update_best_block`].
111+
/// [`ChannelMonitor::best_block_updated`].
112112
///
113113
/// Used instead of [`block_connected`] by clients that are notified of transactions rather than
114114
/// blocks. May be called before or after [`transactions_confirmed`] for the corresponding
@@ -122,12 +122,12 @@ where C::Target: chain::Filter,
122122
/// [`block_connected`]: Self::block_connected
123123
/// [`transactions_confirmed`]: Self::transactions_confirmed
124124
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
125-
pub fn update_best_block(&self, header: &BlockHeader, height: u32) {
125+
pub fn best_block_updated(&self, header: &BlockHeader, height: u32) {
126126
self.process_chain_data(header, &[], |monitor, txdata| {
127127
// While in practice there shouldn't be any recursive calls when given empty txdata,
128128
// it's still possible if a chain::Filter implementation returns a transaction.
129129
debug_assert!(txdata.is_empty());
130-
monitor.update_best_block(
130+
monitor.best_block_updated(
131131
header, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
132132
});
133133
}
@@ -187,11 +187,11 @@ where C::Target: chain::Filter,
187187
/// [`ChannelMonitor::transaction_unconfirmed`] for details.
188188
///
189189
/// 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.
190+
/// than blocks. May be called before or after [`best_block_updated`] for transactions in the
191+
/// corresponding block. See [`best_block_updated`] for further calling expectations.
192192
///
193193
/// [`block_disconnected`]: Self::block_disconnected
194-
/// [`update_best_block`]: Self::update_best_block
194+
/// [`best_block_updated`]: Self::best_block_updated
195195
pub fn transaction_unconfirmed(&self, txid: &Txid) {
196196
let monitors = self.monitors.read().unwrap();
197197
for monitor in monitors.values() {

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>

lightning/src/ln/channel.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3600,7 +3600,7 @@ impl<Signer: Sign> Channel<Signer> {
36003600
}
36013601
}
36023602
// If we allow 1-conf funding, we may need to check for funding_locked here and
3603-
// send it immediately instead of waiting for an update_best_block call (which
3603+
// send it immediately instead of waiting for a best_block_updated call (which
36043604
// may have already happened for this block).
36053605
if let Some(funding_locked) = self.check_get_funding_locked(height) {
36063606
return Ok(Some(funding_locked));
@@ -3631,7 +3631,7 @@ impl<Signer: Sign> Channel<Signer> {
36313631
///
36323632
/// May return some HTLCs (and their payment_hash) which have timed out and should be failed
36333633
/// back.
3634-
pub fn update_best_block(&mut self, height: u32, highest_header_time: u32) -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> {
3634+
pub fn best_block_updated(&mut self, height: u32, highest_header_time: u32) -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> {
36353635
let mut timed_out_htlcs = Vec::new();
36363636
let unforwarded_htlc_cltv_limit = height + HTLC_FAIL_BACK_BUFFER;
36373637
self.holding_cell_htlc_updates.retain(|htlc_update| {
@@ -3683,14 +3683,14 @@ impl<Signer: Sign> Channel<Signer> {
36833683
/// before the channel has reached funding_locked and we can just wait for more blocks.
36843684
pub fn funding_transaction_unconfirmed(&mut self) -> Result<(), msgs::ErrorMessage> {
36853685
if self.funding_tx_confirmation_height != 0 {
3686-
// We handle the funding disconnection by calling update_best_block with a height one
3686+
// We handle the funding disconnection by calling best_block_updated with a height one
36873687
// below where our funding was connected, implying a reorg back to conf_height - 1.
36883688
let reorg_height = self.funding_tx_confirmation_height - 1;
36893689
// We use the time field to bump the current time we set on channel updates if its
36903690
// larger. If we don't know that time has moved forward, we can just set it to the last
36913691
// time we saw and it will be ignored.
36923692
let best_time = self.update_time_counter;
3693-
match self.update_best_block(reorg_height, best_time) {
3693+
match self.best_block_updated(reorg_height, best_time) {
36943694
Ok((funding_locked, timed_out_htlcs)) => {
36953695
assert!(funding_locked.is_none(), "We can't generate a funding with 0 confirmations?");
36963696
assert!(timed_out_htlcs.is_empty(), "We can't have accepted HTLCs with a timeout before our funding confirmation?");

0 commit comments

Comments
 (0)