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
use std::fmt;
use oasis_types::{Address, Balance};
pub trait Storage = serde::Serialize + serde::de::DeserializeOwned;
pub trait Service {
fn coalesce() -> Self;
fn sunder(c: Self);
}
pub trait Event {
fn emit(&self);
}
#[derive(Default, Copy, Clone, Debug)]
pub struct Context {
#[doc(hidden)]
pub sender: Option<Address>,
#[doc(hidden)]
pub value: Option<Balance>,
#[doc(hidden)]
pub gas: Option<u64>,
#[doc(hidden)]
pub call_type: CallType,
}
#[derive(Copy, Clone, Debug)]
pub enum CallType {
Default,
Delegated,
Constant,
}
impl Default for CallType {
fn default() -> Self {
CallType::Default
}
}
impl Context {
#[cfg(any(test, target_os = "wasi"))]
pub fn delegated() -> Self {
Self {
call_type: CallType::Delegated,
..Default::default()
}
}
pub fn with_gas(mut self, gas: u64) -> Self {
self.gas = Some(gas);
self
}
pub fn sender(&self) -> Address {
self.sender.unwrap_or_else(crate::backend::sender)
}
pub fn address(&self) -> Address {
crate::backend::address()
}
pub fn aad(&self) -> Vec<u8> {
crate::backend::aad()
}
pub fn value(&self) -> Balance {
self.value.unwrap_or_else(crate::backend::value)
}
}
impl Context {
#[cfg(any(test, not(target_os = "wasi")))]
pub fn with_sender(mut self, sender: Address) -> Self {
self.sender = Some(sender);
self
}
pub fn with_value<B: Into<Balance>>(mut self, value: B) -> Self {
self.value = Some(value.into());
self
}
}
#[derive(Clone, Serialize, Deserialize, failure::Fail)]
pub enum RpcError<E: Send + Sync + 'static> {
InvalidCallee,
InsufficientFunds,
InsufficientGas,
InvalidInput,
InvalidOutput(Vec<u8>),
Exec(E),
}
impl<E> From<crate::backend::Error> for RpcError<E>
where
E: serde::de::DeserializeOwned + Send + Sync,
{
fn from(err: crate::backend::Error) -> Self {
use crate::backend::Error as BackendError;
match err {
BackendError::Unknown => panic!("Unknown error occured."),
BackendError::InsufficientFunds => RpcError::InsufficientFunds,
BackendError::InvalidInput => RpcError::InvalidInput,
BackendError::InvalidCallee => RpcError::InvalidCallee,
BackendError::Execution { payload, .. } => {
match serde_cbor::from_slice::<E>(&payload) {
Ok(e) => RpcError::Exec(e),
Err(_) => RpcError::InvalidOutput(payload),
}
}
}
}
}
impl<E: Send + Sync> fmt::Debug for RpcError<E> {
default fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RpcError::InvalidCallee => write!(f, "invalid callee"),
RpcError::InsufficientFunds => write!(f, "caller has insufficient funds"),
RpcError::InsufficientGas => write!(f, "not enough gas to complete transaction"),
RpcError::InvalidInput => write!(f, "invalid input provided to RPC"),
RpcError::InvalidOutput(_) => write!(f, "invalid output returned by RPC"),
RpcError::Exec(_) => write!(f, "execution error"),
}
}
}
impl<E: Send + Sync> fmt::Display for RpcError<E> {
default fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl<E: Send + Sync + fmt::Debug> fmt::Debug for RpcError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RpcError::Exec(e) => write!(f, "execution error {:?}", e),
_ => fmt::Debug::fmt(self, f),
}
}
}
impl<E: Send + Sync + fmt::Display> fmt::Display for RpcError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RpcError::Exec(e) => write!(f, "execution error: {}", e),
_ => fmt::Display::fmt(self, f),
}
}
}