Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
sync with upstream for shanghai changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vimpunk committed Apr 27, 2023
1 parent 169af74 commit bdb00fc
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 12 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion ethjson/src/spec/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ pub enum ForkSpec {
Istanbul,
/// Berlin (#12,244,000, 2021-04-15)
Berlin,
/// London (To be announced)
/// London (#12,965,000, 2021-08-05)
London,
/// Paris - The Merge (#15,537,394, 2022-09-15)
Merge,
/// Shanghai (#17,034,870, 2023-04-12)
Shanghai,

/// Byzantium transition test-net
EIP158ToByzantiumAt5,
/// Homestead transition test-net
Expand All @@ -58,6 +63,17 @@ pub enum ForkSpec {
ConstantinopleFixToIstanbulAt5,
}

impl ForkSpec {
/// Returns true if the fork is at or after the merge.
pub fn is_eth2(&self) -> bool {
// NOTE: Include new forks in this match arm.
matches!(
*self,
ForkSpec::London | ForkSpec::Merge | ForkSpec::Shanghai
)
}
}

/// Spec deserialization.
#[derive(Debug, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
Expand Down
10 changes: 8 additions & 2 deletions ethjson/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ pub struct Env {
#[serde(rename = "currentBaseFee")]
#[serde(default)]
pub block_base_fee_per_gas: Uint,
/// Pre-seeded random value for testing
#[serde(rename = "currentRandom")]
#[serde(default)]
pub random: Option<Uint>,
}

#[cfg(test)]
Expand All @@ -145,7 +149,8 @@ mod tests {
"currentDifficulty" : "0x0100",
"currentGasLimit" : "0x0f4240",
"currentNumber" : "0x00",
"currentTimestamp" : "0x01"
"currentTimestamp" : "0x01",
"currentRandom" : "0x01"
},
"exec" : {
"address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
Expand Down Expand Up @@ -192,7 +197,8 @@ mod tests {
gas_limit: Uint(0x0f4240.into()),
number: Uint(0.into()),
timestamp: Uint(1.into()),
block_base_fee_per_gas: Uint(0.into())
block_base_fee_per_gas: Uint(0.into()),
random: Some(Uint(1.into())),
}
);
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion jsontests/res/ethtests
25 changes: 21 additions & 4 deletions jsontests/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ impl Test {
return None;
}

let block_randomness = if spec.is_eth2() {
self.0.env.random.map(|r| r.0)
} else {
None
};

Some(MemoryVicinity {
gas_price,
origin: self.unwrap_caller(),
Expand All @@ -81,6 +87,7 @@ impl Test {
block_gas_limit: self.0.env.gas_limit.into(),
chain_id: U256::one(),
block_base_fee_per_gas,
block_randomness,
})
}
}
Expand Down Expand Up @@ -139,6 +146,10 @@ impl JsonPrecompile {
}
// precompiles for London and Berlin are the same
ForkSpec::London => Self::precompile(&ForkSpec::Berlin),
// precompiles for Merge and Berlin are the same
ForkSpec::Merge => Self::precompile(&ForkSpec::Berlin),
// precompiles for Shanghai and Berlin are the same
ForkSpec::Shanghai => Self::precompile(&ForkSpec::Berlin),
_ => None,
}
}
Expand Down Expand Up @@ -211,8 +222,10 @@ fn test_run(name: &str, test: Test) {
ethjson::spec::ForkSpec::Istanbul => (Config::istanbul(), true),
ethjson::spec::ForkSpec::Berlin => (Config::berlin(), true),
ethjson::spec::ForkSpec::London => (Config::london(), true),
ethjson::spec::ForkSpec::Merge => (Config::merge(), true),
ethjson::spec::ForkSpec::Shanghai => (Config::shanghai(), true),
spec => {
println!("Skip spec {:?}", spec);
println!("Skip spec {spec:?}");
continue;
}
};
Expand Down Expand Up @@ -288,8 +301,9 @@ fn test_run(name: &str, test: Test) {
}

let actual_fee = executor.fee(vicinity.gas_price);
let mniner_reward = if let ForkSpec::London = spec {
// see EIP-1559
// Forks after London burn miner rewards and thus have different gas fee
// calculation (see EIP-1559)
let miner_reward = if spec.is_eth2() {
let max_priority_fee_per_gas = test.0.transaction.max_priority_fee_per_gas();
let max_fee_per_gas = test.0.transaction.max_fee_per_gas();
let base_fee_per_gas = vicinity.block_base_fee_per_gas;
Expand All @@ -299,11 +313,14 @@ fn test_run(name: &str, test: Test) {
} else {
actual_fee
};

executor
.state_mut()
.deposit(vicinity.block_coinbase, mniner_reward);
.deposit(vicinity.block_coinbase, miner_reward);
executor.state_mut().deposit(caller, total_fee - actual_fee);

let (values, logs) = executor.into_state().deconstruct();

backend.apply(values, logs, delete_empty);
}

Expand Down
1 change: 1 addition & 0 deletions jsontests/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl Test {
block_gas_limit: self.0.env.gas_limit.clone().into(),
chain_id: U256::zero(),
block_base_fee_per_gas: self.0.transaction.gas_price.clone().into(),
block_randomness: self.0.env.random.map(|r| r.0),
}
}

Expand Down

0 comments on commit bdb00fc

Please sign in to comment.