feat: add eth_getBlockByHash (#9)

This commit is contained in:
Noah Citron 2022-09-02 00:13:22 -04:00 committed by GitHub
parent e0411e1e97
commit dbeeffb9f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -17,6 +17,7 @@ pub struct Client {
execution: ExecutionClient, execution: ExecutionClient,
config: Arc<Config>, config: Arc<Config>,
payloads: HashMap<u64, ExecutionPayload>, payloads: HashMap<u64, ExecutionPayload>,
block_hashes: HashMap<Vec<u8>, u64>,
block_head: u64, block_head: u64,
} }
@ -31,12 +32,14 @@ impl Client {
let execution = ExecutionClient::new(execution_rpc); let execution = ExecutionClient::new(execution_rpc);
let payloads = HashMap::new(); let payloads = HashMap::new();
let block_hashes = HashMap::new();
Ok(Client { Ok(Client {
consensus, consensus,
execution, execution,
config, config,
payloads, payloads,
block_hashes,
block_head: 0, block_head: 0,
}) })
} }
@ -49,7 +52,10 @@ impl Client {
.consensus .consensus
.get_execution_payload(&Some(head.slot)) .get_execution_payload(&Some(head.slot))
.await?; .await?;
self.block_head = payload.block_number; 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); self.payloads.insert(payload.block_number, payload);
Ok(()) Ok(())
@ -63,7 +69,10 @@ impl Client {
.consensus .consensus
.get_execution_payload(&Some(head.slot)) .get_execution_payload(&Some(head.slot))
.await?; .await?;
self.block_head = payload.block_number; 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); self.payloads.insert(payload.block_number, payload);
Ok(()) Ok(())
@ -152,6 +161,12 @@ impl Client {
self.execution.get_block(&payload) self.execution.get_block(&payload)
} }
pub fn get_block_by_hash(&self, hash: &Vec<u8>) -> Result<ExecutionBlock> {
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 { pub fn chain_id(&self) -> u64 {
self.config.general.chain_id self.config.general.chain_id
} }

View File

@ -64,6 +64,8 @@ trait EthRpc {
async fn block_number(&self) -> Result<String, Error>; async fn block_number(&self) -> Result<String, Error>;
#[method(name = "getBlockByNumber")] #[method(name = "getBlockByNumber")]
async fn get_block_by_number(&self, num: &str, full_tx: bool) -> Result<ExecutionBlock, Error>; async fn get_block_by_number(&self, num: &str, full_tx: bool) -> Result<ExecutionBlock, Error>;
#[method(name = "getBlockByHash")]
async fn get_block_by_hash(&self, hash: &str, full_tx: bool) -> Result<ExecutionBlock, Error>;
#[method(name = "sendRawTransaction")] #[method(name = "sendRawTransaction")]
async fn send_raw_transaction(&self, bytes: &str) -> Result<String, Error>; async fn send_raw_transaction(&self, bytes: &str) -> Result<String, Error>;
#[method(name = "getTransactionReceipt")] #[method(name = "getTransactionReceipt")]
@ -164,6 +166,14 @@ impl EthRpcServer for RpcInner {
Ok(block) Ok(block)
} }
async fn get_block_by_hash(&self, hash: &str, _full_tx: bool) -> Result<ExecutionBlock, Error> {
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<String, Error> { async fn send_raw_transaction(&self, bytes: &str) -> Result<String, Error> {
let client = self.client.lock().await; let client = self.client.lock().await;
let bytes = convert_err(hex_str_to_bytes(bytes))?; let bytes = convert_err(hex_str_to_bytes(bytes))?;