Skip to content

Commit

Permalink
feat: define state related eth_* rpc endpoints (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
morph-dev authored Aug 8, 2024
1 parent 57bb4b5 commit 928459b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
18 changes: 16 additions & 2 deletions ethportal-api/src/eth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloy_primitives::{B256, U256};
use alloy_primitives::{Address, Bytes, B256, U256};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use reth_rpc_types::Block;
use reth_rpc_types::{Block, BlockId};

/// Web3 JSON-RPC endpoints
#[rpc(client, server, namespace = "eth")]
Expand All @@ -14,4 +14,18 @@ pub trait EthApi {
block_hash: B256,
hydrated_transactions: bool,
) -> RpcResult<Block>;

#[method(name = "getBalance")]
async fn get_balance(&self, address: Address, block: BlockId) -> RpcResult<U256>;

#[method(name = "getCode")]
async fn get_code(&self, address: Address, block: BlockId) -> RpcResult<Bytes>;

#[method(name = "getStorageAt")]
async fn get_storage_at(
&self,
address: Address,
slot: U256,
block: BlockId,
) -> RpcResult<Bytes>;
}
3 changes: 2 additions & 1 deletion rpc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ impl RpcModuleBuilder {
.history_tx
.clone()
.expect("History protocol not initialized");
EthApi::new(history_tx).into_rpc().into()
let state_tx = self.state_tx.clone();
EthApi::new(history_tx, state_tx).into_rpc().into()
}
PortalRpcModule::History => {
let history_tx = self
Expand Down
43 changes: 35 additions & 8 deletions rpc/src/eth_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use alloy_primitives::{B256, U256};
use reth_rpc_types::{other::OtherFields, Block, BlockTransactions};
use alloy_primitives::{Address, Bytes, B256, U256};
use reth_rpc_types::{other::OtherFields, Block, BlockId, BlockTransactions};
use tokio::sync::mpsc;

use ethportal_api::{
types::{execution::block_body::BlockBody, jsonrpc::request::HistoryJsonRpcRequest},
types::{
execution::block_body::BlockBody,
jsonrpc::request::{HistoryJsonRpcRequest, StateJsonRpcRequest},
},
EthApiServer,
};
use trin_validation::constants::CHAIN_ID;
Expand All @@ -15,12 +18,19 @@ use crate::{
};

pub struct EthApi {
network: mpsc::UnboundedSender<HistoryJsonRpcRequest>,
history_network: mpsc::UnboundedSender<HistoryJsonRpcRequest>,
_state_network: Option<mpsc::UnboundedSender<StateJsonRpcRequest>>,
}

impl EthApi {
pub fn new(network: mpsc::UnboundedSender<HistoryJsonRpcRequest>) -> Self {
Self { network }
pub fn new(
history_network: mpsc::UnboundedSender<HistoryJsonRpcRequest>,
state_network: Option<mpsc::UnboundedSender<StateJsonRpcRequest>>,
) -> Self {
Self {
history_network,
_state_network: state_network,
}
}
}

Expand All @@ -42,8 +52,8 @@ impl EthApiServer for EthApi {
.into());
}

let header = find_header_by_hash(&self.network, block_hash).await?;
let body = find_block_body_by_hash(&self.network, block_hash).await?;
let header = find_header_by_hash(&self.history_network, block_hash).await?;
let body = find_block_body_by_hash(&self.history_network, block_hash).await?;
let transactions = match body {
BlockBody::Legacy(body) => body.txs,
BlockBody::Merge(body) => body.txs,
Expand All @@ -67,6 +77,23 @@ impl EthApiServer for EthApi {
};
Ok(block)
}

async fn get_balance(&self, _address: Address, _block: BlockId) -> RpcResult<U256> {
todo!()
}

async fn get_code(&self, _address: Address, _block: BlockId) -> RpcResult<Bytes> {
todo!()
}

async fn get_storage_at(
&self,
_address: Address,
_slot: U256,
_block: BlockId,
) -> RpcResult<Bytes> {
todo!()
}
}

impl std::fmt::Debug for EthApi {
Expand Down

0 comments on commit 928459b

Please sign in to comment.