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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#![warn(missing_docs)]
#![allow(unreachable_code)]
use std::{fmt, io};
use canonical::CanonError;
use failure::Fail;
mod call_context;
mod contract;
mod gas;
mod ops;
mod resolver;
mod state;
pub use dusk_abi;
pub use call_context::StandardABI;
pub use contract::{Contract, ContractId};
pub use gas::{Gas, GasMeter};
pub use state::NetworkState;
#[derive(Fail)]
pub enum VMError {
InvalidArguments,
ContractPanic(String),
MemoryNotFound,
InvalidABICall,
InvalidUtf8,
InvalidEd25519PublicKey,
InvalidEd25519Signature,
ContractReturn(i32, i32),
OutOfGas,
NotEnoughFunds,
UnknownContract,
WASMError(failure::Error),
Trap(wasmi::Trap),
WasmiError(wasmi::Error),
IOError(io::Error),
InvalidWASMModule,
StoreError(CanonError),
}
impl From<io::Error> for VMError {
fn from(e: io::Error) -> Self {
VMError::IOError(e)
}
}
impl From<wasmi::Error> for VMError {
fn from(e: wasmi::Error) -> Self {
VMError::WasmiError(e)
}
}
impl From<wasmi::Trap> for VMError {
fn from(e: wasmi::Trap) -> Self {
VMError::Trap(e)
}
}
impl VMError {
pub fn from_store_error(err: CanonError) -> Self {
VMError::StoreError(err)
}
}
impl wasmi::HostError for VMError {}
impl fmt::Display for VMError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
VMError::InvalidArguments => write!(f, "Invalid arguments")?,
VMError::ContractPanic(string) => {
write!(f, "Contract panic \"{}\"", string)?
}
VMError::InvalidUtf8 => write!(f, "Invalid UTF-8")?,
VMError::InvalidEd25519PublicKey => {
write!(f, "Invalid Ed25519 Public Key")?
}
VMError::InvalidEd25519Signature => {
write!(f, "Invalid Ed25519 Signature")?
}
VMError::ContractReturn(_, _) => write!(f, "Contract Return")?,
VMError::OutOfGas => write!(f, "Out of Gas error")?,
VMError::NotEnoughFunds => write!(f, "Not enough funds error")?,
VMError::WASMError(e) => write!(f, "WASM Error ({:?})", e)?,
VMError::MemoryNotFound => write!(f, "Memory not found")?,
VMError::InvalidABICall => write!(f, "Invalid ABI Call")?,
VMError::IOError(e) => write!(f, "Input/Output Error ({:?})", e)?,
VMError::Trap(e) => write!(f, "Trap ({:?})", e)?,
VMError::WasmiError(e) => write!(f, "WASMI Error ({:?})", e)?,
VMError::UnknownContract => write!(f, "Unknown Contract")?,
VMError::InvalidWASMModule => write!(f, "Invalid WASM module")?,
VMError::StoreError(e) => write!(f, "Store error {:?}", e)?,
}
Ok(())
}
}
impl fmt::Debug for VMError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
}
}
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, PartialEq, Eq)]
pub struct Schedule {
pub version: u32,
pub put_code_per_byte_cost: Gas,
pub grow_mem_cost: Gas,
pub regular_op_cost: Gas,
pub return_data_per_byte_cost: Gas,
pub event_data_per_byte_cost: Gas,
pub event_per_topic_cost: Gas,
pub event_base_cost: Gas,
pub call_base_cost: Gas,
pub instantiate_base_cost: Gas,
pub sandbox_data_read_cost: Gas,
pub sandbox_data_write_cost: Gas,
pub max_event_topics: u32,
pub max_stack_height: u32,
pub max_memory_pages: u32,
pub max_table_size: u32,
pub enable_println: bool,
pub max_subject_len: u32,
}
impl Default for Schedule {
fn default() -> Schedule {
Schedule {
version: 0,
put_code_per_byte_cost: 1,
grow_mem_cost: 1,
regular_op_cost: 1,
return_data_per_byte_cost: 1,
event_data_per_byte_cost: 1,
event_per_topic_cost: 1,
event_base_cost: 1,
call_base_cost: 135,
instantiate_base_cost: 175,
sandbox_data_read_cost: 1,
sandbox_data_write_cost: 1,
max_event_topics: 4,
max_stack_height: 64 * 1024,
max_memory_pages: 16,
max_table_size: 16 * 1024,
enable_println: false,
max_subject_len: 32,
}
}
}