Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
feat: remove difficulty after eip-4399 aka POS
Browse files Browse the repository at this point in the history
  • Loading branch information
johntaiko committed Aug 22, 2023
1 parent 5c3d641 commit 4fdac73
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 69 deletions.
10 changes: 6 additions & 4 deletions bus-mapping/src/circuit_input_builder/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
operation::{OperationContainer, RWCounter},
Error,
};
use eth_types::{evm_unimplemented, Address, Word};
use eth_types::{evm_unimplemented, Address, Hash, Word};
use std::collections::HashMap;

/// Context of a [`Block`] which can mutate in a [`Transaction`].
Expand Down Expand Up @@ -69,8 +69,8 @@ pub struct Block {
pub number: Word,
/// difficulty
pub timestamp: Word,
/// gas limit
pub difficulty: Word,
/// mix hash
pub mix_hash: Hash,
/// base fee
pub base_fee: Word,
/// State root of the previous block
Expand Down Expand Up @@ -126,7 +126,9 @@ impl Block {
.low_u64()
.into(),
timestamp: eth_block.timestamp,
difficulty: eth_block.difficulty,
mix_hash: eth_block
.mix_hash
.ok_or(Error::EthTypeError(eth_types::Error::IncompleteBlock))?,
base_fee: eth_block.base_fee_per_gas.unwrap_or_default(),
prev_state_root,
container: OperationContainer::new(),
Expand Down
6 changes: 3 additions & 3 deletions bus-mapping/src/evm/opcodes/stackonlyop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ mod stackonlyop_tests {
bytecode,
evm_types::{OpcodeId, StackAddress},
geth_types::GethData,
word, Bytecode, Word,
word, Bytecode, ToWord, Word,
};
use itertools::Itertools;
use mock::{
test_ctx::{helpers::*, TestContext},
MOCK_BASEFEE, MOCK_DIFFICULTY, MOCK_GASLIMIT,
MOCK_BASEFEE, MOCK_GASLIMIT, MOCK_MIX_HASH,
};
use pretty_assertions::assert_eq;
use std::ops::{BitOr, BitXor};
Expand Down Expand Up @@ -375,7 +375,7 @@ mod stackonlyop_tests {
STOP
},
vec![],
vec![StackOp::new(1, StackAddress(1023), *MOCK_DIFFICULTY)],
vec![StackOp::new(1, StackAddress(1023), MOCK_MIX_HASH.to_word())],
);
}

Expand Down
8 changes: 4 additions & 4 deletions eth-types/src/geth_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct BlockConstants {
/// U64 type is required to serialize into proper hex with 0x prefix
pub number: U64,
/// difficulty
pub difficulty: Word,
pub mix_hash: Hash,
/// gas limit
pub gas_limit: Word,
/// base fee
Expand All @@ -87,7 +87,7 @@ impl<TX> TryFrom<&Block<TX>> for BlockConstants {
coinbase: block.author.ok_or(Error::IncompleteBlock)?,
timestamp: block.timestamp,
number: block.number.ok_or(Error::IncompleteBlock)?,
difficulty: block.difficulty,
mix_hash: block.mix_hash.ok_or(Error::IncompleteBlock)?,
gas_limit: block.gas_limit,
base_fee: block.base_fee_per_gas.ok_or(Error::IncompleteBlock)?,
})
Expand All @@ -100,15 +100,15 @@ impl BlockConstants {
coinbase: Address,
timestamp: Word,
number: U64,
difficulty: Word,
mix_hash: Hash,
gas_limit: Word,
base_fee: Word,
) -> BlockConstants {
BlockConstants {
coinbase,
timestamp,
number,
difficulty,
mix_hash,
gas_limit,
base_fee,
}
Expand Down
17 changes: 7 additions & 10 deletions geth-utils/gethutil/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ func FormatLogs(logs []logger.StructLog) []StructLogRes {
}

type Block struct {
Coinbase common.Address `json:"coinbase"`
Timestamp *hexutil.Big `json:"timestamp"`
Number *hexutil.Big `json:"number"`
Difficulty *hexutil.Big `json:"difficulty"`
GasLimit *hexutil.Big `json:"gas_limit"`
BaseFee *hexutil.Big `json:"base_fee"`
Coinbase common.Address `json:"coinbase"`
Timestamp *hexutil.Big `json:"timestamp"`
Number *hexutil.Big `json:"number"`
GasLimit *hexutil.Big `json:"gas_limit"`
BaseFee *hexutil.Big `json:"base_fee"`
MixHash common.Hash `json:"mix_hash"`
}

type Account struct {
Expand Down Expand Up @@ -186,8 +186,6 @@ func Trace(config TraceConfig) ([]*ExecutionResult, error) {
return nil, fmt.Errorf("txs total gas: %d Exceeds block gas limit: %d", txsGasLimit, blockGasLimit)
}

random := common.BigToHash(toBigInt(config.Block.Difficulty))

blockCtx := vm.BlockContext{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
Expand All @@ -202,10 +200,9 @@ func Trace(config TraceConfig) ([]*ExecutionResult, error) {
Coinbase: config.Block.Coinbase,
BlockNumber: toBigInt(config.Block.Number),
Time: toBigInt(config.Block.Timestamp).Uint64(),
Difficulty: toBigInt(config.Block.Difficulty),
BaseFee: toBigInt(config.Block.BaseFee),
GasLimit: blockGasLimit,
Random: &random,
Random: &config.Block.MixHash,
}

// Setup state db with accounts from argument
Expand Down
6 changes: 3 additions & 3 deletions mock/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Mock Block definition and builder related methods.
use crate::{MockTransaction, MOCK_BASEFEE, MOCK_CHAIN_ID, MOCK_DIFFICULTY, MOCK_GASLIMIT};
use crate::{MockTransaction, MOCK_BASEFEE, MOCK_CHAIN_ID, MOCK_GASLIMIT, MOCK_MIX_HASH};
use eth_types::{Address, Block, Bytes, Hash, Transaction, Word, H64, U64};
use ethers_core::types::{Bloom, OtherFields};

Expand Down Expand Up @@ -54,13 +54,13 @@ impl Default for MockBlock {
extra_data: Bytes::default(),
logs_bloom: None,
timestamp: Word::from(123456789u64),
difficulty: *MOCK_DIFFICULTY,
mix_hash: *MOCK_MIX_HASH,
total_difficulty: Word::zero(),
difficulty: Word::zero(),
seal_fields: Vec::new(),
uncles: Vec::new(),
transactions: Vec::new(),
size: Word::zero(),
mix_hash: Hash::zero(),
nonce: H64::zero(),
chain_id: *MOCK_CHAIN_ID,
}
Expand Down
4 changes: 3 additions & 1 deletion mock/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Mock types and functions to generate GethData used for tests
use std::str::FromStr;

use eth_types::{address, bytecode, bytecode::Bytecode, word, Address, Bytes, Hash, Word};
use ethers_signers::LocalWallet;
use lazy_static::lazy_static;
Expand Down Expand Up @@ -34,7 +36,7 @@ lazy_static! {
/// Mock chain ID value
pub static ref MOCK_CHAIN_ID: Word = Word::from(1338u64);
/// Mock DIFFICULTY value
pub static ref MOCK_DIFFICULTY: Word = Word::from(0x200000u64);
pub static ref MOCK_MIX_HASH: Hash = Hash::from_str("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347").unwrap();
/// Mock accounts loaded with ETH to use for test cases.
pub static ref MOCK_ACCOUNTS: Vec<Address> = vec![
address!("0x000000000000000000000000000000000cafe111"),
Expand Down
4 changes: 2 additions & 2 deletions testool/src/statetest/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn into_traceconfig(st: StateTest) -> (String, TraceConfig, StateTestResult) {
coinbase: st.env.current_coinbase,
timestamp: U256::from(st.env.current_timestamp),
number: U64::from(st.env.current_number),
difficulty: st.env.current_difficulty,
mix_hash: st.env.current_mix_hash,
gas_limit: U256::from(st.env.current_gas_limit),
base_fee: U256::one(),
},
Expand Down Expand Up @@ -222,7 +222,7 @@ pub fn run_test(
author: Some(trace_config.block_constants.coinbase),
timestamp: trace_config.block_constants.timestamp,
number: Some(U64::from(trace_config.block_constants.number.as_u64())),
difficulty: trace_config.block_constants.difficulty,
mix_hash: Some(trace_config.block_constants.mix_hash),
gas_limit: trace_config.block_constants.gas_limit,
base_fee_per_gas: Some(trace_config.block_constants.base_fee),
transactions,
Expand Down
10 changes: 6 additions & 4 deletions testool/src/statetest/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::collections::HashMap;
#[serde(rename_all = "camelCase")]
struct TestEnv {
current_coinbase: String,
current_difficulty: String,
current_mix_hash: String,
current_gas_limit: String,
current_number: String,
current_timestamp: String,
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<'a> JsonStateTestBuilder<'a> {
fn parse_env(env: &TestEnv) -> Result<Env> {
Ok(Env {
current_coinbase: parse::parse_address(&env.current_coinbase)?,
current_difficulty: parse::parse_u256(&env.current_difficulty)?,
current_mix_hash: parse::parse_hash(&env.current_mix_hash)?,
current_gas_limit: parse::parse_u64(&env.current_gas_limit)?,
current_number: parse::parse_u64(&env.current_number)?,
current_timestamp: parse::parse_u64(&env.current_timestamp)?,
Expand Down Expand Up @@ -305,7 +305,7 @@ mod test {
},
"env" : {
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentDifficulty" : "0x20000",
"currentMixHash" : "0x20000",
"currentGasLimit" : "0xFF112233445566",
"currentNumber" : "1",
"currentTimestamp" : "1000",
Expand Down Expand Up @@ -371,7 +371,9 @@ mod test {
id: "add11_d0_g0_v0".to_string(),
env: Env {
current_coinbase: Address::from_str("0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba")?,
current_difficulty: U256::from(131072u64),
current_mix_hash: H256::from_str(
"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
)?,
current_gas_limit: 0xFF112233445566,
current_number: 1,
current_timestamp: 1000,
Expand Down
9 changes: 3 additions & 6 deletions testool/src/statetest/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{collections::HashMap, str::FromStr};
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Env {
pub current_coinbase: Address,
pub current_difficulty: U256,
pub current_mix_hash: H256,
pub current_gas_limit: u64,
pub current_number: u64,
pub current_timestamp: u64,
Expand Down Expand Up @@ -90,10 +90,7 @@ impl std::fmt::Display for StateTest {
}
table.add_row(row!["coinbase", format!("{:?}", self.env.current_coinbase)]);

table.add_row(row![
"difficulty",
format!("{}", self.env.current_difficulty)
]);
table.add_row(row!["mix_hash", format!("{}", self.env.current_mix_hash)]);
table.add_row(row!["number", format!("{}", self.env.current_number)]);
table.add_row(row!["timestamp", format!("{}", self.env.current_timestamp)]);
table.add_row(row!["prev_hash", format!("{:?}", self.env.previous_hash)]);
Expand Down Expand Up @@ -262,7 +259,7 @@ impl StateTest {
id: String::default(),
env: Env {
current_coinbase: *mock::MOCK_COINBASE,
current_difficulty: U256::default(),
current_mix_hash: H256::default(),
current_gas_limit: 16000000,
current_number: 1,
current_timestamp: 1,
Expand Down
8 changes: 5 additions & 3 deletions testool/src/statetest/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl<'a> YamlStateTestBuilder<'a> {
fn parse_env(yaml: &Yaml) -> Result<Env> {
Ok(Env {
current_coinbase: Self::parse_address(&yaml["currentCoinbase"])?,
current_difficulty: Self::parse_u256(&yaml["currentDifficulty"])?,
current_mix_hash: Self::parse_hash(&yaml["currentMixHash"])?,
current_gas_limit: Self::parse_u64(&yaml["currentGasLimit"])?,
current_number: Self::parse_u64(&yaml["currentNumber"])?,
current_timestamp: Self::parse_u64(&yaml["currentTimestamp"])?,
Expand Down Expand Up @@ -436,7 +436,7 @@ mod test {
arith:
env:
currentCoinbase: 2adc25665018aa1fe0e6bc666dac8fc2697ff9ba
currentDifficulty: 0x20000
currentMixHash: 0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347
currentGasLimit: {{ gas_limit }}
currentNumber: 1
currentTimestamp: 1000
Expand Down Expand Up @@ -600,7 +600,9 @@ arith:
id: "arith_d0_g0_v0".into(),
env: Env {
current_coinbase: address!("0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"),
current_difficulty: U256::from(0x20000u64),
current_mix_hash: H256::from_str(
"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
)?,
current_number: 1,
current_timestamp: 1000,
current_gas_limit: 100000000,
Expand Down
28 changes: 16 additions & 12 deletions zkevm-circuits/src/pi_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct BlockValues {
gas_limit: u64,
number: u64,
timestamp: u64,
difficulty: Word,
mix_hash: H256,
base_fee: Word, // NOTE: BaseFee was added by EIP-1559 and is ignored in legacy headers.
chain_id: u64,
history_hashes: Vec<H256>,
Expand Down Expand Up @@ -125,7 +125,7 @@ impl PublicData {
gas_limit: self.block_constants.gas_limit.as_u64(),
number: self.block_constants.number.as_u64(),
timestamp: self.block_constants.timestamp.as_u64(),
difficulty: self.block_constants.difficulty,
mix_hash: self.block_constants.mix_hash,
base_fee: self.block_constants.base_fee,
chain_id: self.chain_id.as_u64(),
history_hashes,
Expand Down Expand Up @@ -926,21 +926,23 @@ impl<F: Field> PiCircuitConfig<F> {
raw_pi_vals[offset] = timestamp;
offset += 1;

// difficulty
let difficulty = rlc(block_values.difficulty.to_le_bytes(), randomness);
let mut mix_hash_bytes = block_values.mix_hash.to_fixed_bytes();
mix_hash_bytes.reverse();
// mix hash
let mix_hash = rlc(mix_hash_bytes, randomness);
region.assign_advice(
|| "difficulty",
|| "mix_hash",
self.block_table.value,
offset,
|| Value::known(difficulty),
|| Value::known(mix_hash),
)?;
region.assign_advice(
|| "difficulty",
|| "mix_hash",
self.raw_public_inputs,
offset,
|| Value::known(difficulty),
|| Value::known(mix_hash),
)?;
raw_pi_vals[offset] = difficulty;
raw_pi_vals[offset] = mix_hash;
offset += 1;

// base_fee
Expand Down Expand Up @@ -1164,7 +1166,7 @@ impl<F: Field> SubCircuit<F> for PiCircuit<F> {
coinbase: block.context.coinbase,
timestamp: block.context.timestamp,
number: block.context.number.as_u64().into(),
difficulty: block.context.difficulty,
mix_hash: block.context.mix_hash,
gas_limit: block.context.gas_limit.into(),
base_fee: block.context.base_fee,
},
Expand Down Expand Up @@ -1487,8 +1489,10 @@ fn raw_public_inputs_col<F: Field>(
// timestamp
result[offset] = F::from(block.timestamp);
offset += 1;
// difficulty
result[offset] = rlc(block.difficulty.to_le_bytes(), randomness);
// mix hash
let mut mix_hash_bytes = block.mix_hash.to_fixed_bytes();
mix_hash_bytes.reverse();
result[offset] = rlc(mix_hash_bytes, randomness);
offset += 1;
// base_fee
result[offset] = rlc(block.base_fee.to_le_bytes(), randomness);
Expand Down
Loading

0 comments on commit 4fdac73

Please sign in to comment.