Skip to content

Commit

Permalink
feat: use objects from portal-verkle-primitives and delete duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
morph-dev committed Jul 31, 2024
1 parent d39ae95 commit 747781b
Show file tree
Hide file tree
Showing 23 changed files with 170 additions and 1,065 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions portal-verkle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ anyhow = "1"
clap = { version = "4", features = ["derive"] }
derive_more = "0.99"
ethportal-api = { git = "https://github.com/morph-dev/trin.git", rev = "d1b4f4cbf8ac1d93753a7fa39e08ecd70139bb3f" }
itertools = "0.13"
jsonrpsee = { version = "0.20", features = ["async-client", "client"] }
portal-verkle-primitives = { git = "https://github.com/morph-dev/portal-verkle-primitives.git", rev = "65ce73011fd2636733271219057c9dc194800ded" }
reqwest = { version = "0.12", features = ["json"] }
Expand Down
2 changes: 1 addition & 1 deletion portal-verkle/src/beacon_block_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use reqwest::{Client, Url};
use serde::Deserialize;

use crate::{
paths::beacon_slot_path,
types::{beacon::SignedBeaconBlock, JsonResponseMessage},
utils::beacon_slot_path,
};

const BEACON_BLOCK_URL_PATH: &str = "eth/v2/beacon/blocks/";
Expand Down
2 changes: 1 addition & 1 deletion portal-verkle/src/bin/fetch_state_from_portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use anyhow::bail;
use clap::Parser;
use portal_verkle::{
beacon_block_fetcher::BeaconBlockFetcher, state_trie_fetcher::StateTrieFetcher,
types::genesis::GenesisConfig,
};
use portal_verkle_primitives::verkle::genesis_config::GenesisConfig;

const LOCALHOST_BEACON_RPC_URL: &str = "http://localhost:9596/";
const LOCALHOST_PORTAL_RPC_URL: &str = "http://localhost:8545/";
Expand Down
134 changes: 66 additions & 68 deletions portal-verkle/src/bin/gossip_to_portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ use std::{

use alloy_primitives::B256;
use clap::Parser;
use ethportal_api::{
types::content_key::verkle::LeafFragmentKey, VerkleContentKey, VerkleContentValue,
VerkleNetworkApiClient,
};
use ethportal_api::{VerkleContentKey, VerkleContentValue, VerkleNetworkApiClient};
use jsonrpsee::http_client::{HttpClient, HttpClientBuilder};
use portal_verkle::{
beacon_block_fetcher::BeaconBlockFetcher,
evm::VerkleEvm,
types::{genesis::GenesisConfig, state_write::StateWrites},
utils::read_genesis,
beacon_block_fetcher::BeaconBlockFetcher, evm::VerkleEvm, utils::read_genesis,
};
use portal_verkle_primitives::{
ssz::TriePath,
verkle::{genesis_config::GenesisConfig, StateWrites},
};
use portal_verkle_primitives::{constants::PORTAL_NETWORK_NODE_WIDTH, ssz::TriePath};

const LOCALHOST_BEACON_RPC_URL: &str = "http://localhost:9596/";
const LOCALHOST_PORTAL_RPC_URL: &str = "http://localhost:8545/";
Expand Down Expand Up @@ -45,7 +42,7 @@ impl Gossiper {
let portal_client = HttpClientBuilder::new()
.request_timeout(Duration::from_secs(60))
.build(&args.portal_rpc_url)?;
let evm = VerkleEvm::new(&read_genesis()?)?;
let evm = VerkleEvm::new(read_genesis()?)?;

Ok(Self {
block_fetcher,
Expand All @@ -55,8 +52,7 @@ impl Gossiper {
}

async fn gossip_genesis(&mut self) -> anyhow::Result<()> {
let genesis_config = read_genesis()?;
let state_writes = genesis_config.generate_state_diff().into();
let state_writes = read_genesis()?.into_state_writes();
println!("Gossiping genesis...");
self.gossip_state_writes(
GenesisConfig::DEVNET6_BLOCK_HASH,
Expand Down Expand Up @@ -99,11 +95,11 @@ impl Gossiper {
for stem_state_write in state_writes.iter() {
let stem = &stem_state_write.stem;
let path_to_leaf = self.evm.state_trie().traverse_to_leaf(stem)?;
for (depth, branch) in path_to_leaf.branches.iter().enumerate() {
for (depth, (branch, _)) in path_to_leaf.trie_path.iter().enumerate() {
let trie_path = TriePath::from(stem[..depth].to_vec());

// Branch bundle
let content_key = VerkleContentKey::Bundle(branch.commitment().clone());
let content_key = VerkleContentKey::Bundle(branch.commitment().to_point());
if new_branch_nodes.contains(&trie_path)
&& content_to_gossip.contains_key(&content_key)
{
Expand All @@ -123,39 +119,40 @@ impl Gossiper {
todo!()
});

let fragment_indices = if new_branch_nodes.contains(&trie_path) {
0..PORTAL_NETWORK_NODE_WIDTH
} else {
let fragment_index = stem[depth] as usize / PORTAL_NETWORK_NODE_WIDTH;
fragment_index..fragment_index + 1
};
// let fragment_indices = if new_branch_nodes.contains(&trie_path) {
// 0..PORTAL_NETWORK_NODE_WIDTH
// } else {
// let fragment_index = stem[depth] as usize / PORTAL_NETWORK_NODE_WIDTH;
// fragment_index..fragment_index + 1
// };
// Branch fragment
for fragment_index in fragment_indices {
let (fragment_commitment, _fragment) =
branch.extract_fragment_node(fragment_index);
if fragment_commitment.is_zero() {
continue;
}
let content_key = VerkleContentKey::BranchFragment(fragment_commitment);
content_to_gossip.entry(content_key).or_insert_with(|| {
// VerkleContentValue::NodeWithProof(
// PortalVerkleNodeWithProof::BranchFragment(
// BranchFragmentNodeWithProof {
// node: fragment,
// block_hash,
// path: trie_path.clone(),
// proof: trie_proof,
// },
// ),
// )
todo!()
});
}
// for fragment_index in fragment_indices {
// let (fragment_commitment, _fragment) =
// branch.extract_fragment_node(fragment_index);
// if fragment_commitment.is_zero() {
// continue;
// }
// let content_key = VerkleContentKey::BranchFragment(fragment_commitment);
// content_to_gossip.entry(content_key).or_insert_with(|| {
// // VerkleContentValue::NodeWithProof(
// // PortalVerkleNodeWithProof::BranchFragment(
// // BranchFragmentNodeWithProof {
// // node: fragment,
// // block_hash,
// // path: trie_path.clone(),
// // proof: trie_proof,
// // },
// // ),
// // )
// todo!()
// });
// }
todo!()
}

// Leaf bundle
let bundle_commitment = path_to_leaf.leaf.commitment();
let content_key = VerkleContentKey::Bundle(bundle_commitment.clone());
let content_key = VerkleContentKey::Bundle(bundle_commitment.to_point());
content_to_gossip.entry(content_key).or_insert_with(|| {
// VerkleContentValue::NodeWithProof(PortalVerkleNodeWithProof::LeafBundle(
// LeafBundleNodeWithProof {
Expand All @@ -168,32 +165,33 @@ impl Gossiper {
});

// Leaf Fragments
let mut modified_fragments = stem_state_write
.suffix_writes
.iter()
.map(|suffix_write| suffix_write.suffix as usize / PORTAL_NETWORK_NODE_WIDTH)
.collect::<Vec<_>>();
modified_fragments.sort();
modified_fragments.dedup();
for fragment_index in modified_fragments {
let (fragment_commitment, _fragment_node) =
path_to_leaf.leaf.extract_fragment_node(fragment_index);
let content_key = VerkleContentKey::LeafFragment(LeafFragmentKey {
stem: *stem,
commitment: fragment_commitment,
});

content_to_gossip.entry(content_key).or_insert_with(|| {
// VerkleContentValue::NodeWithProof(PortalVerkleNodeWithProof::LeafFragment(
// LeafFragmentNodeWithProof {
// node: fragment_node,
// block_hash,
// proof: trie_proof,
// },
// ))
todo!()
});
}
// let mut modified_fragments = stem_state_write
// .writes
// .iter()
// .map(|(suffix, _value)| *suffix as usize / PORTAL_NETWORK_NODE_WIDTH)
// .collect::<Vec<_>>();
// modified_fragments.sort();
// modified_fragments.dedup();
// for fragment_index in modified_fragments {
// let (fragment_commitment, _fragment_node) =
// path_to_leaf.leaf.extract_fragment_node(fragment_index);
// let content_key = VerkleContentKey::LeafFragment(LeafFragmentKey {
// stem: *stem,
// commitment: fragment_commitment,
// });

// content_to_gossip.entry(content_key).or_insert_with(|| {
// // VerkleContentValue::NodeWithProof(PortalVerkleNodeWithProof::LeafFragment(
// // LeafFragmentNodeWithProof {
// // node: fragment_node,
// // block_hash,
// // proof: trie_proof,
// // },
// // ))
// todo!()
// });
// }
todo!()
}

for (key, value) in content_to_gossip {
Expand Down
3 changes: 1 addition & 2 deletions portal-verkle/src/evm/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use alloy_primitives::B256;
use portal_verkle_primitives::verkle::error::VerkleTrieError;
use thiserror::Error;

use crate::verkle_trie::error::VerkleTrieError;

#[derive(Debug, Error)]
pub enum EvmError {
#[error("Expected block {expected}, but received {actual}")]
Expand Down
58 changes: 32 additions & 26 deletions portal-verkle/src/evm/verkle_evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ use alloy_primitives::{address, keccak256, U8};
use portal_verkle_primitives::{
constants::{BALANCE_LEAF_KEY, CODE_KECCAK_LEAF_KEY, NONCE_LEAF_KEY, VERSION_LEAF_KEY},
ssz::TriePath,
verkle::storage::AccountStorageLayout,
verkle::{
genesis_config::GenesisConfig, storage::AccountStorageLayout, StateWrites, VerkleTrie,
},
TrieValue,
};

use super::error::EvmError;
use crate::{
types::{
beacon::ExecutionPayload,
genesis::GenesisConfig,
state_write::StateWrites,
witness::{StateDiff, SuffixStateDiff},
},
verkle_trie::VerkleTrie,
use crate::types::{
beacon::ExecutionPayload,
witness::{StateDiff, StemStateDiff, SuffixStateDiff},
};

pub struct VerkleEvm {
Expand All @@ -30,11 +27,9 @@ pub struct ProcessBlockResult {
}

impl VerkleEvm {
pub fn new(genesis_config: &GenesisConfig) -> Result<Self, EvmError> {
pub fn new(genesis_config: GenesisConfig) -> Result<Self, EvmError> {
let mut state_trie = VerkleTrie::new();
state_trie
.update(&genesis_config.generate_state_diff().into())
.map_err(EvmError::TrieError)?;
state_trie.update(&genesis_config.into_state_writes());
Ok(Self {
block: 0,
state_trie,
Expand Down Expand Up @@ -66,12 +61,14 @@ impl VerkleEvm {
update_state_diff_for_eip2935(&mut state_diff);
}

let state_writes = StateWrites::from(state_diff);
let state_writes = StateWrites::new(
state_diff
.into_iter()
.filter_map(StemStateDiff::into_stem_state_write)
.collect(),
);

let new_branch_nodes = self
.state_trie
.update(&state_writes)
.map_err(EvmError::TrieError)?;
let new_branch_nodes = self.state_trie.update(&state_writes);
self.block += 1;

if self.state_trie.root() != execution_payload.state_root {
Expand Down Expand Up @@ -115,15 +112,18 @@ fn update_state_diff_for_eip2935(state_diff: &mut StateDiff) {

#[cfg(test)]
mod tests {
use std::{fs::File, io::BufReader};
use std::{
fs::File,
io::{stdout, BufReader},
};

use alloy_primitives::{b256, B256};
use anyhow::Result;
use anyhow::{bail, Result};
use portal_verkle_primitives::verkle::trie_printer::TriePrinter;

use crate::{
paths::{beacon_slot_path, test_path},
types::SuccessMessage,
utils::read_genesis_for_test,
utils::{beacon_slot_path, read_genesis_for_test, test_path},
};

use super::*;
Expand All @@ -133,15 +133,15 @@ mod tests {
const STATE_ROOT: B256 =
b256!("1fbf85345a3cbba9a6d44f991b721e55620a22397c2a93ee8d5011136ac300ee");

let evm = VerkleEvm::new(&read_genesis_for_test()?)?;
let evm = VerkleEvm::new(read_genesis_for_test()?)?;

assert_eq!(evm.state_trie.root(), STATE_ROOT);
Ok(())
}

#[test]
fn process_block_1() -> Result<()> {
let mut evm = VerkleEvm::new(&read_genesis_for_test()?)?;
let mut evm = VerkleEvm::new(read_genesis_for_test()?)?;

let reader = BufReader::new(File::open(test_path(beacon_slot_path(1)))?);
let response: SuccessMessage = serde_json::from_reader(reader)?;
Expand All @@ -153,7 +153,7 @@ mod tests {

#[test]
fn process_block_1000() -> Result<()> {
let mut evm = VerkleEvm::new(&read_genesis_for_test()?)?;
let mut evm = VerkleEvm::new(read_genesis_for_test()?)?;

for block in 1..=1000 {
let path = test_path(beacon_slot_path(block));
Expand All @@ -163,7 +163,13 @@ mod tests {
let reader = BufReader::new(File::open(path)?);
let response: SuccessMessage = serde_json::from_reader(reader)?;
let execution_payload = response.data.message.body.execution_payload;
evm.process_block(&execution_payload)?;
if let Err(err) = evm.process_block(&execution_payload) {
println!("Failed at block {block}");
evm.state_trie.print_state(&mut stdout())?;
evm.state_trie
.print_trie_with_identation(&mut stdout(), 0)?;
bail!(err);
}
}
Ok(())
}
Expand Down
2 changes: 0 additions & 2 deletions portal-verkle/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
pub mod beacon_block_fetcher;
pub mod evm;
pub mod paths;
pub mod state_trie_fetcher;
pub mod types;
pub mod utils;
pub mod verkle_trie;
16 changes: 0 additions & 16 deletions portal-verkle/src/paths.rs

This file was deleted.

Loading

0 comments on commit 747781b

Please sign in to comment.