mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-21 22:04:44 +00:00
Add search for TTD block
This commit is contained in:
@@ -30,6 +30,7 @@ impl From<ApiError> for Error {
|
||||
|
||||
struct Inner {
|
||||
engines: Engines<HttpJsonRpc>,
|
||||
total_terminal_difficulty: Uint256,
|
||||
executor: TaskExecutor,
|
||||
log: Logger,
|
||||
}
|
||||
@@ -42,6 +43,7 @@ pub struct ExecutionLayer {
|
||||
impl ExecutionLayer {
|
||||
pub fn from_urls(
|
||||
urls: Vec<SensitiveUrl>,
|
||||
total_terminal_difficulty: Uint256,
|
||||
executor: TaskExecutor,
|
||||
log: Logger,
|
||||
) -> Result<Self, Error> {
|
||||
@@ -59,6 +61,7 @@ impl ExecutionLayer {
|
||||
engines,
|
||||
log: log.clone(),
|
||||
},
|
||||
total_terminal_difficulty,
|
||||
executor,
|
||||
log,
|
||||
};
|
||||
@@ -78,6 +81,10 @@ impl ExecutionLayer {
|
||||
&self.inner.executor
|
||||
}
|
||||
|
||||
fn total_terminal_difficulty(&self) -> Uint256 {
|
||||
self.inner.total_terminal_difficulty
|
||||
}
|
||||
|
||||
fn log(&self) -> &Logger {
|
||||
&self.inner.log
|
||||
}
|
||||
@@ -218,4 +225,27 @@ impl ExecutionLayer {
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn find_ttd_block_hash(&self) -> Result<Option<Hash256>, Error> {
|
||||
self.engines()
|
||||
.first_success(|engine| async move {
|
||||
let mut ttd_exceeding_block = None;
|
||||
let mut block = engine
|
||||
.api
|
||||
.get_block_by_number(BlockByNumberQuery::Tag(LATEST_TAG))
|
||||
.await?;
|
||||
|
||||
loop {
|
||||
if block.total_difficulty >= self.total_terminal_difficulty() {
|
||||
ttd_exceeding_block = Some(block.block_hash);
|
||||
|
||||
block = engine.api.get_block_by_hash(block.parent_hash).await?;
|
||||
} else {
|
||||
return Ok::<_, ApiError>(ttd_exceeding_block);
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(Error::EngineErrors)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user