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() 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) 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) self.node.read().await.get_block_by_hash(hash)
} }

View File

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

View File

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