Skip to content

Commit 9406bea

Browse files
committed
Use HashMaps as, well, HashMaps (don't iter for key match)
1 parent ac9a2c8 commit 9406bea

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

lightning/src/ln/interactivetxs.rs

+31-25
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,27 @@ 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-
}
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)
192186
},
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+
}
197201
}
198202

199203
fn received_tx_remove_input(&mut self, msg: &msgs::TxRemoveInput) -> Result<(), AbortReason> {
@@ -263,16 +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);
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+
},
271281
}
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(())
276282
}
277283

278284
fn received_tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput) -> Result<(), AbortReason> {

0 commit comments

Comments
 (0)