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): miniblock max payload size (BFT-417) #1284

Merged
merged 17 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
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
3 changes: 3 additions & 0 deletions core/lib/config/src/configs/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub struct StateKeeperConfig {
/// sealing will block until some of the miniblocks from the queue are processed.
/// 0 means that sealing is synchronous; this is mostly useful for performance comparison, testing etc.
pub miniblock_seal_queue_capacity: usize,
/// The max payload size threshold (in bytes) that triggers sealing of a miniblock.
pub miniblock_max_payload_size: usize,

/// The max number of gas to spend on an L1 tx before its batch should be sealed by the gas sealer.
pub max_single_tx_gas: u32,
Expand Down Expand Up @@ -127,6 +129,7 @@ impl StateKeeperConfig {
block_commit_deadline_ms: 2500,
miniblock_commit_deadline_ms: 1000,
miniblock_seal_queue_capacity: 10,
miniblock_max_payload_size: 1_000_000,
max_single_tx_gas: 6000000,
max_allowed_l2_tx_gas_limit: 4000000000,
reject_tx_at_geometry_percentage: 0.95,
Expand Down
1 change: 1 addition & 0 deletions core/lib/config/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ impl RandomConfig for configs::chain::StateKeeperConfig {
block_commit_deadline_ms: g.gen(),
miniblock_commit_deadline_ms: g.gen(),
miniblock_seal_queue_capacity: g.gen(),
miniblock_max_payload_size: g.gen(),
max_single_tx_gas: g.gen(),
max_allowed_l2_tx_gas_limit: g.gen(),
reject_tx_at_geometry_percentage: g.gen(),
Expand Down
2 changes: 1 addition & 1 deletion core/lib/dal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub mod fri_witness_generator_dal;
pub mod healthcheck;
mod instrument;
mod metrics;
mod models;
pub mod models;
moshababo marked this conversation as resolved.
Show resolved Hide resolved
pub mod proof_generation_dal;
pub mod protocol_versions_dal;
pub mod protocol_versions_web3_dal;
Expand Down
2 changes: 1 addition & 1 deletion core/lib/dal/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod consensus;
mod proto;
pub mod proto;
pub mod storage_block;
pub mod storage_eth_tx;
pub mod storage_event;
Expand Down
2 changes: 2 additions & 0 deletions core/lib/env_config/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ mod tests {
block_commit_deadline_ms: 2500,
miniblock_commit_deadline_ms: 1000,
miniblock_seal_queue_capacity: 10,
miniblock_max_payload_size: 1_000_000,
max_single_tx_gas: 1_000_000,
max_allowed_l2_tx_gas_limit: 2_000_000_000,
close_block_at_eth_params_percentage: 0.2,
Expand Down Expand Up @@ -114,6 +115,7 @@ mod tests {
CHAIN_STATE_KEEPER_BLOCK_COMMIT_DEADLINE_MS="2500"
CHAIN_STATE_KEEPER_MINIBLOCK_COMMIT_DEADLINE_MS="1000"
CHAIN_STATE_KEEPER_MINIBLOCK_SEAL_QUEUE_CAPACITY="10"
CHAIN_STATE_KEEPER_MINIBLOCK_MAX_PAYLOAD_SIZE="1000000"
CHAIN_STATE_KEEPER_MINIMAL_L2_GAS_PRICE="100000000"
CHAIN_STATE_KEEPER_COMPUTE_OVERHEAD_PART="0.0"
CHAIN_STATE_KEEPER_PUBDATA_OVERHEAD_PART="1.0"
Expand Down
4 changes: 4 additions & 0 deletions core/lib/protobuf_config/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ impl ProtoRepr for proto::StateKeeper {
miniblock_seal_queue_capacity: required(&self.miniblock_seal_queue_capacity)
.and_then(|x| Ok((*x).try_into()?))
.context("miniblock_seal_queue_capacity")?,
miniblock_max_payload_size: required(&self.miniblock_max_payload_size)
.and_then(|x| Ok((*x).try_into()?))
.context("miniblock_max_payload_size")?,
max_single_tx_gas: *required(&self.max_single_tx_gas).context("max_single_tx_gas")?,
max_allowed_l2_tx_gas_limit: *required(&self.max_allowed_l2_tx_gas_limit)
.context("max_allowed_l2_tx_gas_limit")?,
Expand Down Expand Up @@ -151,6 +154,7 @@ impl ProtoRepr for proto::StateKeeper {
miniblock_seal_queue_capacity: Some(
this.miniblock_seal_queue_capacity.try_into().unwrap(),
),
miniblock_max_payload_size: Some(this.miniblock_max_payload_size.try_into().unwrap()),
max_single_tx_gas: Some(this.max_single_tx_gas),
max_allowed_l2_tx_gas_limit: Some(this.max_allowed_l2_tx_gas_limit),
reject_tx_at_geometry_percentage: Some(this.reject_tx_at_geometry_percentage),
Expand Down
1 change: 1 addition & 0 deletions core/lib/protobuf_config/src/proto/chain.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ message StateKeeper {
optional uint32 virtual_blocks_per_miniblock = 24; // required
optional bool upload_witness_inputs_to_gcs = 25; // required
optional uint64 enum_index_migration_chunk_size = 26; // optional
optional uint64 miniblock_max_payload_size = 27; // required
}

message OperationsManager {
Expand Down
10 changes: 8 additions & 2 deletions core/lib/zksync_core/src/state_keeper/io/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{
},
mempool_actor::l2_tx_filter,
metrics::KEEPER_METRICS,
seal_criteria::{IoSealCriteria, TimeoutSealer},
seal_criteria::{IoSealCriteria, MiniblockMaxPayloadSizeSealer, TimeoutSealer},
updates::{MiniblockUpdates, UpdatesManager},
MempoolGuard,
},
Expand All @@ -50,6 +50,7 @@ pub struct MempoolIO {
pool: ConnectionPool,
object_store: Arc<dyn ObjectStore>,
timeout_sealer: TimeoutSealer,
miniblock_max_payload_size_sealer: MiniblockMaxPayloadSizeSealer,
filter: L2TxFilter,
current_miniblock_number: MiniblockNumber,
prev_miniblock_hash: H256,
Expand All @@ -76,7 +77,11 @@ impl IoSealCriteria for MempoolIO {
}

fn should_seal_miniblock(&mut self, manager: &UpdatesManager) -> bool {
self.timeout_sealer.should_seal_miniblock(manager)
if self.timeout_sealer.should_seal_miniblock(manager) {
return true;
}
self.miniblock_max_payload_size_sealer
.should_seal_miniblock(manager)
}
}

Expand Down Expand Up @@ -457,6 +462,7 @@ impl MempoolIO {
object_store,
pool,
timeout_sealer: TimeoutSealer::new(config),
miniblock_max_payload_size_sealer: MiniblockMaxPayloadSizeSealer::new(config),
filter: L2TxFilter::default(),
// ^ Will be initialized properly on the first newly opened batch
current_l1_batch_number: cursor.l1_batch,
Expand Down
45 changes: 42 additions & 3 deletions core/lib/zksync_core/src/state_keeper/seal_criteria/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ impl IoSealCriteria for TimeoutSealer {
}
}

#[derive(Debug, Clone, Copy)]
pub(super) struct MiniblockMaxPayloadSizeSealer {
max_payload_size: usize,
}

impl MiniblockMaxPayloadSizeSealer {
pub fn new(config: &StateKeeperConfig) -> Self {
Self {
max_payload_size: config.miniblock_max_payload_size,
}
}

pub fn should_seal_miniblock(&mut self, manager: &UpdatesManager) -> bool {
manager.miniblock.payload_encoding_size >= self.max_payload_size
slowli marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[cfg(test)]
mod tests {
use zksync_utils::time::seconds_since_epoch;
Expand All @@ -186,8 +203,7 @@ mod tests {
create_execution_result, create_transaction, create_updates_manager,
};

fn apply_tx_to_manager(manager: &mut UpdatesManager) {
let tx = create_transaction(10, 100);
fn apply_tx_to_manager(tx: Transaction, manager: &mut UpdatesManager) {
manager.extend_from_executed_transaction(
tx,
create_execution_result(0, []),
Expand Down Expand Up @@ -215,7 +231,7 @@ mod tests {
);

// Non-empty miniblock should trigger.
apply_tx_to_manager(&mut manager);
apply_tx_to_manager(create_transaction(10, 100), &mut manager);
assert!(
timeout_miniblock_sealer.should_seal_miniblock(&manager),
"Non-empty miniblock with old timestamp should be sealed"
Expand All @@ -230,4 +246,27 @@ mod tests {
"Non-empty miniblock with too recent timestamp shouldn't be sealed"
);
}

#[test]
fn max_size_miniblock_sealer() {
let tx = create_transaction(10, 100);
let tx_encoding_size =
zksync_protobuf::repr::encode::<zksync_dal::models::proto::Transaction>(&tx).len();

let mut max_payload_sealer = MiniblockMaxPayloadSizeSealer {
max_payload_size: tx_encoding_size,
};

let mut manager = create_updates_manager();
assert!(
!max_payload_sealer.should_seal_miniblock(&manager),
"Empty miniblock shouldn't be sealed"
);

apply_tx_to_manager(tx, &mut manager);
assert!(
max_payload_sealer.should_seal_miniblock(&manager),
"Miniblock with payload encoding size equal or greater than max payload size should be sealed"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct MiniblockUpdates {
pub l1_gas_count: BlockGasCount,
pub block_execution_metrics: ExecutionMetrics,
pub txs_encoding_size: usize,
pub payload_encoding_size: usize,
moshababo marked this conversation as resolved.
Show resolved Hide resolved
pub timestamp: u64,
pub number: u32,
pub prev_block_hash: H256,
Expand All @@ -51,6 +52,7 @@ impl MiniblockUpdates {
l1_gas_count: BlockGasCount::default(),
block_execution_metrics: ExecutionMetrics::default(),
txs_encoding_size: 0,
payload_encoding_size: 0,
timestamp,
number,
prev_block_hash,
Expand Down Expand Up @@ -130,6 +132,8 @@ impl MiniblockUpdates {
self.l1_gas_count += tx_l1_gas_this_tx;
self.block_execution_metrics += execution_metrics;
self.txs_encoding_size += tx.bootloader_encoding_size();
self.payload_encoding_size +=
zksync_protobuf::repr::encode::<zksync_dal::models::proto::Transaction>(&tx).len();
slowli marked this conversation as resolved.
Show resolved Hide resolved
self.storage_logs
.extend(tx_execution_result.logs.storage_logs);

Expand Down Expand Up @@ -182,6 +186,9 @@ mod tests {
MiniblockUpdates::new(0, 0, H256::random(), 0, ProtocolVersionId::latest());
let tx = create_transaction(10, 100);
let bootloader_encoding_size = tx.bootloader_encoding_size();
let payload_encoding_size =
zksync_protobuf::repr::encode::<zksync_dal::models::proto::Transaction>(&tx).len();

accumulator.extend_from_executed_transaction(
tx,
create_execution_result(0, []),
Expand All @@ -200,5 +207,6 @@ mod tests {
assert_eq!(accumulator.new_factory_deps.len(), 0);
assert_eq!(accumulator.block_execution_metrics.l2_to_l1_logs, 0);
assert_eq!(accumulator.txs_encoding_size, bootloader_encoding_size);
assert_eq!(accumulator.payload_encoding_size, payload_encoding_size);
}
}
1 change: 1 addition & 0 deletions etc/env/base/chain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ max_allowed_l2_tx_gas_limit=4000000000
block_commit_deadline_ms=2500
miniblock_commit_deadline_ms=1000
miniblock_seal_queue_capacity=10
miniblock_max_payload_size=1000000
# Max gas that can used to include single block in aggregated operation
max_single_tx_gas=6000000

Expand Down
2 changes: 1 addition & 1 deletion etc/env/consensus_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ server_addr: '127.0.0.1:3054'
public_addr: '127.0.0.1:3054'
validators:
- 'validator:public:bn254:8b0ff0ad1a250e64b0209277148ccee3b64534d8fa60cf25ba0bcc8b65d4d89309cdae79197c2db873d351401093fa0542a5a2071c1a247f2e1abe56d08cbabb'
max_payload_size: 5000000
max_payload_size: 2500000
gossip_static_inbound:
- 'node:public:ed25519:147bb71be895846e1d6f5b1c6a8be53848b82bdafcf66e9dfe6ca65581076a1d'
gossip_dynamic_inbound_limit: 0
2 changes: 1 addition & 1 deletion etc/env/en_consensus_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ server_addr: '127.0.0.1:3055'
public_addr: '127.0.0.1:3055'
validators:
- 'validator:public:bn254:8b0ff0ad1a250e64b0209277148ccee3b64534d8fa60cf25ba0bcc8b65d4d89309cdae79197c2db873d351401093fa0542a5a2071c1a247f2e1abe56d08cbabb'
max_payload_size: 5000000
max_payload_size: 2500000
gossip_dynamic_inbound_limit: 0
gossip_static_outbound:
- key: 'node:public:ed25519:ee717abba6aec5baae5e09d457bd2ffc2f121b576cf4170ce15a68163ce4c868'
Expand Down
Loading