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