From 251e8b3ab01de883973923d614646a777d8e8eec Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Mon, 11 Mar 2024 14:06:56 +0100 Subject: [PATCH 01/15] wip --- .../api_server/web3/backend_jsonrpsee/mod.rs | 1 + .../src/api_server/web3/tests/mod.rs | 70 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs index 6ff938fd1b6c..a2e9f0ee715b 100644 --- a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs @@ -37,6 +37,7 @@ pub(crate) fn into_jsrpc_error(err: Web3Error) -> ErrorObjectOwned { Web3Error::PubSubTimeout => 4, Web3Error::RequestTimeout => 5, Web3Error::TreeApiUnavailable => 6, + Web3Error::PubdataNotFound => todo!(), }, match err { Web3Error::SubmitTransactionError(message, _) => message, diff --git a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs index 2ea1d677e890..fd91344b2fac 100644 --- a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs @@ -933,3 +933,73 @@ impl HttpTest for AllAccountBalancesTest { async fn getting_all_account_balances() { test_http_server(AllAccountBalancesTest).await; } + +#[derive(Debug)] +struct GetPubdataTest; + +impl GetPubdataTest { + const ADDRESS: Address = Address::repeat_byte(0x11); +} + +#[async_trait] +impl HttpTest for GetPubdataTest { + async fn test(&self, client: &HttpClient, pool: &ConnectionPool) -> anyhow::Result<()> { + let pubdata = client.get_batch_pubdata().await?; + assert_eq!(pubdata, HashMap::new()); + + // let mut storage = pool.access_storage().await?; + // store_miniblock(&mut storage, MiniblockNumber(1), &[]).await?; + + // let eth_balance_key = storage_key_for_eth_balance(&Self::ADDRESS); + // let eth_balance = U256::one() << 64; + // let eth_balance_log = StorageLog::new_write_log(eth_balance_key, u256_to_h256(eth_balance)); + // storage + // .storage_logs_dal() + // .insert_storage_logs(MiniblockNumber(1), &[(H256::zero(), vec![eth_balance_log])]) + // .await?; + // // Create a custom token, but don't set balance for it yet. + // let custom_token = TokenInfo { + // l1_address: Address::repeat_byte(0xfe), + // l2_address: Address::repeat_byte(0xfe), + // metadata: TokenMetadata::default(Address::repeat_byte(0xfe)), + // }; + // storage + // .tokens_dal() + // .add_tokens(slice::from_ref(&custom_token)) + // .await?; + + // let balances = client.get_all_account_balances(Self::ADDRESS).await?; + // assert_eq!(balances, HashMap::from([(Address::zero(), eth_balance)])); + + // store_miniblock(&mut storage, MiniblockNumber(2), &[]).await?; + // let token_balance_key = storage_key_for_standard_token_balance( + // AccountTreeId::new(custom_token.l2_address), + // &Self::ADDRESS, + // ); + // let token_balance = 123.into(); + // let token_balance_log = + // StorageLog::new_write_log(token_balance_key, u256_to_h256(token_balance)); + // storage + // .storage_logs_dal() + // .insert_storage_logs( + // MiniblockNumber(2), + // &[(H256::zero(), vec![token_balance_log])], + // ) + // .await?; + + // let balances = client.get_all_account_balances(Self::ADDRESS).await?; + // assert_eq!( + // balances, + // HashMap::from([ + // (Address::zero(), eth_balance), + // (custom_token.l2_address, token_balance), + // ]) + // ); + Ok(()) + } +} + +#[tokio::test] +async fn get_batch_pubdata_impl() { + test_http_server(GetPubdataTest).await; +} From e8e028dd5df0a2543404baa54b61a3a9e8652dcc Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Mon, 11 Mar 2024 19:40:17 +0100 Subject: [PATCH 02/15] wip --- core/lib/dal/src/blocks_dal.rs | 20 +++++++++- core/lib/dal/src/models/storage_block.rs | 37 ++++++++++++++++++- .../api_server/web3/backend_jsonrpsee/mod.rs | 1 - .../src/api_server/web3/tests/mod.rs | 22 ++++++++--- 4 files changed, 69 insertions(+), 11 deletions(-) diff --git a/core/lib/dal/src/blocks_dal.rs b/core/lib/dal/src/blocks_dal.rs index 4d1029e64923..6014380d4751 100644 --- a/core/lib/dal/src/blocks_dal.rs +++ b/core/lib/dal/src/blocks_dal.rs @@ -12,6 +12,7 @@ use zksync_types::{ block::{BlockGasCount, L1BatchHeader, L1BatchTreeData, MiniblockHeader}, circuit::CircuitStatistic, commitment::{L1BatchCommitmentArtifacts, L1BatchWithMetadata}, + l1, zk_evm_types::LogQuery, Address, L1BatchNumber, MiniblockNumber, ProtocolVersionId, H256, U256, }; @@ -1680,6 +1681,7 @@ impl BlocksDal<'_, '_> { &mut self, number: L1BatchNumber, ) -> anyhow::Result> { + println!("Getting l1 batch metadata for {}", number.0); let Some(l1_batch) = self .get_storage_l1_batch(number) .await @@ -1687,9 +1689,15 @@ impl BlocksDal<'_, '_> { else { return Ok(None); }; - self.get_l1_batch_with_metadata(l1_batch) + println!("Got l1 batch {:?}", l1_batch); + + let l1_batch_with_metadata = self + .get_l1_batch_with_metadata(l1_batch) .await - .context("get_l1_batch_with_metadata") + .context("get_l1_batch_with_metadata"); + + println!("L1 batch with metadata {:?}", l1_batch_with_metadata); + l1_batch_with_metadata } pub async fn get_l1_batch_tree_data( @@ -1726,10 +1734,18 @@ impl BlocksDal<'_, '_> { .get_l1_batch_factory_deps(L1BatchNumber(storage_batch.number as u32)) .await .context("get_l1_batch_factory_deps()")?; + + println!("Unsorted factory deps {:?}", unsorted_factory_deps.clone()); + let header: L1BatchHeader = storage_batch.clone().into(); let Ok(metadata) = storage_batch.try_into() else { + println!("Failed to convert storage batch into metadata"); return Ok(None); }; + println!( + "Getting raw published bytecode hashes for {}", + header.number.0 + ); let raw_published_bytecode_hashes = self .storage .events_dal() diff --git a/core/lib/dal/src/models/storage_block.rs b/core/lib/dal/src/models/storage_block.rs index 9b67695c444b..e33cb750f9a0 100644 --- a/core/lib/dal/src/models/storage_block.rs +++ b/core/lib/dal/src/models/storage_block.rs @@ -192,7 +192,37 @@ impl TryInto for StorageL1Batch { type Error = StorageL1BatchConvertError; fn try_into(self) -> Result { - Ok(L1BatchMetadata { + println!("Trying into"); + println!("Hash is: {:?}", self.hash.is_some()); + println!( + "Rollup last leaf index is: {:?}", + self.rollup_last_leaf_index.is_some() + ); + println!("Merkle root hash is: {:?}", self.merkle_root_hash.is_some()); + println!( + "Compressed initial writes is: {:?}", + self.compressed_initial_writes.is_some() + ); + println!( + "Compressed repeated writes is: {:?}", + self.compressed_repeated_writes.is_some() + ); + println!( + "L2 L1 merkle root is: {:?}", + self.l2_l1_merkle_root.is_some() + ); + println!("Aux data hash is: {:?}", self.aux_data_hash.is_some()); + println!( + "Meta parameters hash is: {:?}", + self.meta_parameters_hash.is_some() + ); + println!( + "Pass through data hash is: {:?}", + self.pass_through_data_hash.is_some() + ); + println!("Commitment is: {:?}", self.commitment.is_some()); + + let result = L1BatchMetadata { root_hash: H256::from_slice(&self.hash.ok_or(StorageL1BatchConvertError::Incomplete)?), rollup_last_leaf_index: self .rollup_last_leaf_index @@ -250,7 +280,10 @@ impl TryInto for StorageL1Batch { bootloader_initial_content_commitment: self .bootloader_initial_content_commitment .map(|v| H256::from_slice(&v)), - }) + }; + + println!("Result: {:?}", result); + Ok(result) } } diff --git a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs index a2e9f0ee715b..6ff938fd1b6c 100644 --- a/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/backend_jsonrpsee/mod.rs @@ -37,7 +37,6 @@ pub(crate) fn into_jsrpc_error(err: Web3Error) -> ErrorObjectOwned { Web3Error::PubSubTimeout => 4, Web3Error::RequestTimeout => 5, Web3Error::TreeApiUnavailable => 6, - Web3Error::PubdataNotFound => todo!(), }, match err { Web3Error::SubmitTransactionError(message, _) => message, diff --git a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs index fd91344b2fac..facfa52c6c0a 100644 --- a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs @@ -25,7 +25,8 @@ use zksync_types::{ TransactionExecutionResult, }, utils::{storage_key_for_eth_balance, storage_key_for_standard_token_balance}, - AccountTreeId, Address, L1BatchNumber, Nonce, StorageKey, StorageLog, VmEvent, H256, U64, + AccountTreeId, Address, Bytes, L1BatchNumber, Nonce, StorageKey, StorageLog, VmEvent, H256, + U64, }; use zksync_utils::u256_to_h256; use zksync_web3_decl::{ @@ -254,7 +255,7 @@ impl StorageInitialization { async fn test_http_server(test: impl HttpTest) { let pool = ConnectionPool::test_pool().await; let network_config = NetworkConfig::for_tests(); - let mut storage = pool.access_storage().await.unwrap(); + let mut storage: StorageProcessor<'_> = pool.access_storage().await.unwrap(); test.storage_initialization() .prepare_storage(&network_config, &mut storage) .await @@ -943,13 +944,22 @@ impl GetPubdataTest { #[async_trait] impl HttpTest for GetPubdataTest { + fn storage_initialization(&self) -> StorageInitialization { + StorageInitialization::Genesis + } + async fn test(&self, client: &HttpClient, pool: &ConnectionPool) -> anyhow::Result<()> { - let pubdata = client.get_batch_pubdata().await?; - assert_eq!(pubdata, HashMap::new()); + let pubdata = client.get_batch_pubdata(L1BatchNumber(1)).await?; + assert_eq!(pubdata, None); - // let mut storage = pool.access_storage().await?; - // store_miniblock(&mut storage, MiniblockNumber(1), &[]).await?; + let mut storage = pool.access_storage().await?; + storage + .blocks_dal() + .insert_mock_l1_batch(&create_l1_batch(1)) + .await?; + let pubdata = client.get_batch_pubdata(L1BatchNumber(1)).await?; + assert!(pubdata.is_some()); // let eth_balance_key = storage_key_for_eth_balance(&Self::ADDRESS); // let eth_balance = U256::one() << 64; // let eth_balance_log = StorageLog::new_write_log(eth_balance_key, u256_to_h256(eth_balance)); From fddb8c2c6fece21dff1d0b25846f20ced66f7e2d Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Tue, 12 Mar 2024 16:22:57 +0100 Subject: [PATCH 03/15] store l1 batch in test --- core/lib/dal/sqlx-data.json | 3 + core/lib/dal/src/blocks_dal.rs | 12 ---- core/lib/dal/src/models/storage_block.rs | 30 -------- core/lib/types/src/commitment/mod.rs | 27 ++++++- .../src/api_server/web3/tests/mod.rs | 70 ++++++------------- .../src/eth_sender/tests_helpers.rs | 32 ++------- prover/prover_dal/sqlx-data.json | 3 + 7 files changed, 60 insertions(+), 117 deletions(-) create mode 100644 core/lib/dal/sqlx-data.json create mode 100644 prover/prover_dal/sqlx-data.json diff --git a/core/lib/dal/sqlx-data.json b/core/lib/dal/sqlx-data.json new file mode 100644 index 000000000000..95c8c858baaf --- /dev/null +++ b/core/lib/dal/sqlx-data.json @@ -0,0 +1,3 @@ +{ + "db": "PostgreSQL" +} \ No newline at end of file diff --git a/core/lib/dal/src/blocks_dal.rs b/core/lib/dal/src/blocks_dal.rs index 6014380d4751..1b27b102a6e5 100644 --- a/core/lib/dal/src/blocks_dal.rs +++ b/core/lib/dal/src/blocks_dal.rs @@ -12,7 +12,6 @@ use zksync_types::{ block::{BlockGasCount, L1BatchHeader, L1BatchTreeData, MiniblockHeader}, circuit::CircuitStatistic, commitment::{L1BatchCommitmentArtifacts, L1BatchWithMetadata}, - l1, zk_evm_types::LogQuery, Address, L1BatchNumber, MiniblockNumber, ProtocolVersionId, H256, U256, }; @@ -1681,7 +1680,6 @@ impl BlocksDal<'_, '_> { &mut self, number: L1BatchNumber, ) -> anyhow::Result> { - println!("Getting l1 batch metadata for {}", number.0); let Some(l1_batch) = self .get_storage_l1_batch(number) .await @@ -1689,14 +1687,11 @@ impl BlocksDal<'_, '_> { else { return Ok(None); }; - println!("Got l1 batch {:?}", l1_batch); - let l1_batch_with_metadata = self .get_l1_batch_with_metadata(l1_batch) .await .context("get_l1_batch_with_metadata"); - println!("L1 batch with metadata {:?}", l1_batch_with_metadata); l1_batch_with_metadata } @@ -1735,17 +1730,10 @@ impl BlocksDal<'_, '_> { .await .context("get_l1_batch_factory_deps()")?; - println!("Unsorted factory deps {:?}", unsorted_factory_deps.clone()); - let header: L1BatchHeader = storage_batch.clone().into(); let Ok(metadata) = storage_batch.try_into() else { - println!("Failed to convert storage batch into metadata"); return Ok(None); }; - println!( - "Getting raw published bytecode hashes for {}", - header.number.0 - ); let raw_published_bytecode_hashes = self .storage .events_dal() diff --git a/core/lib/dal/src/models/storage_block.rs b/core/lib/dal/src/models/storage_block.rs index e33cb750f9a0..12df5799fe9b 100644 --- a/core/lib/dal/src/models/storage_block.rs +++ b/core/lib/dal/src/models/storage_block.rs @@ -192,36 +192,6 @@ impl TryInto for StorageL1Batch { type Error = StorageL1BatchConvertError; fn try_into(self) -> Result { - println!("Trying into"); - println!("Hash is: {:?}", self.hash.is_some()); - println!( - "Rollup last leaf index is: {:?}", - self.rollup_last_leaf_index.is_some() - ); - println!("Merkle root hash is: {:?}", self.merkle_root_hash.is_some()); - println!( - "Compressed initial writes is: {:?}", - self.compressed_initial_writes.is_some() - ); - println!( - "Compressed repeated writes is: {:?}", - self.compressed_repeated_writes.is_some() - ); - println!( - "L2 L1 merkle root is: {:?}", - self.l2_l1_merkle_root.is_some() - ); - println!("Aux data hash is: {:?}", self.aux_data_hash.is_some()); - println!( - "Meta parameters hash is: {:?}", - self.meta_parameters_hash.is_some() - ); - println!( - "Pass through data hash is: {:?}", - self.pass_through_data_hash.is_some() - ); - println!("Commitment is: {:?}", self.commitment.is_some()); - let result = L1BatchMetadata { root_hash: H256::from_slice(&self.hash.ok_or(StorageL1BatchConvertError::Incomplete)?), rollup_last_leaf_index: self diff --git a/core/lib/types/src/commitment/mod.rs b/core/lib/types/src/commitment/mod.rs index a65faca0e50c..65280b4991d1 100644 --- a/core/lib/types/src/commitment/mod.rs +++ b/core/lib/types/src/commitment/mod.rs @@ -99,6 +99,31 @@ impl L1BatchMetadata { } } +impl Default for L1BatchMetadata { + fn default() -> Self { + L1BatchMetadata { + root_hash: Default::default(), + rollup_last_leaf_index: 0, + merkle_root_hash: Default::default(), + initial_writes_compressed: Some(vec![]), + repeated_writes_compressed: Some(vec![]), + commitment: Default::default(), + l2_l1_merkle_root: Default::default(), + block_meta_params: L1BatchMetaParameters { + zkporter_is_available: false, + bootloader_code_hash: Default::default(), + default_aa_code_hash: Default::default(), + }, + aux_data_hash: Default::default(), + meta_parameters_hash: Default::default(), + pass_through_data_hash: Default::default(), + events_queue_commitment: Some(H256::zero()), + bootloader_initial_content_commitment: Some(H256::zero()), + state_diffs_compressed: vec![], + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct L1BatchWithMetadata { pub header: L1BatchHeader, @@ -446,7 +471,7 @@ impl L1BatchAuxiliaryOutput { } /// Meta parameters for an L1 batch. They are the same for each L1 batch per run. -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct L1BatchMetaParameters { pub zkporter_is_available: bool, pub bootloader_code_hash: H256, diff --git a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs index facfa52c6c0a..c82feb5081f5 100644 --- a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs @@ -15,6 +15,7 @@ use zksync_health_check::CheckHealth; use zksync_types::{ api, block::MiniblockHeader, + commitment::L1BatchMetadata, fee::TransactionExecutionMetrics, get_nonce_key, l2::L2Tx, @@ -40,6 +41,7 @@ use crate::{ execution_sandbox::testonly::MockTransactionExecutor, tx_sender::tests::create_test_tx_sender, }, + eth_sender::aggregated_operations::AggregatedOperation, genesis::{ensure_genesis_state, GenesisParams}, utils::testonly::{ create_l1_batch, create_l1_batch_metadata, create_l2_transaction, create_miniblock, @@ -953,58 +955,32 @@ impl HttpTest for GetPubdataTest { assert_eq!(pubdata, None); let mut storage = pool.access_storage().await?; + + // Insertion + let header = create_l1_batch(1); + let metadata = L1BatchMetadata::default(); + storage .blocks_dal() - .insert_mock_l1_batch(&create_l1_batch(1)) - .await?; + .insert_mock_l1_batch(&header) + .await + .unwrap(); + storage + .blocks_dal() + .save_l1_batch_tree_data(header.number, &metadata.tree_data()) + .await + .unwrap(); + storage + .blocks_dal() + .save_l1_batch_commitment_artifacts( + header.number, + &l1_batch_metadata_to_commitment_artifacts(&metadata), + ) + .await + .unwrap(); let pubdata = client.get_batch_pubdata(L1BatchNumber(1)).await?; assert!(pubdata.is_some()); - // let eth_balance_key = storage_key_for_eth_balance(&Self::ADDRESS); - // let eth_balance = U256::one() << 64; - // let eth_balance_log = StorageLog::new_write_log(eth_balance_key, u256_to_h256(eth_balance)); - // storage - // .storage_logs_dal() - // .insert_storage_logs(MiniblockNumber(1), &[(H256::zero(), vec![eth_balance_log])]) - // .await?; - // // Create a custom token, but don't set balance for it yet. - // let custom_token = TokenInfo { - // l1_address: Address::repeat_byte(0xfe), - // l2_address: Address::repeat_byte(0xfe), - // metadata: TokenMetadata::default(Address::repeat_byte(0xfe)), - // }; - // storage - // .tokens_dal() - // .add_tokens(slice::from_ref(&custom_token)) - // .await?; - - // let balances = client.get_all_account_balances(Self::ADDRESS).await?; - // assert_eq!(balances, HashMap::from([(Address::zero(), eth_balance)])); - - // store_miniblock(&mut storage, MiniblockNumber(2), &[]).await?; - // let token_balance_key = storage_key_for_standard_token_balance( - // AccountTreeId::new(custom_token.l2_address), - // &Self::ADDRESS, - // ); - // let token_balance = 123.into(); - // let token_balance_log = - // StorageLog::new_write_log(token_balance_key, u256_to_h256(token_balance)); - // storage - // .storage_logs_dal() - // .insert_storage_logs( - // MiniblockNumber(2), - // &[(H256::zero(), vec![token_balance_log])], - // ) - // .await?; - - // let balances = client.get_all_account_balances(Self::ADDRESS).await?; - // assert_eq!( - // balances, - // HashMap::from([ - // (Address::zero(), eth_balance), - // (custom_token.l2_address, token_balance), - // ]) - // ); Ok(()) } } diff --git a/core/lib/zksync_core/src/eth_sender/tests_helpers.rs b/core/lib/zksync_core/src/eth_sender/tests_helpers.rs index 3a0e73c46398..107816658e7b 100644 --- a/core/lib/zksync_core/src/eth_sender/tests_helpers.rs +++ b/core/lib/zksync_core/src/eth_sender/tests_helpers.rs @@ -12,7 +12,7 @@ use zksync_l1_contract_interface::i_executor::methods::{ExecuteBatches, ProveBat use zksync_object_store::ObjectStoreFactory; use zksync_types::{ block::L1BatchHeader, - commitment::{L1BatchMetaParameters, L1BatchMetadata, L1BatchWithMetadata}, + commitment::{L1BatchMetadata, L1BatchWithMetadata}, ethabi::Token, helpers::unix_timestamp_ms, web3::contract::Error, @@ -36,7 +36,7 @@ static DUMMY_OPERATION: Lazy = Lazy::new(|| { AggregatedOperation::Execute(ExecuteBatches { l1_batches: vec![L1BatchWithMetadata { header: create_l1_batch(1), - metadata: default_l1_batch_metadata(), + metadata: L1BatchMetadata::default(), raw_published_factory_deps: Vec::new(), }], }) @@ -149,34 +149,11 @@ impl EthSenderTester { fn l1_batch_with_metadata(header: L1BatchHeader) -> L1BatchWithMetadata { L1BatchWithMetadata { header, - metadata: default_l1_batch_metadata(), + metadata: L1BatchMetadata::default(), raw_published_factory_deps: vec![], } } -fn default_l1_batch_metadata() -> L1BatchMetadata { - L1BatchMetadata { - root_hash: Default::default(), - rollup_last_leaf_index: 0, - merkle_root_hash: Default::default(), - initial_writes_compressed: Some(vec![]), - repeated_writes_compressed: Some(vec![]), - commitment: Default::default(), - l2_l1_merkle_root: Default::default(), - block_meta_params: L1BatchMetaParameters { - zkporter_is_available: false, - bootloader_code_hash: Default::default(), - default_aa_code_hash: Default::default(), - }, - aux_data_hash: Default::default(), - meta_parameters_hash: Default::default(), - pass_through_data_hash: Default::default(), - events_queue_commitment: Some(H256::zero()), - bootloader_initial_content_commitment: Some(H256::zero()), - state_diffs_compressed: vec![], - } -} - pub(crate) async fn confirm_many(tester: &mut EthSenderTester) -> anyhow::Result<()> { let mut hashes = vec![]; @@ -617,7 +594,8 @@ async fn insert_l1_batch(tester: &EthSenderTester, number: L1BatchNumber) -> L1B .insert_mock_l1_batch(&header) .await .unwrap(); - let metadata = default_l1_batch_metadata(); + let metadata = L1BatchMetadata::default(); + tester .storage() .await diff --git a/prover/prover_dal/sqlx-data.json b/prover/prover_dal/sqlx-data.json new file mode 100644 index 000000000000..95c8c858baaf --- /dev/null +++ b/prover/prover_dal/sqlx-data.json @@ -0,0 +1,3 @@ +{ + "db": "PostgreSQL" +} \ No newline at end of file From b03c90f51bd5759381bb07f52ea4279bd1faf000 Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Tue, 12 Mar 2024 16:23:39 +0100 Subject: [PATCH 04/15] fmt --- core/lib/zksync_core/src/api_server/web3/tests/mod.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs index c82feb5081f5..6d16c2f70b84 100644 --- a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs @@ -26,8 +26,7 @@ use zksync_types::{ TransactionExecutionResult, }, utils::{storage_key_for_eth_balance, storage_key_for_standard_token_balance}, - AccountTreeId, Address, Bytes, L1BatchNumber, Nonce, StorageKey, StorageLog, VmEvent, H256, - U64, + AccountTreeId, Address, L1BatchNumber, Nonce, StorageKey, StorageLog, VmEvent, H256, U64, }; use zksync_utils::u256_to_h256; use zksync_web3_decl::{ @@ -41,7 +40,6 @@ use crate::{ execution_sandbox::testonly::MockTransactionExecutor, tx_sender::tests::create_test_tx_sender, }, - eth_sender::aggregated_operations::AggregatedOperation, genesis::{ensure_genesis_state, GenesisParams}, utils::testonly::{ create_l1_batch, create_l1_batch_metadata, create_l2_transaction, create_miniblock, @@ -940,9 +938,7 @@ async fn getting_all_account_balances() { #[derive(Debug)] struct GetPubdataTest; -impl GetPubdataTest { - const ADDRESS: Address = Address::repeat_byte(0x11); -} +impl GetPubdataTest {} #[async_trait] impl HttpTest for GetPubdataTest { From e4857f7bce64bb5dd05310923aaa8a1c4d2f14a2 Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Tue, 12 Mar 2024 16:25:10 +0100 Subject: [PATCH 05/15] remove compile files --- core/lib/dal/sqlx-data.json | 3 --- prover/prover_dal/sqlx-data.json | 3 --- 2 files changed, 6 deletions(-) delete mode 100644 core/lib/dal/sqlx-data.json delete mode 100644 prover/prover_dal/sqlx-data.json diff --git a/core/lib/dal/sqlx-data.json b/core/lib/dal/sqlx-data.json deleted file mode 100644 index 95c8c858baaf..000000000000 --- a/core/lib/dal/sqlx-data.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "db": "PostgreSQL" -} \ No newline at end of file diff --git a/prover/prover_dal/sqlx-data.json b/prover/prover_dal/sqlx-data.json deleted file mode 100644 index 95c8c858baaf..000000000000 --- a/prover/prover_dal/sqlx-data.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "db": "PostgreSQL" -} \ No newline at end of file From 5aece03ec9980eabda07293699244870d7f68ef8 Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Tue, 12 Mar 2024 16:32:53 +0100 Subject: [PATCH 06/15] revert changes --- core/lib/dal/src/blocks_dal.rs | 8 ++------ core/lib/dal/src/models/storage_block.rs | 7 ++----- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/core/lib/dal/src/blocks_dal.rs b/core/lib/dal/src/blocks_dal.rs index 1b27b102a6e5..4d1029e64923 100644 --- a/core/lib/dal/src/blocks_dal.rs +++ b/core/lib/dal/src/blocks_dal.rs @@ -1687,12 +1687,9 @@ impl BlocksDal<'_, '_> { else { return Ok(None); }; - let l1_batch_with_metadata = self - .get_l1_batch_with_metadata(l1_batch) + self.get_l1_batch_with_metadata(l1_batch) .await - .context("get_l1_batch_with_metadata"); - - l1_batch_with_metadata + .context("get_l1_batch_with_metadata") } pub async fn get_l1_batch_tree_data( @@ -1729,7 +1726,6 @@ impl BlocksDal<'_, '_> { .get_l1_batch_factory_deps(L1BatchNumber(storage_batch.number as u32)) .await .context("get_l1_batch_factory_deps()")?; - let header: L1BatchHeader = storage_batch.clone().into(); let Ok(metadata) = storage_batch.try_into() else { return Ok(None); diff --git a/core/lib/dal/src/models/storage_block.rs b/core/lib/dal/src/models/storage_block.rs index 12df5799fe9b..9b67695c444b 100644 --- a/core/lib/dal/src/models/storage_block.rs +++ b/core/lib/dal/src/models/storage_block.rs @@ -192,7 +192,7 @@ impl TryInto for StorageL1Batch { type Error = StorageL1BatchConvertError; fn try_into(self) -> Result { - let result = L1BatchMetadata { + Ok(L1BatchMetadata { root_hash: H256::from_slice(&self.hash.ok_or(StorageL1BatchConvertError::Incomplete)?), rollup_last_leaf_index: self .rollup_last_leaf_index @@ -250,10 +250,7 @@ impl TryInto for StorageL1Batch { bootloader_initial_content_commitment: self .bootloader_initial_content_commitment .map(|v| H256::from_slice(&v)), - }; - - println!("Result: {:?}", result); - Ok(result) + }) } } From 07bfc0e4aca0e8cac17d3627e9934528e7fb0cbf Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Wed, 13 Mar 2024 10:59:05 +0100 Subject: [PATCH 07/15] making test pass in rollup --- .../src/api_server/web3/tests/mod.rs | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs index 6d16c2f70b84..b7d29db6afda 100644 --- a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs @@ -15,10 +15,11 @@ use zksync_health_check::CheckHealth; use zksync_types::{ api, block::MiniblockHeader, - commitment::L1BatchMetadata, + commitment::{L1BatchMetadata, L1BatchWithMetadata}, fee::TransactionExecutionMetrics, get_nonce_key, l2::L2Tx, + l2_to_l1_log::{L2ToL1Log, UserL2ToL1Log}, storage::get_code_key, tokens::{TokenInfo, TokenMetadata}, tx::{ @@ -26,7 +27,8 @@ use zksync_types::{ TransactionExecutionResult, }, utils::{storage_key_for_eth_balance, storage_key_for_standard_token_balance}, - AccountTreeId, Address, L1BatchNumber, Nonce, StorageKey, StorageLog, VmEvent, H256, U64, + AccountTreeId, Address, Bytes, L1BatchNumber, Nonce, StorageKey, StorageLog, VmEvent, H256, + U64, }; use zksync_utils::u256_to_h256; use zksync_web3_decl::{ @@ -950,11 +952,29 @@ impl HttpTest for GetPubdataTest { let pubdata = client.get_batch_pubdata(L1BatchNumber(1)).await?; assert_eq!(pubdata, None); + let mut expected_pubdata: Vec = vec![]; let mut storage = pool.access_storage().await?; // Insertion - let header = create_l1_batch(1); + let mut header = create_l1_batch(1); + let l2_to_l1_log = L2ToL1Log { + shard_id: 1, + tx_number_in_block: 1, + is_service: false, + sender: Address::repeat_byte(1), + key: H256::repeat_byte(1), + value: H256::repeat_byte(1), + }; + let user_l2_to_l1_log = UserL2ToL1Log(l2_to_l1_log); + + header.l2_to_l1_logs.push(user_l2_to_l1_log.clone()); + expected_pubdata.extend((header.l2_to_l1_logs.len() as u32).to_be_bytes()); + expected_pubdata.extend(user_l2_to_l1_log.0.to_bytes()); + expected_pubdata.extend((header.l2_to_l1_messages.len() as u32).to_be_bytes()); + let metadata = L1BatchMetadata::default(); + expected_pubdata.extend((0 as u32).to_be_bytes()); + expected_pubdata.extend(metadata.clone().state_diffs_compressed); storage .blocks_dal() @@ -976,7 +996,7 @@ impl HttpTest for GetPubdataTest { .unwrap(); let pubdata = client.get_batch_pubdata(L1BatchNumber(1)).await?; - assert!(pubdata.is_some()); + assert_eq!(pubdata, Some(expected_pubdata.into())); Ok(()) } } From 438e7e323cbbed9323efe42305045c5880860b2f Mon Sep 17 00:00:00 2001 From: ilitteri Date: Wed, 13 Mar 2024 12:31:51 -0300 Subject: [PATCH 08/15] Make getBatchPubdata endpoint return an empty-bytes vector when there's no pubdata in the requested batch --- .../zksync_core/src/api_server/web3/namespaces/zks.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs index dff09efafce2..8d3b5ff73e01 100644 --- a/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs +++ b/core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs @@ -648,7 +648,16 @@ impl ZksNamespace { .get_l1_batch_metadata(l1_batch_number) .await .map_err(|err| internal_error(METHOD_NAME, err))? - .map(|l1_batch_with_metadata| l1_batch_with_metadata.construct_pubdata().into()); + .map(|l1_batch_with_metadata| { + let partial_pubdata = l1_batch_with_metadata.construct_pubdata(); + // The encoding of empty pubdata results in a vector of zeroes. We should return + // `None in this case. + if partial_pubdata == vec![0u8; partial_pubdata.len()] { + Bytes::default() + } else { + partial_pubdata.into() + } + }); method_latency.observe(); Ok(pubdata) From 24f1343089cbee71e7ddbcfecfd642884dbe7d4e Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Thu, 14 Mar 2024 10:40:00 +0100 Subject: [PATCH 09/15] remove imports --- core/lib/zksync_core/src/api_server/web3/tests/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs index b7d29db6afda..9a2a7debae4b 100644 --- a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs @@ -15,7 +15,7 @@ use zksync_health_check::CheckHealth; use zksync_types::{ api, block::MiniblockHeader, - commitment::{L1BatchMetadata, L1BatchWithMetadata}, + commitment::L1BatchMetadata, fee::TransactionExecutionMetrics, get_nonce_key, l2::L2Tx, @@ -27,8 +27,7 @@ use zksync_types::{ TransactionExecutionResult, }, utils::{storage_key_for_eth_balance, storage_key_for_standard_token_balance}, - AccountTreeId, Address, Bytes, L1BatchNumber, Nonce, StorageKey, StorageLog, VmEvent, H256, - U64, + AccountTreeId, Address, L1BatchNumber, Nonce, StorageKey, StorageLog, VmEvent, H256, U64, }; use zksync_utils::u256_to_h256; use zksync_web3_decl::{ From ec248626cb7290356c1c6bbd4ba77b6b99dbe84a Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Thu, 14 Mar 2024 10:59:42 +0100 Subject: [PATCH 10/15] make aggregated operations public --- core/lib/zksync_core/src/eth_sender/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/zksync_core/src/eth_sender/mod.rs b/core/lib/zksync_core/src/eth_sender/mod.rs index 109624e5a4e8..845b6b7e3a60 100644 --- a/core/lib/zksync_core/src/eth_sender/mod.rs +++ b/core/lib/zksync_core/src/eth_sender/mod.rs @@ -1,4 +1,4 @@ -mod aggregated_operations; +pub mod aggregated_operations; mod aggregator; mod error; mod eth_tx_aggregator; From f93dfd9528b8966ea1396a554cbbfc29246234ac Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Thu, 14 Mar 2024 11:00:36 +0100 Subject: [PATCH 11/15] make EthTxAggregator::save_eth_tx public to the crate --- core/lib/zksync_core/src/eth_sender/eth_tx_aggregator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/zksync_core/src/eth_sender/eth_tx_aggregator.rs b/core/lib/zksync_core/src/eth_sender/eth_tx_aggregator.rs index fa9bce07321e..6f655bc5550a 100644 --- a/core/lib/zksync_core/src/eth_sender/eth_tx_aggregator.rs +++ b/core/lib/zksync_core/src/eth_sender/eth_tx_aggregator.rs @@ -461,7 +461,7 @@ impl EthTxAggregator { } } - pub(super) async fn save_eth_tx( + pub(crate) async fn save_eth_tx( &self, storage: &mut StorageProcessor<'_>, aggregated_op: &AggregatedOperation, From 0d05f73fe3b4a1470554ccc316602ad58e1f3080 Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Thu, 14 Mar 2024 12:06:31 +0100 Subject: [PATCH 12/15] making test pass copying methods from EthSenderTester --- .../src/api_server/web3/tests/mod.rs | 208 ++++++++++++++---- 1 file changed, 168 insertions(+), 40 deletions(-) diff --git a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs index 9a2a7debae4b..9e92982af5c1 100644 --- a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs @@ -5,17 +5,23 @@ use async_trait::async_trait; use jsonrpsee::core::ClientError; use multivm::zk_evm_latest::ethereum_types::U256; use tokio::sync::watch; -use zksync_config::configs::{ - api::Web3JsonRpcConfig, - chain::{NetworkConfig, StateKeeperConfig}, - ContractsConfig, +use zksync_config::{ + configs::{ + api::Web3JsonRpcConfig, + chain::{NetworkConfig, StateKeeperConfig}, + eth_sender::{ProofSendingMode, SenderConfig}, + ContractsConfig, + }, + ETHSenderConfig, GasAdjusterConfig, }; use zksync_dal::{transactions_dal::L2TxSubmissionResult, ConnectionPool, StorageProcessor}; +use zksync_eth_client::clients::MockEthereum; use zksync_health_check::CheckHealth; +use zksync_object_store::ObjectStoreFactory; use zksync_types::{ api, - block::MiniblockHeader, - commitment::L1BatchMetadata, + block::{L1BatchHeader, MiniblockHeader}, + commitment::{L1BatchMetadata, L1BatchWithMetadata}, fee::TransactionExecutionMetrics, get_nonce_key, l2::L2Tx, @@ -27,7 +33,8 @@ use zksync_types::{ TransactionExecutionResult, }, utils::{storage_key_for_eth_balance, storage_key_for_standard_token_balance}, - AccountTreeId, Address, L1BatchNumber, Nonce, StorageKey, StorageLog, VmEvent, H256, U64, + AccountTreeId, Address, L1BatchNumber, L1BlockNumber, Nonce, StorageKey, StorageLog, VmEvent, + H256, U64, }; use zksync_utils::u256_to_h256; use zksync_web3_decl::{ @@ -41,7 +48,13 @@ use crate::{ execution_sandbox::testonly::MockTransactionExecutor, tx_sender::tests::create_test_tx_sender, }, + eth_sender::{ + aggregated_operations::AggregatedOperation, + l1_batch_commit_data_generator::RollupModeL1BatchCommitDataGenerator, Aggregator, + EthTxAggregator, EthTxManager, + }, genesis::{ensure_genesis_state, GenesisParams}, + l1_gas_price::GasAdjuster, utils::testonly::{ create_l1_batch, create_l1_batch_metadata, create_l2_transaction, create_miniblock, l1_batch_metadata_to_commitment_artifacts, prepare_recovery_snapshot, @@ -941,20 +954,147 @@ struct GetPubdataTest; impl GetPubdataTest {} -#[async_trait] -impl HttpTest for GetPubdataTest { - fn storage_initialization(&self) -> StorageInitialization { - StorageInitialization::Genesis +fn l1_batch_with_metadata(header: L1BatchHeader) -> L1BatchWithMetadata { + L1BatchWithMetadata { + header, + metadata: L1BatchMetadata::default(), + raw_published_factory_deps: vec![], } +} + +async fn insert_l1_batch( + storage: &mut StorageProcessor<'_>, + header: L1BatchHeader, +) -> L1BatchHeader { + // Save L1 batch to the database + storage + .blocks_dal() + .insert_mock_l1_batch(&header) + .await + .unwrap(); + let metadata = L1BatchMetadata::default(); + + storage + .blocks_dal() + .save_l1_batch_tree_data(header.number, &metadata.tree_data()) + .await + .unwrap(); + storage + .blocks_dal() + .save_l1_batch_commitment_artifacts( + header.number, + &l1_batch_metadata_to_commitment_artifacts(&metadata), + ) + .await + .unwrap(); + header +} + +async fn send_operation( + aggregator: &EthTxAggregator, + manager: &mut EthTxManager, + storage: &mut StorageProcessor<'_>, + aggregated_operation: AggregatedOperation, + current_block: L1BlockNumber, +) -> H256 { + let tx = aggregator + .save_eth_tx(storage, &aggregated_operation, true) + .await + .unwrap(); + + let hash = manager + .send_eth_tx(storage, &tx, 0, current_block) + .await + .unwrap(); + // if confirm { + // confirm_tx(tester, hash).await; + // } + hash +} + +async fn commit_l1_batch( + aggregator: &EthTxAggregator, + manager: &mut EthTxManager, + storage: &mut StorageProcessor<'_>, + last_committed_l1_batch: L1BatchHeader, + l1_batch: L1BatchHeader, + current_block: L1BlockNumber, +) -> H256 { + let operation = AggregatedOperation::Commit( + l1_batch_with_metadata(last_committed_l1_batch), + vec![l1_batch_with_metadata(l1_batch)], + ); + send_operation(aggregator, manager, storage, operation, current_block).await +} + +#[async_trait] +impl HttpTest for GetPubdataTest { async fn test(&self, client: &HttpClient, pool: &ConnectionPool) -> anyhow::Result<()> { let pubdata = client.get_batch_pubdata(L1BatchNumber(1)).await?; assert_eq!(pubdata, None); - let mut expected_pubdata: Vec = vec![]; - let mut storage = pool.access_storage().await?; + let eth_sender_config = ETHSenderConfig::for_tests(); + let aggregator_config = SenderConfig { + aggregated_proof_sizes: vec![1], + ..eth_sender_config.sender.clone() + }; + let contracts_config = ContractsConfig::for_tests(); + let store_factory = ObjectStoreFactory::mock(); + let gateway = Arc::new( + MockEthereum::default() + .with_fee_history( + std::iter::repeat(0) + .take(10 as usize) + .chain(vec![10; 100]) + .collect(), + ) + .with_non_ordering_confirmation(false) + .with_multicall_address(contracts_config.l1_multicall3_addr), + ); + gateway.advance_block_number(10); + + let gas_adjuster = Arc::new( + GasAdjuster::new( + gateway.clone(), + GasAdjusterConfig { + max_base_fee_samples: 3, + pricing_formula_parameter_a: 3.0, + pricing_formula_parameter_b: 2.0, + ..eth_sender_config.gas_adjuster + }, + ) + .await + .unwrap(), + ); + let aggregator = EthTxAggregator::new( + SenderConfig { + proof_sending_mode: ProofSendingMode::SkipEveryProof, + ..eth_sender_config.sender.clone() + }, + // Aggregator - unused + Aggregator::new( + aggregator_config.clone(), + store_factory.create_store().await, + Arc::new(RollupModeL1BatchCommitDataGenerator {}), + ), + gateway.clone(), + // zkSync contract address + Address::random(), + contracts_config.l1_multicall3_addr, + Address::random(), + Default::default(), + Arc::new(RollupModeL1BatchCommitDataGenerator {}), + ) + .await; + + let mut manager = EthTxManager::new( + eth_sender_config.sender, + gas_adjuster.clone(), + gateway.clone(), + ); - // Insertion + let mut storage = pool.access_storage().await?; let mut header = create_l1_batch(1); let l2_to_l1_log = L2ToL1Log { shard_id: 1, @@ -967,35 +1107,23 @@ impl HttpTest for GetPubdataTest { let user_l2_to_l1_log = UserL2ToL1Log(l2_to_l1_log); header.l2_to_l1_logs.push(user_l2_to_l1_log.clone()); - expected_pubdata.extend((header.l2_to_l1_logs.len() as u32).to_be_bytes()); - expected_pubdata.extend(user_l2_to_l1_log.0.to_bytes()); - expected_pubdata.extend((header.l2_to_l1_messages.len() as u32).to_be_bytes()); - - let metadata = L1BatchMetadata::default(); - expected_pubdata.extend((0 as u32).to_be_bytes()); - expected_pubdata.extend(metadata.clone().state_diffs_compressed); - storage - .blocks_dal() - .insert_mock_l1_batch(&header) - .await - .unwrap(); - storage - .blocks_dal() - .save_l1_batch_tree_data(header.number, &metadata.tree_data()) - .await - .unwrap(); - storage - .blocks_dal() - .save_l1_batch_commitment_artifacts( - header.number, - &l1_batch_metadata_to_commitment_artifacts(&metadata), - ) - .await - .unwrap(); + header = insert_l1_batch(&mut storage, header).await; + commit_l1_batch( + &aggregator, + &mut manager, + &mut storage, + header.clone(), + header.clone(), + L1BlockNumber(1), + ) + .await; let pubdata = client.get_batch_pubdata(L1BatchNumber(1)).await?; - assert_eq!(pubdata, Some(expected_pubdata.into())); + dbg!(pubdata.clone()); + assert!(pubdata.is_some()); + // assert_eq!(pubdata, Some(expected_pubdata.into())); + Ok(()) } } From 1e6fd30724b6d84ca9f6440e3f35d0c9084f12f0 Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Thu, 14 Mar 2024 13:31:39 +0100 Subject: [PATCH 13/15] refactor test --- .../src/api_server/web3/tests/mod.rs | 225 +++++++++++------- 1 file changed, 136 insertions(+), 89 deletions(-) diff --git a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs index 9e92982af5c1..02b1df118500 100644 --- a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs @@ -50,8 +50,10 @@ use crate::{ }, eth_sender::{ aggregated_operations::AggregatedOperation, - l1_batch_commit_data_generator::RollupModeL1BatchCommitDataGenerator, Aggregator, - EthTxAggregator, EthTxManager, + l1_batch_commit_data_generator::{ + RollupModeL1BatchCommitDataGenerator, ValidiumModeL1BatchCommitDataGenerator, + }, + Aggregator, EthTxAggregator, EthTxManager, }, genesis::{ensure_genesis_state, GenesisParams}, l1_gas_price::GasAdjuster, @@ -952,80 +954,138 @@ async fn getting_all_account_balances() { #[derive(Debug)] struct GetPubdataTest; -impl GetPubdataTest {} +impl GetPubdataTest { + const WAIT_CONFIRMATIONS: u64 = 10; + const MAX_BASE_FEE_SAMPLES: usize = 3; + + fn l1_batch_with_metadata(header: L1BatchHeader) -> L1BatchWithMetadata { + let mut metadata = L1BatchMetadata::default(); + metadata.state_diffs_compressed = Self::build_state_diffs_compressed(); + L1BatchWithMetadata { + header, + metadata: metadata, + raw_published_factory_deps: Self::build_raw_published_factory_deps(), + } + } + + async fn insert_l1_batch( + storage: &mut StorageProcessor<'_>, + number: L1BatchNumber, + ) -> L1BatchHeader { + let mut header = create_l1_batch(number.0); + header.l2_to_l1_logs = Self::build_l2_to_l1_logs(); + header.l2_to_l1_messages = Self::build_l2_to_l1_messages(); + // Save L1 batch to the database + storage + .blocks_dal() + .insert_mock_l1_batch(&header) + .await + .unwrap(); + let metadata = L1BatchMetadata::default(); -fn l1_batch_with_metadata(header: L1BatchHeader) -> L1BatchWithMetadata { - L1BatchWithMetadata { - header, - metadata: L1BatchMetadata::default(), - raw_published_factory_deps: vec![], + storage + .blocks_dal() + .save_l1_batch_tree_data(header.number, &metadata.tree_data()) + .await + .unwrap(); + storage + .blocks_dal() + .save_l1_batch_commitment_artifacts( + header.number, + &l1_batch_metadata_to_commitment_artifacts(&metadata), + ) + .await + .unwrap(); + header } -} -async fn insert_l1_batch( - storage: &mut StorageProcessor<'_>, - header: L1BatchHeader, -) -> L1BatchHeader { - // Save L1 batch to the database - storage - .blocks_dal() - .insert_mock_l1_batch(&header) - .await - .unwrap(); - let metadata = L1BatchMetadata::default(); + async fn send_operation( + aggregator: &EthTxAggregator, + manager: &mut EthTxManager, + storage: &mut StorageProcessor<'_>, + aggregated_operation: AggregatedOperation, + current_block: L1BlockNumber, + ) -> H256 { + let tx = aggregator + .save_eth_tx(storage, &aggregated_operation, true) + .await + .unwrap(); - storage - .blocks_dal() - .save_l1_batch_tree_data(header.number, &metadata.tree_data()) - .await - .unwrap(); - storage - .blocks_dal() - .save_l1_batch_commitment_artifacts( - header.number, - &l1_batch_metadata_to_commitment_artifacts(&metadata), - ) - .await - .unwrap(); - header -} + let hash = manager + .send_eth_tx(storage, &tx, 0, current_block) + .await + .unwrap(); -async fn send_operation( - aggregator: &EthTxAggregator, - manager: &mut EthTxManager, - storage: &mut StorageProcessor<'_>, - aggregated_operation: AggregatedOperation, - current_block: L1BlockNumber, -) -> H256 { - let tx = aggregator - .save_eth_tx(storage, &aggregated_operation, true) - .await - .unwrap(); + hash + } - let hash = manager - .send_eth_tx(storage, &tx, 0, current_block) - .await - .unwrap(); + async fn commit_l1_batch( + aggregator: &EthTxAggregator, + manager: &mut EthTxManager, + storage: &mut StorageProcessor<'_>, + last_committed_l1_batch: L1BatchHeader, + l1_batch: L1BatchHeader, + current_block: L1BlockNumber, + ) -> H256 { + let operation = AggregatedOperation::Commit( + Self::l1_batch_with_metadata(last_committed_l1_batch), + vec![Self::l1_batch_with_metadata(l1_batch)], + ); + Self::send_operation(aggregator, manager, storage, operation, current_block).await + } - // if confirm { - // confirm_tx(tester, hash).await; - // } - hash -} + fn build_l2_to_l1_logs() -> Vec { + let l2_to_l1_log = L2ToL1Log { + shard_id: 1, + tx_number_in_block: 1, + is_service: false, + sender: Address::repeat_byte(1), + key: H256::repeat_byte(1), + value: H256::repeat_byte(1), + }; + let user_l2_to_l1_log = UserL2ToL1Log(l2_to_l1_log); -async fn commit_l1_batch( - aggregator: &EthTxAggregator, - manager: &mut EthTxManager, - storage: &mut StorageProcessor<'_>, - last_committed_l1_batch: L1BatchHeader, - l1_batch: L1BatchHeader, - current_block: L1BlockNumber, -) -> H256 { - let operation = AggregatedOperation::Commit( - l1_batch_with_metadata(last_committed_l1_batch), - vec![l1_batch_with_metadata(l1_batch)], - ); - send_operation(aggregator, manager, storage, operation, current_block).await + vec![user_l2_to_l1_log] + } + + fn build_l2_to_l1_messages() -> Vec> { + vec![vec![1, 2, 3]] + } + + fn build_raw_published_factory_deps() -> Vec> { + vec![vec![1, 2, 3]] + } + + fn build_state_diffs_compressed() -> Vec { + vec![1, 2, 3] + } + + fn build_expected_pubdata() -> Vec { + let l2_to_l1_logs = Self::build_l2_to_l1_logs(); + let l2_to_l1_messages = Self::build_l2_to_l1_messages(); + let raw_published_factory_deps = Self::build_raw_published_factory_deps(); + let state_diffs_compressed = Self::build_state_diffs_compressed(); + + let mut expected_pubdata = vec![]; + expected_pubdata.extend((l2_to_l1_logs.len() as u32).to_be_bytes()); + for l2_to_l1_log in &l2_to_l1_logs { + expected_pubdata.extend(l2_to_l1_log.0.to_bytes()); + } + expected_pubdata.extend((l2_to_l1_messages.len() as u32).to_be_bytes()); + for msg in &l2_to_l1_messages { + expected_pubdata.extend((msg.len() as u32).to_be_bytes()); + expected_pubdata.extend(msg); + } + expected_pubdata.extend((raw_published_factory_deps.len() as u32).to_be_bytes()); + for bytecode in &raw_published_factory_deps { + expected_pubdata.extend((bytecode.len() as u32).to_be_bytes()); + expected_pubdata.extend(bytecode); + } + dbg!(expected_pubdata.clone()); + expected_pubdata.extend(&state_diffs_compressed); + + expected_pubdata + } } #[async_trait] @@ -1045,20 +1105,20 @@ impl HttpTest for GetPubdataTest { MockEthereum::default() .with_fee_history( std::iter::repeat(0) - .take(10 as usize) + .take(Self::WAIT_CONFIRMATIONS as usize) .chain(vec![10; 100]) .collect(), ) .with_non_ordering_confirmation(false) .with_multicall_address(contracts_config.l1_multicall3_addr), ); - gateway.advance_block_number(10); + gateway.advance_block_number(Self::WAIT_CONFIRMATIONS); let gas_adjuster = Arc::new( GasAdjuster::new( gateway.clone(), GasAdjusterConfig { - max_base_fee_samples: 3, + max_base_fee_samples: Self::MAX_BASE_FEE_SAMPLES, pricing_formula_parameter_a: 3.0, pricing_formula_parameter_b: 2.0, ..eth_sender_config.gas_adjuster @@ -1094,22 +1154,11 @@ impl HttpTest for GetPubdataTest { gateway.clone(), ); + let block_number = L1BatchNumber(1); let mut storage = pool.access_storage().await?; - let mut header = create_l1_batch(1); - let l2_to_l1_log = L2ToL1Log { - shard_id: 1, - tx_number_in_block: 1, - is_service: false, - sender: Address::repeat_byte(1), - key: H256::repeat_byte(1), - value: H256::repeat_byte(1), - }; - let user_l2_to_l1_log = UserL2ToL1Log(l2_to_l1_log); - header.l2_to_l1_logs.push(user_l2_to_l1_log.clone()); - - header = insert_l1_batch(&mut storage, header).await; - commit_l1_batch( + let header = Self::insert_l1_batch(&mut storage, block_number).await; + Self::commit_l1_batch( &aggregator, &mut manager, &mut storage, @@ -1119,10 +1168,8 @@ impl HttpTest for GetPubdataTest { ) .await; - let pubdata = client.get_batch_pubdata(L1BatchNumber(1)).await?; - dbg!(pubdata.clone()); - assert!(pubdata.is_some()); - // assert_eq!(pubdata, Some(expected_pubdata.into())); + let pubdata = client.get_batch_pubdata(block_number).await?; + assert_eq!(pubdata, Some(Self::build_expected_pubdata().into())); Ok(()) } From 59f5686543a23cd097f1538bdc0bd8feca42eb72 Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Thu, 14 Mar 2024 18:27:10 +0100 Subject: [PATCH 14/15] use construct_pubdata for expected_pubdata --- .../src/api_server/web3/tests/mod.rs | 82 ++++++++++--------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs index 02b1df118500..3d75ef167555 100644 --- a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs @@ -15,7 +15,7 @@ use zksync_config::{ ETHSenderConfig, GasAdjusterConfig, }; use zksync_dal::{transactions_dal::L2TxSubmissionResult, ConnectionPool, StorageProcessor}; -use zksync_eth_client::clients::MockEthereum; +use zksync_eth_client::{clients::MockEthereum, EthInterface}; use zksync_health_check::CheckHealth; use zksync_object_store::ObjectStoreFactory; use zksync_types::{ @@ -33,8 +33,8 @@ use zksync_types::{ TransactionExecutionResult, }, utils::{storage_key_for_eth_balance, storage_key_for_standard_token_balance}, - AccountTreeId, Address, L1BatchNumber, L1BlockNumber, Nonce, StorageKey, StorageLog, VmEvent, - H256, U64, + AccountTreeId, Address, Bytes, L1BatchNumber, L1BlockNumber, Nonce, StorageKey, StorageLog, + VmEvent, H256, U64, }; use zksync_utils::u256_to_h256; use zksync_web3_decl::{ @@ -1060,32 +1060,37 @@ impl GetPubdataTest { vec![1, 2, 3] } - fn build_expected_pubdata() -> Vec { - let l2_to_l1_logs = Self::build_l2_to_l1_logs(); - let l2_to_l1_messages = Self::build_l2_to_l1_messages(); - let raw_published_factory_deps = Self::build_raw_published_factory_deps(); - let state_diffs_compressed = Self::build_state_diffs_compressed(); - - let mut expected_pubdata = vec![]; - expected_pubdata.extend((l2_to_l1_logs.len() as u32).to_be_bytes()); - for l2_to_l1_log in &l2_to_l1_logs { - expected_pubdata.extend(l2_to_l1_log.0.to_bytes()); - } - expected_pubdata.extend((l2_to_l1_messages.len() as u32).to_be_bytes()); - for msg in &l2_to_l1_messages { - expected_pubdata.extend((msg.len() as u32).to_be_bytes()); - expected_pubdata.extend(msg); - } - expected_pubdata.extend((raw_published_factory_deps.len() as u32).to_be_bytes()); - for bytecode in &raw_published_factory_deps { - expected_pubdata.extend((bytecode.len() as u32).to_be_bytes()); - expected_pubdata.extend(bytecode); - } - dbg!(expected_pubdata.clone()); - expected_pubdata.extend(&state_diffs_compressed); - - expected_pubdata - } + // fn build_expected_pubdata(l1_batch_with_metadata: L1BatchWithMetadata) -> Vec { + // let l2_to_l1_logs = Self::build_l2_to_l1_logs(); + // let l2_to_l1_messages = Self::build_l2_to_l1_messages(); + // let raw_published_factory_deps = Self::build_raw_published_factory_deps(); + // let state_diffs_compressed = Self::build_state_diffs_compressed(); + + // let mut expected_pubdata = vec![]; + // expected_pubdata.extend((l2_to_l1_logs.len() as u32).to_be_bytes()); + // for l2_to_l1_log in &l2_to_l1_logs { + // expected_pubdata.extend(l2_to_l1_log.0.to_bytes()); + // } + // //dbg!(&expected_pubdata.len()); + // expected_pubdata.extend((l2_to_l1_messages.len() as u32).to_be_bytes()); + // for msg in &l2_to_l1_messages { + // expected_pubdata.extend((msg.len() as u32).to_be_bytes()); + // expected_pubdata.extend(msg); + // } + // //dbg!(&expected_pubdata.len()); + + // expected_pubdata.extend((raw_published_factory_deps.len() as u32).to_be_bytes()); + // for bytecode in &raw_published_factory_deps { + // expected_pubdata.extend((bytecode.len() as u32).to_be_bytes()); + // expected_pubdata.extend(bytecode); + // } + // //dbg!(&expected_pubdata.len()); + // expected_pubdata.extend(&state_diffs_compressed); + + // //dbg!(&expected_pubdata.len()); + + // expected_pubdata + // } } #[async_trait] @@ -1153,23 +1158,26 @@ impl HttpTest for GetPubdataTest { gas_adjuster.clone(), gateway.clone(), ); - - let block_number = L1BatchNumber(1); let mut storage = pool.access_storage().await?; - let header = Self::insert_l1_batch(&mut storage, block_number).await; + let genesis_l1_batch = create_l1_batch(0); + let first_l1_batch = Self::insert_l1_batch(&mut storage, L1BatchNumber(1)).await; + Self::commit_l1_batch( &aggregator, &mut manager, &mut storage, - header.clone(), - header.clone(), - L1BlockNumber(1), + genesis_l1_batch.clone(), + first_l1_batch.clone(), + L1BlockNumber(gateway.block_number("").await.unwrap().as_u32()), ) .await; - let pubdata = client.get_batch_pubdata(block_number).await?; - assert_eq!(pubdata, Some(Self::build_expected_pubdata().into())); + let l1_batch_with_metadata = Self::l1_batch_with_metadata(first_l1_batch); + let expected_pubdata: Bytes = l1_batch_with_metadata.construct_pubdata().into(); + let pubdata = client.get_batch_pubdata(L1BatchNumber(1)).await?; + + assert_eq!(pubdata, Some(expected_pubdata)); Ok(()) } From 421897f8c817df857ec882c1fb232d5fb768d2f0 Mon Sep 17 00:00:00 2001 From: toni-calvin Date: Fri, 15 Mar 2024 15:56:12 +0100 Subject: [PATCH 15/15] Simplify test and make it pass --- .../structures/commit_batch_info.rs | 2 +- core/lib/types/src/commitment/mod.rs | 27 +- .../src/api_server/web3/tests/mod.rs | 231 +++--------------- .../src/api_server/web3/tests/vm.rs | 1 + .../src/eth_sender/eth_tx_aggregator.rs | 2 +- core/lib/zksync_core/src/eth_sender/mod.rs | 2 +- .../src/eth_sender/tests_helpers.rs | 32 ++- 7 files changed, 72 insertions(+), 225 deletions(-) diff --git a/core/lib/l1_contract_interface/src/i_executor/structures/commit_batch_info.rs b/core/lib/l1_contract_interface/src/i_executor/structures/commit_batch_info.rs index 0ea8a627b654..528daa34caca 100644 --- a/core/lib/l1_contract_interface/src/i_executor/structures/commit_batch_info.rs +++ b/core/lib/l1_contract_interface/src/i_executor/structures/commit_batch_info.rs @@ -51,7 +51,7 @@ impl<'a> Tokenizable for CommitBatchInfoRollup<'a> { Token::Tuple(encode_l1_commit( &self.l1_batch_with_metadata.header, &self.l1_batch_with_metadata.metadata, - Some(&self.l1_batch_with_metadata), + Some(self.l1_batch_with_metadata), )) } } diff --git a/core/lib/types/src/commitment/mod.rs b/core/lib/types/src/commitment/mod.rs index 65280b4991d1..a65faca0e50c 100644 --- a/core/lib/types/src/commitment/mod.rs +++ b/core/lib/types/src/commitment/mod.rs @@ -99,31 +99,6 @@ impl L1BatchMetadata { } } -impl Default for L1BatchMetadata { - fn default() -> Self { - L1BatchMetadata { - root_hash: Default::default(), - rollup_last_leaf_index: 0, - merkle_root_hash: Default::default(), - initial_writes_compressed: Some(vec![]), - repeated_writes_compressed: Some(vec![]), - commitment: Default::default(), - l2_l1_merkle_root: Default::default(), - block_meta_params: L1BatchMetaParameters { - zkporter_is_available: false, - bootloader_code_hash: Default::default(), - default_aa_code_hash: Default::default(), - }, - aux_data_hash: Default::default(), - meta_parameters_hash: Default::default(), - pass_through_data_hash: Default::default(), - events_queue_commitment: Some(H256::zero()), - bootloader_initial_content_commitment: Some(H256::zero()), - state_diffs_compressed: vec![], - } - } -} - #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct L1BatchWithMetadata { pub header: L1BatchHeader, @@ -471,7 +446,7 @@ impl L1BatchAuxiliaryOutput { } /// Meta parameters for an L1 batch. They are the same for each L1 batch per run. -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct L1BatchMetaParameters { pub zkporter_is_available: bool, pub bootloader_code_hash: H256, diff --git a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs index 3d75ef167555..4477d3e29c89 100644 --- a/core/lib/zksync_core/src/api_server/web3/tests/mod.rs +++ b/core/lib/zksync_core/src/api_server/web3/tests/mod.rs @@ -5,19 +5,9 @@ use async_trait::async_trait; use jsonrpsee::core::ClientError; use multivm::zk_evm_latest::ethereum_types::U256; use tokio::sync::watch; -use zksync_config::{ - configs::{ - api::Web3JsonRpcConfig, - chain::{NetworkConfig, StateKeeperConfig}, - eth_sender::{ProofSendingMode, SenderConfig}, - ContractsConfig, - }, - ETHSenderConfig, GasAdjusterConfig, -}; +use zksync_config::configs::{api::Web3JsonRpcConfig, chain::NetworkConfig, ContractsConfig}; use zksync_dal::{transactions_dal::L2TxSubmissionResult, ConnectionPool, StorageProcessor}; -use zksync_eth_client::{clients::MockEthereum, EthInterface}; use zksync_health_check::CheckHealth; -use zksync_object_store::ObjectStoreFactory; use zksync_types::{ api, block::{L1BatchHeader, MiniblockHeader}, @@ -33,8 +23,8 @@ use zksync_types::{ TransactionExecutionResult, }, utils::{storage_key_for_eth_balance, storage_key_for_standard_token_balance}, - AccountTreeId, Address, Bytes, L1BatchNumber, L1BlockNumber, Nonce, StorageKey, StorageLog, - VmEvent, H256, U64, + AccountTreeId, Address, Bytes, L1BatchNumber, Nonce, StorageKey, StorageLog, VmEvent, H256, + U64, }; use zksync_utils::u256_to_h256; use zksync_web3_decl::{ @@ -48,15 +38,7 @@ use crate::{ execution_sandbox::testonly::MockTransactionExecutor, tx_sender::tests::create_test_tx_sender, }, - eth_sender::{ - aggregated_operations::AggregatedOperation, - l1_batch_commit_data_generator::{ - RollupModeL1BatchCommitDataGenerator, ValidiumModeL1BatchCommitDataGenerator, - }, - Aggregator, EthTxAggregator, EthTxManager, - }, genesis::{ensure_genesis_state, GenesisParams}, - l1_gas_price::GasAdjuster, utils::testonly::{ create_l1_batch, create_l1_batch_metadata, create_l2_transaction, create_miniblock, l1_batch_metadata_to_commitment_artifacts, prepare_recovery_snapshot, @@ -955,83 +937,45 @@ async fn getting_all_account_balances() { struct GetPubdataTest; impl GetPubdataTest { - const WAIT_CONFIRMATIONS: u64 = 10; - const MAX_BASE_FEE_SAMPLES: usize = 3; - - fn l1_batch_with_metadata(header: L1BatchHeader) -> L1BatchWithMetadata { - let mut metadata = L1BatchMetadata::default(); - metadata.state_diffs_compressed = Self::build_state_diffs_compressed(); - L1BatchWithMetadata { - header, - metadata: metadata, - raw_published_factory_deps: Self::build_raw_published_factory_deps(), - } - } - - async fn insert_l1_batch( + async fn insert_l1_batch_with_metadata( storage: &mut StorageProcessor<'_>, - number: L1BatchNumber, + l1_batch_header: L1BatchHeader, + l1_batch_metadata: L1BatchMetadata, ) -> L1BatchHeader { - let mut header = create_l1_batch(number.0); - header.l2_to_l1_logs = Self::build_l2_to_l1_logs(); - header.l2_to_l1_messages = Self::build_l2_to_l1_messages(); // Save L1 batch to the database storage .blocks_dal() - .insert_mock_l1_batch(&header) + .insert_mock_l1_batch(&l1_batch_header) .await .unwrap(); - let metadata = L1BatchMetadata::default(); storage .blocks_dal() - .save_l1_batch_tree_data(header.number, &metadata.tree_data()) + .save_l1_batch_tree_data(l1_batch_header.number, &l1_batch_metadata.tree_data()) .await .unwrap(); storage .blocks_dal() .save_l1_batch_commitment_artifacts( - header.number, - &l1_batch_metadata_to_commitment_artifacts(&metadata), + l1_batch_header.number, + &l1_batch_metadata_to_commitment_artifacts(&l1_batch_metadata), ) .await .unwrap(); - header - } - async fn send_operation( - aggregator: &EthTxAggregator, - manager: &mut EthTxManager, - storage: &mut StorageProcessor<'_>, - aggregated_operation: AggregatedOperation, - current_block: L1BlockNumber, - ) -> H256 { - let tx = aggregator - .save_eth_tx(storage, &aggregated_operation, true) + storage + .blocks_dal() + .insert_miniblock(&create_miniblock(l1_batch_header.number.0)) .await .unwrap(); - let hash = manager - .send_eth_tx(storage, &tx, 0, current_block) + storage + .blocks_dal() + .mark_miniblocks_as_executed_in_l1_batch(l1_batch_header.number) .await .unwrap(); - hash - } - - async fn commit_l1_batch( - aggregator: &EthTxAggregator, - manager: &mut EthTxManager, - storage: &mut StorageProcessor<'_>, - last_committed_l1_batch: L1BatchHeader, - l1_batch: L1BatchHeader, - current_block: L1BlockNumber, - ) -> H256 { - let operation = AggregatedOperation::Commit( - Self::l1_batch_with_metadata(last_committed_l1_batch), - vec![Self::l1_batch_with_metadata(l1_batch)], - ); - Self::send_operation(aggregator, manager, storage, operation, current_block).await + l1_batch_header } fn build_l2_to_l1_logs() -> Vec { @@ -1047,136 +991,41 @@ impl GetPubdataTest { vec![user_l2_to_l1_log] } - - fn build_l2_to_l1_messages() -> Vec> { - vec![vec![1, 2, 3]] - } - - fn build_raw_published_factory_deps() -> Vec> { - vec![vec![1, 2, 3]] - } - - fn build_state_diffs_compressed() -> Vec { - vec![1, 2, 3] - } - - // fn build_expected_pubdata(l1_batch_with_metadata: L1BatchWithMetadata) -> Vec { - // let l2_to_l1_logs = Self::build_l2_to_l1_logs(); - // let l2_to_l1_messages = Self::build_l2_to_l1_messages(); - // let raw_published_factory_deps = Self::build_raw_published_factory_deps(); - // let state_diffs_compressed = Self::build_state_diffs_compressed(); - - // let mut expected_pubdata = vec![]; - // expected_pubdata.extend((l2_to_l1_logs.len() as u32).to_be_bytes()); - // for l2_to_l1_log in &l2_to_l1_logs { - // expected_pubdata.extend(l2_to_l1_log.0.to_bytes()); - // } - // //dbg!(&expected_pubdata.len()); - // expected_pubdata.extend((l2_to_l1_messages.len() as u32).to_be_bytes()); - // for msg in &l2_to_l1_messages { - // expected_pubdata.extend((msg.len() as u32).to_be_bytes()); - // expected_pubdata.extend(msg); - // } - // //dbg!(&expected_pubdata.len()); - - // expected_pubdata.extend((raw_published_factory_deps.len() as u32).to_be_bytes()); - // for bytecode in &raw_published_factory_deps { - // expected_pubdata.extend((bytecode.len() as u32).to_be_bytes()); - // expected_pubdata.extend(bytecode); - // } - // //dbg!(&expected_pubdata.len()); - // expected_pubdata.extend(&state_diffs_compressed); - - // //dbg!(&expected_pubdata.len()); - - // expected_pubdata - // } } #[async_trait] impl HttpTest for GetPubdataTest { async fn test(&self, client: &HttpClient, pool: &ConnectionPool) -> anyhow::Result<()> { - let pubdata = client.get_batch_pubdata(L1BatchNumber(1)).await?; + let l1_batch_number = L1BatchNumber(1); + // Assert pubdata is None if L1BatchWithMetadata is not found + let pubdata = client.get_batch_pubdata(l1_batch_number).await?; assert_eq!(pubdata, None); - let eth_sender_config = ETHSenderConfig::for_tests(); - let aggregator_config = SenderConfig { - aggregated_proof_sizes: vec![1], - ..eth_sender_config.sender.clone() - }; - let contracts_config = ContractsConfig::for_tests(); - let store_factory = ObjectStoreFactory::mock(); - let gateway = Arc::new( - MockEthereum::default() - .with_fee_history( - std::iter::repeat(0) - .take(Self::WAIT_CONFIRMATIONS as usize) - .chain(vec![10; 100]) - .collect(), - ) - .with_non_ordering_confirmation(false) - .with_multicall_address(contracts_config.l1_multicall3_addr), - ); - gateway.advance_block_number(Self::WAIT_CONFIRMATIONS); - - let gas_adjuster = Arc::new( - GasAdjuster::new( - gateway.clone(), - GasAdjusterConfig { - max_base_fee_samples: Self::MAX_BASE_FEE_SAMPLES, - pricing_formula_parameter_a: 3.0, - pricing_formula_parameter_b: 2.0, - ..eth_sender_config.gas_adjuster - }, - ) - .await - .unwrap(), - ); - let aggregator = EthTxAggregator::new( - SenderConfig { - proof_sending_mode: ProofSendingMode::SkipEveryProof, - ..eth_sender_config.sender.clone() - }, - // Aggregator - unused - Aggregator::new( - aggregator_config.clone(), - store_factory.create_store().await, - Arc::new(RollupModeL1BatchCommitDataGenerator {}), - ), - gateway.clone(), - // zkSync contract address - Address::random(), - contracts_config.l1_multicall3_addr, - Address::random(), - Default::default(), - Arc::new(RollupModeL1BatchCommitDataGenerator {}), - ) - .await; + // Assert pubdata is expected if L1BatchWithMetadata is found + // Create L1 batch header and add pubdata (l2_to_l1_logs, l2_to_l1_messages) + let mut l1_batch_header: L1BatchHeader = create_l1_batch(l1_batch_number.0); + l1_batch_header.l2_to_l1_logs = Self::build_l2_to_l1_logs(); + l1_batch_header.l2_to_l1_messages = vec![vec![1, 2, 3]]; - let mut manager = EthTxManager::new( - eth_sender_config.sender, - gas_adjuster.clone(), - gateway.clone(), - ); - let mut storage = pool.access_storage().await?; + // Create metadata + let l1_batch_metadata: L1BatchMetadata = create_l1_batch_metadata(l1_batch_number.0); - let genesis_l1_batch = create_l1_batch(0); - let first_l1_batch = Self::insert_l1_batch(&mut storage, L1BatchNumber(1)).await; - - Self::commit_l1_batch( - &aggregator, - &mut manager, - &mut storage, - genesis_l1_batch.clone(), - first_l1_batch.clone(), - L1BlockNumber(gateway.block_number("").await.unwrap().as_u32()), - ) - .await; + // Build L1BatchWithMetadata + let l1_batch_with_metadata = L1BatchWithMetadata { + header: l1_batch_header.clone(), + metadata: l1_batch_metadata.clone(), + raw_published_factory_deps: vec![], + }; - let l1_batch_with_metadata = Self::l1_batch_with_metadata(first_l1_batch); let expected_pubdata: Bytes = l1_batch_with_metadata.construct_pubdata().into(); - let pubdata = client.get_batch_pubdata(L1BatchNumber(1)).await?; + let mut storage = pool.access_storage().await?; + Self::insert_l1_batch_with_metadata(&mut storage, l1_batch_header, l1_batch_metadata).await; + + // Call the endpoint + let pubdata = client.get_batch_pubdata(l1_batch_number).await?; + + // Assert pubdata is equal to expected pubdata assert_eq!(pubdata, Some(expected_pubdata)); Ok(()) diff --git a/core/lib/zksync_core/src/api_server/web3/tests/vm.rs b/core/lib/zksync_core/src/api_server/web3/tests/vm.rs index 22760c37feb1..f91c95081f84 100644 --- a/core/lib/zksync_core/src/api_server/web3/tests/vm.rs +++ b/core/lib/zksync_core/src/api_server/web3/tests/vm.rs @@ -3,6 +3,7 @@ use std::sync::atomic::{AtomicU32, Ordering}; use multivm::interface::{ExecutionResult, VmRevertReason}; +use zksync_config::configs::chain::StateKeeperConfig; use zksync_types::{ get_intrinsic_constants, transaction_request::CallRequest, L2ChainId, PackedEthSignature, U256, }; diff --git a/core/lib/zksync_core/src/eth_sender/eth_tx_aggregator.rs b/core/lib/zksync_core/src/eth_sender/eth_tx_aggregator.rs index 6f655bc5550a..fa9bce07321e 100644 --- a/core/lib/zksync_core/src/eth_sender/eth_tx_aggregator.rs +++ b/core/lib/zksync_core/src/eth_sender/eth_tx_aggregator.rs @@ -461,7 +461,7 @@ impl EthTxAggregator { } } - pub(crate) async fn save_eth_tx( + pub(super) async fn save_eth_tx( &self, storage: &mut StorageProcessor<'_>, aggregated_op: &AggregatedOperation, diff --git a/core/lib/zksync_core/src/eth_sender/mod.rs b/core/lib/zksync_core/src/eth_sender/mod.rs index 845b6b7e3a60..109624e5a4e8 100644 --- a/core/lib/zksync_core/src/eth_sender/mod.rs +++ b/core/lib/zksync_core/src/eth_sender/mod.rs @@ -1,4 +1,4 @@ -pub mod aggregated_operations; +mod aggregated_operations; mod aggregator; mod error; mod eth_tx_aggregator; diff --git a/core/lib/zksync_core/src/eth_sender/tests_helpers.rs b/core/lib/zksync_core/src/eth_sender/tests_helpers.rs index 107816658e7b..3a0e73c46398 100644 --- a/core/lib/zksync_core/src/eth_sender/tests_helpers.rs +++ b/core/lib/zksync_core/src/eth_sender/tests_helpers.rs @@ -12,7 +12,7 @@ use zksync_l1_contract_interface::i_executor::methods::{ExecuteBatches, ProveBat use zksync_object_store::ObjectStoreFactory; use zksync_types::{ block::L1BatchHeader, - commitment::{L1BatchMetadata, L1BatchWithMetadata}, + commitment::{L1BatchMetaParameters, L1BatchMetadata, L1BatchWithMetadata}, ethabi::Token, helpers::unix_timestamp_ms, web3::contract::Error, @@ -36,7 +36,7 @@ static DUMMY_OPERATION: Lazy = Lazy::new(|| { AggregatedOperation::Execute(ExecuteBatches { l1_batches: vec![L1BatchWithMetadata { header: create_l1_batch(1), - metadata: L1BatchMetadata::default(), + metadata: default_l1_batch_metadata(), raw_published_factory_deps: Vec::new(), }], }) @@ -149,11 +149,34 @@ impl EthSenderTester { fn l1_batch_with_metadata(header: L1BatchHeader) -> L1BatchWithMetadata { L1BatchWithMetadata { header, - metadata: L1BatchMetadata::default(), + metadata: default_l1_batch_metadata(), raw_published_factory_deps: vec![], } } +fn default_l1_batch_metadata() -> L1BatchMetadata { + L1BatchMetadata { + root_hash: Default::default(), + rollup_last_leaf_index: 0, + merkle_root_hash: Default::default(), + initial_writes_compressed: Some(vec![]), + repeated_writes_compressed: Some(vec![]), + commitment: Default::default(), + l2_l1_merkle_root: Default::default(), + block_meta_params: L1BatchMetaParameters { + zkporter_is_available: false, + bootloader_code_hash: Default::default(), + default_aa_code_hash: Default::default(), + }, + aux_data_hash: Default::default(), + meta_parameters_hash: Default::default(), + pass_through_data_hash: Default::default(), + events_queue_commitment: Some(H256::zero()), + bootloader_initial_content_commitment: Some(H256::zero()), + state_diffs_compressed: vec![], + } +} + pub(crate) async fn confirm_many(tester: &mut EthSenderTester) -> anyhow::Result<()> { let mut hashes = vec![]; @@ -594,8 +617,7 @@ async fn insert_l1_batch(tester: &EthSenderTester, number: L1BatchNumber) -> L1B .insert_mock_l1_batch(&header) .await .unwrap(); - let metadata = L1BatchMetadata::default(); - + let metadata = default_l1_batch_metadata(); tester .storage() .await