-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.rs
332 lines (277 loc) · 9.51 KB
/
executor.rs
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use std::collections::HashMap;
use crate::compiler::BinCode;
use tmelcrypt::ed25519_keygen;
use blkstructs::{
Transaction,
melvm::{Value, Executor, Covenant, OpCode}};
pub type ProgramCounter = usize;
type Stack = Vec<Value>;
type Heap = HashMap<u16, Value>;
pub struct ExecutionEnv<'a> {
/// A stack and heap environment.
executor: Executor,
/// Program instructions to execute.
ops: &'a [OpCode],
}
impl<'a> ExecutionEnv<'a> {
/*
pub fn from_bin(tx: &'a Transaction, bin: BinCode) -> Option<ExecutionEnv<'a>> {
let cov_hash = &tmelcrypt::hash_single(&bin.0);
// Disassemble binary
let ops = disassemble(bin)?;
Some( ExecutionEnv::new(tx, ops, cov_hash) )
}
*/
pub fn new(tx: &Transaction, ops: &'a [OpCode], cov_hash: &[u8]) -> ExecutionEnv<'a> {
// Pre-load the tx onto the heap
let mut heap = std::collections::HashMap::new();
heap.insert(0, Value::from(tx.clone()));
heap.insert(1, Value::from_bytes(&tx.hash_nosigs()));
heap.insert(2, Value::from_bytes(&tmelcrypt::hash_single(cov_hash)));
ExecutionEnv {
executor: Executor::new(heap),
ops,
}
}
pub fn view(&self) -> (Stack, Heap) {
(self.executor.stack.clone(), self.executor.heap.clone())
}
}
impl<'a,'b> IntoIterator for &'b mut ExecutionEnv<'a> {
type Item = Option<(Stack, Heap)>;
type IntoIter = ExecutorIter<'a,'b>;
fn into_iter(self) -> ExecutorIter<'a,'b> {
ExecutorIter {
env: self,
pc: 0,
}
}
}
pub struct ExecutorIter<'a,'b> {
env: &'b mut ExecutionEnv<'a>,
/// Tracks the next instruction to be executed.
pc: ProgramCounter,
}
/// Iterate over program instructions in an [ExecutionEnv], returning an optional
/// view into the environment state each time. If the inner optional is none, the
/// program failed execution. If the outer optional is none, execution finished
/// successfully. If the inner optional is not checked, this iterator may be
/// non-terminating.
impl<'a,'b> Iterator for ExecutorIter<'a,'b> {
type Item = Option<(Stack, Heap)>;
fn next(&mut self) -> Option<Self::Item> {
match self.env.ops.get(self.pc) {
Some(op_next) => match self.env.executor.do_op(op_next, self.pc as u32) {
Some(pc_new) => {
self.pc = pc_new as usize;
Some( Some(self.env.view()) )
},
None => Some(None),
},
None => None,
}
}
}
/// Disassemble a binary code using the MelVM disassembler.
pub fn disassemble(bin: BinCode) -> Option<Vec<OpCode>> {
// Wrap in a covenant
let script = Covenant(bin.0.clone());
// Disassemble compiled binary
script.to_ops()
}
pub fn execute(mut env: ExecutionEnv) -> Result<(Stack, Heap), ()> {
// Execute
//let mut env = ExecutionEnv::new(&tx, &ops);
/*
let final_state = env.into_iter()
.take_while(|x| x.is_some())
.map(|x| x.unwrap());
//.last();
*/
let mut final_state = (vec![], HashMap::new());
let e = &mut env;
for x in e.into_iter() {
match x {
None => return Err(()),
Some(state) => final_state = state,
}
};
Ok(final_state)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::parse;
use crate::types::MelExpr;
use crate::compiler::{Compile, BinCode};
use primitive_types::U256;
use tmelcrypt::{Ed25519PK, Ed25519SK};
use im::vector;
fn compile(ops: MelExpr) -> BinCode {
// Compile to binary
let empty = BinCode(Vec::new());
ops.compile_onto(empty)
}
fn key_and_empty_tx() -> (Ed25519PK, Ed25519SK, Transaction) {
let (pk, sk) = ed25519_keygen();
let tx = Transaction::empty_test().sign_ed25519(sk);
(pk, sk, tx)
}
fn exec(tx: &Transaction, ops: MelExpr) -> (Stack, Heap) {
let bin = compile(ops);
let cov_hash = tmelcrypt::hash_single(&bin.0);
let dis = disassemble(bin).expect("Failed to disassemble");
execute(ExecutionEnv::new(&tx, &dis, &cov_hash))
.expect(&format!("Failed to execute: {:?}", dis))
}
#[test]
fn add_numbers() {
let ops = parse("(+ 1 2)").unwrap();
let (pk, sk, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Int(U256::from(3))]);
}
#[test]
fn test_eql() {
let ops = parse("(= 1 1)").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Int(U256::from(1))]);
}
#[test]
fn test_not_eql() {
let ops = parse("(= (+ 2 2) 1)").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Int(U256::from(0))]);
}
#[test]
fn set_value() {
let ops = parse("(let (x 1) (set! x 2) x)").unwrap();
let (pk, sk, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Int(U256::from(2))]);
}
#[test]
fn cons_nil_1() {
let ops = parse("(cons 1 nil)").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Vector(vector![Value::Int(U256::one())])]);
}
#[test]
fn concat_vectors() {
let ops = parse("(concat (cons 2 nil) (cons 1 nil))").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Vector(
vector![Value::Int(U256::one()), Value::Int(U256::from(2))])]);
}
#[test]
fn ref_vector() {
let ops = parse("(get 1 (concat (cons 2 nil) (cons 1 nil)))").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Int(U256::from(2))]);
}
#[test]
fn nested_lets() {
let ops = parse("(let (x 3) (let (y 2) (* x y)))").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Int(U256::from(6))]);
}
#[test]
fn if_true_branch() {
let ops = parse("(if (and 1 1) (* 2 2) 1)").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Int(U256::from(4))]);
}
#[test]
fn if_false_branch() {
let ops = parse("(if 0 (* 2 2) 1)").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Int(U256::from(1))]);
}
#[test]
fn vset_vector() {
let ops = parse("(vfrom 2 0 (cons 1 nil))").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Vector(vector![Value::Int(U256::from(2))])]);
}
#[test]
fn vset_bytes() {
let ops = parse("(vfrom 2 0 0x00)").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Bytes(vector![2])]);
}
#[test]
fn inlined_comments() {
let ops = parse("
(if ; this is just
0 ;for tests
(* 2 2); dont mind me
1)").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Int(U256::from(1))]);
}
#[test]
fn empty_string_is_empty_bytes() {
let ops = parse("(let (x \"\") x)").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Bytes(vector![])]);
}
#[test]
fn init_vec_native() {
let ops = parse("(let (v [1 2 3]) v)").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Vector(vector![
Value::Int(U256::from(1)),
Value::Int(U256::from(2)),
Value::Int(U256::from(3))])]);
}
#[test]
fn loop_add_expr_4_times() {
let ops = parse("(loop 4 (+ 1 2))").unwrap();
let (_, _, tx) = key_and_empty_tx();
let state = exec(&tx, ops);
assert_eq!(
state.0,
vec![Value::Int(U256::from(3)),
Value::Int(U256::from(3)),
Value::Int(U256::from(3)),
Value::Int(U256::from(3))]);
}
#[test]
fn hash_bytes() {
let ops = parse("(hash 1 0xF0)").unwrap();
let (_, _, tx) = key_and_empty_tx();
let mut state = exec(&tx, ops);
if let blkstructs::melvm::Value::Bytes(im_bytes) = state.0.pop().unwrap() {
assert_eq!(im_bytes.into_iter().collect::<Vec<u8>>(), vec![
233, 131, 224, 169, 229, 83, 12, 43, 119, 20, 230,
120, 233, 61, 188, 129, 150, 148, 124, 190, 111, 195,
63, 163, 212, 106, 36, 240, 111, 251, 98, 193]);
} else {
panic!();
}
}
#[test]
fn sigeok_bytes() {
let (pk, _, tx) = key_and_empty_tx();
let ops = parse(&format!("
(sigeok 32
(get 0 (get 6 SpenderTx))
0x{}
SpenderTxHash)", hex::encode(&pk.0))).unwrap();
let state = exec(&tx, ops);
assert_eq!(state.0, vec![Value::Int(U256::one())]);
}
}