Skip to content

Commit

Permalink
receipts + state root
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Sep 14, 2024
1 parent 1df373f commit d0392fe
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
7 changes: 4 additions & 3 deletions crates/rpc/rpc-eth-api/src/helpers/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ pub trait EthCall: Call + LoadPendingBlock {
block_env.number += U256::from(1);
block_env.timestamp += U256::from(1);

if !validation {
block_env.basefee = U256::ZERO;
} else {
if validation {
let chain_spec = LoadPendingBlock::provider(&this).chain_spec();
let base_fee_params =
chain_spec.base_fee_params_at_timestamp(block_env.timestamp.to());
Expand All @@ -135,6 +133,8 @@ pub trait EthCall: Call + LoadPendingBlock {
.unwrap_or_default() as u128
};
block_env.basefee = U256::from(base_fee);
} else {
block_env.basefee = U256::ZERO;
}

let SimBlock { block_overrides, state_overrides, mut calls } = block;
Expand Down Expand Up @@ -195,6 +195,7 @@ pub trait EthCall: Call + LoadPendingBlock {
parent_hash,
total_difficulty,
return_full_transactions,
&db,
)?;

parent_hash = block.inner.header.hash;
Expand Down
62 changes: 55 additions & 7 deletions crates/rpc/rpc-eth-types/src/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
use alloy_consensus::{TxEip4844Variant, TxType, TypedTransaction};
use jsonrpsee_types::ErrorObject;
use reth_primitives::{
BlockWithSenders, Signature, Transaction, TransactionSigned, TransactionSignedNoHash,
logs_bloom,
proofs::{calculate_receipt_root, calculate_transaction_root},
BlockWithSenders, Receipt, Signature, Transaction, TransactionSigned, TransactionSignedNoHash,
};
use reth_revm::database::StateProviderDatabase;
use reth_rpc_server_types::result::rpc_err;
use reth_rpc_types::{
simulate::{SimCallResult, SimulateError, SimulatedBlock},
Block, BlockTransactionsKind, ToRpcError, TransactionRequest, WithOtherFields,
};
use reth_rpc_types_compat::block::from_block;
use revm::Database;
use revm_primitives::{Address, BlockEnv, Bytes, ExecutionResult, TxKind, B256, U256};
use reth_storage_api::StateRootProvider;
use reth_trie::{HashedPostState, HashedStorage};
use revm::{db::CacheDB, Database};
use revm_primitives::{keccak256, Address, BlockEnv, Bytes, ExecutionResult, TxKind, B256, U256};

use crate::{EthApiError, RevertError, RpcInvalidTransactionError};
use crate::{
cache::db::StateProviderTraitObjWrapper, EthApiError, RevertError, RpcInvalidTransactionError,
};

/// Errors which may occur during `eth_simulateV1` execution.
#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -67,7 +74,11 @@ where
return Err(EthApiError::Other(Box::new(EthSimulateError::BlockGasLimitExceeded)))
}

(block_gas_limit - total_specified_gas) / txs_without_gas_limit as u128
if txs_without_gas_limit > 0 {
(block_gas_limit - total_specified_gas) / txs_without_gas_limit as u128
} else {
0
}
};

for tx in txs {
Expand Down Expand Up @@ -164,9 +175,11 @@ pub fn build_block(
parent_hash: B256,
total_difficulty: U256,
full_transactions: bool,
db: &CacheDB<StateProviderDatabase<StateProviderTraitObjWrapper<'_>>>,
) -> Result<SimulatedBlock<Block<WithOtherFields<reth_rpc_types::Transaction>>>, EthApiError> {
let mut calls = Vec::with_capacity(results.len());
let mut calls: Vec<SimCallResult> = Vec::with_capacity(results.len());
let mut senders = Vec::with_capacity(results.len());
let mut receipts = Vec::new();

let mut log_index = 0;
for (transaction_index, ((sender, result), tx)) in
Expand Down Expand Up @@ -224,19 +237,54 @@ pub fn build_block(
},
};

receipts.push(
Receipt {
tx_type: tx.tx_type(),
success: call.status,
cumulative_gas_used: call.gas_used + calls.iter().map(|c| c.gas_used).sum::<u64>(),
logs: call.logs.iter().map(|log| &log.inner).cloned().collect(),
..Default::default()
}
.into(),
);

calls.push(call);
}

let mut hashed_state = HashedPostState::default();
for (address, account) in &db.accounts {
let hashed_address = keccak256(address);
hashed_state.accounts.insert(hashed_address, Some(account.info.clone().into()));

let storage = hashed_state
.storages
.entry(hashed_address)
.or_insert_with(|| HashedStorage::new(account.account_state.is_storage_cleared()));

for (slot, value) in &account.storage {
let slot = B256::from(*slot);
let hashed_slot = keccak256(slot);
storage.storage.insert(hashed_slot, *value);
}
}

let state_root = db.db.0.state_root(hashed_state)?;

let header = reth_primitives::Header {
beneficiary: block_env.coinbase,
difficulty: block_env.difficulty,
number: block_env.number.to(),
timestamp: block_env.timestamp.to(),
base_fee_per_gas: Some(block_env.basefee.to()),
gas_limit: block_env.gas_limit.to(),
gas_used: 0,
gas_used: calls.iter().map(|c| c.gas_used).sum::<u64>(),
blob_gas_used: Some(0),
parent_hash,
receipts_root: calculate_receipt_root(&receipts),
transactions_root: calculate_transaction_root(&transactions),
state_root,
logs_bloom: logs_bloom(receipts.iter().flat_map(|r| r.receipt.logs.iter())),
mix_hash: block_env.prevrandao.unwrap_or_default(),
..Default::default()
};

Expand Down
5 changes: 3 additions & 2 deletions crates/rpc/rpc/src/otterscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ where
value: op.value,
r#type: match op.kind {
TransferKind::Call => OperationType::OpTransfer,
TransferKind::Create => OperationType::OpCreate,
TransferKind::Create | TransferKind::EofCreate => {
OperationType::OpCreate
}
TransferKind::Create2 => OperationType::OpCreate2,
TransferKind::SelfDestruct => OperationType::OpSelfDestruct,
TransferKind::EofCreate => OperationType::OpCreate,
},
})
.collect::<Vec<_>>()
Expand Down

0 comments on commit d0392fe

Please sign in to comment.