1
1
import BN = require( 'bn.js' )
2
+ import { toBuffer } from 'ethereumjs-util'
3
+ import Account from 'ethereumjs-account'
2
4
import PStateManager from '../state/promisified'
5
+ import { VmError , ERROR } from '../exceptions'
3
6
import Message from './message'
4
- import { toBuffer } from 'ethereumjs-util'
7
+ import { RunState , RunResult } from './loop'
8
+ import Interpreter from './interpreter'
5
9
const promisify = require ( 'util.promisify' )
6
- const { VmError, ERROR } = require ( '../exceptions' )
10
+
11
+ export interface Env {
12
+ blockchain : any // TODO: update to ethereumjs-blockchain v4.0.0
13
+ address : Buffer
14
+ caller : Buffer
15
+ callData : Buffer
16
+ callValue : BN
17
+ code : Buffer
18
+ isStatic : boolean
19
+ depth : number
20
+ gasPrice : Buffer // TODO: Set type to BN?
21
+ origin : Buffer
22
+ block : any
23
+ contract : Account
24
+ }
7
25
8
26
export default class EEI {
9
- _env : any
10
- _runState : any
11
- _result : any
27
+ _env : Env
28
+ _runState : RunState
29
+ _result : RunResult
12
30
_state : PStateManager
13
- _interpreter : any
31
+ _interpreter : Interpreter
14
32
_lastReturned : Buffer
15
33
16
- constructor ( env : any , runState : any , result : any , state : PStateManager , interpreter : any ) {
34
+ constructor ( env : Env , runState : RunState , result : RunResult , state : PStateManager , interpreter : Interpreter ) {
17
35
this . _env = env
18
36
this . _runState = runState
19
37
this . _result = result
@@ -36,7 +54,7 @@ export default class EEI {
36
54
}
37
55
38
56
refundGas ( amount : BN ) : void {
39
- this . _result . gasRefund . iaddn ( amount )
57
+ this . _result . gasRefund . iadd ( amount )
40
58
}
41
59
42
60
/**
@@ -115,9 +133,9 @@ export default class EEI {
115
133
116
134
/**
117
135
* Returns the code running in current environment.
118
- * @returns {BN }
136
+ * @returns {Buffer }
119
137
*/
120
- getCode ( ) : BN {
138
+ getCode ( ) : Buffer {
121
139
return this . _env . code
122
140
}
123
141
@@ -314,9 +332,9 @@ export default class EEI {
314
332
* Creates a new log in the current environment.
315
333
* @param {Buffer } data
316
334
* @param {Number } numberOfTopics
317
- * @param {BN [] } topics
335
+ * @param {Buffer [] } topics
318
336
*/
319
- log ( data : Buffer , numberOfTopics : number , topics : BN [ ] ) : void {
337
+ log ( data : Buffer , numberOfTopics : number , topics : Buffer [ ] ) : void {
320
338
if ( numberOfTopics < 0 || numberOfTopics > 4 ) {
321
339
trap ( ERROR . OUT_OF_RANGE )
322
340
}
@@ -326,7 +344,7 @@ export default class EEI {
326
344
}
327
345
328
346
// add address
329
- const log = [ this . _env . address ]
347
+ const log : any = [ this . _env . address ]
330
348
log . push ( topics )
331
349
332
350
// add data
@@ -451,7 +469,7 @@ export default class EEI {
451
469
this . useGas ( results . gasUsed )
452
470
453
471
// Set return value
454
- if ( results . vm . return && ( ! results . vm . exceptionError || results . vm . exceptionError . error === ERROR . REVERT ) ) {
472
+ if ( results . vm . return && ( ! results . vm . exceptionError || ( results . vm . exceptionError as VmError ) . error === ERROR . REVERT ) ) {
455
473
this . _lastReturned = results . vm . return
456
474
}
457
475
@@ -491,7 +509,7 @@ export default class EEI {
491
509
return new BN ( 0 )
492
510
}
493
511
494
- this . _env . contract . nonce = new BN ( this . _env . contract . nonce ) . addn ( 1 )
512
+ this . _env . contract . nonce = toBuffer ( new BN ( this . _env . contract . nonce ) . addn ( 1 ) )
495
513
await this . _state . putAccount ( this . _env . address , this . _env . contract )
496
514
497
515
const results = await this . _interpreter . executeMessage ( msg )
@@ -509,7 +527,7 @@ export default class EEI {
509
527
this . useGas ( results . gasUsed )
510
528
511
529
// Set return buffer in case revert happened
512
- if ( results . vm . exceptionError && results . vm . exceptionError . error === ERROR . REVERT ) {
530
+ if ( results . vm . exceptionError && ( results . vm . exceptionError as VmError ) . error === ERROR . REVERT ) {
513
531
this . _lastReturned = results . vm . return
514
532
}
515
533
@@ -522,11 +540,6 @@ export default class EEI {
522
540
// push the created address to the stack
523
541
return new BN ( results . createdAddress )
524
542
}
525
- } else {
526
- // creation failed so don't increment the nonce
527
- if ( results . vm . createdAddress ) {
528
- this . _env . contract . nonce = new BN ( this . _env . contract . nonce ) . subn ( 1 )
529
- }
530
543
}
531
544
532
545
return new BN ( results . vm . exception )
@@ -554,7 +567,7 @@ export default class EEI {
554
567
}
555
568
}
556
569
557
- function trap ( err : string ) {
570
+ function trap ( err : ERROR ) {
558
571
throw new VmError ( err )
559
572
}
560
573
0 commit comments