Skip to content

Commit

Permalink
feat(api): Sunset API translator (#675)
Browse files Browse the repository at this point in the history
## What ❔

- Removes API translator component, `zks_getLogsWithVirtualBlocks `
method, and related no-longer-needed code.

## Why ❔

- Component was introduced for the period of the block.timestamp
migration; it's no longer needed.

## 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.
- [ ] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
- [x] Spellcheck has been run via `cargo spellcheck
--cfg=./spellcheck/era.cfg --code 1`.
  • Loading branch information
popzxc committed Dec 13, 2023
1 parent f3c3bf5 commit 846fd33
Show file tree
Hide file tree
Showing 16 changed files with 13 additions and 559 deletions.
6 changes: 0 additions & 6 deletions core/lib/constants/src/system_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ pub const CURRENT_VIRTUAL_BLOCK_INFO_POSITION: H256 = H256([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0c,
]);

// It is equal to CURRENT_VIRTUAL_BLOCK_INFO_POSITION + 1
pub const VIRTUIAL_BLOCK_UPGRADE_INFO_POSITION: H256 = H256([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0d,
]);

/// Block info is stored compactly as SYSTEM_BLOCK_INFO_BLOCK_NUMBER_MULTIPLIER * block_number + block_timestamp.
/// This number is equal to 2**128
pub const SYSTEM_BLOCK_INFO_BLOCK_NUMBER_MULTIPLIER: U256 = U256([0, 0, 1, 0]);
42 changes: 0 additions & 42 deletions core/lib/dal/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -4592,27 +4592,6 @@
},
"query": "SELECT base_fee_per_gas FROM miniblocks WHERE number <= $1 ORDER BY number DESC LIMIT $2"
},
"58ae859333cf7fadbb83d9cde66dee2abe18b4883f883e69130024d11a4a5cc6": {
"describe": {
"columns": [
{
"name": "number",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Int8",
"Numeric"
]
}
},
"query": "SELECT number FROM ( SELECT number, sum(virtual_blocks) OVER(ORDER BY number) AS virtual_block_sum FROM miniblocks WHERE l1_batch_number >= $1 ) AS vts WHERE virtual_block_sum <= $2 ORDER BY number DESC LIMIT 1"
},
"5922fdf40632a6ffecfe824a3ba29bcf7b379aff5253db2739cc7be6145524e8": {
"describe": {
"columns": [
Expand Down Expand Up @@ -4722,27 +4701,6 @@
},
"query": "SELECT fee_account_address FROM l1_batches WHERE number = $1"
},
"5a31eab41a980cc82ad3609610d377a185ce38bd654ee93766c119aa6cae1040": {
"describe": {
"columns": [
{
"name": "number",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Int8",
"Numeric"
]
}
},
"query": "SELECT number FROM ( SELECT number, sum(virtual_blocks) OVER(ORDER BY number) AS virtual_block_sum FROM miniblocks WHERE l1_batch_number >= $1 ) AS vts WHERE virtual_block_sum >= $2 ORDER BY number LIMIT 1"
},
"5a5844af61cc685a414fcd3cad70900bdce8f48e905c105f8dd50dc52e0c6f14": {
"describe": {
"columns": [
Expand Down
174 changes: 0 additions & 174 deletions core/lib/dal/src/blocks_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,94 +499,10 @@ impl BlocksWeb3Dal<'_, '_> {
Ok(l1_batch_details.map(api::L1BatchDetails::from))
}
}

pub async fn get_miniblock_for_virtual_block_from(
&mut self,
migration_start_l1_batch_number: u64,
from_virtual_block_number: u64,
) -> sqlx::Result<Option<u32>> {
// Since virtual blocks are numerated from `migration_start_l1_batch_number` number and not from 0
// we have to subtract (migration_start_l1_batch_number - 1) from the `from` virtual block
// to find miniblock using query below
let virtual_block_offset = from_virtual_block_number - migration_start_l1_batch_number + 1;

// In the query below `virtual_block_sum` is actually latest virtual block number, created within this miniblock
// and that can be calculated as sum of all virtual blocks counts, created in previous miniblocks.
// It is considered that all logs are created in the last virtual block of this miniblock,
// that's why we are interested in funding it.
// The goal of this query is to find the first miniblock, which contains given virtual block.
let record = sqlx::query!(
"SELECT number \
FROM ( \
SELECT number, sum(virtual_blocks) OVER(ORDER BY number) AS virtual_block_sum \
FROM miniblocks \
WHERE l1_batch_number >= $1 \
) AS vts \
WHERE virtual_block_sum >= $2 \
ORDER BY number LIMIT 1",
migration_start_l1_batch_number as i64,
virtual_block_offset as i64
)
.instrument("get_miniblock_for_virtual_block_from")
.with_arg(
"migration_start_l1_batch_number",
&migration_start_l1_batch_number,
)
.report_latency()
.fetch_optional(self.storage.conn())
.await?;

let result = record.map(|row| row.number as u32);

Ok(result)
}

pub async fn get_miniblock_for_virtual_block_to(
&mut self,
migration_start_l1_batch_number: u64,
to_virtual_block_number: u64,
) -> sqlx::Result<Option<u32>> {
// Since virtual blocks are numerated from `migration_start_l1_batch_number` number and not from 0
// we have to subtract (migration_start_l1_batch_number - 1) from the `to` virtual block
// to find miniblock using query below
let virtual_block_offset = to_virtual_block_number - migration_start_l1_batch_number + 1;

// In the query below `virtual_block_sum` is actually latest virtual block number, created within this miniblock
// and that can be calculated as sum of all virtual blocks counts, created in previous miniblocks.
// It is considered that all logs are created in the last virtual block of this miniblock,
// that's why we are interested in funding it.
// The goal of this query is to find the last miniblock, that contains logs all logs(in the last virtual block),
// created before or in a given virtual block.
let record = sqlx::query!(
"SELECT number \
FROM ( \
SELECT number, sum(virtual_blocks) OVER(ORDER BY number) AS virtual_block_sum \
FROM miniblocks \
WHERE l1_batch_number >= $1 \
) AS vts \
WHERE virtual_block_sum <= $2 \
ORDER BY number DESC LIMIT 1",
migration_start_l1_batch_number as i64,
virtual_block_offset as i64
)
.instrument("get_miniblock_for_virtual_block_to")
.with_arg(
"migration_start_l1_batch_number",
&migration_start_l1_batch_number,
)
.report_latency()
.fetch_optional(self.storage.conn())
.await?;

let result = record.map(|row| row.number as u32);

Ok(result)
}
}

#[cfg(test)]
mod tests {
use zksync_contracts::BaseSystemContractsHashes;
use zksync_types::{
block::{MiniblockHasher, MiniblockHeader},
MiniblockNumber, ProtocolVersion, ProtocolVersionId,
Expand Down Expand Up @@ -758,94 +674,4 @@ mod tests {
.await;
assert_eq!(miniblock_number.unwrap(), None);
}

#[tokio::test]
async fn getting_miniblocks_for_virtual_block() {
let connection_pool = ConnectionPool::test_pool().await;
let mut conn = connection_pool.access_storage().await.unwrap();

conn.protocol_versions_dal()
.save_protocol_version_with_tx(ProtocolVersion::default())
.await;

let mut header = MiniblockHeader {
number: MiniblockNumber(0),
timestamp: 0,
hash: MiniblockHasher::new(MiniblockNumber(0), 0, H256::zero())
.finalize(ProtocolVersionId::latest()),
l1_tx_count: 0,
l2_tx_count: 0,
base_fee_per_gas: 100,
l1_gas_price: 100,
l2_fair_gas_price: 100,
base_system_contracts_hashes: BaseSystemContractsHashes::default(),
protocol_version: Some(ProtocolVersionId::default()),
virtual_blocks: 0,
};
conn.blocks_dal().insert_miniblock(&header).await.unwrap();
conn.blocks_dal()
.mark_miniblocks_as_executed_in_l1_batch(L1BatchNumber(0))
.await
.unwrap();

header.number = MiniblockNumber(1);
conn.blocks_dal().insert_miniblock(&header).await.unwrap();
conn.blocks_dal()
.mark_miniblocks_as_executed_in_l1_batch(L1BatchNumber(1))
.await
.unwrap();

for i in 2..=100 {
header.number = MiniblockNumber(i);
header.virtual_blocks = 5;

conn.blocks_dal().insert_miniblock(&header).await.unwrap();
conn.blocks_dal()
.mark_miniblocks_as_executed_in_l1_batch(L1BatchNumber(i))
.await
.unwrap();
}

let virtual_block_ranges = [
(2, 4),
(20, 24),
(11, 15),
(1, 10),
(88, 99),
(1, 100),
(1000000, 10000000),
];
let expected_miniblock_ranges = [
(Some(2), Some(1)),
(Some(5), Some(5)),
(Some(4), Some(4)),
(Some(2), Some(3)),
(Some(19), Some(20)),
(Some(2), Some(21)),
(None, Some(100)),
];

let inputs_with_expected_values =
IntoIterator::into_iter(virtual_block_ranges).zip(expected_miniblock_ranges);
for (
(virtual_block_start, virtual_block_end),
(expected_miniblock_from, expected_miniblock_to),
) in inputs_with_expected_values
{
// migration_start_l1_batch_number = 1
let miniblock_from = conn
.blocks_web3_dal()
.get_miniblock_for_virtual_block_from(1, virtual_block_start)
.await
.unwrap();
assert_eq!(miniblock_from, expected_miniblock_from);

let miniblock_to = conn
.blocks_web3_dal()
.get_miniblock_for_virtual_block_to(1, virtual_block_end)
.await
.unwrap();
assert_eq!(miniblock_to, expected_miniblock_to);
}
}
}
8 changes: 0 additions & 8 deletions core/lib/types/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,6 @@ pub fn pack_block_info(block_number: u64, block_timestamp: u64) -> U256 {
+ U256::from(block_timestamp)
}

/// Returns virtual_block_start_batch and virtual_block_finish_l2_block based on the virtual block upgrade information
pub fn unpack_block_upgrade_info(info: U256) -> (u64, u64) {
// its safe to use SYSTEM_BLOCK_INFO_BLOCK_NUMBER_MULTIPLIER here, since VirtualBlockUpgradeInfo and BlockInfo are packed same way
let virtual_block_start_batch = (info / SYSTEM_BLOCK_INFO_BLOCK_NUMBER_MULTIPLIER).as_u64();
let virtual_block_finish_l2_block = (info % SYSTEM_BLOCK_INFO_BLOCK_NUMBER_MULTIPLIER).as_u64();
(virtual_block_start_batch, virtual_block_finish_l2_block)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 0 additions & 2 deletions core/lib/web3_decl/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ pub enum Web3Error {
LogsLimitExceeded(usize, u32, u32),
#[error("invalid filter: if blockHash is supplied fromBlock and toBlock must not be")]
InvalidFilterBlockHash,
#[error("Query returned more than {0} results. Try smaller range of blocks")]
TooManyLogs(usize),
#[error("Tree API is not available")]
TreeApiUnavailable,
}
5 changes: 1 addition & 4 deletions core/lib/web3_decl/src/namespaces/zks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use zksync_types::{
Address, L1BatchNumber, MiniblockNumber, H256, U256, U64,
};

use crate::types::{Filter, Log, Token};
use crate::types::Token;

#[cfg_attr(
all(feature = "client", feature = "server"),
Expand Down Expand Up @@ -107,9 +107,6 @@ pub trait ZksNamespace {
version_id: Option<u16>,
) -> RpcResult<Option<ProtocolVersion>>;

#[method(name = "getLogsWithVirtualBlocks")]
async fn get_logs_with_virtual_blocks(&self, filter: Filter) -> RpcResult<Vec<Log>>;

#[method(name = "getProof")]
async fn get_proof(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub fn into_jsrpc_error(err: Web3Error) -> Error {
| Web3Error::FilterNotFound
| Web3Error::InvalidFeeParams(_)
| Web3Error::LogsLimitExceeded(_, _, _)
| Web3Error::TooManyLogs(_)
| Web3Error::InvalidFilterBlockHash => ErrorCode::InvalidParams,
Web3Error::SubmitTransactionError(_, _) | Web3Error::SerializationError(_) => 3.into(),
Web3Error::PubSubTimeout => 4.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use zksync_types::{
transaction_request::CallRequest,
Address, L1BatchNumber, MiniblockNumber, H256, U256, U64,
};
use zksync_web3_decl::types::{Filter, Log, Token};
use zksync_web3_decl::types::Token;

use crate::{
l1_gas_price::L1GasPriceProvider,
Expand Down Expand Up @@ -106,9 +106,6 @@ pub trait ZksNamespaceT {
version_id: Option<u16>,
) -> BoxFuture<Result<Option<ProtocolVersion>>>;

#[rpc(name = "zks_getLogsWithVirtualBlocks")]
fn get_logs_with_virtual_blocks(&self, filter: Filter) -> BoxFuture<Result<Vec<Log>>>;

#[rpc(name = "zks_getProof")]
fn get_proof(
&self,
Expand Down Expand Up @@ -304,16 +301,6 @@ impl<G: L1GasPriceProvider + Send + Sync + 'static> ZksNamespaceT for ZksNamespa
Box::pin(async move { Ok(self_.get_protocol_version_impl(version_id).await) })
}

fn get_logs_with_virtual_blocks(&self, filter: Filter) -> BoxFuture<Result<Vec<Log>>> {
let self_ = self.clone();
Box::pin(async move {
self_
.get_logs_with_virtual_blocks_impl(filter)
.await
.map_err(into_jsrpc_error)
})
}

fn get_proof(
&self,
address: Address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ pub fn into_jsrpc_error(err: Web3Error) -> ErrorObjectOwned {
| Web3Error::FilterNotFound
| Web3Error::InvalidFeeParams(_)
| Web3Error::InvalidFilterBlockHash
| Web3Error::LogsLimitExceeded(_, _, _)
| Web3Error::TooManyLogs(_) => ErrorCode::InvalidParams.code(),
| Web3Error::LogsLimitExceeded(_, _, _) => ErrorCode::InvalidParams.code(),
Web3Error::SubmitTransactionError(_, _) | Web3Error::SerializationError(_) => 3,
Web3Error::PubSubTimeout => 4,
Web3Error::RequestTimeout => 5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use zksync_types::{
use zksync_web3_decl::{
jsonrpsee::core::{async_trait, RpcResult},
namespaces::zks::ZksNamespaceServer,
types::{Filter, Log, Token},
types::Token,
};

use crate::{
Expand Down Expand Up @@ -152,12 +152,6 @@ impl<G: L1GasPriceProvider + Send + Sync + 'static> ZksNamespaceServer for ZksNa
Ok(self.get_protocol_version_impl(version_id).await)
}

async fn get_logs_with_virtual_blocks(&self, filter: Filter) -> RpcResult<Vec<Log>> {
self.get_logs_with_virtual_blocks_impl(filter)
.await
.map_err(into_jsrpc_error)
}

async fn get_proof(
&self,
address: Address,
Expand Down
Loading

0 comments on commit 846fd33

Please sign in to comment.