@@ -23,7 +23,7 @@ use secp256k1;
23
23
use chain:: chaininterface:: { BroadcasterInterface , ChainListener , ChainWatchInterface , FeeEstimator } ;
24
24
use chain:: transaction:: OutPoint ;
25
25
use ln:: channel:: { Channel , ChannelError , ChannelKeys } ;
26
- use ln:: channelmonitor:: ManyChannelMonitor ;
26
+ use ln:: channelmonitor:: { ManyChannelMonitor , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS } ;
27
27
use ln:: router:: { Route , RouteHop } ;
28
28
use ln:: msgs;
29
29
use ln:: msgs:: { HandleError , ChannelMessageHandler } ;
@@ -300,7 +300,27 @@ pub struct ChannelManager {
300
300
logger : Arc < Logger > ,
301
301
}
302
302
303
+ /// The minimum number of blocks between an inbound HTLC's CLTV and the corresponding outbound
304
+ /// HTLC's CLTV. This should always be a few blocks greater than channelmonitor::CLTV_CLAIM_BUFFER,
305
+ /// ie the node we forwarded the payment on to should always have enough room to reliably time out
306
+ /// the HTLC via a full update_fail_htlc/commitment_signed dance before we hit the
307
+ /// CLTV_CLAIM_BUFFER point (we static assert that its at least 3 blocks more).
303
308
const CLTV_EXPIRY_DELTA : u16 = 6 * 24 * 2 ; //TODO?
309
+
310
+ // Check that our CLTV_EXPIRY is at least CLTV_CLAIM_BUFFER + 2*HTLC_FAIL_TIMEOUT_BLOCKS, ie that
311
+ // if the next-hop peer fails the HTLC within HTLC_FAIL_TIMEOUT_BLOCKS then we'll still have
312
+ // HTLC_FAIL_TIMEOUT_BLOCKS left to fail it backwards ourselves before hitting the
313
+ // CLTV_CLAIM_BUFFER point and failing the channel on-chain to time out the HTLC.
314
+ #[ deny( const_err) ]
315
+ #[ allow( dead_code) ]
316
+ const CHECK_CLTV_EXPIRY_SANITY : u32 = CLTV_EXPIRY_DELTA as u32 - 2 * HTLC_FAIL_TIMEOUT_BLOCKS - CLTV_CLAIM_BUFFER ;
317
+
318
+ // Check for ability of an attacker to make us fail on-chain by delaying inbound claim. See
319
+ // ChannelMontior::would_broadcast_at_height for a description of why this is needed.
320
+ #[ deny( const_err) ]
321
+ #[ allow( dead_code) ]
322
+ const CHECK_CLTV_EXPIRY_SANITY_2 : u32 = CLTV_EXPIRY_DELTA as u32 - HTLC_FAIL_TIMEOUT_BLOCKS - 2 * CLTV_CLAIM_BUFFER ;
323
+
304
324
const CLTV_FAR_FAR_AWAY : u16 = 6 * 24 * 7 ; //TODO?
305
325
const FINAL_NODE_TIMEOUT : u16 = 3 ; //TODO?
306
326
@@ -2542,6 +2562,7 @@ mod tests {
2542
2562
use chain:: transaction:: OutPoint ;
2543
2563
use chain:: chaininterface:: ChainListener ;
2544
2564
use ln:: channelmanager:: { ChannelManager , OnionKeys } ;
2565
+ use ln:: channelmonitor:: { CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS } ;
2545
2566
use ln:: router:: { Route , RouteHop , Router } ;
2546
2567
use ln:: msgs;
2547
2568
use ln:: msgs:: { ChannelMessageHandler , RoutingMessageHandler } ;
@@ -2574,6 +2595,7 @@ mod tests {
2574
2595
use std:: default:: Default ;
2575
2596
use std:: rc:: Rc ;
2576
2597
use std:: sync:: { Arc , Mutex } ;
2598
+ use std:: sync:: atomic:: Ordering ;
2577
2599
use std:: time:: Instant ;
2578
2600
use std:: mem;
2579
2601
@@ -4446,13 +4468,17 @@ mod tests {
4446
4468
assert_eq ! ( nodes[ 2 ] . node. list_channels( ) . len( ) , 0 ) ;
4447
4469
assert_eq ! ( nodes[ 3 ] . node. list_channels( ) . len( ) , 1 ) ;
4448
4470
4471
+ assert_eq ! ( nodes[ 3 ] . node. latest_block_height. load( Ordering :: Acquire ) , 1 ) ;
4472
+ assert_eq ! ( nodes[ 4 ] . node. latest_block_height. load( Ordering :: Acquire ) , 1 ) ;
4449
4473
// One pending HTLC to time out:
4450
4474
let payment_preimage_2 = route_payment ( & nodes[ 3 ] , & vec ! ( & nodes[ 4 ] ) [ ..] , 3000000 ) . 0 ;
4475
+ // CLTV expires at TEST_FINAL_CLTV + 1 (current height) + 1 (added in send_payment for
4476
+ // buffer space).
4451
4477
4452
4478
{
4453
4479
let mut header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
4454
- nodes[ 3 ] . chain_monitor . block_connected_checked ( & header, 1 , & Vec :: new ( ) [ ..] , & [ 0 ; 0 ] ) ;
4455
- for i in 2 ..TEST_FINAL_CLTV - 3 {
4480
+ nodes[ 3 ] . chain_monitor . block_connected_checked ( & header, 2 , & Vec :: new ( ) [ ..] , & [ 0 ; 0 ] ) ;
4481
+ for i in 3 ..TEST_FINAL_CLTV + 2 + HTLC_FAIL_TIMEOUT_BLOCKS + 1 {
4456
4482
header = BlockHeader { version : 0x20000000 , prev_blockhash : header. bitcoin_hash ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
4457
4483
nodes[ 3 ] . chain_monitor . block_connected_checked ( & header, i, & Vec :: new ( ) [ ..] , & [ 0 ; 0 ] ) ;
4458
4484
}
@@ -4463,8 +4489,8 @@ mod tests {
4463
4489
claim_funds ! ( nodes[ 4 ] , nodes[ 3 ] , payment_preimage_2) ;
4464
4490
4465
4491
header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
4466
- nodes[ 4 ] . chain_monitor . block_connected_checked ( & header, 1 , & Vec :: new ( ) [ ..] , & [ 0 ; 0 ] ) ;
4467
- for i in 2 ..TEST_FINAL_CLTV - 3 {
4492
+ nodes[ 4 ] . chain_monitor . block_connected_checked ( & header, 2 , & Vec :: new ( ) [ ..] , & [ 0 ; 0 ] ) ;
4493
+ for i in 3 ..TEST_FINAL_CLTV + 2 - CLTV_CLAIM_BUFFER + 1 {
4468
4494
header = BlockHeader { version : 0x20000000 , prev_blockhash : header. bitcoin_hash ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
4469
4495
nodes[ 4 ] . chain_monitor . block_connected_checked ( & header, i, & Vec :: new ( ) [ ..] , & [ 0 ; 0 ] ) ;
4470
4496
}
0 commit comments