diff --git a/client/src/client.rs b/client/src/client.rs index bf0424b..96e5dc5 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -143,11 +143,11 @@ impl Client { self.node.read().await.get_block_number() } - pub async fn get_block_by_number(&self, block: &BlockTag) -> Result { + pub async fn get_block_by_number(&self, block: &BlockTag) -> Result> { self.node.read().await.get_block_by_number(block) } - pub async fn get_block_by_hash(&self, hash: &Vec) -> Result { + pub async fn get_block_by_hash(&self, hash: &Vec) -> Result> { self.node.read().await.get_block_by_hash(hash) } diff --git a/client/src/node.rs b/client/src/node.rs index d60b608..4d3f0af 100644 --- a/client/src/node.rs +++ b/client/src/node.rs @@ -166,20 +166,24 @@ impl Node { Ok(payload.block_number) } - pub fn get_block_by_number(&self, block: &BlockTag) -> Result { - let payload = self.get_payload(block)?; - self.execution.get_block(payload) + pub fn get_block_by_number(&self, block: &BlockTag) -> Result> { + 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) -> Result { + pub fn get_block_by_hash(&self, hash: &Vec) -> Result> { let payloads = self .payloads .iter() .filter(|entry| &entry.1.block_hash.to_vec() == hash) .collect::>(); - 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 { diff --git a/client/src/rpc.rs b/client/src/rpc.rs index 2953536..a085d56 100644 --- a/client/src/rpc.rs +++ b/client/src/rpc.rs @@ -68,15 +68,26 @@ trait EthRpc { #[method(name = "blockNumber")] async fn block_number(&self) -> Result; #[method(name = "getBlockByNumber")] - async fn get_block_by_number(&self, num: &str, full_tx: bool) -> Result; + async fn get_block_by_number( + &self, + num: &str, + full_tx: bool, + ) -> Result, Error>; #[method(name = "getBlockByHash")] - async fn get_block_by_hash(&self, hash: &str, full_tx: bool) -> Result; + async fn get_block_by_hash( + &self, + hash: &str, + full_tx: bool, + ) -> Result, Error>; #[method(name = "sendRawTransaction")] async fn send_raw_transaction(&self, bytes: &str) -> Result; #[method(name = "getTransactionReceipt")] - async fn get_transaction_receipt(&self, hash: &str) -> Result; + async fn get_transaction_receipt( + &self, + hash: &str, + ) -> Result, Error>; #[method(name = "getTransactionByHash")] - async fn get_transaction_by_hash(&self, hash: &str) -> Result; + async fn get_transaction_by_hash(&self, hash: &str) -> Result, Error>; } #[rpc(client, server, namespace = "net")] @@ -166,19 +177,21 @@ impl EthRpcServer for RpcInner { &self, block: &str, _full_tx: bool, - ) -> Result { + ) -> Result, 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 { + async fn get_block_by_hash( + &self, + hash: &str, + _full_tx: bool, + ) -> Result, 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 { + async fn get_transaction_receipt( + &self, + hash: &str, + ) -> Result, 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 { + async fn get_transaction_by_hash(&self, hash: &str) -> Result, 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) } }