Skip to content

Commit bb37a75

Browse files
author
Antoine Riard
committed
Replace is_htlc in InputMaterial by InputDescriptor
As we cache more and more transaction elements in OnchainTxHandler we should dry up completly InputMaterial until them being replaced directly by InputDescriptor
1 parent 578cce6 commit bb37a75

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use ln::msgs::DecodeError;
3232
use ln::chan_utils;
3333
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, LocalCommitmentTransaction, HTLCType};
3434
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
35-
use ln::onchaintx::OnchainTxHandler;
35+
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
3636
use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator};
3737
use chain::transaction::OutPoint;
3838
use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
@@ -427,7 +427,7 @@ pub(crate) enum InputMaterial {
427427
witness_script: Script,
428428
pubkey: Option<PublicKey>,
429429
key: SecretKey,
430-
is_htlc: bool,
430+
input_descriptor: InputDescriptors,
431431
amount: u64,
432432
},
433433
RemoteHTLC {
@@ -449,12 +449,12 @@ pub(crate) enum InputMaterial {
449449
impl Writeable for InputMaterial {
450450
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
451451
match self {
452-
&InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref is_htlc, ref amount} => {
452+
&InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref input_descriptor, ref amount} => {
453453
writer.write_all(&[0; 1])?;
454454
witness_script.write(writer)?;
455455
pubkey.write(writer)?;
456456
writer.write_all(&key[..])?;
457-
is_htlc.write(writer)?;
457+
input_descriptor.write(writer)?;
458458
writer.write_all(&byte_utils::be64_to_array(*amount))?;
459459
},
460460
&InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
@@ -486,13 +486,13 @@ impl Readable for InputMaterial {
486486
let witness_script = Readable::read(reader)?;
487487
let pubkey = Readable::read(reader)?;
488488
let key = Readable::read(reader)?;
489-
let is_htlc = Readable::read(reader)?;
489+
let input_descriptor = Readable::read(reader)?;
490490
let amount = Readable::read(reader)?;
491491
InputMaterial::Revoked {
492492
witness_script,
493493
pubkey,
494494
key,
495-
is_htlc,
495+
input_descriptor,
496496
amount
497497
}
498498
},
@@ -1471,7 +1471,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
14711471
// First, process non-htlc outputs (to_local & to_remote)
14721472
for (idx, outp) in tx.output.iter().enumerate() {
14731473
if outp.script_pubkey == revokeable_p2wsh {
1474-
let witness_data = InputMaterial::Revoked { witness_script: revokeable_redeemscript.clone(), pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: false, amount: outp.value };
1474+
let witness_data = InputMaterial::Revoked { witness_script: revokeable_redeemscript.clone(), pubkey: Some(revocation_pubkey), key: revocation_key, input_descriptor: InputDescriptors::RevokedOutput, amount: outp.value };
14751475
claimable_outpoints.push(ClaimRequest { absolute_timelock: height + self.our_to_self_delay as u32, aggregable: true, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 }, witness_data});
14761476
}
14771477
}
@@ -1486,7 +1486,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
14861486
tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
14871487
return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
14881488
}
1489-
let witness_data = InputMaterial::Revoked { witness_script: expected_script, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: true, amount: tx.output[transaction_output_index as usize].value };
1489+
let witness_data = InputMaterial::Revoked { witness_script: expected_script, pubkey: Some(revocation_pubkey), key: revocation_key, input_descriptor: if htlc.offered { InputDescriptors::RevokedOfferedHTLC } else { InputDescriptors::RevokedReceivedHTLC }, amount: tx.output[transaction_output_index as usize].value };
14901490
claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable: true, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: transaction_output_index }, witness_data });
14911491
}
14921492
}
@@ -1671,7 +1671,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16711671
let redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.our_to_self_delay, &delayed_key);
16721672

16731673
log_trace!(self, "Remote HTLC broadcast {}:{}", htlc_txid, 0);
1674-
let witness_data = InputMaterial::Revoked { witness_script: redeemscript, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: false, amount: tx.output[0].value };
1674+
let witness_data = InputMaterial::Revoked { witness_script: redeemscript, pubkey: Some(revocation_pubkey), key: revocation_key, input_descriptor: InputDescriptors::RevokedOutput, amount: tx.output[0].value };
16751675
let claimable_outpoints = vec!(ClaimRequest { absolute_timelock: height + self.our_to_self_delay as u32, aggregable: true, outpoint: BitcoinOutPoint { txid: htlc_txid, vout: 0}, witness_data });
16761676
(claimable_outpoints, Some((htlc_txid, tx.output.clone())))
16771677
}

lightning/src/ln/onchaintx.rs

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ln::msgs::DecodeError;
1818
use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
1919
use ln::channelmanager::{HTLCSource, PaymentPreimage};
2020
use ln::chan_utils;
21-
use ln::chan_utils::{HTLCType, TxCreationKeys, HTLCOutputInCommitment};
21+
use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment};
2222
use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
2323
use chain::keysinterface::ChannelKeys;
2424
use util::logger::Logger;
@@ -112,15 +112,62 @@ impl Readable for ClaimTxBumpMaterial {
112112
}
113113
}
114114

115-
#[derive(PartialEq)]
116-
pub(super) enum InputDescriptors {
115+
#[derive(PartialEq, Clone, Copy)]
116+
pub(crate) enum InputDescriptors {
117117
RevokedOfferedHTLC,
118118
RevokedReceivedHTLC,
119119
OfferedHTLC,
120120
ReceivedHTLC,
121121
RevokedOutput, // either a revoked to_local output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
122122
}
123123

124+
impl Writeable for InputDescriptors {
125+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
126+
match self {
127+
&InputDescriptors::RevokedOfferedHTLC => {
128+
writer.write_all(&[0; 1])?;
129+
},
130+
&InputDescriptors::RevokedReceivedHTLC => {
131+
writer.write_all(&[1; 1])?;
132+
},
133+
&InputDescriptors::OfferedHTLC => {
134+
writer.write_all(&[2; 1])?;
135+
},
136+
&InputDescriptors::ReceivedHTLC => {
137+
writer.write_all(&[3; 1])?;
138+
}
139+
&InputDescriptors::RevokedOutput => {
140+
writer.write_all(&[4; 1])?;
141+
}
142+
}
143+
Ok(())
144+
}
145+
}
146+
147+
impl Readable for InputDescriptors {
148+
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
149+
let input_descriptor = match <u8 as Readable>::read(reader)? {
150+
0 => {
151+
InputDescriptors::RevokedOfferedHTLC
152+
},
153+
1 => {
154+
InputDescriptors::RevokedReceivedHTLC
155+
},
156+
2 => {
157+
InputDescriptors::OfferedHTLC
158+
},
159+
3 => {
160+
InputDescriptors::ReceivedHTLC
161+
},
162+
4 => {
163+
InputDescriptors::RevokedOutput
164+
}
165+
_ => return Err(DecodeError::InvalidValue),
166+
};
167+
Ok(input_descriptor)
168+
}
169+
}
170+
124171
macro_rules! subtract_high_prio_fee {
125172
($self: ident, $fee_estimator: expr, $value: expr, $predicted_weight: expr, $used_feerate: expr) => {
126173
{
@@ -580,8 +627,8 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
580627
let mut dynamic_fee = true;
581628
for per_outp_material in cached_claim_datas.per_input_material.values() {
582629
match per_outp_material {
583-
&InputMaterial::Revoked { ref witness_script, ref is_htlc, ref amount, .. } => {
584-
inputs_witnesses_weight += Self::get_witnesses_weight(if !is_htlc { &[InputDescriptors::RevokedOutput] } else if HTLCType::scriptlen_to_htlctype(witness_script.len()) == Some(HTLCType::OfferedHTLC) { &[InputDescriptors::RevokedOfferedHTLC] } else if HTLCType::scriptlen_to_htlctype(witness_script.len()) == Some(HTLCType::AcceptedHTLC) { &[InputDescriptors::RevokedReceivedHTLC] } else { unreachable!() });
630+
&InputMaterial::Revoked { ref input_descriptor, ref amount, .. } => {
631+
inputs_witnesses_weight += Self::get_witnesses_weight(&[*input_descriptor]);
585632
amt += *amount;
586633
},
587634
&InputMaterial::RemoteHTLC { ref preimage, ref amount, .. } => {
@@ -619,19 +666,19 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
619666

620667
for (i, (outp, per_outp_material)) in cached_claim_datas.per_input_material.iter().enumerate() {
621668
match per_outp_material {
622-
&InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref is_htlc, ref amount } => {
669+
&InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref input_descriptor, ref amount } => {
623670
let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
624671
let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[i], &witness_script, *amount)[..]);
625672
let sig = self.secp_ctx.sign(&sighash, &key);
626673
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
627674
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
628-
if *is_htlc {
675+
if *input_descriptor != InputDescriptors::RevokedOutput {
629676
bumped_tx.input[i].witness.push(pubkey.unwrap().clone().serialize().to_vec());
630677
} else {
631678
bumped_tx.input[i].witness.push(vec!(1));
632679
}
633680
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
634-
log_trace!(self, "Going to broadcast Penalty Transaction {} claiming revoked {} output {} from {} with new feerate {}...", bumped_tx.txid(), if !is_htlc { "to_local" } else if HTLCType::scriptlen_to_htlctype(witness_script.len()) == Some(HTLCType::OfferedHTLC) { "offered" } else if HTLCType::scriptlen_to_htlctype(witness_script.len()) == Some(HTLCType::AcceptedHTLC) { "received" } else { "" }, outp.vout, outp.txid, new_feerate);
681+
log_trace!(self, "Going to broadcast Penalty Transaction {} claiming revoked {} output {} from {} with new feerate {}...", bumped_tx.txid(), if *input_descriptor == InputDescriptors::RevokedOutput { "to_local" } else if *input_descriptor == InputDescriptors::RevokedOfferedHTLC { "offered" } else if *input_descriptor == InputDescriptors::RevokedReceivedHTLC { "received" } else { "" }, outp.vout, outp.txid, new_feerate);
635682
},
636683
&InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
637684
if !preimage.is_some() { bumped_tx.lock_time = *locktime }; // Right now we don't aggregate time-locked transaction, if we do we should set lock_time before to avoid breaking hash computation

0 commit comments

Comments
 (0)