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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::{counters::CRITICAL_ERRORS, create_access_path, logging::AdapterLogSchema};
#[allow(unused_imports)]
use anyhow::format_err;
use diem_logger::prelude::*;
use diem_state_view::{StateView, StateViewId};
use diem_types::{
access_path::AccessPath,
on_chain_config::ConfigStorage,
vm_status::StatusCode,
write_set::{WriteOp, WriteSet},
};
use fail::fail_point;
use move_binary_format::errors::*;
use move_core_types::{
account_address::AccountAddress,
language_storage::{ModuleId, StructTag},
resolver::{ModuleResolver, ResourceResolver},
};
use std::collections::btree_map::BTreeMap;
pub struct StateViewCache<'a> {
data_view: &'a dyn StateView,
data_map: BTreeMap<AccessPath, Option<Vec<u8>>>,
}
impl<'a> StateViewCache<'a> {
pub fn new(data_view: &'a dyn StateView) -> Self {
StateViewCache {
data_view,
data_map: BTreeMap::new(),
}
}
pub(crate) fn push_write_set(&mut self, write_set: &WriteSet) {
for (ref ap, ref write_op) in write_set.iter() {
match write_op {
WriteOp::Value(blob) => {
self.data_map.insert(ap.clone(), Some(blob.clone()));
}
WriteOp::Deletion => {
self.data_map.remove(ap);
self.data_map.insert(ap.clone(), None);
}
}
}
}
}
impl<'block> StateView for StateViewCache<'block> {
fn get(&self, access_path: &AccessPath) -> anyhow::Result<Option<Vec<u8>>> {
fail_point!("move_adapter::data_cache::get", |_| Err(format_err!(
"Injected failure in data_cache::get"
)));
match self.data_map.get(access_path) {
Some(opt_data) => Ok(opt_data.clone()),
None => match self.data_view.get(access_path) {
Ok(remote_data) => Ok(remote_data),
Err(e) => {
let log_context = AdapterLogSchema::new(self.data_view.id(), 0);
CRITICAL_ERRORS.inc();
error!(
log_context,
"[VM, StateView] Error getting data from storage for {:?}", access_path
);
Err(e)
}
},
}
}
fn is_genesis(&self) -> bool {
self.data_view.is_genesis()
}
fn id(&self) -> StateViewId {
self.data_view.id()
}
}
impl<'block> ModuleResolver for StateViewCache<'block> {
type Error = VMError;
fn get_module(&self, module_id: &ModuleId) -> Result<Option<Vec<u8>>, Self::Error> {
RemoteStorage::new(self).get_module(module_id)
}
}
impl<'block> ResourceResolver for StateViewCache<'block> {
type Error = VMError;
fn get_resource(
&self,
address: &AccountAddress,
tag: &StructTag,
) -> Result<Option<Vec<u8>>, Self::Error> {
RemoteStorage::new(self).get_resource(address, tag)
}
}
impl<'block> ConfigStorage for StateViewCache<'block> {
fn fetch_config(&self, access_path: AccessPath) -> Option<Vec<u8>> {
self.get(&access_path).ok()?
}
}
pub struct RemoteStorage<'a, S>(&'a S);
impl<'a, S: StateView> RemoteStorage<'a, S> {
pub fn new(state_store: &'a S) -> Self {
Self(state_store)
}
pub fn get(&self, access_path: &AccessPath) -> PartialVMResult<Option<Vec<u8>>> {
self.0
.get(access_path)
.map_err(|_| PartialVMError::new(StatusCode::STORAGE_ERROR))
}
}
impl<'a, S: StateView> ModuleResolver for RemoteStorage<'a, S> {
type Error = VMError;
fn get_module(&self, module_id: &ModuleId) -> Result<Option<Vec<u8>>, Self::Error> {
let ap = AccessPath::from(module_id);
self.get(&ap).map_err(|e| e.finish(Location::Undefined))
}
}
impl<'a, S: StateView> ResourceResolver for RemoteStorage<'a, S> {
type Error = VMError;
fn get_resource(
&self,
address: &AccountAddress,
struct_tag: &StructTag,
) -> Result<Option<Vec<u8>>, Self::Error> {
let ap = create_access_path(*address, struct_tag.clone());
self.get(&ap).map_err(|e| e.finish(Location::Undefined))
}
}
impl<'a, S: StateView> ConfigStorage for RemoteStorage<'a, S> {
fn fetch_config(&self, access_path: AccessPath) -> Option<Vec<u8>> {
self.get(&access_path).ok()?
}
}