Skip to content

Commit

Permalink
Move chain_id from Tx to Block on mock (privacy-scaling-explorations#439
Browse files Browse the repository at this point in the history
)

* update: Move `chain_id` from Tx to Block on mock

When generating a `TestContext` now, users will be able to set the
`chain_id` parameter only at block level but not at tx one.

Anyway, `MockTransaction` will still have this field. But there's no
public API to interact with it. Instead, the values of the `chain_id`
for the txs of a block will automatically be set to the value that was
set as `chain_id` for the block.

This closes the circle on the turn arround that was done in
privacy-scaling-explorations/zkevm-specs#168 by adding `CHAINID`
as a blockCtx Opcode rather than a Tx one for simplification &
optimization purposes.

Resolves: privacy-scaling-explorations#437

* update: Update Cargo.lock

* fix: Reference MockTransaction instead of MockBlock
  • Loading branch information
CPerezz authored Apr 8, 2022
1 parent 27320f8 commit e806164
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 40 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

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

19 changes: 15 additions & 4 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;
use crate::{MockTransaction, MOCK_CHAIN_ID};
use eth_types::{Address, Block, Bytes, Hash, Transaction, Word, U64};
use ethbloom::Bloom;

Expand Down Expand Up @@ -31,6 +31,10 @@ pub struct MockBlock {
size: Word,
mix_hash: Hash,
nonce: U64,
// This field is handled here as we assume that all block txs have the same ChainId.
// Also, the field is stored in the block_table since we don't have a chain_config
// structure/table.
pub(crate) chain_id: Word,
}

impl Default for MockBlock {
Expand Down Expand Up @@ -58,12 +62,13 @@ impl Default for MockBlock {
size: Word::zero(),
mix_hash: Hash::zero(),
nonce: U64::zero(),
chain_id: *MOCK_CHAIN_ID,
}
}
}

impl From<MockBlock> for Block<Transaction> {
fn from(mock: MockBlock) -> Self {
fn from(mut mock: MockBlock) -> Self {
Block {
hash: mock.hash.or_else(|| Some(Hash::default())),
parent_hash: mock.parent_hash,
Expand All @@ -84,8 +89,8 @@ impl From<MockBlock> for Block<Transaction> {
uncles: mock.uncles,
transactions: mock
.transactions
.iter()
.map(|mock_tx| (mock_tx.to_owned()).into())
.iter_mut()
.map(|mock_tx| (mock_tx.chain_id(mock.chain_id).to_owned()).into())
.collect::<Vec<Transaction>>(),
size: Some(mock.size),
mix_hash: Some(mock.mix_hash),
Expand Down Expand Up @@ -261,6 +266,12 @@ impl MockBlock {
self
}

/// Set chain_id field for the MockBlock.
pub fn chain_id(&mut self, chain_id: Word) -> &mut Self {
self.chain_id = chain_id;
self
}

/// Finalizes the current MockBlock under construction returning a new
/// instance to it.
pub fn build(&mut self) -> Self {
Expand Down
9 changes: 2 additions & 7 deletions mock/src/test_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,7 @@ impl<const NACC: usize, const NTX: usize> TestContext<NACC, NTX> {
block.transactions.extend_from_slice(&transactions);
func_block(&mut block, transactions).build();

let transactions: Vec<Transaction> = block
.transactions
.iter()
.cloned()
.map(Transaction::from)
.collect();
let chain_id = block.chain_id;
let block = Block::<Transaction>::from(block);
let accounts: [Account; NACC] = accounts
.iter()
Expand All @@ -179,7 +174,7 @@ impl<const NACC: usize, const NTX: usize> TestContext<NACC, NTX> {
let geth_traces = gen_geth_traces(block.clone(), accounts.clone(), history_hashes.clone())?;

Ok(Self {
chain_id: transactions[0].chain_id.unwrap_or_default(),
chain_id,
accounts,
history_hashes: history_hashes.unwrap_or_default(),
eth_block: block,
Expand Down
36 changes: 18 additions & 18 deletions mock/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,100 +95,100 @@ impl MockTransaction {
self
}

/// Set block_hash field for the MockBlock.
/// Set block_hash field for the MockTransaction.
pub fn block_hash(&mut self, block_hash: Hash) -> &mut Self {
self.block_hash = block_hash;
self
}

/// Set block_number field for the MockBlock.
/// Set block_number field for the MockTransaction.
pub fn block_number(&mut self, block_number: u64) -> &mut Self {
self.block_number = U64::from(block_number);
self
}

/// Set transaction_idx field for the MockBlock.
/// Set transaction_idx field for the MockTransaction.
pub fn transaction_idx(&mut self, transaction_idx: u64) -> &mut Self {
self.transaction_index = U64::from(transaction_idx);
self
}

/// Set from field for the MockBlock.
/// Set from field for the MockTransaction.
pub fn from(&mut self, from: Address) -> &mut Self {
self.from = from;
self
}

/// Set to field for the MockBlock.
/// Set to field for the MockTransaction.
pub fn to(&mut self, to: Address) -> &mut Self {
self.to = Some(to);
self
}

/// Set value field for the MockBlock.
/// Set value field for the MockTransaction.
pub fn value(&mut self, value: Word) -> &mut Self {
self.value = value;
self
}

/// Set gas_price field for the MockBlock.
/// Set gas_price field for the MockTransaction.
pub fn gas_price(&mut self, gas_price: Word) -> &mut Self {
self.gas_price = gas_price;
self
}

/// Set gas field for the MockBlock.
/// Set gas field for the MockTransaction.
pub fn gas(&mut self, gas: Word) -> &mut Self {
self.gas = gas;
self
}

/// Set input field for the MockBlock.
/// Set input field for the MockTransaction.
pub fn input(&mut self, input: Bytes) -> &mut Self {
self.input = input;
self
}

/// Set sig_data field for the MockBlock.
/// Set sig_data field for the MockTransaction.
pub fn sig_data(&mut self, data: (u64, Word, Word)) -> &mut Self {
self.v = U64::from(data.0);
self.r = data.1;
self.s = data.2;
self
}

/// Set transaction_type field for the MockBlock.
/// Set transaction_type field for the MockTransaction.
pub fn transaction_type(&mut self, transaction_type: u64) -> &mut Self {
self.transaction_type = U64::from(transaction_type);
self
}

/// Set access_list field for the MockBlock.
/// Set access_list field for the MockTransaction.
pub fn access_list(&mut self, access_list: AccessList) -> &mut Self {
self.access_list = access_list;
self
}

/// Set max_priority_fee_per_gas field for the MockBlock.
/// Set max_priority_fee_per_gas field for the MockTransaction.
pub fn max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: Word) -> &mut Self {
self.max_priority_fee_per_gas = max_priority_fee_per_gas;
self
}

/// Set max_fee_per_gas field for the MockBlock.
/// Set max_fee_per_gas field for the MockTransaction.
pub fn max_fee_per_gas(&mut self, max_fee_per_gas: Word) -> &mut Self {
self.max_fee_per_gas = max_fee_per_gas;
self
}

/// Set chain_id field for the MockBlock.
pub fn chain_id(&mut self, chain_id: Word) -> &mut Self {
/// Set chain_id field for the MockTransaction.
pub(crate) fn chain_id(&mut self, chain_id: Word) -> &mut Self {
self.chain_id = chain_id;
self
}

/// Consumes the mutable ref to the MockBlock returning the structure by
/// value.
/// Consumes the mutable ref to the MockTransaction returning the structure
/// by value.
pub fn build(&mut self) -> Self {
self.to_owned()
}
Expand Down

0 comments on commit e806164

Please sign in to comment.