Skip to content

Commit 8da5521

Browse files
committed
Addressing PR comments.
1 parent 8142175 commit 8da5521

File tree

2 files changed

+24
-37
lines changed

2 files changed

+24
-37
lines changed

src/ln/channel.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ const UNCONF_THRESHOLD: u32 = 6;
357357
const BREAKDOWN_TIMEOUT: u16 = 6 * 24 * 7; //TODO?
358358
/// The amount of time we're willing to wait to claim money back to us
359359
const MAX_LOCAL_BREAKDOWN_TIMEOUT: u16 = 6 * 24 * 14;
360+
/// Exposing these two constants for use in test in ChannelMonitor
360361
pub const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
361362
pub const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
362363
const SPENDING_INPUT_FOR_A_OUTPUT_WEIGHT: u64 = 79; // prevout: 36, nSequence: 4, script len: 1, witness lengths: (3+1)/4, sig: 73/4, if-selector: 1, redeemScript: (6 ops + 2*33 pubkeys + 1*2 delay)/4
@@ -1661,16 +1662,16 @@ impl Channel {
16611662
self.feerate_per_kw
16621663
};
16631664

1664-
16651665
let mut local_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, feerate_per_kw);
16661666
let local_commitment_txid = local_commitment_tx.0.txid();
16671667
let local_sighash = Message::from_slice(&bip143::SighashComponents::new(&local_commitment_tx.0).sighash_all(&local_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
16681668
secp_call!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid commitment tx signature from peer", self.channel_id());
16691669

16701670
//If channel fee was updated by funder confirm funder can afford the new fee rate when applied to the current local commitment transaction
16711671
if update_fee {
1672-
let num_htlcs = local_commitment_tx.0.output.len();
1672+
let num_htlcs = local_commitment_tx.1.len();
16731673
let total_fee: u64 = feerate_per_kw as u64 * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
1674+
16741675
if self.channel_value_satoshis - self.value_to_self_msat / 1000 < total_fee + self.their_channel_reserve_satoshis {
16751676
return Err(HandleError { err: "Funding remote cannot afford proposed new fee", action: Some(ErrorAction::DisconnectPeer { msg: None }) });
16761677
}
@@ -1680,8 +1681,6 @@ impl Channel {
16801681
return Err(HandleError{err: "Got wrong number of HTLC signatures from remote", action: None});
16811682
}
16821683

1683-
println!("Len: {}, Num HTLC Sigs: {}", local_commitment_tx.0.output.len(), local_commitment_tx.1.len());
1684-
16851684
let mut new_local_commitment_txn = Vec::with_capacity(local_commitment_tx.1.len() + 1);
16861685
self.sign_commitment_transaction(&mut local_commitment_tx.0, &msg.signature);
16871686
new_local_commitment_txn.push(local_commitment_tx.0.clone());

src/ln/channelmanager.rs

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use secp256k1;
2222

2323
use chain::chaininterface::{BroadcasterInterface,ChainListener,ChainWatchInterface,FeeEstimator};
2424
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};
2626
use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, ManyChannelMonitor, CLTV_CLAIM_BUFFER, HTLC_FAIL_TIMEOUT_BLOCKS};
2727
use ln::router::{Route,RouteHop};
2828
use ln::msgs;
@@ -3212,6 +3212,7 @@ mod tests {
32123212
use chain::chaininterface::{ChainListener, ChainWatchInterface};
32133213
use chain::keysinterface::KeysInterface;
32143214
use chain::keysinterface;
3215+
use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
32153216
use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,OnionKeys,PaymentFailReason,RAACommitmentOrder};
32163217
use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, CLTV_CLAIM_BUFFER, HTLC_FAIL_TIMEOUT_BLOCKS, ManyChannelMonitor};
32173218
use ln::router::{Route, RouteHop, Router};
@@ -4444,62 +4445,49 @@ mod tests {
44444445
#[test]
44454446
fn test_update_fee_that_funder_cannot_afford() {
44464447
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);
44484450
let channel_id = chan.2;
44494451

44504452
let feerate = 260;
44514453
nodes[0].node.update_fee(channel_id, feerate).unwrap();
44524454
check_added_monitors!(nodes[0], 1);
4455+
let update_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
44534456

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();
44674458

4468-
commitment_signed_dance!(nodes[1], nodes[0], commitment_signed, false);
4459+
commitment_signed_dance!(nodes[1], nodes[0], update_msg.commitment_signed, false);
44694460

44704461
//Confirm that the new fee based on the last local commitment txn is what we expected based on the feerate of 260 set above.
44714462
//This value results in a fee that is exactly what the funder can afford (277 sat + 1000 sat channel reserve)
44724463
{
44734464
let chan_lock = nodes[1].node.channel_state.lock().unwrap();
44744465
let chan = chan_lock.by_id.get(&channel_id).unwrap();
44754466

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);
44804473
} //drop the mutex
44814474

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();
44834478
check_added_monitors!(nodes[0], 1);
44844479

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());
44934481

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());
44954483

44964484
//While producing the commitment_signed response after handling a received update_fee request the
44974485
//check to see if the funder, who sent the update_fee request, can afford the new fee (funder_balance >= fee+channel_reserve)
44984486
//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();
45004488

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,
45034491
_ => false,
45044492
});
45054493

0 commit comments

Comments
 (0)