Skip to content

Commit cd9a66e

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 8cd604b commit cd9a66e

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};
@@ -428,7 +428,7 @@ pub(crate) enum InputMaterial {
428428
witness_script: Script,
429429
pubkey: Option<PublicKey>,
430430
key: SecretKey,
431-
is_htlc: bool,
431+
input_descriptor: InputDescriptors,
432432
amount: u64,
433433
},
434434
RemoteHTLC {
@@ -450,12 +450,12 @@ pub(crate) enum InputMaterial {
450450
impl Writeable for InputMaterial {
451451
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
452452
match self {
453-
&InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref is_htlc, ref amount} => {
453+
&InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref input_descriptor, ref amount} => {
454454
writer.write_all(&[0; 1])?;
455455
witness_script.write(writer)?;
456456
pubkey.write(writer)?;
457457
writer.write_all(&key[..])?;
458-
is_htlc.write(writer)?;
458+
input_descriptor.write(writer)?;
459459
writer.write_all(&byte_utils::be64_to_array(*amount))?;
460460
},
461461
&InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
@@ -487,13 +487,13 @@ impl Readable for InputMaterial {
487487
let witness_script = Readable::read(reader)?;
488488
let pubkey = Readable::read(reader)?;
489489
let key = Readable::read(reader)?;
490-
let is_htlc = Readable::read(reader)?;
490+
let input_descriptor = Readable::read(reader)?;
491491
let amount = Readable::read(reader)?;
492492
InputMaterial::Revoked {
493493
witness_script,
494494
pubkey,
495495
key,
496-
is_htlc,
496+
input_descriptor,
497497
amount
498498
}
499499
},
@@ -1474,7 +1474,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
14741474
// First, process non-htlc outputs (to_local & to_remote)
14751475
for (idx, outp) in tx.output.iter().enumerate() {
14761476
if outp.script_pubkey == revokeable_p2wsh {
1477-
let witness_data = InputMaterial::Revoked { witness_script: revokeable_redeemscript.clone(), pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: false, amount: outp.value };
1477+
let witness_data = InputMaterial::Revoked { witness_script: revokeable_redeemscript.clone(), pubkey: Some(revocation_pubkey), key: revocation_key, input_descriptor: InputDescriptors::RevokedOutput, amount: outp.value };
14781478
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});
14791479
}
14801480
}
@@ -1489,7 +1489,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
14891489
tx.output[transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
14901490
return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
14911491
}
1492-
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 };
1492+
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 };
14931493
claimable_outpoints.push(ClaimRequest { absolute_timelock: htlc.cltv_expiry, aggregable: true, outpoint: BitcoinOutPoint { txid: commitment_txid, vout: transaction_output_index }, witness_data });
14941494
}
14951495
}
@@ -1668,7 +1668,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
16681668
let redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.our_to_self_delay, &delayed_key);
16691669

16701670
log_trace!(self, "Remote HTLC broadcast {}:{}", htlc_txid, 0);
1671-
let witness_data = InputMaterial::Revoked { witness_script: redeemscript, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: false, amount: tx.output[0].value };
1671+
let witness_data = InputMaterial::Revoked { witness_script: redeemscript, pubkey: Some(revocation_pubkey), key: revocation_key, input_descriptor: InputDescriptors::RevokedOutput, amount: tx.output[0].value };
16721672
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 });
16731673
(claimable_outpoints, Some((htlc_txid, tx.output.clone())))
16741674
}

lightning/src/ln/onchaintx.rs

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use secp256k1;
1717
use ln::msgs::DecodeError;
1818
use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
1919
use ln::channelmanager::PaymentPreimage;
20-
use ln::chan_utils::{HTLCType, LocalCommitmentTransaction, HTLCOutputInCommitment};
20+
use ln::chan_utils::{LocalCommitmentTransaction, HTLCOutputInCommitment};
2121
use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
2222
use chain::keysinterface::ChannelKeys;
2323
use util::logger::Logger;
@@ -102,15 +102,62 @@ impl Readable for ClaimTxBumpMaterial {
102102
}
103103
}
104104

105-
#[derive(PartialEq)]
106-
pub(super) enum InputDescriptors {
105+
#[derive(PartialEq, Clone, Copy)]
106+
pub(crate) enum InputDescriptors {
107107
RevokedOfferedHTLC,
108108
RevokedReceivedHTLC,
109109
OfferedHTLC,
110110
ReceivedHTLC,
111111
RevokedOutput, // either a revoked to_local output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
112112
}
113113

114+
impl Writeable for InputDescriptors {
115+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
116+
match self {
117+
&InputDescriptors::RevokedOfferedHTLC => {
118+
writer.write_all(&[0; 1])?;
119+
},
120+
&InputDescriptors::RevokedReceivedHTLC => {
121+
writer.write_all(&[1; 1])?;
122+
},
123+
&InputDescriptors::OfferedHTLC => {
124+
writer.write_all(&[2; 1])?;
125+
},
126+
&InputDescriptors::ReceivedHTLC => {
127+
writer.write_all(&[3; 1])?;
128+
}
129+
&InputDescriptors::RevokedOutput => {
130+
writer.write_all(&[4; 1])?;
131+
}
132+
}
133+
Ok(())
134+
}
135+
}
136+
137+
impl Readable for InputDescriptors {
138+
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
139+
let input_descriptor = match <u8 as Readable>::read(reader)? {
140+
0 => {
141+
InputDescriptors::RevokedOfferedHTLC
142+
},
143+
1 => {
144+
InputDescriptors::RevokedReceivedHTLC
145+
},
146+
2 => {
147+
InputDescriptors::OfferedHTLC
148+
},
149+
3 => {
150+
InputDescriptors::ReceivedHTLC
151+
},
152+
4 => {
153+
InputDescriptors::RevokedOutput
154+
}
155+
_ => return Err(DecodeError::InvalidValue),
156+
};
157+
Ok(input_descriptor)
158+
}
159+
}
160+
114161
macro_rules! subtract_high_prio_fee {
115162
($self: ident, $fee_estimator: expr, $value: expr, $predicted_weight: expr, $used_feerate: expr) => {
116163
{
@@ -548,8 +595,8 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
548595
let mut dynamic_fee = true;
549596
for per_outp_material in cached_claim_datas.per_input_material.values() {
550597
match per_outp_material {
551-
&InputMaterial::Revoked { ref witness_script, ref is_htlc, ref amount, .. } => {
552-
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!() });
598+
&InputMaterial::Revoked { ref input_descriptor, ref amount, .. } => {
599+
inputs_witnesses_weight += Self::get_witnesses_weight(&[*input_descriptor]);
553600
amt += *amount;
554601
},
555602
&InputMaterial::RemoteHTLC { ref preimage, ref amount, .. } => {
@@ -587,19 +634,19 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
587634

588635
for (i, (outp, per_outp_material)) in cached_claim_datas.per_input_material.iter().enumerate() {
589636
match per_outp_material {
590-
&InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref is_htlc, ref amount } => {
637+
&InputMaterial::Revoked { ref witness_script, ref pubkey, ref key, ref input_descriptor, ref amount } => {
591638
let sighash_parts = bip143::SighashComponents::new(&bumped_tx);
592639
let sighash = hash_to_message!(&sighash_parts.sighash_all(&bumped_tx.input[i], &witness_script, *amount)[..]);
593640
let sig = self.secp_ctx.sign(&sighash, &key);
594641
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
595642
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
596-
if *is_htlc {
643+
if *input_descriptor != InputDescriptors::RevokedOutput {
597644
bumped_tx.input[i].witness.push(pubkey.unwrap().clone().serialize().to_vec());
598645
} else {
599646
bumped_tx.input[i].witness.push(vec!(1));
600647
}
601648
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
602-
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);
649+
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);
603650
},
604651
&InputMaterial::RemoteHTLC { ref witness_script, ref key, ref preimage, ref amount, ref locktime } => {
605652
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)