Skip to content

Commit

Permalink
fix(zk_toolbox): Fix protocol version (#2118)
Browse files Browse the repository at this point in the history
## What ❔

Fix zk toolbox for using semantic protocol version

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## 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`.

Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo authored May 31, 2024
1 parent f99739b commit 67f6080
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 4 deletions.
1 change: 1 addition & 0 deletions zk_toolbox/Cargo.lock

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
@@ -1,6 +1,9 @@
use std::{collections::HashMap, str::FromStr};

use ethers::types::{Address, H256};
use ethers::{
prelude::U256,
types::{Address, H256},
};
use rand::Rng;
use serde::{Deserialize, Serialize};
use types::ChainId;
Expand Down Expand Up @@ -146,7 +149,7 @@ impl DeployL1Config {
genesis_batch_commitment: genesis_config.genesis_batch_commitment,
genesis_rollup_leaf_index: genesis_config.genesis_rollup_leaf_index,
genesis_root: genesis_config.genesis_root,
latest_protocol_version: genesis_config.genesis_protocol_version,
latest_protocol_version: genesis_config.genesis_protocol_semantic_version.pack(),
recursion_circuits_set_vks_hash: H256::zero(),
recursion_leaf_level_vk_hash: H256::zero(),
recursion_node_level_vk_hash: H256::zero(),
Expand All @@ -173,7 +176,7 @@ pub struct ContractsDeployL1Config {
pub genesis_root: H256,
pub genesis_rollup_leaf_index: u32,
pub genesis_batch_commitment: H256,
pub latest_protocol_version: u64,
pub latest_protocol_version: U256,
pub recursion_node_level_vk_hash: H256,
pub recursion_leaf_level_vk_hash: H256,
pub recursion_circuits_set_vks_hash: H256,
Expand Down
3 changes: 2 additions & 1 deletion zk_toolbox/crates/config/src/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ethers::types::{Address, H256};
use serde::{Deserialize, Serialize};
use types::{ChainId, L1BatchCommitDataGeneratorMode};
use types::{ChainId, L1BatchCommitDataGeneratorMode, ProtocolSemanticVersion};

use crate::{consts::GENESIS_FILE, traits::FileConfigWithDefaultName};

Expand All @@ -16,6 +16,7 @@ pub struct GenesisConfig {
pub genesis_rollup_leaf_index: u32,
pub genesis_root: H256,
pub genesis_protocol_version: u64,
pub genesis_protocol_semantic_version: ProtocolSemanticVersion,
#[serde(flatten)]
pub other: serde_json::Value,
}
Expand Down
1 change: 1 addition & 0 deletions zk_toolbox/crates/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ ethers.workspace = true
serde.workspace = true
strum.workspace = true
strum_macros.workspace = true
thiserror.workspace = true
2 changes: 2 additions & 0 deletions zk_toolbox/crates/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ mod base_token;
mod chain_id;
mod l1_batch_commit_data_generator_mode;
mod l1_network;
mod protocol_version;
mod prover_mode;
mod wallet_creation;

pub use base_token::*;
pub use chain_id::*;
pub use l1_batch_commit_data_generator_mode::*;
pub use l1_network::*;
pub use protocol_version::ProtocolSemanticVersion;
pub use prover_mode::*;
pub use wallet_creation::*;
93 changes: 93 additions & 0 deletions zk_toolbox/crates/types/src/protocol_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::{fmt, num::ParseIntError, str::FromStr};

use ethers::prelude::U256;
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};

pub const PACKED_SEMVER_MINOR_OFFSET: u32 = 32;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ProtocolSemanticVersion {
pub minor: u16,
pub patch: u16,
}

impl ProtocolSemanticVersion {
const MAJOR_VERSION: u8 = 0;

pub fn new(minor: u16, patch: u16) -> Self {
Self { minor, patch }
}

pub fn pack(&self) -> U256 {
(U256::from(self.minor) << U256::from(PACKED_SEMVER_MINOR_OFFSET)) | U256::from(self.patch)
}
}

impl fmt::Display for ProtocolSemanticVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}.{}.{}",
Self::MAJOR_VERSION,
self.minor as u16,
self.patch
)
}
}

#[derive(Debug, thiserror::Error)]
pub enum ParseProtocolSemanticVersionError {
#[error("invalid format")]
InvalidFormat,
#[error("non zero major version")]
NonZeroMajorVersion,
#[error("{0}")]
ParseIntError(ParseIntError),
}

impl FromStr for ProtocolSemanticVersion {
type Err = ParseProtocolSemanticVersionError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = s.split('.').collect();
if parts.len() != 3 {
return Err(ParseProtocolSemanticVersionError::InvalidFormat);
}

let major = parts[0]
.parse::<u16>()
.map_err(ParseProtocolSemanticVersionError::ParseIntError)?;
if major != 0 {
return Err(ParseProtocolSemanticVersionError::NonZeroMajorVersion);
}

let minor = parts[1]
.parse::<u16>()
.map_err(ParseProtocolSemanticVersionError::ParseIntError)?;

let patch = parts[2]
.parse::<u16>()
.map_err(ParseProtocolSemanticVersionError::ParseIntError)?;

Ok(ProtocolSemanticVersion { minor, patch })
}
}

impl<'de> Deserialize<'de> for ProtocolSemanticVersion {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
ProtocolSemanticVersion::from_str(&s).map_err(D::Error::custom)
}
}

impl Serialize for ProtocolSemanticVersion {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

0 comments on commit 67f6080

Please sign in to comment.