Skip to content

Commit e78c787

Browse files
committed
Make sent_tx_* fallible and handle errors & simplify counterparty_weight_contributed calc
1 parent f52a8cb commit e78c787

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

lightning/src/ln/interactivetxs.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -300,33 +300,34 @@ impl NegotiationContext {
300300
}
301301
}
302302

303-
fn sent_tx_add_input(&mut self, msg: &msgs::TxAddInput) {
303+
fn sent_tx_add_input(&mut self, msg: &msgs::TxAddInput) -> Result<(), AbortReason> {
304304
let tx = msg.prevtx.as_transaction();
305305
let input = TxIn {
306306
previous_output: OutPoint { txid: tx.txid(), vout: msg.prevtx_out },
307307
sequence: Sequence(msg.sequence),
308308
..Default::default()
309309
};
310-
debug_assert!((msg.prevtx_out as usize) < tx.output.len());
311-
let prev_output = &tx.output[msg.prevtx_out as usize];
310+
let prev_output =
311+
tx.output.get(msg.prevtx_out as usize).ok_or(AbortReason::PrevTxOutInvalid)?.clone();
312312
self.prevtx_outpoints.insert(input.previous_output.clone());
313-
self.inputs.insert(
314-
msg.serial_id,
315-
TxInputWithPrevOutput { input, prev_output: prev_output.clone() },
316-
);
313+
self.inputs.insert(msg.serial_id, TxInputWithPrevOutput { input, prev_output });
314+
Ok(())
317315
}
318316

319-
fn sent_tx_add_output(&mut self, msg: &msgs::TxAddOutput) {
317+
fn sent_tx_add_output(&mut self, msg: &msgs::TxAddOutput) -> Result<(), AbortReason> {
320318
self.outputs
321319
.insert(msg.serial_id, TxOut { value: msg.sats, script_pubkey: msg.script.clone() });
320+
Ok(())
322321
}
323322

324-
fn sent_tx_remove_input(&mut self, msg: &msgs::TxRemoveInput) {
323+
fn sent_tx_remove_input(&mut self, msg: &msgs::TxRemoveInput) -> Result<(), AbortReason> {
325324
self.inputs.remove(&msg.serial_id);
325+
Ok(())
326326
}
327327

328-
fn sent_tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput) {
328+
fn sent_tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput) -> Result<(), AbortReason> {
329329
self.outputs.remove(&msg.serial_id);
330+
Ok(())
330331
}
331332

332333
fn build_transaction(self) -> Result<Transaction, AbortReason> {
@@ -363,15 +364,15 @@ impl NegotiationContext {
363364
const INPUT_WEIGHT: u64 = BASE_INPUT_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT;
364365

365366
// - the peer's paid feerate does not meet or exceed the agreed feerate (based on the minimum fee).
366-
let counterparty_output_weight_contributed: u64 = self
367+
let mut counterparty_weight_contributed: u64 = self
367368
.counterparty_outputs_contributed()
368369
.map(|output| {
369370
(8 /* value */ + output.script_pubkey.consensus_encode(&mut sink()).unwrap() as u64)
370371
* WITNESS_SCALE_FACTOR as u64
371372
})
372373
.sum();
373-
let counterparty_weight_contributed = counterparty_output_weight_contributed
374-
+ self.counterparty_inputs_contributed().count() as u64 * INPUT_WEIGHT;
374+
counterparty_weight_contributed +=
375+
self.counterparty_inputs_contributed().count() as u64 * INPUT_WEIGHT;
375376
let counterparty_fees_contributed =
376377
counterparty_inputs_value.saturating_sub(counterparty_outputs_value);
377378
let mut required_counterparty_contribution_fee =
@@ -527,7 +528,7 @@ macro_rules! define_state_transitions {
527528
impl<S: ReceivedMsgState> StateTransition<SentChangeMsg, $data> for S {
528529
fn transition(self, data: $data) -> StateTransitionResult<SentChangeMsg> {
529530
let mut context = self.into_negotiation_context();
530-
context.$transition(data);
531+
context.$transition(data)?;
531532
Ok(SentChangeMsg(context))
532533
}
533534
}

0 commit comments

Comments
 (0)