Skip to content

Commit 50fb6eb

Browse files
authored
Change algorithm of versioned_constants selection in Rust (#1772)
1 parent 0ef1e6a commit 50fb6eb

File tree

3 files changed

+511
-2
lines changed

3 files changed

+511
-2
lines changed

vm/rust/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ indexmap = "2.1.0"
1515
cached = "0.46.1"
1616
once_cell = "1.18.0"
1717
lazy_static = "1.4.0"
18+
semver = "1.0.22"
19+
anyhow = "1.0.81"
1820

1921
[lib]
2022
crate-type = ["staticlib"]

vm/rust/src/lib.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use starknet_api::{
3434
core::{ChainId, ClassHash, ContractAddress, EntryPointSelector},
3535
hash::StarkHash,
3636
};
37+
use std::str::FromStr;
3738

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

423424

425+
426+
424427
lazy_static! {
425428
static ref CONSTANTS: HashMap<String, VersionedConstants> = {
426429
let mut m = HashMap::new();
427430
m.insert("0.13.0".to_string(), serde_json::from_slice(include_bytes!("../versioned_constants_13_0.json")).unwrap());
428-
431+
m.insert("0.13.1".to_string(), serde_json::from_slice(include_bytes!("../versioned_constants_13_1.json")).unwrap());
429432
m
430433
};
431434
}
432435

433436
fn get_versioned_constants(version: *const c_char) -> VersionedConstants {
434437
let version_str = unsafe { CStr::from_ptr(version) }.to_str().unwrap();
435-
CONSTANTS.get(&version_str.to_string()).unwrap_or(VersionedConstants::latest_constants()).to_owned()
438+
let version = StarknetVersion::from_str(&version_str).unwrap_or(StarknetVersion::from_str(&"0.0.0").unwrap());
439+
440+
if version < StarknetVersion::from_str(&"0.13.1").unwrap() {
441+
CONSTANTS.get(&"0.13.0".to_string()).unwrap().to_owned()
442+
} else if version < StarknetVersion::from_str(&"0.13.1.1").unwrap() {
443+
CONSTANTS.get(&"0.13.1".to_string()).unwrap().to_owned()
444+
} else {
445+
VersionedConstants::latest_constants().to_owned()
446+
}
447+
}
448+
449+
450+
#[derive(Default, PartialEq, Eq, PartialOrd, Ord)]
451+
pub struct StarknetVersion(u8, u8, u8, u8);
452+
453+
impl StarknetVersion {
454+
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Self {
455+
StarknetVersion(a, b, c, d)
456+
}
457+
}
458+
459+
impl FromStr for StarknetVersion {
460+
type Err = anyhow::Error;
461+
462+
fn from_str(s: &str) -> Result<Self, Self::Err> {
463+
if s.is_empty() {
464+
return Ok(StarknetVersion::new(0, 0, 0, 0));
465+
}
466+
467+
let parts: Vec<_> = s.split('.').collect();
468+
anyhow::ensure!(
469+
parts.len() == 3 || parts.len() == 4,
470+
"Invalid version string, expected 3 or 4 parts but got {}",
471+
parts.len()
472+
);
473+
474+
let a = parts[0].parse()?;
475+
let b = parts[1].parse()?;
476+
let c = parts[2].parse()?;
477+
let d = parts.get(3).map(|x| x.parse()).transpose()?.unwrap_or(0);
478+
479+
Ok(StarknetVersion(a, b, c, d))
480+
}
436481
}

0 commit comments

Comments
 (0)