Prevent infinite loops

This commit is contained in:
Paul Hauner
2021-09-28 17:18:12 +10:00
parent c0692447ed
commit 9678f77bb5
2 changed files with 11 additions and 1 deletions

View File

@@ -22,6 +22,7 @@ pub enum Error {
IsSyncing,
ExecutionBlockNotFound(Hash256),
ExecutionHeadBlockNotFound,
ParentHashEqualsBlockHash(Hash256),
}
impl From<reqwest::Error> for Error {

View File

@@ -309,16 +309,25 @@ impl ExecutionLayer {
self.execution_blocks().await.put(block.block_hash, block);
// TODO(merge): This function can theoretically loop indefinitely, as per the
// specification. We should consider how to fix this. See discussion:
//
// https://discord.com/channels/595666850260713488/692062809701482577/892307257205878785
loop {
if block.total_difficulty >= self.terminal_total_difficulty() {
ttd_exceeding_block = Some(block.block_hash);
// Try to prevent infinite loops.
if block.block_hash == block.parent_hash {
return Err(ApiError::ParentHashEqualsBlockHash(block.block_hash));
}
block = self
.get_pow_block(engine, block.parent_hash)
.await?
.ok_or(ApiError::ExecutionBlockNotFound(block.parent_hash))?;
} else {
return Ok::<_, ApiError>(ttd_exceeding_block);
return Ok(ttd_exceeding_block);
}
}
})