Skip to content

Commit

Permalink
cleanup and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Apr 24, 2023
1 parent 8d02aab commit 1564d9b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 159 deletions.
146 changes: 6 additions & 140 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ hex = "0.4.3"
libflate = "1.2.0"
openssl = { version = "0.10", features = ["vendored"] }
once_cell = "1"
jsonrpsee = {version = "0.17.0", features = ["server", "http-client", "ws-client", "macros", "client-ws-transport-native-tls"]}
jsonrpsee = {version = "0.17.0", features = ["server", "macros"]}

# Logging and Metrics
chrono = "0.4.22"
Expand Down
8 changes: 4 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ pub struct ChainConfig {
/// Network blocktime
#[serde(default = "default_blocktime")]
pub blocktime: u64,
/// L2 To L1 Message parser address
pub l2_to_l1_message_parser_address: Address,
/// L2 To L1 Message passer address
pub l2_to_l1_message_passer: Address,
}

/// Optimism system config contract values
Expand Down Expand Up @@ -212,7 +212,7 @@ impl ChainConfig {
system_config_contract: addr("0xAe851f927Ee40dE99aaBb7461C00f9622ab91d60"),
batch_inbox: addr("0xff00000000000000000000000000000000000420"),
deposit_contract: addr("0x5b47E1A08Ea6d985D6649300584e6722Ec4B1383"),
l2_to_l1_message_parser_address: addr("0xEF2ec5A5465f075E010BE70966a8667c94BCe15a"),
l2_to_l1_message_passer: addr("0xEF2ec5A5465f075E010BE70966a8667c94BCe15a"),
max_channel_size: 100_000_000,
channel_timeout: 300,
seq_window_size: 3600,
Expand Down Expand Up @@ -244,7 +244,7 @@ impl ChainConfig {
system_config_contract: addr("0xb15eea247ece011c68a614e4a77ad648ff495bc1"),
batch_inbox: addr("0x8453100000000000000000000000000000000000"),
deposit_contract: addr("0xe93c8cd0d409341205a592f8c4ac1a5fe5585cfa"),
l2_to_l1_message_parser_address: addr("0x4200000000000000000000000000000000000016"),
l2_to_l1_message_passer: addr("0x4200000000000000000000000000000000000016"),
max_channel_size: 100_000_000,
channel_timeout: 100,
seq_window_size: 3600,
Expand Down
6 changes: 2 additions & 4 deletions src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl Driver<EngineApi> {

tracing::info!("syncing from: {:?}", finalized_head.hash);

let config_clone = config.clone();
let config = Arc::new(config);
let chain_watcher = ChainWatcher::new(
finalized_epoch.number,
Expand All @@ -83,10 +82,9 @@ impl Driver<EngineApi> {
)));

let engine_driver = EngineDriver::new(finalized_head, finalized_epoch, provider, &config)?;
let pipeline = Pipeline::new(state.clone(), config)?;
let pipeline = Pipeline::new(state.clone(), config.clone())?;

let config_arc = Arc::new(config_clone);
let _addr = rpc::run_server(config_arc).await?;
let _addr = rpc::run_server(config).await?;

Ok(Self {
engine_driver,
Expand Down
28 changes: 18 additions & 10 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{net::SocketAddr, sync::Arc};
use std::{net::SocketAddr, sync::Arc, fmt::Display};

use crate::config::Config;

Expand All @@ -18,7 +18,7 @@ use jsonrpsee::{

use serde::{Deserialize, Serialize};

#[rpc(server, client, namespace = "optimism")]
#[rpc(server, namespace = "optimism")]
pub trait Rpc {
#[method(name = "outputAtBlock")]
async fn output_at_block(&self, block_number: u64) -> Result<OutputRootResponse, Error>;
Expand All @@ -33,28 +33,32 @@ pub struct RpcServerImpl {
impl RpcServer for RpcServerImpl {
async fn output_at_block(&self, block_number: u64) -> Result<OutputRootResponse, Error> {
let l2_provider =
Provider::try_from(self.config.l2_rpc_url.clone()).expect("invalid L2 RPC url");
convert_err(Provider::try_from(self.config.l2_rpc_url.clone()))?;

let block = l2_provider.get_block(block_number).await.unwrap().unwrap();
let block = match convert_err(l2_provider.get_block(block_number).await)? {
Some(block) => block,
None => return Err(Error::Custom("unable to get block".to_string()))
};

let state_root = block.state_root;
let block_hash = block.hash.unwrap();
let block_hash = match block.hash {
Some(hash) => hash,
None => return Err(Error::Custom("block hash not found".to_string()))
};
let locations = vec![];
let block_id = Some(BlockId::from(block_hash));

let state_proof = l2_provider
let state_proof = convert_err(l2_provider
.get_proof(
self.config.chain.l2_to_l1_message_parser_address,
self.config.chain.l2_to_l1_message_passer,
locations,
block_id,
)
.await
.unwrap();
.await)?;

let withdrawal_storage_root = state_proof.storage_hash;

let output_root = compute_l2_output_root(block, state_proof.storage_hash);
// TODO: Verify proof like this - https://github.com/ethereum-optimism/optimism/blob/b65152ca11d7d4c2f23156af8a03339b6798c04d/op-node/node/api.go#L111

let version: H256 = Default::default();

Expand All @@ -67,6 +71,10 @@ impl RpcServer for RpcServerImpl {
}
}

fn convert_err<T, E: Display>(res: Result<T, E>) -> Result<T, Error> {
res.map_err(|err| Error::Custom(err.to_string()))
}

fn compute_l2_output_root(block: Block<H256>, storage_root: H256) -> H256 {
let version: H256 = Default::default();
let digest = keccak256(
Expand Down

0 comments on commit 1564d9b

Please sign in to comment.