Skip to content

Commit

Permalink
witness builder, circuit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Nov 23, 2020
1 parent 27fde65 commit 5484b69
Show file tree
Hide file tree
Showing 27 changed files with 813 additions and 610 deletions.
2 changes: 2 additions & 0 deletions core/bin/data_restore/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ fn create_block(block_number: u32, transactions: Vec<ExecutedOperations>) -> Blo
100,
1_000_000.into(),
1_500_000.into(),
H256::default(),
0,
)
}

Expand Down
10 changes: 8 additions & 2 deletions core/bin/prover/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn new_test_data_for_prover() -> ProverData {
let fee_account = Account::default_with_address(&Address::default());
circuit_account_tree.insert(fee_account_id, CircuitAccount::from(fee_account));

let mut witness_accum = WitnessBuilder::new(&mut circuit_account_tree, fee_account_id, 1);
let mut witness_accum = WitnessBuilder::new(&mut circuit_account_tree, fee_account_id, 1, 0);

let empty_account_id = 1;
let empty_account_address = [7u8; 20].into();
Expand All @@ -144,8 +144,13 @@ fn new_test_data_for_prover() -> ProverData {
let deposit_witness = DepositWitness::apply_tx(&mut witness_accum.account_tree, &deposit_op);
let deposit_operations = deposit_witness.calculate_operations(());
let pub_data_from_witness = deposit_witness.get_pubdata();
let offset_commitment = deposit_witness.get_offset_commitment_data();

witness_accum.add_operation_with_pubdata(deposit_operations, pub_data_from_witness);
witness_accum.add_operation_with_pubdata(
deposit_operations,
pub_data_from_witness,
offset_commitment,
);
witness_accum.extend_pubdata_with_noops(smallest_block_size_for_chunks(
DepositOp::CHUNKS,
&ConfigurationOptions::from_env().available_block_chunk_sizes,
Expand All @@ -164,6 +169,7 @@ fn new_test_data_for_prover() -> ProverData {
validator_balances: witness_accum.fee_account_balances.unwrap(),
validator_audit_path: witness_accum.fee_account_audit_path.unwrap(),
validator_account: witness_accum.fee_account_witness.unwrap(),
block_timestamp: witness_accum.timestamp,
}
}

Expand Down
267 changes: 134 additions & 133 deletions core/bin/zksync_api/src/api_server/rest/v1/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,138 +123,139 @@ impl TestServerConfig {
}

pub async fn fill_database(&self) -> anyhow::Result<()> {
static INITED: Lazy<Mutex<bool>> = Lazy::new(|| Mutex::new(false));

// Hold this guard until transaction will be committed to avoid double init.
let mut inited_guard = INITED.lock().await;
if *inited_guard {
return Ok(());
}
*inited_guard = true;

let mut storage = self.pool.access_storage().await?;

// Check if database is been already inited.
if storage.chain().block_schema().get_block(1).await?.is_some() {
return Ok(());
}

// Make changes atomic.
let mut storage = storage.start_transaction().await?;

// Below lies the initialization of the data for the test.
let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);

// Required since we use `EthereumSchema` in this test.
storage.ethereum_schema().initialize_eth_data().await?;

let mut accounts = AccountMap::default();
let n_committed = 5;
let n_verified = n_committed - 2;

// Create and apply several blocks to work with.
for block_number in 1..=n_committed {
let updates = (0..3)
.map(|_| gen_acc_random_updates(&mut rng))
.flatten()
.collect::<Vec<_>>();
apply_updates(&mut accounts, updates.clone());

// Add transactions to every odd block.
let txs = if block_number % 2 == 1 {
Self::gen_zk_txs(1_000)
.into_iter()
.map(|(_tx, op)| op)
.collect()
} else {
vec![]
};

// Store the operation in the block schema.
let operation = storage
.chain()
.block_schema()
.execute_operation(gen_unique_operation_with_txs(
block_number,
Action::Commit,
BLOCK_SIZE_CHUNKS,
txs,
))
.await?;
storage
.chain()
.state_schema()
.commit_state_update(block_number, &updates, 0)
.await?;

// Store & confirm the operation in the ethereum schema, as it's used for obtaining
// commit/verify hashes.
let ethereum_op_id = operation.id.unwrap() as i64;
let eth_tx_hash = dummy_ethereum_tx_hash(ethereum_op_id);
let response = storage
.ethereum_schema()
.save_new_eth_tx(
OperationType::Commit,
Some(ethereum_op_id),
100,
100u32.into(),
Default::default(),
)
.await?;
storage
.ethereum_schema()
.add_hash_entry(response.id, &eth_tx_hash)
.await?;
storage
.ethereum_schema()
.confirm_eth_tx(&eth_tx_hash)
.await?;

// Add verification for the block if required.
if block_number <= n_verified {
storage
.prover_schema()
.store_proof(block_number, &Default::default())
.await?;
let operation = storage
.chain()
.block_schema()
.execute_operation(gen_unique_operation(
block_number,
Action::Verify {
proof: Default::default(),
},
BLOCK_SIZE_CHUNKS,
))
.await?;

let ethereum_op_id = operation.id.unwrap() as i64;
let eth_tx_hash = dummy_ethereum_tx_hash(ethereum_op_id);
let response = storage
.ethereum_schema()
.save_new_eth_tx(
OperationType::Verify,
Some(ethereum_op_id),
100,
100u32.into(),
Default::default(),
)
.await?;
storage
.ethereum_schema()
.add_hash_entry(response.id, &eth_tx_hash)
.await?;
storage
.ethereum_schema()
.confirm_eth_tx(&eth_tx_hash)
.await?;
}
}

storage.commit().await?;
// Storage has been inited, so we can safely drop this guard.
drop(inited_guard);

Ok(())
todo!()
// static INITED: Lazy<Mutex<bool>> = Lazy::new(|| Mutex::new(false));
//
// // Hold this guard until transaction will be committed to avoid double init.
// let mut inited_guard = INITED.lock().await;
// if *inited_guard {
// return Ok(());
// }
// *inited_guard = true;
//
// let mut storage = self.pool.access_storage().await?;
//
// // Check if database is been already inited.
// if storage.chain().block_schema().get_block(1).await?.is_some() {
// return Ok(());
// }
//
// // Make changes atomic.
// let mut storage = storage.start_transaction().await?;
//
// // Below lies the initialization of the data for the test.
// let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);
//
// // Required since we use `EthereumSchema` in this test.
// storage.ethereum_schema().initialize_eth_data().await?;
//
// let mut accounts = AccountMap::default();
// let n_committed = 5;
// let n_verified = n_committed - 2;
//
// // Create and apply several blocks to work with.
// for block_number in 1..=n_committed {
// let updates = (0..3)
// .map(|_| gen_acc_random_updates(&mut rng))
// .flatten()
// .collect::<Vec<_>>();
// apply_updates(&mut accounts, updates.clone());
//
// // Add transactions to every odd block.
// let txs = if block_number % 2 == 1 {
// Self::gen_zk_txs(1_000)
// .into_iter()
// .map(|(_tx, op)| op)
// .collect()
// } else {
// vec![]
// };
//
// // Store the operation in the block schema.
// let operation = storage
// .chain()
// .block_schema()
// .execute_operation(gen_unique_operation_with_txs(
// block_number,
// Action::Commit,
// BLOCK_SIZE_CHUNKS,
// txs,
// ))
// .await?;
// storage
// .chain()
// .state_schema()
// .commit_state_update(block_number, &updates, 0)
// .await?;
//
// // Store & confirm the operation in the ethereum schema, as it's used for obtaining
// // commit/verify hashes.
// let ethereum_op_id = operation.id.unwrap() as i64;
// let eth_tx_hash = dummy_ethereum_tx_hash(ethereum_op_id);
// let response = storage
// .ethereum_schema()
// .save_new_eth_tx(
// OperationType::Commit,
// Some(ethereum_op_id),
// 100,
// 100u32.into(),
// Default::default(),
// )
// .await?;
// storage
// .ethereum_schema()
// .add_hash_entry(response.id, &eth_tx_hash)
// .await?;
// storage
// .ethereum_schema()
// .confirm_eth_tx(&eth_tx_hash)
// .await?;
//
// // Add verification for the block if required.
// if block_number <= n_verified {
// storage
// .prover_schema()
// .store_proof(block_number, &Default::default())
// .await?;
// let operation = storage
// .chain()
// .block_schema()
// .execute_operation(gen_unique_operation(
// block_number,
// Action::Verify {
// proof: Default::default(),
// },
// BLOCK_SIZE_CHUNKS,
// ))
// .await?;
//
// let ethereum_op_id = operation.id.unwrap() as i64;
// let eth_tx_hash = dummy_ethereum_tx_hash(ethereum_op_id);
// let response = storage
// .ethereum_schema()
// .save_new_eth_tx(
// OperationType::Verify,
// Some(ethereum_op_id),
// 100,
// 100u32.into(),
// Default::default(),
// )
// .await?;
// storage
// .ethereum_schema()
// .add_hash_entry(response.id, &eth_tx_hash)
// .await?;
// storage
// .ethereum_schema()
// .confirm_eth_tx(&eth_tx_hash)
// .await?;
// }
// }
//
// storage.commit().await?;
// // Storage has been inited, so we can safely drop this guard.
// drop(inited_guard);
//
// Ok(())
}
}
Loading

0 comments on commit 5484b69

Please sign in to comment.