Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
remove arc and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
frisitano committed Jun 17, 2024
1 parent b1b33c3 commit e4baf7e
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 243 deletions.
52 changes: 25 additions & 27 deletions rpc/src/compat.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
pub trait ToPrimitive<Out> {
fn to_primitive(self) -> Out;
pub trait Compat<Out> {
fn compat(self) -> Out;
}

impl ToPrimitive<primitive_types::H160> for alloy::primitives::Address {
fn to_primitive(self) -> primitive_types::H160 {
impl Compat<__compat_primitive_types::H160> for alloy::primitives::Address {
fn compat(self) -> __compat_primitive_types::H160 {
let alloy::primitives::Address(alloy::primitives::FixedBytes(arr)) = self;
primitive_types::H160(arr)
__compat_primitive_types::H160(arr)
}
}

impl ToPrimitive<primitive_types::H256> for alloy::primitives::B256 {
fn to_primitive(self) -> primitive_types::H256 {
impl Compat<__compat_primitive_types::H256> for alloy::primitives::B256 {
fn compat(self) -> __compat_primitive_types::H256 {
let alloy::primitives::FixedBytes(arr) = self;
primitive_types::H256(arr)
__compat_primitive_types::H256(arr)
}
}

impl ToPrimitive<[primitive_types::U256; 8]> for alloy::primitives::Bloom {
fn to_primitive(self) -> [primitive_types::U256; 8] {
impl Compat<[__compat_primitive_types::U256; 8]> for alloy::primitives::Bloom {
fn compat(self) -> [__compat_primitive_types::U256; 8] {
let alloy::primitives::Bloom(alloy::primitives::FixedBytes(src)) = self;
// have u8 * 256
// want U256 * 8
Expand All @@ -26,44 +26,42 @@ impl ToPrimitive<[primitive_types::U256; 8]> for alloy::primitives::Bloom {
let dst = core::array::from_fn(|_ix| {
// This is a bit spicy because we're going from an uninterpeted array of bytes
// to wide integers, but we trust this `From` impl to do the right thing
primitive_types::U256::from(<[u8; 32]>::try_from(chunks.next().unwrap()).unwrap())
__compat_primitive_types::U256::from(
<[u8; 32]>::try_from(chunks.next().unwrap()).unwrap(),
)
});
assert_eq!(chunks.len(), 0);
dst
}
}

impl ToPrimitive<primitive_types::U256> for alloy::primitives::U256 {
fn to_primitive(self) -> primitive_types::U256 {
primitive_types::U256(self.into_limbs())
impl Compat<__compat_primitive_types::U256> for alloy::primitives::U256 {
fn compat(self) -> __compat_primitive_types::U256 {
__compat_primitive_types::U256(self.into_limbs())
}
}

impl ToPrimitive<Vec<Vec<u8>>> for Vec<alloy::primitives::Bytes> {
fn to_primitive(self) -> Vec<Vec<u8>> {
impl Compat<Vec<Vec<u8>>> for Vec<alloy::primitives::Bytes> {
fn compat(self) -> Vec<Vec<u8>> {
self.into_iter().map(|x| x.to_vec()).collect()
}
}

pub trait ToAlloy<Out> {
fn to_alloy(self) -> Out;
}

impl ToAlloy<alloy::primitives::Address> for primitive_types::H160 {
fn to_alloy(self) -> alloy::primitives::Address {
let primitive_types::H160(arr) = self;
impl Compat<alloy::primitives::Address> for __compat_primitive_types::H160 {
fn compat(self) -> alloy::primitives::Address {
let __compat_primitive_types::H160(arr) = self;
alloy::primitives::Address(alloy::primitives::FixedBytes(arr))
}
}

impl ToAlloy<alloy::primitives::StorageKey> for primitive_types::H256 {
fn to_alloy(self) -> alloy::primitives::StorageKey {
let primitive_types::H256(arr) = self;
impl Compat<alloy::primitives::StorageKey> for __compat_primitive_types::H256 {
fn compat(self) -> alloy::primitives::StorageKey {
let __compat_primitive_types::H256(arr) = self;
alloy::primitives::FixedBytes(arr)
}
}

#[test]
fn bloom() {
let _did_not_panic = alloy::primitives::Bloom::ZERO.to_primitive();
let _did_not_panic = alloy::primitives::Bloom::ZERO.compat();
}
2 changes: 1 addition & 1 deletion rpc/src/jerigon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Deserialize;
use serde_json::json;
use trace_decoder::trace_protocol::{BlockTrace, BlockTraceTriePreImages, TxnInfo};

use super::metadata::fetch_other_block_data;
use super::fetch_other_block_data;

#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
Expand Down
109 changes: 0 additions & 109 deletions rpc/src/metadata.rs

This file was deleted.

34 changes: 0 additions & 34 deletions rpc/src/native/block.rs

This file was deleted.

39 changes: 34 additions & 5 deletions rpc/src/native/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use std::{collections::HashMap, sync::Arc};

use alloy::{providers::Provider, rpc::types::eth::BlockId, transports::Transport};
use alloy::{
providers::Provider,
rpc::types::eth::{BlockId, BlockTransactionsKind},
transports::Transport,
};
use anyhow::Context as _;
use futures::try_join;
use prover::ProverInput;
use trace_decoder::trace_protocol::BlockTrace;

mod block;
mod state;
mod txn;

type CodeDb = HashMap<primitive_types::H256, Vec<u8>>;
type CodeDb = HashMap<__compat_primitive_types::H256, Vec<u8>>;

/// Fetches the prover input for the given BlockId.
pub async fn prover_input<ProviderT, TransportT>(
Expand All @@ -21,12 +26,36 @@ where
TransportT: Transport + Clone,
{
let (block_trace, other_data) = try_join!(
block::process_block_trace(provider.clone(), block_number),
crate::metadata::fetch_other_block_data(provider, block_number, checkpoint_block_number,)
process_block_trace(&provider, block_number),
crate::fetch_other_block_data(&provider, block_number, checkpoint_block_number,)
)?;

Ok(ProverInput {
block_trace,
other_data,
})
}

/// Processes the block with the given block number and returns the block trace.
async fn process_block_trace<ProviderT, TransportT>(
provider: &ProviderT,
block_number: BlockId,
) -> anyhow::Result<BlockTrace>
where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
{
let block = provider
.get_block(block_number, BlockTransactionsKind::Full)
.await?
.context("target block does not exist")?;

let (code_db, txn_info) = txn::process_transactions(&block, provider).await?;
let trie_pre_images = state::process_state_witness(provider, block, &txn_info).await?;

Ok(BlockTrace {
txn_info,
code_db: Option::from(code_db).filter(|x| !x.is_empty()),
trie_pre_images,
})
}
Loading

0 comments on commit e4baf7e

Please sign in to comment.