diff --git a/client/src/client.rs b/client/src/client.rs index efbe00d..2229665 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -17,6 +17,7 @@ pub struct Client { execution: ExecutionClient, config: Arc, payloads: HashMap, + block_hashes: HashMap, u64>, block_head: u64, } @@ -31,12 +32,14 @@ impl Client { let execution = ExecutionClient::new(execution_rpc); let payloads = HashMap::new(); + let block_hashes = HashMap::new(); Ok(Client { consensus, execution, config, payloads, + block_hashes, block_head: 0, }) } @@ -49,7 +52,10 @@ impl Client { .consensus .get_execution_payload(&Some(head.slot)) .await?; + self.block_head = payload.block_number; + self.block_hashes + .insert(payload.block_hash.to_vec(), payload.block_number); self.payloads.insert(payload.block_number, payload); Ok(()) @@ -63,7 +69,10 @@ impl Client { .consensus .get_execution_payload(&Some(head.slot)) .await?; + self.block_head = payload.block_number; + self.block_hashes + .insert(payload.block_hash.to_vec(), payload.block_number); self.payloads.insert(payload.block_number, payload); Ok(()) @@ -152,6 +161,12 @@ impl Client { self.execution.get_block(&payload) } + pub fn get_block_by_hash(&self, hash: &Vec) -> Result { + let block = self.block_hashes.get(hash); + let payload = self.get_payload(&block.cloned())?; + self.execution.get_block(&payload) + } + pub fn chain_id(&self) -> u64 { self.config.general.chain_id } diff --git a/client/src/rpc.rs b/client/src/rpc.rs index 0bbbc3a..5efeb6c 100644 --- a/client/src/rpc.rs +++ b/client/src/rpc.rs @@ -64,6 +64,8 @@ trait EthRpc { async fn block_number(&self) -> Result; #[method(name = "getBlockByNumber")] async fn get_block_by_number(&self, num: &str, full_tx: bool) -> Result; + #[method(name = "getBlockByHash")] + async fn get_block_by_hash(&self, hash: &str, full_tx: bool) -> Result; #[method(name = "sendRawTransaction")] async fn send_raw_transaction(&self, bytes: &str) -> Result; #[method(name = "getTransactionReceipt")] @@ -164,6 +166,14 @@ impl EthRpcServer for RpcInner { Ok(block) } + async fn get_block_by_hash(&self, hash: &str, _full_tx: bool) -> Result { + let hash = convert_err(hex_str_to_bytes(hash))?; + let client = self.client.lock().await; + let block = convert_err(client.get_block_by_hash(&hash))?; + + Ok(block) + } + async fn send_raw_transaction(&self, bytes: &str) -> Result { let client = self.client.lock().await; let bytes = convert_err(hex_str_to_bytes(bytes))?;