Skip to content

Commit e60c94f

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 0f99fa5 commit e60c94f

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;
@@ -348,6 +348,7 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
348348
their_shutdown_scriptpubkey: Option<Script>,
349349

350350
channel_monitor: ChannelMonitor<ChanSigner>,
351+
commitment_secrets: CounterpartyCommitmentSecrets,
351352

352353
network_sync: UpdateStatus,
353354

@@ -523,6 +524,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
523524
their_shutdown_scriptpubkey: None,
524525

525526
channel_monitor: channel_monitor,
527+
commitment_secrets: CounterpartyCommitmentSecrets::new(),
526528

527529
network_sync: UpdateStatus::Fresh,
528530

@@ -743,6 +745,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
743745
their_shutdown_scriptpubkey,
744746

745747
channel_monitor: channel_monitor,
748+
commitment_secrets: CounterpartyCommitmentSecrets::new(),
746749

747750
network_sync: UpdateStatus::Fresh,
748751

@@ -1451,7 +1454,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14511454
// channel.
14521455
return Err(ChannelError::Close("Received funding_created after we got the channel!"));
14531456
}
1454-
if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
1457+
if self.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
14551458
self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
14561459
self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
14571460
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
@@ -1493,7 +1496,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14931496
if self.channel_state & !(ChannelState::MonitorUpdateFailed as u32) != ChannelState::FundingCreated as u32 {
14941497
return Err(ChannelError::Close("Received funding_signed in strange state!"));
14951498
}
1496-
if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
1499+
if self.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
14971500
self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER - 1 ||
14981501
self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
14991502
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
@@ -2002,8 +2005,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
20022005
return Err(ChannelError::Close("Got a revoke commitment secret which didn't correspond to their current pubkey"));
20032006
}
20042007
}
2008+
self.commitment_secrets.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)
2009+
.map_err(|_| ChannelError::Close("Previous secrets did not match new one"))?;
20052010
self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)
2006-
.map_err(|e| ChannelError::Close(e.0))?;
2011+
.unwrap();
20072012

20082013
if self.channel_state & ChannelState::AwaitingRemoteRevoke as u32 == 0 {
20092014
// Our counterparty seems to have burned their coins to us (by revoking a state when we
@@ -3198,7 +3203,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
31983203
if self.channel_state != (ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32) {
31993204
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)");
32003205
}
3201-
if self.channel_monitor.get_min_seen_secret() != (1 << 48) ||
3206+
if self.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
32023207
self.cur_remote_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
32033208
self.cur_local_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
32043209
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
@@ -3277,7 +3282,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
32773282
assert_eq!(self.channel_state & ChannelState::PeerDisconnected as u32, ChannelState::PeerDisconnected as u32);
32783283
assert_ne!(self.cur_remote_commitment_transaction_number, INITIAL_COMMITMENT_NUMBER);
32793284
let data_loss_protect = if self.cur_remote_commitment_transaction_number + 1 < INITIAL_COMMITMENT_NUMBER {
3280-
let remote_last_secret = self.channel_monitor.get_secret(self.cur_remote_commitment_transaction_number + 2).unwrap();
3285+
let remote_last_secret = self.commitment_secrets.get_secret(self.cur_remote_commitment_transaction_number + 2).unwrap();
32813286
log_trace!(self, "Enough info to generate a Data Loss Protect with per_commitment_secret {}", log_bytes!(remote_last_secret));
32823287
OptionalField::Present(DataLossProtect {
32833288
your_last_per_commitment_secret: remote_last_secret,
@@ -3835,6 +3840,8 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
38353840

38363841
write_option!(self.their_shutdown_scriptpubkey);
38373842

3843+
self.commitment_secrets.write(writer)?;
3844+
38383845
self.channel_monitor.write_for_disk(writer)?;
38393846
Ok(())
38403847
}
@@ -3984,6 +3991,8 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
39843991
let their_node_id = Readable::read(reader)?;
39853992

39863993
let their_shutdown_scriptpubkey = Readable::read(reader)?;
3994+
let commitment_secrets = Readable::read(reader)?;
3995+
39873996
let (monitor_last_block, channel_monitor) = ReadableArgs::read(reader, logger.clone())?;
39883997
// We drop the ChannelMonitor's last block connected hash cause we don't actually bother
39893998
// doing full block connection operations on the internal ChannelMonitor copies
@@ -4059,6 +4068,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
40594068
their_shutdown_scriptpubkey,
40604069

40614070
channel_monitor,
4071+
commitment_secrets,
40624072

40634073
network_sync: UpdateStatus::Fresh,
40644074

0 commit comments

Comments
 (0)