-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(starknet_class_manager_types): MemoryClassManagerClient for testing
- Loading branch information
1 parent
e5ae6d0
commit 4968431
Showing
3 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
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,95 @@ | ||
use core::panic; | ||
use std::collections::HashMap; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use async_trait::async_trait; | ||
use starknet_api::core::{ClassHash, CompiledClassHash}; | ||
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedClass; | ||
use starknet_api::state::SierraContractClass; | ||
|
||
use crate::{ | ||
Class, | ||
ClassId, | ||
ClassManagerClient, | ||
ClassManagerClientResult, | ||
ClassManagerError, | ||
ExecutableClass, | ||
ExecutableClassHash, | ||
}; | ||
|
||
pub struct MemoryClassManagerClient { | ||
sierras: Arc<Mutex<HashMap<ClassHash, SierraContractClass>>>, | ||
contracts: Arc<Mutex<HashMap<ClassHash, ExecutableClass>>>, | ||
} | ||
|
||
impl MemoryClassManagerClient { | ||
pub fn new() -> Self { | ||
Self { | ||
sierras: Arc::new(Mutex::new(HashMap::new())), | ||
contracts: Arc::new(Mutex::new(HashMap::new())), | ||
} | ||
} | ||
} | ||
|
||
impl Default for MemoryClassManagerClient { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ClassManagerClient for MemoryClassManagerClient { | ||
async fn add_class( | ||
&self, | ||
class_id: ClassId, | ||
class: Class, | ||
) -> ClassManagerClientResult<ExecutableClassHash> { | ||
if self.sierras.lock().unwrap().insert(class_id, class).is_some() { | ||
panic!("Class already exists"); | ||
} | ||
|
||
Ok(CompiledClassHash(class_id.0)) | ||
} | ||
|
||
async fn get_executable(&self, class_id: ClassId) -> ClassManagerClientResult<ExecutableClass> { | ||
let class = self | ||
.contracts | ||
.lock() | ||
.unwrap() | ||
.get(&class_id) | ||
.cloned() | ||
.ok_or_else(|| ClassManagerError::ClassNotFound { class_id })?; | ||
|
||
Ok(class) | ||
} | ||
|
||
async fn get_sierra(&self, class_id: ClassId) -> ClassManagerClientResult<Class> { | ||
let class = self | ||
.sierras | ||
.lock() | ||
.unwrap() | ||
.get(&class_id) | ||
.cloned() | ||
.ok_or_else(|| ClassManagerError::ClassNotFound { class_id })?; | ||
|
||
Ok(class) | ||
} | ||
|
||
async fn add_deprecated_class( | ||
&self, | ||
class_id: ClassId, | ||
class: DeprecatedClass, | ||
) -> ClassManagerClientResult<()> { | ||
if self | ||
.contracts | ||
.lock() | ||
.unwrap() | ||
.insert(class_id, starknet_api::contract_class::ContractClass::V0(class)) | ||
.is_some() | ||
{ | ||
panic!("Class already exists"); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |