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
#[cfg(target_arch = "wasm32")]
use ewasm_api::finish_data;
#[cfg(target_arch = "wasm32")]
use ewasm_api::log0;
use tiny_keccak::{Hasher, Sha3};
pub use serde::de::DeserializeOwned;
pub use serde::Serialize;
pub use serde_value::{to_value, Value};
#[cfg(target_arch = "wasm32")]
use crate::types::{Address, Raw};
#[cfg(target_arch = "wasm32")]
#[macro_export]
macro_rules! ewasm_dbg {
() => { };
($val:expr $(,)?) => {
match $val {
tmp => {
$crate::utils::log(format!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($val), &tmp));
tmp
}
}
};
($($val:expr),+ $(,)?) => {
($($crate::ewasm_dbg!($val)),+,)
};
}
#[cfg(not(target_arch = "wasm32"))]
#[macro_export]
macro_rules! ewasm_dbg {
() => {};
}
#[cfg(target_arch = "wasm32")]
pub fn log(s: String) {
log0(s.as_bytes());
}
#[cfg(target_arch = "wasm32")]
pub fn ewasm_return(bytes: Vec<u8>) {
finish_data(&bytes);
}
#[cfg(target_arch = "wasm32")]
pub fn ewasm_return_str(s: &str) {
let mut output = Raw::from(32u32).as_bytes().to_vec();
output.append(&mut Raw::from(s.len()).as_bytes().to_vec());
output.append(&mut Raw::from(s).as_bytes().to_vec());
finish_data(&output);
}
#[cfg(target_arch = "wasm32")]
pub fn ewasm_return_vec(v: &Vec<[u8; 32]>) {
let mut output = Raw::from(32u32).as_bytes().to_vec();
output.append(&mut Raw::from(v.len()).as_bytes().to_vec());
for e in v.iter() {
output.extend_from_slice(e);
}
finish_data(&output);
}
#[cfg(target_arch = "wasm32")]
pub fn ewasm_return_bool(is_true: bool) {
let output = if is_true {
vec![
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8,
]
} else {
vec![
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
]
};
finish_data(&output);
}
#[cfg(target_arch = "wasm32")]
pub fn caller() -> Address {
ewasm_api::caller().into()
}
pub fn sha3_256(input: &[u8]) -> [u8; 32] {
let mut output = [0; 32];
let mut hasher = Sha3::v256();
hasher.update(input);
hasher.finalize(&mut output);
output
}
#[inline]
pub fn storage_index_to_addr(idx: usize, addr: &mut [u8; 32]) {
for (j, byte) in addr.iter_mut().enumerate().take((idx / 32) + 1) {
assert!(j < 32, "Too big to store on chain");
*byte = (idx >> (5 * j) & 31) as u8;
}
}
pub fn get_field_by_name<T, R>(data: T, field: &str) -> R
where
T: Serialize,
R: DeserializeOwned,
{
let mut map = match to_value(data) {
Ok(Value::Map(map)) => map,
_ => panic!("expected a struct"),
};
let key = Value::String(field.to_owned());
let value = match map.remove(&key) {
Some(value) => value,
None => panic!("no such field"),
};
match R::deserialize(value) {
Ok(r) => r,
Err(_) => panic!("type incorrect"),
}
}
pub fn pretty_print_storage(
desc: &str,
may_storage: Option<&std::collections::HashMap<[u8; 32], [u8; 32]>>,
) -> String {
match may_storage {
Some(storage) => {
let mut output = if desc.trim().is_empty() {
"Storage: \n".to_string()
} else {
format!("Storage at {}:\n", desc)
};
let mut chrunks = Vec::<([u8; 32], [u8; 32])>::new();
for (k, v) in storage.iter() {
chrunks.push((*k, *v));
}
chrunks.sort_by(|a, b| a.0.cmp(&b.0));
for (k, v) in chrunks.drain(..) {
let mut try_str = String::new();
for b in v.iter() {
if 31 < *b && *b < 127 {
try_str.push(*b as char);
} else {
try_str.push(' ');
}
}
output.push_str(&format!("{}|{}|{:?}\n", hex::encode(k), try_str, v));
}
output
}
None => "Storage not use at this moment".into(),
}
}
#[cfg(feature = "default")]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_storage_index_to_addr() {
let mut addr: [u8; 32] = [0; 32];
storage_index_to_addr(1, &mut addr);
assert_eq!(
addr,
[
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
]
);
storage_index_to_addr(32, &mut addr);
assert_eq!(
addr,
[
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
]
);
storage_index_to_addr(33, &mut addr);
assert_eq!(
addr,
[
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
]
);
storage_index_to_addr(65, &mut addr);
assert_eq!(
addr,
[
1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
]
);
}
}