Skip to content

Commit 6397a45

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 e1897de commit 6397a45

26 files changed

+448
-349
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget,
3838
use lightning::sign::{KeyMaterial, InMemorySigner, Recipient, EntropySource, NodeSigner, SignerProvider};
3939
use lightning::events;
4040
use lightning::events::MessageSendEventsProvider;
41-
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
41+
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
4242
use lightning::ln::channelmanager::{ChainParameters, ChannelDetails, ChannelManager, PaymentSendFailure, ChannelManagerReadArgs, PaymentId, RecipientOnionFields};
4343
use lightning::ln::channel::FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
4444
use lightning::ln::msgs::{self, CommitmentUpdate, ChannelMessageHandler, DecodeError, UpdateAddHTLC, Init};
@@ -138,16 +138,16 @@ impl TestChainMonitor {
138138
}
139139
}
140140
impl chain::Watch<TestChannelSigner> for TestChainMonitor {
141-
fn watch_channel(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<TestChannelSigner>) -> Result<chain::ChannelMonitorUpdateStatus, ()> {
141+
fn watch_channel(&self, funding_txo: OutPoint, channel_id: ChannelId, monitor: channelmonitor::ChannelMonitor<TestChannelSigner>) -> Result<chain::ChannelMonitorUpdateStatus, ()> {
142142
let mut ser = VecWriter(Vec::new());
143143
monitor.write(&mut ser).unwrap();
144144
if let Some(_) = self.latest_monitors.lock().unwrap().insert(funding_txo, (monitor.get_latest_update_id(), ser.0)) {
145145
panic!("Already had monitor pre-watch_channel");
146146
}
147-
self.chain_monitor.watch_channel(funding_txo, monitor)
147+
self.chain_monitor.watch_channel(funding_txo, channel_id, monitor)
148148
}
149149

150-
fn update_channel(&self, funding_txo: OutPoint, update: &channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
150+
fn update_channel(&self, funding_txo: OutPoint, channel_id: ChannelId, update: &channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
151151
let mut map_lock = self.latest_monitors.lock().unwrap();
152152
let mut map_entry = match map_lock.entry(funding_txo) {
153153
hash_map::Entry::Occupied(entry) => entry,
@@ -159,10 +159,10 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
159159
let mut ser = VecWriter(Vec::new());
160160
deserialized_monitor.write(&mut ser).unwrap();
161161
map_entry.insert((update.update_id, ser.0));
162-
self.chain_monitor.update_channel(funding_txo, update)
162+
self.chain_monitor.update_channel(funding_txo, channel_id, update)
163163
}
164164

165-
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)> {
165+
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Option<ChannelId>, Vec<MonitorEvent>, Option<PublicKey>)> {
166166
return self.chain_monitor.release_pending_monitor_events();
167167
}
168168
}
@@ -510,7 +510,8 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
510510

511511
let res = (<(BlockHash, ChanMan)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, chain_monitor.clone());
512512
for (funding_txo, mon) in monitors.drain() {
513-
assert_eq!(chain_monitor.chain_monitor.watch_channel(funding_txo, mon),
513+
let channel_id = mon.get_channel_id().unwrap_or(ChannelId::v1_from_funding_outpoint(funding_txo));
514+
assert_eq!(chain_monitor.chain_monitor.watch_channel(funding_txo, channel_id, mon),
514515
Ok(ChannelMonitorUpdateStatus::Completed));
515516
}
516517
res
@@ -675,7 +676,9 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
675676
lock_fundings!(nodes);
676677

677678
let chan_a = nodes[0].list_usable_channels()[0].short_channel_id.unwrap();
679+
let chan_1_id = Some(nodes[0].list_usable_channels()[0].channel_id);
678680
let chan_b = nodes[2].list_usable_channels()[0].short_channel_id.unwrap();
681+
let chan_2_id = Some(nodes[2].list_usable_channels()[0].channel_id);
679682

680683
let mut payment_id: u8 = 0;
681684
let mut payment_idx: u64 = 0;
@@ -1031,25 +1034,25 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
10311034

10321035
0x08 => {
10331036
if let Some((id, _)) = monitor_a.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1034-
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
1037+
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, chan_1_id, *id);
10351038
nodes[0].process_monitor_events();
10361039
}
10371040
},
10381041
0x09 => {
10391042
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1040-
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
1043+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, chan_1_id, *id);
10411044
nodes[1].process_monitor_events();
10421045
}
10431046
},
10441047
0x0a => {
10451048
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1046-
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
1049+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, chan_2_id, *id);
10471050
nodes[1].process_monitor_events();
10481051
}
10491052
},
10501053
0x0b => {
10511054
if let Some((id, _)) = monitor_c.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1052-
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
1055+
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, chan_2_id, *id);
10531056
nodes[2].process_monitor_events();
10541057
}
10551058
},
@@ -1270,19 +1273,19 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
12701273
*monitor_c.persister.update_ret.lock().unwrap() = ChannelMonitorUpdateStatus::Completed;
12711274

12721275
if let Some((id, _)) = monitor_a.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1273-
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
1276+
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, chan_1_id, *id);
12741277
nodes[0].process_monitor_events();
12751278
}
12761279
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1277-
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
1280+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, chan_1_id, *id);
12781281
nodes[1].process_monitor_events();
12791282
}
12801283
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1281-
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
1284+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, chan_1_id, *id);
12821285
nodes[1].process_monitor_events();
12831286
}
12841287
if let Some((id, _)) = monitor_c.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1285-
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
1288+
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, chan_1_id, *id);
12861289
nodes[2].process_monitor_events();
12871290
}
12881291

fuzz/src/full_stack.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
622622
if let None = loss_detector.txids_confirmed.get(&funding_txid) {
623623
let outpoint = OutPoint { txid: funding_txid, index: 0 };
624624
for chan in channelmanager.list_channels() {
625-
if chan.channel_id == outpoint.to_channel_id() {
625+
if chan.channel_id == funding_generation.0 {
626626
tx.version += 1;
627627
continue 'search_loop;
628628
}

fuzz/src/utils/test_persister.rs

Lines changed: 3 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ mod tests {
896896
use lightning::chain::transaction::OutPoint;
897897
use lightning::events::{Event, PathFailure, MessageSendEventsProvider, MessageSendEvent};
898898
use lightning::{get_event_msg, get_event};
899-
use lightning::ln::PaymentHash;
899+
use lightning::ln::{PaymentHash, ChannelId};
900900
use lightning::ln::channelmanager;
901901
use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, MIN_CLTV_EXPIRY_DELTA, PaymentId};
902902
use lightning::ln::features::{ChannelFeatures, NodeFeatures};
@@ -1380,7 +1380,7 @@ mod tests {
13801380
}
13811381

13821382
// Force-close the channel.
1383-
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();
1383+
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();
13841384

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

lightning-block-sync/src/init.rs

Lines changed: 4 additions & 1 deletion
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().unwrap_or(ChannelId::v1_from_funding_outpoint(funding_outpoint));
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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ mod tests {
441441
#[cfg(not(target_os = "windows"))]
442442
#[test]
443443
fn test_readonly_dir_perm_failure() {
444+
use lightning::ln::ChannelId;
445+
444446
let store = FilesystemStore::new("test_readonly_dir_perm_failure".into());
445447
fs::create_dir_all(&store.get_data_dir()).unwrap();
446448

@@ -455,7 +457,9 @@ mod tests {
455457
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
456458
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
457459
let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
458-
let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
460+
let funding_outpoint = &added_monitors[0].0;
461+
let channel_id = &added_monitors[0].1.get_channel_id().unwrap_or(ChannelId::v1_from_funding_outpoint(*funding_outpoint));
462+
let update_id = update_map.get(&channel_id).unwrap();
459463

460464
// Set the store's directory to read-only, which should result in
461465
// returning an unrecoverable failure when we then attempt to persist a
@@ -469,7 +473,7 @@ mod tests {
469473
txid: Txid::from_str("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
470474
index: 0
471475
};
472-
match store.persist_new_channel(test_txo, &added_monitors[0].1, update_id.2) {
476+
match store.persist_new_channel(test_txo, *channel_id, &added_monitors[0].1, update_id.2) {
473477
ChannelMonitorUpdateStatus::UnrecoverableError => {},
474478
_ => panic!("unexpected result from persisting new channel")
475479
}
@@ -494,7 +498,9 @@ mod tests {
494498
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
495499
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
496500
let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
497-
let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
501+
let funding_outpoint = &added_monitors[0].0;
502+
let channel_id = &added_monitors[0].1.get_channel_id().unwrap_or(ChannelId::v1_from_funding_outpoint(*funding_outpoint));
503+
let update_id = update_map.get(*channel_id).unwrap();
498504

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

0 commit comments

Comments
 (0)