@@ -22,7 +22,7 @@ use secp256k1;
22
22
23
23
use chain:: chaininterface:: { BroadcasterInterface , ChainListener , ChainWatchInterface , FeeEstimator } ;
24
24
use chain:: transaction:: OutPoint ;
25
- use ln:: channel:: { Channel , ChannelError , COMMITMENT_TX_BASE_WEIGHT , COMMITMENT_TX_WEIGHT_PER_HTLC } ;
25
+ use ln:: channel:: { Channel , ChannelError } ;
26
26
use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr , ManyChannelMonitor , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS } ;
27
27
use ln:: router:: { Route , RouteHop } ;
28
28
use ln:: msgs;
@@ -3212,6 +3212,7 @@ mod tests {
3212
3212
use chain:: chaininterface:: { ChainListener , ChainWatchInterface } ;
3213
3213
use chain:: keysinterface:: KeysInterface ;
3214
3214
use chain:: keysinterface;
3215
+ use ln:: channel:: { COMMITMENT_TX_BASE_WEIGHT , COMMITMENT_TX_WEIGHT_PER_HTLC } ;
3215
3216
use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , OnionKeys , PaymentFailReason , RAACommitmentOrder } ;
3216
3217
use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS , ManyChannelMonitor } ;
3217
3218
use ln:: router:: { Route , RouteHop , Router } ;
@@ -4444,62 +4445,49 @@ mod tests {
4444
4445
#[ test]
4445
4446
fn test_update_fee_that_funder_cannot_afford ( ) {
4446
4447
let mut nodes = create_network ( 2 ) ;
4447
- let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1432 , 200000 ) ;
4448
+ let channel_value = 1888 ;
4449
+ let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , channel_value, 700000 ) ;
4448
4450
let channel_id = chan. 2 ;
4449
4451
4450
4452
let feerate = 260 ;
4451
4453
nodes[ 0 ] . node . update_fee ( channel_id, feerate) . unwrap ( ) ;
4452
4454
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
4455
+ let update_msg = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
4453
4456
4454
- let events_0 = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
4455
- assert_eq ! ( events_0. len( ) , 1 ) ;
4456
- let ( update_msg, commitment_signed) = match events_0[ 0 ] {
4457
- MessageSendEvent :: UpdateHTLCs {
4458
- node_id : _, updates : msgs:: CommitmentUpdate {
4459
- update_add_htlcs : _, update_fulfill_htlcs : _, update_fail_htlcs : _, update_fail_malformed_htlcs : _, ref update_fee, ref commitment_signed
4460
- }
4461
- } => {
4462
- ( update_fee. as_ref ( ) , commitment_signed)
4463
- } ,
4464
- _ => panic ! ( "Unexpected event" ) ,
4465
- } ;
4466
- nodes[ 1 ] . node . handle_update_fee ( & nodes[ 0 ] . node . get_our_node_id ( ) , update_msg. unwrap ( ) ) . unwrap ( ) ;
4457
+ nodes[ 1 ] . node . handle_update_fee ( & nodes[ 0 ] . node . get_our_node_id ( ) , & update_msg. update_fee . unwrap ( ) ) . unwrap ( ) ;
4467
4458
4468
- commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , commitment_signed, false ) ;
4459
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , update_msg . commitment_signed, false ) ;
4469
4460
4470
4461
//Confirm that the new fee based on the last local commitment txn is what we expected based on the feerate of 260 set above.
4471
4462
//This value results in a fee that is exactly what the funder can afford (277 sat + 1000 sat channel reserve)
4472
4463
{
4473
4464
let chan_lock = nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) ;
4474
4465
let chan = chan_lock. by_id . get ( & channel_id) . unwrap ( ) ;
4475
4466
4476
- let num_htlcs = chan. last_local_commitment_txn [ 0 ] . output . len ( ) ;
4477
- let total_fee: u64 = feerate * ( super :: COMMITMENT_TX_BASE_WEIGHT + ( num_htlcs as u64 ) * super :: COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000 ;
4478
- let fee_for_single_update_to_commitment = feerate * ( super :: COMMITMENT_TX_BASE_WEIGHT + 1 * super :: COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000 ;
4479
- assert_eq ! ( total_fee, fee_for_single_update_to_commitment) ;
4467
+ //We made sure neither party's funds are below the dust limit so -2 non-HTLC txns from number of outputs
4468
+ let num_htlcs = chan. last_local_commitment_txn [ 0 ] . output . len ( ) - 2 ;
4469
+ let total_fee: u64 = feerate * ( COMMITMENT_TX_BASE_WEIGHT + ( num_htlcs as u64 ) * COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000 ;
4470
+ let mut actual_fee = chan. last_local_commitment_txn [ 0 ] . output . iter ( ) . fold ( 0 , |acc, output| acc + output. value ) ;
4471
+ actual_fee = channel_value - actual_fee;
4472
+ assert_eq ! ( total_fee, actual_fee) ;
4480
4473
} //drop the mutex
4481
4474
4482
- nodes[ 0 ] . node . update_fee ( channel_id, feerate+1 ) . unwrap ( ) ;
4475
+ //Add 2 to the previous fee rate to the final fee increases by 1 (with no HTLCs the fee is essentially
4476
+ //fee_rate*(724/1000) so the increment of 1*0.724 is rounded back down)
4477
+ nodes[ 0 ] . node . update_fee ( channel_id, feerate+2 ) . unwrap ( ) ;
4483
4478
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
4484
4479
4485
- let events_0 = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
4486
- assert_eq ! ( events_0. len( ) , 1 ) ;
4487
- let ( update_msg, commitment_signed) = match events_0[ 0 ] {
4488
- MessageSendEvent :: UpdateHTLCs { node_id : _, updates : msgs:: CommitmentUpdate { update_add_htlcs : _, update_fulfill_htlcs : _, update_fail_htlcs : _, update_fail_malformed_htlcs : _, ref update_fee, ref commitment_signed } } => {
4489
- ( update_fee. as_ref ( ) , commitment_signed)
4490
- } ,
4491
- _ => panic ! ( "Unexpected event" ) ,
4492
- } ;
4480
+ let update2_msg = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
4493
4481
4494
- nodes[ 1 ] . node . handle_update_fee ( & nodes[ 0 ] . node . get_our_node_id ( ) , update_msg . unwrap ( ) ) ;
4482
+ nodes[ 1 ] . node . handle_update_fee ( & nodes[ 0 ] . node . get_our_node_id ( ) , & update2_msg . update_fee . unwrap ( ) ) ;
4495
4483
4496
4484
//While producing the commitment_signed response after handling a received update_fee request the
4497
4485
//check to see if the funder, who sent the update_fee request, can afford the new fee (funder_balance >= fee+channel_reserve)
4498
4486
//Should produce and error.
4499
- let err = nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & commitment_signed) . unwrap_err ( ) ;
4487
+ let err = nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & update2_msg . commitment_signed ) . unwrap_err ( ) ;
4500
4488
4501
- assert ! ( match err. action . unwrap ( ) {
4502
- msgs :: ErrorAction :: DisconnectPeer { .. } => true ,
4489
+ assert ! ( match err. err {
4490
+ "Funding remote cannot afford proposed new fee" => true ,
4503
4491
_ => false ,
4504
4492
} ) ;
4505
4493
0 commit comments