Skip to content

Commit 855a97f

Browse files
authored
chore(core): improve import_blocks readability (#3016)
**Motivation** <!-- Why does this pull request exist? What are its goals? --> **Description** <!-- A clear and concise general description of the changes this PR introduces --> <!-- Link to issues: Resolves #111, Resolves #222 --> Closes #issue_number
1 parent 7e13253 commit 855a97f

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

cmd/ethrex/cli.rs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -378,38 +378,31 @@ pub async fn import_blocks(
378378
let size = blocks.len();
379379
for block in &blocks {
380380
let hash = block.hash();
381-
382-
info!(
383-
"Adding block {} with hash {:#x}.",
384-
block.header.number, hash
385-
);
381+
let number = block.header.number;
382+
info!("Adding block {number} with hash {hash:#x}.");
386383
// Check if the block is already in the blockchain, if it is do nothing, if not add it
387-
match store.get_block_number(hash).await {
388-
Ok(Some(_)) => {
389-
info!("Block {} is already in the blockchain", block.hash());
390-
continue;
391-
}
392-
Ok(None) => {
393-
if let Err(error) = blockchain.add_block(block).await {
394-
warn!(
395-
"Failed to add block {} with hash {:#x}",
396-
block.header.number, hash
397-
);
398-
return Err(error);
399-
}
400-
}
401-
Err(_) => {
402-
return Err(ChainError::Custom(String::from(
403-
"Couldn't check if block is already in the blockchain",
404-
)));
405-
}
384+
let block_number = store.get_block_number(hash).await.map_err(|_e| {
385+
ChainError::Custom(String::from(
386+
"Couldn't check if block is already in the blockchain",
387+
))
388+
})?;
389+
390+
if block_number.is_some() {
391+
info!("Block {} is already in the blockchain", block.hash());
392+
continue;
406393
}
394+
395+
blockchain
396+
.add_block(block)
397+
.await
398+
.inspect_err(|_| warn!("Failed to add block {number} with hash {hash:#x}",))?;
407399
}
400+
408401
if let Some(last_block) = blocks.last() {
409402
let hash = last_block.hash();
410-
if let Err(error) = apply_fork_choice(&store, hash, hash, hash).await {
411-
warn!("Failed to apply fork choice: {}", error);
412-
}
403+
let _ = apply_fork_choice(&store, hash, hash, hash)
404+
.await
405+
.inspect_err(|error| warn!("Failed to apply fork choice: {}", error));
413406
}
414407
info!("Added {size} blocks to blockchain");
415408
Ok(())

0 commit comments

Comments
 (0)