1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::{Result, StorageKey};
use ceres_std::BTreeMap;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
pub trait Storage {
fn set(&mut self, code_hash: StorageKey, data: BTreeMap<StorageKey, Vec<u8>>) -> Result<()>;
fn get(&self, code_hash: StorageKey) -> Option<BTreeMap<StorageKey, Vec<u8>>>;
fn new_state(&self) -> BTreeMap<StorageKey, Vec<u8>>;
}
#[derive(Default)]
pub struct MemoryStorage(pub BTreeMap<StorageKey, BTreeMap<StorageKey, Vec<u8>>>);
impl MemoryStorage {
pub fn new() -> MemoryStorage {
Self::default()
}
}
impl Storage for MemoryStorage {
fn set(&mut self, code_hash: StorageKey, data: BTreeMap<StorageKey, Vec<u8>>) -> Result<()> {
self.0.insert(code_hash, data);
Ok(())
}
#[allow(clippy::map_clone)]
fn get(&self, code_hash: StorageKey) -> Option<BTreeMap<StorageKey, Vec<u8>>> {
self.0.get(&code_hash).map(|v| v.clone())
}
fn new_state(&self) -> BTreeMap<StorageKey, Vec<u8>> {
BTreeMap::new()
}
}