-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEVM.js
697 lines (627 loc) · 15 KB
/
EVM.js
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
const BN = require("bn.js");
const uint256 = new BN(2).pow(new BN(256));
/**
DATA STRUCTURES
**/
const globalState = () => {
return {
callState: callState(),
subState: subState(),
message: message(),
txData: txData(),
blockInfo: blockInfo(),
accounts: accounts(),
env: env()
};
};
const account = () => {
return {
address: "",
balance: 0,
code: "",
storage: {},
nonce: 0
};
};
const env = () => {
return {
address: 0,
contract: account(),
intiCode: "",
runtimeCode: "",
returnValue: new BN(0)
};
};
const message = () => {
return {
msgHash: "",
txNonce: 0,
txGasPrice: 0,
txGasLimit: 0,
to: 0,
value: 0,
sigV: 0,
sigR: 0,
sigS: 0,
data: ""
};
};
const callState = () => {
return {
code: "",
bytes: "",
id: 0,
caller: "",
callData: "",
callValue: "",
stack: [],
memory: [],
pc: 0,
gas: 0,
memoryUsed: 0,
previousGas: 0,
static: false,
depth: 0,
halt: false,
address: ""
};
};
const subState = () => {
return {
selfDestruct: false,
log: "",
refund: 0
};
};
const txData = () => {
return {
gasprice: 0,
origin: ""
};
};
const blockInfo = () => {
return {
previousHash: 0,
ommersHash: 0,
coinbase: 0,
stateRoot: 0,
transactionsRoot: 0,
receiptsRoot: 0,
logsBloom: [],
difficulty: 0,
number: 0,
gasLimit: 0,
gasUsed: 0,
timestamp: 0,
extraData: [],
mixHash: 0,
blockNonce: 0,
ommerBlockHeaders: 0,
blockhash: ""
};
};
/**
MAIN EXECUTION
**/
// Takes the globalState as a main parameter executes until it reaches the end of the code or if finds a halting error.
function execute(globalState) {
const callState = { ...globalState.callState };
let opcode = getOp(callState);
if (callState.halt || opcode == "" || opcode == 0 || opcode == NaN) {
return { ...globalState, callState };
}
return execute(codes(opcode)(globalState));
}
/**
OPCODE HANDLING
**/
// Takes an `op` parameter and returns a curried function, which takes globalState as parameter and executes the given opcode.
const codes = op => {
//PUSH opcodes
if (op >= 0x60 && op <= 0x7f) {
return PUSH(op);
}
//SWAP opcodes
if (op >= 0x90 && op <= 0x9f) {
return SWAP(op);
}
//DUP opcodes
if (op >= 0x80 && op <= 0x8f) {
return DUP(op);
}
if (op)
switch (op) {
case 0x01:
return stackOp2(ADD);
case 0x02:
return stackOp2(MUL);
case 0x03:
return stackOp2(SUB);
case 0x04:
return stackOp2(DIV);
case 0x05:
return stackOp2(SDIV);
case 0x06:
return stackOp2(MOD);
case 0x07:
return stackOp2(SMOD);
case 0x08:
return stackOp3(ADDMOD);
case 0x09:
return stackOp3(MULMOD);
case 0x0a:
return stackOp2(EXP);
case 0x0b:
return stackOp2(SIGNEXTEND);
case 0x10:
return stackOp2(LT);
case 0x11:
return stackOp2(GT);
case 0x12:
return stackOp2(SLT);
case 0x13:
return stackOp2(SGT);
case 0x14:
return stackOp2(EQ);
case 0x15:
return stackOp1(ISZERO);
case 0x16:
return stackOp2(AND);
case 0x17:
return stackOp2(OR);
case 0x18:
return stackOp2(XOR);
case 0x19:
return stackOp1(NOT);
case 0x1a:
return stackOp2(BYTE);
case 0x30:
return stateToStack(["env", "address"]);
case 0x32:
return stateToStack(["txData", "origin"]);
case 0x33:
return stateToStack(["callState", "caller"]);
case 0x34:
return stateToStack(["callState", "callValue"]);
case 0x35:
return CALLDATALOAD();
case 0x36:
return CALLDATASIZE();
case 0x37:
return CALLDATACOPY();
case 0x38:
return CODESIZE();
case 0x41:
return stateToStack(["blockInfo", "coinbase"]);
case 0x42:
return stateToStack(["blockInfo", "timestamp"]);
case 0x43:
return stateToStack(["blockInfo", "number"]);
case 0x44:
return stateToStack(["blockInfo", "difficulty"]);
case 0x45:
return stateToStack(["blockInfo", "gasLimit"]);
case 0x51:
return MLOAD();
case 0x52:
case 0x53:
return MSTORE();
case 0x54:
return SLOAD();
case 0x55:
return SSTORE();
case 0x56:
return JUMP();
case 0x57:
return JUMPI();
case 0x5b:
return JUMPDEST();
case 0xf1:
return CALL();
case 0xf3:
return RETURNOP();
default:
return errorState();
}
};
//Generic PUSH function. It takes globalState and returns a new globalState with the program counter adjusted.
const PUSH = op => globalState => {
let word = globalState.callState.code.substr(callState.pc + 2, op - 0x5e);
let pc = globalState.callState.pc + (op - 0x5e) * 2;
return {
...globalState,
callState: {
...callState,
pc,
stack: [...callState.stack, new BN(word)]
}
};
};
//Generic DUP. Takes the opcode number and duplicates the correct stack position
const DUP = (op = globalState => {
const pos = op - 0x7f;
let stack = [...globalState.callState.stack];
stack.concat(stack[stack.length - pos]);
return {
...globalState,
callState: {
...globalState.callState,
stack
}
};
});
//Generic SWAP. Takes the opcode number and swap the correct stack position
const SWAP = op => globalState => {
const pos = op - 0x8f;
let stack = [...globalState.callState.stack];
const head = stack[0];
const temp = stack[stack.length - pos];
stack[stack.length - pos] = head;
stack[0] = temp;
return {
...globalState,
callState: {
...globalState.callState,
stack
}
};
};
const JUMP = () => globalState => {
const [dest] = globalState.callState.stack.slice(0, 1);
const destOp = getOP(globalState.callState, dest.toNumber());
if (destOp != 0x5b) {
return errorState();
}
return {
...globalState,
callState: {
...callState,
stack: [...callState.stack.slice(2)],
pc: dest
}
};
};
const JUMPI = () => globalState => {
const [dest, cond] = globalState.callState.stack.slice(0, 2);
const destOp = getOP(globalState.callState, dest.toNumber());
if (destOp != 0x5b) {
return errorState();
}
return {
...globalState,
callState: {
...callState,
stack: [...callState.stack.slice(2)],
pc: cond.isZero() ? callState.pc + 2 : dest + 1
}
};
};
const JUMPDEST = () => globalState => {
return {
...globalState,
callState: {
...callState,
pc: globalState.callState.pc + 2
}
};
};
const CALLDATALOAD = () => globalState => {
const a = globalState.callState.stack.slice(0, 1);
const word = globalState.callState.callData.slice(a, a + 32);
return {
...globalState,
callState: {
...globalState.callState,
stack: [...globalState.callState.stack.slice(1), new BN(word)],
pc: globalState.callState.pc + 2
}
};
};
const CALLDATASIZE = () => globalState => {
return {
...globalState,
callState: {
...globalState.callState,
stack: [
...globalState.callState.stack,
new BN(globalState.callState.callData.length)
],
pc: globalState.callState.pc + 2
}
};
};
const CALLDATACOPY = () => globalState => {
const [memOffset, dataOffset, length] = globalState.callState.stack.slice(
0,
3
);
const word = globalState.callState.callData.slice(
dataOffset,
dataOffset + length
);
const memory = [
...mem.slice(0, memOffset),
...word,
...mem.slice(memOffset + length)
];
return {
...globalState,
callState: {
...globalState.callState,
stack: [...globalState.callState.stack.slice(3)],
memory,
pc: globalState.callState.pc + 2
}
};
};
const CODESIZE = () => globalState => {
return {
...globalState,
callState: {
...globalState.callState,
stack: [
...globalState.callState.stack,
new BN(globalState.env.contract.code.length)
],
pc: globalState.callState.pc + 2
}
};
};
const SLOAD = () => globalState => {
const key = globalState.callState.stack.slice(0, 1);
const value = getAccount().storage[new Buffer(key)];
return {
...globalState,
callState: {
...globalState.callState,
stack: [...globalState.callState.stack.slice(1), new BN(value)],
pc: globalState.callState.pc + 2
}
};
};
const SSTORE = () => globalState => {
const [key, value] = globalState.callState.stack.slice(0, 2);
const account = getAccount();
account.storage[key] = value;
return {
...putAccount(account),
callState: {
...globalState.callState,
stack: [...globalState.callState.stack.slice(2)],
pc: globalState.callState.pc + 2
}
};
};
const MLOAD = () => globalState => {
const a = globalState.callState.stack.slice(0, 1);
const word = globalState.callState.memory.slice(a, a + 32);
return {
...globalState,
callState: {
...globalState.callState,
stack: [...globalState.callState.stack.slice(1), new BN(word)],
pc: globalState.callState.pc + 2
}
};
};
const MSTORE = () => globalState => {
const [offset, value] = globalState.callState.stack.slice(0, 2);
const valueArray = toBuffer(value);
const mem = globalState.callState.memory;
const memory = [
...mem.slice(0, offset),
...valueArray,
...mem.slice(offset + value.length)
];
return {
...globalState,
callState: {
...globalState.callState,
stack: [...globalState.callState.stack.slice(2)],
memory,
pc: globalState.callState.pc + 2
}
};
};
const CALL = () => globalState => {
[
gas,
addr,
value,
argsOffset,
argsLength,
retOffset,
retLength
] = globalState.callState.stack.slice(0, 7);
let account = getAccount(addr);
let callState = {
...callState(),
code: account.code, //load destination Address
caller: globalState.env.address, //
callData: new Buffer(
globalState.callState.memory.slice(argsOffset, argsOffset + argsLength)
), //load callData
callValue: value, //load Call value
depth: globalState.callState.depth + 1 //Increase depth
};
let env = {
...env(),
address: addr,
contract: account
};
state = {
...globalState,
callState,
env
};
let ret = execute(state);
const stack = [...globalState.callState.stack.slice(7)];
const mem = globalState.callState.memory;
const memory = [
...mem.slice(0, retOffset),
...ret.env.returnValue,
...mem.slice(retOffset + retLength)
];
const accounts = ret.callState.halt
? { ...globalState.accounts }
: { ...ret.accounts };
return {
...globalState,
callState: {
...callState,
stack: stack.concat(new BN(ret.callState.halt ? 0 : 1)),
pc: globalState.callState.pc + 2,
memory
},
env: {
...globalState.env,
returnValue: ret.env.returnValue
},
accounts
};
};
const RETURNOP = () => globalState => {
[offset, length] = globalState.callState.stack.slice(0, 2);
word = globalState.callState.memory.slice(offset, offseta + length);
return {
...globalState,
callState: {
...callState,
stack: globalState.callState.stack.slice(2)
},
env: {
...globalState.callState.evn,
returnValue: word
}
};
};
//ARITHMETIC
const ADD = (a, b) => a.add(b).mod(uint256);
const MUL = (a, b) => a.mul(b).mod(uint256);
const SUB = (a, b) => a.sub(b).mod(uint256);
const DIV = (a, b) => (b.isZero() ? new BN(b) : a.div(b));
const SDIV = (a, b) =>
b.isZero()
? new BN(b)
: a
.fromTwos(256)
.div(b.fromTwos(256))
.toTwos(256);
const MOD = (a, b) => a.mod(b);
const SMOD = (a, b) => {
if (b.isZero()) {
r = new BN(b);
} else {
a = a.fromTwos(256);
b = b.fromTwos(256);
r = a.abs().mod(b.abs());
if (a.isNeg()) {
r = r.ineg();
}
r = r.toTwos(256);
}
return r;
};
const ADDMOD = (a, b, c) => a.add(b.mod(c));
const MULMOD = (a, b, c) => a.mul(b.mod(c));
const EXP = (a, b) => a.pow(b).mod(uint256); //NOTE EXP cost dynamic gas, That's no dealt with yet
const SIGNEXTEND = (k, val) => {
val = val.toArrayLike(Buffer, "be", 32);
var extendOnes = false;
if (k.lten(31)) {
k = k.toNumber();
if (val[31 - k] & 0x80) {
extendOnes = true;
}
// 31-k-1 since k-th byte shouldn't be modified
for (var i = 30 - k; i >= 0; i--) {
val[i] = extendOnes ? 0xff : 0;
}
}
return new BN(val);
};
// COMPARISSON
const LT = (a, b) => new BN(a < b ? 1 : 0);
const GT = (a, b) => new BN(a > b ? 1 : 0);
const SLT = (a, b) => new BN(a.fromTwos(256).lt(b.fromTwos(256)) ? 1 : 0);
const SGT = (a, b) => new BN(a.fromTwos(256).gt(b.fromTwos(256)) ? 1 : 0);
const EQ = (a, b) => new BN(a == b ? 1 : 0);
const ISZERO = a => new BN(a == 0 ? 1 : 0);
//BIT
const AND = (a, b) => a.and(b);
const OR = (a, b) => a.or(b);
const XOR = (a, b) => a.xor(b);
const NOT = a => a.notn(256);
const BYTE = (a, b) =>
new BN(a.gten(32) ? 0 : b.shrn((31 - a.toNumber()) * 8).andln(0xff));
/**
HELPERS
**/
const getOp = (call, pc = call.pc) => {
if (call.pc > call.code.length) {
return "";
}
return parseInt(call.code[pc] + call.code[pc + 1], 16);
};
//Read `item` form globalState and add it to the stack
const stateToStack = path => globalState => {
const item = path.reduce((state, key) => {
return state[key];
}, globalState);
return {
...globalState,
callState: {
...callState,
pc: globalState.callState.pc + 2,
stack: [...globalState.callState.stack, item]
}
};
};
const stackOp1 = op => globalState => {
const a = globalState.callState.stack.slice(0, 1);
return {
...globalState,
callState: {
...callState,
stack: [...callState.stack.slice(1), op(a)],
pc: callState.pc + 2
}
};
};
const stackOp2 = op => globalState => {
const [a, b] = globalState.callState.stack.slice(0, 2);
return {
...globalState,
callState: {
...callState,
stack: [...callState.stack.slice(2), op(a, b)],
pc: callState.pc + 2
}
};
};
const stackOp3 = op => globalState => {
const [a, b, c] = globalState.callState.stack.slice(0, 3);
return {
...globalState,
callState: {
...callState,
stack: [...callState.stack.slice(3), op(a, b, c)],
pc: callState.pc + 2
}
};
};
// A helper function to raise an exception state
const errorState = () => globalState => {
return { ...globalState, callState: { ...callState, halt: true } };
};
const toBuffer = value => {
return value.toArray("be", 32);
};
const getAccount = globalState => (address = globalState.env.address) => {
return globalState.accounts[address];
};
const putAccount = globalState => account => {
const newState = { ...globalState };
newState.accounts[globalState.address.to] = account;
return newState;
};