Skip to content

Commit 5756a56

Browse files
committed
Make sent_tx_* fallible and handle errors & simplify counterparty_weight_contributed calc
1 parent 3f4a742 commit 5756a56

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
@@ -301,33 +301,34 @@ impl NegotiationContext {
301301
}
302302
}
303303

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

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

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

329-
fn sent_tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput) {
329+
fn sent_tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput) -> Result<(), AbortReason> {
330330
self.outputs.remove(&msg.serial_id);
331+
Ok(())
331332
}
332333

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

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

0 commit comments

Comments
 (0)