Skip to content

Commit

Permalink
chore: remove recalculate_miniblock_hashes method (matter-labs#494)
Browse files Browse the repository at this point in the history
## What ❔

Remove `recalculate_miniblock_hashes` method and other functions, that
are used only by it

## Why ❔

It's not 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).
- [ ] 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`.
  • Loading branch information
Artemka374 authored Nov 22, 2023
1 parent 6e4d228 commit 9aec8ef
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 244 deletions.
1 change: 0 additions & 1 deletion core/bin/external_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ async fn build_state_keeper(
chain_id,
)
.await;
io.recalculate_miniblock_hashes().await;

ZkSyncStateKeeper::without_sealer(stop_receiver, Box::new(io), batch_executor_base)
}
Expand Down
88 changes: 0 additions & 88 deletions core/lib/dal/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -744,26 +744,6 @@
},
"query": "SELECT number FROM l1_batches LEFT JOIN eth_txs_history AS prove_tx ON (l1_batches.eth_prove_tx_id = prove_tx.eth_tx_id) WHERE prove_tx.confirmed_at IS NOT NULL ORDER BY number DESC LIMIT 1"
},
"15135331e56e3e4e3eeae3aac609d8e8c7146d190dfe26c1a24f92d21cd34858": {
"describe": {
"columns": [
{
"name": "number",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Int8"
]
}
},
"query": "SELECT number from miniblocks where timestamp > $1 ORDER BY number ASC LIMIT 1"
},
"157fc4ef4f5fd831399219850bc59ec0bd32d938ec8685dacaf913efdccfe7fe": {
"describe": {
"columns": [
Expand Down Expand Up @@ -3861,33 +3841,6 @@
},
"query": "UPDATE storage SET value = u.value FROM UNNEST($1::bytea[], $2::bytea[]) AS u(key, value) WHERE u.key = hashed_key"
},
"400bb5f012b95f5b327a65bf8a55e61a9e41a8040f546d75b9b8aa6be45e78d5": {
"describe": {
"columns": [
{
"name": "number",
"ordinal": 0,
"type_info": "Int8"
},
{
"name": "hash",
"ordinal": 1,
"type_info": "Bytea"
}
],
"nullable": [
false,
false
],
"parameters": {
"Left": [
"Int4",
"Int8"
]
}
},
"query": "SELECT number, hash FROM miniblocks WHERE protocol_version = $1 ORDER BY number DESC LIMIT $2"
},
"4029dd84cde963ed8541426a659b10ccdbacbf4392664e34bfc29737aa630b28": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -9378,19 +9331,6 @@
},
"query": "\n UPDATE leaf_aggregation_witness_jobs_fri\n SET status ='failed', error= $1, updated_at = now()\n WHERE id = $2\n "
},
"b14d9a82e6b0a4174dde61642d3abc001cd8cb80d988eb81a685255e3ce920de": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8Array",
"ByteaArray"
]
}
},
"query": "UPDATE miniblocks SET hash = u.hash FROM UNNEST($1::bigint[], $2::bytea[]) AS u(number, hash) WHERE miniblocks.number = u.number\n "
},
"b250f4cb646081c8c0296a286d3fd921a1aefb310951a1ea25ec0fc533ed32ab": {
"describe": {
"columns": [
Expand Down Expand Up @@ -10850,34 +10790,6 @@
},
"query": "INSERT INTO contract_verification_requests ( contract_address, source_code, contract_name, zk_compiler_version, compiler_version, optimization_used, optimizer_mode, constructor_arguments, is_system, status, created_at, updated_at )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, 'queued', now(), now()) RETURNING id"
},
"e409b39a5e62a3a4ec5d3b6aae4935c13b93129a22ffe6a0b68b5ade1f6082c8": {
"describe": {
"columns": [
{
"name": "number",
"ordinal": 0,
"type_info": "Int8"
},
{
"name": "hash",
"ordinal": 1,
"type_info": "Bytea"
}
],
"nullable": [
false,
false
],
"parameters": {
"Left": [
"Int8",
"Int4",
"Int8"
]
}
},
"query": "SELECT number, hash FROM miniblocks WHERE number >= $1 and protocol_version = $2 ORDER BY number LIMIT $3"
},
"e429061bd0f67910ad8676a34f2b89a051a6df3097c8afde81a491c342a10e3a": {
"describe": {
"columns": [
Expand Down
94 changes: 0 additions & 94 deletions core/lib/dal/src/blocks_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,75 +36,6 @@ impl BlocksDal<'_, '_> {
Ok(count == 0)
}

pub async fn get_miniblock_hashes_from_date(
&mut self,
timestamp: u64,
limit: u32,
version: ProtocolVersionId,
) -> sqlx::Result<Vec<(MiniblockNumber, H256)>> {
let number = sqlx::query!(
"SELECT number from miniblocks where timestamp > $1 ORDER BY number ASC LIMIT 1",
timestamp as i64
)
.fetch_one(self.storage.conn())
.await?
.number;
self.storage
.blocks_dal()
.get_miniblocks_since_block(number, limit, version)
.await
}

pub async fn get_last_miniblocks_for_version(
&mut self,
limit: u32,
version: ProtocolVersionId,
) -> sqlx::Result<Vec<(MiniblockNumber, H256)>> {
let minibloks = sqlx::query!(
"SELECT number, hash FROM miniblocks WHERE protocol_version = $1 ORDER BY number DESC LIMIT $2",
version as i32,
limit as i32
)
.fetch_all(self.storage.conn())
.await?
.iter()
.map(|block| {
(
MiniblockNumber(block.number as u32),
H256::from_slice(&block.hash),
)
})
.collect();

Ok(minibloks)
}

pub async fn get_miniblocks_since_block(
&mut self,
number: i64,
limit: u32,
version: ProtocolVersionId,
) -> sqlx::Result<Vec<(MiniblockNumber, H256)>> {
let minibloks = sqlx::query!(
"SELECT number, hash FROM miniblocks WHERE number >= $1 and protocol_version = $2 ORDER BY number LIMIT $3",
number,
version as i32,
limit as i32
)
.fetch_all(self.storage.conn())
.await?
.iter()
.map(|block| {
(
MiniblockNumber(block.number as u32),
H256::from_slice(&block.hash),
)
})
.collect();

Ok(minibloks)
}

pub async fn get_sealed_l1_batch_number(&mut self) -> anyhow::Result<L1BatchNumber> {
let number = sqlx::query!(
"SELECT MAX(number) as \"number\" FROM l1_batches WHERE is_finished = TRUE"
Expand Down Expand Up @@ -465,7 +396,6 @@ impl BlocksDal<'_, '_> {
.await?;
Ok(())
}

/// Sets consensus-related fields for the specified miniblock.
pub async fn set_miniblock_consensus_fields(
&mut self,
Expand All @@ -487,30 +417,6 @@ impl BlocksDal<'_, '_> {
Ok(())
}

pub async fn update_hashes(
&mut self,
number_and_hashes: &[(MiniblockNumber, H256)],
) -> sqlx::Result<()> {
let mut numbers = vec![];
let mut hashes = vec![];
for (number, hash) in number_and_hashes {
numbers.push(number.0 as i64);
hashes.push(hash.as_bytes().to_vec());
}

sqlx::query!(
"UPDATE miniblocks SET hash = u.hash \
FROM UNNEST($1::bigint[], $2::bytea[]) AS u(number, hash) \
WHERE miniblocks.number = u.number
",
&numbers,
&hashes
)
.execute(self.storage.conn())
.await?;
Ok(())
}

pub async fn get_last_sealed_miniblock_header(
&mut self,
) -> sqlx::Result<Option<MiniblockHeader>> {
Expand Down
64 changes: 3 additions & 61 deletions core/lib/zksync_core/src/sync_layer/external_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use multivm::interface::{FinishedL1Batch, L1BatchEnv, SystemEnv};
use zksync_contracts::{BaseSystemContracts, SystemContractCode};
use zksync_dal::ConnectionPool;
use zksync_types::{
block::legacy_miniblock_hash, ethabi::Address, l1::L1Tx, l2::L2Tx,
protocol_version::ProtocolUpgradeTx, witness_block_state::WitnessBlockState, L1BatchNumber,
L1BlockNumber, L2ChainId, MiniblockNumber, ProtocolVersionId, Transaction, H256, U256,
ethabi::Address, l1::L1Tx, l2::L2Tx, protocol_version::ProtocolUpgradeTx,
witness_block_state::WitnessBlockState, L1BatchNumber, L1BlockNumber, L2ChainId,
MiniblockNumber, ProtocolVersionId, Transaction, H256, U256,
};
use zksync_utils::{be_words_to_bytes, bytes_to_be_words};

Expand Down Expand Up @@ -106,64 +106,6 @@ impl ExternalIO {
}
}

pub async fn recalculate_miniblock_hashes(&self) {
let mut storage = self.pool.access_storage_tagged("sync_layer").await.unwrap();
let last_blocks: Vec<_> = storage
.blocks_dal()
.get_last_miniblocks_for_version(5, ProtocolVersionId::Version12)
.await
.unwrap();

// All last miniblocks are good that means we have already applied this migrations
if last_blocks
.into_iter()
.all(|(number, hash)| legacy_miniblock_hash(number) == hash)
{
return;
}

// August 29 2023
let timestamp = 1693267200;
let mut miniblock_and_hashes = storage
.blocks_dal()
.get_miniblock_hashes_from_date(timestamp, 1000, ProtocolVersionId::Version12)
.await
.unwrap();

let mut updated_hashes = vec![];

let mut last_miniblock_number = 0;
while !miniblock_and_hashes.is_empty() {
for (number, hash) in miniblock_and_hashes {
if hash != legacy_miniblock_hash(number) {
updated_hashes.push((number, legacy_miniblock_hash(number)))
}
last_miniblock_number = number.0;
}
if !updated_hashes.is_empty() {
storage
.blocks_dal()
.update_hashes(&updated_hashes)
.await
.unwrap();
updated_hashes = vec![];
}

miniblock_and_hashes = storage
.blocks_dal()
.get_miniblocks_since_block(
last_miniblock_number as i64 + 1,
1000,
ProtocolVersionId::Version12,
)
.await
.unwrap();
tracing::info!("Last updated miniblock {}", last_miniblock_number);
}

tracing::info!("Finish the hash recalculation")
}

async fn load_previous_l1_batch_hash(&self) -> U256 {
let mut storage = self.pool.access_storage_tagged("sync_layer").await.unwrap();

Expand Down

0 comments on commit 9aec8ef

Please sign in to comment.