Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(state-keeper): circuits seal criterion #729

Merged
merged 32 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
00513b2
Add circuits seal criterion
perekopskiy Dec 14, 2023
d19806e
Merge branch 'main' into circuits-seal-criterion
perekopskiy Dec 19, 2023
6dfd883
Proper calculation of used circuits
perekopskiy Dec 21, 2023
eef8c1d
fmt
perekopskiy Dec 21, 2023
740221d
Merge branch 'main' into circuits-seal-criterion
perekopskiy Dec 21, 2023
8de4cc3
Clean up geometry criteria interface
perekopskiy Dec 21, 2023
6c9185d
Add comment
perekopskiy Dec 21, 2023
fcaec4d
revert
perekopskiy Dec 21, 2023
5bd13d3
Revert
perekopskiy Dec 21, 2023
a128c01
Fix
perekopskiy Dec 21, 2023
3d9d349
Fix
perekopskiy Dec 21, 2023
0844bd9
try f32
perekopskiy Dec 21, 2023
812f211
Clean up
perekopskiy Dec 21, 2023
569a493
Track hot write and reads
perekopskiy Dec 21, 2023
df9b7df
Add comment
perekopskiy Dec 22, 2023
fae2f1f
Apply some review suggestions
perekopskiy Jan 2, 2024
8a487a2
Use updated zk_evm_abstractions
perekopskiy Jan 2, 2024
428618f
Add comments
perekopskiy Jan 3, 2024
8bda047
Change approach to tracing precompiles
perekopskiy Jan 3, 2024
8cc77fd
Merge branch 'main' into circuits-seal-criterion
perekopskiy Jan 3, 2024
7a80f06
Tests
perekopskiy Jan 3, 2024
987e5fc
Switch to main branches back
perekopskiy Jan 3, 2024
a3413e6
Update prover lock file
perekopskiy Jan 3, 2024
2e6ad23
Add comment
perekopskiy Jan 4, 2024
cc8f1cf
new db column
perekopskiy Jan 4, 2024
e37e910
new db column
perekopskiy Jan 4, 2024
51fb475
Merge branch 'main' into circuits-seal-criterion
perekopskiy Jan 4, 2024
a14c189
Add changes to vm_boojum_integration
perekopskiy Jan 5, 2024
bc30894
Add sanity panic
perekopskiy Jan 5, 2024
31c9499
Fix
perekopskiy Jan 5, 2024
066a635
Merge branch 'main' into circuits-seal-criterion
perekopskiy Jan 5, 2024
a3916dc
Fix test
perekopskiy Jan 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion core/lib/multivm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ zk_evm_1_4_0 = { package = "zk_evm", git = "https://github.com/matter-labs/era-z
zk_evm_1_3_3 = { package = "zk_evm", git = "https://github.com/matter-labs/era-zk_evm.git", tag= "v1.3.3-rc2" }
zk_evm_1_3_1 = { package = "zk_evm", git = "https://github.com/matter-labs/era-zk_evm.git", tag= "v1.3.1-rc2" }

zkevm_test_harness_1_4_0 = { git = "https://github.com/matter-labs/era-zkevm_test_harness.git", branch = "v1.4.0", package = "zkevm_test_harness" }

zksync_types = { path = "../types" }
zksync_state = { path = "../state" }
zksync_contracts = { path = "../contracts" }
Expand All @@ -29,7 +31,6 @@ thiserror = "1.0"
tracing = "0.1"
vise = { git = "https://github.com/matter-labs/vise.git", version = "0.1.0", rev = "dd05139b76ab0843443ab3ff730174942c825dae" }


[dev-dependencies]
tokio = { version = "1", features = ["time"] }
zksync_test_account = { path = "../test_account" }
Expand Down
6 changes: 6 additions & 0 deletions core/lib/multivm/src/glue/types/vm/vm_block_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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,
},
refunds: Refunds::default(),
},
Expand Down Expand Up @@ -63,6 +64,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,
},
refunds: Refunds::default(),
},
Expand Down Expand Up @@ -106,6 +108,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,
},
refunds: Refunds::default(),
},
Expand Down Expand Up @@ -165,6 +168,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,
},
refunds: Refunds::default(),
}
Expand Down Expand Up @@ -195,6 +199,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,
},
refunds: Refunds::default(),
}
Expand Down Expand Up @@ -236,6 +241,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,
},
refunds: Refunds::default(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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,
},
refunds: crate::interface::Refunds {
gas_refunded: 0,
Expand All @@ -39,6 +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,
},
refunds: crate::interface::Refunds {
gas_refunded: 0,
Expand All @@ -62,6 +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,
},
refunds: crate::interface::Refunds {
gas_refunded: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +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,
}
}
}
1 change: 1 addition & 0 deletions core/lib/multivm/src/interface/types/outputs/statistic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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: usize,
}

/// Oracle metrics of the VM.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
.map(|x| (x.get_refunds(), x.pubdata_published()))
.unwrap_or_default();

let estimated_circuits_used =
tx_tracer.circuits_tracer.estimated_circuits_used.ceil() as usize;

let statistics = self.get_statistics(
timestamp_initial,
cycles_initial,
Expand All @@ -80,6 +83,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
spent_pubdata_counter_before,
pubdata_published,
logs.total_log_queries_count,
estimated_circuits_used,
);
let result = tx_tracer.result_tracer.into_result();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +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: usize,
) -> VmExecutionStatistics {
let computational_gas_used = self.calculate_computational_gas_used(
tracer,
Expand All @@ -40,6 +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,
}
}

Expand Down
22 changes: 20 additions & 2 deletions core/lib/multivm/src/versions/vm_latest/oracles/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ pub struct StorageOracle<S: WriteStorage, H: HistoryMode> {

// Storage refunds that oracle has returned in `estimate_refunds_for_write`.
pub(crate) returned_refunds: HistoryRecorder<Vec<u32>, H>,

// Keeps track of storage keys that were written to.
perekopskiy marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) hot_writes: HistoryRecorder<HashMap<StorageKey, ()>, HistoryEnabled>,
// Keeps track of storage keys that were read.
perekopskiy marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) hot_reads: HistoryRecorder<HashMap<StorageKey, ()>, HistoryEnabled>,
}

impl<S: WriteStorage> OracleWithHistory for StorageOracle<S, HistoryEnabled> {
Expand All @@ -69,6 +74,8 @@ impl<S: WriteStorage> OracleWithHistory for StorageOracle<S, HistoryEnabled> {
self.paid_changes.rollback_to_timestamp(timestamp);
self.initial_values.rollback_to_timestamp(timestamp);
self.returned_refunds.rollback_to_timestamp(timestamp);
self.hot_writes.rollback_to_timestamp(timestamp);
self.hot_reads.rollback_to_timestamp(timestamp);
}
}

Expand All @@ -81,6 +88,8 @@ impl<S: WriteStorage, H: HistoryMode> StorageOracle<S, H> {
paid_changes: Default::default(),
initial_values: Default::default(),
returned_refunds: Default::default(),
hot_writes: Default::default(),
hot_reads: Default::default(),
}
}

Expand All @@ -91,6 +100,8 @@ impl<S: WriteStorage, H: HistoryMode> StorageOracle<S, H> {
self.paid_changes.delete_history();
self.initial_values.delete_history();
self.returned_refunds.delete_history();
self.hot_writes.delete_history();
self.hot_reads.delete_history();
}

fn is_storage_key_free(&self, key: &StorageKey) -> bool {
Expand All @@ -108,8 +119,12 @@ impl<S: WriteStorage, H: HistoryMode> StorageOracle<S, H> {
}
}

pub fn read_value(&mut self, mut query: LogQuery) -> LogQuery {
fn read_value(&mut self, mut query: LogQuery) -> LogQuery {
let key = triplet_to_storage_key(query.shard_id, query.address, query.key);

if !self.hot_reads.inner().contains_key(&key) {
self.hot_reads.insert(key, (), query.timestamp);
}
let current_value = self.storage.read_from_storage(&key);

query.read_value = current_value;
Expand All @@ -127,8 +142,11 @@ impl<S: WriteStorage, H: HistoryMode> StorageOracle<S, H> {
query
}

pub fn write_value(&mut self, query: LogQuery) -> LogQuery {
fn write_value(&mut self, query: LogQuery) -> LogQuery {
let key = triplet_to_storage_key(query.shard_id, query.address, query.key);
if !self.hot_writes.inner().contains_key(&key) {
self.hot_writes.insert(key, (), query.timestamp);
}
let current_value =
self.storage
.write_to_storage(key, query.written_value, query.timestamp);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use zkevm_test_harness_1_4_0::{geometry_config::get_geometry_config, toolset::GeometryConfig};

const GEOMETRY_CONFIG: GeometryConfig = get_geometry_config();
const OVERESTIMATE_PERCENT: f32 = 1.05;

const MAIN_VM_CYCLE_FRACTION: f32 =
OVERESTIMATE_PERCENT / GEOMETRY_CONFIG.cycles_per_vm_snapshot as f32;

const CODE_DECOMMITTER_SORTER_CYCLE_FRACTION: f32 =
OVERESTIMATE_PERCENT / GEOMETRY_CONFIG.cycles_code_decommitter_sorter as f32;

const LOG_DEMUXER_CYCLE_FRACTION: f32 =
OVERESTIMATE_PERCENT / GEOMETRY_CONFIG.cycles_per_log_demuxer as f32;

const STORAGE_SORTER_CYCLE_FRACTION: f32 =
OVERESTIMATE_PERCENT / GEOMETRY_CONFIG.cycles_per_storage_sorter as f32;

const EVENTS_OR_L1_MESSAGES_SORTER_CYCLE_FRACTION: f32 =
OVERESTIMATE_PERCENT / GEOMETRY_CONFIG.cycles_per_events_or_l1_messages_sorter as f32;

const RAM_PERMUTATION_CYCLE_FRACTION: f32 =
OVERESTIMATE_PERCENT / GEOMETRY_CONFIG.cycles_per_ram_permutation as f32;

pub(crate) const CODE_DECOMMITTER_CYCLE_FRACTION: f32 =
OVERESTIMATE_PERCENT / GEOMETRY_CONFIG.cycles_per_code_decommitter as f32;

pub(crate) const STORAGE_APPLICATION_CYCLE_FRACTION: f32 =
OVERESTIMATE_PERCENT / GEOMETRY_CONFIG.cycles_per_storage_application as f32;

pub(crate) const KECCAK256_CYCLE_FRACTION: f32 =
OVERESTIMATE_PERCENT / GEOMETRY_CONFIG.cycles_per_keccak256_circuit as f32;

pub(crate) const SHA256_CYCLE_FRACTION: f32 =
OVERESTIMATE_PERCENT / GEOMETRY_CONFIG.cycles_per_sha256_circuit as f32;

pub(crate) const ECRECOVER_CYCLE_FRACTION: f32 =
OVERESTIMATE_PERCENT / GEOMETRY_CONFIG.cycles_per_ecrecover_circuit as f32;

pub(crate) const RICH_ADDRESSING_OPCODE_FRACTION: f32 =
MAIN_VM_CYCLE_FRACTION + 3.0 * RAM_PERMUTATION_CYCLE_FRACTION;
perekopskiy marked this conversation as resolved.
Show resolved Hide resolved

pub(crate) const AVERAGE_OPCODE_FRACTION: f32 =
MAIN_VM_CYCLE_FRACTION + RAM_PERMUTATION_CYCLE_FRACTION;

pub(crate) const STORAGE_READ_BASE_FRACTION: f32 = MAIN_VM_CYCLE_FRACTION
+ RAM_PERMUTATION_CYCLE_FRACTION
+ LOG_DEMUXER_CYCLE_FRACTION
+ STORAGE_SORTER_CYCLE_FRACTION;

pub(crate) const EVENT_OR_L1_MESSAGE_FRACTION: f32 = 2.0 * MAIN_VM_CYCLE_FRACTION
perekopskiy marked this conversation as resolved.
Show resolved Hide resolved
+ RAM_PERMUTATION_CYCLE_FRACTION
+ 2.0 * LOG_DEMUXER_CYCLE_FRACTION
+ 2.0 * EVENTS_OR_L1_MESSAGES_SORTER_CYCLE_FRACTION;

pub(crate) const STORAGE_WRITE_BASE_FRACTION: f32 = 2.0 * MAIN_VM_CYCLE_FRACTION
+ RAM_PERMUTATION_CYCLE_FRACTION
+ 2.0 * LOG_DEMUXER_CYCLE_FRACTION
+ 2.0 * STORAGE_SORTER_CYCLE_FRACTION;

pub(crate) const FAR_CALL_FRACTION: f32 = 2.0 * MAIN_VM_CYCLE_FRACTION
+ RAM_PERMUTATION_CYCLE_FRACTION
+ STORAGE_SORTER_CYCLE_FRACTION
+ CODE_DECOMMITTER_SORTER_CYCLE_FRACTION;

pub(crate) const UMA_WRITE_FRACTION: f32 =
2.0 * MAIN_VM_CYCLE_FRACTION + 5.0 * RAM_PERMUTATION_CYCLE_FRACTION;

pub(crate) const UMA_READ_FRACTION: f32 =
2.0 * MAIN_VM_CYCLE_FRACTION + 3.0 * RAM_PERMUTATION_CYCLE_FRACTION;

pub(crate) const PRECOMPILE_CALL_COMMON_FRACTION: f32 =
MAIN_VM_CYCLE_FRACTION + RAM_PERMUTATION_CYCLE_FRACTION + LOG_DEMUXER_CYCLE_FRACTION;
Loading