@@ -177,21 +177,25 @@ 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
180
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 ( )
181
+ match self . inputs . entry ( msg. serial_id ) {
182
+ hash_map:: Entry :: Occupied ( _) => {
183
+ // The receiving node:
184
+ // - MUST fail the negotiation if:
185
+ // - the `serial_id` is already included in the transaction
186
+ return Err ( AbortReason :: DuplicateSerialId ) ;
192
187
} ,
193
- prev_output : prev_out,
194
- } ) ;
188
+ hash_map:: Entry :: Vacant ( entry) => {
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
+ } ,
198
+ }
195
199
self . prevtx_outpoints . insert ( prev_outpoint) ;
196
200
Ok ( ( ) )
197
201
}
@@ -263,15 +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 ) ;
271
- }
272
-
273
270
let output = TxOut { value : msg. sats , script_pubkey : msg. script . clone ( ) } ;
274
- self . outputs . entry ( msg. serial_id ) . or_insert ( output) ;
271
+ match self . outputs . entry ( msg. serial_id ) {
272
+ hash_map:: Entry :: Occupied ( _) => {
273
+ // The receiving node:
274
+ // - MUST fail the negotiation if:
275
+ // - the `serial_id` is already included in the transaction
276
+ return Err ( AbortReason :: DuplicateSerialId ) ;
277
+ } ,
278
+ hash_map:: Entry :: Vacant ( entry) => {
279
+ entry. insert ( output) ;
280
+ } ,
281
+ } ;
275
282
Ok ( ( ) )
276
283
}
277
284
0 commit comments