Skip to content

Commit

Permalink
feat(starknet_class_manager): builder for test storage
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware committed Feb 9, 2025
1 parent d1c4b7c commit 85e6a08
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/starknet_class_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ license.workspace = true
repository.workspace = true
version.workspace = true

[features]
testing = []

[lints]
workspace = true

Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_class_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ impl Clone for FsClassManager {
}

pub use FsClassManager as ClassManager;

#[cfg(any(feature = "testing", test))]
pub mod test_utils;
52 changes: 52 additions & 0 deletions crates/starknet_class_manager/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::path::PathBuf;

use tempfile::TempDir;

use crate::class_storage::{ClassHashStorage, FsClassStorage};
use crate::config::{ClassHashStorageConfig, FsClassStorageConfig};

pub type FileHandles = (TempDir, TempDir);

pub struct FsClassStorageBuilderForTesting {
config: FsClassStorageConfig,
handles: Option<FileHandles>,
}

impl Default for FsClassStorageBuilderForTesting {
fn default() -> Self {
let class_hash_storage_handle = tempfile::tempdir().unwrap();
let persistent_root_handle = tempfile::tempdir().unwrap();
let persistent_root = persistent_root_handle.path().to_path_buf();
let config = FsClassStorageConfig {
persistent_root,
class_hash_storage_config: ClassHashStorageConfig {
path_prefix: class_hash_storage_handle.path().to_path_buf(),
enforce_file_exists: false,
max_size: 1 << 20, // 1MB.
},
};
Self { config, handles: Some((class_hash_storage_handle, persistent_root_handle)) }
}
}

impl FsClassStorageBuilderForTesting {
pub fn with_existing_paths(
mut self,
class_hash_storage_path_prefix: PathBuf,
persistent_path: PathBuf,
) -> Self {
self.config.class_hash_storage_config.path_prefix = class_hash_storage_path_prefix;
self.config.persistent_root = persistent_path;
self.handles = None;
self
}

pub fn build(self) -> (FsClassStorage, FsClassStorageConfig, Option<FileHandles>) {
let Self { config, handles } = self;
let class_hash_storage =
ClassHashStorage::new(config.class_hash_storage_config.clone()).unwrap();
let fs_class_storage =
FsClassStorage { persistent_root: config.persistent_root.clone(), class_hash_storage };
(fs_class_storage, config, handles)
}
}

0 comments on commit 85e6a08

Please sign in to comment.