Skip to content

Commit 7fc8a56

Browse files
committed
Remove Outpoint::to_channel_id method
To avoid confusion and for accuracy going forward, we remove this method as it is inconsistent with channel IDs generated during V2 channel establishment. If one wants to create a V1, funding outpoint-based channel ID, then `ChannelId::v1_from_funding_outpoint` should be used instead. A large portion of the library has always made the assumption that having the funding outpoint will always allow us to generate the channel ID. This will not be the case anymore and we need to pass the channel ID along where appropriate. All channels that could have been persisted up to this point could only have used V1 establishment, so if some structures don't store a channel ID for them they can safely fall back to the funding outpoint-based version.
1 parent 4bab9c8 commit 7fc8a56

24 files changed

+346
-249
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

+6-4
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};
@@ -176,7 +176,7 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
176176
self.chain_monitor.watch_channel(funding_txo, monitor)
177177
}
178178

179-
fn update_channel(&self, funding_txo: OutPoint, update: &channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
179+
fn update_channel(&self, funding_txo: OutPoint, channel_id: ChannelId, update: &channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
180180
let mut map_lock = self.latest_monitors.lock().unwrap();
181181
let mut map_entry = match map_lock.entry(funding_txo) {
182182
hash_map::Entry::Occupied(entry) => entry,
@@ -188,10 +188,10 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
188188
let mut ser = VecWriter(Vec::new());
189189
deserialized_monitor.write(&mut ser).unwrap();
190190
map_entry.insert((update.update_id, ser.0));
191-
self.chain_monitor.update_channel(funding_txo, update)
191+
self.chain_monitor.update_channel(funding_txo, channel_id, 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
}
@@ -704,7 +704,9 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
704704
lock_fundings!(nodes);
705705

706706
let chan_a = nodes[0].list_usable_channels()[0].short_channel_id.unwrap();
707+
let chan_1_id = nodes[0].list_usable_channels()[0].channel_id;
707708
let chan_b = nodes[2].list_usable_channels()[0].short_channel_id.unwrap();
709+
let chan_2_id = nodes[2].list_usable_channels()[0].channel_id;
708710

709711
let mut payment_id: u8 = 0;
710712
let mut payment_idx: u64 = 0;

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.get_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.get_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

+25-14
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.get_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.get_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.get_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();
@@ -752,12 +762,12 @@ where C::Target: chain::Filter,
752762
Ok(persist_res)
753763
}
754764

755-
fn update_channel(&self, funding_txo: OutPoint, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus {
765+
fn update_channel(&self, funding_txo: OutPoint, channel_id: ChannelId, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus {
756766
// Update the monitor that watches the channel referred to by the given outpoint.
757767
let monitors = self.monitors.read().unwrap();
758768
match monitors.get(&funding_txo) {
759769
None => {
760-
let logger = WithContext::from(&self.logger, update.counterparty_node_id, Some(funding_txo.to_channel_id()));
770+
let logger = WithContext::from(&self.logger, update.counterparty_node_id, Some(channel_id));
761771
log_error!(logger, "Failed to update channel monitor: no such monitor registered");
762772

763773
// We should never ever trigger this from within ChannelManager. Technically a
@@ -815,7 +825,7 @@ where C::Target: chain::Filter,
815825
}
816826
}
817827

818-
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)> {
828+
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)> {
819829
let mut pending_monitor_events = self.pending_monitor_events.lock().unwrap().split_off(0);
820830
for monitor_state in self.monitors.read().unwrap().values() {
821831
let logger = WithChannelMonitor::from(&self.logger, &monitor_state.monitor);
@@ -829,8 +839,9 @@ where C::Target: chain::Filter,
829839
let monitor_events = monitor_state.monitor.get_and_clear_pending_monitor_events();
830840
if monitor_events.len() > 0 {
831841
let monitor_outpoint = monitor_state.monitor.get_funding_txo().0;
842+
let monitor_channel_id = monitor_state.monitor.get_channel_id();
832843
let counterparty_node_id = monitor_state.monitor.get_counterparty_node_id();
833-
pending_monitor_events.push((monitor_outpoint, monitor_events, counterparty_node_id));
844+
pending_monitor_events.push((monitor_outpoint, monitor_channel_id, monitor_events, counterparty_node_id));
834845
}
835846
}
836847
}

0 commit comments

Comments
 (0)