Skip to content

Commit 5fa999d

Browse files
committed
Track counterparty's commitment secrets in Channel directly.
In the process of removing a local ChannelMonitor in each Channel, we need to track our counterpartys' commitment secrets so that we can check them locally instead of calling our channel monitor to do that work for us.
1 parent 2b9cb95 commit 5fa999d

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lightning/src/ln/channel.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use ln::msgs;
2020
use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
2121
use ln::channelmonitor::ChannelMonitor;
2222
use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
23-
use ln::chan_utils::{LocalCommitmentTransaction, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys};
23+
use ln::chan_utils::{CounterpartyCommitmentSecrets, LocalCommitmentTransaction, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys};
2424
use ln::chan_utils;
2525
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
2626
use chain::transaction::OutPoint;
@@ -351,6 +351,7 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
351351
their_shutdown_scriptpubkey: Option<Script>,
352352

353353
channel_monitor: ChannelMonitor<ChanSigner>,
354+
commitment_secrets: CounterpartyCommitmentSecrets,
354355

355356
network_sync: UpdateStatus,
356357

@@ -526,6 +527,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
526527
their_shutdown_scriptpubkey: None,
527528

528529
channel_monitor: channel_monitor,
530+
commitment_secrets: CounterpartyCommitmentSecrets::new(),
529531

530532
network_sync: UpdateStatus::Fresh,
531533

@@ -746,6 +748,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
746748
their_shutdown_scriptpubkey,
747749

748750
channel_monitor: channel_monitor,
751+
commitment_secrets: CounterpartyCommitmentSecrets::new(),
749752

750753
network_sync: UpdateStatus::Fresh,
751754

@@ -1454,7 +1457,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14541457
// channel.
14551458
return Err(ChannelError::Close("Received funding_created after we got the channel!"));
14561459
}
1457-
if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
1460+
if self.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
14581461
self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
14591462
self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
14601463
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
@@ -1496,7 +1499,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14961499
if self.channel_state & !(ChannelState::MonitorUpdateFailed as u32) != ChannelState::FundingCreated as u32 {
14971500
return Err(ChannelError::Close("Received funding_signed in strange state!"));
14981501
}
1499-
if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
1502+
if self.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
15001503
self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER - 1 ||
15011504
self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
15021505
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
@@ -2005,8 +2008,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
20052008
return Err(ChannelError::Close("Got a revoke commitment secret which didn't correspond to their current pubkey"));
20062009
}
20072010
}
2011+
self.commitment_secrets.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)
2012+
.map_err(|_| ChannelError::Close("Previous secrets did not match new one"))?;
20082013
self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)
2009-
.map_err(|e| ChannelError::Close(e.0))?;
2014+
.unwrap();
20102015

20112016
if self.channel_state & ChannelState::AwaitingRemoteRevoke as u32 == 0 {
20122017
// Our counterparty seems to have burned their coins to us (by revoking a state when we
@@ -3214,7 +3219,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
32143219
if self.channel_state != (ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32) {
32153220
panic!("Tried to get a funding_created messsage at a time other than immediately after initial handshake completion (or tried to get funding_created twice)");
32163221
}
3217-
if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
3222+
if self.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
32183223
self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
32193224
self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
32203225
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
@@ -3293,7 +3298,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
32933298
assert_eq!(self.channel_state & ChannelState::PeerDisconnected as u32, ChannelState::PeerDisconnected as u32);
32943299
assert_ne!(self.cur_remote_commitment_transaction_number, INITIAL_COMMITMENT_NUMBER);
32953300
let data_loss_protect = if self.cur_remote_commitment_transaction_number + 1 < INITIAL_COMMITMENT_NUMBER {
3296-
let remote_last_secret = self.channel_monitor.get_secret(self.cur_remote_commitment_transaction_number + 2).unwrap();
3301+
let remote_last_secret = self.commitment_secrets.get_secret(self.cur_remote_commitment_transaction_number + 2).unwrap();
32973302
log_trace!(self, "Enough info to generate a Data Loss Protect with per_commitment_secret {}", log_bytes!(remote_last_secret));
32983303
OptionalField::Present(DataLossProtect {
32993304
your_last_per_commitment_secret: remote_last_secret,
@@ -3848,6 +3853,8 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
38483853

38493854
write_option!(self.their_shutdown_scriptpubkey);
38503855

3856+
self.commitment_secrets.write(writer)?;
3857+
38513858
self.channel_monitor.write_for_disk(writer)?;
38523859
Ok(())
38533860
}
@@ -3997,6 +4004,8 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
39974004
let their_node_id = Readable::read(reader)?;
39984005

39994006
let their_shutdown_scriptpubkey = Readable::read(reader)?;
4007+
let commitment_secrets = Readable::read(reader)?;
4008+
40004009
let (monitor_last_block, channel_monitor) = ReadableArgs::read(reader, logger.clone())?;
40014010
// We drop the ChannelMonitor's last block connected hash cause we don't actually bother
40024011
// doing full block connection operations on the internal ChannelMonitor copies
@@ -4072,6 +4081,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
40724081
their_shutdown_scriptpubkey,
40734082

40744083
channel_monitor,
4084+
commitment_secrets,
40754085

40764086
network_sync: UpdateStatus::Fresh,
40774087

0 commit comments

Comments
 (0)