fix: handle null rpc responses (#47)

This commit is contained in:
Noah Citron 2022-09-22 20:19:24 -07:00 committed by GitHub
parent abfed6a8fe
commit b137df4b85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 29 deletions

View File

@ -143,11 +143,11 @@ impl<DB: Database> Client<DB> {
self.node.read().await.get_block_number()
}
pub async fn get_block_by_number(&self, block: &BlockTag) -> Result<ExecutionBlock> {
pub async fn get_block_by_number(&self, block: &BlockTag) -> Result<Option<ExecutionBlock>> {
self.node.read().await.get_block_by_number(block)
}
pub async fn get_block_by_hash(&self, hash: &Vec<u8>) -> Result<ExecutionBlock> {
pub async fn get_block_by_hash(&self, hash: &Vec<u8>) -> Result<Option<ExecutionBlock>> {
self.node.read().await.get_block_by_hash(hash)
}

View File

@ -166,20 +166,24 @@ impl Node {
Ok(payload.block_number)
}
pub fn get_block_by_number(&self, block: &BlockTag) -> Result<ExecutionBlock> {
let payload = self.get_payload(block)?;
self.execution.get_block(payload)
pub fn get_block_by_number(&self, block: &BlockTag) -> Result<Option<ExecutionBlock>> {
match self.get_payload(block) {
Ok(payload) => self.execution.get_block(payload).map(|b| Some(b)),
Err(_) => Ok(None),
}
}
pub fn get_block_by_hash(&self, hash: &Vec<u8>) -> Result<ExecutionBlock> {
pub fn get_block_by_hash(&self, hash: &Vec<u8>) -> Result<Option<ExecutionBlock>> {
let payloads = self
.payloads
.iter()
.filter(|entry| &entry.1.block_hash.to_vec() == hash)
.collect::<Vec<(&u64, &ExecutionPayload)>>();
let payload = payloads.get(0).ok_or(eyre!("Block Not Found"))?.1;
self.execution.get_block(payload)
match payloads.get(0) {
Some(payload_entry) => self.execution.get_block(payload_entry.1).map(|b| Some(b)),
None => Ok(None),
}
}
pub fn chain_id(&self) -> u64 {

View File

@ -68,15 +68,26 @@ trait EthRpc {
#[method(name = "blockNumber")]
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>;
async fn get_block_by_number(
&self,
num: &str,
full_tx: bool,
) -> Result<Option<ExecutionBlock>, Error>;
#[method(name = "getBlockByHash")]
async fn get_block_by_hash(&self, hash: &str, full_tx: bool) -> Result<ExecutionBlock, Error>;
async fn get_block_by_hash(
&self,
hash: &str,
full_tx: bool,
) -> Result<Option<ExecutionBlock>, Error>;
#[method(name = "sendRawTransaction")]
async fn send_raw_transaction(&self, bytes: &str) -> Result<String, Error>;
#[method(name = "getTransactionReceipt")]
async fn get_transaction_receipt(&self, hash: &str) -> Result<TransactionReceipt, Error>;
async fn get_transaction_receipt(
&self,
hash: &str,
) -> Result<Option<TransactionReceipt>, Error>;
#[method(name = "getTransactionByHash")]
async fn get_transaction_by_hash(&self, hash: &str) -> Result<Transaction, Error>;
async fn get_transaction_by_hash(&self, hash: &str) -> Result<Option<Transaction>, Error>;
}
#[rpc(client, server, namespace = "net")]
@ -166,19 +177,21 @@ impl EthRpcServer for RpcInner {
&self,
block: &str,
_full_tx: bool,
) -> Result<ExecutionBlock, Error> {
) -> Result<Option<ExecutionBlock>, Error> {
let block = convert_err(decode_block(block))?;
let node = self.node.read().await;
let block = convert_err(node.get_block_by_number(&block))?;
Ok(block)
}
async fn get_block_by_hash(&self, hash: &str, _full_tx: bool) -> Result<ExecutionBlock, Error> {
async fn get_block_by_hash(
&self,
hash: &str,
_full_tx: bool,
) -> Result<Option<ExecutionBlock>, Error> {
let hash = convert_err(hex_str_to_bytes(hash))?;
let node = self.node.read().await;
let block = convert_err(node.get_block_by_hash(&hash))?;
Ok(block)
}
@ -189,26 +202,20 @@ impl EthRpcServer for RpcInner {
Ok(hex::encode(tx_hash))
}
async fn get_transaction_receipt(&self, hash: &str) -> Result<TransactionReceipt, Error> {
async fn get_transaction_receipt(
&self,
hash: &str,
) -> Result<Option<TransactionReceipt>, Error> {
let node = self.node.read().await;
let hash = H256::from_slice(&convert_err(hex_str_to_bytes(hash))?);
let receipt = convert_err(node.get_transaction_receipt(&hash).await)?;
match receipt {
Some(receipt) => Ok(receipt),
None => Err(Error::Custom("Receipt Not Found".to_string())),
}
Ok(receipt)
}
async fn get_transaction_by_hash(&self, hash: &str) -> Result<Transaction, Error> {
async fn get_transaction_by_hash(&self, hash: &str) -> Result<Option<Transaction>, Error> {
let node = self.node.read().await;
let hash = H256::from_slice(&convert_err(hex_str_to_bytes(hash))?);
let tx = convert_err(node.get_transaction_by_hash(&hash).await)?;
match tx {
Some(tx) => Ok(tx),
None => Err(Error::Custom("Transaction Not Found".to_string())),
}
convert_err(node.get_transaction_by_hash(&hash).await)
}
}