Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Force telos state #59

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
34b2985
Add tests to trigger panics on compare_state_diffs function
guilledk Oct 15, 2024
f81ec7f
Fix test suite HashMap::default and finish clean impl of MockDBError …
guilledk Oct 15, 2024
1b59f02
Add state override mechanism for most cases, need to finish storage
guilledk Oct 16, 2024
7db24f0
Add code override based on original_size and separate override code test
guilledk Oct 16, 2024
3162f0d
Changes to make all tests pass except the one commented out due to hi…
guilledk Oct 16, 2024
4c8af52
Bring amirs rebase fixes in execute.rs
guilledk Oct 16, 2024
a0bde85
Reverting unnecesary changes
guilledk Oct 17, 2024
c5e4871
Load account into cache before we attempt to read/write it's storage
poplexity Oct 17, 2024
c450dc5
More strict on cache loading so we don't think we are writing to stor…
poplexity Oct 17, 2024
9c3c2ca
Disambiguate error messages and fix Option/Result for cached account …
poplexity Oct 17, 2024
8621882
If the account is not in reth_db then set status to Created|Touched
poplexity Oct 18, 2024
7140959
Adding the start of a storage compare script
poplexity Oct 19, 2024
4cd6a5c
Starting of compare_account
poplexity Oct 19, 2024
bc434d9
A little more on compare_account function
poplexity Oct 20, 2024
e93c822
Almost working storage_compare
poplexity Oct 21, 2024
e70287b
Fix for Packer trait mismatch with dependency versions, still not wor…
poplexity Oct 21, 2024
a1150f0
Bumping version of antelope-rs
poplexity Oct 21, 2024
e389b48
Moving to use latest master of antelope-client
poplexity Oct 21, 2024
9fd65d2
Refactor and fix for storage comparision logic
aamirpashaa Oct 22, 2024
c1640ac
Modifications to storage_compare
poplexity Oct 22, 2024
05e5941
Cleanup compare function
poplexity Oct 23, 2024
fe9843c
Add block number to print output
poplexity Oct 23, 2024
766632f
Set correct original_value while overriding storage
aamirpashaa Oct 23, 2024
9fd0003
Refactor code compraison by size
guilledk Oct 25, 2024
ced4d2d
Add back in search by code hash for case where we hit the de-duplicator
guilledk Oct 25, 2024
ae4a3b7
Add two-way storage compare
aamirpashaa Nov 7, 2024
ebfd5f4
Dont call compare function due to bugs on it and new two-way-compare …
guilledk Nov 7, 2024
ca62b6a
Fix getTransactionByhash
poplexity Nov 12, 2024
2e91fe1
Add more checks to two-way storage compare
aamirpashaa Nov 13, 2024
200948c
Fill missing telos args
aamirpashaa Nov 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 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 @@ -601,7 +601,7 @@ reth-node-telos = { path = "crates/telos/node" }
reth-telos-rpc = { path = "crates/telos/rpc" }
reth-telos-primitives-traits = { path = "crates/telos/primitives-traits" }
reth-telos-rpc-engine-api = { path = "crates/telos/rpc-engine-api" }
antelope-client = { git = "https://github.com/telosnetwork/antelope-rs", branch = "development" }
antelope-client = { git = "https://github.com/telosnetwork/antelope-rs", branch = "master" }

[patch.crates-io]
#alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "8c499409"}
Expand Down
1 change: 1 addition & 0 deletions crates/ethereum/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ reth-revm.workspace = true
reth-ethereum-consensus.workspace = true
reth-prune-types.workspace = true
reth-execution-types.workspace = true
sha2.workspace = true

# Ethereum
revm-primitives.workspace = true
Expand Down
91 changes: 73 additions & 18 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ use reth_telos_rpc_engine_api::compare::compare_state_diffs;
use revm_primitives::{Address, Account, AccountInfo, AccountStatus, Bytecode, HashMap, KECCAK_EMPTY};
#[cfg(feature = "telos")]
use alloy_primitives::B256;
#[cfg(feature = "telos")]
use sha2::{Sha256, Digest};

/// Provides executors to execute regular ethereum blocks
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -192,6 +194,7 @@ where

// execute transactions
let mut cumulative_gas_used = 0;
#[cfg(not(feature = "telos"))]
let mut receipts = Vec::with_capacity(block.body.transactions.len());
for (sender, transaction) in block.transactions_with_sender() {
#[cfg(feature = "telos")]
Expand Down Expand Up @@ -233,6 +236,7 @@ where
// append gas used
cumulative_gas_used += result.gas_used();

#[cfg(not(feature = "telos"))]
// Push transaction changeset and calculate header bloom filter for receipt.
receipts.push(
#[allow(clippy::needless_update)] // side-effect of optimism fields
Expand All @@ -259,22 +263,31 @@ where
new_addresses_using_create_iter.next();
}

#[cfg(feature = "telos")] {
// Perform state diff comparision
let revm_state_diffs = evm.db_mut().transition_state.clone().unwrap_or_default().transitions;
let block_num = block.block.header.number;
println!(
"Compare: block {block_num} {}",
compare_state_diffs(
&mut evm,
revm_state_diffs,
unwrapped_telos_extra_fields.statediffs_account.unwrap_or_default(),
unwrapped_telos_extra_fields.statediffs_accountstate.unwrap_or_default(),
unwrapped_telos_extra_fields.new_addresses_using_create.unwrap_or_default(),
unwrapped_telos_extra_fields.new_addresses_using_openwallet.unwrap_or_default()
)
);
}
// #[cfg(feature = "telos")]
// {
// // Perform state diff comparision
// let revm_state_diffs = evm.db_mut().transition_state.clone().unwrap_or_default().transitions;
// let block_num = block.block.header.number;
// println!(
// "Compare: block {block_num} {}",
// compare_state_diffs(
// &mut evm,
// revm_state_diffs,
// unwrapped_telos_extra_fields.statediffs_account.clone().unwrap_or_default(),
// unwrapped_telos_extra_fields.statediffs_accountstate.clone().unwrap_or_default(),
// unwrapped_telos_extra_fields.new_addresses_using_create.clone().unwrap_or_default(),
// unwrapped_telos_extra_fields.new_addresses_using_openwallet.clone().unwrap_or_default(),
// false
// )
// );
// }

#[cfg(feature = "telos")]
let receipts = if unwrapped_telos_extra_fields.receipts.is_some() {
unwrapped_telos_extra_fields.receipts.clone().unwrap()
} else {
vec![]
};

let requests = if self.chain_spec.is_prague_active_at_timestamp(block.timestamp) {
// Collect all EIP-6110 deposits
Expand All @@ -288,7 +301,44 @@ where
vec![]
};

Ok(EthExecuteOutput { receipts, requests, gas_used: cumulative_gas_used })
// #[cfg(feature = "telos")]
// {
// let mut addr_to_accstate: HashMap<Address, HashMap<U256, EvmStorageSlot>> = HashMap::new();

// for sdiff_accstate in unwrapped_telos_extra_fields.clone().statediffs_accountstate.unwrap_or(vec![]) {
// if !addr_to_accstate.contains_key(&sdiff_accstate.address) {
// addr_to_accstate.insert(sdiff_accstate.address, HashMap::new());
// }
// let mut acc_storage = addr_to_accstate.get_mut(&sdiff_accstate.address).unwrap();
// acc_storage.insert(sdiff_accstate.key, EvmStorageSlot { original_value: Default::default(), present_value: sdiff_accstate.value, is_cold: false });
// }

// let mut state: HashMap<Address, Account> = HashMap::new();

// for sdiff_acc in unwrapped_telos_extra_fields.clone().statediffs_account.unwrap_or(vec![]) {
// state.insert(
// sdiff_acc.address,
// Account {
// info: AccountInfo {
// balance: sdiff_acc.balance,
// nonce: sdiff_acc.nonce,
// code_hash: B256::from(Sha256::digest(sdiff_acc.code.as_ref()).as_ref()),
// code: Some(Bytecode::LegacyRaw(sdiff_acc.code)),
// },
// storage: addr_to_accstate.get(&sdiff_acc.address).unwrap_or(&HashMap::new()).clone(),
// status: AccountStatus::Touched | AccountStatus::LoadedAsNotExisting,
// }
// );
// }

// evm.db_mut().commit(state);
// }

Ok(EthExecuteOutput {
receipts,
requests,
gas_used: cumulative_gas_used
})
}
}

Expand Down Expand Up @@ -366,7 +416,12 @@ where
let env = self.evm_env_for_block(&block.header, total_difficulty);
let output = {
let evm = self.executor.evm_config.evm_with_env(&mut self.state, env);
self.executor.execute_state_transitions(block, evm, #[cfg(feature = "telos")] telos_extra_fields)
self.executor.execute_state_transitions(
block,
evm,
#[cfg(feature = "telos")]
telos_extra_fields
)
}?;

// 3. apply post execution changes
Expand Down
2 changes: 2 additions & 0 deletions crates/telos/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ reth-chainspec.workspace = true
reth-provider.workspace = true
reth-node-telos.workspace = true
reth-telos-rpc.workspace = true
reth-db.workspace = true
alloy-primitives.workspace = true


clap = { workspace = true, features = ["derive", "env"] }
Expand Down
41 changes: 41 additions & 0 deletions crates/telos/bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
static ALLOC: reth_cli_util::allocator::Allocator = reth_cli_util::allocator::new_allocator();

use clap::Parser;
use tracing::{error, info, warn};
use reth::args::utils::EthereumChainSpecParser;
use reth_node_builder::{engine_tree_config::TreeConfig, EngineNodeLauncher};
use reth::cli::Cli;
use reth_node_telos::{TelosArgs, TelosNode};
use reth_node_telos::node::TelosAddOns;
use reth_provider::providers::BlockchainProvider2;
use reth_telos_rpc::TelosClient;
use reth::primitives::BlockId;
use reth::rpc::types::BlockNumberOrTag;
use reth_provider::{DatabaseProviderFactory, StateProviderFactory};
use reth_db::{PlainAccountState, PlainStorageState};


#[cfg(feature = "telos")]
fn main() {
use reth_provider::BlockNumReader;

reth_cli_util::sigsegv_handler::install();

// Enable backtraces unless a RUST_BACKTRACE value has already been explicitly provided.
Expand Down Expand Up @@ -45,6 +52,10 @@ fn main() {
handle.node_exit_future.await
},
false => {
let two_way_storage_compare = telos_args.two_way_storage_compare.clone();
let telos_rpc = telos_args.telos_endpoint.clone();
let block_delta = telos_args.block_delta.clone();

let handle = builder
.node(TelosNode::new(telos_args.clone()))
.extend_rpc_modules(move |ctx| {
Expand All @@ -59,6 +70,36 @@ fn main() {
.launch()
.await?;

match two_way_storage_compare {
true => {
if telos_rpc.is_none() {
warn!("Telos RPC Endpoint is not specified, skipping two-way storage compare");
} else if block_delta.is_none() {
warn!("Block delta is not specified, skipping two-way storage compare");
} else {
info!("Fetching account and accountstate from Telos native RPC (Can take a long time)...");

let (account_table, accountstate_table, block_number) = reth_node_telos::two_way_storage_compare::get_telos_tables(telos_rpc.unwrap().as_str(), block_delta.unwrap()).await;

if block_number.as_u64().unwrap() <= handle.node.provider.best_block_number().unwrap() {
info!("Two-way comparing state (Reth vs. Telos) at height: {:?}", block_number);

let state_at_specific_height = handle.node.provider.state_by_block_id(BlockId::Number(BlockNumberOrTag::Number(block_number.as_u64().unwrap()))).unwrap();
let plain_account_state = handle.node.provider.database_provider_ro().unwrap().table::<PlainAccountState>().unwrap();
let plain_storage_state = handle.node.provider.database_provider_ro().unwrap().table::<PlainStorageState>().unwrap();

let match_counter = reth_node_telos::two_way_storage_compare::two_side_state_compare(account_table, accountstate_table, state_at_specific_height, plain_account_state, plain_storage_state).await;
match_counter.print();

info!("Comparing done");
} else {
error!("Nodeos is ahead of reth, failed to compare state");
}
}
}
_ => {}
}

handle.node_exit_future.await
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/telos/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ reth-stages.workspace = true
reth-telos-rpc.workspace = true
reth-tracing.workspace = true
reth-transaction-pool.workspace = true
reth-telos-rpc-engine-api.workspace = true
alloy-primitives.workspace = true
reth-db.workspace = true

clap.workspace = true
serde = { workspace = true, features = ["derive"] }
Expand Down
8 changes: 8 additions & 0 deletions crates/telos/node/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ pub struct TelosArgs {
/// a batch of downloaded blocks.
#[arg(long = "engine.max-execute-block-batch-size", requires = "experimental", default_value_t = DEFAULT_MAX_EXECUTE_BLOCK_BATCH_SIZE)]
pub max_execute_block_batch_size: usize,

/// Enable Two-way storage compare between reth and telos
#[arg(long = "telos.two_way_storage_compare", default_value = "false")]
pub two_way_storage_compare: bool,

/// Block delta between native and EVM
#[arg(long = "telos.block_delta")]
pub block_delta: Option<u32>,
}

impl From<TelosArgs> for TelosClientArgs {
Expand Down
1 change: 1 addition & 0 deletions crates/telos/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

pub mod args;
pub mod node;
pub mod two_way_storage_compare;

pub use crate::args::TelosArgs;
pub use crate::node::TelosNode;
Expand Down
Loading
Loading