diff --git a/client/src/client.rs b/client/src/client.rs index d413d04..bf0424b 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -10,7 +10,7 @@ use consensus::types::Header; use execution::types::{CallOpts, ExecutionBlock}; use log::{info, warn}; use tokio::spawn; -use tokio::sync::Mutex; +use tokio::sync::RwLock; use tokio::time::sleep; use crate::database::{Database, FileDB}; @@ -18,7 +18,7 @@ use crate::node::{BlockTag, Node}; use crate::rpc::Rpc; pub struct Client { - node: Arc>, + node: Arc>, rpc: Option, db: DB, } @@ -27,7 +27,7 @@ impl Client { pub async fn new(config: Config) -> Result { let config = Arc::new(config); let node = Node::new(config.clone()).await?; - let node = Arc::new(Mutex::new(node)); + let node = Arc::new(RwLock::new(node)); let rpc = if let Some(port) = config.general.rpc_port { Some(Rpc::new(node.clone(), port)) @@ -48,13 +48,13 @@ impl Client { let node = self.node.clone(); spawn(async move { - let res = node.lock().await.sync().await; + let res = node.write().await.sync().await; if let Err(err) = res { warn!("{}", err); } loop { - let res = node.lock().await.advance().await; + let res = node.write().await.advance().await; if let Err(err) = res { warn!("{}", err); } @@ -70,7 +70,7 @@ impl Client { println!(); info!("shutting down"); - let node = self.node.lock().await; + let node = self.node.read().await; let checkpoint = if let Some(checkpoint) = node.get_last_checkpoint() { checkpoint } else { @@ -85,31 +85,31 @@ impl Client { } pub async fn call(&self, opts: &CallOpts, block: &BlockTag) -> Result> { - self.node.lock().await.call(opts, block) + self.node.read().await.call(opts, block) } pub async fn estimate_gas(&self, opts: &CallOpts) -> Result { - self.node.lock().await.estimate_gas(opts) + self.node.read().await.estimate_gas(opts) } pub async fn get_balance(&self, address: &Address, block: &BlockTag) -> Result { - self.node.lock().await.get_balance(address, block).await + self.node.read().await.get_balance(address, block).await } pub async fn get_nonce(&self, address: &Address, block: &BlockTag) -> Result { - self.node.lock().await.get_nonce(address, block).await + self.node.read().await.get_nonce(address, block).await } pub async fn get_code(&self, address: &Address, block: &BlockTag) -> Result> { - self.node.lock().await.get_code(address, block).await + self.node.read().await.get_code(address, block).await } pub async fn get_storage_at(&self, address: &Address, slot: H256) -> Result { - self.node.lock().await.get_storage_at(address, slot).await + self.node.read().await.get_storage_at(address, slot).await } pub async fn send_raw_transaction(&self, bytes: &Vec) -> Result { - self.node.lock().await.send_raw_transaction(bytes).await + self.node.read().await.send_raw_transaction(bytes).await } pub async fn get_transaction_receipt( @@ -117,7 +117,7 @@ impl Client { tx_hash: &H256, ) -> Result> { self.node - .lock() + .read() .await .get_transaction_receipt(tx_hash) .await @@ -125,37 +125,37 @@ impl Client { pub async fn get_transaction_by_hash(&self, tx_hash: &H256) -> Result> { self.node - .lock() + .read() .await .get_transaction_by_hash(tx_hash) .await } pub async fn get_gas_price(&self) -> Result { - self.node.lock().await.get_gas_price() + self.node.read().await.get_gas_price() } pub async fn get_priority_fee(&self) -> Result { - self.node.lock().await.get_priority_fee() + self.node.read().await.get_priority_fee() } pub async fn get_block_number(&self) -> Result { - self.node.lock().await.get_block_number() + self.node.read().await.get_block_number() } pub async fn get_block_by_number(&self, block: &BlockTag) -> Result { - self.node.lock().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) -> Result { - self.node.lock().await.get_block_by_hash(hash) + self.node.read().await.get_block_by_hash(hash) } pub async fn chain_id(&self) -> u64 { - self.node.lock().await.chain_id() + self.node.read().await.chain_id() } pub async fn get_header(&self) -> Header { - self.node.lock().await.get_header().clone() + self.node.read().await.get_header().clone() } } diff --git a/client/src/rpc.rs b/client/src/rpc.rs index ff35b0d..2953536 100644 --- a/client/src/rpc.rs +++ b/client/src/rpc.rs @@ -5,7 +5,7 @@ use ethers::{ use eyre::Result; use log::{debug, info, warn}; use std::{fmt::Display, net::SocketAddr, str::FromStr, sync::Arc}; -use tokio::sync::Mutex; +use tokio::sync::RwLock; use jsonrpsee::{ core::{async_trait, server::rpc_module::Methods, Error}, @@ -19,13 +19,13 @@ use common::utils::{hex_str_to_bytes, u64_to_hex_string}; use execution::types::{CallOpts, ExecutionBlock}; pub struct Rpc { - node: Arc>, + node: Arc>, handle: Option, port: u16, } impl Rpc { - pub fn new(node: Arc>, port: u16) -> Self { + pub fn new(node: Arc>, port: u16) -> Self { Rpc { node, handle: None, @@ -87,7 +87,7 @@ trait NetRpc { #[derive(Clone)] struct RpcInner { - node: Arc>, + node: Arc>, port: u16, } @@ -97,7 +97,7 @@ impl EthRpcServer for RpcInner { debug!("eth_getBalance"); let block = convert_err(decode_block(block))?; let address = convert_err(Address::from_str(address))?; - let node = self.node.lock().await; + let node = self.node.read().await; let balance = convert_err(node.get_balance(&address, &block).await)?; Ok(balance.encode_hex()) @@ -106,7 +106,7 @@ impl EthRpcServer for RpcInner { async fn get_transaction_count(&self, address: &str, block: &str) -> Result { let block = convert_err(decode_block(block))?; let address = convert_err(Address::from_str(address))?; - let node = self.node.lock().await; + let node = self.node.read().await; let nonce = convert_err(node.get_nonce(&address, &block).await)?; Ok(nonce.encode_hex()) @@ -115,7 +115,7 @@ impl EthRpcServer for RpcInner { async fn get_code(&self, address: &str, block: &str) -> Result { let block = convert_err(decode_block(block))?; let address = convert_err(Address::from_str(address))?; - let node = self.node.lock().await; + let node = self.node.read().await; let code = convert_err(node.get_code(&address, &block).await)?; Ok(hex::encode(code)) @@ -124,7 +124,7 @@ impl EthRpcServer for RpcInner { async fn call(&self, opts: CallOpts, block: &str) -> Result { debug!("eth_call"); let block = convert_err(decode_block(block))?; - let node = self.node.lock().await; + let node = self.node.read().await; let res = convert_err(node.call(&opts, &block))?; Ok(format!("0x{}", hex::encode(res))) @@ -132,32 +132,32 @@ impl EthRpcServer for RpcInner { async fn estimate_gas(&self, opts: CallOpts) -> Result { debug!("eth_estimateGas"); - let node = self.node.lock().await; + let node = self.node.read().await; let gas = convert_err(node.estimate_gas(&opts))?; Ok(u64_to_hex_string(gas)) } async fn chain_id(&self) -> Result { - let node = self.node.lock().await; + let node = self.node.read().await; let id = node.chain_id(); Ok(u64_to_hex_string(id)) } async fn gas_price(&self) -> Result { - let node = self.node.lock().await; + let node = self.node.read().await; let gas_price = convert_err(node.get_gas_price())?; Ok(gas_price.encode_hex()) } async fn max_priority_fee_per_gas(&self) -> Result { - let node = self.node.lock().await; + let node = self.node.read().await; let tip = convert_err(node.get_priority_fee())?; Ok(tip.encode_hex()) } async fn block_number(&self) -> Result { - let node = self.node.lock().await; + let node = self.node.read().await; let num = convert_err(node.get_block_number())?; Ok(u64_to_hex_string(num)) } @@ -168,7 +168,7 @@ impl EthRpcServer for RpcInner { _full_tx: bool, ) -> Result { let block = convert_err(decode_block(block))?; - let node = self.node.lock().await; + let node = self.node.read().await; let block = convert_err(node.get_block_by_number(&block))?; Ok(block) @@ -176,21 +176,21 @@ impl EthRpcServer for RpcInner { async fn get_block_by_hash(&self, hash: &str, _full_tx: bool) -> Result { let hash = convert_err(hex_str_to_bytes(hash))?; - let node = self.node.lock().await; + let node = self.node.read().await; let block = convert_err(node.get_block_by_hash(&hash))?; Ok(block) } async fn send_raw_transaction(&self, bytes: &str) -> Result { - let node = self.node.lock().await; + let node = self.node.read().await; let bytes = convert_err(hex_str_to_bytes(bytes))?; let tx_hash = convert_err(node.send_raw_transaction(&bytes).await)?; Ok(hex::encode(tx_hash)) } async fn get_transaction_receipt(&self, hash: &str) -> Result { - let node = self.node.lock().await; + 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)?; @@ -201,7 +201,7 @@ impl EthRpcServer for RpcInner { } async fn get_transaction_by_hash(&self, hash: &str) -> Result { - let node = self.node.lock().await; + 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)?; @@ -215,7 +215,7 @@ impl EthRpcServer for RpcInner { #[async_trait] impl NetRpcServer for RpcInner { async fn version(&self) -> Result { - let node = self.node.lock().await; + let node = self.node.read().await; Ok(node.chain_id().to_string()) } }