-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(zk_toolbox): Fix protocol version (#2118)
## 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
1 parent
f99739b
commit 67f6080
Showing
6 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
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
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
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,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()) | ||
} | ||
} |