Skip to content

Commit

Permalink
Merge branch 'master' into add-eth_getFilterLogs
Browse files Browse the repository at this point in the history
  • Loading branch information
eshaan7 authored Nov 24, 2024
2 parents 3aef8c7 + aee6c31 commit aedd853
Show file tree
Hide file tree
Showing 24 changed files with 424 additions and 83 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 26 additions & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ use std::{
sync::{Arc, Mutex},
};

use alloy::primitives::hex;
use alloy::primitives::B256;
use clap::{Args, Parser, Subcommand};
use dirs::home_dir;
use eyre::Result;
use figment::providers::Serialized;
use figment::value::Value;
use futures::executor::block_on;
use tracing::{error, info};
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
use tracing_subscriber::FmtSubscriber;

use helios_core::client::Client;
use helios_core::consensus::Consensus;
use helios_core::network_spec::NetworkSpec;
use helios_ethereum::config::{cli::CliConfig, Config as EthereumConfig};
use helios_ethereum::database::FileDB;
use helios_ethereum::{EthereumClient, EthereumClientBuilder};
use helios_opstack::{config::Config as OpStackConfig, OpStackClient, OpStackClientBuilder};
use tracing::{error, info};
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
use tracing_subscriber::FmtSubscriber;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -191,6 +191,20 @@ struct OpStackArgs {
execution_rpc: Option<String>,
#[clap(short, long, env)]
consensus_rpc: Option<String>,
#[clap(
short = 'w',
long = "ethereum-checkpoint",
env = "ETHEREUM_CHECKPOINT",
help = "Set custom weak subjectivity checkpoint for chosen Ethereum network. Helios uses this to sync and trustlessly fetch the correct unsafe signer address used by <NETWORK>"
)]
checkpoint: Option<B256>,
#[clap(
short = 'l',
long = "ethereum-load-external-fallback",
env = "ETHEREUM_LOAD_EXTERNAL_FALLBACK",
help = "Enable fallback for weak subjectivity checkpoint. Use if --ethereum-checkpoint fails."
)]
load_external_fallback: bool,
}

impl OpStackArgs {
Expand Down Expand Up @@ -232,6 +246,14 @@ impl OpStackArgs {
user_dict.insert("rpc_port", Value::from(port));
}

if self.load_external_fallback {
user_dict.insert("load_external_fallback", Value::from(true));
}

if let Some(checkpoint) = self.checkpoint {
user_dict.insert("checkpoint", Value::from(hex::encode(checkpoint)));
}

Serialized::from(user_dict, &self.network)
}
}
Expand Down
10 changes: 10 additions & 0 deletions core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ impl<N: NetworkSpec, C: Consensus<N::TransactionResponse>> Client<N, C> {
.await
}

pub async fn get_transaction_by_block_number_and_index(
&self,
block: BlockTag,
index: u64,
) -> Result<Option<N::TransactionResponse>> {
self.node
.get_transaction_by_block_number_and_index(block, index)
.await
}

pub async fn chain_id(&self) -> u64 {
self.node.chain_id()
}
Expand Down
13 changes: 13 additions & 0 deletions core/src/client/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ impl<N: NetworkSpec, C: Consensus<N::TransactionResponse>> Node<N, C> {
.await
}

pub async fn get_transaction_by_block_number_and_index(
&self,
block: BlockTag,
index: u64,
) -> Result<Option<N::TransactionResponse>> {
self.check_blocktag_age(&block).await?;

Ok(self
.execution
.get_transaction_by_block_number_and_index(block, index)
.await)
}

pub async fn get_logs(&self, filter: &Filter) -> Result<Vec<Log>> {
self.execution.get_logs(filter).await
}
Expand Down
18 changes: 18 additions & 0 deletions core/src/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ trait EthRpc<TX: TransactionResponse + RpcObject, TXR: RpcObject, R: ReceiptResp
hash: B256,
index: U64,
) -> Result<Option<TX>, ErrorObjectOwned>;
#[method(name = "getTransactionByBlockNumberAndIndex")]
async fn get_transaction_by_block_number_and_index(
&self,
block: BlockTag,
index: U64,
) -> Result<Option<TX>, ErrorObjectOwned>;
#[method(name = "getLogs")]
async fn get_logs(&self, filter: Filter) -> Result<Vec<Log>, ErrorObjectOwned>;
#[method(name = "getFilterChanges")]
Expand Down Expand Up @@ -297,6 +303,18 @@ impl<N: NetworkSpec, C: Consensus<N::TransactionResponse>>
.await)
}

async fn get_transaction_by_block_number_and_index(
&self,
block: BlockTag,
index: U64,
) -> Result<Option<N::TransactionResponse>, ErrorObjectOwned> {
convert_err(
self.node
.get_transaction_by_block_number_and_index(block, index.to())
.await,
)
}

async fn coinbase(&self) -> Result<Address, ErrorObjectOwned> {
convert_err(self.node.get_coinbase().await)
}
Expand Down
17 changes: 13 additions & 4 deletions core/src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ use self::types::Account;
pub mod constants;
pub mod errors;
pub mod evm;
pub mod proof;
pub mod rpc;
pub mod state;
pub mod types;

mod proof;

#[derive(Clone)]
pub struct ExecutionClient<N: NetworkSpec, R: ExecutionRpc<N>> {
pub rpc: R,
Expand Down Expand Up @@ -64,7 +63,7 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionClient<N, R> {

let proof = self
.rpc
.get_proof(address, slots, block.number.to())
.get_proof(address, slots, block.number.into())
.await?;

let account_path = keccak256(address).to_vec();
Expand Down Expand Up @@ -175,7 +174,17 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> ExecutionClient<N, R> {
index: u64,
) -> Option<N::TransactionResponse> {
self.state
.get_transaction_by_block_and_index(block_hash, index)
.get_transaction_by_block_hash_and_index(block_hash, index)
.await
}

pub async fn get_transaction_by_block_number_and_index(
&self,
tag: BlockTag,
index: u64,
) -> Option<N::TransactionResponse> {
self.state
.get_transaction_by_block_and_index(tag, index)
.await
}

Expand Down
14 changes: 9 additions & 5 deletions core/src/execution/rpc/http_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ impl<N: NetworkSpec> ExecutionRpc<N> for HttpRpc<N> {
&self,
address: Address,
slots: &[B256],
block: u64,
block: BlockId,
) -> Result<EIP1186AccountProofResponse> {
let proof_response = self
.provider
.get_proof(address, slots.to_vec())
.block_id(block.into())
.block_id(block)
.await
.map_err(|e| RpcError::new("get_proof", e))?;

Expand Down Expand Up @@ -166,9 +166,13 @@ impl<N: NetworkSpec> ExecutionRpc<N> for HttpRpc<N> {
.map_err(|e| RpcError::new("get_filter_logs", e))?)
}

async fn uninstall_filter(&self, _filter_id: U256) -> Result<bool> {
// TODO: support uninstalling
Ok(true)

async fn uninstall_filter(&self, filter_id: U256) -> Result<bool> {
Ok(self
.provider
.raw_request("eth_uninstallFilter".into(), (filter_id,))
.await
.map_err(|e| RpcError::new("uninstall_filter", e))?)
}

async fn get_new_filter(&self, filter: &Filter) -> Result<U256> {
Expand Down
6 changes: 4 additions & 2 deletions core/src/execution/rpc/mock_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{fs::read_to_string, path::PathBuf};

use alloy::primitives::{Address, B256, U256};
use alloy::rpc::types::{AccessList, EIP1186AccountProofResponse, FeeHistory, Filter, Log};
use alloy::rpc::types::{
AccessList, BlockId, EIP1186AccountProofResponse, FeeHistory, Filter, Log,
};
use async_trait::async_trait;
use eyre::{eyre, Result};

Expand All @@ -26,7 +28,7 @@ impl<N: NetworkSpec> ExecutionRpc<N> for MockRpc {
&self,
_address: Address,
_slots: &[B256],
_block: u64,
_block: BlockId,
) -> Result<EIP1186AccountProofResponse> {
let proof = read_to_string(self.path.join("proof.json"))?;
Ok(serde_json::from_str(&proof)?)
Expand Down
6 changes: 4 additions & 2 deletions core/src/execution/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use alloy::primitives::{Address, B256, U256};
use alloy::rpc::types::{AccessList, EIP1186AccountProofResponse, FeeHistory, Filter, Log};
use alloy::rpc::types::{
AccessList, BlockId, EIP1186AccountProofResponse, FeeHistory, Filter, Log,
};
use async_trait::async_trait;
use eyre::Result;

Expand All @@ -20,7 +22,7 @@ pub trait ExecutionRpc<N: NetworkSpec>: Send + Clone + Sync + 'static {
&self,
address: Address,
slots: &[B256],
block: u64,
block: BlockId,
) -> Result<EIP1186AccountProofResponse>;

async fn create_access_list(
Expand Down
14 changes: 13 additions & 1 deletion core/src/execution/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> State<N, R> {
.cloned()
}

pub async fn get_transaction_by_block_and_index(
pub async fn get_transaction_by_block_hash_and_index(
&self,
block_hash: B256,
index: u64,
Expand All @@ -125,6 +125,18 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> State<N, R> {
.cloned()
}

pub async fn get_transaction_by_block_and_index(
&self,
tag: BlockTag,
index: u64,
) -> Option<N::TransactionResponse> {
let block = self.get_block(tag).await?;
match &block.transactions {
Transactions::Full(txs) => txs.get(index as usize).cloned(),
Transactions::Hashes(_) => unreachable!(),
}
}

// block field fetch

pub async fn get_state_root(&self, tag: BlockTag) -> Option<B256> {
Expand Down
3 changes: 1 addition & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pub mod client;
pub mod consensus;
pub mod errors;
pub mod execution;
pub mod network_spec;
pub mod time;
pub mod types;

mod execution;
1 change: 1 addition & 0 deletions core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Block<T: TransactionResponse> {
pub size: U64,
pub state_root: B256,
pub timestamp: U64,
#[serde(default)]
pub total_difficulty: U64,
pub transactions: Transactions<T>,
pub transactions_root: B256,
Expand Down
21 changes: 21 additions & 0 deletions ethereum/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,24 @@ impl Config {
}
}
}

impl From<BaseConfig> for Config {
fn from(base: BaseConfig) -> Self {
Config {
rpc_bind_ip: Some(base.rpc_bind_ip),
rpc_port: Some(base.rpc_port),
consensus_rpc: base.consensus_rpc.unwrap_or_default(),
execution_rpc: String::new(),
checkpoint: None,
default_checkpoint: base.default_checkpoint,
chain: base.chain,
forks: base.forks,
max_checkpoint_age: base.max_checkpoint_age,
data_dir: base.data_dir,
fallback: None,
load_external_fallback: base.load_external_fallback,
strict_checkpoint_age: base.strict_checkpoint_age,
database_type: None,
}
}
}
Loading

0 comments on commit aedd853

Please sign in to comment.