Skip to content

Commit 4dbc845

Browse files
committed
Use HashMaps as, well, HashMaps (don't iter for key match)
1 parent 1d2a27d commit 4dbc845

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,25 @@ impl NegotiationContext {
177177
} else {
178178
return Err(AbortReason::PrevTxOutInvalid);
179179
};
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-
}
186180
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);
192187
},
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+
}
195199
self.prevtx_outpoints.insert(prev_outpoint);
196200
Ok(())
197201
}
@@ -263,15 +267,18 @@ impl NegotiationContext {
263267
return Err(AbortReason::InvalidOutputScript);
264268
}
265269

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-
273270
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+
};
275282
Ok(())
276283
}
277284

0 commit comments

Comments
 (0)