Skip to content

Commit

Permalink
feat: make to address optional for transaction data (#2852)
Browse files Browse the repository at this point in the history
## What ❔

This PR is the first part of [this
one](#2631).

We decided to separate it into smaller PRs to make it easier to read and
review.

This PR contains only the changes to the `TransactionData` structs for
the different VM versions, modifying them for an optional field.

The other parts are currently available in our repo fork, as they are
based on this branch. The other PRs are:
- [feat: evm simulator as base system
contract](lambdaclass#256):
integrates the EVM interpreter contract as a base system contract and
includes all necessary updates for zksync-era to run with it.
- [feat: make evm simulator optional for the
server](lambdaclass#257): makes the
evm simulator optional to run using a config file.

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
IAvecilla authored Sep 19, 2024
1 parent 86299e7 commit 8363c1d
Show file tree
Hide file tree
Showing 80 changed files with 204 additions and 189 deletions.
4 changes: 2 additions & 2 deletions core/bin/system-constants-generator/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub(super) fn get_l2_tx(
pubdata_price: u32,
) -> L2Tx {
L2Tx::new_signed(
contract_address,
Some(contract_address),
vec![],
Nonce(0),
Fee {
Expand Down Expand Up @@ -134,7 +134,7 @@ pub(super) fn get_l1_tx(
) -> L1Tx {
L1Tx {
execute: Execute {
contract_address,
contract_address: Some(contract_address),
calldata: custom_calldata.unwrap_or_default(),
value: U256::from(0),
factory_deps,
Expand Down
9 changes: 5 additions & 4 deletions core/lib/dal/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,10 @@ impl ProtoRepr for proto::Transaction {
}
},
execute: Execute {
contract_address: required(&execute.contract_address)
.and_then(|x| parse_h160(x))
.context("execute.contract_address")?,
contract_address: execute
.contract_address
.as_ref()
.and_then(|x| parse_h160(x).ok()),
calldata: required(&execute.calldata).context("calldata")?.clone(),
value: required(&execute.value)
.and_then(|x| parse_h256(x))
Expand Down Expand Up @@ -487,7 +488,7 @@ impl ProtoRepr for proto::Transaction {
}
};
let execute = proto::Execute {
contract_address: Some(this.execute.contract_address.as_bytes().into()),
contract_address: this.execute.contract_address.map(|x| x.as_bytes().into()),
calldata: Some(this.execute.calldata.clone()),
value: Some(u256_to_h256(this.execute.value).as_bytes().into()),
factory_deps: this.execute.factory_deps.clone(),
Expand Down
2 changes: 1 addition & 1 deletion core/lib/dal/src/consensus/proto/mod.proto
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ message ProtocolUpgradeTxCommonData {
}

message Execute {
optional bytes contract_address = 1; // required; H160
optional bytes contract_address = 1; // optional; H160
optional bytes calldata = 2; // required
optional bytes value = 3; // required; U256
repeated bytes factory_deps = 4;
Expand Down
2 changes: 1 addition & 1 deletion core/lib/dal/src/consensus/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::tests::mock_protocol_upgrade_transaction;

fn execute(rng: &mut impl Rng) -> Execute {
Execute {
contract_address: rng.gen(),
contract_address: Some(rng.gen()),
value: rng.gen::<u128>().into(),
calldata: (0..10 * 32).map(|_| rng.gen()).collect(),
// TODO: find a way to generate valid random bytecode.
Expand Down
2 changes: 1 addition & 1 deletion core/lib/dal/src/models/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{models::storage_transaction::StorageTransaction, BigDecimal};

fn default_execute() -> Execute {
Execute {
contract_address: H160::random(),
contract_address: Some(H160::random()),
value: U256::from(10i32),
calldata: hex::decode(
"a9059cbb00000000000000000000000058d595f318167d5af45d9e44ade4348dd4e\
Expand Down
6 changes: 3 additions & 3 deletions core/lib/dal/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub(crate) fn mock_l2_transaction() -> L2Tx {
gas_per_pubdata_limit: U256::from(DEFAULT_GAS_PER_PUBDATA),
};
let mut l2_tx = L2Tx::new_signed(
Address::random(),
Some(Address::random()),
vec![],
zksync_types::Nonce(0),
fee,
Expand Down Expand Up @@ -110,7 +110,7 @@ pub(crate) fn mock_l1_execute() -> L1Tx {
};

let execute = Execute {
contract_address: H160::random(),
contract_address: Some(H160::random()),
value: Default::default(),
calldata: vec![],
factory_deps: vec![],
Expand Down Expand Up @@ -138,7 +138,7 @@ pub(crate) fn mock_protocol_upgrade_transaction() -> ProtocolUpgradeTx {
};

let execute = Execute {
contract_address: H160::random(),
contract_address: Some(H160::random()),
value: Default::default(),
calldata: vec![],
factory_deps: vec![],
Expand Down
39 changes: 25 additions & 14 deletions core/lib/dal/src/transactions_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ impl TransactionsDal<'_, '_> {
tx: &L1Tx,
l1_block_number: L1BlockNumber,
) -> DalResult<()> {
let contract_address = tx.execute.contract_address.as_bytes();
let contract_address = tx.execute.contract_address;
let contract_address_as_bytes = contract_address.map(|addr| addr.as_bytes().to_vec());
let tx_hash = tx.hash();
let tx_hash_bytes = tx_hash.as_bytes();
let json_data = serde_json::to_value(&tx.execute)
Expand Down Expand Up @@ -143,7 +144,7 @@ impl TransactionsDal<'_, '_> {
serial_id,
full_fee,
layer_2_tip_fee,
contract_address,
contract_address_as_bytes,
l1_block_number.0 as i32,
value,
empty_address.as_bytes(),
Expand All @@ -161,7 +162,8 @@ impl TransactionsDal<'_, '_> {
}

pub async fn insert_system_transaction(&mut self, tx: &ProtocolUpgradeTx) -> DalResult<()> {
let contract_address = tx.execute.contract_address.as_bytes().to_vec();
let contract_address = tx.execute.contract_address;
let contract_address_as_bytes = contract_address.map(|addr| addr.as_bytes().to_vec());
let tx_hash = tx.common_data.hash().0.to_vec();
let json_data = serde_json::to_value(&tx.execute)
.unwrap_or_else(|_| panic!("cannot serialize tx {:?} to json", tx.common_data.hash()));
Expand Down Expand Up @@ -238,7 +240,7 @@ impl TransactionsDal<'_, '_> {
gas_per_pubdata_limit,
json_data,
upgrade_id,
contract_address,
contract_address_as_bytes,
l1_block_number,
value,
&Address::default().0.to_vec(),
Expand Down Expand Up @@ -284,7 +286,8 @@ impl TransactionsDal<'_, '_> {
}

let initiator_address = tx.initiator_account();
let contract_address = tx.execute.contract_address.as_bytes();
let contract_address = tx.execute.contract_address;
let contract_address_as_bytes = contract_address.map(|addr| addr.as_bytes().to_vec());
let json_data = serde_json::to_value(&tx.execute)
.unwrap_or_else(|_| panic!("cannot serialize tx {:?} to json", tx.hash()));
let gas_limit = u256_to_big_decimal(tx.common_data.fee.gas_limit);
Expand Down Expand Up @@ -413,7 +416,7 @@ impl TransactionsDal<'_, '_> {
input_data,
&json_data,
tx_format,
contract_address,
contract_address_as_bytes,
value,
&paymaster,
&paymaster_input,
Expand Down Expand Up @@ -697,8 +700,10 @@ impl TransactionsDal<'_, '_> {
.arg_error(&format!("transactions[{index_in_block}].refunded_gas"), err)
})?;

let contract_address = transaction.execute.contract_address;
let contract_address_as_bytes = contract_address.map(|addr| addr.as_bytes().to_vec());
l2_values.push(u256_to_big_decimal(transaction.execute.value));
l2_contract_addresses.push(transaction.execute.contract_address.as_bytes());
l2_contract_addresses.push(contract_address_as_bytes);
l2_paymaster_input.push(&common_data.paymaster_params.paymaster_input[..]);
l2_paymaster.push(common_data.paymaster_params.paymaster.as_bytes());
l2_hashes.push(tx_res.hash.as_bytes());
Expand Down Expand Up @@ -818,7 +823,7 @@ impl TransactionsDal<'_, '_> {
&l2_inputs as &[&[u8]],
&l2_datas,
&l2_tx_formats,
&l2_contract_addresses as &[&[u8]],
&l2_contract_addresses as &[Option<Vec<u8>>],
&l2_values,
&l2_paymaster as &[&[u8]],
&l2_paymaster_input as &[&[u8]],
Expand Down Expand Up @@ -901,8 +906,10 @@ impl TransactionsDal<'_, '_> {
.arg_error(&format!("transactions[{index_in_block}].refunded_gas"), err)
})?;

let contract_address = transaction.execute.contract_address;
let contract_address_as_bytes = contract_address.map(|addr| addr.as_bytes().to_vec());
l2_values.push(u256_to_big_decimal(transaction.execute.value));
l2_contract_addresses.push(transaction.execute.contract_address.as_bytes());
l2_contract_addresses.push(contract_address_as_bytes);
l2_paymaster_input.push(&common_data.paymaster_params.paymaster_input[..]);
l2_paymaster.push(common_data.paymaster_params.paymaster.as_bytes());
l2_hashes.push(tx_res.hash.as_bytes());
Expand Down Expand Up @@ -1013,7 +1020,7 @@ impl TransactionsDal<'_, '_> {
&l2_datas,
&l2_refunded_gas,
&l2_values,
&l2_contract_addresses as &[&[u8]],
&l2_contract_addresses as &[Option<Vec<u8>>],
&l2_paymaster as &[&[u8]],
&l2_paymaster_input as &[&[u8]],
l2_block_number.0 as i32,
Expand Down Expand Up @@ -1083,6 +1090,8 @@ impl TransactionsDal<'_, '_> {
.arg_error(&format!("transactions[{index_in_block}].refunded_gas"), err)
})?;

let contract_address = transaction.execute.contract_address;
let contract_address_as_bytes = contract_address.map(|addr| addr.as_bytes().to_vec());
let tx = &tx_res.transaction;
l1_hashes.push(tx_res.hash.as_bytes());
l1_initiator_address.push(common_data.sender.as_bytes());
Expand All @@ -1096,7 +1105,7 @@ impl TransactionsDal<'_, '_> {
l1_priority_op_id.push(common_data.serial_id.0 as i64);
l1_full_fee.push(u256_to_big_decimal(common_data.full_fee));
l1_layer_2_tip_fee.push(u256_to_big_decimal(common_data.layer_2_tip_fee));
l1_contract_address.push(tx.execute.contract_address.as_bytes());
l1_contract_address.push(contract_address_as_bytes);
l1_l1_block_number.push(common_data.eth_block as i32);
l1_value.push(u256_to_big_decimal(tx.execute.value));
l1_tx_format.push(common_data.tx_format() as i32);
Expand Down Expand Up @@ -1203,7 +1212,7 @@ impl TransactionsDal<'_, '_> {
&l1_priority_op_id,
&l1_full_fee,
&l1_layer_2_tip_fee,
&l1_contract_address as &[&[u8]],
&l1_contract_address as &[Option<Vec<u8>>],
&l1_l1_block_number,
&l1_value,
&l1_tx_format,
Expand Down Expand Up @@ -1373,6 +1382,8 @@ impl TransactionsDal<'_, '_> {
.arg_error(&format!("transactions[{index_in_block}].refunded_gas"), err)
})?;

let contract_address = transaction.execute.contract_address;
let contract_address_as_bytes = contract_address.map(|addr| addr.as_bytes().to_vec());
let tx = &tx_res.transaction;
upgrade_hashes.push(tx_res.hash.as_bytes());
upgrade_initiator_address.push(common_data.sender.as_bytes());
Expand All @@ -1385,7 +1396,7 @@ impl TransactionsDal<'_, '_> {
.unwrap_or_else(|_| panic!("cannot serialize tx {:?} to json", tx.hash())),
);
upgrade_upgrade_id.push(common_data.upgrade_id as i32);
upgrade_contract_address.push(tx.execute.contract_address.as_bytes());
upgrade_contract_address.push(contract_address_as_bytes);
upgrade_l1_block_number.push(common_data.eth_block as i32);
upgrade_value.push(u256_to_big_decimal(tx.execute.value));
upgrade_tx_format.push(common_data.tx_format() as i32);
Expand Down Expand Up @@ -1484,7 +1495,7 @@ impl TransactionsDal<'_, '_> {
&upgrade_gas_per_pubdata_limit,
&upgrade_data,
&upgrade_upgrade_id,
&upgrade_contract_address as &[&[u8]],
&upgrade_contract_address as &[Option<Vec<u8>>],
&upgrade_l1_block_number,
&upgrade_value,
&upgrade_tx_format,
Expand Down
4 changes: 2 additions & 2 deletions core/lib/mempool/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ fn gen_l2_tx(address: Address, nonce: Nonce) -> Transaction {

fn gen_l2_tx_with_timestamp(address: Address, nonce: Nonce, received_at_ms: u64) -> Transaction {
let mut txn = L2Tx::new(
Address::default(),
Some(Address::default()),
Vec::new(),
nonce,
Fee::default(),
Expand All @@ -386,7 +386,7 @@ fn gen_l2_tx_with_timestamp(address: Address, nonce: Nonce, received_at_ms: u64)

fn gen_l1_tx(priority_id: PriorityOpId) -> Transaction {
let execute = Execute {
contract_address: Address::repeat_byte(0x11),
contract_address: Some(Address::repeat_byte(0x11)),
calldata: vec![1, 2, 3],
factory_deps: vec![],
value: U256::zero(),
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/vm_1_3_2/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn get_create_execute(code: &[u8], calldata: &[u8]) -> Execute {
.expect("failed to encode parameters");

Execute {
contract_address: CONTRACT_DEPLOYER_ADDRESS,
contract_address: Some(CONTRACT_DEPLOYER_ADDRESS),
calldata,
factory_deps: vec![code.to_vec()],
value: U256::zero(),
Expand Down
6 changes: 3 additions & 3 deletions core/lib/multivm/src/versions/vm_1_3_2/transaction_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::vm_1_3_2::vm_with_bootloader::{
pub struct TransactionData {
pub tx_type: u8,
pub from: Address,
pub to: Address,
pub to: Option<Address>,
pub gas_limit: U256,
pub pubdata_price_limit: U256,
pub max_fee_per_gas: U256,
Expand Down Expand Up @@ -170,7 +170,7 @@ impl TransactionData {
encode(&[Token::Tuple(vec![
Token::Uint(U256::from_big_endian(&self.tx_type.to_be_bytes())),
Token::Address(self.from),
Token::Address(self.to),
Token::Address(self.to.unwrap_or_default()),
Token::Uint(self.gas_limit),
Token::Uint(self.pubdata_price_limit),
Token::Uint(self.max_fee_per_gas),
Expand Down Expand Up @@ -593,7 +593,7 @@ mod tests {
let transaction = TransactionData {
tx_type: 113,
from: Address::random(),
to: Address::random(),
to: Some(Address::random()),
gas_limit: U256::from(1u32),
pubdata_price_limit: U256::from(1u32),
max_fee_per_gas: U256::from(1u32),
Expand Down
6 changes: 2 additions & 4 deletions core/lib/multivm/src/versions/vm_1_4_1/tests/gas_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ fn test_tx_gas_limit_offset() {
let gas_limit = 9999.into();
let tx = vm.rich_accounts[0].get_l2_tx_for_execute(
Execute {
contract_address: Default::default(),
calldata: vec![],
value: Default::default(),
factory_deps: None,
contract_address: Some(Default::default()),
..Default::default()
},
Some(Fee {
gas_limit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::vm_1_4_1::{
pub(crate) struct TransactionData {
pub(crate) tx_type: u8,
pub(crate) from: Address,
pub(crate) to: Address,
pub(crate) to: Option<Address>,
pub(crate) gas_limit: U256,
pub(crate) pubdata_price_limit: U256,
pub(crate) max_fee_per_gas: U256,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl TransactionData {
encode(&[Token::Tuple(vec![
Token::Uint(U256::from_big_endian(&self.tx_type.to_be_bytes())),
Token::Address(self.from),
Token::Address(self.to),
Token::Address(self.to.unwrap_or_default()),
Token::Uint(self.gas_limit),
Token::Uint(self.pubdata_price_limit),
Token::Uint(self.max_fee_per_gas),
Expand Down Expand Up @@ -311,7 +311,7 @@ mod tests {
let transaction = TransactionData {
tx_type: 113,
from: Address::random(),
to: Address::random(),
to: Some(Address::random()),
gas_limit: U256::from(1u32),
pubdata_price_limit: U256::from(1u32),
max_fee_per_gas: U256::from(1u32),
Expand Down
6 changes: 2 additions & 4 deletions core/lib/multivm/src/versions/vm_1_4_2/tests/gas_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ fn test_tx_gas_limit_offset() {
let gas_limit = 9999.into();
let tx = vm.rich_accounts[0].get_l2_tx_for_execute(
Execute {
contract_address: Default::default(),
calldata: vec![],
value: Default::default(),
factory_deps: None,
contract_address: Some(Default::default()),
..Default::default()
},
Some(Fee {
gas_limit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::vm_1_4_2::{
pub(crate) struct TransactionData {
pub(crate) tx_type: u8,
pub(crate) from: Address,
pub(crate) to: Address,
pub(crate) to: Option<Address>,
pub(crate) gas_limit: U256,
pub(crate) pubdata_price_limit: U256,
pub(crate) max_fee_per_gas: U256,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl TransactionData {
encode(&[Token::Tuple(vec![
Token::Uint(U256::from_big_endian(&self.tx_type.to_be_bytes())),
Token::Address(self.from),
Token::Address(self.to),
Token::Address(self.to.unwrap_or_default()),
Token::Uint(self.gas_limit),
Token::Uint(self.pubdata_price_limit),
Token::Uint(self.max_fee_per_gas),
Expand Down Expand Up @@ -311,7 +311,7 @@ mod tests {
let transaction = TransactionData {
tx_type: 113,
from: Address::random(),
to: Address::random(),
to: Some(Address::random()),
gas_limit: U256::from(1u32),
pubdata_price_limit: U256::from(1u32),
max_fee_per_gas: U256::from(1u32),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ fn test_tx_gas_limit_offset() {
let gas_limit = 9999.into();
let tx = vm.rich_accounts[0].get_l2_tx_for_execute(
Execute {
contract_address: Default::default(),
calldata: vec![],
value: Default::default(),
factory_deps: None,
contract_address: Some(Default::default()),
..Default::default()
},
Some(Fee {
gas_limit,
Expand Down
Loading

0 comments on commit 8363c1d

Please sign in to comment.