Skip to content

Commit ec75175

Browse files
committed
Track satisfaction_weight in InteractiveTxInput
Instead of estimating the input weight, track the satisfaction_weight in InteractiveTxInput. This then can be used compute the transaction weight without storing individual weights in NegotiatedTxInput in an upcoming commit.
1 parent f159304 commit ec75175

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ struct NegotiationContext {
842842
feerate_sat_per_kw: u32,
843843
}
844844

845-
pub(crate) fn estimate_input_weight(prev_output: &TxOut) -> Weight {
845+
fn estimate_input_satisfaction_weight(prev_output: &TxOut) -> Weight {
846846
Weight::from_wu(if prev_output.script_pubkey.is_p2wpkh() {
847847
P2WPKH_INPUT_WEIGHT_LOWER_BOUND
848848
} else if prev_output.script_pubkey.is_p2wsh() {
@@ -851,7 +851,7 @@ pub(crate) fn estimate_input_weight(prev_output: &TxOut) -> Weight {
851851
P2TR_INPUT_WEIGHT_LOWER_BOUND
852852
} else {
853853
UNKNOWN_SEGWIT_VERSION_INPUT_WEIGHT_LOWER_BOUND
854-
})
854+
} - BASE_INPUT_WEIGHT)
855855
}
856856

857857
pub(crate) fn get_output_weight(script_pubkey: &ScriptBuf) -> Weight {
@@ -906,7 +906,9 @@ impl NegotiationContext {
906906
.iter()
907907
.filter(|(serial_id, _)| self.is_serial_id_valid_for_counterparty(serial_id))
908908
.fold(0u64, |weight, (_, input)| {
909-
weight.saturating_add(input.estimate_input_weight().to_wu())
909+
weight
910+
.saturating_add(BASE_INPUT_WEIGHT)
911+
.saturating_add(input.satisfaction_weight().to_wu())
910912
}),
911913
)
912914
}
@@ -998,6 +1000,7 @@ impl NegotiationContext {
9981000
input: txin,
9991001
prev_tx: prevtx.clone(),
10001002
prev_output: tx_out.clone(),
1003+
satisfaction_weight: estimate_input_satisfaction_weight(&tx_out),
10011004
}),
10021005
prev_outpoint,
10031006
)
@@ -1150,7 +1153,7 @@ impl NegotiationContext {
11501153
}
11511154
}
11521155

1153-
fn sent_tx_add_input(&mut self, msg: &msgs::TxAddInput) -> Result<(), AbortReason> {
1156+
fn sent_tx_add_input(&mut self, (msg, satisfaction_weight): (&msgs::TxAddInput, Weight)) -> Result<(), AbortReason> {
11541157
let vout = msg.prevtx_out as usize;
11551158
let (prev_outpoint, input) = if let Some(shared_input_txid) = msg.shared_input_txid {
11561159
let prev_outpoint = OutPoint { txid: shared_input_txid, vout: msg.prevtx_out };
@@ -1169,7 +1172,7 @@ impl NegotiationContext {
11691172
..Default::default()
11701173
};
11711174
let single_input =
1172-
SingleOwnedInput { input: txin, prev_tx: prevtx.clone(), prev_output };
1175+
SingleOwnedInput { input: txin, prev_tx: prevtx.clone(), prev_output, satisfaction_weight };
11731176
(prev_outpoint, InputOwned::Single(single_input))
11741177
} else {
11751178
return Err(AbortReason::MissingPrevTx);
@@ -1432,7 +1435,7 @@ define_state_transitions!(SENT_MSG_STATE, [
14321435
// State transitions when we have received some messages from our counterparty and we should
14331436
// respond.
14341437
define_state_transitions!(RECEIVED_MSG_STATE, [
1435-
DATA &msgs::TxAddInput, TRANSITION sent_tx_add_input,
1438+
DATA (&msgs::TxAddInput, Weight), TRANSITION sent_tx_add_input,
14361439
DATA &msgs::TxRemoveInput, TRANSITION sent_tx_remove_input,
14371440
DATA &msgs::TxAddOutput, TRANSITION sent_tx_add_output,
14381441
DATA &msgs::TxRemoveOutput, TRANSITION sent_tx_remove_output
@@ -1499,7 +1502,7 @@ impl StateMachine {
14991502
}
15001503

15011504
// TxAddInput
1502-
define_state_machine_transitions!(sent_tx_add_input, &msgs::TxAddInput, [
1505+
define_state_machine_transitions!(sent_tx_add_input, (&msgs::TxAddInput, Weight), [
15031506
FROM ReceivedChangeMsg, TO SentChangeMsg,
15041507
FROM ReceivedTxComplete, TO SentChangeMsg
15051508
]);
@@ -1566,6 +1569,7 @@ struct SingleOwnedInput {
15661569
input: TxIn,
15671570
prev_tx: Transaction,
15681571
prev_output: TxOut,
1572+
satisfaction_weight: Weight,
15691573
}
15701574

15711575
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -1658,12 +1662,12 @@ impl InputOwned {
16581662
}
16591663
}
16601664

1661-
fn estimate_input_weight(&self) -> Weight {
1665+
fn satisfaction_weight(&self) -> Weight {
16621666
match self {
1663-
InputOwned::Single(single) => estimate_input_weight(&single.prev_output),
1667+
InputOwned::Single(single) => single.satisfaction_weight,
16641668
// TODO(taproot): Needs to consider different weights based on channel type
16651669
InputOwned::Shared(_) => Weight::from_wu(
1666-
BASE_INPUT_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT + FUNDING_TRANSACTION_WITNESS_WEIGHT,
1670+
EMPTY_SCRIPT_SIG_WEIGHT + FUNDING_TRANSACTION_WITNESS_WEIGHT,
16671671
),
16681672
}
16691673
}
@@ -1835,12 +1839,12 @@ impl InteractiveTxInput {
18351839
self.input.remote_value(self.added_by)
18361840
}
18371841

1838-
pub fn estimate_input_weight(&self) -> Weight {
1839-
self.input.estimate_input_weight()
1842+
pub fn satisfaction_weight(&self) -> Weight {
1843+
self.input.satisfaction_weight()
18401844
}
18411845

18421846
fn into_negotiated_input(self) -> NegotiatedTxInput {
1843-
let weight = self.input.estimate_input_weight();
1847+
let weight = Weight::from_wu(BASE_INPUT_WEIGHT) + self.input.satisfaction_weight();
18441848
let (txin, prev_output) = self.input.into_tx_in_with_prev_output();
18451849
NegotiatedTxInput { serial_id: self.serial_id, txin, weight, prev_output }
18461850
}
@@ -1965,8 +1969,12 @@ impl InteractiveTxConstructor {
19651969
let serial_id = generate_holder_serial_id(entropy_source, is_initiator);
19661970
let txin = TxIn { previous_output: utxo.outpoint, sequence, ..Default::default() };
19671971
let prev_output = utxo.output;
1968-
let input =
1969-
InputOwned::Single(SingleOwnedInput { input: txin, prev_tx, prev_output });
1972+
let input = InputOwned::Single(SingleOwnedInput {
1973+
input: txin,
1974+
prev_tx,
1975+
prev_output,
1976+
satisfaction_weight: Weight::from_wu(utxo.satisfaction_weight),
1977+
});
19701978
(serial_id, input)
19711979
})
19721980
.collect();
@@ -2022,6 +2030,7 @@ impl InteractiveTxConstructor {
20222030
// We first attempt to send inputs we want to add, then outputs. Once we are done sending
20232031
// them both, then we always send tx_complete.
20242032
if let Some((serial_id, input)) = self.inputs_to_contribute.pop() {
2033+
let satisfaction_weight = input.satisfaction_weight();
20252034
let msg = match input {
20262035
InputOwned::Single(single) => msgs::TxAddInput {
20272036
channel_id: self.channel_id,
@@ -2040,7 +2049,7 @@ impl InteractiveTxConstructor {
20402049
shared_input_txid: Some(shared.input.previous_output.txid),
20412050
},
20422051
};
2043-
do_state_transition!(self, sent_tx_add_input, &msg)?;
2052+
do_state_transition!(self, sent_tx_add_input, (&msg, satisfaction_weight))?;
20442053
Ok(InteractiveTxMessageSend::TxAddInput(msg))
20452054
} else if let Some((serial_id, output)) = self.outputs_to_contribute.pop() {
20462055
let msg = msgs::TxAddOutput {

0 commit comments

Comments
 (0)