multiversx_sc_scenario/executor/debug/
contract_map.rs1use super::*;
2
3use std::{
4 collections::HashMap,
5 fmt,
6 sync::{Arc, Mutex, MutexGuard},
7};
8
9pub struct ContractMap {
10 contract_objs: HashMap<Vec<u8>, ContractContainerRef>,
11}
12
13impl fmt::Debug for ContractMap {
14 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15 f.debug_struct("ContractMap").finish()
16 }
17}
18
19impl ContractMap {
20 pub fn new() -> Self {
21 ContractMap {
22 contract_objs: HashMap::new(),
23 }
24 }
25
26 pub fn get_contract(&self, contract_identifier: &[u8]) -> Option<ContractContainerRef> {
27 self.contract_objs.get(contract_identifier).cloned()
28 }
29
30 pub fn register_contract(
31 &mut self,
32 contract_bytes: Vec<u8>,
33 contract_container: ContractContainer,
34 ) {
35 let previous_entry = self.contract_objs.insert(
36 contract_bytes,
37 ContractContainerRef::new(contract_container),
38 );
39 assert!(previous_entry.is_none(), "contract inserted twice");
40 }
41
42 pub fn contains_contract(&self, contract_bytes: &[u8]) -> bool {
43 self.contract_objs.contains_key(contract_bytes)
44 }
45}
46
47impl Default for ContractMap {
48 fn default() -> Self {
49 Self::new()
50 }
51}
52
53#[derive(Default, Clone, Debug)]
54pub struct ContractMapRef(Arc<Mutex<ContractMap>>);
55
56impl ContractMapRef {
57 pub fn new() -> Self {
58 ContractMapRef(Arc::new(Mutex::new(ContractMap::new())))
59 }
60
61 pub fn lock(&self) -> MutexGuard<'_, ContractMap> {
62 self.0.lock().unwrap()
63 }
64}