Skip to content

Commit ebd57c5

Browse files
committed
Make sent_tx_* fallible and handle errors & simplify counterparty_weight_contributed calc
1 parent 774b048 commit ebd57c5

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -299,33 +299,34 @@ impl NegotiationContext {
299299
}
300300
}
301301

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

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

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

327-
fn sent_tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput) {
327+
fn sent_tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput) -> Result<(), AbortReason> {
328328
self.outputs.remove(&msg.serial_id);
329+
Ok(())
329330
}
330331

331332
fn build_transaction(self) -> Result<Transaction, AbortReason> {
@@ -358,15 +359,15 @@ impl NegotiationContext {
358359
const INPUT_WEIGHT: u64 = BASE_INPUT_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT;
359360

360361
// - the peer's paid feerate does not meet or exceed the agreed feerate (based on the minimum fee).
361-
let counterparty_output_weight_contributed: u64 = self
362+
let mut counterparty_weight_contributed: u64 = self
362363
.counterparty_outputs_contributed()
363364
.map(|output| {
364365
(8 /* value */ + output.script_pubkey.consensus_encode(&mut sink()).unwrap() as u64)
365366
* WITNESS_SCALE_FACTOR as u64
366367
})
367368
.sum();
368-
let counterparty_weight_contributed = counterparty_output_weight_contributed
369-
+ self.counterparty_inputs_contributed().count() as u64 * INPUT_WEIGHT;
369+
counterparty_weight_contributed +=
370+
self.counterparty_inputs_contributed().count() as u64 * INPUT_WEIGHT;
370371
let counterparty_fees_contributed =
371372
counterparty_inputs_value.saturating_sub(counterparty_outputs_value);
372373
let mut required_counterparty_contribution_fee =
@@ -522,7 +523,7 @@ macro_rules! define_state_transitions {
522523
impl<S: ReceivedMsgState> StateTransition<SentChangeMsg, $data> for S {
523524
fn transition(self, data: $data) -> StateTransitionResult<SentChangeMsg> {
524525
let mut context = self.into_negotiation_context();
525-
context.$transition(data);
526+
context.$transition(data)?;
526527
Ok(SentChangeMsg(context))
527528
}
528529
}

0 commit comments

Comments
 (0)