Skip to content

Commit

Permalink
Change algorithm of versioned_constants selection in Rust (#1772)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan authored Mar 29, 2024
1 parent 0ef1e6a commit 50fb6eb
Show file tree
Hide file tree
Showing 3 changed files with 511 additions and 2 deletions.
2 changes: 2 additions & 0 deletions vm/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ indexmap = "2.1.0"
cached = "0.46.1"
once_cell = "1.18.0"
lazy_static = "1.4.0"
semver = "1.0.22"
anyhow = "1.0.81"

[lib]
crate-type = ["staticlib"]
49 changes: 47 additions & 2 deletions vm/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use starknet_api::{
core::{ChainId, ClassHash, ContractAddress, EntryPointSelector},
hash::StarkHash,
};
use std::str::FromStr;

extern "C" {
fn JunoReportError(reader_handle: usize, txnIndex: c_longlong, err: *const c_char);
Expand Down Expand Up @@ -421,16 +422,60 @@ fn build_block_context(
}




lazy_static! {
static ref CONSTANTS: HashMap<String, VersionedConstants> = {
let mut m = HashMap::new();
m.insert("0.13.0".to_string(), serde_json::from_slice(include_bytes!("../versioned_constants_13_0.json")).unwrap());

m.insert("0.13.1".to_string(), serde_json::from_slice(include_bytes!("../versioned_constants_13_1.json")).unwrap());
m
};
}

fn get_versioned_constants(version: *const c_char) -> VersionedConstants {
let version_str = unsafe { CStr::from_ptr(version) }.to_str().unwrap();
CONSTANTS.get(&version_str.to_string()).unwrap_or(VersionedConstants::latest_constants()).to_owned()
let version = StarknetVersion::from_str(&version_str).unwrap_or(StarknetVersion::from_str(&"0.0.0").unwrap());

if version < StarknetVersion::from_str(&"0.13.1").unwrap() {
CONSTANTS.get(&"0.13.0".to_string()).unwrap().to_owned()
} else if version < StarknetVersion::from_str(&"0.13.1.1").unwrap() {
CONSTANTS.get(&"0.13.1".to_string()).unwrap().to_owned()
} else {
VersionedConstants::latest_constants().to_owned()
}
}


#[derive(Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct StarknetVersion(u8, u8, u8, u8);

impl StarknetVersion {
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Self {
StarknetVersion(a, b, c, d)
}
}

impl FromStr for StarknetVersion {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
return Ok(StarknetVersion::new(0, 0, 0, 0));
}

let parts: Vec<_> = s.split('.').collect();
anyhow::ensure!(
parts.len() == 3 || parts.len() == 4,
"Invalid version string, expected 3 or 4 parts but got {}",
parts.len()
);

let a = parts[0].parse()?;
let b = parts[1].parse()?;
let c = parts[2].parse()?;
let d = parts.get(3).map(|x| x.parse()).transpose()?.unwrap_or(0);

Ok(StarknetVersion(a, b, c, d))
}
}
Loading

0 comments on commit 50fb6eb

Please sign in to comment.