@@ -177,23 +177,27 @@ impl NegotiationContext {
177
177
} else {
178
178
return Err ( AbortReason :: PrevTxOutInvalid ) ;
179
179
} ;
180
- if self . inputs . iter ( ) . any ( |( serial_id, _) | * serial_id == msg. serial_id ) {
181
- // The receiving node:
182
- // - MUST fail the negotiation if:
183
- // - the `serial_id` is already included in the transaction
184
- return Err ( AbortReason :: DuplicateSerialId ) ;
185
- }
186
- let prev_outpoint = OutPoint { txid, vout : msg. prevtx_out } ;
187
- self . inputs . entry ( msg. serial_id ) . or_insert_with ( || TxInputWithPrevOutput {
188
- input : TxIn {
189
- previous_output : prev_outpoint. clone ( ) ,
190
- sequence : Sequence ( msg. sequence ) ,
191
- ..Default :: default ( )
180
+ match self . inputs . entry ( msg. serial_id ) {
181
+ hash_map:: Entry :: Occupied ( _) => {
182
+ // The receiving node:
183
+ // - MUST fail the negotiation if:
184
+ // - the `serial_id` is already included in the transaction
185
+ Err ( AbortReason :: DuplicateSerialId )
192
186
} ,
193
- prev_output : prev_out,
194
- } ) ;
195
- self . prevtx_outpoints . insert ( prev_outpoint) ;
196
- Ok ( ( ) )
187
+ hash_map:: Entry :: Vacant ( entry) => {
188
+ let prev_outpoint = OutPoint { txid, vout : msg. prevtx_out } ;
189
+ entry. insert ( TxInputWithPrevOutput {
190
+ input : TxIn {
191
+ previous_output : prev_outpoint. clone ( ) ,
192
+ sequence : Sequence ( msg. sequence ) ,
193
+ ..Default :: default ( )
194
+ } ,
195
+ prev_output : prev_out,
196
+ } ) ;
197
+ self . prevtx_outpoints . insert ( prev_outpoint) ;
198
+ Ok ( ( ) )
199
+ } ,
200
+ }
197
201
}
198
202
199
203
fn received_tx_remove_input ( & mut self , msg : & msgs:: TxRemoveInput ) -> Result < ( ) , AbortReason > {
@@ -263,16 +267,18 @@ impl NegotiationContext {
263
267
return Err ( AbortReason :: InvalidOutputScript ) ;
264
268
}
265
269
266
- if self . outputs . iter ( ) . any ( |( serial_id, _) | * serial_id == msg. serial_id ) {
267
- // The receiving node:
268
- // - MUST fail the negotiation if:
269
- // - the `serial_id` is already included in the transaction
270
- return Err ( AbortReason :: DuplicateSerialId ) ;
270
+ match self . outputs . entry ( msg. serial_id ) {
271
+ hash_map:: Entry :: Occupied ( _) => {
272
+ // The receiving node:
273
+ // - MUST fail the negotiation if:
274
+ // - the `serial_id` is already included in the transaction
275
+ Err ( AbortReason :: DuplicateSerialId )
276
+ } ,
277
+ hash_map:: Entry :: Vacant ( entry) => {
278
+ entry. insert ( TxOut { value : msg. sats , script_pubkey : msg. script . clone ( ) } ) ;
279
+ Ok ( ( ) )
280
+ } ,
271
281
}
272
-
273
- let output = TxOut { value : msg. sats , script_pubkey : msg. script . clone ( ) } ;
274
- self . outputs . entry ( msg. serial_id ) . or_insert ( output) ;
275
- Ok ( ( ) )
276
282
}
277
283
278
284
fn received_tx_remove_output ( & mut self , msg : & msgs:: TxRemoveOutput ) -> Result < ( ) , AbortReason > {
0 commit comments