Skip to content

Commit d5b05bc

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 15b7f66 commit d5b05bc

26 files changed

+442
-348
lines changed

fuzz/src/chanmon_consistency.rs

+18-15
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};
@@ -167,16 +167,16 @@ impl TestChainMonitor {
167167
}
168168
}
169169
impl chain::Watch<TestChannelSigner> for TestChainMonitor {
170-
fn watch_channel(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<TestChannelSigner>) -> Result<chain::ChannelMonitorUpdateStatus, ()> {
170+
fn watch_channel(&self, funding_txo: OutPoint, channel_id: ChannelId, monitor: channelmonitor::ChannelMonitor<TestChannelSigner>) -> Result<chain::ChannelMonitorUpdateStatus, ()> {
171171
let mut ser = VecWriter(Vec::new());
172172
monitor.write(&mut ser).unwrap();
173173
if let Some(_) = self.latest_monitors.lock().unwrap().insert(funding_txo, (monitor.get_latest_update_id(), ser.0)) {
174174
panic!("Already had monitor pre-watch_channel");
175175
}
176-
self.chain_monitor.watch_channel(funding_txo, monitor)
176+
self.chain_monitor.watch_channel(funding_txo, channel_id, 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, Option<ChannelId>, Vec<MonitorEvent>, Option<PublicKey>)> {
195195
return self.chain_monitor.release_pending_monitor_events();
196196
}
197197
}
@@ -539,7 +539,8 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
539539

540540
let res = (<(BlockHash, ChanMan)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, chain_monitor.clone());
541541
for (funding_txo, mon) in monitors.drain() {
542-
assert_eq!(chain_monitor.chain_monitor.watch_channel(funding_txo, mon),
542+
let channel_id = mon.get_channel_id();
543+
assert_eq!(chain_monitor.chain_monitor.watch_channel(funding_txo, channel_id, mon),
543544
Ok(ChannelMonitorUpdateStatus::Completed));
544545
}
545546
res
@@ -704,7 +705,9 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
704705
lock_fundings!(nodes);
705706

706707
let chan_a = nodes[0].list_usable_channels()[0].short_channel_id.unwrap();
708+
let chan_1_id = Some(nodes[0].list_usable_channels()[0].channel_id);
707709
let chan_b = nodes[2].list_usable_channels()[0].short_channel_id.unwrap();
710+
let chan_2_id = Some(nodes[2].list_usable_channels()[0].channel_id);
708711

709712
let mut payment_id: u8 = 0;
710713
let mut payment_idx: u64 = 0;
@@ -1060,25 +1063,25 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
10601063

10611064
0x08 => {
10621065
if let Some((id, _)) = monitor_a.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1063-
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
1066+
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, chan_1_id, *id);
10641067
nodes[0].process_monitor_events();
10651068
}
10661069
},
10671070
0x09 => {
10681071
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1069-
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
1072+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, chan_1_id, *id);
10701073
nodes[1].process_monitor_events();
10711074
}
10721075
},
10731076
0x0a => {
10741077
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1075-
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
1078+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, chan_2_id, *id);
10761079
nodes[1].process_monitor_events();
10771080
}
10781081
},
10791082
0x0b => {
10801083
if let Some((id, _)) = monitor_c.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1081-
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
1084+
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, chan_2_id, *id);
10821085
nodes[2].process_monitor_events();
10831086
}
10841087
},
@@ -1299,19 +1302,19 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
12991302
*monitor_c.persister.update_ret.lock().unwrap() = ChannelMonitorUpdateStatus::Completed;
13001303

13011304
if let Some((id, _)) = monitor_a.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1302-
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
1305+
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, chan_1_id, *id);
13031306
nodes[0].process_monitor_events();
13041307
}
13051308
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1306-
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
1309+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, chan_1_id, *id);
13071310
nodes[1].process_monitor_events();
13081311
}
13091312
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1310-
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
1313+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, chan_1_id, *id);
13111314
nodes[1].process_monitor_events();
13121315
}
13131316
if let Some((id, _)) = monitor_c.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1314-
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
1317+
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, chan_1_id, *id);
13151318
nodes[2].process_monitor_events();
13161319
}
13171320

fuzz/src/full_stack.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
651651
if let None = loss_detector.txids_confirmed.get(&funding_txid) {
652652
let outpoint = OutPoint { txid: funding_txid, index: 0 };
653653
for chan in channelmanager.list_channels() {
654-
if chan.channel_id == outpoint.to_channel_id() {
654+
if chan.channel_id == funding_generation.0 {
655655
tx.version += 1;
656656
continue 'search_loop;
657657
}

fuzz/src/utils/test_persister.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use lightning::chain;
22
use lightning::chain::{chainmonitor, channelmonitor};
33
use lightning::chain::chainmonitor::MonitorUpdateId;
44
use lightning::chain::transaction::OutPoint;
5+
use lightning::ln::ChannelId;
56
use lightning::util::test_channel_signer::TestChannelSigner;
67

78
use std::sync::Mutex;
@@ -10,11 +11,11 @@ pub struct TestPersister {
1011
pub update_ret: Mutex<chain::ChannelMonitorUpdateStatus>,
1112
}
1213
impl chainmonitor::Persist<TestChannelSigner> for TestPersister {
13-
fn persist_new_channel(&self, _funding_txo: OutPoint, _data: &channelmonitor::ChannelMonitor<TestChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
14+
fn persist_new_channel(&self, _funding_txo: OutPoint, _channel_id: ChannelId, _data: &channelmonitor::ChannelMonitor<TestChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
1415
self.update_ret.lock().unwrap().clone()
1516
}
1617

17-
fn update_persisted_channel(&self, _funding_txo: OutPoint, _update: Option<&channelmonitor::ChannelMonitorUpdate>, _data: &channelmonitor::ChannelMonitor<TestChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
18+
fn update_persisted_channel(&self, _funding_txo: OutPoint, _channel_id: ChannelId, _update: Option<&channelmonitor::ChannelMonitorUpdate>, _data: &channelmonitor::ChannelMonitor<TestChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
1819
self.update_ret.lock().unwrap().clone()
1920
}
2021
}

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-block-sync/src/init.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ BlockSourceResult<ValidatedBlockHeader> where B::Target: BlockSource {
4949
/// use lightning::chain::chaininterface::FeeEstimator;
5050
/// use lightning::sign;
5151
/// use lightning::sign::{EntropySource, NodeSigner, SignerProvider};
52+
/// use lightning::ln::ChannelId;
5253
/// use lightning::ln::channelmanager::{ChannelManager, ChannelManagerReadArgs};
5354
/// use lightning::routing::router::Router;
5455
/// use lightning::util::config::UserConfig;
@@ -119,7 +120,9 @@ BlockSourceResult<ValidatedBlockHeader> where B::Target: BlockSource {
119120
///
120121
/// // Allow the chain monitor to watch any channels.
121122
/// let monitor = monitor_listener.0;
122-
/// chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
123+
/// let funding_outpoint = monitor.get_funding_txo().0;
124+
/// let channel_id = monitor.get_channel_id();
125+
/// chain_monitor.watch_channel(funding_outpoint, channel_id, monitor);
123126
///
124127
/// // Create an SPV client to notify the chain monitor and channel manager of block events.
125128
/// let chain_poller = poll::ChainPoller::new(block_source, Network::Bitcoin);

lightning-persister/src/fs_store.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ mod tests {
436436
#[cfg(not(target_os = "windows"))]
437437
#[test]
438438
fn test_readonly_dir_perm_failure() {
439+
use lightning::ln::ChannelId;
440+
439441
let store = FilesystemStore::new("test_readonly_dir_perm_failure".into());
440442
fs::create_dir_all(&store.get_data_dir()).unwrap();
441443

@@ -450,7 +452,9 @@ mod tests {
450452
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
451453
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
452454
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();
455+
let funding_outpoint = &added_monitors[0].0;
456+
let channel_id = &added_monitors[0].1.get_channel_id();
457+
let update_id = update_map.get(&channel_id).unwrap();
454458

455459
// Set the store's directory to read-only, which should result in
456460
// returning an unrecoverable failure when we then attempt to persist a
@@ -464,7 +468,7 @@ mod tests {
464468
txid: Txid::from_str("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
465469
index: 0
466470
};
467-
match store.persist_new_channel(test_txo, &added_monitors[0].1, update_id.2) {
471+
match store.persist_new_channel(test_txo, *channel_id, &added_monitors[0].1, update_id.2) {
468472
ChannelMonitorUpdateStatus::UnrecoverableError => {},
469473
_ => panic!("unexpected result from persisting new channel")
470474
}
@@ -489,7 +493,9 @@ mod tests {
489493
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
490494
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
491495
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();
496+
let funding_outpoint = &added_monitors[0].0;
497+
let channel_id = &added_monitors[0].1.get_channel_id();
498+
let update_id = update_map.get(*channel_id).unwrap();
493499

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

0 commit comments

Comments
 (0)