Skip to content

Commit

Permalink
Simplify test and make it pass
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-calvin committed Mar 15, 2024
1 parent 59f5686 commit 421897f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
))
}
}
Expand Down
27 changes: 1 addition & 26 deletions core/lib/types/src/commitment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
231 changes: 40 additions & 191 deletions core/lib/zksync_core/src/api_server/web3/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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::{
Expand All @@ -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,
Expand Down Expand Up @@ -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<UserL2ToL1Log> {
Expand All @@ -1047,136 +991,41 @@ impl GetPubdataTest {

vec![user_l2_to_l1_log]
}

fn build_l2_to_l1_messages() -> Vec<Vec<u8>> {
vec![vec![1, 2, 3]]
}

fn build_raw_published_factory_deps() -> Vec<Vec<u8>> {
vec![vec![1, 2, 3]]
}

fn build_state_diffs_compressed() -> Vec<u8> {
vec![1, 2, 3]
}

// fn build_expected_pubdata(l1_batch_with_metadata: L1BatchWithMetadata) -> Vec<u8> {
// 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(())
Expand Down
1 change: 1 addition & 0 deletions core/lib/zksync_core/src/api_server/web3/tests/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/eth_sender/eth_tx_aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/eth_sender/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod aggregated_operations;
mod aggregated_operations;
mod aggregator;
mod error;
mod eth_tx_aggregator;
Expand Down
Loading

0 comments on commit 421897f

Please sign in to comment.