Add search for TTD block

This commit is contained in:
Paul Hauner
2021-09-27 16:05:19 +10:00
parent c329fae53c
commit f9fd6ac392
3 changed files with 82 additions and 1 deletions

View File

@@ -2,7 +2,9 @@ use async_trait::async_trait;
use eth1::http::RpcError; use eth1::http::RpcError;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub use types::{Address, EthSpec, ExecutionPayload, Hash256}; pub const LATEST_TAG: &str = "latest";
pub use types::{Address, EthSpec, ExecutionPayload, Hash256, Uint256};
pub mod http; pub mod http;
@@ -37,6 +39,13 @@ impl From<serde_json::Error> for Error {
pub trait EngineApi { pub trait EngineApi {
async fn upcheck(&self) -> Result<(), Error>; async fn upcheck(&self) -> Result<(), Error>;
async fn get_block_by_number<'a>(
&self,
block_by_number: BlockByNumberQuery<'a>,
) -> Result<ExecutionBlock, Error>;
async fn get_block_by_hash<'a>(&self, block_hash: Hash256) -> Result<ExecutionBlock, Error>;
async fn prepare_payload( async fn prepare_payload(
&self, &self,
parent_hash: Hash256, parent_hash: Hash256,
@@ -82,3 +91,18 @@ pub enum ConsensusStatus {
Valid, Valid,
Invalid, Invalid,
} }
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
#[serde(untagged)]
pub enum BlockByNumberQuery<'a> {
Tag(&'a str),
}
#[derive(Clone, Copy, Debug, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExecutionBlock {
pub block_hash: Hash256,
pub block_number: u64,
pub parent_hash: Hash256,
pub total_difficulty: Uint256,
}

View File

@@ -13,6 +13,12 @@ pub use reqwest::Client;
const STATIC_ID: u32 = 1; const STATIC_ID: u32 = 1;
const JSONRPC_VERSION: &str = "2.0"; const JSONRPC_VERSION: &str = "2.0";
const ETH_GET_BLOCK_BY_NUMBER: &str = "eth_getBlockByNumber";
const ETH_GET_BLOCK_BY_NUMBER_TIMEOUT: Duration = Duration::from_secs(1);
const ETH_GET_BLOCK_BY_HASH: &str = "eth_getBlockByHash";
const ETH_GET_BLOCK_BY_HASH_TIMEOUT: Duration = Duration::from_secs(1);
const ETH_SYNCING: &str = "eth_syncing"; const ETH_SYNCING: &str = "eth_syncing";
const ETH_SYNCING_TIMEOUT: Duration = Duration::from_millis(250); const ETH_SYNCING_TIMEOUT: Duration = Duration::from_millis(250);
@@ -103,6 +109,27 @@ impl EngineApi for HttpJsonRpc {
} }
} }
async fn get_block_by_number<'a>(
&self,
query: BlockByNumberQuery<'a>,
) -> Result<ExecutionBlock, Error> {
let params = json!([query]);
self.rpc_request(
ETH_GET_BLOCK_BY_NUMBER,
params,
ETH_GET_BLOCK_BY_NUMBER_TIMEOUT,
)
.await
}
async fn get_block_by_hash<'a>(&self, block_hash: Hash256) -> Result<ExecutionBlock, Error> {
let params = json!([block_hash]);
self.rpc_request(ETH_GET_BLOCK_BY_HASH, params, ETH_GET_BLOCK_BY_HASH_TIMEOUT)
.await
}
async fn prepare_payload( async fn prepare_payload(
&self, &self,
parent_hash: Hash256, parent_hash: Hash256,

View File

@@ -30,6 +30,7 @@ impl From<ApiError> for Error {
struct Inner { struct Inner {
engines: Engines<HttpJsonRpc>, engines: Engines<HttpJsonRpc>,
total_terminal_difficulty: Uint256,
executor: TaskExecutor, executor: TaskExecutor,
log: Logger, log: Logger,
} }
@@ -42,6 +43,7 @@ pub struct ExecutionLayer {
impl ExecutionLayer { impl ExecutionLayer {
pub fn from_urls( pub fn from_urls(
urls: Vec<SensitiveUrl>, urls: Vec<SensitiveUrl>,
total_terminal_difficulty: Uint256,
executor: TaskExecutor, executor: TaskExecutor,
log: Logger, log: Logger,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
@@ -59,6 +61,7 @@ impl ExecutionLayer {
engines, engines,
log: log.clone(), log: log.clone(),
}, },
total_terminal_difficulty,
executor, executor,
log, log,
}; };
@@ -78,6 +81,10 @@ impl ExecutionLayer {
&self.inner.executor &self.inner.executor
} }
fn total_terminal_difficulty(&self) -> Uint256 {
self.inner.total_terminal_difficulty
}
fn log(&self) -> &Logger { fn log(&self) -> &Logger {
&self.inner.log &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)
}
} }