Skip to content

Commit

Permalink
rework eth sender aggregator criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
perekopskiy committed Nov 22, 2024
1 parent 37bb0bd commit 525b37a
Show file tree
Hide file tree
Showing 31 changed files with 142 additions and 381 deletions.
2 changes: 0 additions & 2 deletions core/lib/config/src/configs/eth_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ impl EthConfig {
pub fn for_tests() -> Self {
Self {
sender: Some(SenderConfig {
aggregated_proof_sizes: vec![1],
wait_confirmations: Some(10),
tx_poll_period: 1,
aggregate_tx_poll_period: 1,
Expand Down Expand Up @@ -82,7 +81,6 @@ pub enum ProofLoadingMode {

#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct SenderConfig {
pub aggregated_proof_sizes: Vec<usize>,
/// Amount of confirmations required to consider L1 transaction committed.
/// If not specified L1 transaction will be considered finalized once its block is finalized.
pub wait_confirmations: Option<u64>,
Expand Down
1 change: 0 additions & 1 deletion core/lib/config/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ impl Distribution<configs::eth_sender::ProofLoadingMode> for EncodeDist {
impl Distribution<configs::eth_sender::SenderConfig> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> configs::eth_sender::SenderConfig {
configs::eth_sender::SenderConfig {
aggregated_proof_sizes: self.sample_collect(rng),
wait_confirmations: self.sample(rng),
tx_poll_period: self.sample(rng),
aggregate_tx_poll_period: self.sample(rng),
Expand Down

This file was deleted.

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

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

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

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

This file was deleted.

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

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

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

This file was deleted.

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 eth_txs
ALTER COLUMN predicted_gas_cost SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE eth_txs
ALTER COLUMN predicted_gas_cost DROP NOT NULL;
109 changes: 3 additions & 106 deletions core/lib/dal/src/blocks_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use std::{
};

use anyhow::Context as _;
use bigdecimal::{BigDecimal, FromPrimitive, ToPrimitive};
use bigdecimal::{BigDecimal, FromPrimitive};
use sqlx::types::chrono::{DateTime, Utc};
use zksync_db_connection::{
connection::Connection,
error::{DalResult, SqlxContext},
instrument::{InstrumentExt, Instrumented},
interpolate_query, match_query_as,
};
use zksync_types::{
aggregated_operations::AggregatedActionType,
Expand Down Expand Up @@ -2220,40 +2219,6 @@ impl BlocksDal<'_, '_> {
Ok(())
}

/// Returns sum of predicted gas costs on the given L1 batch range.
/// Panics if the sum doesn't fit into `u32`.
pub async fn get_l1_batches_predicted_gas(
&mut self,
number_range: ops::RangeInclusive<L1BatchNumber>,
op_type: AggregatedActionType,
) -> anyhow::Result<u32> {
#[derive(Debug)]
struct SumRow {
sum: BigDecimal,
}

let start = i64::from(number_range.start().0);
let end = i64::from(number_range.end().0);
let query = match_query_as!(
SumRow,
[
"SELECT COALESCE(SUM(", _, r#"), 0) AS "sum!" FROM l1_batches WHERE number BETWEEN $1 AND $2"#
],
match (op_type) {
AggregatedActionType::Commit => ("predicted_commit_gas_cost"; start, end),
AggregatedActionType::PublishProofOnchain => ("predicted_prove_gas_cost"; start, end),
AggregatedActionType::Execute => ("predicted_execute_gas_cost"; start, end),
}
);

query
.fetch_one(self.storage.conn())
.await?
.sum
.to_u32()
.context("Sum of predicted gas costs should fit into u32")
}

pub async fn get_l2_block_range_of_l1_batch(
&mut self,
l1_batch_number: L1BatchNumber,
Expand Down Expand Up @@ -2848,8 +2813,7 @@ impl BlocksDal<'_, '_> {

#[cfg(test)]
mod tests {
use zksync_contracts::BaseSystemContractsHashes;
use zksync_types::{tx::IncludedTxLocation, Address, ProtocolVersion, ProtocolVersionId};
use zksync_types::{tx::IncludedTxLocation, Address, ProtocolVersion};

use super::*;
use crate::{
Expand All @@ -2864,7 +2828,7 @@ mod tests {
vec![],
action_type,
Address::default(),
1,
Some(1),
None,
None,
false,
Expand Down Expand Up @@ -3064,71 +3028,4 @@ mod tests {
.unwrap()
.is_none());
}

#[tokio::test]
async fn getting_predicted_gas() {
let pool = ConnectionPool::<Core>::test_pool().await;
let mut conn = pool.connection().await.unwrap();
conn.protocol_versions_dal()
.save_protocol_version_with_tx(&ProtocolVersion::default())
.await
.unwrap();
let mut header = L1BatchHeader::new(
L1BatchNumber(1),
100,
BaseSystemContractsHashes::default(),
ProtocolVersionId::default(),
);
conn.blocks_dal()
.insert_l1_batch(
header.to_unsealed_header(BatchFeeInput::pubdata_independent(100, 100, 100)),
)
.await
.unwrap();
conn.blocks_dal()
.mark_l1_batch_as_sealed(&header, &[], &[], &[], Default::default())
.await
.unwrap();

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

let action_types_and_predicted_gas = [
(AggregatedActionType::Execute, 10),
(AggregatedActionType::Commit, 2),
(AggregatedActionType::PublishProofOnchain, 3),
];
for (action_type, expected_gas) in action_types_and_predicted_gas {
let gas = conn
.blocks_dal()
.get_l1_batches_predicted_gas(L1BatchNumber(1)..=L1BatchNumber(1), action_type)
.await
.unwrap();
assert_eq!(gas, expected_gas);

let gas = conn
.blocks_dal()
.get_l1_batches_predicted_gas(L1BatchNumber(2)..=L1BatchNumber(2), action_type)
.await
.unwrap();
assert_eq!(gas, 2 * expected_gas);

let gas = conn
.blocks_dal()
.get_l1_batches_predicted_gas(L1BatchNumber(1)..=L1BatchNumber(2), action_type)
.await
.unwrap();
assert_eq!(gas, 3 * expected_gas);
}
}
}
2 changes: 1 addition & 1 deletion core/lib/dal/src/blocks_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ mod tests {
vec![],
AggregatedActionType::Commit,
Address::default(),
0,
None,
None,
None,
false,
Expand Down
6 changes: 3 additions & 3 deletions core/lib/dal/src/eth_sender_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl EthSenderDal<'_, '_> {
raw_tx: Vec<u8>,
tx_type: AggregatedActionType,
contract_address: Address,
predicted_gas_cost: u32,
predicted_gas_cost: Option<u32>,
from_address: Option<Address>,
blob_sidecar: Option<EthTxBlobSidecar>,
is_gateway: bool,
Expand Down Expand Up @@ -259,7 +259,7 @@ impl EthSenderDal<'_, '_> {
nonce as i64,
tx_type.to_string(),
address,
i64::from(predicted_gas_cost),
predicted_gas_cost.map(|c| i64::from(c)),
from_address.as_ref().map(Address::as_bytes),
blob_sidecar.map(|sidecar| bincode::serialize(&sidecar)
.expect("can always bincode serialize EthTxBlobSidecar; qed")),
Expand Down Expand Up @@ -490,7 +490,7 @@ impl EthSenderDal<'_, '_> {
// Insert general tx descriptor.
let eth_tx_id = sqlx::query_scalar!(
"INSERT INTO eth_txs (raw_tx, nonce, tx_type, contract_address, predicted_gas_cost, created_at, updated_at) \
VALUES ('\\x00', 0, $1, '', 0, now(), now()) \
VALUES ('\\x00', 0, $1, '', NULL, now(), now()) \
RETURNING id",
tx_type.to_string()
)
Expand Down
4 changes: 2 additions & 2 deletions core/lib/dal/src/models/storage_eth_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct StorageEthTx {
pub has_failed: bool,
pub confirmed_eth_tx_history_id: Option<i32>,
pub gas_used: Option<i64>,
pub predicted_gas_cost: i64,
pub predicted_gas_cost: Option<i64>,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
// TODO (SMA-1614): remove the field
Expand Down Expand Up @@ -80,7 +80,7 @@ impl From<StorageEthTx> for EthTx {
raw_tx: tx.raw_tx.clone(),
tx_type: AggregatedActionType::from_str(&tx.tx_type).expect("Wrong agg type"),
created_at_timestamp: tx.created_at.and_utc().timestamp() as u64,
predicted_gas_cost: tx.predicted_gas_cost as u64,
predicted_gas_cost: tx.predicted_gas_cost.map(|c| c as u64),
from_addr: tx.from_addr.map(|f| Address::from_slice(&f)),
blob_sidecar: tx.blob_sidecar.map(|b| {
bincode::deserialize(&b).expect("EthTxBlobSidecar is encoded correctly; qed")
Expand Down
2 changes: 0 additions & 2 deletions core/lib/env_config/src/eth_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ mod tests {
(
EthConfig {
sender: Some(SenderConfig {
aggregated_proof_sizes: vec![1, 5],
aggregated_block_commit_deadline: 30,
aggregated_block_prove_deadline: 3_000,
aggregated_block_execute_deadline: 4_000,
Expand Down Expand Up @@ -124,7 +123,6 @@ mod tests {
ETH_SENDER_GAS_ADJUSTER_MAX_BLOB_BASE_FEE_SAMPLES="10"
ETH_SENDER_GAS_ADJUSTER_INTERNAL_PUBDATA_PRICING_MULTIPLIER="1.0"
ETH_SENDER_WAIT_FOR_PROOFS="false"
ETH_SENDER_SENDER_AGGREGATED_PROOF_SIZES="1,5"
ETH_SENDER_SENDER_MAX_AGGREGATED_BLOCKS_TO_COMMIT="3"
ETH_SENDER_SENDER_MAX_AGGREGATED_BLOCKS_TO_EXECUTE="4"
ETH_SENDER_SENDER_AGGREGATED_BLOCK_COMMIT_DEADLINE="30"
Expand Down
Loading

0 comments on commit 525b37a

Please sign in to comment.