forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 5
/
opFns.js
1036 lines (923 loc) · 29.8 KB
/
opFns.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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const Buffer = require('safe-buffer').Buffer
const async = require('async')
const utils = require('ethereumjs-util')
const BN = utils.BN
const exceptions = require('./exceptions.js')
const logTable = require('./logTable.js')
const ERROR = exceptions.ERROR
const VmError = exceptions.VmError
const MASK_160 = new BN(1).shln(160).subn(1)
// Find Ceil(`this` / `num`)
BN.prototype.divCeil = function divCeil (num) {
var dm = this.divmod(num)
// Fast case - exact division
if (dm.mod.isZero()) return dm.div
// Round up
return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1)
}
function addressToBuffer (address) {
return address.and(MASK_160).toArrayLike(Buffer, 'be', 20)
}
// the opcode functions
module.exports = {
STOP: function (runState) {
runState.stopped = true
},
ADD: function (a, b, runState) {
return a.add(b).mod(utils.TWO_POW256)
},
MUL: function (a, b, runState) {
return a.mul(b).mod(utils.TWO_POW256)
},
SUB: function (a, b, runState) {
return a.sub(b).toTwos(256)
},
DIV: function (a, b, runState) {
if (b.isZero()) {
return new BN(b)
} else {
return a.div(b)
}
},
SDIV: function (a, b, runState) {
if (b.isZero()) {
return new BN(b)
} else {
a = a.fromTwos(256)
b = b.fromTwos(256)
return a.div(b).toTwos(256)
}
},
MOD: function (a, b, runState) {
if (b.isZero()) {
return new BN(b)
} else {
return a.mod(b)
}
},
SMOD: function (a, b, runState) {
if (b.isZero()) {
return new BN(b)
} else {
a = a.fromTwos(256)
b = b.fromTwos(256)
var r = a.abs().mod(b.abs())
if (a.isNeg()) {
r = r.ineg()
}
return r.toTwos(256)
}
},
ADDMOD: function (a, b, c, runState) {
if (c.isZero()) {
return new BN(c)
} else {
return a.add(b).mod(c)
}
},
MULMOD: function (a, b, c, runState) {
if (c.isZero()) {
return new BN(c)
} else {
return a.mul(b).mod(c)
}
},
EXP: function (base, exponent, runState) {
var m = BN.red(utils.TWO_POW256)
base = base.toRed(m)
if (!exponent.isZero()) {
var bytes = 1 + logTable(exponent)
subGas(runState, new BN(bytes).muln(runState._common.param('gasPrices', 'expByte')))
return base.redPow(exponent)
} else {
return new BN(1)
}
},
SIGNEXTEND: function (k, val, runState) {
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)
},
// 0x10 range - bit ops
LT: function (a, b, runState) {
return new BN(a.lt(b) ? 1 : 0)
},
GT: function (a, b, runState) {
return new BN(a.gt(b) ? 1 : 0)
},
SLT: function (a, b, runState) {
return new BN(a.fromTwos(256).lt(b.fromTwos(256)) ? 1 : 0)
},
SGT: function (a, b, runState) {
return new BN(a.fromTwos(256).gt(b.fromTwos(256)) ? 1 : 0)
},
EQ: function (a, b, runState) {
return new BN(a.eq(b) ? 1 : 0)
},
ISZERO: function (a, runState) {
return new BN(a.isZero() ? 1 : 0)
},
AND: function (a, b, runState) {
return a.and(b)
},
OR: function (a, b, runState) {
return a.or(b)
},
XOR: function (a, b, runState) {
return a.xor(b)
},
NOT: function (a, runState) {
return a.notn(256)
},
BYTE: function (pos, word, runState) {
if (pos.gten(32)) {
return new BN(0)
}
return new BN(word.shrn((31 - pos.toNumber()) * 8).andln(0xff))
},
SHL: function (a, b, runState) {
if (!runState._common.gteHardfork('constantinople')) {
trap(ERROR.INVALID_OPCODE)
}
if (a.gten(256)) {
return new BN(0)
}
return b.shln(a.toNumber()).iand(utils.MAX_INTEGER)
},
SHR: function (a, b, runState) {
if (!runState._common.gteHardfork('constantinople')) {
trap(ERROR.INVALID_OPCODE)
}
if (a.gten(256)) {
return new BN(0)
}
return b.shrn(a.toNumber())
},
SAR: function (a, b, runState) {
if (!runState._common.gteHardfork('constantinople')) {
trap(ERROR.INVALID_OPCODE)
}
const isSigned = b.testn(255)
if (a.gten(256)) {
if (isSigned) {
return new BN(utils.MAX_INTEGER)
} else {
return new BN(0)
}
}
const c = b.shrn(a.toNumber())
if (isSigned) {
const shiftedOutWidth = 255 - a.toNumber()
const mask = utils.MAX_INTEGER.shrn(shiftedOutWidth).shln(shiftedOutWidth)
return c.ior(mask)
} else {
return c
}
},
// 0x20 range - crypto
SHA3: function (offset, length, runState) {
var data = memLoad(runState, offset, length)
// copy fee
subGas(runState, new BN(runState._common.param('gasPrices', 'sha3Word')).imul(length.divCeil(new BN(32))))
return new BN(utils.keccak256(data))
},
// 0x30 range - closure state
ADDRESS: function (runState) {
return new BN(runState.address)
},
BALANCE: function (address, runState, cb) {
var stateManager = runState.stateManager
// stack to address
address = addressToBuffer(address)
// shortcut if current account
if (address.toString('hex') === runState.address.toString('hex')) {
cb(null, new BN(runState.contract.balance))
return
}
// otherwise load account then return balance
stateManager.getAccountBalance(address, function (err, value) {
if (err) {
return cb(err)
}
cb(null, new BN(value))
})
},
ORIGIN: function (runState) {
return new BN(runState.origin)
},
CALLER: function (runState) {
return new BN(runState.caller)
},
CALLVALUE: function (runState) {
return new BN(runState.callValue)
},
CALLDATALOAD: function (pos, runState) {
if (pos.gtn(runState.callData.length)) {
return new BN(0)
} else {
pos = pos.toNumber()
var loaded = runState.callData.slice(pos, pos + 32)
loaded = loaded.length ? loaded : Buffer.from([0])
return new BN(utils.setLengthRight(loaded, 32))
}
},
CALLDATASIZE: function (runState) {
if (runState.callData.length === 1 && runState.callData[0] === 0) {
return new BN(0)
} else {
return new BN(runState.callData.length)
}
},
CALLDATACOPY: function (memOffset, dataOffset, dataLength, runState) {
memStore(runState, memOffset, runState.callData, dataOffset, dataLength)
// sub the COPY fee
subGas(runState, new BN(runState._common.param('gasPrices', 'copy')).imul(dataLength.divCeil(new BN(32))))
},
CODESIZE: function (runState) {
return new BN(runState.code.length)
},
CODECOPY: function (memOffset, codeOffset, length, runState) {
memStore(runState, memOffset, runState.code, codeOffset, length)
// sub the COPY fee
subGas(runState, new BN(runState._common.param('gasPrices', 'copy')).imul(length.divCeil(new BN(32))))
},
EXTCODESIZE: function (address, runState, cb) {
var stateManager = runState.stateManager
address = addressToBuffer(address)
stateManager.getContractCode(address, function (err, code) {
if (err) return cb(err)
cb(null, new BN(code.length))
})
},
EXTCODECOPY: function (address, memOffset, codeOffset, length, runState, cb) {
var stateManager = runState.stateManager
address = addressToBuffer(address)
// FIXME: for some reason this must come before subGas
subMemUsage(runState, memOffset, length)
// copy fee
subGas(runState, new BN(runState._common.param('gasPrices', 'copy')).imul(length.divCeil(new BN(32))))
stateManager.getContractCode(address, function (err, code) {
if (err) return cb(err)
memStore(runState, memOffset, code, codeOffset, length, false)
cb(null)
})
},
RETURNDATASIZE: function (runState) {
return new BN(runState.lastReturned.length)
},
RETURNDATACOPY: function (memOffset, returnDataOffset, length, runState) {
if ((returnDataOffset.add(length)).gtn(runState.lastReturned.length)) {
trap(ERROR.OUT_OF_GAS)
}
memStore(runState, memOffset, utils.toBuffer(runState.lastReturned), returnDataOffset, length, false)
// sub the COPY fee
subGas(runState, new BN(runState._common.param('gasPrices', 'copy')).mul(length.divCeil(new BN(32))))
},
GASPRICE: function (runState) {
return new BN(runState.gasPrice)
},
// '0x40' range - block operations
BLOCKHASH: function (number, runState, cb) {
var stateManager = runState.stateManager
var diff = new BN(runState.block.header.number).sub(number)
// block lookups must be within the past 256 blocks
if (diff.gtn(256) || diff.lten(0)) {
cb(null, new BN(0))
return
}
stateManager.getBlockHash(number.toArrayLike(Buffer, 'be', 32), function (err, blockHash) {
if (err) return cb(err)
cb(null, new BN(blockHash))
})
},
COINBASE: function (runState) {
return new BN(runState.block.header.coinbase)
},
TIMESTAMP: function (runState) {
return new BN(runState.block.header.timestamp)
},
NUMBER: function (runState) {
return new BN(runState.block.header.number)
},
DIFFICULTY: function (runState) {
return new BN(runState.block.header.difficulty)
},
GASLIMIT: function (runState) {
return new BN(runState.block.header.gasLimit)
},
// 0x50 range - 'storage' and execution
POP: function () {},
MLOAD: function (pos, runState) {
return new BN(memLoad(runState, pos, new BN(32)))
},
MSTORE: function (offset, word, runState) {
word = word.toArrayLike(Buffer, 'be', 32)
memStore(runState, offset, word, new BN(0), new BN(32))
},
MSTORE8: function (offset, byte, runState) {
// NOTE: we're using a 'trick' here to get the least significant byte
byte = Buffer.from([ byte.andln(0xff) ])
memStore(runState, offset, byte, new BN(0), new BN(1))
},
SLOAD: function (key, runState, cb) {
var stateManager = runState.stateManager
key = key.toArrayLike(Buffer, 'be', 32)
stateManager.getContractStorage(runState.address, key, function (err, value) {
if (err) return cb(err)
value = value.length ? new BN(value) : new BN(0)
cb(null, value)
})
},
SSTORE: function (key, val, runState, cb) {
if (runState.static) {
trap(ERROR.STATIC_STATE_CHANGE)
}
var stateManager = runState.stateManager
var address = runState.address
key = key.toArrayLike(Buffer, 'be', 32)
// NOTE: this should be the shortest representation
var value
if (val.isZero()) {
value = Buffer.from([])
} else {
value = val.toArrayLike(Buffer, 'be')
}
stateManager.getContractStorage(runState.address, key, function (err, found) {
if (err) return cb(err)
try {
if (value.length === 0 && !found.length) {
subGas(runState, new BN(runState._common.param('gasPrices', 'sstoreReset')))
} else if (value.length === 0 && found.length) {
subGas(runState, new BN(runState._common.param('gasPrices', 'sstoreReset')))
runState.gasRefund.iaddn(runState._common.param('gasPrices', 'sstoreRefund'))
} else if (value.length !== 0 && !found.length) {
subGas(runState, new BN(runState._common.param('gasPrices', 'sstoreSet')))
} else if (value.length !== 0 && found.length) {
subGas(runState, new BN(runState._common.param('gasPrices', 'sstoreReset')))
}
} catch (e) {
cb(e.error)
return
}
stateManager.putContractStorage(address, key, value, function (err) {
if (err) return cb(err)
runState.contract = stateManager.cache.get(address)
cb(null)
})
})
},
JUMP: function (dest, runState) {
if (dest.gtn(runState.code.length)) {
trap(ERROR.INVALID_JUMP + ' at ' + describeLocation(runState))
}
dest = dest.toNumber()
if (!jumpIsValid(runState, dest)) {
trap(ERROR.INVALID_JUMP + ' at ' + describeLocation(runState))
}
runState.programCounter = dest
},
JUMPI: function (dest, cond, runState) {
if (!cond.isZero()) {
if (dest.gtn(runState.code.length)) {
trap(ERROR.INVALID_JUMP + ' at ' + describeLocation(runState))
}
dest = dest.toNumber()
if (!jumpIsValid(runState, dest)) {
trap(ERROR.INVALID_JUMP + ' at ' + describeLocation(runState))
}
runState.programCounter = dest
}
},
PC: function (runState) {
return new BN(runState.programCounter - 1)
},
MSIZE: function (runState) {
return runState.memoryWordCount.muln(32)
},
GAS: function (runState) {
return new BN(runState.gasLeft)
},
JUMPDEST: function (runState) {},
PUSH: function (runState) {
const numToPush = runState.opCode - 0x5f
const loaded = new BN(runState.code.slice(runState.programCounter, runState.programCounter + numToPush).toString('hex'), 16)
runState.programCounter += numToPush
return loaded
},
DUP: function (runState) {
// NOTE: this function manipulates the stack directly!
const stackPos = runState.opCode - 0x7f
if (stackPos > runState.stack.length) {
trap(ERROR.STACK_UNDERFLOW)
}
// create a new copy
return new BN(runState.stack[runState.stack.length - stackPos])
},
SWAP: function (runState) {
// NOTE: this function manipulates the stack directly!
var stackPos = runState.opCode - 0x8f
// check the stack to make sure we have enough items on teh stack
var swapIndex = runState.stack.length - stackPos - 1
if (swapIndex < 0) {
trap(ERROR.STACK_UNDERFLOW)
}
// preform the swap
var topIndex = runState.stack.length - 1
var tmp = runState.stack[topIndex]
runState.stack[topIndex] = runState.stack[swapIndex]
runState.stack[swapIndex] = tmp
},
LOG: function (memOffset, memLength) {
var args = Array.prototype.slice.call(arguments, 0)
var runState = args.pop()
if (runState.static) {
trap(ERROR.STATIC_STATE_CHANGE)
}
var topics = args.slice(2)
topics = topics.map(function (a) {
return a.toArrayLike(Buffer, 'be', 32)
})
const numOfTopics = runState.opCode - 0xa0
const mem = memLoad(runState, memOffset, memLength)
subGas(runState, new BN(runState._common.param('gasPrices', 'logTopic')).imuln(numOfTopics).iadd(memLength.muln(runState._common.param('gasPrices', 'logData'))))
// add address
var log = [runState.address]
log.push(topics)
// add data
log.push(mem)
runState.logs.push(log)
},
// '0xf0' range - closures
CREATE: function (value, offset, length, runState, done) {
if (runState.static) {
trap(ERROR.STATIC_STATE_CHANGE)
}
var data = memLoad(runState, offset, length)
// set up config
var options = {
value: value,
data: data
}
var localOpts = {
inOffset: offset,
inLength: length,
outOffset: new BN(0),
outLength: new BN(0)
}
checkCallMemCost(runState, options, localOpts)
checkOutOfGas(runState, options)
makeCall(runState, options, localOpts, done)
},
CALL: function (gasLimit, toAddress, value, inOffset, inLength, outOffset, outLength, runState, done) {
var stateManager = runState.stateManager
toAddress = addressToBuffer(toAddress)
if (runState.static && !value.isZero()) {
trap(ERROR.STATIC_STATE_CHANGE)
}
var data = memLoad(runState, inOffset, inLength)
var options = {
gasLimit: gasLimit,
value: value,
to: toAddress,
data: data,
static: runState.static
}
var localOpts = {
inOffset: inOffset,
inLength: inLength,
outOffset: outOffset,
outLength: outLength
}
if (!value.isZero()) {
subGas(runState, new BN(runState._common.param('gasPrices', 'callValueTransfer')))
}
stateManager.exists(toAddress, function (err, exists) {
if (err) {
done(err)
return
}
stateManager.accountIsEmpty(toAddress, function (err, empty) {
if (err) {
done(err)
return
}
if (!exists || empty) {
if (!value.isZero()) {
try {
subGas(runState, new BN(runState._common.param('gasPrices', 'callNewAccount')))
} catch (e) {
done(e.error)
return
}
}
}
try {
checkCallMemCost(runState, options, localOpts)
checkOutOfGas(runState, options)
} catch (e) {
done(e.error)
return
}
if (!value.isZero()) {
runState.gasLeft.iaddn(runState._common.param('gasPrices', 'callStipend'))
options.gasLimit.iaddn(runState._common.param('gasPrices', 'callStipend'))
}
makeCall(runState, options, localOpts, done)
})
})
},
CALLCODE: function (gas, toAddress, value, inOffset, inLength, outOffset, outLength, runState, done) {
var stateManager = runState.stateManager
toAddress = addressToBuffer(toAddress)
var data = memLoad(runState, inOffset, inLength)
const options = {
gasLimit: gas,
value: value,
data: data,
to: runState.address,
static: runState.static
}
const localOpts = {
inOffset: inOffset,
inLength: inLength,
outOffset: outOffset,
outLength: outLength
}
if (!value.isZero()) {
subGas(runState, new BN(runState._common.param('gasPrices', 'callValueTransfer')))
}
checkCallMemCost(runState, options, localOpts)
checkOutOfGas(runState, options)
if (!value.isZero()) {
runState.gasLeft.iaddn(runState._common.param('gasPrices', 'callStipend'))
options.gasLimit.iaddn(runState._common.param('gasPrices', 'callStipend'))
}
// load the code
stateManager.getAccount(toAddress, function (err, account) {
if (err) return done(err)
if (runState._precompiled[toAddress.toString('hex')]) {
options.compiled = true
options.code = runState._precompiled[toAddress.toString('hex')]
makeCall(runState, options, localOpts, done)
} else {
stateManager.getContractCode(toAddress, function (err, code, compiled) {
if (err) return done(err)
options.compiled = compiled || false
options.code = code
makeCall(runState, options, localOpts, done)
})
}
})
},
DELEGATECALL: function (gas, toAddress, inOffset, inLength, outOffset, outLength, runState, done) {
var stateManager = runState.stateManager
var value = runState.callValue
toAddress = addressToBuffer(toAddress)
var data = memLoad(runState, inOffset, inLength)
const options = {
gasLimit: gas,
value: value,
data: data,
to: runState.address,
caller: runState.caller,
delegatecall: true,
static: runState.static
}
const localOpts = {
inOffset: inOffset,
inLength: inLength,
outOffset: outOffset,
outLength: outLength
}
checkCallMemCost(runState, options, localOpts)
checkOutOfGas(runState, options)
// load the code
stateManager.getAccount(toAddress, function (err, account) {
if (err) return done(err)
if (runState._precompiled[toAddress.toString('hex')]) {
options.compiled = true
options.code = runState._precompiled[toAddress.toString('hex')]
makeCall(runState, options, localOpts, done)
} else {
stateManager.getContractCode(toAddress, function (err, code, compiled) {
if (err) return done(err)
options.compiled = compiled || false
options.code = code
makeCall(runState, options, localOpts, done)
})
}
})
},
STATICCALL: function (gasLimit, toAddress, inOffset, inLength, outOffset, outLength, runState, done) {
var stateManager = runState.stateManager
var value = new BN(0)
toAddress = addressToBuffer(toAddress)
var data = memLoad(runState, inOffset, inLength)
var options = {
gasLimit: gasLimit,
value: value,
to: toAddress,
data: data,
static: true
}
var localOpts = {
inOffset: inOffset,
inLength: inLength,
outOffset: outOffset,
outLength: outLength
}
stateManager.exists(toAddress, function (err, exists) {
if (err) {
done(err)
return
}
stateManager.accountIsEmpty(toAddress, function (err, empty) {
if (err) {
done(err)
return
}
try {
checkCallMemCost(runState, options, localOpts)
checkOutOfGas(runState, options)
} catch (e) {
done(e.error)
return
}
makeCall(runState, options, localOpts, done)
})
})
},
RETURN: function (offset, length, runState) {
runState.returnValue = memLoad(runState, offset, length)
},
REVERT: function (offset, length, runState) {
runState.stopped = true
runState.returnValue = memLoad(runState, offset, length)
trap(ERROR.REVERT)
},
// '0x70', range - other
SELFDESTRUCT: function (selfdestructToAddress, runState, cb) {
if (runState.static) {
trap(ERROR.STATIC_STATE_CHANGE)
}
var stateManager = runState.stateManager
var contract = runState.contract
var contractAddress = runState.address
selfdestructToAddress = addressToBuffer(selfdestructToAddress)
stateManager.getAccount(selfdestructToAddress, function (err, toAccount) {
// update balances
if (err) {
cb(err)
return
}
stateManager.accountIsEmpty(selfdestructToAddress, function (error, empty) {
if (error) {
cb(error)
return
}
if ((new BN(contract.balance)).gtn(0)) {
if (!toAccount.exists || empty) {
try {
subGas(runState, new BN(runState._common.param('gasPrices', 'callNewAccount')))
} catch (e) {
cb(e.error)
return
}
}
}
// only add to refund if this is the first selfdestruct for the address
if (!runState.selfdestruct[contractAddress.toString('hex')]) {
runState.gasRefund = runState.gasRefund.addn(runState._common.param('gasPrices', 'selfdestructRefund'))
}
runState.selfdestruct[contractAddress.toString('hex')] = selfdestructToAddress
runState.stopped = true
var newBalance = new BN(contract.balance).add(new BN(toAccount.balance))
async.series([
stateManager.putAccountBalance.bind(stateManager, selfdestructToAddress, newBalance),
stateManager.putAccountBalance.bind(stateManager, contractAddress, new BN(0))
], function (err) {
// The reason for this is to avoid sending an array of results
cb(err)
})
})
})
}
}
function describeLocation (runState) {
var hash = utils.keccak256(runState.code).toString('hex')
var address = runState.address.toString('hex')
var pc = runState.programCounter - 1
return hash + '/' + address + ':' + pc
}
function subGas (runState, amount) {
runState.gasLeft.isub(amount)
if (runState.gasLeft.ltn(0)) {
runState.gasLeft = new BN(0)
trap(ERROR.OUT_OF_GAS)
}
}
function trap (err) {
throw new VmError(err)
}
/**
* Subtracts the amount needed for memory usage from `runState.gasLeft`
* @method subMemUsage
* @param {BN} offset
* @param {BN} length
* @return {String}
*/
function subMemUsage (runState, offset, length) {
// YP (225): access with zero length will not extend the memory
if (length.isZero()) return
const newMemoryWordCount = offset.add(length).divCeil(new BN(32))
if (newMemoryWordCount.lte(runState.memoryWordCount)) return
const words = newMemoryWordCount
const fee = new BN(runState._common.param('gasPrices', 'memory'))
const quadCoeff = new BN(runState._common.param('gasPrices', 'quadCoeffDiv'))
// words * 3 + words ^2 / 512
const cost = words.mul(fee).add(words.mul(words).div(quadCoeff))
if (cost.gt(runState.highestMemCost)) {
subGas(runState, cost.sub(runState.highestMemCost))
runState.highestMemCost = cost
}
runState.memoryWordCount = newMemoryWordCount
}
/**
* Loads bytes from memory and returns them as a buffer. If an error occurs
* a string is instead returned. The function also subtracts the amount of
* gas need for memory expansion.
* @method memLoad
* @param {BN} offset where to start reading from
* @param {BN} length how far to read
* @return {Buffer|String}
*/
function memLoad (runState, offset, length) {
// check to see if we have enougth gas for the mem read
subMemUsage(runState, offset, length)
// shortcut
if (length.isZero()) {
return Buffer.alloc(0)
}
// NOTE: in theory this could overflow, but unlikely due to OOG above
offset = offset.toNumber()
length = length.toNumber()
var loaded = runState.memory.slice(offset, offset + length)
// fill the remaining lenth with zeros
for (var i = loaded.length; i < length; i++) {
loaded[i] = 0
}
return Buffer.from(loaded)
}
/**
* Stores bytes to memory. If an error occurs a string is instead returned.
* The function also subtracts the amount of gas need for memory expansion.
* @method memStore
* @param {BN} offset where to start reading from
* @param {Buffer} val
* @param {BN} valOffset
* @param {BN} length how far to read
* @param {Boolean} skipSubMem
* @return {Buffer|String}
*/
function memStore (runState, offset, val, valOffset, length, skipSubMem) {
if (skipSubMem !== false) {
subMemUsage(runState, offset, length)
}
// shortcut
if (length.isZero()) {
return
}
// NOTE: in theory this could overflow, but unlikely due to OOG above
offset = offset.toNumber()
length = length.toNumber()
var safeLen = 0
if (valOffset.addn(length).gtn(val.length)) {
if (valOffset.gten(val.length)) {
safeLen = 0
} else {
valOffset = valOffset.toNumber()
safeLen = val.length - valOffset
}
} else {
valOffset = valOffset.toNumber()
safeLen = val.length
}
let i = 0
if (safeLen > 0) {
safeLen = safeLen > length ? length : safeLen
for (; i < safeLen; i++) {
runState.memory[offset + i] = val[valOffset + i]
}
}
/*
pad the remaining length with zeros IF AND ONLY IF a value was stored
(even if value offset > value length, strange spec...)
*/
if (val.length > 0 && i < length) {
for (; i < length; i++) {
runState.memory[offset + i] = 0
}
}
}
// checks if a jump is valid given a destination
function jumpIsValid (runState, dest) {
return runState.validJumps.indexOf(dest) !== -1
}
// checks to see if we have enough gas left for the memory reads and writes
// required by the CALLs
function checkCallMemCost (runState, callOptions, localOpts) {
// calculates the gas need for saving the output in memory
subMemUsage(runState, localOpts.outOffset, localOpts.outLength)
if (!callOptions.gasLimit) {
callOptions.gasLimit = new BN(runState.gasLeft)
}
}
function checkOutOfGas (runState, callOptions) {
const gasAllowed = runState.gasLeft.sub(runState.gasLeft.divn(64))
if (callOptions.gasLimit.gt(gasAllowed)) {
callOptions.gasLimit = gasAllowed
}
}
// sets up and calls runCall
function makeCall (runState, callOptions, localOpts, cb) {
callOptions.caller = callOptions.caller || runState.address
callOptions.origin = runState.origin
callOptions.gasPrice = runState.gasPrice
callOptions.block = runState.block
callOptions.populateCache = false
callOptions.static = callOptions.static || false
callOptions.selfdestruct = runState.selfdestruct
// increment the runState.depth
callOptions.depth = runState.depth + 1
// empty the return data buffer
runState.lastReturned = Buffer.alloc(0)
// check if account has enough ether
// Note: in the case of delegatecall, the value is persisted and doesn't need to be deducted again
if (runState.depth >= runState._common.param('vm', 'stackLimit') || (callOptions.delegatecall !== true && new BN(runState.contract.balance).lt(callOptions.value))) {
cb(null, new BN(0))
} else {
// if creating a new contract then increament the nonce
if (!callOptions.to) {
runState.contract.nonce = new BN(runState.contract.nonce).addn(1)
}
runState.stateManager.cache.put(runState.address, runState.contract)
runState._vm.runCall(callOptions, parseCallResults)
}
function parseCallResults (err, results) {
if (err) return cb(err)
// concat the runState.logs
if (results.vm.logs) {
runState.logs = runState.logs.concat(results.vm.logs)
}
// add gasRefund
if (results.vm.gasRefund) {
runState.gasRefund = runState.gasRefund.add(results.vm.gasRefund)
}
// this should always be safe
runState.gasLeft.isub(results.gasUsed)
// save results to memory
if (results.vm.return && (!results.vm.exceptionError || results.vm.exceptionError.error === ERROR.REVERT)) {
memStore(runState, localOpts.outOffset, results.vm.return, new BN(0), localOpts.outLength, false)
if (results.vm.exceptionError && results.vm.exceptionError.error === ERROR.REVERT && runState.opName === 'CREATE') {