@@ -25,7 +25,8 @@ use util::config::UserConfig;
25
25
26
26
use bitcoin:: util:: hash:: BitcoinHash ;
27
27
use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
28
- use bitcoin:: hash_types:: { Txid , BlockHash } ;
28
+ use bitcoin:: hashes:: HashEngine ;
29
+ use bitcoin:: hash_types:: { Txid , BlockHash , WPubkeyHash } ;
29
30
use bitcoin:: util:: bip143;
30
31
use bitcoin:: util:: address:: Address ;
31
32
use bitcoin:: util:: bip32:: { ChildNumber , ExtendedPubKey , ExtendedPrivKey } ;
@@ -1553,6 +1554,184 @@ fn test_basic_channel_reserve() {
1553
1554
send_payment ( & nodes[ 0 ] , & vec ! [ & nodes[ 1 ] ] , max_can_send, max_can_send) ;
1554
1555
}
1555
1556
1557
+ #[ test]
1558
+ fn test_fee_spike_violation_fails_htlc ( ) {
1559
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1560
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1561
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1562
+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
1563
+ let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 95000000 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
1564
+ let logger = test_utils:: TestLogger :: new ( ) ;
1565
+
1566
+ macro_rules! get_route_and_payment_hash {
1567
+ ( $recv_value: expr) => { {
1568
+ let ( payment_preimage, payment_hash) = get_payment_preimage_hash!( nodes[ 1 ] ) ;
1569
+ let net_graph_msg_handler = & nodes[ 0 ] . net_graph_msg_handler;
1570
+ let route = get_route( & nodes[ 0 ] . node. get_our_node_id( ) , net_graph_msg_handler, & nodes. last( ) . unwrap( ) . node. get_our_node_id( ) , None , & Vec :: new( ) , $recv_value, TEST_FINAL_CLTV , & logger) . unwrap( ) ;
1571
+ ( route, payment_hash, payment_preimage)
1572
+ } }
1573
+ } ;
1574
+
1575
+ let ( route, payment_hash, _) = get_route_and_payment_hash ! ( 3460001 ) ;
1576
+ // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1577
+ let secp_ctx = Secp256k1 :: new ( ) ;
1578
+ let session_priv = SecretKey :: from_slice ( & {
1579
+ let mut session_key = [ 0 ; 32 ] ;
1580
+ let mut rng = thread_rng ( ) ;
1581
+ rng. fill_bytes ( & mut session_key) ;
1582
+ session_key
1583
+ } ) . expect ( "RNG is bad!" ) ;
1584
+
1585
+ let cur_height = nodes[ 1 ] . node . latest_block_height . load ( Ordering :: Acquire ) as u32 + 1 ;
1586
+
1587
+ let onion_keys = onion_utils:: construct_onion_keys ( & secp_ctx, & route. paths [ 0 ] , & session_priv) . unwrap ( ) ;
1588
+ let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( & route. paths [ 0 ] , 3460001 , & None , cur_height) . unwrap ( ) ;
1589
+ let onion_packet = onion_utils:: construct_onion_packet ( onion_payloads, onion_keys, [ 0 ; 32 ] , & payment_hash) ;
1590
+ let msg = msgs:: UpdateAddHTLC {
1591
+ channel_id : chan. 2 ,
1592
+ htlc_id : 0 ,
1593
+ amount_msat : htlc_msat,
1594
+ payment_hash : payment_hash,
1595
+ cltv_expiry : htlc_cltv,
1596
+ onion_routing_packet : onion_packet,
1597
+ } ;
1598
+
1599
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & msg) ;
1600
+
1601
+ // Now manually create the commitment_signed message corresponding to the update_add
1602
+ // nodes[0] just sent. In the code for construction of this message, "local" refers
1603
+ // to the sender of the message, and "remote" refers to the receiver.
1604
+
1605
+ let feerate_per_kw = get_feerate ! ( nodes[ 0 ] , chan. 2 ) ;
1606
+
1607
+ // Get the EnforcingChannelKeys for each channel, which will be used to (1) get the keys
1608
+ // needed to sign the new commitment tx and (2) sign the new commitment tx.
1609
+ let ( local_revocation_basepoint, local_htlc_basepoint, local_payment_point, local_chan_commitment_seed) = {
1610
+ let chan_lock = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
1611
+ let local_chan = chan_lock. by_id . get ( & chan. 2 ) . unwrap ( ) ;
1612
+ let chan_keys = local_chan. get_local_keys ( ) ;
1613
+ let pubkeys = chan_keys. pubkeys ( ) ;
1614
+ ( pubkeys. revocation_basepoint , pubkeys. htlc_basepoint , pubkeys. payment_point , * chan_keys. commitment_seed ( ) )
1615
+ } ;
1616
+ let ( remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_payment_point, remote_chan_commitment_seed) = {
1617
+ let chan_lock = nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) ;
1618
+ let remote_chan = chan_lock. by_id . get ( & chan. 2 ) . unwrap ( ) ;
1619
+ let chan_keys = remote_chan. get_local_keys ( ) ;
1620
+ let pubkeys = chan_keys. pubkeys ( ) ;
1621
+ ( pubkeys. delayed_payment_basepoint , pubkeys. htlc_basepoint , pubkeys. payment_point , * chan_keys. commitment_seed ( ) )
1622
+ } ;
1623
+
1624
+ // Assemble the set of keys we can use for signatures for our commitment_signed message.
1625
+ const INITIAL_COMMITMENT_NUMBER : u64 = ( 1 << 48 ) - 1 ;
1626
+ let commitment_secret = {
1627
+ let res = chan_utils:: build_commitment_secret ( & remote_chan_commitment_seed, INITIAL_COMMITMENT_NUMBER - 1 ) ;
1628
+ SecretKey :: from_slice ( & res) . unwrap ( )
1629
+ } ;
1630
+ let per_commitment_point = PublicKey :: from_secret_key ( & secp_ctx, & commitment_secret) ;
1631
+ let commit_tx_keys = chan_utils:: TxCreationKeys :: new ( & secp_ctx, & per_commitment_point, & remote_delayed_payment_basepoint,
1632
+ & remote_htlc_basepoint, & local_revocation_basepoint, & local_htlc_basepoint) . unwrap ( ) ;
1633
+
1634
+ // Build the remote commitment transaction so we can sign it, and then later use the
1635
+ // signature for the commitment_signed message.
1636
+ let local_chan_balance = 1313 ;
1637
+ let static_payment_pk = local_payment_point. serialize ( ) ;
1638
+ let remote_commit_tx_output = TxOut {
1639
+ script_pubkey : Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_PUSHBYTES_0 )
1640
+ . push_slice ( & WPubkeyHash :: hash ( & static_payment_pk) [ ..] )
1641
+ . into_script ( ) ,
1642
+ value : local_chan_balance as u64
1643
+ } ;
1644
+
1645
+ let local_commit_tx_output = TxOut {
1646
+ script_pubkey : chan_utils:: get_revokeable_redeemscript ( & commit_tx_keys. revocation_key ,
1647
+ BREAKDOWN_TIMEOUT ,
1648
+ & commit_tx_keys. a_delayed_payment_key ) . to_v0_p2wsh ( ) ,
1649
+ value : 95000 ,
1650
+ } ;
1651
+
1652
+ let accepted_htlc_info = chan_utils:: HTLCOutputInCommitment {
1653
+ offered : false ,
1654
+ amount_msat : 3460001 ,
1655
+ cltv_expiry : htlc_cltv,
1656
+ payment_hash : payment_hash,
1657
+ transaction_output_index : Some ( 1 ) ,
1658
+ } ;
1659
+
1660
+ let htlc_output = TxOut {
1661
+ script_pubkey : chan_utils:: get_htlc_redeemscript ( & accepted_htlc_info, & commit_tx_keys) . to_v0_p2wsh ( ) ,
1662
+ value : 3460001 / 1000
1663
+ } ;
1664
+
1665
+ let commit_tx_obscure_factor = {
1666
+ let mut sha = Sha256 :: engine ( ) ;
1667
+ let remote_payment_point = & remote_payment_point. serialize ( ) ;
1668
+ sha. input ( & local_payment_point. serialize ( ) ) ;
1669
+ sha. input ( remote_payment_point) ;
1670
+ let res = Sha256 :: from_engine ( sha) . into_inner ( ) ;
1671
+
1672
+ ( ( res[ 26 ] as u64 ) << 5 * 8 ) |
1673
+ ( ( res[ 27 ] as u64 ) << 4 * 8 ) |
1674
+ ( ( res[ 28 ] as u64 ) << 3 * 8 ) |
1675
+ ( ( res[ 29 ] as u64 ) << 2 * 8 ) |
1676
+ ( ( res[ 30 ] as u64 ) << 1 * 8 ) |
1677
+ ( ( res[ 31 ] as u64 ) << 0 * 8 )
1678
+ } ;
1679
+ let commitment_number = 1 ;
1680
+ let obscured_commitment_transaction_number = commit_tx_obscure_factor ^ commitment_number;
1681
+ let lock_time = ( ( 0x20 as u32 ) << 8 * 3 ) | ( ( obscured_commitment_transaction_number & 0xffffffu64 ) as u32 ) ;
1682
+ let input = TxIn {
1683
+ previous_output : BitcoinOutPoint { txid : chan. 3 . txid ( ) , vout : 0 } ,
1684
+ script_sig : Script :: new ( ) ,
1685
+ sequence : ( ( 0x80 as u32 ) << 8 * 3 ) | ( ( obscured_commitment_transaction_number >> 3 * 8 ) as u32 ) ,
1686
+ witness : Vec :: new ( ) ,
1687
+ } ;
1688
+
1689
+ let commit_tx = Transaction {
1690
+ version : 2 ,
1691
+ lock_time,
1692
+ input : vec ! [ input] ,
1693
+ output : vec ! [ remote_commit_tx_output, htlc_output, local_commit_tx_output] ,
1694
+ } ;
1695
+ let res = {
1696
+ let local_chan_lock = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
1697
+ let local_chan = local_chan_lock. by_id . get ( & chan. 2 ) . unwrap ( ) ;
1698
+ let local_chan_keys = local_chan. get_local_keys ( ) ;
1699
+ local_chan_keys. sign_remote_commitment ( feerate_per_kw, & commit_tx, & commit_tx_keys, & [ & accepted_htlc_info] ,
1700
+ BREAKDOWN_TIMEOUT , & secp_ctx) . unwrap ( )
1701
+ } ;
1702
+
1703
+ let commit_signed_msg = msgs:: CommitmentSigned {
1704
+ channel_id : chan. 2 ,
1705
+ signature : res. 0 ,
1706
+ htlc_signatures : res. 1
1707
+ } ;
1708
+
1709
+ // Send the commitment_signed message to the nodes[1].
1710
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & commit_signed_msg) ;
1711
+ let _ = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1712
+
1713
+ // Send the RAA to nodes[1].
1714
+ let per_commitment_secret = chan_utils:: build_commitment_secret ( & local_chan_commitment_seed, INITIAL_COMMITMENT_NUMBER ) ;
1715
+ let next_secret = SecretKey :: from_slice ( & chan_utils:: build_commitment_secret ( & local_chan_commitment_seed, INITIAL_COMMITMENT_NUMBER - 2 ) ) . unwrap ( ) ;
1716
+ let next_per_commitment_point = PublicKey :: from_secret_key ( & secp_ctx, & next_secret) ;
1717
+ let raa_msg = msgs:: RevokeAndACK { channel_id : chan. 2 , per_commitment_secret, next_per_commitment_point} ;
1718
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , & raa_msg) ;
1719
+
1720
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1721
+ assert_eq ! ( events. len( ) , 1 ) ;
1722
+ // Make sure the HTLC failed in the way we expect.
1723
+ match events[ 0 ] {
1724
+ MessageSendEvent :: UpdateHTLCs { updates : msgs:: CommitmentUpdate { ref update_fail_htlcs, .. } , .. } => {
1725
+ assert_eq ! ( update_fail_htlcs. len( ) , 1 ) ;
1726
+ update_fail_htlcs[ 0 ] . clone ( )
1727
+ } ,
1728
+ _ => panic ! ( "Unexpected event" ) ,
1729
+ } ;
1730
+ nodes[ 1 ] . logger . assert_log ( "lightning::ln::channel" . to_string ( ) , "Attempting to fail HTLC due to fee spike buffer violation" . to_string ( ) , 1 ) ;
1731
+
1732
+ check_added_monitors ! ( nodes[ 1 ] , 2 ) ;
1733
+ }
1734
+
1556
1735
#[ test]
1557
1736
fn test_chan_reserve_violation_outbound_htlc_inbound_chan ( ) {
1558
1737
let mut chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
0 commit comments