Skip to content

Commit b732865

Browse files
authored
Merge pull request #3638 from wpaulino/bye-bye-outpoint-to-peer
Require counterparty_node_id TLV for ChannelMonitor
2 parents 3a5f428 + fa4ff0c commit b732865

10 files changed

+224
-487
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
282282

283283
fn release_pending_monitor_events(
284284
&self,
285-
) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)> {
285+
) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, PublicKey)> {
286286
return self.chain_monitor.release_pending_monitor_events();
287287
}
288288
}

lightning/src/chain/chainmonitor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ pub struct ChainMonitor<ChannelSigner: EcdsaChannelSigner, C: Deref, T: Deref, F
246246
persister: P,
247247
/// "User-provided" (ie persistence-completion/-failed) [`MonitorEvent`]s. These came directly
248248
/// from the user and not from a [`ChannelMonitor`].
249-
pending_monitor_events: Mutex<Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)>>,
249+
pending_monitor_events: Mutex<Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, PublicKey)>>,
250250
/// The best block height seen, used as a proxy for the passage of time.
251251
highest_chain_height: AtomicUsize,
252252

@@ -804,7 +804,7 @@ where C::Target: chain::Filter,
804804
let monitors = self.monitors.read().unwrap();
805805
match monitors.get(&channel_id) {
806806
None => {
807-
let logger = WithContext::from(&self.logger, update.counterparty_node_id, Some(channel_id), None);
807+
let logger = WithContext::from(&self.logger, None, Some(channel_id), None);
808808
log_error!(logger, "Failed to update channel monitor: no such monitor registered");
809809

810810
// We should never ever trigger this from within ChannelManager. Technically a
@@ -874,7 +874,7 @@ where C::Target: chain::Filter,
874874
}
875875
}
876876

877-
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)> {
877+
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, PublicKey)> {
878878
let mut pending_monitor_events = self.pending_monitor_events.lock().unwrap().split_off(0);
879879
for monitor_state in self.monitors.read().unwrap().values() {
880880
let monitor_events = monitor_state.monitor.get_and_clear_pending_monitor_events();

lightning/src/chain/channelmonitor.rs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,6 @@ use crate::sync::{Mutex, LockTestExt};
7474
#[must_use]
7575
pub struct ChannelMonitorUpdate {
7676
pub(crate) updates: Vec<ChannelMonitorUpdateStep>,
77-
/// Historically, [`ChannelMonitor`]s didn't know their counterparty node id. However,
78-
/// `ChannelManager` really wants to know it so that it can easily look up the corresponding
79-
/// channel. For now, this results in a temporary map in `ChannelManager` to look up channels
80-
/// by only the funding outpoint.
81-
///
82-
/// To eventually remove that, we repeat the counterparty node id here so that we can upgrade
83-
/// `ChannelMonitor`s to become aware of the counterparty node id if they were generated prior
84-
/// to when it was stored directly in them.
85-
pub(crate) counterparty_node_id: Option<PublicKey>,
8677
/// The sequence number of this update. Updates *must* be replayed in-order according to this
8778
/// sequence number (and updates may panic if they are not). The update_id values are strictly
8879
/// increasing and increase by one for each new update, with two exceptions specified below.
@@ -117,7 +108,7 @@ impl Writeable for ChannelMonitorUpdate {
117108
update_step.write(w)?;
118109
}
119110
write_tlv_fields!(w, {
120-
(1, self.counterparty_node_id, option),
111+
// 1 was previously used to store `counterparty_node_id`
121112
(3, self.channel_id, option),
122113
});
123114
Ok(())
@@ -134,13 +125,12 @@ impl Readable for ChannelMonitorUpdate {
134125
updates.push(upd);
135126
}
136127
}
137-
let mut counterparty_node_id = None;
138128
let mut channel_id = None;
139129
read_tlv_fields!(r, {
140-
(1, counterparty_node_id, option),
130+
// 1 was previously used to store `counterparty_node_id`
141131
(3, channel_id, option),
142132
});
143-
Ok(Self { update_id, counterparty_node_id, updates, channel_id })
133+
Ok(Self { update_id, updates, channel_id })
144134
}
145135
}
146136

@@ -1020,7 +1010,7 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
10201010
best_block: BestBlock,
10211011

10221012
/// The node_id of our counterparty
1023-
counterparty_node_id: Option<PublicKey>,
1013+
counterparty_node_id: PublicKey,
10241014

10251015
/// Initial counterparty commmitment data needed to recreate the commitment tx
10261016
/// in the persistence pipeline for third-party watchtowers. This will only be present on
@@ -1242,7 +1232,7 @@ impl<Signer: EcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signer> {
12421232
(3, self.htlcs_resolved_on_chain, required_vec),
12431233
(5, pending_monitor_events, required_vec),
12441234
(7, self.funding_spend_seen, required),
1245-
(9, self.counterparty_node_id, option),
1235+
(9, self.counterparty_node_id, required),
12461236
(11, self.confirmed_commitment_tx_counterparty_output, option),
12471237
(13, self.spendable_txids_confirmed, required_vec),
12481238
(15, self.counterparty_fulfilled_htlcs, required),
@@ -1338,7 +1328,7 @@ impl<'a, L: Deref> WithChannelMonitor<'a, L> where L::Target: Logger {
13381328
}
13391329

13401330
pub(crate) fn from_impl<S: EcdsaChannelSigner>(logger: &'a L, monitor_impl: &ChannelMonitorImpl<S>, payment_hash: Option<PaymentHash>) -> Self {
1341-
let peer_id = monitor_impl.counterparty_node_id;
1331+
let peer_id = Some(monitor_impl.counterparty_node_id);
13421332
let channel_id = Some(monitor_impl.channel_id());
13431333
WithChannelMonitor {
13441334
logger, peer_id, channel_id, payment_hash,
@@ -1462,7 +1452,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
14621452
spendable_txids_confirmed: Vec::new(),
14631453

14641454
best_block,
1465-
counterparty_node_id: Some(counterparty_node_id),
1455+
counterparty_node_id: counterparty_node_id,
14661456
initial_counterparty_commitment_info: None,
14671457
balances_empty_height: None,
14681458

@@ -1788,10 +1778,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
17881778
}
17891779

17901780
/// Gets the `node_id` of the counterparty for this channel.
1791-
///
1792-
/// Will be `None` for channels constructed on LDK versions prior to 0.0.110 and always `Some`
1793-
/// otherwise.
1794-
pub fn get_counterparty_node_id(&self) -> Option<PublicKey> {
1781+
pub fn get_counterparty_node_id(&self) -> PublicKey {
17951782
self.inner.lock().unwrap().counterparty_node_id
17961783
}
17971784

@@ -3200,14 +3187,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32003187
log_funding_info!(self), self.latest_update_id, updates.update_id, updates.updates.len());
32013188
}
32023189

3203-
if updates.counterparty_node_id.is_some() {
3204-
if self.counterparty_node_id.is_none() {
3205-
self.counterparty_node_id = updates.counterparty_node_id;
3206-
} else {
3207-
debug_assert_eq!(self.counterparty_node_id, updates.counterparty_node_id);
3208-
}
3209-
}
3210-
32113190
// ChannelMonitor updates may be applied after force close if we receive a preimage for a
32123191
// broadcasted commitment transaction HTLC output that we'd like to claim on-chain. If this
32133192
// is the case, we no longer have guaranteed access to the monitor's update ID, so we use a
@@ -3376,10 +3355,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33763355
package_target_feerate_sat_per_1000_weight, commitment_tx, anchor_output_idx,
33773356
} => {
33783357
let channel_id = self.channel_id;
3379-
// unwrap safety: `ClaimEvent`s are only available for Anchor channels,
3380-
// introduced with v0.0.116. counterparty_node_id is guaranteed to be `Some`
3381-
// since v0.0.110.
3382-
let counterparty_node_id = self.counterparty_node_id.unwrap();
3358+
let counterparty_node_id = self.counterparty_node_id;
33833359
let commitment_txid = commitment_tx.compute_txid();
33843360
debug_assert_eq!(self.current_holder_commitment_tx.txid, commitment_txid);
33853361
let pending_htlcs = self.current_holder_commitment_tx.non_dust_htlcs();
@@ -3410,10 +3386,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34103386
target_feerate_sat_per_1000_weight, htlcs, tx_lock_time,
34113387
} => {
34123388
let channel_id = self.channel_id;
3413-
// unwrap safety: `ClaimEvent`s are only available for Anchor channels,
3414-
// introduced with v0.0.116. counterparty_node_id is guaranteed to be `Some`
3415-
// since v0.0.110.
3416-
let counterparty_node_id = self.counterparty_node_id.unwrap();
3389+
let counterparty_node_id = self.counterparty_node_id;
34173390
let mut htlc_descriptors = Vec::with_capacity(htlcs.len());
34183391
for htlc in htlcs {
34193392
htlc_descriptors.push(HTLCDescriptor {
@@ -5129,6 +5102,13 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
51295102
chan_utils::get_to_countersignatory_with_anchors_redeemscript(&payment_point).to_p2wsh();
51305103
}
51315104

5105+
let channel_id = channel_id.unwrap_or(ChannelId::v1_from_funding_outpoint(outpoint));
5106+
if counterparty_node_id.is_none() {
5107+
panic!("Found monitor for channel {} with no updates since v0.0.118.\
5108+
These monitors are no longer supported.\
5109+
To continue, run a v0.1 release, send/route a payment over the channel or close it.", channel_id);
5110+
}
5111+
51325112
Ok((best_block.block_hash, ChannelMonitor::from_impl(ChannelMonitorImpl {
51335113
latest_update_id,
51345114
commitment_transaction_number_obscure_factor,
@@ -5140,7 +5120,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
51405120

51415121
channel_keys_id,
51425122
holder_revocation_basepoint,
5143-
channel_id: channel_id.unwrap_or(ChannelId::v1_from_funding_outpoint(outpoint)),
5123+
channel_id,
51445124
funding_info,
51455125
first_confirmed_funding_txo: first_confirmed_funding_txo.0.unwrap(),
51465126
current_counterparty_commitment_txid,
@@ -5184,7 +5164,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
51845164
spendable_txids_confirmed: spendable_txids_confirmed.unwrap(),
51855165

51865166
best_block,
5187-
counterparty_node_id,
5167+
counterparty_node_id: counterparty_node_id.unwrap(),
51885168
initial_counterparty_commitment_info,
51895169
balances_empty_height,
51905170
failed_back_htlc_ids: new_hash_set(),

lightning/src/chain/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ pub trait Watch<ChannelSigner: EcdsaChannelSigner> {
304304
///
305305
/// For details on asynchronous [`ChannelMonitor`] updating and returning
306306
/// [`MonitorEvent::Completed`] here, see [`ChannelMonitorUpdateStatus::InProgress`].
307-
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)>;
307+
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, PublicKey)>;
308308
}
309309

310310
/// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to

lightning/src/ln/async_signer_tests.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use bitcoin::transaction::Version;
1818

1919
use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS;
2020
use crate::chain::ChannelMonitorUpdateStatus;
21-
use crate::chain::transaction::OutPoint;
2221
use crate::events::bump_transaction::WalletSource;
2322
use crate::events::{ClosureReason, Event};
2423
use crate::ln::chan_utils::ClosingTransaction;
@@ -1091,9 +1090,4 @@ fn do_test_closing_signed(extra_closing_signed: bool, reconnect: bool) {
10911090
assert!(nodes[1].node.list_channels().is_empty());
10921091
check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
10931092
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
1094-
1095-
// Check that our maps have been updated after async signing channel closure.
1096-
let funding_outpoint = OutPoint { txid: funding_tx.compute_txid(), index: 0 };
1097-
assert!(nodes[0].node().outpoint_to_peer.lock().unwrap().get(&funding_outpoint).is_none());
1098-
assert!(nodes[1].node().outpoint_to_peer.lock().unwrap().get(&funding_outpoint).is_none());
10991093
}

lightning/src/ln/channel.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3457,6 +3457,18 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34573457
!matches!(self.channel_state, ChannelState::AwaitingChannelReady(flags) if flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH))
34583458
}
34593459

3460+
fn unset_funding_info(&mut self, funding: &mut FundingScope) {
3461+
debug_assert!(
3462+
matches!(self.channel_state, ChannelState::FundingNegotiated)
3463+
|| matches!(self.channel_state, ChannelState::AwaitingChannelReady(_))
3464+
);
3465+
funding.channel_transaction_parameters.funding_outpoint = None;
3466+
self.channel_id = self.temporary_channel_id.expect(
3467+
"temporary_channel_id should be set since unset_funding_info is only called on funded \
3468+
channels that were unfunded immediately beforehand"
3469+
);
3470+
}
3471+
34603472
fn validate_commitment_signed<L: Deref>(
34613473
&self, funding: &FundingScope, holder_commitment_point: &HolderCommitmentPoint,
34623474
msg: &msgs::CommitmentSigned, logger: &L,
@@ -4516,7 +4528,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
45164528
self.latest_monitor_update_id += 1;
45174529
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), ChannelMonitorUpdate {
45184530
update_id: self.latest_monitor_update_id,
4519-
counterparty_node_id: Some(self.counterparty_node_id),
45204531
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
45214532
channel_id: Some(self.channel_id()),
45224533
}))
@@ -5095,7 +5106,6 @@ impl<SP: Deref> FundedChannel<SP> where
50955106
self.context.latest_monitor_update_id += 1;
50965107
let monitor_update = ChannelMonitorUpdate {
50975108
update_id: self.context.latest_monitor_update_id,
5098-
counterparty_node_id: Some(self.context.counterparty_node_id),
50995109
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
51005110
payment_preimage: payment_preimage_arg.clone(),
51015111
payment_info,
@@ -5310,14 +5320,7 @@ impl<SP: Deref> FundedChannel<SP> where
53105320
/// Further, the channel must be immediately shut down after this with a call to
53115321
/// [`ChannelContext::force_shutdown`].
53125322
pub fn unset_funding_info(&mut self) {
5313-
debug_assert!(matches!(
5314-
self.context.channel_state, ChannelState::AwaitingChannelReady(_)
5315-
));
5316-
self.funding.channel_transaction_parameters.funding_outpoint = None;
5317-
self.context.channel_id = self.context.temporary_channel_id.expect(
5318-
"temporary_channel_id should be set since unset_funding_info is only called on funded \
5319-
channels that were unfunded immediately beforehand"
5320-
);
5323+
self.context.unset_funding_info(&mut self.funding);
53215324
}
53225325

53235326
/// Handles a channel_ready message from our peer. If we've already sent our channel_ready
@@ -5710,7 +5713,6 @@ impl<SP: Deref> FundedChannel<SP> where
57105713
self.context.latest_monitor_update_id += 1;
57115714
let mut monitor_update = ChannelMonitorUpdate {
57125715
update_id: self.context.latest_monitor_update_id,
5713-
counterparty_node_id: Some(self.context.counterparty_node_id),
57145716
updates: vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
57155717
commitment_tx,
57165718
htlc_outputs,
@@ -5792,7 +5794,6 @@ impl<SP: Deref> FundedChannel<SP> where
57925794

57935795
let mut monitor_update = ChannelMonitorUpdate {
57945796
update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
5795-
counterparty_node_id: Some(self.context.counterparty_node_id),
57965797
updates: Vec::new(),
57975798
channel_id: Some(self.context.channel_id()),
57985799
};
@@ -5985,7 +5986,6 @@ impl<SP: Deref> FundedChannel<SP> where
59855986
self.context.latest_monitor_update_id += 1;
59865987
let mut monitor_update = ChannelMonitorUpdate {
59875988
update_id: self.context.latest_monitor_update_id,
5988-
counterparty_node_id: Some(self.context.counterparty_node_id),
59895989
updates: vec![ChannelMonitorUpdateStep::CommitmentSecret {
59905990
idx: self.context.cur_counterparty_commitment_transaction_number + 1,
59915991
secret: msg.per_commitment_secret,
@@ -7257,7 +7257,6 @@ impl<SP: Deref> FundedChannel<SP> where
72577257
self.context.latest_monitor_update_id += 1;
72587258
let monitor_update = ChannelMonitorUpdate {
72597259
update_id: self.context.latest_monitor_update_id,
7260-
counterparty_node_id: Some(self.context.counterparty_node_id),
72617260
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
72627261
scriptpubkey: self.get_closing_scriptpubkey(),
72637262
}],
@@ -8543,7 +8542,6 @@ impl<SP: Deref> FundedChannel<SP> where
85438542
self.context.latest_monitor_update_id += 1;
85448543
let monitor_update = ChannelMonitorUpdate {
85458544
update_id: self.context.latest_monitor_update_id,
8546-
counterparty_node_id: Some(self.context.counterparty_node_id),
85478545
updates: vec![ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo {
85488546
commitment_txid: counterparty_commitment_txid,
85498547
htlc_outputs: htlcs.clone(),
@@ -8755,7 +8753,6 @@ impl<SP: Deref> FundedChannel<SP> where
87558753
self.context.latest_monitor_update_id += 1;
87568754
let monitor_update = ChannelMonitorUpdate {
87578755
update_id: self.context.latest_monitor_update_id,
8758-
counterparty_node_id: Some(self.context.counterparty_node_id),
87598756
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
87608757
scriptpubkey: self.get_closing_scriptpubkey(),
87618758
}],
@@ -9315,6 +9312,14 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
93159312
} else { None };
93169313
(open_channel, funding_created)
93179314
}
9315+
9316+
/// Unsets the existing funding information.
9317+
///
9318+
/// The channel must be immediately shut down after this with a call to
9319+
/// [`ChannelContext::force_shutdown`].
9320+
pub fn unset_funding_info(&mut self) {
9321+
self.context.unset_funding_info(&mut self.funding);
9322+
}
93189323
}
93199324

93209325
/// A not-yet-funded inbound (from counterparty) channel using V1 channel establishment.

0 commit comments

Comments
 (0)