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