1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 01:43:29 +01:00

Keep HashMap immutable in Chain::load() (#853)

This commit is contained in:
Roman Zeyde 2023-03-04 22:16:45 +02:00 committed by GitHub
parent 1a13a4cd74
commit 5397897602
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -60,12 +60,12 @@ impl Chain {
pub(crate) fn load(&mut self, headers: Vec<BlockHeader>, tip: BlockHash) {
let genesis_hash = self.headers[0].0;
let mut header_map: HashMap<BlockHash, BlockHeader> =
let header_map: HashMap<BlockHash, BlockHeader> =
headers.into_iter().map(|h| (h.block_hash(), h)).collect();
let mut blockhash = tip;
let mut new_headers = Vec::with_capacity(header_map.len());
let mut new_headers: Vec<&BlockHeader> = Vec::with_capacity(header_map.len());
while blockhash != genesis_hash {
let header = match header_map.remove(&blockhash) {
let header = match header_map.get(&blockhash) {
Some(header) => header,
None => panic!("missing header {} while loading from DB", blockhash),
};
@ -73,7 +73,7 @@ impl Chain {
new_headers.push(header);
}
info!("loading {} headers, tip={}", new_headers.len(), tip);
let new_headers = new_headers.into_iter().rev(); // order by height
let new_headers = new_headers.into_iter().rev().copied(); // order by height
self.update(new_headers.zip(1..).map(NewHeader::from).collect())
}