Skip to content

Commit 21972c2

Browse files
authored
feat: replace DIFFICULTY with PREVRANDAO since merge (EIP-4399) (#162)
1 parent e7138f7 commit 21972c2

File tree

10 files changed

+50
-2
lines changed

10 files changed

+50
-2
lines changed

benches/loop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn run_loop_contract() {
1919
block_gas_limit: Default::default(),
2020
chain_id: U256::one(),
2121
block_base_fee_per_gas: U256::zero(),
22+
block_randomness: None,
2223
};
2324

2425
let mut state = BTreeMap::new();

core/src/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub enum ExitError {
127127
/// Create init code exceeds limit (runtime).
128128
#[cfg_attr(feature = "with-codec", codec(index = 7))]
129129
CreateContractLimit,
130+
130131
/// Invalid opcode during execution or starting byte is 0xef. See [EIP-3541](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3541.md).
131132
#[cfg_attr(feature = "with-codec", codec(index = 15))]
132133
InvalidCode(Opcode),

runtime/src/eval/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn eval<H: Handler>(state: &mut Runtime, opcode: Opcode, handler: &mut H) ->
4040
Opcode::COINBASE => system::coinbase(state, handler),
4141
Opcode::TIMESTAMP => system::timestamp(state, handler),
4242
Opcode::NUMBER => system::number(state, handler),
43-
Opcode::DIFFICULTY => system::difficulty(state, handler),
43+
Opcode::DIFFICULTY => system::prevrandao(state, handler),
4444
Opcode::GASLIMIT => system::gaslimit(state, handler),
4545
Opcode::SLOAD => system::sload(state, handler),
4646
Opcode::SSTORE => system::sstore(state, handler),

runtime/src/eval/system.rs

+9
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ pub fn difficulty<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H>
185185
Control::Continue
186186
}
187187

188+
pub fn prevrandao<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
189+
if let Some(rand) = handler.block_randomness() {
190+
push!(runtime, rand);
191+
Control::Continue
192+
} else {
193+
difficulty(runtime, handler)
194+
}
195+
}
196+
188197
pub fn gaslimit<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
189198
push_u256!(runtime, handler.block_gas_limit());
190199
Control::Continue

runtime/src/handler.rs

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ pub trait Handler {
5454
fn block_timestamp(&self) -> U256;
5555
/// Get environmental block difficulty.
5656
fn block_difficulty(&self) -> U256;
57+
/// Get environmental block randomness.
58+
fn block_randomness(&self) -> Option<H256>;
5759
/// Get environmental gas limit.
5860
fn block_gas_limit(&self) -> U256;
5961
/// Environmental block base fee.

runtime/src/lib.rs

+19
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@ impl Config {
409409
Self::config_with_derived_values(DerivedConfigInputs::london())
410410
}
411411

412+
/// The Merge (Paris) hard fork configuration.
413+
pub const fn merge() -> Config {
414+
Self::config_with_derived_values(DerivedConfigInputs::merge())
415+
}
416+
412417
/// Shanghai hard fork configuration.
413418
pub const fn shanghai() -> Config {
414419
Self::config_with_derived_values(DerivedConfigInputs::shanghai())
@@ -535,6 +540,20 @@ impl DerivedConfigInputs {
535540
}
536541
}
537542

543+
const fn merge() -> Self {
544+
Self {
545+
gas_storage_read_warm: 100,
546+
gas_sload_cold: 2100,
547+
gas_access_list_storage_key: 1900,
548+
decrease_clears_refund: true,
549+
has_base_fee: true,
550+
has_push0: false,
551+
disallow_executable_format: true,
552+
warm_coinbase_address: false,
553+
max_initcode_size: None,
554+
}
555+
}
556+
538557
const fn shanghai() -> Self {
539558
Self {
540559
gas_storage_read_warm: 100,

src/backend/memory.rs

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ pub struct MemoryVicinity {
3131
pub block_gas_limit: U256,
3232
/// Environmental base fee per gas.
3333
pub block_base_fee_per_gas: U256,
34+
/// Environmental randomness.
35+
///
36+
/// In Ethereum, this is the randomness beacon provided by the beacon
37+
/// chain and is only enabled post Merge.
38+
pub block_randomness: Option<H256>,
3439
}
3540

3641
/// Account information of a memory backend.
@@ -110,6 +115,9 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> {
110115
fn block_difficulty(&self) -> U256 {
111116
self.vicinity.block_difficulty
112117
}
118+
fn block_randomness(&self) -> Option<H256> {
119+
self.vicinity.block_randomness
120+
}
113121
fn block_gas_limit(&self) -> U256 {
114122
self.vicinity.block_gas_limit
115123
}

src/backend/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub trait Backend {
6666
fn block_timestamp(&self) -> U256;
6767
/// Environmental block difficulty.
6868
fn block_difficulty(&self) -> U256;
69+
/// Get environmental block randomness.
70+
fn block_randomness(&self) -> Option<H256>;
6971
/// Environmental block gas limit.
7072
fn block_gas_limit(&self) -> U256;
7173
/// Environmental block base fee.

src/executor/stack/executor.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
304304
/// Execute using Runtimes on the call_stack until it returns.
305305
fn execute_with_call_stack(
306306
&mut self,
307-
call_stack: &mut Vec<TaggedRuntime>,
307+
call_stack: &mut Vec<TaggedRuntime<'_>>,
308308
) -> (ExitReason, Option<H160>, Vec<u8>) {
309309
// This `interrupt_runtime` is used to pass the runtime obtained from the
310310
// `Capture::Trap` branch in the match below back to the top of the call stack.
@@ -1108,6 +1108,9 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler
11081108
fn block_difficulty(&self) -> U256 {
11091109
self.state.block_difficulty()
11101110
}
1111+
fn block_randomness(&self) -> Option<H256> {
1112+
self.state.block_randomness()
1113+
}
11111114
fn block_gas_limit(&self) -> U256 {
11121115
self.state.block_gas_limit()
11131116
}

src/executor/stack/memory.rs

+3
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf
421421
fn block_difficulty(&self) -> U256 {
422422
self.backend.block_difficulty()
423423
}
424+
fn block_randomness(&self) -> Option<H256> {
425+
self.backend.block_randomness()
426+
}
424427
fn block_gas_limit(&self) -> U256 {
425428
self.backend.block_gas_limit()
426429
}

0 commit comments

Comments
 (0)