Skip to content

Commit 7f177bb

Browse files
authored
Merge pull request #2797 from dunxen/2023-12-purgetochannelid
Remove `Outpoint::to_channel_id` method
2 parents 51d9ee3 + cf2c278 commit 7f177bb

24 files changed

+337
-232
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ lightning-rapid-gossip-sync/res/full_graph.lngossip
1313
lightning-custom-message/target
1414
lightning-transaction-sync/target
1515
no-std-check/target
16+
msrv-no-dev-deps-check/target

fuzz/src/chanmon_consistency.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget,
4040
use lightning::sign::{KeyMaterial, InMemorySigner, Recipient, EntropySource, NodeSigner, SignerProvider};
4141
use lightning::events;
4242
use lightning::events::MessageSendEventsProvider;
43-
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
43+
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
4444
use lightning::ln::channelmanager::{ChainParameters, ChannelDetails, ChannelManager, PaymentSendFailure, ChannelManagerReadArgs, PaymentId, RecipientOnionFields};
4545
use lightning::ln::channel::FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
4646
use lightning::ln::msgs::{self, CommitmentUpdate, ChannelMessageHandler, DecodeError, UpdateAddHTLC, Init};
@@ -191,7 +191,7 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
191191
self.chain_monitor.update_channel(funding_txo, update)
192192
}
193193

194-
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)> {
194+
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)> {
195195
return self.chain_monitor.release_pending_monitor_events();
196196
}
197197
}

fuzz/src/full_stack.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ use bitcoin::consensus::encode::deserialize;
2222
use bitcoin::network::constants::Network;
2323

2424
use bitcoin::hashes::hex::FromHex;
25-
use bitcoin::hashes::Hash as TraitImport;
26-
use bitcoin::hashes::HashEngine as TraitImportEngine;
25+
use bitcoin::hashes::Hash as _;
2726
use bitcoin::hashes::sha256::Hash as Sha256;
2827
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
2928
use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
@@ -651,7 +650,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
651650
if let None = loss_detector.txids_confirmed.get(&funding_txid) {
652651
let outpoint = OutPoint { txid: funding_txid, index: 0 };
653652
for chan in channelmanager.list_channels() {
654-
if chan.channel_id == outpoint.to_channel_id() {
653+
if chan.funding_txo == Some(outpoint) {
655654
tx.version += 1;
656655
continue 'search_loop;
657656
}

lightning-background-processor/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ mod tests {
929929
use lightning::chain::transaction::OutPoint;
930930
use lightning::events::{Event, PathFailure, MessageSendEventsProvider, MessageSendEvent};
931931
use lightning::{get_event_msg, get_event};
932-
use lightning::ln::PaymentHash;
932+
use lightning::ln::{PaymentHash, ChannelId};
933933
use lightning::ln::channelmanager;
934934
use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, MIN_CLTV_EXPIRY_DELTA, PaymentId};
935935
use lightning::ln::features::{ChannelFeatures, NodeFeatures};
@@ -1414,7 +1414,7 @@ mod tests {
14141414
}
14151415

14161416
// Force-close the channel.
1417-
nodes[0].node.force_close_broadcasting_latest_txn(&OutPoint { txid: tx.txid(), index: 0 }.to_channel_id(), &nodes[1].node.get_our_node_id()).unwrap();
1417+
nodes[0].node.force_close_broadcasting_latest_txn(&ChannelId::v1_from_funding_outpoint(OutPoint { txid: tx.txid(), index: 0 }), &nodes[1].node.get_our_node_id()).unwrap();
14181418

14191419
// Check that the force-close updates are persisted.
14201420
check_persisted_data!(nodes[0].node, filepath.clone());

lightning-persister/src/fs_store.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ mod tests {
450450
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
451451
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
452452
let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
453-
let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
453+
let update_id = update_map.get(&added_monitors[0].1.channel_id()).unwrap();
454454

455455
// Set the store's directory to read-only, which should result in
456456
// returning an unrecoverable failure when we then attempt to persist a
@@ -489,7 +489,7 @@ mod tests {
489489
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
490490
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
491491
let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
492-
let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
492+
let update_id = update_map.get(&added_monitors[0].1.channel_id()).unwrap();
493493

494494
// Create the store with an invalid directory name and test that the
495495
// channel fails to open because the directories fail to be created. There

lightning/src/chain/chainmonitor.rs

+27-13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
3131
use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
3232
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, Balance, MonitorEvent, TransactionOutputs, WithChannelMonitor, LATENCY_GRACE_PERIOD_BLOCKS};
3333
use crate::chain::transaction::{OutPoint, TransactionData};
34+
use crate::ln::ChannelId;
3435
use crate::sign::ecdsa::WriteableEcdsaChannelSigner;
3536
use crate::events;
3637
use crate::events::{Event, EventHandler};
@@ -158,7 +159,7 @@ pub trait Persist<ChannelSigner: WriteableEcdsaChannelSigner> {
158159
///
159160
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
160161
/// [`Writeable::write`]: crate::util::ser::Writeable::write
161-
fn persist_new_channel(&self, channel_id: OutPoint, data: &ChannelMonitor<ChannelSigner>, update_id: MonitorUpdateId) -> ChannelMonitorUpdateStatus;
162+
fn persist_new_channel(&self, channel_funding_outpoint: OutPoint, data: &ChannelMonitor<ChannelSigner>, update_id: MonitorUpdateId) -> ChannelMonitorUpdateStatus;
162163

163164
/// Update one channel's data. The provided [`ChannelMonitor`] has already applied the given
164165
/// update.
@@ -193,7 +194,7 @@ pub trait Persist<ChannelSigner: WriteableEcdsaChannelSigner> {
193194
/// [`ChannelMonitorUpdateStatus`] for requirements when returning errors.
194195
///
195196
/// [`Writeable::write`]: crate::util::ser::Writeable::write
196-
fn update_persisted_channel(&self, channel_id: OutPoint, update: Option<&ChannelMonitorUpdate>, data: &ChannelMonitor<ChannelSigner>, update_id: MonitorUpdateId) -> ChannelMonitorUpdateStatus;
197+
fn update_persisted_channel(&self, channel_funding_outpoint: OutPoint, update: Option<&ChannelMonitorUpdate>, data: &ChannelMonitor<ChannelSigner>, update_id: MonitorUpdateId) -> ChannelMonitorUpdateStatus;
197198
}
198199

199200
struct MonitorHolder<ChannelSigner: WriteableEcdsaChannelSigner> {
@@ -287,7 +288,7 @@ pub struct ChainMonitor<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T:
287288
persister: P,
288289
/// "User-provided" (ie persistence-completion/-failed) [`MonitorEvent`]s. These came directly
289290
/// from the user and not from a [`ChannelMonitor`].
290-
pending_monitor_events: Mutex<Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)>>,
291+
pending_monitor_events: Mutex<Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)>>,
291292
/// The best block height seen, used as a proxy for the passage of time.
292293
highest_chain_height: AtomicUsize,
293294

@@ -471,12 +472,15 @@ where C::Target: chain::Filter,
471472
}
472473
}
473474

474-
/// Lists the funding outpoint of each [`ChannelMonitor`] being monitored.
475+
/// Lists the funding outpoint and channel ID of each [`ChannelMonitor`] being monitored.
475476
///
476477
/// Note that [`ChannelMonitor`]s are not removed when a channel is closed as they are always
477478
/// monitoring for on-chain state resolutions.
478-
pub fn list_monitors(&self) -> Vec<OutPoint> {
479-
self.monitors.read().unwrap().keys().map(|outpoint| *outpoint).collect()
479+
pub fn list_monitors(&self) -> Vec<(OutPoint, ChannelId)> {
480+
self.monitors.read().unwrap().iter().map(|(outpoint, monitor_holder)| {
481+
let channel_id = monitor_holder.monitor.channel_id();
482+
(*outpoint, channel_id)
483+
}).collect()
480484
}
481485

482486
#[cfg(not(c_bindings))]
@@ -542,8 +546,9 @@ where C::Target: chain::Filter,
542546
// Completed event.
543547
return Ok(());
544548
}
545-
self.pending_monitor_events.lock().unwrap().push((funding_txo, vec![MonitorEvent::Completed {
546-
funding_txo,
549+
let channel_id = monitor_data.monitor.channel_id();
550+
self.pending_monitor_events.lock().unwrap().push((funding_txo, channel_id, vec![MonitorEvent::Completed {
551+
funding_txo, channel_id,
547552
monitor_update_id: monitor_data.monitor.get_latest_update_id(),
548553
}], monitor_data.monitor.get_counterparty_node_id()));
549554
},
@@ -565,9 +570,14 @@ where C::Target: chain::Filter,
565570
#[cfg(any(test, fuzzing))]
566571
pub fn force_channel_monitor_updated(&self, funding_txo: OutPoint, monitor_update_id: u64) {
567572
let monitors = self.monitors.read().unwrap();
568-
let counterparty_node_id = monitors.get(&funding_txo).and_then(|m| m.monitor.get_counterparty_node_id());
569-
self.pending_monitor_events.lock().unwrap().push((funding_txo, vec![MonitorEvent::Completed {
573+
let (counterparty_node_id, channel_id) = if let Some(m) = monitors.get(&funding_txo) {
574+
(m.monitor.get_counterparty_node_id(), m.monitor.channel_id())
575+
} else {
576+
(None, ChannelId::v1_from_funding_outpoint(funding_txo))
577+
};
578+
self.pending_monitor_events.lock().unwrap().push((funding_txo, channel_id, vec![MonitorEvent::Completed {
570579
funding_txo,
580+
channel_id,
571581
monitor_update_id,
572582
}], counterparty_node_id));
573583
self.event_notifier.notify();
@@ -753,11 +763,14 @@ where C::Target: chain::Filter,
753763
}
754764

755765
fn update_channel(&self, funding_txo: OutPoint, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus {
766+
// `ChannelMonitorUpdate`'s `channel_id` is `None` prior to 0.0.121 and all channels in those
767+
// versions are V1-established. For 0.0.121+ the `channel_id` fields is always `Some`.
768+
let channel_id = update.channel_id.unwrap_or(ChannelId::v1_from_funding_outpoint(funding_txo));
756769
// Update the monitor that watches the channel referred to by the given outpoint.
757770
let monitors = self.monitors.read().unwrap();
758771
match monitors.get(&funding_txo) {
759772
None => {
760-
let logger = WithContext::from(&self.logger, update.counterparty_node_id, Some(funding_txo.to_channel_id()));
773+
let logger = WithContext::from(&self.logger, update.counterparty_node_id, Some(channel_id));
761774
log_error!(logger, "Failed to update channel monitor: no such monitor registered");
762775

763776
// We should never ever trigger this from within ChannelManager. Technically a
@@ -815,7 +828,7 @@ where C::Target: chain::Filter,
815828
}
816829
}
817830

818-
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)> {
831+
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)> {
819832
let mut pending_monitor_events = self.pending_monitor_events.lock().unwrap().split_off(0);
820833
for monitor_state in self.monitors.read().unwrap().values() {
821834
let logger = WithChannelMonitor::from(&self.logger, &monitor_state.monitor);
@@ -829,8 +842,9 @@ where C::Target: chain::Filter,
829842
let monitor_events = monitor_state.monitor.get_and_clear_pending_monitor_events();
830843
if monitor_events.len() > 0 {
831844
let monitor_outpoint = monitor_state.monitor.get_funding_txo().0;
845+
let monitor_channel_id = monitor_state.monitor.channel_id();
832846
let counterparty_node_id = monitor_state.monitor.get_counterparty_node_id();
833-
pending_monitor_events.push((monitor_outpoint, monitor_events, counterparty_node_id));
847+
pending_monitor_events.push((monitor_outpoint, monitor_channel_id, monitor_events, counterparty_node_id));
834848
}
835849
}
836850
}

0 commit comments

Comments
 (0)