Skip to content

Commit 02c0ad2

Browse files
committed
eagerly unwrap height option, save one collect
1 parent 16fde66 commit 02c0ad2

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/blockchain/utils.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,31 +251,29 @@ pub trait ElectrumLikeSync {
251251
chunk_size: usize,
252252
) -> Result<HashMap<Txid, u64>, Error> {
253253
let mut txid_timestamp = HashMap::new();
254-
let needed_txid_height: HashMap<&Txid, &Option<u32>> = txid_height
254+
let needed_txid_height: HashMap<&Txid, u32> = txid_height
255255
.iter()
256-
.filter(|(txid, _)| txs_details_in_db.get(*txid).is_none())
256+
.filter(|(t, _)| txs_details_in_db.get(*t).is_none())
257+
.filter_map(|(t, o)| o.map(|h| (t, h)))
257258
.collect();
258-
let needed_heights: HashSet<u32> =
259-
needed_txid_height.iter().filter_map(|(_, b)| **b).collect();
259+
let needed_heights: HashSet<u32> = needed_txid_height.values().cloned().collect();
260260
if !needed_heights.is_empty() {
261261
info!("{} headers to download for timestamp", needed_heights.len());
262262
let mut height_timestamp: HashMap<u32, u64> = HashMap::new();
263263
for chunk in ChunksIterator::new(needed_heights.into_iter(), chunk_size) {
264264
let call_result: Vec<BlockHeader> =
265265
maybe_await!(self.els_batch_block_header(chunk.clone()))?;
266-
let vec: Vec<(u32, u64)> = chunk
267-
.into_iter()
268-
.zip(call_result.iter().map(|h| h.time as u64))
269-
.collect();
270-
height_timestamp.extend(vec);
266+
height_timestamp.extend(
267+
chunk
268+
.into_iter()
269+
.zip(call_result.iter().map(|h| h.time as u64)),
270+
);
271271
}
272-
for (txid, height_opt) in needed_txid_height {
273-
if let Some(height) = height_opt {
274-
let timestamp = height_timestamp
275-
.get(height)
276-
.ok_or_else(|| Error::Generic("timestamp missing".to_string()))?;
277-
txid_timestamp.insert(*txid, *timestamp);
278-
}
272+
for (txid, height) in needed_txid_height {
273+
let timestamp = height_timestamp
274+
.get(&height)
275+
.ok_or_else(|| Error::Generic("timestamp missing".to_string()))?;
276+
txid_timestamp.insert(*txid, *timestamp);
279277
}
280278
}
281279

0 commit comments

Comments
 (0)