Skip to content

Commit

Permalink
Make address optional in transaction data
Browse files Browse the repository at this point in the history
  • Loading branch information
IAvecilla committed Sep 11, 2024
1 parent 89fcb3a commit bbd7db9
Show file tree
Hide file tree
Showing 72 changed files with 219 additions and 163 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
16 changes: 12 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,11 @@ 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: Some(
required(&execute.contract_address)
.and_then(|x| parse_h160(x))
.context("execute.contract_address")?,
),
calldata: required(&execute.calldata).context("calldata")?.clone(),
value: required(&execute.value)
.and_then(|x| parse_h256(x))
Expand Down Expand Up @@ -487,7 +489,13 @@ impl ProtoRepr for proto::Transaction {
}
};
let execute = proto::Execute {
contract_address: Some(this.execute.contract_address.as_bytes().into()),
contract_address: Some(
this.execute
.contract_address
.unwrap_or_default()
.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/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
74 changes: 60 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,13 @@ 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 unwrapped_contract_address = contract_address.unwrap_or_default();
let contract_address_b: &[u8] = if contract_address.is_none() {
&[]
} else {
unwrapped_contract_address.as_bytes()
};
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 +149,7 @@ impl TransactionsDal<'_, '_> {
serial_id,
full_fee,
layer_2_tip_fee,
contract_address,
contract_address_b,
l1_block_number.0 as i32,
value,
empty_address.as_bytes(),
Expand All @@ -161,7 +167,13 @@ 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 unwrapped_contract_address = contract_address.unwrap_or_default();
let contract_address_b: &[u8] = if contract_address.is_none() {
&[]
} else {
unwrapped_contract_address.as_bytes()
};
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 +250,7 @@ impl TransactionsDal<'_, '_> {
gas_per_pubdata_limit,
json_data,
upgrade_id,
contract_address,
contract_address_b,
l1_block_number,
value,
&Address::default().0.to_vec(),
Expand Down Expand Up @@ -284,7 +296,13 @@ 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 unwrapped_contract_address = contract_address.unwrap_or_default();
let contract_address_b: &[u8] = if contract_address.is_none() {
&[]
} else {
unwrapped_contract_address.as_bytes()
};
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 +431,7 @@ impl TransactionsDal<'_, '_> {
input_data,
&json_data,
tx_format,
contract_address,
contract_address_b,
value,
&paymaster,
&paymaster_input,
Expand Down Expand Up @@ -697,8 +715,15 @@ impl TransactionsDal<'_, '_> {
.arg_error(&format!("transactions[{index_in_block}].refunded_gas"), err)
})?;

let contract_address = transaction.execute.contract_address;
let unwrapped_contract_address = contract_address.unwrap_or_default();
let contract_address_b: Vec<u8> = if contract_address.is_none() {
Vec::new()
} else {
unwrapped_contract_address.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_b);
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 +843,7 @@ impl TransactionsDal<'_, '_> {
&l2_inputs as &[&[u8]],
&l2_datas,
&l2_tx_formats,
&l2_contract_addresses as &[&[u8]],
&l2_contract_addresses,
&l2_values,
&l2_paymaster as &[&[u8]],
&l2_paymaster_input as &[&[u8]],
Expand Down Expand Up @@ -901,8 +926,15 @@ impl TransactionsDal<'_, '_> {
.arg_error(&format!("transactions[{index_in_block}].refunded_gas"), err)
})?;

let contract_address = transaction.execute.contract_address;
let unwrapped_contract_address = contract_address.unwrap_or_default();
let contract_address_b: Vec<u8> = if contract_address.is_none() {
Vec::new()
} else {
unwrapped_contract_address.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_b);
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 +1045,7 @@ impl TransactionsDal<'_, '_> {
&l2_datas,
&l2_refunded_gas,
&l2_values,
&l2_contract_addresses as &[&[u8]],
&l2_contract_addresses,
&l2_paymaster as &[&[u8]],
&l2_paymaster_input as &[&[u8]],
l2_block_number.0 as i32,
Expand Down Expand Up @@ -1083,6 +1115,13 @@ impl TransactionsDal<'_, '_> {
.arg_error(&format!("transactions[{index_in_block}].refunded_gas"), err)
})?;

let contract_address = transaction.execute.contract_address;
let unwrapped_contract_address = contract_address.unwrap_or_default();
let contract_address_b: Vec<u8> = if contract_address.is_none() {
Vec::new()
} else {
unwrapped_contract_address.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 +1135,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_b);
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 +1242,7 @@ impl TransactionsDal<'_, '_> {
&l1_priority_op_id,
&l1_full_fee,
&l1_layer_2_tip_fee,
&l1_contract_address as &[&[u8]],
&l1_contract_address,
&l1_l1_block_number,
&l1_value,
&l1_tx_format,
Expand Down Expand Up @@ -1373,6 +1412,13 @@ impl TransactionsDal<'_, '_> {
.arg_error(&format!("transactions[{index_in_block}].refunded_gas"), err)
})?;

let contract_address = transaction.execute.contract_address;
let unwrapped_contract_address = contract_address.unwrap_or_default();
let contract_address_b: Vec<u8> = if contract_address.is_none() {
Vec::new()
} else {
unwrapped_contract_address.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 +1431,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_b);
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 +1530,7 @@ impl TransactionsDal<'_, '_> {
&upgrade_gas_per_pubdata_limit,
&upgrade_data,
&upgrade_upgrade_id,
&upgrade_contract_address as &[&[u8]],
&upgrade_contract_address,
&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
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
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
Loading

0 comments on commit bbd7db9

Please sign in to comment.