forked from matter-labs/zksync-era
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Use protocol version v22 as latest (matter-labs#1432)
## What ❔ - Use protocol version v22 as latest - Migration for syncing v22 batches/miniblock for ENs. ## Why ❔ - There was a protocol upgrade v22 - We had to update protocol version for already sealed batches in main node DB. ENs should resync their data. ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted via `zk fmt` and `zk lint`. - [ ] Spellcheck has been run via `zk spellcheck`. - [ ] Linkcheck has been run via `zk linkcheck`.
- Loading branch information
1 parent
8bf37ac
commit 1757412
Showing
8 changed files
with
284 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
use std::cmp::Ordering; | ||
|
||
use anyhow::Context; | ||
use zksync_basic_types::{L1BatchNumber, MiniblockNumber}; | ||
use zksync_dal::{ConnectionPool, Server, ServerDals}; | ||
use zksync_types::ProtocolVersionId; | ||
use zksync_web3_decl::{ | ||
jsonrpsee::http_client::HttpClient, | ||
namespaces::{EnNamespaceClient, ZksNamespaceClient}, | ||
}; | ||
|
||
pub async fn get_l1_batch_remote_protocol_version( | ||
main_node_client: &HttpClient, | ||
l1_batch_number: L1BatchNumber, | ||
) -> anyhow::Result<Option<ProtocolVersionId>> { | ||
let Some((miniblock, _)) = main_node_client | ||
.get_miniblock_range(l1_batch_number) | ||
.await? | ||
else { | ||
return Ok(None); | ||
}; | ||
let sync_block = main_node_client | ||
.sync_l2_block(MiniblockNumber(miniblock.as_u32()), false) | ||
.await?; | ||
Ok(sync_block.map(|b| b.protocol_version)) | ||
} | ||
|
||
// Synchronizes protocol version in `l1_batches` and `miniblocks` tables between EN and main node. | ||
pub async fn sync_versions( | ||
connection_pool: ConnectionPool<Server>, | ||
main_node_client: HttpClient, | ||
) -> anyhow::Result<()> { | ||
tracing::info!("Starting syncing protocol version of blocks"); | ||
|
||
let mut connection = connection_pool.access_storage().await?; | ||
|
||
// Load the first local batch number with version 22. | ||
let Some(local_first_v22_l1_batch) = connection | ||
.blocks_dal() | ||
.get_first_l1_batch_number_for_version(ProtocolVersionId::Version22) | ||
.await? | ||
else { | ||
return Ok(()); | ||
}; | ||
tracing::info!("First local v22 batch is #{local_first_v22_l1_batch}"); | ||
|
||
// Find the first remote batch with version 22, assuming it's less then or equal than local one. | ||
// Uses binary search. | ||
|
||
let mut left_bound = L1BatchNumber(0); | ||
let mut right_bound = local_first_v22_l1_batch; | ||
|
||
let right_bound_remote_version = | ||
get_l1_batch_remote_protocol_version(&main_node_client, right_bound).await?; | ||
if right_bound_remote_version != Some(ProtocolVersionId::Version22) { | ||
anyhow::bail!("Remote protocol versions should be v22 for the first local v22 batch, got {right_bound_remote_version:?}"); | ||
} | ||
|
||
while left_bound < right_bound { | ||
let mid_batch = L1BatchNumber((left_bound.0 + right_bound.0) / 2); | ||
let (mid_miniblock, _) = connection | ||
.blocks_dal() | ||
.get_miniblock_range_of_l1_batch(mid_batch) | ||
.await | ||
.with_context(|| format!("Failed to get miniblock range for L1 batch #{mid_batch}"))? | ||
.with_context(|| { | ||
format!("Postgres is inconsistent: missing miniblocks for L1 batch #{mid_batch}") | ||
})?; | ||
let mid_protocol_version = main_node_client | ||
.sync_l2_block(mid_miniblock, false) | ||
.await? | ||
.with_context(|| format!("Main node missing data about miniblock #{mid_miniblock}"))? | ||
.protocol_version; | ||
|
||
match mid_protocol_version.cmp(&ProtocolVersionId::Version22) { | ||
Ordering::Less => { | ||
left_bound = mid_batch + 1; | ||
} | ||
Ordering::Equal => { | ||
right_bound = mid_batch; | ||
} | ||
Ordering::Greater => { | ||
anyhow::bail!("Unexpected remote protocol version: {mid_protocol_version:?} for miniblock #{mid_miniblock}"); | ||
} | ||
} | ||
} | ||
|
||
let remote_first_v22_l1_batch = left_bound; | ||
let (remote_first_v22_miniblock, _) = connection | ||
.blocks_dal() | ||
.get_miniblock_range_of_l1_batch(remote_first_v22_l1_batch) | ||
.await | ||
.with_context(|| format!("Failed to get miniblock range for L1 batch #{remote_first_v22_l1_batch}"))? | ||
.with_context(|| { | ||
format!("Postgres is inconsistent: missing miniblocks for L1 batch #{remote_first_v22_l1_batch}") | ||
})?; | ||
|
||
let mut transaction = connection.start_transaction().await?; | ||
|
||
tracing::info!( | ||
"Setting version 22 for batches {remote_first_v22_l1_batch}..={local_first_v22_l1_batch}" | ||
); | ||
transaction | ||
.blocks_dal() | ||
.reset_protocol_version_for_l1_batches( | ||
remote_first_v22_l1_batch..=local_first_v22_l1_batch, | ||
ProtocolVersionId::Version22, | ||
) | ||
.await?; | ||
|
||
let (local_first_v22_miniblock, _) = transaction | ||
.blocks_dal() | ||
.get_miniblock_range_of_l1_batch(local_first_v22_l1_batch) | ||
.await | ||
.with_context(|| format!("Failed to get miniblock range for L1 batch #{local_first_v22_l1_batch}"))? | ||
.with_context(|| { | ||
format!("Postgres is inconsistent: missing miniblocks for L1 batch #{local_first_v22_l1_batch}") | ||
})?; | ||
|
||
tracing::info!("Setting version 22 for miniblocks {remote_first_v22_miniblock}..={local_first_v22_miniblock}"); | ||
transaction | ||
.blocks_dal() | ||
.reset_protocol_version_for_miniblocks( | ||
remote_first_v22_miniblock..=local_first_v22_miniblock, | ||
ProtocolVersionId::Version22, | ||
) | ||
.await?; | ||
|
||
transaction.commit().await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...lib/dal/.sqlx/query-00220170d8f9e577321a0522337a7db7673811ac181e801a16a7aefc984d60b0.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
...lib/dal/.sqlx/query-86cbe509988c8775bcf738d5cb1edac2f0db60c263c1564b64c717f8ae53e44d.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
...lib/dal/.sqlx/query-e673789d5b4a7aae11feccb085bf3f9dd37b771cf76261762ae18fd14cf0efc5.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters