@@ -301,33 +301,34 @@ impl NegotiationContext {
301
301
}
302
302
}
303
303
304
- fn sent_tx_add_input ( & mut self , msg : & msgs:: TxAddInput ) {
304
+ fn sent_tx_add_input ( & mut self , msg : & msgs:: TxAddInput ) -> Result < ( ) , AbortReason > {
305
305
let tx = msg. prevtx . as_transaction ( ) ;
306
306
let input = TxIn {
307
307
previous_output : OutPoint { txid : tx. txid ( ) , vout : msg. prevtx_out } ,
308
308
sequence : Sequence ( msg. sequence ) ,
309
309
..Default :: default ( )
310
310
} ;
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 ( ) ;
313
313
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 ( ( ) )
318
316
}
319
317
320
- fn sent_tx_add_output ( & mut self , msg : & msgs:: TxAddOutput ) {
318
+ fn sent_tx_add_output ( & mut self , msg : & msgs:: TxAddOutput ) -> Result < ( ) , AbortReason > {
321
319
self . outputs
322
320
. insert ( msg. serial_id , TxOut { value : msg. sats , script_pubkey : msg. script . clone ( ) } ) ;
321
+ Ok ( ( ) )
323
322
}
324
323
325
- fn sent_tx_remove_input ( & mut self , msg : & msgs:: TxRemoveInput ) {
324
+ fn sent_tx_remove_input ( & mut self , msg : & msgs:: TxRemoveInput ) -> Result < ( ) , AbortReason > {
326
325
self . inputs . remove ( & msg. serial_id ) ;
326
+ Ok ( ( ) )
327
327
}
328
328
329
- fn sent_tx_remove_output ( & mut self , msg : & msgs:: TxRemoveOutput ) {
329
+ fn sent_tx_remove_output ( & mut self , msg : & msgs:: TxRemoveOutput ) -> Result < ( ) , AbortReason > {
330
330
self . outputs . remove ( & msg. serial_id ) ;
331
+ Ok ( ( ) )
331
332
}
332
333
333
334
fn build_transaction ( self ) -> Result < Transaction , AbortReason > {
@@ -364,15 +365,15 @@ impl NegotiationContext {
364
365
const INPUT_WEIGHT : u64 = BASE_INPUT_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT ;
365
366
366
367
// - 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
368
369
. counterparty_outputs_contributed ( )
369
370
. map ( |output| {
370
371
( 8 /* value */ + output. script_pubkey . consensus_encode ( & mut sink ( ) ) . unwrap ( ) as u64 )
371
372
* WITNESS_SCALE_FACTOR as u64
372
373
} )
373
374
. 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 ;
376
377
let counterparty_fees_contributed =
377
378
counterparty_inputs_value. saturating_sub ( counterparty_outputs_value) ;
378
379
let mut required_counterparty_contribution_fee =
@@ -528,7 +529,7 @@ macro_rules! define_state_transitions {
528
529
impl <S : ReceivedMsgState > StateTransition <SentChangeMsg , $data> for S {
529
530
fn transition( self , data: $data) -> StateTransitionResult <SentChangeMsg > {
530
531
let mut context = self . into_negotiation_context( ) ;
531
- context. $transition( data) ;
532
+ context. $transition( data) ? ;
532
533
Ok ( SentChangeMsg ( context) )
533
534
}
534
535
}
0 commit comments