Skip to content

Commit c906f28

Browse files
authored
Merge pull request #461 from ariard/2020-remove-duplicata
Remove some duplicata of broadcast txn from ChannelMonitor
2 parents 3c9e8c9 + 494219e commit c906f28

File tree

4 files changed

+107
-94
lines changed

4 files changed

+107
-94
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,11 +1799,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17991799
let mut inputs_info = Vec::new();
18001800

18011801
macro_rules! sign_input {
1802-
($sighash_parts: expr, $input: expr, $amount: expr, $preimage: expr) => {
1802+
($sighash_parts: expr, $input: expr, $amount: expr, $preimage: expr, $idx: expr) => {
18031803
{
18041804
let (sig, redeemscript, htlc_key) = match self.key_storage {
18051805
Storage::Local { ref htlc_base_key, .. } => {
1806-
let htlc = &per_commitment_option.unwrap()[$input.sequence as usize].0;
1806+
let htlc = &per_commitment_option.unwrap()[$idx as usize].0;
18071807
let redeemscript = chan_utils::get_htlc_redeemscript_with_explicit_keys(htlc, &a_htlc_key, &b_htlc_key, &revocation_pubkey);
18081808
let sighash = hash_to_message!(&$sighash_parts.sighash_all(&$input, &redeemscript, $amount)[..]);
18091809
let htlc_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &htlc_base_key));
@@ -1832,19 +1832,19 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18321832
}
18331833
if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
18341834
if htlc.offered {
1835-
let input = TxIn {
1835+
let mut input = TxIn {
18361836
previous_output: BitcoinOutPoint {
18371837
txid: commitment_txid,
18381838
vout: transaction_output_index,
18391839
},
18401840
script_sig: Script::new(),
1841-
sequence: idx as u32, // reset to 0xfffffffd in sign_input
1841+
sequence: 0xff_ff_ff_fd,
18421842
witness: Vec::new(),
18431843
};
18441844
if htlc.cltv_expiry > height + CLTV_SHARED_CLAIM_BUFFER {
18451845
inputs.push(input);
18461846
inputs_desc.push(if htlc.offered { InputDescriptors::OfferedHTLC } else { InputDescriptors::ReceivedHTLC });
1847-
inputs_info.push((payment_preimage, tx.output[transaction_output_index as usize].value, htlc.cltv_expiry));
1847+
inputs_info.push((payment_preimage, tx.output[transaction_output_index as usize].value, htlc.cltv_expiry, idx));
18481848
total_value += tx.output[transaction_output_index as usize].value;
18491849
} else {
18501850
let mut single_htlc_tx = Transaction {
@@ -1861,7 +1861,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18611861
let mut used_feerate;
18621862
if subtract_high_prio_fee!(self, fee_estimator, single_htlc_tx.output[0].value, predicted_weight, used_feerate) {
18631863
let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
1864-
let (redeemscript, htlc_key) = sign_input!(sighash_parts, single_htlc_tx.input[0], htlc.amount_msat / 1000, payment_preimage.0.to_vec());
1864+
let (redeemscript, htlc_key) = sign_input!(sighash_parts, single_htlc_tx.input[0], htlc.amount_msat / 1000, payment_preimage.0.to_vec(), idx);
18651865
assert!(predicted_weight >= single_htlc_tx.get_weight());
18661866
spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
18671867
outpoint: BitcoinOutPoint { txid: single_htlc_tx.txid(), vout: 0 },
@@ -1892,7 +1892,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18921892
vout: transaction_output_index,
18931893
},
18941894
script_sig: Script::new(),
1895-
sequence: idx as u32,
1895+
sequence: 0xff_ff_ff_fd,
18961896
witness: Vec::new(),
18971897
};
18981898
let mut timeout_tx = Transaction {
@@ -1909,7 +1909,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
19091909
let mut used_feerate;
19101910
if subtract_high_prio_fee!(self, fee_estimator, timeout_tx.output[0].value, predicted_weight, used_feerate) {
19111911
let sighash_parts = bip143::SighashComponents::new(&timeout_tx);
1912-
let (redeemscript, htlc_key) = sign_input!(sighash_parts, timeout_tx.input[0], htlc.amount_msat / 1000, vec![0]);
1912+
let (redeemscript, htlc_key) = sign_input!(sighash_parts, timeout_tx.input[0], htlc.amount_msat / 1000, vec![0], idx);
19131913
assert!(predicted_weight >= timeout_tx.get_weight());
19141914
//TODO: track SpendableOutputDescriptor
19151915
log_trace!(self, "Outpoint {}:{} is being being claimed, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", timeout_tx.input[0].previous_output.txid, timeout_tx.input[0].previous_output.vout, height_timer);
@@ -1961,7 +1961,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
19611961
let height_timer = Self::get_height_timer(height, soonest_timelock);
19621962
let spend_txid = spend_tx.txid();
19631963
for (input, info) in spend_tx.input.iter_mut().zip(inputs_info.iter()) {
1964-
let (redeemscript, htlc_key) = sign_input!(sighash_parts, input, info.1, (info.0).0.to_vec());
1964+
let (redeemscript, htlc_key) = sign_input!(sighash_parts, input, info.1, (info.0).0.to_vec(), info.3);
19651965
log_trace!(self, "Outpoint {}:{} is being being claimed, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", input.previous_output.txid, input.previous_output.vout, height_timer);
19661966
per_input_material.insert(input.previous_output, InputMaterial::RemoteHTLC { script: redeemscript, key: htlc_key, preimage: Some(*(info.0)), amount: info.1, locktime: 0});
19671967
match self.claimable_outpoints.entry(input.previous_output) {
@@ -2908,7 +2908,6 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
29082908
for per_outp_material in cached_claim_datas.per_input_material.values() {
29092909
match per_outp_material {
29102910
&InputMaterial::Revoked { ref script, ref is_htlc, ref amount, .. } => {
2911-
log_trace!(self, "Is HLTC ? {}", is_htlc);
29122911
inputs_witnesses_weight += Self::get_witnesses_weight(if !is_htlc { &[InputDescriptors::RevokedOutput] } else if HTLCType::scriptlen_to_htlctype(script.len()) == Some(HTLCType::OfferedHTLC) { &[InputDescriptors::RevokedOfferedHTLC] } else if HTLCType::scriptlen_to_htlctype(script.len()) == Some(HTLCType::AcceptedHTLC) { &[InputDescriptors::RevokedReceivedHTLC] } else { unreachable!() });
29132912
amt += *amount;
29142913
},

lightning/src/ln/functional_test_utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use std::cell::RefCell;
3535
use std::rc::Rc;
3636
use std::sync::{Arc, Mutex};
3737
use std::mem;
38+
use std::collections::HashSet;
3839

3940
pub const CHAN_CONFIRM_DEPTH: u32 = 100;
4041
pub fn confirm_transaction<'a, 'b: 'a>(notifier: &'a chaininterface::BlockNotifierRef<'b>, chain: &chaininterface::ChainWatchInterfaceUtil, tx: &Transaction, chan_id: u32) {
@@ -857,7 +858,7 @@ pub fn create_node_cfgs(node_count: usize) -> Vec<NodeCfg> {
857858
let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
858859
let fee_estimator = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
859860
let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, logger.clone() as Arc<Logger>));
860-
let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
861+
let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), broadcasted_txn: Mutex::new(HashSet::new())});
861862
let mut seed = [0; 32];
862863
rng.fill_bytes(&mut seed);
863864
let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, logger.clone() as Arc<Logger>));

0 commit comments

Comments
 (0)