From b1e313f26de90771b3570eac2282bad8e68ef95a 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 | 27 ++++++++++++++------------- src/ln/channelmonitor.rs | 5 +++-- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/ln/channel.rs b/src/ln/channel.rs index 2b29fb5f..1823b63c 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 a30f904e..70db9092 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -3355,6 +3355,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}; @@ -5887,7 +5888,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); @@ -6006,7 +6007,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 @@ -6040,8 +6041,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); @@ -6049,7 +6050,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); @@ -6120,15 +6121,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); @@ -8408,7 +8409,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); @@ -8476,7 +8477,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()); @@ -8528,7 +8529,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 @@ -8582,7 +8583,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 @@ -8614,7 +8615,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 5028f2d6..fb7dc805 100644 --- a/src/ln/channelmonitor.rs +++ b/src/ln/channelmonitor.rs @@ -30,6 +30,7 @@ use ln::msgs::DecodeError; use ln::chan_utils; use ln::chan_utils::HTLCOutputInCommitment; use ln::channelmanager::HTLCSource; +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; @@ -1812,10 +1813,10 @@ impl ChannelMonitor { // to broadcast solving backward if let Some((source, payment_hash)) = payment_data { let mut payment_preimage = [0; 32]; - if input.witness.len() == 5 && input.witness[4].len() == 138 { + if input.witness.len() == 5 && input.witness[4].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT { payment_preimage.copy_from_slice(&tx.input[0].witness[3]); htlc_updated.push((source, Some(payment_preimage), payment_hash)); - } 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 { payment_preimage.copy_from_slice(&tx.input[0].witness[1]); htlc_updated.push((source, Some(payment_preimage), payment_hash)); } else { -- 2.30.2