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