Skip to content

Commit 1049ac3

Browse files
committed
(XXX: Add tests) Handle 1-conf funding_locked in channel no matter the event order
See comment in the diff for more details
1 parent c6ff62a commit 1049ac3

File tree

1 file changed

+60
-38
lines changed

1 file changed

+60
-38
lines changed

lightning/src/ln/channel.rs

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3501,11 +3501,59 @@ impl<Signer: Sign> Channel<Signer> {
35013501
self.network_sync == UpdateStatus::DisabledMarked
35023502
}
35033503

3504+
fn check_get_funding_locked(&mut self, height: u32) -> Option<msgs::FundingLocked> {
3505+
if self.funding_tx_confirmation_height > 0 {
3506+
let funding_tx_confirmations = height as i64 - self.funding_tx_confirmation_height as i64 + 1;
3507+
if funding_tx_confirmations <= 0 {
3508+
self.funding_tx_confirmation_height = 0;
3509+
}
3510+
3511+
if funding_tx_confirmations >= self.minimum_depth as i64 {
3512+
let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
3513+
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
3514+
self.channel_state |= ChannelState::OurFundingLocked as u32;
3515+
true
3516+
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) {
3517+
self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
3518+
self.update_time_counter += 1;
3519+
true
3520+
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
3521+
// We got a reorg but not enough to trigger a force close, just update
3522+
// funding_tx_confirmed_in and return.
3523+
false
3524+
} else if self.channel_state < ChannelState::ChannelFunded as u32 {
3525+
panic!("Started confirming a channel in a state pre-FundingSent?: {}", self.channel_state);
3526+
} else {
3527+
// We got a reorg but not enough to trigger a force close, just update
3528+
// funding_tx_confirmed_in and return.
3529+
false
3530+
};
3531+
3532+
//TODO: Note that this must be a duplicate of the previous commitment point they sent us,
3533+
//as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
3534+
//they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
3535+
//a protocol oversight, but I assume I'm just missing something.
3536+
if need_commitment_update {
3537+
if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
3538+
let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
3539+
return Some(msgs::FundingLocked {
3540+
channel_id: self.channel_id,
3541+
next_per_commitment_point,
3542+
});
3543+
} else {
3544+
self.monitor_pending_funding_locked = true;
3545+
}
3546+
}
3547+
}
3548+
}
3549+
None
3550+
}
3551+
35043552
/// When a transaction is confirmed, we check whether it is or spends the funding transaction
35053553
/// In the first case, we store the confirmation height and calculating the short channel id.
35063554
/// In the second, we simply return an Err indicating we need to be force-closed now.
35073555
pub fn transactions_confirmed<L: Deref>(&mut self, block_hash: &BlockHash, height: u32, txdata: &TransactionData, logger: &L)
3508-
-> Result<(), msgs::ErrorMessage> where L::Target: Logger {
3556+
-> Result<Option<msgs::FundingLocked>, msgs::ErrorMessage> where L::Target: Logger {
35093557
let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
35103558
for &(index_in_block, tx) in txdata.iter() {
35113559
if let Some(funding_txo) = self.get_funding_txo() {
@@ -3548,6 +3596,13 @@ impl<Signer: Sign> Channel<Signer> {
35483596
}
35493597
}
35503598
}
3599+
// If we allow 1-conf funding, we may need to check for funding_locked here and
3600+
// send it immediately instead of waiting for an update_best_block call (which
3601+
// may have already happened for this block).
3602+
// XXX: Test this case!
3603+
if let Some(funding_locked) = self.check_get_funding_locked(height) {
3604+
return Ok(Some(funding_locked));
3605+
}
35513606
}
35523607
for inp in tx.input.iter() {
35533608
if inp.previous_output == funding_txo.into_bitcoin_outpoint() {
@@ -3560,7 +3615,7 @@ impl<Signer: Sign> Channel<Signer> {
35603615
}
35613616
}
35623617
}
3563-
Ok(())
3618+
Ok(None)
35643619
}
35653620

35663621
/// When a new block is connected, we check the height of the block against outbound holding
@@ -3590,6 +3645,7 @@ impl<Signer: Sign> Channel<Signer> {
35903645
});
35913646

35923647
self.update_time_counter = cmp::max(self.update_time_counter, highest_header_time);
3648+
35933649
if self.funding_tx_confirmation_height > 0 {
35943650
let funding_tx_confirmations = height as i64 - self.funding_tx_confirmation_height as i64 + 1;
35953651
if funding_tx_confirmations <= 0 {
@@ -3606,42 +3662,8 @@ impl<Signer: Sign> Channel<Signer> {
36063662
});
36073663
}
36083664

3609-
if funding_tx_confirmations == self.minimum_depth as i64 {
3610-
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
3611-
self.channel_state |= ChannelState::OurFundingLocked as u32;
3612-
true
3613-
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) {
3614-
self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
3615-
self.update_time_counter += 1;
3616-
true
3617-
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
3618-
// We got a reorg but not enough to trigger a force close, just update
3619-
// funding_tx_confirmed_in and return.
3620-
false
3621-
} else if self.channel_state < ChannelState::ChannelFunded as u32 {
3622-
panic!("Started confirming a channel in a state pre-FundingSent?: {}", self.channel_state);
3623-
} else {
3624-
// We got a reorg but not enough to trigger a force close, just update
3625-
// funding_tx_confirmed_in and return.
3626-
false
3627-
};
3628-
3629-
//TODO: Note that this must be a duplicate of the previous commitment point they sent us,
3630-
//as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
3631-
//they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
3632-
//a protocol oversight, but I assume I'm just missing something.
3633-
if need_commitment_update {
3634-
if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
3635-
let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
3636-
return Ok((Some(msgs::FundingLocked {
3637-
channel_id: self.channel_id,
3638-
next_per_commitment_point,
3639-
}), timed_out_htlcs));
3640-
} else {
3641-
self.monitor_pending_funding_locked = true;
3642-
return Ok((None, timed_out_htlcs));
3643-
}
3644-
}
3665+
if let Some(funding_locked) = self.check_get_funding_locked(height) {
3666+
return Ok((Some(funding_locked), timed_out_htlcs));
36453667
}
36463668
}
36473669

0 commit comments

Comments
 (0)