From 781a0debd46529d3c75e9b67e6cc6ef6258f16af Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Wed, 10 Oct 2018 01:30:03 +0000 Subject: [PATCH] Add const ACCEPTED_HTLC_SCRIPT_WEIGHT and OFFERED_HTLC_SCRIPT_WEIGHT to ease readability Conditionnal compilation for weight of second one to handle test special cltv values --- src/ln/channel.rs | 6 +++++ src/ln/channelmanager.rs | 57 ++++++++++++++++++++-------------------- src/ln/channelmonitor.rs | 5 ++-- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/ln/channel.rs b/src/ln/channel.rs index fabad3b4f..6be2bb69c 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -364,6 +364,12 @@ const B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT: u64 = 104; // prevout: 40, nSequence: /// it's 2^24. pub const MAX_FUNDING_SATOSHIS: u64 = (1 << 24); +#[cfg(test)] +pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 138; //Here we have a diff due to HTLC CLTV expiry being < 2^15 in test +#[cfg(not(test))] +pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 139; +pub const OFFERED_HTLC_SCRIPT_WEIGHT: usize = 133; + /// Used to return a simple Error back to ChannelManager. Will get converted to a /// msgs::ErrorAction::SendErrorMessage or msgs::ErrorAction::IgnoreError as appropriate with our /// channel_id in ChannelManager. diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index ca85f6fce..ac6763894 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -3282,6 +3282,7 @@ mod tests { use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC}; use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,OnionKeys,PaymentFailReason,RAACommitmentOrder}; use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, CLTV_CLAIM_BUFFER, HTLC_FAIL_TIMEOUT_BLOCKS, ManyChannelMonitor}; + use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT}; use ln::router::{Route, RouteHop, Router}; use ln::msgs; use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler}; @@ -5838,7 +5839,7 @@ mod tests { assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to 0 are present assert_eq!(revoked_local_txn[1].input.len(), 1); assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid()); - assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), 133); // HTLC-Timeout + assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout // Revoke the old state claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3); @@ -5957,7 +5958,7 @@ mod tests { assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid()); assert_eq!(revoked_local_txn[1].input.len(), 1); assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid()); - assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), 133); // HTLC-Timeout + assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout check_spends!(revoked_local_txn[1], revoked_local_txn[0].clone()); //Revoke the old state @@ -5983,8 +5984,8 @@ mod tests { witness_lens.insert(node_txn[0].input[2].witness.last().unwrap().len()); assert_eq!(witness_lens.len(), 3); assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local - assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), 133); // revoked offered HTLC - assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), 138); // revoked received HTLC + assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC + assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC // Next nodes[1] broadcasts its current local tx state: assert_eq!(node_txn[1].input.len(), 1); @@ -5992,7 +5993,7 @@ mod tests { assert_eq!(node_txn[2].input.len(), 1); let witness_script = node_txn[2].clone().input[0].witness.pop().unwrap(); - assert_eq!(witness_script.len(), 133); //Spending an offered htlc output + assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output assert_eq!(node_txn[2].input[0].previous_output.txid, node_txn[1].txid()); assert_ne!(node_txn[2].input[0].previous_output.txid, node_txn[0].input[0].previous_output.txid); assert_ne!(node_txn[2].input[0].previous_output.txid, node_txn[0].input[1].previous_output.txid); @@ -6055,15 +6056,15 @@ mod tests { witness_lens.insert(node_txn[2].input[0].witness.last().unwrap().len()); assert_eq!(witness_lens.len(), 3); assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local - assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), 133); // revoked offered HTLC - assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), 138); // revoked received HTLC + assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC + assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC assert_eq!(node_txn[3].input.len(), 1); check_spends!(node_txn[3], chan_1.3.clone()); assert_eq!(node_txn[4].input.len(), 1); let witness_script = node_txn[4].input[0].witness.last().unwrap(); - assert_eq!(witness_script.len(), 133); //Spending an offered htlc output + assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output assert_eq!(node_txn[4].input[0].previous_output.txid, node_txn[3].txid()); assert_ne!(node_txn[4].input[0].previous_output.txid, node_txn[0].input[0].previous_output.txid); assert_ne!(node_txn[4].input[0].previous_output.txid, node_txn[1].input[0].previous_output.txid); @@ -6128,11 +6129,11 @@ mod tests { let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx) assert_eq!(node_txn.len(), 3); check_spends!(node_txn[0], commitment_tx[0].clone()); - assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138); + assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); check_spends!(node_txn[1], chan_2.3.clone()); check_spends!(node_txn[2], node_txn[1].clone()); assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71); - assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 138); + assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn}, 1); @@ -6163,12 +6164,12 @@ mod tests { assert_eq!(node_txn[0], node_txn[3]); check_spends!(node_txn[0], commitment_tx[0].clone()); check_spends!(node_txn[3], commitment_tx[0].clone()); - assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138); - assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138); + assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); + assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); check_spends!(node_txn[1], chan_2.3.clone()); check_spends!(node_txn[2], node_txn[1].clone()); assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71); - assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133); + assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); node_txn.clear() } @@ -6187,9 +6188,9 @@ mod tests { assert_eq!(node_txn.len(), 3); assert_eq!(node_txn[0], node_txn[2]); check_spends!(node_txn[0], commitment_tx[0].clone()); - assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 133); + assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); check_spends!(node_txn[2], commitment_tx[0].clone()); - assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133); + assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); check_spends!(node_txn[1], chan_1.3.clone()); assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71); let commitment_tx = node_txn[1].clone(); @@ -6207,12 +6208,12 @@ mod tests { assert_eq!(node_txn[0], node_txn[3]); check_spends!(node_txn[0], commitment_tx.clone()); check_spends!(node_txn[3], commitment_tx.clone()); - assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138); - assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138); + assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); + assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); check_spends!(node_txn[1], chan_1.3.clone()); check_spends!(node_txn[2], node_txn[1].clone()); assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71); - assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133); + assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); } #[test] @@ -6282,15 +6283,15 @@ mod tests { assert_eq!(node_txn[1], node_txn[6]); assert_eq!(node_txn[2], node_txn[7]); check_spends!(node_txn[0], commitment_tx[0].clone()); - assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138); + assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); check_spends!(node_txn[1], chan_2.3.clone()); check_spends!(node_txn[2], node_txn[1].clone()); assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71); - assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133); + assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); check_spends!(node_txn[3], chan_2.3.clone()); check_spends!(node_txn[4], node_txn[3].clone()); assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 71); - assert_eq!(node_txn[4].input[0].witness.clone().last().unwrap().len(), 133); + assert_eq!(node_txn[4].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); timeout_tx = node_txn[0].clone(); node_txn.clear(); } @@ -6335,11 +6336,11 @@ mod tests { assert_eq!(node_txn.len(), 4); assert_eq!(node_txn[0], node_txn[3]); check_spends!(node_txn[0], commitment_tx[0].clone()); - assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138); + assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); check_spends!(node_txn[1], chan_1.3.clone()); check_spends!(node_txn[2], node_txn[1].clone()); assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71); - assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133); + assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); } #[test] @@ -8201,7 +8202,7 @@ mod tests { let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); // ChannelManager : 1 (local commitment tx), ChannelMonitor: 2 (1 preimage tx) * 2 (block-rescan) check_spends!(node_txn[0], commitment_tx[0].clone()); assert_eq!(node_txn[0], node_txn[2]); - assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 133); + assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); check_spends!(node_txn[1], chan_1.3.clone()); let spend_txn = check_spendable_outputs!(nodes[1], 1); // , 0, 0, 1, 1); @@ -8269,7 +8270,7 @@ mod tests { assert_eq!(revoked_htlc_txn.len(), 3); assert_eq!(revoked_htlc_txn[0], revoked_htlc_txn[2]); assert_eq!(revoked_htlc_txn[0].input.len(), 1); - assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), 133); + assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone()); check_spends!(revoked_htlc_txn[1], chan_1.3.clone()); @@ -8321,7 +8322,7 @@ mod tests { assert_eq!(revoked_htlc_txn.len(), 3); assert_eq!(revoked_htlc_txn[0], revoked_htlc_txn[2]); assert_eq!(revoked_htlc_txn[0].input.len(), 1); - assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), 138); + assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone()); // A will generate justice tx from B's revoked commitment/HTLC tx @@ -8375,7 +8376,7 @@ mod tests { } let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_txn[0].input.len(), 1); - assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 138); + assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); check_spends!(node_txn[0], local_txn[0].clone()); // Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor @@ -8407,7 +8408,7 @@ mod tests { } let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_txn[0].input.len(), 1); - assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 133); + assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); check_spends!(node_txn[0], local_txn[0].clone()); // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor diff --git a/src/ln/channelmonitor.rs b/src/ln/channelmonitor.rs index f782b68b4..86b7ea17f 100644 --- a/src/ln/channelmonitor.rs +++ b/src/ln/channelmonitor.rs @@ -31,6 +31,7 @@ use ln::chan_utils; use ln::chan_utils::HTLCOutputInCommitment; use ln::channelmanager::{HTLCSource, HTLCPreviousHopData}; use ln::router::{Route, RouteHop}; +use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT}; use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface}; use chain::transaction::OutPoint; use chain::keysinterface::SpendableOutputDescriptor; @@ -1915,12 +1916,12 @@ impl ChannelMonitor { if payment_data.0.is_some() && payment_data.2.is_some() { let mut payment_preimage = [0; 32]; let mut preimage = None; - if input.witness.len() == 5 && input.witness[4].len() == 138 { + if input.witness.len() == 5 && input.witness[4].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT { for (arr, vec) in payment_preimage.iter_mut().zip(tx.input[0].witness[3].iter()) { *arr = *vec; } preimage = Some(payment_preimage); - } else if input.witness.len() == 3 && input.witness[2].len() == 133 { + } else if input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT { for (arr, vec) in payment_preimage.iter_mut().zip(tx.input[0].witness[1].iter()) { *arr = *vec; } -- 2.39.5