Skip to content

Commit

Permalink
feat(vm): detailed circuit statistic (#845)
Browse files Browse the repository at this point in the history
## What ❔

Adds statistic with a number of circuit estimated for each circuit type.

## Why ❔

Better visibility/tests/debugging opportunity

## Checklist

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

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [x] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
- [x] Spellcheck has been run via `zk spellcheck`.
  • Loading branch information
perekopskiy committed Jan 26, 2024
1 parent 1aed8de commit a20af60
Show file tree
Hide file tree
Showing 49 changed files with 592 additions and 402 deletions.
4 changes: 2 additions & 2 deletions core/bin/snapshots_creator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rand::{thread_rng, Rng};
use zksync_dal::StorageProcessor;
use zksync_object_store::ObjectStore;
use zksync_types::{
block::{BlockGasCount, L1BatchHeader, MiniblockHeader},
block::{L1BatchHeader, MiniblockHeader},
snapshots::{
SnapshotFactoryDependencies, SnapshotFactoryDependency, SnapshotStorageLog,
SnapshotStorageLogsChunk, SnapshotStorageLogsStorageKey,
Expand Down Expand Up @@ -173,7 +173,7 @@ async fn create_l1_batch(
);
header.is_finished = true;
conn.blocks_dal()
.insert_l1_batch(&header, &[], BlockGasCount::default(), &[], &[], 0)
.insert_mock_l1_batch(&header)
.await
.unwrap();
conn.blocks_dal()
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE l1_batches
DROP COLUMN IF EXISTS predicted_circuits_by_type;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE l1_batches
ADD COLUMN IF NOT EXISTS predicted_circuits_by_type JSONB;
25 changes: 19 additions & 6 deletions core/lib/dal/src/blocks_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use sqlx::Row;
use zksync_types::{
aggregated_operations::AggregatedActionType,
block::{BlockGasCount, L1BatchHeader, MiniblockHeader},
circuit::CircuitStatistic,
commitment::{L1BatchMetadata, L1BatchWithMetadata},
zk_evm_types::LogQuery,
Address, L1BatchNumber, MiniblockNumber, ProtocolVersionId, H256, U256,
Expand Down Expand Up @@ -448,7 +449,7 @@ impl BlocksDal<'_, '_> {
predicted_block_gas: BlockGasCount,
events_queue: &[LogQuery],
storage_refunds: &[u32],
predicted_circuits: u32,
predicted_circuits_by_type: CircuitStatistic, // predicted number of circuits for each circuit type
) -> anyhow::Result<()> {
let priority_onchain_data: Vec<Vec<u8>> = header
.priority_ops_onchain_data
Expand Down Expand Up @@ -508,7 +509,7 @@ impl BlocksDal<'_, '_> {
system_logs,
storage_refunds,
pubdata_input,
predicted_circuits,
predicted_circuits_by_type,
created_at,
updated_at
)
Expand Down Expand Up @@ -567,7 +568,7 @@ impl BlocksDal<'_, '_> {
&system_logs,
&storage_refunds,
pubdata_input,
predicted_circuits as i32,
serde_json::to_value(predicted_circuits_by_type).unwrap(),
)
.execute(transaction.conn())
.await?;
Expand Down Expand Up @@ -2198,6 +2199,18 @@ impl BlocksDal<'_, '_> {
Ok(())
}

pub async fn insert_mock_l1_batch(&mut self, header: &L1BatchHeader) -> anyhow::Result<()> {
self.insert_l1_batch(
header,
&[],
Default::default(),
&[],
&[],
Default::default(),
)
.await
}

/// Deletes all miniblocks and L1 batches, including the genesis ones. Should only be used in tests.
pub async fn delete_genesis(&mut self) -> anyhow::Result<()> {
self.delete_miniblocks_inner(None)
Expand Down Expand Up @@ -2264,7 +2277,7 @@ mod tests {
header.l2_to_l1_messages.push(vec![33; 33]);

conn.blocks_dal()
.insert_l1_batch(&header, &[], BlockGasCount::default(), &[], &[], 0)
.insert_mock_l1_batch(&header)
.await
.unwrap();

Expand Down Expand Up @@ -2317,15 +2330,15 @@ mod tests {
execute: 10,
};
conn.blocks_dal()
.insert_l1_batch(&header, &[], predicted_gas, &[], &[], 0)
.insert_l1_batch(&header, &[], predicted_gas, &[], &[], Default::default())
.await
.unwrap();

header.number = L1BatchNumber(2);
header.timestamp += 100;
predicted_gas += predicted_gas;
conn.blocks_dal()
.insert_l1_batch(&header, &[], predicted_gas, &[], &[], 0)
.insert_l1_batch(&header, &[], predicted_gas, &[], &[], Default::default())
.await
.unwrap();

Expand Down
7 changes: 2 additions & 5 deletions core/lib/dal/src/storage_logs_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,10 +784,7 @@ impl StorageLogsDal<'_, '_> {
#[cfg(test)]
mod tests {
use zksync_contracts::BaseSystemContractsHashes;
use zksync_types::{
block::{BlockGasCount, L1BatchHeader},
ProtocolVersion, ProtocolVersionId,
};
use zksync_types::{block::L1BatchHeader, ProtocolVersion, ProtocolVersionId};

use super::*;
use crate::{tests::create_miniblock_header, ConnectionPool};
Expand All @@ -802,7 +799,7 @@ mod tests {
);
header.is_finished = true;
conn.blocks_dal()
.insert_l1_batch(&header, &[], BlockGasCount::default(), &[], &[], 0)
.insert_mock_l1_batch(&header)
.await
.unwrap();
conn.blocks_dal()
Expand Down
8 changes: 3 additions & 5 deletions core/lib/dal/src/storage_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ impl StorageWeb3Dal<'_, '_> {
#[cfg(test)]
mod tests {
use zksync_types::{
block::{BlockGasCount, L1BatchHeader},
snapshots::SnapshotRecoveryStatus,
ProtocolVersion, ProtocolVersionId,
block::L1BatchHeader, snapshots::SnapshotRecoveryStatus, ProtocolVersion, ProtocolVersionId,
};

use super::*;
Expand All @@ -285,7 +283,7 @@ mod tests {
ProtocolVersionId::latest(),
);
conn.blocks_dal()
.insert_l1_batch(&l1_batch_header, &[], BlockGasCount::default(), &[], &[], 0)
.insert_mock_l1_batch(&l1_batch_header)
.await
.unwrap();
conn.blocks_dal()
Expand Down Expand Up @@ -389,7 +387,7 @@ mod tests {
ProtocolVersionId::latest(),
);
conn.blocks_dal()
.insert_l1_batch(&l1_batch_header, &[], BlockGasCount::default(), &[], &[], 0)
.insert_mock_l1_batch(&l1_batch_header)
.await
.unwrap();
conn.blocks_dal()
Expand Down
9 changes: 4 additions & 5 deletions core/lib/dal/src/sync_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ impl SyncDal<'_, '_> {
#[cfg(test)]
mod tests {
use zksync_types::{
block::{BlockGasCount, L1BatchHeader},
fee::TransactionExecutionMetrics,
L1BatchNumber, ProtocolVersion, ProtocolVersionId, Transaction,
block::L1BatchHeader, fee::TransactionExecutionMetrics, L1BatchNumber, ProtocolVersion,
ProtocolVersionId, Transaction,
};

use super::*;
Expand Down Expand Up @@ -127,7 +126,7 @@ mod tests {
ProtocolVersionId::latest(),
);
conn.blocks_dal()
.insert_l1_batch(&l1_batch_header, &[], BlockGasCount::default(), &[], &[], 0)
.insert_mock_l1_batch(&l1_batch_header)
.await
.unwrap();
conn.blocks_dal()
Expand Down Expand Up @@ -202,7 +201,7 @@ mod tests {
l1_batch_header.number = L1BatchNumber(1);
l1_batch_header.timestamp = 1;
conn.blocks_dal()
.insert_l1_batch(&l1_batch_header, &[], BlockGasCount::default(), &[], &[], 0)
.insert_mock_l1_batch(&l1_batch_header)
.await
.unwrap();
conn.blocks_dal()
Expand Down
12 changes: 6 additions & 6 deletions core/lib/multivm/src/glue/types/vm/vm_block_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl GlueFrom<crate::vm_m5::vm_instance::VmBlockResult> for crate::interface::Fi
computational_gas_used: value.full_result.gas_used,
gas_used: value.full_result.gas_used,
pubdata_published: 0,
estimated_circuits_used: 0.0,
circuit_statistic: Default::default(),
},
refunds: Refunds::default(),
},
Expand Down Expand Up @@ -105,7 +105,7 @@ impl GlueFrom<crate::vm_m6::vm_instance::VmBlockResult> for crate::interface::Fi
computational_gas_used: value.full_result.computational_gas_used,
gas_used: value.full_result.gas_used,
pubdata_published: 0,
estimated_circuits_used: 0.0,
circuit_statistic: Default::default(),
},
refunds: Refunds::default(),
},
Expand Down Expand Up @@ -164,7 +164,7 @@ impl GlueFrom<crate::vm_1_3_2::vm_instance::VmBlockResult> for crate::interface:
computational_gas_used: value.full_result.computational_gas_used,
gas_used: value.full_result.gas_used,
pubdata_published: 0,
estimated_circuits_used: 0.0,
circuit_statistic: Default::default(),
},
refunds: Refunds::default(),
},
Expand Down Expand Up @@ -237,7 +237,7 @@ impl GlueFrom<crate::vm_1_3_2::vm_instance::VmBlockResult>
computational_gas_used: value.full_result.computational_gas_used,
gas_used: value.full_result.gas_used,
pubdata_published: 0,
estimated_circuits_used: 0.0,
circuit_statistic: Default::default(),
},
refunds: Refunds::default(),
}
Expand Down Expand Up @@ -268,7 +268,7 @@ impl GlueFrom<crate::vm_m5::vm_instance::VmBlockResult>
computational_gas_used: 0,
gas_used: value.full_result.gas_used,
pubdata_published: 0,
estimated_circuits_used: 0.0,
circuit_statistic: Default::default(),
},
refunds: Refunds::default(),
}
Expand Down Expand Up @@ -315,7 +315,7 @@ impl GlueFrom<crate::vm_m6::vm_instance::VmBlockResult>
computational_gas_used: value.full_result.computational_gas_used,
gas_used: value.full_result.gas_used,
pubdata_published: 0,
estimated_circuits_used: 0.0,
circuit_statistic: Default::default(),
},
refunds: Refunds::default(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl GlueFrom<crate::vm_m5::vm_instance::VmPartialExecutionResult>
// There are no such fields in `m5`
computational_gas_used: 0,
pubdata_published: 0,
estimated_circuits_used: 0.0,
circuit_statistic: Default::default(),
},
refunds: crate::interface::Refunds {
gas_refunded: 0,
Expand All @@ -40,7 +40,7 @@ impl GlueFrom<crate::vm_m6::vm_instance::VmPartialExecutionResult>
computational_gas_used: value.computational_gas_used,
total_log_queries: value.logs.total_log_queries_count,
pubdata_published: 0,
estimated_circuits_used: 0.0,
circuit_statistic: Default::default(),
},
refunds: crate::interface::Refunds {
gas_refunded: 0,
Expand All @@ -64,7 +64,7 @@ impl GlueFrom<crate::vm_1_3_2::vm_instance::VmPartialExecutionResult>
computational_gas_used: value.computational_gas_used,
total_log_queries: value.logs.total_log_queries_count,
pubdata_published: 0,
estimated_circuits_used: 0.0,
circuit_statistic: Default::default(),
},
refunds: crate::interface::Refunds {
gas_refunded: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl VmExecutionResultAndLogs {
cycles_used: self.statistics.cycles_used,
computational_gas_used: self.statistics.computational_gas_used,
pubdata_published: self.statistics.pubdata_published,
estimated_circuits_used: self.statistics.estimated_circuits_used,
circuit_statistic: self.statistics.circuit_statistic,
}
}
}
4 changes: 3 additions & 1 deletion core/lib/multivm/src/interface/types/outputs/statistic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use zksync_types::circuit::CircuitStatistic;

/// Statistics of the tx execution.
#[derive(Debug, Default, Clone)]
pub struct VmExecutionStatistics {
Expand All @@ -12,7 +14,7 @@ pub struct VmExecutionStatistics {
/// Number of log queries produced by the VM during the tx execution.
pub total_log_queries: usize,
pub pubdata_published: u32,
pub estimated_circuits_used: f32,
pub circuit_statistic: CircuitStatistic,
}

/// Oracle metrics of the VM.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::{
vm_boojum_integration::{
old_vm::utils::{vm_may_have_ended_inner, VmExecutionResult},
tracers::{
dispatcher::TracerDispatcher, DefaultExecutionTracer, PubdataTracer, RefundsTracer,
circuits_capacity::circuit_statistic_from_cycles, dispatcher::TracerDispatcher,
DefaultExecutionTracer, PubdataTracer, RefundsTracer,
},
vm::Vm,
},
Expand Down Expand Up @@ -80,7 +81,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
spent_pubdata_counter_before,
pubdata_published,
logs.total_log_queries_count,
tx_tracer.circuits_tracer.estimated_circuits_used,
circuit_statistic_from_cycles(tx_tracer.circuits_tracer.statistics),
);
let result = tx_tracer.result_tracer.into_result();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use zk_evm_1_4_0::aux_structures::Timestamp;
use zksync_state::WriteStorage;
use zksync_types::U256;
use zksync_types::{circuit::CircuitStatistic, U256};

use crate::{
interface::{VmExecutionStatistics, VmMemoryMetrics},
Expand All @@ -24,7 +24,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
spent_pubdata_counter_before: u32,
pubdata_published: u32,
total_log_queries_count: usize,
estimated_circuits_used: f32,
circuit_statistic: CircuitStatistic,
) -> VmExecutionStatistics {
let computational_gas_used = self.calculate_computational_gas_used(
tracer,
Expand All @@ -41,7 +41,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
computational_gas_used,
total_log_queries: total_log_queries_count,
pubdata_published,
estimated_circuits_used,
circuit_statistic,
}
}

Expand Down
Loading

0 comments on commit a20af60

Please sign in to comment.