Skip to content

Commit b02ccbb

Browse files
committed
Fix sending funding_locked with 1 conf.
We previously tracked funding transaction confirmation by marking funding_tx_confirmations to 1 when we see it in a block and incrementing each block thereafter if its non-0. To avoid double-incrementing the first confirmation, we did the increment (and funding_locked check) after doing the first-confirmation checks. Thus, we'd never hit the funding_locked case during the first confirmation. To address this, we simply swap the order of the checks, though bumping the funding_tx_confirmations increment up to the top. Reported-by: Igor Cota <[email protected]>
1 parent 88b7dcd commit b02ccbb

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

lightning/src/ln/channel.rs

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,49 +2987,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
29872987
pub fn block_connected(&mut self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) -> Result<Option<msgs::FundingLocked>, msgs::ErrorMessage> {
29882988
let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
29892989
if header.bitcoin_hash() != self.last_block_connected {
2990-
self.last_block_connected = header.bitcoin_hash();
2991-
self.channel_monitor.last_block_hash = self.last_block_connected;
29922990
if self.funding_tx_confirmations > 0 {
29932991
self.funding_tx_confirmations += 1;
2994-
if self.funding_tx_confirmations == self.minimum_depth as u64 {
2995-
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
2996-
self.channel_state |= ChannelState::OurFundingLocked as u32;
2997-
true
2998-
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) {
2999-
self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
3000-
self.channel_update_count += 1;
3001-
true
3002-
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
3003-
// We got a reorg but not enough to trigger a force close, just update
3004-
// funding_tx_confirmed_in and return.
3005-
false
3006-
} else if self.channel_state < ChannelState::ChannelFunded as u32 {
3007-
panic!("Started confirming a channel in a state pre-FundingSent?: {}", self.channel_state);
3008-
} else {
3009-
// We got a reorg but not enough to trigger a force close, just update
3010-
// funding_tx_confirmed_in and return.
3011-
false
3012-
};
3013-
self.funding_tx_confirmed_in = Some(header.bitcoin_hash());
3014-
3015-
//TODO: Note that this must be a duplicate of the previous commitment point they sent us,
3016-
//as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
3017-
//they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
3018-
//a protocol oversight, but I assume I'm just missing something.
3019-
if need_commitment_update {
3020-
if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
3021-
let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
3022-
let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
3023-
return Ok(Some(msgs::FundingLocked {
3024-
channel_id: self.channel_id,
3025-
next_per_commitment_point: next_per_commitment_point,
3026-
}));
3027-
} else {
3028-
self.monitor_pending_funding_locked = true;
3029-
return Ok(None);
3030-
}
3031-
}
3032-
}
30332992
}
30342993
}
30352994
if non_shutdown_state & !(ChannelState::TheirFundingLocked as u32) == ChannelState::FundingSent as u32 {
@@ -3072,6 +3031,51 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
30723031
}
30733032
}
30743033
}
3034+
if header.bitcoin_hash() != self.last_block_connected {
3035+
self.last_block_connected = header.bitcoin_hash();
3036+
self.channel_monitor.last_block_hash = self.last_block_connected;
3037+
if self.funding_tx_confirmations > 0 {
3038+
if self.funding_tx_confirmations == self.minimum_depth as u64 {
3039+
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
3040+
self.channel_state |= ChannelState::OurFundingLocked as u32;
3041+
true
3042+
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) {
3043+
self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
3044+
self.channel_update_count += 1;
3045+
true
3046+
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
3047+
// We got a reorg but not enough to trigger a force close, just update
3048+
// funding_tx_confirmed_in and return.
3049+
false
3050+
} else if self.channel_state < ChannelState::ChannelFunded as u32 {
3051+
panic!("Started confirming a channel in a state pre-FundingSent?: {}", self.channel_state);
3052+
} else {
3053+
// We got a reorg but not enough to trigger a force close, just update
3054+
// funding_tx_confirmed_in and return.
3055+
false
3056+
};
3057+
self.funding_tx_confirmed_in = Some(header.bitcoin_hash());
3058+
3059+
//TODO: Note that this must be a duplicate of the previous commitment point they sent us,
3060+
//as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
3061+
//they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
3062+
//a protocol oversight, but I assume I'm just missing something.
3063+
if need_commitment_update {
3064+
if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
3065+
let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
3066+
let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
3067+
return Ok(Some(msgs::FundingLocked {
3068+
channel_id: self.channel_id,
3069+
next_per_commitment_point: next_per_commitment_point,
3070+
}));
3071+
} else {
3072+
self.monitor_pending_funding_locked = true;
3073+
return Ok(None);
3074+
}
3075+
}
3076+
}
3077+
}
3078+
}
30753079
Ok(None)
30763080
}
30773081

0 commit comments

Comments
 (0)