-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructions.go
1706 lines (1332 loc) · 45.9 KB
/
instructions.go
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
package vm
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
"golang.org/x/crypto/sha3"
)
// bock number, opcode_name, execution time , state bloat
type dataOP struct {
B int `json:"b"`
O string `json:"o"`
E int `json:"e"`
L uint `json:"l"`
}
type data_ex struct {
B uint64 `json:"b"`
T uint `json:"t"`
}
var m sync.Mutex
var siz uint = 0 // current siz of chain data
var min_prev int = -1 // prev value of m(minutes)
var tem int = 0 // to check if file is open
var arr1 [1000000]uint //for block execution time
var arr2 [1000000]uint
var arr3 [1000000]uint
var arr_op1 [1000000]uint //for total number of opcodes
var arr_op2 [1000000]uint
var arr_op3 [1000000]uint
var ele_map = make(map[string]uint) // map to store 1 million blocks execution time before converting that to json
var ele_map_op = make(map[string]uint)
var f *os.File
var num_file_write int = 2 // no. of files written to use trigger the next file
var num_file int = 1 // number of file
//extra data
var temp_ex int = 0
var f_ex *os.File
func write_block(bn uint64) {
//check the condition to write on file again
tem := bn / 1000000
k := (tem % 3)
if tem == uint64(num_file_write) {
num_file_write = num_file_write + 1
for i := 0; i < 1000000; i++ {
id := strconv.Itoa((num_file-1)*1000000 + i)
if k == 0 {
ele_map[id] = arr2[i]
arr2[i] = 0
ele_map_op[id] = arr_op2[i]
arr_op2[i] = 0
} else if k == 1 {
ele_map[id] = arr3[i]
arr3[i] = 0
ele_map_op[id] = arr_op3[i]
arr_op3[i] = 0
} else {
ele_map[id] = arr1[i]
arr1[i] = 0
ele_map_op[id] = arr_op1[i]
arr_op1[i] = 0
}
}
//write in file using map
//path of file
pathc := fmt.Sprintf("/home/ubuntu/data-evm-f/block_data/%v.json", num_file)
pathc_op := fmt.Sprintf("/home/ubuntu/data-evm-f/block_data_op/%v.json", num_file)
fc, err := os.OpenFile(pathc, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
fmt.Println(err)
}
byteArray, _ := json.Marshal(ele_map)
if _, err := fmt.Fprintf(fc, "%s\n", byteArray); err != nil {
fmt.Println(err)
}
defer fc.Close()
fc_op, err := os.OpenFile(pathc_op, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
fmt.Println(err)
}
//write in file for opcode number
byteArray, _ = json.Marshal(ele_map_op)
if _, err := fmt.Fprintf(fc_op, "%s\n", byteArray); err != nil {
fmt.Println(err)
}
defer fc_op.Close()
//intialise the map again to delete the data
ele_map = make(map[string]uint)
ele_map_op = make(map[string]uint)
num_file = num_file + 1
}
}
func write_ex(t uint, bn uint64) {
if temp_ex == 0 {
f_ex, _ = os.OpenFile("/home/ubuntu/data-evm-f/data_ex/data.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
temp_ex = 1
}
tempData := data_ex{B: bn, T: t}
byteArray, err := json.Marshal(tempData)
if err != nil {
fmt.Println(err)
}
if _, err := fmt.Fprintf(f_ex, "%s\n", byteArray); err != nil {
fmt.Println(err)
}
}
func checkExe(t uint, bn uint64) {
if bn < (uint64(num_file)-1)*1000000 {
write_ex(t, bn)
} else {
tem := bn / 1000000
idx := bn % 1000000
m.Lock()
if tem%3 == 0 {
//update the array1
arr1[idx] = arr1[idx] + t
arr_op1[idx] = arr_op1[idx] + 1
} else if tem%3 == 1 {
//update the array2
arr2[idx] = arr2[idx] + t
arr_op2[idx] = arr_op2[idx] + 1
} else {
//update the array3
arr3[idx] = arr3[idx] + t
arr_op3[idx] = arr_op3[idx] + 1
}
if tem == uint64(num_file_write) { //trigger to write in file (1st million gets added in file after 2 million)
write_block(bn)
}
m.Unlock()
}
}
func writeFile(s string, i int, bnum int) {
if bnum%10000 == 0 {
siz = DirSize()
}
dt := time.Now()
d := dt.Format("01-02")
h := dt.Hour()
m := dt.Minute() / 5
if m != min_prev {
min_prev = m
if tem != 0 {
defer f.Close()
}
tem = 1
path := fmt.Sprintf("/home/ubuntu/data-evm-f/data/%s/%v/%v.json", d, h, m)
f, _ = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
}
tempData := dataOP{B: bnum, O: s, E: i, L: siz}
byteArray, err := json.Marshal(tempData)
if err != nil {
fmt.Println(err)
}
if _, err := fmt.Fprintf(f, "%s\n", byteArray); err != nil {
fmt.Println(err)
}
}
func DirSize() uint {
paths := "/home/ubuntu/.bor/data/bor/chaindata"
var size uint64
err := filepath.Walk(paths, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += uint64(info.Size())
}
return err
})
if err != nil {
fmt.Print(err)
}
t := size / 1048576 // returns size in MiB
return uint(t)
}
func opAdd(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
op := "Add"
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
y.Add(&x, y)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
writeFile(op, int(dif), int(cbnum))
return nil, nil
}
func opSub(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
y.Sub(&x, y)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opMul(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
y.Mul(&x, y)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opDiv(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
y.Div(&x, y)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opSdiv(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
y.SDiv(&x, y)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opMod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
y.Mod(&x, y)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opSmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
y.SMod(&x, y)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opExp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
base, exponent := scope.Stack.pop(), scope.Stack.peek()
exponent.Exp(&base, exponent)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opSignExtend(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
back, num := scope.Stack.pop(), scope.Stack.peek()
num.ExtendSign(num, &back)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opNot(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x := scope.Stack.peek()
x.Not(x)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opLt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
if x.Lt(y) {
y.SetOne()
} else {
y.Clear()
}
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opGt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
if x.Gt(y) {
y.SetOne()
} else {
y.Clear()
}
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opSlt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
if x.Slt(y) {
y.SetOne()
} else {
y.Clear()
}
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opSgt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
if x.Sgt(y) {
y.SetOne()
} else {
y.Clear()
}
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opEq(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
if x.Eq(y) {
y.SetOne()
} else {
y.Clear()
}
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opIszero(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x := scope.Stack.peek()
if x.IsZero() {
x.SetOne()
} else {
x.Clear()
}
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opAnd(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
y.And(&x, y)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opOr(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
y.Or(&x, y)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opXor(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y := scope.Stack.pop(), scope.Stack.peek()
y.Xor(&x, y)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opByte(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
th, val := scope.Stack.pop(), scope.Stack.peek()
val.Byte(&th)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opAddmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y, z := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.peek()
if z.IsZero() {
z.Clear()
} else {
z.AddMod(&x, &y, z)
}
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opMulmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
x, y, z := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.peek()
z.MulMod(&x, &y, z)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
// opSHL implements Shift Left
// The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2,
// and pushes on the stack arg2 shifted to the left by arg1 number of bits.
func opSHL(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
var startTime = time.Now().UnixNano()
shift, value := scope.Stack.pop(), scope.Stack.peek()
if shift.LtUint64(256) {
value.Lsh(value, uint(shift.Uint64()))
} else {
value.Clear()
}
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
// opSHR implements Logical Shift Right
// The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2,
// and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill.
func opSHR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
var startTime = time.Now().UnixNano()
shift, value := scope.Stack.pop(), scope.Stack.peek()
if shift.LtUint64(256) {
value.Rsh(value, uint(shift.Uint64()))
} else {
value.Clear()
}
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
// opSAR implements Arithmetic Shift Right
// The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2,
// and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension.
func opSAR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
shift, value := scope.Stack.pop(), scope.Stack.peek()
if shift.GtUint64(256) {
if value.Sign() >= 0 {
value.Clear()
} else {
// Max negative shift: all bits set
value.SetAllOne()
}
return nil, nil
}
n := uint(shift.Uint64())
value.SRsh(value, n)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opSha3(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
op := "Sha3"
var startTime = time.Now().UnixNano()
offset, size := scope.Stack.pop(), scope.Stack.peek()
data := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
if interpreter.hasher == nil {
interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState)
} else {
interpreter.hasher.Reset()
}
interpreter.hasher.Write(data)
interpreter.hasher.Read(interpreter.hasherBuf[:])
evm := interpreter.evm
if evm.Config.EnablePreimageRecording {
evm.StateDB.AddPreimage(interpreter.hasherBuf, data)
}
size.SetBytes(interpreter.hasherBuf[:])
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
writeFile(op, int(dif), int(cbnum))
return nil, nil
}
func opAddress(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Address().Bytes()))
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
slot := scope.Stack.peek()
address := common.Address(slot.Bytes20())
slot.SetFromBig(interpreter.evm.StateDB.GetBalance(address))
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opOrigin(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Origin.Bytes()))
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opCaller(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Caller().Bytes()))
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opCallValue(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
v, _ := uint256.FromBig(scope.Contract.value)
scope.Stack.push(v)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
op := "CallDataLoad"
var startTime = time.Now().UnixNano()
x := scope.Stack.peek()
if offset, overflow := x.Uint64WithOverflow(); !overflow {
data := getData(scope.Contract.Input, offset, 32)
x.SetBytes(data)
} else {
x.Clear()
}
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
writeFile(op, int(dif), int(cbnum))
return nil, nil
}
func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
op := "CallDataSize"
var startTime = time.Now().UnixNano()
scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(scope.Contract.Input))))
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
writeFile(op, int(dif), int(cbnum))
return nil, nil
}
func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
op := "CallDataCopy"
var startTime = time.Now().UnixNano()
var (
memOffset = scope.Stack.pop()
dataOffset = scope.Stack.pop()
length = scope.Stack.pop()
)
dataOffset64, overflow := dataOffset.Uint64WithOverflow()
if overflow {
dataOffset64 = 0xffffffffffffffff
}
// These values are checked for overflow during gas cost calculation
memOffset64 := memOffset.Uint64()
length64 := length.Uint64()
scope.Memory.Set(memOffset64, length64, getData(scope.Contract.Input, dataOffset64, length64))
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
writeFile(op, int(dif), int(cbnum))
return nil, nil
}
func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(interpreter.returnData))))
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
var (
memOffset = scope.Stack.pop()
dataOffset = scope.Stack.pop()
length = scope.Stack.pop()
)
offset64, overflow := dataOffset.Uint64WithOverflow()
if overflow {
return nil, ErrReturnDataOutOfBounds
}
// we can reuse dataOffset now (aliasing it for clarity)
var end = dataOffset
end.Add(&dataOffset, &length)
end64, overflow := end.Uint64WithOverflow()
if overflow || uint64(len(interpreter.returnData)) < end64 {
return nil, ErrReturnDataOutOfBounds
}
scope.Memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[offset64:end64])
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
slot := scope.Stack.peek()
slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(slot.Bytes20())))
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
l := new(uint256.Int)
l.SetUint64(uint64(len(scope.Contract.Code)))
scope.Stack.push(l)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
var (
memOffset = scope.Stack.pop()
codeOffset = scope.Stack.pop()
length = scope.Stack.pop()
)
uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
if overflow {
uint64CodeOffset = 0xffffffffffffffff
}
codeCopy := getData(scope.Contract.Code, uint64CodeOffset, length.Uint64())
scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
var (
stack = scope.Stack
a = stack.pop()
memOffset = stack.pop()
codeOffset = stack.pop()
length = stack.pop()
)
uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
if overflow {
uint64CodeOffset = 0xffffffffffffffff
}
addr := common.Address(a.Bytes20())
codeCopy := getData(interpreter.evm.StateDB.GetCode(addr), uint64CodeOffset, length.Uint64())
scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
// opExtCodeHash returns the code hash of a specified account.
// There are several cases when the function is called, while we can relay everything
// to `state.GetCodeHash` function to ensure the correctness.
// (1) Caller tries to get the code hash of a normal contract account, state
// should return the relative code hash and set it as the result.
//
// (2) Caller tries to get the code hash of a non-existent account, state should
// return common.Hash{} and zero will be set as the result.
//
// (3) Caller tries to get the code hash for an account without contract code,
// state should return emptyCodeHash(0xc5d246...) as the result.
//
// (4) Caller tries to get the code hash of a precompiled account, the result
// should be zero or emptyCodeHash.
//
// It is worth noting that in order to avoid unnecessary create and clean,
// all precompile accounts on mainnet have been transferred 1 wei, so the return
// here should be emptyCodeHash.
// If the precompile account is not transferred any amount on a private or
// customized chain, the return value will be zero.
//
// (5) Caller tries to get the code hash for an account which is marked as suicided
// in the current transaction, the code hash of this account should be returned.
//
// (6) Caller tries to get the code hash for an account which is marked as deleted,
// this account should be regarded as a non-existent account and zero should be returned.
func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
slot := scope.Stack.peek()
address := common.Address(slot.Bytes20())
if interpreter.evm.StateDB.Empty(address) {
slot.Clear()
} else {
slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(address).Bytes())
}
var endTime = time.Now().UnixNano()
var dif = endTime - startTime
cbnum := interpreter.evm.Context.BlockNumber.Uint64()
checkExe(uint(dif), cbnum)
return nil, nil
}
func opGasprice(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var startTime = time.Now().UnixNano()
v, _ := uint256.FromBig(interpreter.evm.GasPrice)
scope.Stack.push(v)
var endTime = time.Now().UnixNano()