Skip to content

Commit

Permalink
feat(revm): Add prevrandao field to EnvBlock (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita authored Nov 16, 2022
1 parent d1703cd commit 69e302b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bins/revme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ hex = "0.4"
indicatif = "0.17"
plain_hasher = "0.2"
primitive-types = { version = "0.11", features = ["rlp", "serde"] }
revm = { path = "../../crates/revm", version = "2.1", default-features = false, features = ["web3db","std","secp256k1"] }
revm = { path = "../../crates/revm", version = "2.2", default-features = false, features = ["web3db","std","secp256k1"] }
rlp = { version = "0.5", default-features = false }
ruint = { version = "1.6.0", features = ["rlp", "serde"] }
serde = "1.0"
Expand Down
2 changes: 2 additions & 0 deletions bins/revme/src/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ pub fn execute_test_suit(path: &Path, elapsed: &Arc<Mutex<Duration>>) -> Result<
env.block.gas_limit = unit.env.current_gas_limit;
env.block.basefee = unit.env.current_base_fee.unwrap_or_default();
env.block.difficulty = unit.env.current_difficulty;
// after the Merge prevrandao replaces mix_hash field in block and replaced difficulty opcode in EVM.
env.block.prevrandao = Some(unit.env.current_difficulty.to_be_bytes().into());

//tx env
env.tx.caller =
Expand Down
4 changes: 4 additions & 0 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact
let gas_limit = self.data.env.tx.gas_limit;
let exit = |reason: Return| (ExecutionResult::new_with_reason(reason), State::new());

if GSPEC::enabled(MERGE) && self.data.env.block.prevrandao.is_none() {
return exit(Return::PrevrandaoNotSet);
}

if GSPEC::enabled(LONDON) {
if let Some(priority_fee) = self.data.env.tx.gas_priority_fee {
if priority_fee > self.data.env.tx.gas_price {
Expand Down
3 changes: 2 additions & 1 deletion crates/revm/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub enum Return {
OutOfOffset,
FatalExternalError,
GasMaxFeeGreaterThanPriorityFee,
PrevrandaoNotSet,
GasPriceLessThenBasefee,
CallerGasLimitMoreThenBlock,
/// EIP-3607 Reject transactions from senders with deployed code
Expand Down Expand Up @@ -225,7 +226,7 @@ pub fn eval<H: Host, S: Spec>(opcode: u8, interp: &mut Interpreter, host: &mut H
opcode::COINBASE => host_env::coinbase(interp, host),
opcode::TIMESTAMP => host_env::timestamp(interp, host),
opcode::NUMBER => host_env::number(interp, host),
opcode::DIFFICULTY => host_env::difficulty(interp, host),
opcode::DIFFICULTY => host_env::difficulty::<H, S>(interp, host),
opcode::GASLIMIT => host_env::gaslimit(interp, host),
opcode::SLOAD => host::sload::<H, S>(interp, host),
opcode::SSTORE => host::sstore::<H, S>(interp, host),
Expand Down
8 changes: 6 additions & 2 deletions crates/revm/src/instructions/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ pub fn number<H: Host>(interp: &mut Interpreter, host: &mut H) -> Return {
Return::Continue
}

pub fn difficulty<H: Host>(interp: &mut Interpreter, host: &mut H) -> Return {
pub fn difficulty<H: Host, SPEC: Spec>(interp: &mut Interpreter, host: &mut H) -> Return {
// gas!(interp, gas::BASE);
push!(interp, host.env().block.difficulty);
if SPEC::enabled(MERGE) {
push_h256!(interp, host.env().block.prevrandao.unwrap());
} else {
push!(interp, host.env().block.difficulty);
}
Return::Continue
}

Expand Down
5 changes: 5 additions & 0 deletions crates/revm/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ pub struct BlockEnv {
/// Address where we are going to send gas spend
pub coinbase: H160,
pub timestamp: U256,
/// Difficulty is removed and not used after Paris (aka TheMerge). Value is replaced with prevrandao.
pub difficulty: U256,
/// Prevrandao is used after Paris (aka TheMerge) instead of the difficulty value.
/// NOTE: prevrandao can be found in block in place of mix_hash.
pub prevrandao: Option<H256>,
/// basefee is added in EIP1559 London upgrade
pub basefee: U256,
pub gas_limit: U256,
Expand Down Expand Up @@ -288,6 +292,7 @@ impl Default for BlockEnv {
coinbase: H160::zero(),
timestamp: U256::from(1),
difficulty: U256::ZERO,
prevrandao: Some(H256::zero()),
basefee: U256::ZERO,
}
}
Expand Down

0 comments on commit 69e302b

Please sign in to comment.