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,
config: Arc<Config>,
payloads: HashMap<u64, ExecutionPayload>,
block_hashes: HashMap<Vec<u8>, 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<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 {
self.config.general.chain_id
}

View File

@ -64,6 +64,8 @@ trait EthRpc {
async fn block_number(&self) -> Result<String, Error>;
#[method(name = "getBlockByNumber")]
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")]
async fn send_raw_transaction(&self, bytes: &str) -> Result<String, Error>;
#[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<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> {
let client = self.client.lock().await;
let bytes = convert_err(hex_str_to_bytes(bytes))?;