Skip to content

Commit eecea92

Browse files
author
Antoine Riard
committed
Add package.rs file
Package.rs aims to gather interfaces to communicate between onchain channel transactions parser (ChannelMonitor) and outputs claiming logic (OnchainTxHandler). These interfaces are data structures, generated per-case by ChannelMonitor and consumed blindly by OnchainTxHandler.
1 parent cea954c commit eecea92

File tree

4 files changed

+105
-96
lines changed

4 files changed

+105
-96
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ use ln::msgs::DecodeError;
3939
use ln::chan_utils;
4040
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCType, ChannelTransactionParameters, HolderCommitmentTransaction};
4141
use ln::channelmanager::{BestBlock, HTLCSource};
42-
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
42+
use ln::onchaintx::OnchainTxHandler;
43+
use ln::package::InputDescriptors;
4344
use chain;
4445
use chain::WatchedOutput;
4546
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
@@ -352,7 +353,7 @@ pub(crate) enum InputMaterial {
352353
}
353354
}
354355

355-
impl Writeable for InputMaterial {
356+
impl Writeable for InputMaterial {
356357
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
357358
match self {
358359
&InputMaterial::Revoked { ref per_commitment_point, ref counterparty_delayed_payment_base_key, ref counterparty_htlc_base_key, ref per_commitment_key, ref input_descriptor, ref amount, ref htlc, ref on_counterparty_tx_csv} => {
@@ -3052,7 +3053,8 @@ mod tests {
30523053
use chain::transaction::OutPoint;
30533054
use ln::{PaymentPreimage, PaymentHash};
30543055
use ln::channelmanager::BestBlock;
3055-
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
3056+
use ln::package;
3057+
use ln::package::InputDescriptors;
30563058
use ln::chan_utils;
30573059
use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
30583060
use util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator};
@@ -3266,7 +3268,7 @@ mod tests {
32663268
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
32673269
}
32683270
}
3269-
assert_eq!(base_weight + OnchainTxHandler::<InMemorySigner>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
3271+
assert_eq!(base_weight + package::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
32703272

32713273
// Claim tx with 1 offered HTLCs, 3 received HTLCs
32723274
claim_tx.input.clear();
@@ -3290,7 +3292,7 @@ mod tests {
32903292
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
32913293
}
32923294
}
3293-
assert_eq!(base_weight + OnchainTxHandler::<InMemorySigner>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
3295+
assert_eq!(base_weight + package::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
32943296

32953297
// Justice tx with 1 revoked HTLC-Success tx output
32963298
claim_tx.input.clear();
@@ -3312,7 +3314,7 @@ mod tests {
33123314
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
33133315
}
33143316
}
3315-
assert_eq!(base_weight + OnchainTxHandler::<InMemorySigner>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
3317+
assert_eq!(base_weight + package::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
33163318
}
33173319

33183320
// Further testing is done in the ChannelManager integration tests.

lightning/src/ln/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub mod peer_handler;
2828
pub mod chan_utils;
2929
pub mod features;
3030
pub(crate) mod onchaintx;
31+
pub(crate) mod package;
3132

3233
#[cfg(feature = "fuzztarget")]
3334
pub mod peer_channel_encryptor;

lightning/src/ln/onchaintx.rs

Lines changed: 6 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use ln::msgs::DecodeError;
2525
use ln::PaymentPreimage;
2626
use ln::chan_utils;
2727
use ln::chan_utils::{TxCreationKeys, ChannelTransactionParameters, HolderCommitmentTransaction};
28+
use ln::package::InputDescriptors;
29+
use ln::package;
2830
use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
2931
use chain::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
3032
use chain::keysinterface::{Sign, KeysInterface};
@@ -123,62 +125,6 @@ impl Readable for ClaimTxBumpMaterial {
123125
}
124126
}
125127

126-
#[derive(PartialEq, Clone, Copy)]
127-
pub(crate) enum InputDescriptors {
128-
RevokedOfferedHTLC,
129-
RevokedReceivedHTLC,
130-
OfferedHTLC,
131-
ReceivedHTLC,
132-
RevokedOutput, // either a revoked to_holder output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
133-
}
134-
135-
impl Writeable for InputDescriptors {
136-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
137-
match self {
138-
&InputDescriptors::RevokedOfferedHTLC => {
139-
writer.write_all(&[0; 1])?;
140-
},
141-
&InputDescriptors::RevokedReceivedHTLC => {
142-
writer.write_all(&[1; 1])?;
143-
},
144-
&InputDescriptors::OfferedHTLC => {
145-
writer.write_all(&[2; 1])?;
146-
},
147-
&InputDescriptors::ReceivedHTLC => {
148-
writer.write_all(&[3; 1])?;
149-
}
150-
&InputDescriptors::RevokedOutput => {
151-
writer.write_all(&[4; 1])?;
152-
}
153-
}
154-
Ok(())
155-
}
156-
}
157-
158-
impl Readable for InputDescriptors {
159-
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
160-
let input_descriptor = match <u8 as Readable>::read(reader)? {
161-
0 => {
162-
InputDescriptors::RevokedOfferedHTLC
163-
},
164-
1 => {
165-
InputDescriptors::RevokedReceivedHTLC
166-
},
167-
2 => {
168-
InputDescriptors::OfferedHTLC
169-
},
170-
3 => {
171-
InputDescriptors::ReceivedHTLC
172-
},
173-
4 => {
174-
InputDescriptors::RevokedOutput
175-
}
176-
_ => return Err(DecodeError::InvalidValue),
177-
};
178-
Ok(input_descriptor)
179-
}
180-
}
181-
182128
macro_rules! subtract_high_prio_fee {
183129
($logger: ident, $fee_estimator: expr, $value: expr, $predicted_weight: expr, $used_feerate: expr) => {
184130
{
@@ -271,7 +217,7 @@ pub struct OnchainTxHandler<ChannelSigner: Sign> {
271217
prev_holder_commitment: Option<HolderCommitmentTransaction>,
272218
prev_holder_htlc_sigs: Option<Vec<Option<(usize, Signature)>>>,
273219

274-
signer: ChannelSigner,
220+
pub(super) signer: ChannelSigner,
275221
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
276222

277223
// Used to track claiming requests. If claim tx doesn't confirm before height timer expiration we need to bump
@@ -305,7 +251,7 @@ pub struct OnchainTxHandler<ChannelSigner: Sign> {
305251

306252
latest_height: u32,
307253

308-
secp_ctx: Secp256k1<secp256k1::All>,
254+
pub(super) secp_ctx: Secp256k1<secp256k1::All>,
309255
}
310256

311257
impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
@@ -460,36 +406,6 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
460406
}
461407
}
462408

463-
pub(crate) fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize {
464-
let mut tx_weight = 2; // count segwit flags
465-
for inp in inputs {
466-
// We use expected weight (and not actual) as signatures and time lock delays may vary
467-
tx_weight += match inp {
468-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
469-
&InputDescriptors::RevokedOfferedHTLC => {
470-
1 + 1 + 73 + 1 + 33 + 1 + 133
471-
},
472-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
473-
&InputDescriptors::RevokedReceivedHTLC => {
474-
1 + 1 + 73 + 1 + 33 + 1 + 139
475-
},
476-
// number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script
477-
&InputDescriptors::OfferedHTLC => {
478-
1 + 1 + 73 + 1 + 32 + 1 + 133
479-
},
480-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
481-
&InputDescriptors::ReceivedHTLC => {
482-
1 + 1 + 73 + 1 + 1 + 1 + 139
483-
},
484-
// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
485-
&InputDescriptors::RevokedOutput => {
486-
1 + 1 + 73 + 1 + 1 + 1 + 77
487-
},
488-
};
489-
}
490-
tx_weight
491-
}
492-
493409
/// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
494410
/// output detection, we generate a first version of a claim tx and associate to it a height timer. A height timer is an absolute block
495411
/// height than once reached we should generate a new bumped "version" of the claim tx to be sure than we safely claim outputs before
@@ -581,11 +497,11 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
581497
for per_outp_material in cached_claim_datas.per_input_material.values() {
582498
match per_outp_material {
583499
&InputMaterial::Revoked { ref input_descriptor, ref amount, .. } => {
584-
inputs_witnesses_weight += Self::get_witnesses_weight(&[*input_descriptor]);
500+
inputs_witnesses_weight += package::get_witnesses_weight(&[*input_descriptor]);
585501
amt += *amount;
586502
},
587503
&InputMaterial::CounterpartyHTLC { ref preimage, ref htlc, .. } => {
588-
inputs_witnesses_weight += Self::get_witnesses_weight(if preimage.is_some() { &[InputDescriptors::OfferedHTLC] } else { &[InputDescriptors::ReceivedHTLC] });
504+
inputs_witnesses_weight += package::get_witnesses_weight(if preimage.is_some() { &[InputDescriptors::OfferedHTLC] } else { &[InputDescriptors::ReceivedHTLC] });
589505
amt += htlc.amount_msat / 1000;
590506
},
591507
&InputMaterial::HolderHTLC { .. } => {

lightning/src/ln/package.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! Utilities for computing witnesses weight and feerate computation for onchain operation
2+
3+
use ln::msgs::DecodeError;
4+
use util::ser::{Readable, Writer, Writeable};
5+
6+
#[derive(PartialEq, Clone, Copy)]
7+
pub(crate) enum InputDescriptors {
8+
RevokedOfferedHTLC,
9+
RevokedReceivedHTLC,
10+
OfferedHTLC,
11+
ReceivedHTLC,
12+
RevokedOutput, // either a revoked to_holder output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
13+
}
14+
15+
impl Writeable for InputDescriptors {
16+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
17+
match self {
18+
&InputDescriptors::RevokedOfferedHTLC => {
19+
writer.write_all(&[0; 1])?;
20+
},
21+
&InputDescriptors::RevokedReceivedHTLC => {
22+
writer.write_all(&[1; 1])?;
23+
},
24+
&InputDescriptors::OfferedHTLC => {
25+
writer.write_all(&[2; 1])?;
26+
},
27+
&InputDescriptors::ReceivedHTLC => {
28+
writer.write_all(&[3; 1])?;
29+
}
30+
&InputDescriptors::RevokedOutput => {
31+
writer.write_all(&[4; 1])?;
32+
}
33+
}
34+
Ok(())
35+
}
36+
}
37+
38+
impl Readable for InputDescriptors {
39+
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
40+
let input_descriptor = match <u8 as Readable>::read(reader)? {
41+
0 => {
42+
InputDescriptors::RevokedOfferedHTLC
43+
},
44+
1 => {
45+
InputDescriptors::RevokedReceivedHTLC
46+
},
47+
2 => {
48+
InputDescriptors::OfferedHTLC
49+
},
50+
3 => {
51+
InputDescriptors::ReceivedHTLC
52+
},
53+
4 => {
54+
InputDescriptors::RevokedOutput
55+
}
56+
_ => return Err(DecodeError::InvalidValue),
57+
};
58+
Ok(input_descriptor)
59+
}
60+
}
61+
62+
pub(crate) fn get_witnesses_weight(inputs: &[InputDescriptors]) -> usize {
63+
let mut tx_weight = 2; // count segwit flags
64+
for inp in inputs {
65+
// We use expected weight (and not actual) as signatures and time lock delays may vary
66+
tx_weight += match inp {
67+
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
68+
&InputDescriptors::RevokedOfferedHTLC => {
69+
1 + 1 + 73 + 1 + 33 + 1 + 133
70+
},
71+
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
72+
&InputDescriptors::RevokedReceivedHTLC => {
73+
1 + 1 + 73 + 1 + 33 + 1 + 139
74+
},
75+
// number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script
76+
&InputDescriptors::OfferedHTLC => {
77+
1 + 1 + 73 + 1 + 32 + 1 + 133
78+
},
79+
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
80+
&InputDescriptors::ReceivedHTLC => {
81+
1 + 1 + 73 + 1 + 1 + 1 + 139
82+
},
83+
// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
84+
&InputDescriptors::RevokedOutput => {
85+
1 + 1 + 73 + 1 + 1 + 1 + 77
86+
},
87+
};
88+
}
89+
tx_weight
90+
}

0 commit comments

Comments
 (0)