diff --git a/Cargo.lock b/Cargo.lock index aa86fa44c..fc2b95249 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -123,6 +123,26 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "250f629c0161ad8107cf89319e990051fae62832fd343083bea452d93e2205fd" +[[package]] +name = "alloy-primitives" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08ca2c09d5911548a5cb620382ea0e1af99d3c898ce0efecbbd274a4676cf53e" +dependencies = [ + "alloy-rlp", + "bytes", + "cfg-if", + "const-hex", + "derive_more", + "hex-literal", + "itoa", + "proptest", + "rand 0.8.5", + "ruint", + "serde", + "tiny-keccak", +] + [[package]] name = "alloy-rlp" version = "0.3.3" @@ -3578,6 +3598,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + [[package]] name = "hkdf" version = "0.12.3" @@ -4964,6 +4990,7 @@ name = "lightning-rpc" version = "0.0.0" dependencies = [ "affair 0.1.2", + "alloy-primitives", "anyhow", "async-trait", "autometrics", @@ -7844,9 +7871,9 @@ dependencies = [ [[package]] name = "ruint" -version = "1.10.1" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95294d6e3a6192f3aabf91c38f56505a625aa495533442744185a36d75a790c4" +checksum = "608a5726529f2f0ef81b8fde9873c4bb829d6b5b5ca6be4d97345ddf0749c825" dependencies = [ "alloy-rlp", "ark-ff 0.3.0", @@ -7854,6 +7881,7 @@ dependencies = [ "bytes", "fastrlp", "num-bigint", + "num-traits", "parity-scale-codec", "primitive-types", "proptest", diff --git a/core/rpc/Cargo.toml b/core/rpc/Cargo.toml index f6f01c3b6..2f9fe4386 100644 --- a/core/rpc/Cargo.toml +++ b/core/rpc/Cargo.toml @@ -33,6 +33,7 @@ clap = { version = "4.4.10", features = ["derive"]} lightning-interfaces = { path = "../interfaces" } lightning-openrpc = { path = "../rpc-openrpc" } lightning-openrpc-macros = { path = "../rpc-openrpc-macros" } +alloy-primitives = "0.5.2" [dev-dependencies] diff --git a/core/rpc/src/api/eth.rs b/core/rpc/src/api/eth.rs index d35257bec..dd4d4e042 100644 --- a/core/rpc/src/api/eth.rs +++ b/core/rpc/src/api/eth.rs @@ -1,3 +1,4 @@ +use alloy_primitives::U64; use ethers::types::{ Address, Block, @@ -34,11 +35,12 @@ pub trait EthApi { ) -> RpcResult; #[method(name = "protocolVersion")] - async fn protocol_version(&self) -> RpcResult; + async fn protocol_version(&self) -> RpcResult; #[method(name = "chainId")] - async fn chain_id(&self) -> RpcResult; + async fn chain_id(&self) -> RpcResult>; + /// todo! #[method(name = "syncing")] async fn syncing(&self) -> RpcResult; @@ -52,11 +54,11 @@ pub trait EthApi { async fn block_by_number( &self, block_number: BlockNumber, - hydrated: bool, - ) -> RpcResult>; + full: bool, + ) -> RpcResult>>; #[method(name = "getBlockByHash")] - async fn block_by_hash(&self, hash: H256, hydrated: bool) -> RpcResult>>; + async fn block_by_hash(&self, hash: H256, full: bool) -> RpcResult>>; #[method(name = "gasPrice")] async fn gas_price(&self) -> RpcResult; @@ -65,7 +67,7 @@ pub trait EthApi { async fn estimate_gas(&self, request: CallRequest) -> RpcResult; #[method(name = "sendRawTransaction")] - async fn send_raw_transaction(&self, transaction: Bytes) -> RpcResult; + async fn send_raw_transaction(&self, bytes: Bytes) -> RpcResult; #[method(name = "getCode")] async fn code( @@ -112,7 +114,7 @@ pub trait EthApi { #[method(name = "call")] async fn call( &self, - tx: TransactionRequest, + request: TransactionRequest, block_number: Option, state_overrides: Option, ) -> RpcResult; diff --git a/core/rpc/src/error.rs b/core/rpc/src/error.rs index 9b7ee6961..2d807fbea 100644 --- a/core/rpc/src/error.rs +++ b/core/rpc/src/error.rs @@ -3,6 +3,7 @@ use std::error::Error; use ethers::utils::rlp; use jsonrpsee::types::error::INTERNAL_ERROR_CODE; use jsonrpsee::types::ErrorObject; +use ruint::ParseError; #[derive(Debug)] pub struct SocketErrorWrapper(String); @@ -37,6 +38,9 @@ pub enum RPCError { #[error("Unimplemented")] Unimplemented, + #[error("Failed to parse uint {}", .0)] + ParseError(#[from] ParseError), + #[error("RPCError {}", .0)] Custom(String), } @@ -75,6 +79,7 @@ impl From for ErrorObject<'static> { RPCError::SocketError(e) => internal_err(e), RPCError::Unimplemented => internal_err_from_string("Unimplemented".to_string()), RPCError::Custom(s) => internal_err_from_string(s), + RPCError::ParseError(e) => internal_err(e), } } } diff --git a/core/rpc/src/logic/eth_impl.rs b/core/rpc/src/logic/eth_impl.rs index 7169cc563..464fff763 100644 --- a/core/rpc/src/logic/eth_impl.rs +++ b/core/rpc/src/logic/eth_impl.rs @@ -1,5 +1,6 @@ use std::sync::Arc; +use alloy_primitives::U64; use ethers::types::{ Address, Block, @@ -42,17 +43,14 @@ impl EthApiServer for EthApi { Ok(U256::from(self.data.query_runner.get_block_number())) } + /// todo this function always returns the current nonce async fn transaction_count( &self, address: EthAddress, - block: Option, + _block: Option, ) -> RpcResult { trace!(target: "rpc::eth", ?address, "Serving eth_getTransactionCount"); - if block.is_some() { - return Err(RPCError::custom("block number not supported".to_string()).into()); - } - Ok(U256::from( self.data.query_runner.get_account_nonce(&address) + 1, )) @@ -73,14 +71,21 @@ impl EthApiServer for EthApi { .unwrap_or(U256::zero())) } - async fn protocol_version(&self) -> RpcResult { + async fn protocol_version(&self) -> RpcResult { trace!(target: "rpc::eth", "Serving eth_protocolVersion"); - Ok(0x41) + Ok("0".parse::().map_err(RPCError::from)?) } - async fn chain_id(&self) -> RpcResult { + async fn chain_id(&self) -> RpcResult> { trace!(target: "rpc::eth", "Serving eth_chainId"); - Ok(self.data.query_runner.get_chain_id()) + Ok(Some( + self.data + .query_runner + .get_chain_id() + .to_string() + .parse::() + .map_err(RPCError::from)?, + )) } /// todo(n) @@ -89,11 +94,15 @@ impl EthApiServer for EthApi { Ok(false) } - async fn block_by_number(&self, number: BlockNumber, full: bool) -> RpcResult> { + async fn block_by_number( + &self, + number: BlockNumber, + full: bool, + ) -> RpcResult>> { trace!(target: "rpc::eth", ?number, ?full, "Serving eth_getBlockByNumber"); if let Some(socket) = &self.data.archive_socket { match socket.run(ArchiveRequest::GetBlockByNumber(number)).await { - Ok(Ok(ArchiveResponse::Block(block))) => Ok(Some(block.block_hash.into())), + Ok(Ok(ArchiveResponse::Block(block))) => Ok(Some(block.into())), Ok(Ok(ArchiveResponse::None)) => Ok(None), _ => Err(RPCError::custom("Failed to query block".to_string()).into()), } diff --git a/core/rpc/src/logic/net_impl.rs b/core/rpc/src/logic/net_impl.rs index 2146d1de5..b61a9d35b 100644 --- a/core/rpc/src/logic/net_impl.rs +++ b/core/rpc/src/logic/net_impl.rs @@ -2,7 +2,6 @@ use std::sync::Arc; use jsonrpsee::core::RpcResult; use lightning_interfaces::infu_collection::Collection; -use lightning_interfaces::SyncQueryRunnerInterface; use tracing::trace; use crate::api::NetApiServer; @@ -10,20 +9,21 @@ use crate::error::RPCError; use crate::Data; pub struct NetApi { - data: Arc>, + _data: Arc>, } impl NetApi { pub(crate) fn new(data: Arc>) -> Self { - Self { data } + Self { _data: data } } } #[async_trait::async_trait] impl NetApiServer for NetApi { + /// todo!() async fn peer_count(&self) -> RpcResult> { trace!(target: "rpc::eth", "Serving eth_chainId"); - Ok(Some(self.data.query_runner.get_chain_id().to_string())) + Ok(Some("0x40".into())) } async fn version(&self) -> RpcResult> {