Skip to content

Commit 6d8b85c

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 3705440 commit 6d8b85c

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;
@@ -111,15 +111,62 @@ impl Readable for ClaimTxBumpMaterial {
111111
}
112112
}
113113

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

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

619666
for (i, (outp, per_outp_material)) in cached_claim_datas.per_input_material.iter().enumerate() {
620667
match per_outp_material {
621-
&InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref is_htlc, ref amount } => {
668+
&InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref input_descriptor, ref amount } => {
622669
let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
623670
let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[i], &witness_script, *amount)[..]);
624671
let sig = self.secp_ctx.sign(&sighash, &key);
625672
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
626673
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
627-
if *is_htlc {
674+
if *input_descriptor != InputDescriptors::RevokedOutput {
628675
bumped_tx.input[i].witness.push(pubkey.unwrap().clone().serialize().to_vec());
629676
} else {
630677
bumped_tx.input[i].witness.push(vec!(1));
631678
}
632679
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
633-
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);
680+
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);
634681
},
635682
&InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
636683
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)