Skip to content

Commit 8470e60

Browse files
authored
Merge pull request #336 from ariard/2019-04-in-flight-txn-tracking-clean
2019 04 in flight txn tracking clean
2 parents 100197c + 757bcc2 commit 8470e60

File tree

7 files changed

+914
-145
lines changed

7 files changed

+914
-145
lines changed

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 9 additions & 4 deletions
Large diffs are not rendered by default.

src/chain/chaininterface.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ pub trait ChainListener: Sync + Send {
7878
fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]);
7979
/// Notifies a listener that a block was disconnected.
8080
/// Unlike block_connected, this *must* never be called twice for the same disconnect event.
81-
fn block_disconnected(&self, header: &BlockHeader);
81+
/// Height must be the one of the block which was disconnected (not new height of the best chain)
82+
fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32);
8283
}
8384

8485
/// An enum that represents the speed at which we want a transaction to confirm used for feerate
@@ -279,11 +280,11 @@ impl ChainWatchInterfaceUtil {
279280
}
280281

281282
/// Notify listeners that a block was disconnected.
282-
pub fn block_disconnected(&self, header: &BlockHeader) {
283+
pub fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
283284
let listeners = self.listeners.lock().unwrap().clone();
284285
for listener in listeners.iter() {
285286
match listener.upgrade() {
286-
Some(arc) => arc.block_disconnected(header),
287+
Some(arc) => arc.block_disconnected(&header, disconnected_height),
287288
None => ()
288289
}
289290
}

src/ln/channelmanager.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use secp256k1;
2828
use chain::chaininterface::{BroadcasterInterface,ChainListener,ChainWatchInterface,FeeEstimator};
2929
use chain::transaction::OutPoint;
3030
use ln::channel::{Channel, ChannelError};
31-
use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, ManyChannelMonitor, CLTV_CLAIM_BUFFER, HTLC_FAIL_TIMEOUT_BLOCKS, HTLC_FAIL_ANTI_REORG_DELAY};
31+
use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, ManyChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
3232
use ln::router::Route;
3333
use ln::msgs;
3434
use ln::onion_utils;
@@ -348,20 +348,21 @@ pub struct ChannelManager {
348348
const CLTV_EXPIRY_DELTA: u16 = 6 * 12; //TODO?
349349
pub(super) const CLTV_FAR_FAR_AWAY: u32 = 6 * 24 * 7; //TODO?
350350

351-
// Check that our CLTV_EXPIRY is at least CLTV_CLAIM_BUFFER + 2*HTLC_FAIL_TIMEOUT_BLOCKS +
352-
// HTLC_FAIL_ANTI_REORG_DELAY, ie that if the next-hop peer fails the HTLC within
353-
// HTLC_FAIL_TIMEOUT_BLOCKS then we'll still have HTLC_FAIL_TIMEOUT_BLOCKS left to fail it
354-
// backwards ourselves before hitting the CLTV_CLAIM_BUFFER point and failing the channel
355-
// on-chain to time out the HTLC.
351+
// Check that our CLTV_EXPIRY is at least CLTV_CLAIM_BUFFER + ANTI_REORG_DELAY + LATENCY_GRACE_PERIOD_BLOCKS,
352+
// ie that if the next-hop peer fails the HTLC within
353+
// LATENCY_GRACE_PERIOD_BLOCKS then we'll still have CLTV_CLAIM_BUFFER left to timeout it onchain,
354+
// then waiting ANTI_REORG_DELAY to be reorg-safe on the outbound HLTC and
355+
// failing the corresponding htlc backward, and us now seeing the last block of ANTI_REORG_DELAY before
356+
// LATENCY_GRACE_PERIOD_BLOCKS.
356357
#[deny(const_err)]
357358
#[allow(dead_code)]
358-
const CHECK_CLTV_EXPIRY_SANITY: u32 = CLTV_EXPIRY_DELTA as u32 - 2*HTLC_FAIL_TIMEOUT_BLOCKS - CLTV_CLAIM_BUFFER - HTLC_FAIL_ANTI_REORG_DELAY;
359+
const CHECK_CLTV_EXPIRY_SANITY: u32 = CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - CLTV_CLAIM_BUFFER - ANTI_REORG_DELAY - LATENCY_GRACE_PERIOD_BLOCKS;
359360

360361
// Check for ability of an attacker to make us fail on-chain by delaying inbound claim. See
361362
// ChannelMontior::would_broadcast_at_height for a description of why this is needed.
362363
#[deny(const_err)]
363364
#[allow(dead_code)]
364-
const CHECK_CLTV_EXPIRY_SANITY_2: u32 = CLTV_EXPIRY_DELTA as u32 - HTLC_FAIL_TIMEOUT_BLOCKS - 2*CLTV_CLAIM_BUFFER;
365+
const CHECK_CLTV_EXPIRY_SANITY_2: u32 = CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - 2*CLTV_CLAIM_BUFFER;
365366

366367
macro_rules! secp_call {
367368
( $res: expr, $err: expr ) => {
@@ -841,7 +842,7 @@ impl ChannelManager {
841842
let pending_forward_info = if next_hop_data.hmac == [0; 32] {
842843
// OUR PAYMENT!
843844
// final_expiry_too_soon
844-
if (msg.cltv_expiry as u64) < self.latest_block_height.load(Ordering::Acquire) as u64 + (CLTV_CLAIM_BUFFER + HTLC_FAIL_TIMEOUT_BLOCKS) as u64 {
845+
if (msg.cltv_expiry as u64) < self.latest_block_height.load(Ordering::Acquire) as u64 + (CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS) as u64 {
845846
return_err!("The final CLTV expiry is too soon to handle", 17, &[0;0]);
846847
}
847848
// final_incorrect_htlc_amount
@@ -933,8 +934,8 @@ impl ChannelManager {
933934
break Some(("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta", 0x1000 | 13, Some(self.get_channel_update(chan).unwrap())));
934935
}
935936
let cur_height = self.latest_block_height.load(Ordering::Acquire) as u32 + 1;
936-
// We want to have at least HTLC_FAIL_TIMEOUT_BLOCKS to fail prior to going on chain CLAIM_BUFFER blocks before expiration
937-
if msg.cltv_expiry <= cur_height + CLTV_CLAIM_BUFFER + HTLC_FAIL_TIMEOUT_BLOCKS as u32 { // expiry_too_soon
937+
// We want to have at least LATENCY_GRACE_PERIOD_BLOCKS to fail prior to going on chain CLAIM_BUFFER blocks before expiration
938+
if msg.cltv_expiry <= cur_height + CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS as u32 { // expiry_too_soon
938939
break Some(("CLTV expiry is too close", 0x1000 | 14, Some(self.get_channel_update(chan).unwrap())));
939940
}
940941
if msg.cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 { // expiry_too_far
@@ -2489,7 +2490,7 @@ impl ChainListener for ChannelManager {
24892490
}
24902491

24912492
/// We force-close the channel without letting our counterparty participate in the shutdown
2492-
fn block_disconnected(&self, header: &BlockHeader) {
2493+
fn block_disconnected(&self, header: &BlockHeader, _: u32) {
24932494
let _ = self.total_consistency_lock.read().unwrap();
24942495
let mut failed_channels = Vec::new();
24952496
{

0 commit comments

Comments
 (0)