Skip to content

Commit 1e510cc

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 91845e4 commit 1e510cc

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

lightning/src/ln/interactivetxs.rs

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

845-
pub(crate) fn estimate_input_weight(prev_output: &TxOut) -> Weight {
846-
Weight::from_wu(if prev_output.script_pubkey.is_p2wpkh() {
847-
P2WPKH_INPUT_WEIGHT_LOWER_BOUND
848-
} else if prev_output.script_pubkey.is_p2wsh() {
849-
P2WSH_INPUT_WEIGHT_LOWER_BOUND
850-
} else if prev_output.script_pubkey.is_p2tr() {
851-
P2TR_INPUT_WEIGHT_LOWER_BOUND
852-
} else {
853-
UNKNOWN_SEGWIT_VERSION_INPUT_WEIGHT_LOWER_BOUND
854-
})
845+
fn estimate_input_satisfaction_weight(prev_output: &TxOut) -> Weight {
846+
Weight::from_wu(
847+
if prev_output.script_pubkey.is_p2wpkh() {
848+
P2WPKH_INPUT_WEIGHT_LOWER_BOUND
849+
} else if prev_output.script_pubkey.is_p2wsh() {
850+
P2WSH_INPUT_WEIGHT_LOWER_BOUND
851+
} else if prev_output.script_pubkey.is_p2tr() {
852+
P2TR_INPUT_WEIGHT_LOWER_BOUND
853+
} else {
854+
UNKNOWN_SEGWIT_VERSION_INPUT_WEIGHT_LOWER_BOUND
855+
} - BASE_INPUT_WEIGHT,
856+
)
855857
}
856858

857859
pub(crate) fn get_output_weight(script_pubkey: &ScriptBuf) -> Weight {
@@ -906,7 +908,9 @@ impl NegotiationContext {
906908
.iter()
907909
.filter(|(serial_id, _)| self.is_serial_id_valid_for_counterparty(serial_id))
908910
.fold(0u64, |weight, (_, input)| {
909-
weight.saturating_add(input.estimate_input_weight().to_wu())
911+
weight
912+
.saturating_add(BASE_INPUT_WEIGHT)
913+
.saturating_add(input.satisfaction_weight().to_wu())
910914
}),
911915
)
912916
}
@@ -998,6 +1002,7 @@ impl NegotiationContext {
9981002
input: txin,
9991003
prev_tx: prevtx.clone(),
10001004
prev_output: tx_out.clone(),
1005+
satisfaction_weight: estimate_input_satisfaction_weight(&tx_out),
10011006
}),
10021007
prev_outpoint,
10031008
)
@@ -1150,7 +1155,9 @@ impl NegotiationContext {
11501155
}
11511156
}
11521157

1153-
fn sent_tx_add_input(&mut self, msg: &msgs::TxAddInput) -> Result<(), AbortReason> {
1158+
fn sent_tx_add_input(
1159+
&mut self, (msg, satisfaction_weight): (&msgs::TxAddInput, Weight),
1160+
) -> Result<(), AbortReason> {
11541161
let vout = msg.prevtx_out as usize;
11551162
let (prev_outpoint, input) = if let Some(shared_input_txid) = msg.shared_input_txid {
11561163
let prev_outpoint = OutPoint { txid: shared_input_txid, vout: msg.prevtx_out };
@@ -1168,8 +1175,12 @@ impl NegotiationContext {
11681175
sequence: Sequence(msg.sequence),
11691176
..Default::default()
11701177
};
1171-
let single_input =
1172-
SingleOwnedInput { input: txin, prev_tx: prevtx.clone(), prev_output };
1178+
let single_input = SingleOwnedInput {
1179+
input: txin,
1180+
prev_tx: prevtx.clone(),
1181+
prev_output,
1182+
satisfaction_weight,
1183+
};
11731184
(prev_outpoint, InputOwned::Single(single_input))
11741185
} else {
11751186
return Err(AbortReason::MissingPrevTx);
@@ -1432,7 +1443,7 @@ define_state_transitions!(SENT_MSG_STATE, [
14321443
// State transitions when we have received some messages from our counterparty and we should
14331444
// respond.
14341445
define_state_transitions!(RECEIVED_MSG_STATE, [
1435-
DATA &msgs::TxAddInput, TRANSITION sent_tx_add_input,
1446+
DATA (&msgs::TxAddInput, Weight), TRANSITION sent_tx_add_input,
14361447
DATA &msgs::TxRemoveInput, TRANSITION sent_tx_remove_input,
14371448
DATA &msgs::TxAddOutput, TRANSITION sent_tx_add_output,
14381449
DATA &msgs::TxRemoveOutput, TRANSITION sent_tx_remove_output
@@ -1499,7 +1510,7 @@ impl StateMachine {
14991510
}
15001511

15011512
// TxAddInput
1502-
define_state_machine_transitions!(sent_tx_add_input, &msgs::TxAddInput, [
1513+
define_state_machine_transitions!(sent_tx_add_input, (&msgs::TxAddInput, Weight), [
15031514
FROM ReceivedChangeMsg, TO SentChangeMsg,
15041515
FROM ReceivedTxComplete, TO SentChangeMsg
15051516
]);
@@ -1566,6 +1577,7 @@ struct SingleOwnedInput {
15661577
input: TxIn,
15671578
prev_tx: Transaction,
15681579
prev_output: TxOut,
1580+
satisfaction_weight: Weight,
15691581
}
15701582

15711583
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -1658,13 +1670,13 @@ impl InputOwned {
16581670
}
16591671
}
16601672

1661-
fn estimate_input_weight(&self) -> Weight {
1673+
fn satisfaction_weight(&self) -> Weight {
16621674
match self {
1663-
InputOwned::Single(single) => estimate_input_weight(&single.prev_output),
1675+
InputOwned::Single(single) => single.satisfaction_weight,
16641676
// TODO(taproot): Needs to consider different weights based on channel type
1665-
InputOwned::Shared(_) => Weight::from_wu(
1666-
BASE_INPUT_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT + FUNDING_TRANSACTION_WITNESS_WEIGHT,
1667-
),
1677+
InputOwned::Shared(_) => {
1678+
Weight::from_wu(EMPTY_SCRIPT_SIG_WEIGHT + FUNDING_TRANSACTION_WITNESS_WEIGHT)
1679+
},
16681680
}
16691681
}
16701682

@@ -1835,12 +1847,12 @@ impl InteractiveTxInput {
18351847
self.input.remote_value(self.added_by)
18361848
}
18371849

1838-
pub fn estimate_input_weight(&self) -> Weight {
1839-
self.input.estimate_input_weight()
1850+
pub fn satisfaction_weight(&self) -> Weight {
1851+
self.input.satisfaction_weight()
18401852
}
18411853

18421854
fn into_negotiated_input(self) -> NegotiatedTxInput {
1843-
let weight = self.input.estimate_input_weight();
1855+
let weight = Weight::from_wu(BASE_INPUT_WEIGHT) + self.input.satisfaction_weight();
18441856
let (txin, prev_output) = self.input.into_tx_in_with_prev_output();
18451857
NegotiatedTxInput { serial_id: self.serial_id, txin, weight, prev_output }
18461858
}
@@ -1965,8 +1977,12 @@ impl InteractiveTxConstructor {
19651977
let serial_id = generate_holder_serial_id(entropy_source, is_initiator);
19661978
let txin = TxIn { previous_output: utxo.outpoint, sequence, ..Default::default() };
19671979
let prev_output = utxo.output;
1968-
let input =
1969-
InputOwned::Single(SingleOwnedInput { input: txin, prev_tx, prev_output });
1980+
let input = InputOwned::Single(SingleOwnedInput {
1981+
input: txin,
1982+
prev_tx,
1983+
prev_output,
1984+
satisfaction_weight: Weight::from_wu(utxo.satisfaction_weight),
1985+
});
19701986
(serial_id, input)
19711987
})
19721988
.collect();
@@ -2022,6 +2038,7 @@ impl InteractiveTxConstructor {
20222038
// We first attempt to send inputs we want to add, then outputs. Once we are done sending
20232039
// them both, then we always send tx_complete.
20242040
if let Some((serial_id, input)) = self.inputs_to_contribute.pop() {
2041+
let satisfaction_weight = input.satisfaction_weight();
20252042
let msg = match input {
20262043
InputOwned::Single(single) => msgs::TxAddInput {
20272044
channel_id: self.channel_id,
@@ -2040,7 +2057,7 @@ impl InteractiveTxConstructor {
20402057
shared_input_txid: Some(shared.input.previous_output.txid),
20412058
},
20422059
};
2043-
do_state_transition!(self, sent_tx_add_input, &msg)?;
2060+
do_state_transition!(self, sent_tx_add_input, (&msg, satisfaction_weight))?;
20442061
Ok(InteractiveTxMessageSend::TxAddInput(msg))
20452062
} else if let Some((serial_id, output)) = self.outputs_to_contribute.pop() {
20462063
let msg = msgs::TxAddOutput {

0 commit comments

Comments
 (0)