Skip to content

Commit

Permalink
Merge branch 'telos-main' into integration-more-tests
Browse files Browse the repository at this point in the history
# Conflicts:
#	Cargo.lock
#	Cargo.toml
  • Loading branch information
lesa-telos committed Dec 9, 2024
2 parents 1b21d2d + c817fcb commit 3b9b9df
Show file tree
Hide file tree
Showing 24 changed files with 2,241 additions and 110 deletions.
675 changes: 628 additions & 47 deletions Cargo.lock

Large diffs are not rendered by default.

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
89 changes: 72 additions & 17 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,23 +263,32 @@ 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
let deposit_requests =
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
95 changes: 95 additions & 0 deletions crates/rpc/rpc-eth-api/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ where
number: BlockNumberOrTag,
full: bool,
) -> RpcResult<Option<RpcBlock<T::NetworkTypes>>> {
#[cfg(feature = "telos")]
let number = match number {
BlockNumberOrTag::Pending => BlockNumberOrTag::Latest,
_ => number,
};
trace!(target: "rpc::eth", ?number, ?full, "Serving eth_getBlockByNumber");
Ok(EthBlocks::rpc_block(self, number.into(), full).await?)
}
Expand All @@ -440,6 +445,11 @@ where
&self,
number: BlockNumberOrTag,
) -> RpcResult<Option<U256>> {
#[cfg(feature = "telos")]
let number = match number {
BlockNumberOrTag::Pending => BlockNumberOrTag::Latest,
_ => number,
};
trace!(target: "rpc::eth", ?number, "Serving eth_getBlockTransactionCountByNumber");
Ok(EthBlocks::block_transaction_count(self, number.into()).await?.map(U256::from))
}
Expand All @@ -455,6 +465,11 @@ where
&self,
number: BlockNumberOrTag,
) -> RpcResult<Option<U256>> {
#[cfg(feature = "telos")]
let number = match number {
BlockNumberOrTag::Pending => BlockNumberOrTag::Latest,
_ => number,
};
trace!(target: "rpc::eth", ?number, "Serving eth_getUncleCountByBlockNumber");
Ok(EthBlocks::ommers(self, number.into())?.map(|ommers| U256::from(ommers.len())))
}
Expand All @@ -464,6 +479,11 @@ where
&self,
block_id: BlockId,
) -> RpcResult<Option<Vec<RpcReceipt<T::NetworkTypes>>>> {
#[cfg(feature = "telos")]
let block_id = match block_id {
BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest),
_ => block_id,
};
trace!(target: "rpc::eth", ?block_id, "Serving eth_getBlockReceipts");
Ok(EthBlocks::block_receipts(self, block_id).await?)
}
Expand All @@ -484,6 +504,11 @@ where
number: BlockNumberOrTag,
index: Index,
) -> RpcResult<Option<RpcBlock<T::NetworkTypes>>> {
#[cfg(feature = "telos")]
let number = match number {
BlockNumberOrTag::Pending => BlockNumberOrTag::Latest,
_ => number,
};
trace!(target: "rpc::eth", ?number, ?index, "Serving eth_getUncleByBlockNumberAndIndex");
Ok(EthBlocks::ommer_by_block_and_index(self, number.into(), index).await?)
}
Expand Down Expand Up @@ -533,6 +558,11 @@ where
number: BlockNumberOrTag,
index: Index,
) -> RpcResult<Option<Bytes>> {
#[cfg(feature = "telos")]
let number = match number {
BlockNumberOrTag::Pending => BlockNumberOrTag::Latest,
_ => number,
};
trace!(target: "rpc::eth", ?number, ?index, "Serving eth_getRawTransactionByBlockNumberAndIndex");
Ok(EthTransactions::raw_transaction_by_block_and_tx_index(
self,
Expand All @@ -548,6 +578,11 @@ where
number: BlockNumberOrTag,
index: Index,
) -> RpcResult<Option<RpcTransaction<T::NetworkTypes>>> {
#[cfg(feature = "telos")]
let number = match number {
BlockNumberOrTag::Pending => BlockNumberOrTag::Latest,
_ => number,
};
trace!(target: "rpc::eth", ?number, ?index, "Serving eth_getTransactionByBlockNumberAndIndex");
Ok(EthTransactions::transaction_by_block_and_tx_index(self, number.into(), index.into())
.await?)
Expand Down Expand Up @@ -575,6 +610,11 @@ where

/// Handler for: `eth_getBalance`
async fn balance(&self, address: Address, block_number: Option<BlockId>) -> RpcResult<U256> {
#[cfg(feature = "telos")]
let block_number = match block_number {
Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)),
_ => block_number,
};
trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getBalance");
Ok(EthState::balance(self, address, block_number).await?)
}
Expand All @@ -586,6 +626,11 @@ where
index: JsonStorageKey,
block_number: Option<BlockId>,
) -> RpcResult<B256> {
#[cfg(feature = "telos")]
let block_number = match block_number {
Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)),
_ => block_number,
};
trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getStorageAt");
Ok(EthState::storage_at(self, address, index, block_number).await?)
}
Expand All @@ -596,18 +641,33 @@ where
address: Address,
block_number: Option<BlockId>,
) -> RpcResult<U256> {
#[cfg(feature = "telos")]
let block_number = match block_number {
Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)),
_ => block_number,
};
trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getTransactionCount");
Ok(EthState::transaction_count(self, address, block_number).await?)
}

/// Handler for: `eth_getCode`
async fn get_code(&self, address: Address, block_number: Option<BlockId>) -> RpcResult<Bytes> {
#[cfg(feature = "telos")]
let block_number = match block_number {
Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)),
_ => block_number,
};
trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getCode");
Ok(EthState::get_code(self, address, block_number).await?)
}

/// Handler for: `eth_getHeaderByNumber`
async fn header_by_number(&self, block_number: BlockNumberOrTag) -> RpcResult<Option<Header>> {
#[cfg(feature = "telos")]
let block_number: BlockNumberOrTag = match block_number {
BlockNumberOrTag::Pending => BlockNumberOrTag::Latest,
_ => block_number,
};
trace!(target: "rpc::eth", ?block_number, "Serving eth_getHeaderByNumber");
Ok(EthBlocks::rpc_block_header(self, block_number.into()).await?)
}
Expand All @@ -624,6 +684,11 @@ where
payload: SimulatePayload,
block_number: Option<BlockId>,
) -> RpcResult<Vec<SimulatedBlock<RpcBlock<T::NetworkTypes>>>> {
#[cfg(feature = "telos")]
let block_number = match block_number {
Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)),
_ => block_number,
};
trace!(target: "rpc::eth", ?block_number, "Serving eth_simulateV1");
Ok(EthCall::simulate_v1(self, payload, block_number).await?)
}
Expand All @@ -636,6 +701,11 @@ where
state_overrides: Option<StateOverride>,
block_overrides: Option<Box<BlockOverrides>>,
) -> RpcResult<Bytes> {
#[cfg(feature = "telos")]
let block_number = match block_number {
Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)),
_ => block_number,
};
trace!(target: "rpc::eth", ?request, ?block_number, ?state_overrides, ?block_overrides, "Serving eth_call");
Ok(EthCall::call(
self,
Expand Down Expand Up @@ -663,6 +733,11 @@ where
request: TransactionRequest,
block_number: Option<BlockId>,
) -> RpcResult<AccessListResult> {
#[cfg(feature = "telos")]
let block_number = match block_number {
Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)),
_ => block_number,
};
trace!(target: "rpc::eth", ?request, ?block_number, "Serving eth_createAccessList");
Ok(EthCall::create_access_list_at(self, request, block_number).await?)
}
Expand All @@ -674,6 +749,11 @@ where
block_number: Option<BlockId>,
state_override: Option<StateOverride>,
) -> RpcResult<U256> {
#[cfg(feature = "telos")]
let block_number = match block_number {
Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)),
_ => block_number,
};
trace!(target: "rpc::eth", ?request, ?block_number, "Serving eth_estimateGas");
Ok(EthCall::estimate_gas_at(
self,
Expand All @@ -696,6 +776,11 @@ where
address: Address,
block: BlockId,
) -> RpcResult<Option<alloy_rpc_types::Account>> {
#[cfg(feature = "telos")]
let block = match block {
BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest),
_ => block,
};
trace!(target: "rpc::eth", "Serving eth_getAccount");
Ok(EthState::get_account(self, address, block).await?)
}
Expand Down Expand Up @@ -727,6 +812,11 @@ where
newest_block: BlockNumberOrTag,
reward_percentiles: Option<Vec<f64>>,
) -> RpcResult<FeeHistory> {
#[cfg(feature = "telos")]
let newest_block = match newest_block {
BlockNumberOrTag::Pending => BlockNumberOrTag::Latest,
_ => newest_block,
};
trace!(target: "rpc::eth", ?block_count, ?newest_block, ?reward_percentiles, "Serving eth_feeHistory");
Ok(EthFees::fee_history(self, block_count.to(), newest_block, reward_percentiles).await?)
}
Expand Down Expand Up @@ -797,6 +887,11 @@ where
keys: Vec<JsonStorageKey>,
block_number: Option<BlockId>,
) -> RpcResult<EIP1186AccountProofResponse> {
#[cfg(feature = "telos")]
let block_number = match block_number {
Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)),
_ => block_number,
};
trace!(target: "rpc::eth", ?address, ?keys, ?block_number, "Serving eth_getProof");
Ok(EthState::get_proof(self, address, keys, block_number)?.await?)
}
Expand Down
Loading

0 comments on commit 3b9b9df

Please sign in to comment.