Skip to content

Commit c077dd6

Browse files
kkawulacptartur
andauthored
Pin cache version to the forge version (#3039)
<!-- Reference any GitHub issues resolved by this PR --> Closes #3021 ## Introduced changes <!-- A brief description of the changes --> - ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md` --------- Co-authored-by: Artur Michałek <[email protected]>
1 parent 83d1912 commit c077dd6

File tree

9 files changed

+39
-15
lines changed

9 files changed

+39
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Forge
1111

12+
#### Changed
13+
14+
- Fork cache version is pinned to the forge version.
15+
1216
#### Fixed
1317

1418
- `snforge_scarb_plugin` now emits an error when parameters are passed without using the `#[fuzzer]` attribute

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cheatnet/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cheatnet"
3-
version = "1.0.0"
3+
version.workspace = true
44
edition.workspace = true
55

66
[features]

crates/cheatnet/src/forking/cache.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ use std::collections::HashMap;
1313
use std::fs;
1414
use std::fs::OpenOptions;
1515
use std::io::{Read, Write};
16+
use std::string::ToString;
1617
use url::Url;
1718

18-
pub const CACHE_VERSION: usize = 3;
19+
#[must_use]
20+
pub fn cache_version() -> String {
21+
env!("CARGO_PKG_VERSION").replace('.', "_")
22+
}
1923

2024
#[derive(Serialize, Deserialize, Debug)]
2125
struct ForkCacheContent {
22-
cache_version: usize,
26+
cache_version: String,
2327
storage_at: HashMap<ContractAddress, HashMap<StorageKey, Felt>>,
2428
nonce_at: HashMap<ContractAddress, Nonce>,
2529
class_hash_at: HashMap<ContractAddress, ClassHash>,
@@ -30,7 +34,7 @@ struct ForkCacheContent {
3034
impl Default for ForkCacheContent {
3135
fn default() -> Self {
3236
Self {
33-
cache_version: CACHE_VERSION,
37+
cache_version: cache_version(),
3438
storage_at: HashMap::default(),
3539
nonce_at: HashMap::default(),
3640
class_hash_at: HashMap::default(),
@@ -45,9 +49,11 @@ impl ForkCacheContent {
4549
let cache: Self =
4650
serde_json::from_str(serialized).expect("Could not deserialize cache from json");
4751

48-
assert!(
49-
cache.cache_version == CACHE_VERSION,
50-
"Expected the Version {CACHE_VERSION}"
52+
assert_eq!(
53+
cache.cache_version,
54+
cache_version(),
55+
"Expected the Version {}",
56+
cache_version()
5157
);
5258

5359
cache
@@ -247,7 +253,8 @@ fn cache_file_path_from_fork_config(
247253
let sanitized_path = re.replace_all(url.as_str(), "_");
248254

249255
let cache_file_path = cache_dir.join(format!(
250-
"{sanitized_path}_{block_number}_v{CACHE_VERSION}.json"
256+
"{sanitized_path}_{block_number}_v{}.json",
257+
cache_version()
251258
));
252259

253260
fs::create_dir_all(cache_file_path.parent().unwrap())

crates/cheatnet/tests/starknet/forking.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use blockifier::state::cached_state::CachedState;
66
use cairo_vm::vm::errors::hint_errors::HintError;
77
use camino::Utf8Path;
88
use cheatnet::constants::build_testing_state;
9-
use cheatnet::forking::{cache::CACHE_VERSION, state::ForkStateReader};
9+
use cheatnet::forking::cache::cache_version;
10+
use cheatnet::forking::state::ForkStateReader;
1011
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::CheatcodeError;
1112
use cheatnet::state::{BlockInfoReader, CheatnetState, ExtendedStateReader};
1213
use conversions::IntoConv;
@@ -407,7 +408,7 @@ async fn using_specified_block_nb_is_cached() {
407408
let cache = read_cache(
408409
cache_dir
409410
.path()
410-
.join(format!("*v{CACHE_VERSION}.json"))
411+
.join(format!("*v{}.json", cache_version()))
411412
.to_str()
412413
.unwrap(),
413414
);
@@ -487,7 +488,7 @@ async fn test_cache_merging() {
487488
let cache = read_cache(
488489
cache_dir
489490
.path()
490-
.join(format!("*v{CACHE_VERSION}.json"))
491+
.join(format!("*v{}.json", cache_version()))
491492
.to_str()
492493
.unwrap(),
493494
);
@@ -600,7 +601,7 @@ async fn test_cached_block_info_merging() {
600601
let cache = read_cache(
601602
cache_dir
602603
.path()
603-
.join(format!("*v{CACHE_VERSION}.json"))
604+
.join(format!("*v{}.json", cache_version()))
604605
.to_str()
605606
.unwrap(),
606607
);

crates/forge/tests/data/forking/.snfoundry_cache/http___188_34_188_184_7070_rpc_v0_7_54060_v0_38_0.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

crates/forge/tests/data/forking/.snfoundry_cache/http___188_34_188_184_7070_rpc_v0_7_54060_v3.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/forge/tests/data/forking/src/lib.cairo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#[cfg(test)]
22
mod tests {
3-
use starknet::ContractAddress;
43
use starknet::contract_address_const;
54

65
#[starknet::interface]

scripts/release.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ rm snforge_std/Scarb.toml.bak 2> /dev/null
1919
sed -i.bak "/\[package\]/,/version =/ s/version = \".*/version = \"${VERSION}\"/" crates/snforge-scarb-plugin/Scarb.toml
2020
rm crates/snforge-scarb-plugin/Scarb.toml.bak 2> /dev/null
2121

22+
# start: Update cache test data
23+
VERSION_UNDERSCORED=$(echo "$VERSION" | tr '.' '_')
24+
25+
DIRECTORY="crates/forge/tests/data/forking/.snfoundry_cache"
26+
OLD_FILE_PATH=$(find "$DIRECTORY" -type f -regex '.*_v[0-9][0-9_]*\.json')
27+
NEW_FILE_PATH=$(echo "$OLD_FILE_PATH" | sed -E "s/_v[0-9_]+\.json$/_v${VERSION_UNDERSCORED}.json/")
28+
29+
mv "$OLD_FILE_PATH" "$NEW_FILE_PATH"
30+
31+
sed -i.bak -E "s/\"cache_version\":\"[0-9_]+\"/\"cache_version\":\"${VERSION_UNDERSCORED}\"/" "$NEW_FILE_PATH"
32+
rm "$NEW_FILE_PATH.bak" 2> /dev/null
33+
# end
34+
2235
scarb --manifest-path snforge_std/Scarb.toml build
2336

2437
cargo update -p forge

0 commit comments

Comments
 (0)