Skip to content

Commit

Permalink
feat: state overrides (#4463)
Browse files Browse the repository at this point in the history
Co-authored-by: Franco Victorio <victorio.franco@gmail.com>
  • Loading branch information
Wodann and fvictorio authored Oct 13, 2023
1 parent ee6b363 commit 7b8843d
Show file tree
Hide file tree
Showing 28 changed files with 531 additions and 100 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/hardhat-viem-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
node-version: 18
cache: yarn
- name: Install
run: yarn --frozen-lockfile
Expand All @@ -52,7 +52,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
node-version: 18
cache: yarn
- name: Install
run: yarn --frozen-lockfile
Expand All @@ -66,7 +66,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [16, 18, 20]
node: [18, 20]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand Down
10 changes: 5 additions & 5 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 crates/rethnet_eth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ reqwest = { version = "0.11", features = ["blocking", "json"] }
# https://github.com/TrueLayer/reqwest-middleware/blob/main/reqwest-retry/CHANGELOG.md#unreleased
reqwest-middleware = { git = "https://github.com/TrueLayer/reqwest-middleware", rev = "a54319a", default-features = false }
reqwest-retry = { git = "https://github.com/TrueLayer/reqwest-middleware", rev = "a54319a", default-features = false }
revm-primitives = { git = "https://github.com/Wodann/revm", rev = "20df365", version = "1.1", default-features = false }
revm-primitives = { git = "https://github.com/Wodann/revm", rev = "93d8ebe", version = "1.1", default-features = false }
# revm-primitives = { path = "../../../revm/crates/primitives", version = "1.1", default-features = false }
rlp = { version = "0.5.2", default-features = false, features = ["derive"] }
secp256k1 = { version = "0.24.0", default-features = false, features = ["alloc", "recovery"] }
Expand Down
49 changes: 27 additions & 22 deletions crates/rethnet_eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct Block {
impl Block {
/// Constructs a new block from the provided partial header, transactions, and ommers.
pub fn new(
partial_header: PartialHeader,
mut partial_header: PartialHeader,
transactions: Vec<SignedTransaction>,
ommers: Vec<Header>,
withdrawals: Option<Vec<Withdrawal>>,
Expand All @@ -65,17 +65,14 @@ impl Block {
let transactions_root =
trie::ordered_trie_root(transactions.iter().map(|r| rlp::encode(r).freeze()));

let withdrawals_root = withdrawals.as_ref().map(|withdrawals| {
trie::ordered_trie_root(withdrawals.iter().map(|r| rlp::encode(r).freeze()))
});
if let Some(withdrawals) = withdrawals.as_ref() {
partial_header.withdrawals_root = Some(trie::ordered_trie_root(
withdrawals.iter().map(|r| rlp::encode(r).freeze()),
));
}

Self {
header: Header::new(
partial_header,
ommers_hash,
transactions_root,
withdrawals_root,
),
header: Header::new(partial_header, ommers_hash, transactions_root),
transactions,
ommers,
withdrawals,
Expand Down Expand Up @@ -172,12 +169,7 @@ pub struct BlobGas {

impl Header {
/// Constructs a header from the provided [`PartialHeader`], ommers' root hash, transactions' root hash, and withdrawals' root hash.
pub fn new(
partial_header: PartialHeader,
ommers_hash: B256,
transactions_root: B256,
withdrawals_root: Option<B256>,
) -> Self {
pub fn new(partial_header: PartialHeader, ommers_hash: B256, transactions_root: B256) -> Self {
Self {
parent_hash: partial_header.parent_hash,
ommers_hash,
Expand All @@ -195,7 +187,7 @@ impl Header {
mix_hash: partial_header.mix_hash,
nonce: partial_header.nonce,
base_fee_per_gas: partial_header.base_fee,
withdrawals_root,
withdrawals_root: partial_header.withdrawals_root,
blob_gas: partial_header.blob_gas,
parent_beacon_block_root: partial_header.parent_beacon_block_root,
}
Expand Down Expand Up @@ -345,6 +337,8 @@ pub struct PartialHeader {
pub nonce: B64,
/// BaseFee was added by EIP-1559 and is ignored in legacy headers.
pub base_fee: Option<U256>,
/// WithdrawalsHash was added by EIP-4895 and is ignored in legacy headers.
pub withdrawals_root: Option<B256>,
/// Blob gas was added by EIP-4844 and is ignored in older headers.
pub blob_gas: Option<BlobGas>,
/// The hash tree root of the parent beacon block for the given execution block (EIP-4788).
Expand Down Expand Up @@ -400,6 +394,13 @@ impl PartialHeader {
None
}
}),
withdrawals_root: options.withdrawals_root.or_else(|| {
if spec_id >= SpecId::SHANGHAI {
Some(KECCAK_NULL_RLP)
} else {
None
}
}),
blob_gas: if spec_id >= SpecId::CANCUN {
let excess_gas = parent.and_then(|parent| parent.blob_gas.as_ref()).map_or(
// For the first (post-fork) block, both parent.blob_gas_used and parent.excess_blob_gas are evaluated as 0.
Expand All @@ -417,11 +418,13 @@ impl PartialHeader {
} else {
None
},
parent_beacon_block_root: if spec_id >= SpecId::CANCUN {
Some(options.parent_beacon_block_root.unwrap_or_else(B256::zero))
} else {
None
},
parent_beacon_block_root: options.parent_beacon_block_root.or_else(|| {
if spec_id >= SpecId::CANCUN {
Some(B256::zero())
} else {
None
}
}),
}
}
}
Expand All @@ -445,6 +448,7 @@ impl Default for PartialHeader {
mix_hash: B256::default(),
nonce: B64::default(),
base_fee: None,
withdrawals_root: None,
blob_gas: None,
parent_beacon_block_root: None,
}
Expand All @@ -468,6 +472,7 @@ impl From<Header> for PartialHeader {
mix_hash: header.mix_hash,
nonce: header.nonce,
base_fee: header.base_fee_per_gas,
withdrawals_root: header.withdrawals_root,
blob_gas: header.blob_gas,
parent_beacon_block_root: header.parent_beacon_block_root,
}
Expand Down
2 changes: 2 additions & 0 deletions crates/rethnet_eth/src/block/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct BlockOptions {
pub nonce: Option<B64>,
/// The block's base gas fee
pub base_fee: Option<U256>,
/// The block's withdrawals root
pub withdrawals_root: Option<B256>,
/// The hash tree root of the parent beacon block for the given execution block (EIP-4788).
pub parent_beacon_block_root: Option<B256>,
}
2 changes: 1 addition & 1 deletion crates/rethnet_evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ once_cell = { version = "1.18.0", default-features = false, features = ["alloc",
parking_lot = { version = "0.12.1", default-features = false }
rethnet_defaults = { version = "0.1.0-dev", path = "../rethnet_defaults" }
rethnet_eth = { version = "0.1.0-dev", path = "../rethnet_eth", features = ["serde"] }
revm = { git = "https://github.com/Wodann/revm", rev = "20df365", version = "3.3", default-features = false, features = ["dev", "secp256k1", "serde", "std"] }
revm = { git = "https://github.com/Wodann/revm", rev = "93d8ebe", version = "3.3", default-features = false, features = ["dev", "secp256k1", "serde", "std"] }
# revm = { path = "../../../revm/crates/revm", version = "3.3", default-features = false, features = ["dev", "serde", "std"] }
rlp = { version = "0.5.2", default-features = false }
serde = { version = "1.0.158", default-features = false, features = ["std"] }
Expand Down
10 changes: 2 additions & 8 deletions crates/rethnet_evm/src/block/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl BlockBuilder {

let evm = build_evm(
blockchain,
state,
&state,
self.cfg.clone(),
transaction.clone().into(),
block.clone(),
Expand Down Expand Up @@ -356,20 +356,14 @@ impl BlockBuilder {
);
}

let withdrawals = if self.cfg.spec_id >= SpecId::SHANGHAI {
Some(Vec::new())
} else {
None
};

// TODO: handle ommers
let block = LocalBlock::new(
self.header,
self.transactions,
self.callers,
self.receipts,
Vec::new(),
withdrawals,
None,
);

Ok(BuildBlockResult {
Expand Down
29 changes: 10 additions & 19 deletions crates/rethnet_evm/src/block/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rethnet_eth::{
transaction::{DetailedTransaction, SignedTransaction},
trie,
withdrawal::Withdrawal,
Address, SpecId, B256, U256,
Address, B256, U256,
};
use revm::primitives::keccak256;

Expand All @@ -29,26 +29,20 @@ pub struct LocalBlock {

impl LocalBlock {
/// Constructs an empty block, i.e. no transactions.
pub fn empty(partial_header: PartialHeader, spec_id: SpecId) -> Self {
let withdrawals = if spec_id >= SpecId::SHANGHAI {
Some(Vec::new())
} else {
None
};

pub fn empty(partial_header: PartialHeader) -> Self {
Self::new(
partial_header,
Vec::new(),
Vec::new(),
Vec::new(),
Vec::new(),
withdrawals,
None,
)
}

/// Constructs a new instance with the provided data.
pub fn new(
partial_header: PartialHeader,
mut partial_header: PartialHeader,
transactions: Vec<SignedTransaction>,
transaction_callers: Vec<Address>,
transaction_receipts: Vec<TransactionReceipt<Log>>,
Expand All @@ -59,16 +53,13 @@ impl LocalBlock {
let transactions_root =
trie::ordered_trie_root(transactions.iter().map(|r| rlp::encode(r).freeze()));

let withdrawals_root = withdrawals.as_ref().map(|withdrawals| {
trie::ordered_trie_root(withdrawals.iter().map(|r| rlp::encode(r).freeze()))
});
if let Some(withdrawals) = withdrawals.as_ref() {
partial_header.withdrawals_root = Some(trie::ordered_trie_root(
withdrawals.iter().map(|r| rlp::encode(r).freeze()),
));
}

let header = Header::new(
partial_header,
ommers_hash,
transactions_root,
withdrawals_root,
);
let header = Header::new(partial_header, ommers_hash, transactions_root);

let hash = header.hash();
let transaction_receipts =
Expand Down
7 changes: 6 additions & 1 deletion crates/rethnet_evm/src/blockchain/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ impl LocalBlockchain {
} else {
None
},
withdrawals_root: if spec_id >= SpecId::SHANGHAI {
Some(KECCAK_NULL_RLP)
} else {
None
},
..PartialHeader::default()
};

Ok(unsafe {
Self::with_genesis_block_unchecked(
LocalBlock::empty(partial_header, spec_id),
LocalBlock::empty(partial_header),
genesis_state,
chain_id,
spec_id,
Expand Down
24 changes: 14 additions & 10 deletions crates/rethnet_evm/src/blockchain/storage/reservable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{fmt::Debug, num::NonZeroUsize, sync::Arc};

use parking_lot::{RwLock, RwLockUpgradableReadGuard, RwLockWriteGuard};
use rethnet_eth::{block::PartialHeader, receipt::BlockReceipt, SpecId, B256, U256};
use rethnet_eth::{
block::PartialHeader, receipt::BlockReceipt, trie::KECCAK_NULL_RLP, SpecId, B256, U256,
};
use revm::primitives::HashMap;

use crate::{state::StateDiff, Block, LocalBlock};
Expand Down Expand Up @@ -278,16 +280,18 @@ impl<BlockT: Block + Clone + From<LocalBlock>> ReservableSparseBlockchainStorage
block_number,
);

let block = LocalBlock::empty(
PartialHeader {
number: *block_number,
state_root: reservation.previous_state_root,
base_fee: reservation.previous_base_fee_per_gas,
timestamp,
..PartialHeader::default()
let block = LocalBlock::empty(PartialHeader {
number: *block_number,
state_root: reservation.previous_state_root,
base_fee: reservation.previous_base_fee_per_gas,
timestamp,
withdrawals_root: if reservation.spec_id >= SpecId::SHANGHAI {
Some(KECCAK_NULL_RLP)
} else {
None
},
reservation.spec_id,
);
..PartialHeader::default()
});

let mut storage = RwLockUpgradableReadGuard::upgrade(storage);

Expand Down
8 changes: 4 additions & 4 deletions crates/rethnet_evm/src/evm.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::fmt::Debug;

use revm::{
db::{DatabaseComponentError, DatabaseComponents},
db::{DatabaseComponentError, DatabaseComponents, StateRef},
primitives::{BlockEnv, CfgEnv, EVMError, ResultAndState, TxEnv},
Inspector,
};

use crate::{blockchain::SyncBlockchain, state::SyncState, SyncDatabase};
use crate::{blockchain::SyncBlockchain, SyncDatabase};

/// Super trait for an inspector of an `AsyncDatabase` that's debuggable.
pub trait SyncInspector<BE, SE>: Inspector<DatabaseComponentError<SE, BE>> + Debug + Send
Expand All @@ -25,10 +25,10 @@ where
}

/// Creates an evm from the provided database, config, transaction, and block.
#[cfg_attr(feature = "tracing", tracing::instrument)]
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
pub fn build_evm<'b, 's, BlockchainErrorT, StateErrorT>(
blockchain: &'b dyn SyncBlockchain<BlockchainErrorT, StateErrorT>,
state: &'s dyn SyncState<StateErrorT>,
state: &'s dyn StateRef<Error = StateErrorT>,
cfg: CfgEnv,
transaction: TxEnv,
block: BlockEnv,
Expand Down
Loading

0 comments on commit 7b8843d

Please sign in to comment.