1
1
import BN = require( 'bn.js' )
2
- import { keccak256 , setLengthRight , TWO_POW256 , MAX_INTEGER , KECCAK256_NULL } from 'ethereumjs-util'
2
+ import * as utils from 'ethereumjs-util'
3
3
import { ERROR , VmError } from '../exceptions'
4
4
import { RunState } from './interpreter'
5
5
@@ -38,12 +38,12 @@ export const handlers: { [k: string]: OpHandler } = {
38
38
} ,
39
39
ADD : function ( runState : RunState ) {
40
40
const [ a , b ] = runState . stack . popN ( 2 )
41
- const r = a . add ( b ) . mod ( TWO_POW256 )
41
+ const r = a . add ( b ) . mod ( utils . TWO_POW256 )
42
42
runState . stack . push ( r )
43
43
} ,
44
44
MUL : function ( runState : RunState ) {
45
45
const [ a , b ] = runState . stack . popN ( 2 )
46
- const r = a . mul ( b ) . mod ( TWO_POW256 )
46
+ const r = a . mul ( b ) . mod ( utils . TWO_POW256 )
47
47
runState . stack . push ( r )
48
48
} ,
49
49
SUB : function ( runState : RunState ) {
@@ -137,7 +137,7 @@ export const handlers: { [k: string]: OpHandler } = {
137
137
runState . stack . push ( new BN ( 0 ) )
138
138
return
139
139
}
140
- const m = BN . red ( TWO_POW256 )
140
+ const m = BN . red ( utils . TWO_POW256 )
141
141
const redBase = base . toRed ( m )
142
142
const r = redBase . redPow ( exponent )
143
143
runState . stack . push ( r . fromRed ( ) )
@@ -229,7 +229,7 @@ export const handlers: { [k: string]: OpHandler } = {
229
229
return
230
230
}
231
231
232
- const r = b . shln ( a . toNumber ( ) ) . iand ( MAX_INTEGER )
232
+ const r = b . shln ( a . toNumber ( ) ) . iand ( utils . MAX_INTEGER )
233
233
runState . stack . push ( r )
234
234
} ,
235
235
SHR : function ( runState : RunState ) {
@@ -255,7 +255,7 @@ export const handlers: { [k: string]: OpHandler } = {
255
255
const isSigned = b . testn ( 255 )
256
256
if ( a . gten ( 256 ) ) {
257
257
if ( isSigned ) {
258
- r = new BN ( MAX_INTEGER )
258
+ r = new BN ( utils . MAX_INTEGER )
259
259
} else {
260
260
r = new BN ( 0 )
261
261
}
@@ -266,7 +266,7 @@ export const handlers: { [k: string]: OpHandler } = {
266
266
const c = b . shrn ( a . toNumber ( ) )
267
267
if ( isSigned ) {
268
268
const shiftedOutWidth = 255 - a . toNumber ( )
269
- const mask = MAX_INTEGER . shrn ( shiftedOutWidth ) . shln ( shiftedOutWidth )
269
+ const mask = utils . MAX_INTEGER . shrn ( shiftedOutWidth ) . shln ( shiftedOutWidth )
270
270
r = c . ior ( mask )
271
271
} else {
272
272
r = c
@@ -285,7 +285,7 @@ export const handlers: { [k: string]: OpHandler } = {
285
285
runState . eei . useGas (
286
286
new BN ( runState . _common . param ( 'gasPrices' , 'sha3Word' ) ) . imul ( divCeil ( length , new BN ( 32 ) ) ) ,
287
287
)
288
- const r = new BN ( keccak256 ( data ) )
288
+ const r = new BN ( utils . keccak256 ( data ) )
289
289
runState . stack . push ( r )
290
290
} ,
291
291
// 0x30 range - closure state
@@ -317,7 +317,7 @@ export const handlers: { [k: string]: OpHandler } = {
317
317
const i = pos . toNumber ( )
318
318
let loaded = runState . eei . getCallData ( ) . slice ( i , i + 32 )
319
319
loaded = loaded . length ? loaded : Buffer . from ( [ 0 ] )
320
- const r = new BN ( setLengthRight ( loaded , 32 ) )
320
+ const r = new BN ( utils . setLengthRight ( loaded , 32 ) )
321
321
322
322
runState . stack . push ( r )
323
323
} ,
@@ -394,11 +394,11 @@ export const handlers: { [k: string]: OpHandler } = {
394
394
395
395
const code = await runState . eei . getExternalCode ( address )
396
396
if ( code . length === 0 ) {
397
- runState . stack . push ( new BN ( KECCAK256_NULL ) )
397
+ runState . stack . push ( new BN ( utils . KECCAK256_NULL ) )
398
398
return
399
399
}
400
400
401
- runState . stack . push ( new BN ( keccak256 ( code ) ) )
401
+ runState . stack . push ( new BN ( utils . keccak256 ( code ) ) )
402
402
} ,
403
403
RETURNDATASIZE : function ( runState : RunState ) {
404
404
runState . stack . push ( runState . eei . getReturnDataSize ( ) )
@@ -567,10 +567,18 @@ export const handlers: { [k: string]: OpHandler } = {
567
567
} ,
568
568
JUMPDEST : function ( runState : RunState ) { } ,
569
569
BEGINSUB : function ( runState : RunState ) {
570
+ if ( ! runState . _common . gteHardfork ( 'berlin' ) ) {
571
+ trap ( ERROR . INVALID_OPCODE )
572
+ }
573
+
570
574
trap ( ERROR . INVALID_BEGINSUB + ' at ' + describeLocation ( runState ) )
571
575
} ,
572
576
JUMPSUB : function ( runState : RunState ) {
573
577
const dest = runState . stack . pop ( )
578
+ if ( ! runState . _common . gteHardfork ( 'berlin' ) ) {
579
+ trap ( ERROR . INVALID_OPCODE )
580
+ }
581
+
574
582
if ( dest . gt ( runState . eei . getCodeSize ( ) ) ) {
575
583
trap ( ERROR . INVALID_JUMPSUB + ' at ' + describeLocation ( runState ) )
576
584
}
@@ -585,6 +593,10 @@ export const handlers: { [k: string]: OpHandler } = {
585
593
runState . programCounter = destNum + 1
586
594
} ,
587
595
RETURNSUB : function ( runState : RunState ) {
596
+ if ( ! runState . _common . gteHardfork ( 'berlin' ) ) {
597
+ trap ( ERROR . INVALID_OPCODE )
598
+ }
599
+
588
600
if ( runState . returnStack . length < 1 ) {
589
601
trap ( ERROR . INVALID_RETURNSUB )
590
602
}
@@ -852,7 +864,7 @@ export const handlers: { [k: string]: OpHandler } = {
852
864
}
853
865
854
866
function describeLocation ( runState : RunState ) {
855
- var hash = keccak256 ( runState . eei . getCode ( ) ) . toString ( 'hex' )
867
+ var hash = utils . keccak256 ( runState . eei . getCode ( ) ) . toString ( 'hex' )
856
868
var address = runState . eei . getAddress ( ) . toString ( 'hex' )
857
869
var pc = runState . programCounter - 1
858
870
return hash + '/' + address + ':' + pc
@@ -912,7 +924,7 @@ function getDataSlice(data: Buffer, offset: BN, length: BN): Buffer {
912
924
913
925
data = data . slice ( offset . toNumber ( ) , end . toNumber ( ) )
914
926
// Right-pad with zeros to fill dataLength bytes
915
- data = setLengthRight ( data , length . toNumber ( ) )
927
+ data = utils . setLengthRight ( data , length . toNumber ( ) )
916
928
917
929
return data
918
930
}
0 commit comments