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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use crate::{External, StorageGetMode, ValuePtr};
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::types::TrieNodesCount;
use near_primitives_core::types::{AccountId, Balance};
use std::collections::HashMap;
#[derive(Default, Clone)]
pub struct MockedExternal {
pub fake_trie: HashMap<Vec<u8>, Vec<u8>>,
pub validators: HashMap<AccountId, Balance>,
data_count: u64,
}
pub struct MockedValuePtr {
value: Vec<u8>,
}
impl MockedValuePtr {
pub fn new<T>(value: T) -> Self
where
T: AsRef<[u8]>,
{
MockedValuePtr { value: value.as_ref().to_vec() }
}
}
impl ValuePtr for MockedValuePtr {
fn len(&self) -> u32 {
self.value.len() as u32
}
fn deref(&self) -> crate::dependencies::Result<Vec<u8>> {
Ok(self.value.clone())
}
}
impl MockedExternal {
pub fn new() -> Self {
Self::default()
}
}
use crate::dependencies::Result;
impl External for MockedExternal {
fn storage_set(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
self.fake_trie.insert(key.to_vec(), value.to_vec());
Ok(())
}
fn storage_get(&self, key: &[u8], _mode: StorageGetMode) -> Result<Option<Box<dyn ValuePtr>>> {
Ok(self
.fake_trie
.get(key)
.map(|value| Box::new(MockedValuePtr { value: value.clone() }) as Box<_>))
}
fn storage_remove(&mut self, key: &[u8]) -> Result<()> {
self.fake_trie.remove(key);
Ok(())
}
fn storage_remove_subtree(&mut self, prefix: &[u8]) -> Result<()> {
self.fake_trie.retain(|key, _| !key.starts_with(prefix));
Ok(())
}
fn storage_has_key(&mut self, key: &[u8]) -> Result<bool> {
Ok(self.fake_trie.contains_key(key))
}
fn generate_data_id(&mut self) -> CryptoHash {
let data_id = hash(&self.data_count.to_le_bytes());
self.data_count += 1;
data_id
}
fn get_trie_nodes_count(&self) -> TrieNodesCount {
TrieNodesCount { db_reads: 0, mem_reads: 0 }
}
fn validator_stake(&self, account_id: &AccountId) -> Result<Option<Balance>> {
Ok(self.validators.get(account_id).cloned())
}
fn validator_total_stake(&self) -> Result<Balance> {
Ok(self.validators.values().sum())
}
}