Skip to content

Commit

Permalink
hydrated -> full, fix chain id encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Dec 7, 2023
1 parent 55bdd51 commit ea16159
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 24 deletions.
32 changes: 30 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -4964,6 +4990,7 @@ name = "lightning-rpc"
version = "0.0.0"
dependencies = [
"affair 0.1.2",
"alloy-primitives",
"anyhow",
"async-trait",
"autometrics",
Expand Down Expand Up @@ -7844,16 +7871,17 @@ 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",
"ark-ff 0.4.2",
"bytes",
"fastrlp",
"num-bigint",
"num-traits",
"parity-scale-codec",
"primitive-types",
"proptest",
Expand Down
1 change: 1 addition & 0 deletions core/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 9 additions & 7 deletions core/rpc/src/api/eth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloy_primitives::U64;
use ethers::types::{
Address,
Block,
Expand Down Expand Up @@ -34,11 +35,12 @@ pub trait EthApi {
) -> RpcResult<U256>;

#[method(name = "protocolVersion")]
async fn protocol_version(&self) -> RpcResult<u64>;
async fn protocol_version(&self) -> RpcResult<U64>;

#[method(name = "chainId")]
async fn chain_id(&self) -> RpcResult<u32>;
async fn chain_id(&self) -> RpcResult<Option<U64>>;

/// todo!
#[method(name = "syncing")]
async fn syncing(&self) -> RpcResult<bool>;

Expand All @@ -52,11 +54,11 @@ pub trait EthApi {
async fn block_by_number(
&self,
block_number: BlockNumber,
hydrated: bool,
) -> RpcResult<Option<H256>>;
full: bool,
) -> RpcResult<Option<Block<H256>>>;

#[method(name = "getBlockByHash")]
async fn block_by_hash(&self, hash: H256, hydrated: bool) -> RpcResult<Option<Block<H256>>>;
async fn block_by_hash(&self, hash: H256, full: bool) -> RpcResult<Option<Block<H256>>>;

#[method(name = "gasPrice")]
async fn gas_price(&self) -> RpcResult<U256>;
Expand All @@ -65,7 +67,7 @@ pub trait EthApi {
async fn estimate_gas(&self, request: CallRequest) -> RpcResult<U256>;

#[method(name = "sendRawTransaction")]
async fn send_raw_transaction(&self, transaction: Bytes) -> RpcResult<H256>;
async fn send_raw_transaction(&self, bytes: Bytes) -> RpcResult<H256>;

#[method(name = "getCode")]
async fn code(
Expand Down Expand Up @@ -112,7 +114,7 @@ pub trait EthApi {
#[method(name = "call")]
async fn call(
&self,
tx: TransactionRequest,
request: TransactionRequest,
block_number: Option<BlockNumber>,
state_overrides: Option<StateOverride>,
) -> RpcResult<Bytes>;
Expand Down
5 changes: 5 additions & 0 deletions core/rpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -37,6 +38,9 @@ pub enum RPCError {
#[error("Unimplemented")]
Unimplemented,

#[error("Failed to parse uint {}", .0)]
ParseError(#[from] ParseError),

#[error("RPCError {}", .0)]
Custom(String),
}
Expand Down Expand Up @@ -75,6 +79,7 @@ impl From<RPCError> 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),
}
}
}
Expand Down
31 changes: 20 additions & 11 deletions core/rpc/src/logic/eth_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use alloy_primitives::U64;
use ethers::types::{
Address,
Block,
Expand Down Expand Up @@ -42,17 +43,14 @@ impl<C: Collection> EthApiServer for EthApi<C> {
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<BlockNumber>,
_block: Option<BlockNumber>,
) -> RpcResult<U256> {
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,
))
Expand All @@ -73,14 +71,21 @@ impl<C: Collection> EthApiServer for EthApi<C> {
.unwrap_or(U256::zero()))
}

async fn protocol_version(&self) -> RpcResult<u64> {
async fn protocol_version(&self) -> RpcResult<U64> {
trace!(target: "rpc::eth", "Serving eth_protocolVersion");
Ok(0x41)
Ok("0".parse::<U64>().map_err(RPCError::from)?)
}

async fn chain_id(&self) -> RpcResult<u32> {
async fn chain_id(&self) -> RpcResult<Option<U64>> {
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::<U64>()
.map_err(RPCError::from)?,
))
}

/// todo(n)
Expand All @@ -89,11 +94,15 @@ impl<C: Collection> EthApiServer for EthApi<C> {
Ok(false)
}

async fn block_by_number(&self, number: BlockNumber, full: bool) -> RpcResult<Option<H256>> {
async fn block_by_number(
&self,
number: BlockNumber,
full: bool,
) -> RpcResult<Option<Block<H256>>> {
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()),
}
Expand Down
8 changes: 4 additions & 4 deletions core/rpc/src/logic/net_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ 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;
use crate::error::RPCError;
use crate::Data;

pub struct NetApi<C: Collection> {
data: Arc<Data<C>>,
_data: Arc<Data<C>>,
}

impl<C: Collection> NetApi<C> {
pub(crate) fn new(data: Arc<Data<C>>) -> Self {
Self { data }
Self { _data: data }
}
}

#[async_trait::async_trait]
impl<C: Collection> NetApiServer for NetApi<C> {
/// todo!()
async fn peer_count(&self) -> RpcResult<Option<String>> {
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<Option<String>> {
Expand Down

0 comments on commit ea16159

Please sign in to comment.