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(api): Rework zks_getProtocolVersion #2146

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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

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.

45 changes: 23 additions & 22 deletions core/lib/dal/src/models/storage_protocol_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub struct StorageProtocolVersion {
pub recursion_circuits_set_vks_hash: Vec<u8>,
pub bootloader_code_hash: Vec<u8>,
pub default_account_code_hash: Vec<u8>,
pub upgrade_tx_hash: Option<Vec<u8>>,
}

pub(crate) fn protocol_version_from_storage(
Expand Down Expand Up @@ -56,35 +55,37 @@ pub(crate) fn protocol_version_from_storage(
}
}

impl From<StorageProtocolVersion> for api::ProtocolVersion {
fn from(storage_protocol_version: StorageProtocolVersion) -> Self {
#[derive(sqlx::FromRow)]
pub struct StorageApiProtocolVersion {
pub minor: i32,
pub timestamp: i64,
pub bootloader_code_hash: Vec<u8>,
pub default_account_code_hash: Vec<u8>,
pub upgrade_tx_hash: Option<Vec<u8>>,
}

impl From<StorageApiProtocolVersion> for api::ProtocolVersion {
#[allow(deprecated)]
fn from(storage_protocol_version: StorageApiProtocolVersion) -> Self {
let l2_system_upgrade_tx_hash = storage_protocol_version
.upgrade_tx_hash
.as_ref()
.map(|hash| H256::from_slice(hash));
api::ProtocolVersion {
version_id: storage_protocol_version.minor as u16,
version_id: Some(storage_protocol_version.minor as u16),
minor_version: Some(storage_protocol_version.minor as u16),
timestamp: storage_protocol_version.timestamp as u64,
verification_keys_hashes: L1VerifierConfig {
params: VerifierParams {
recursion_node_level_vk_hash: H256::from_slice(
&storage_protocol_version.recursion_node_level_vk_hash,
),
recursion_leaf_level_vk_hash: H256::from_slice(
&storage_protocol_version.recursion_leaf_level_vk_hash,
),
recursion_circuits_set_vks_hash: H256::from_slice(
&storage_protocol_version.recursion_circuits_set_vks_hash,
),
},
recursion_scheduler_level_vk_hash: H256::from_slice(
&storage_protocol_version.recursion_scheduler_level_vk_hash,
),
},
base_system_contracts: BaseSystemContractsHashes {
verification_keys_hashes: Some(Default::default()),
base_system_contracts: Some(BaseSystemContractsHashes {
bootloader: H256::from_slice(&storage_protocol_version.bootloader_code_hash),
default_aa: H256::from_slice(&storage_protocol_version.default_account_code_hash),
},
}),
bootloader_code_hash: Some(H256::from_slice(
&storage_protocol_version.bootloader_code_hash,
)),
default_account_code_hash: Some(H256::from_slice(
&storage_protocol_version.default_account_code_hash,
)),
l2_system_upgrade_tx_hash,
}
}
Expand Down
1 change: 0 additions & 1 deletion core/lib/dal/src/protocol_versions_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ impl ProtocolVersionsDal<'_, '_> {
protocol_versions.timestamp,
protocol_versions.bootloader_code_hash,
protocol_versions.default_account_code_hash,
protocol_versions.upgrade_tx_hash,
protocol_patches.patch,
protocol_patches.recursion_scheduler_level_vk_hash,
protocol_patches.recursion_node_level_vk_hash,
Expand Down
24 changes: 7 additions & 17 deletions core/lib/dal/src/protocol_versions_web3_dal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use zksync_db_connection::{connection::Connection, error::DalResult, instrument::InstrumentExt};
use zksync_types::api::ProtocolVersion;

use crate::{models::storage_protocol_version::StorageProtocolVersion, Core, CoreDal};
use crate::{models::storage_protocol_version::StorageApiProtocolVersion, Core, CoreDal};

#[derive(Debug)]
pub struct ProtocolVersionsWeb3Dal<'a, 'c> {
Expand All @@ -14,28 +14,18 @@ impl ProtocolVersionsWeb3Dal<'_, '_> {
version_id: u16,
) -> DalResult<Option<ProtocolVersion>> {
let storage_protocol_version = sqlx::query_as!(
StorageProtocolVersion,
StorageApiProtocolVersion,
r#"
SELECT
protocol_versions.id AS "minor!",
protocol_versions.timestamp,
protocol_versions.bootloader_code_hash,
protocol_versions.default_account_code_hash,
protocol_versions.upgrade_tx_hash,
protocol_patches.patch,
protocol_patches.recursion_scheduler_level_vk_hash,
protocol_patches.recursion_node_level_vk_hash,
protocol_patches.recursion_leaf_level_vk_hash,
protocol_patches.recursion_circuits_set_vks_hash
id AS "minor!",
timestamp,
bootloader_code_hash,
default_account_code_hash,
upgrade_tx_hash
FROM
protocol_versions
JOIN protocol_patches ON protocol_patches.minor = protocol_versions.id
WHERE
id = $1
ORDER BY
protocol_patches.patch DESC
LIMIT
1
"#,
i32::from(version_id)
)
Expand Down
54 changes: 50 additions & 4 deletions core/lib/types/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,15 +642,28 @@ impl From<Call> for DebugCall {

#[derive(Default, Serialize, Deserialize, Clone, Debug)]
pub struct ProtocolVersion {
perekopskiy marked this conversation as resolved.
Show resolved Hide resolved
/// Protocol version ID
pub version_id: u16,
/// Minor version of the protocol
#[deprecated]
pub version_id: Option<u16>,
/// Minor version of the protocol
#[serde(rename = "minorVersion")]
pub minor_version: Option<u16>,
/// Timestamp at which upgrade should be performed
pub timestamp: u64,
/// Verifier configuration
pub verification_keys_hashes: L1VerifierConfig,
#[deprecated]
pub verification_keys_hashes: Option<L1VerifierConfig>,
/// Hashes of base system contracts (bootloader and default account)
pub base_system_contracts: BaseSystemContractsHashes,
#[deprecated]
pub base_system_contracts: Option<BaseSystemContractsHashes>,
/// Bootloader code hash
#[serde(rename = "bootloaderCodeHash")]
pub bootloader_code_hash: Option<H256>,
/// Default account code hash
#[serde(rename = "defaultAccountCodeHash")]
pub default_account_code_hash: Option<H256>,
/// L2 Upgrade transaction hash
#[serde(rename = "l2SystemUpgradeTxHash")]
pub l2_system_upgrade_tx_hash: Option<H256>,
}

Expand Down Expand Up @@ -751,3 +764,36 @@ pub struct ApiStorageLog {
pub key: U256,
pub written_value: U256,
}

#[cfg(test)]
mod tests {
use super::*;

#[allow(deprecated)]
#[test]
fn check_protocol_version_type_compatibility() {
perekopskiy marked this conversation as resolved.
Show resolved Hide resolved
let new_version = ProtocolVersion {
version_id: Some(24),
minor_version: Some(24),
timestamp: 0,
verification_keys_hashes: Some(Default::default()),
base_system_contracts: Some(Default::default()),
bootloader_code_hash: Some(Default::default()),
default_account_code_hash: Some(Default::default()),
l2_system_upgrade_tx_hash: Default::default(),
};

#[derive(Deserialize)]
#[allow(dead_code)]
struct OldProtocolVersion {
pub version_id: u16,
pub timestamp: u64,
pub verification_keys_hashes: L1VerifierConfig,
pub base_system_contracts: BaseSystemContractsHashes,
pub l2_system_upgrade_tx_hash: Option<H256>,
}

serde_json::from_str::<OldProtocolVersion>(&serde_json::to_string(&new_version).unwrap())
.unwrap();
}
}
42 changes: 32 additions & 10 deletions core/node/node_sync/src/external_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,35 +337,57 @@ impl StateKeeperIO for ExternalIO {
.await
.context("failed to fetch protocol version from the main node")?
.context("protocol version is missing on the main node")?;
#[allow(deprecated)]
let (minor, bootloader_code_hash, default_account_code_hash) = {
let minor = protocol_version
.minor_version
.or(protocol_version.version_id)
.context("Missing minor protocol version")?;
let bootloader_code_hash = protocol_version
.bootloader_code_hash
.or_else(|| {
protocol_version
.base_system_contracts
.map(|hashes| hashes.bootloader)
})
.context("Missing bootloader code hash")?;
let default_account_code_hash = protocol_version
.default_account_code_hash
.or_else(|| {
protocol_version
.base_system_contracts
.map(|hashes| hashes.default_aa)
})
.context("Missing default account code hash")?;
perekopskiy marked this conversation as resolved.
Show resolved Hide resolved
(minor, bootloader_code_hash, default_account_code_hash)
};
self.pool
.connection_tagged("sync_layer")
.await?
.protocol_versions_dal()
.save_protocol_version(
ProtocolSemanticVersion {
minor: protocol_version
.version_id
minor: minor
.try_into()
.context("cannot convert protocol version")?,
patch: VersionPatch(0),
},
protocol_version.timestamp,
protocol_version.verification_keys_hashes,
protocol_version.base_system_contracts,
Default::default(), // verification keys are unused for EN
BaseSystemContractsHashes {
bootloader: bootloader_code_hash,
default_aa: default_account_code_hash,
},
protocol_version.l2_system_upgrade_tx_hash,
)
.await?;

let BaseSystemContractsHashes {
bootloader,
default_aa,
} = protocol_version.base_system_contracts;
let bootloader = self
.get_base_system_contract(bootloader, cursor.next_l2_block)
.get_base_system_contract(bootloader_code_hash, cursor.next_l2_block)
.await
.with_context(|| format!("cannot fetch bootloader code for {protocol_version:?}"))?;
let default_aa = self
.get_base_system_contract(default_aa, cursor.next_l2_block)
.get_base_system_contract(default_account_code_hash, cursor.next_l2_block)
.await
.with_context(|| format!("cannot fetch default AA code for {protocol_version:?}"))?;
Ok(BaseSystemContracts {
Expand Down
Loading
Loading