Skip to content

Commit 105c686

Browse files
committed
Remove input value fields from ConstructedTransaction
The local and remote input values are used to determine which node sends tx_signatures first. Instead of persisting these values, compute them only when needed from the input metadata. The spec states that the entire shared input value is to be included for the node sending the corresponding tx_add_input, so it isn't necessary to know the local and remote balances which the metadata does not contain.
1 parent a8d7646 commit 105c686

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,9 @@ impl Display for AbortReason {
197197
#[derive(Debug, Clone, PartialEq, Eq)]
198198
pub(crate) struct ConstructedTransaction {
199199
holder_is_initiator: bool,
200-
201200
input_metadata: Vec<TxInMetadata>,
202201
output_metadata: Vec<TxOutMetadata>,
203202
tx: Transaction,
204-
205-
local_inputs_value_satoshis: u64,
206-
remote_inputs_value_satoshis: u64,
207-
208203
shared_input_index: Option<u32>,
209204
}
210205

@@ -249,9 +244,7 @@ impl_writeable_tlv_based!(ConstructedTransaction, {
249244
(3, input_metadata, required),
250245
(5, output_metadata, required),
251246
(7, tx, required),
252-
(9, local_inputs_value_satoshis, required),
253-
(11, remote_inputs_value_satoshis, required),
254-
(13, shared_input_index, option),
247+
(9, shared_input_index, option),
255248
});
256249

257250
impl ConstructedTransaction {
@@ -271,12 +264,6 @@ impl ConstructedTransaction {
271264
return Err(AbortReason::MissingFundingOutput);
272265
}
273266

274-
let local_inputs_value_satoshis = context
275-
.inputs
276-
.iter()
277-
.fold(0u64, |value, (_, input)| value.saturating_add(input.local_value()));
278-
let remote_inputs_value_satoshis = context.remote_inputs_value();
279-
280267
let satisfaction_weight =
281268
Weight::from_wu(context.inputs.iter().fold(0u64, |value, (_, input)| {
282269
value.saturating_add(input.satisfaction_weight().to_wu())
@@ -313,14 +300,9 @@ impl ConstructedTransaction {
313300

314301
Ok(Self {
315302
holder_is_initiator: context.holder_is_initiator,
316-
317303
input_metadata,
318304
output_metadata,
319305
tx,
320-
321-
local_inputs_value_satoshis,
322-
remote_inputs_value_satoshis,
323-
324306
shared_input_index,
325307
})
326308
}
@@ -337,6 +319,26 @@ impl ConstructedTransaction {
337319
self.tx().compute_txid()
338320
}
339321

322+
/// Returns the total input value from all local contributions, including the entire shared
323+
/// input value if applicable.
324+
fn local_contributed_input_value(&self) -> Amount {
325+
self.input_metadata
326+
.iter()
327+
.filter(|input| input.is_local(self.holder_is_initiator))
328+
.map(|input| input.prev_output.value)
329+
.sum()
330+
}
331+
332+
/// Returns the total input value from all remote contributions, including the entire shared
333+
/// input value if applicable.
334+
fn remote_contributed_input_value(&self) -> Amount {
335+
self.input_metadata
336+
.iter()
337+
.filter(|input| !input.is_local(self.holder_is_initiator))
338+
.map(|input| input.prev_output.value)
339+
.sum()
340+
}
341+
340342
/// Adds provided holder witnesses to holder inputs of unsigned transaction.
341343
///
342344
/// Note that it is assumed that the witness count equals the holder input count.
@@ -1379,11 +1381,13 @@ macro_rules! define_state_transitions {
13791381
let tx = context.validate_tx()?;
13801382

13811383
// Strict ordering prevents deadlocks during tx_signatures exchange
1384+
let local_contributed_input_value = tx.local_contributed_input_value();
1385+
let remote_contributed_input_value = tx.remote_contributed_input_value();
13821386
let holder_sends_tx_signatures_first =
1383-
if tx.local_inputs_value_satoshis == tx.remote_inputs_value_satoshis {
1387+
if local_contributed_input_value == remote_contributed_input_value {
13841388
holder_node_id.serialize() < counterparty_node_id.serialize()
13851389
} else {
1386-
tx.local_inputs_value_satoshis < tx.remote_inputs_value_satoshis
1390+
local_contributed_input_value < remote_contributed_input_value
13871391
};
13881392

13891393
let signing_session = InteractiveTxSigningSession {
@@ -3306,8 +3310,6 @@ mod tests {
33063310
input_metadata,
33073311
output_metadata: vec![], // N/A for test
33083312
tx: transaction.clone(),
3309-
local_inputs_value_satoshis: 0, // N/A for test
3310-
remote_inputs_value_satoshis: 0, // N/A for test
33113313
shared_input_index: None,
33123314
};
33133315

0 commit comments

Comments
 (0)