Skip to content

Commit c0e9b8a

Browse files
authored
Merge pull request #1011 from ethereumjs/fix-vm-default-block-common
Use vm's common when creating default blocks in `runCall` and `runCode`
2 parents 60ed11b + 6727262 commit c0e9b8a

File tree

3 files changed

+37
-45
lines changed

3 files changed

+37
-45
lines changed

packages/vm/lib/runCall.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface RunCallOpts {
1818
value?: BN
1919
data?: Buffer
2020
/**
21-
* This is for CALLCODE where the code to load is different than the code from the to account
21+
* This is for CALLCODE where the code to load is different than the code from the `opts.to` address.
2222
*/
2323
code?: Buffer
2424
depth?: number
@@ -33,27 +33,29 @@ export interface RunCallOpts {
3333
* @ignore
3434
*/
3535
export default function runCall(this: VM, opts: RunCallOpts): Promise<EVMResult> {
36-
const block = opts.block || new Block()
36+
const block = opts.block ?? Block.fromBlockData({}, { common: this._common })
3737

3838
const txContext = new TxContext(
39-
opts.gasPrice || new BN(0),
40-
opts.origin || opts.caller || Address.zero()
39+
opts.gasPrice ?? new BN(0),
40+
opts.origin ?? opts.caller ?? Address.zero()
4141
)
42+
4243
const message = new Message({
4344
caller: opts.caller,
44-
gasLimit: opts.gasLimit ? opts.gasLimit : new BN(0xffffff),
45-
to: opts.to ? opts.to : undefined,
45+
gasLimit: opts.gasLimit ?? new BN(0xffffff),
46+
to: opts.to ?? undefined,
4647
value: opts.value,
4748
data: opts.data,
4849
code: opts.code,
49-
depth: opts.depth || 0,
50-
isCompiled: opts.compiled || false,
51-
isStatic: opts.static || false,
52-
salt: opts.salt || null,
53-
selfdestruct: opts.selfdestruct || {},
54-
delegatecall: opts.delegatecall || false,
50+
depth: opts.depth ?? 0,
51+
isCompiled: opts.compiled ?? false,
52+
isStatic: opts.static ?? false,
53+
salt: opts.salt ?? null,
54+
selfdestruct: opts.selfdestruct ?? {},
55+
delegatecall: opts.delegatecall ?? false,
5556
})
5657

5758
const evm = new EVM(this, txContext, block)
59+
5860
return evm.executeMessage(message)
5961
}

packages/vm/lib/runCode.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ This is the core of the Ethereum Virtual Machine (EVM or just VM).
44
55
NOTES:
66
7-
stack items are lazily duplicated.
8-
So you must never directly change a buffer from the stack,
9-
instead you should `copy` it first
7+
1. Stack items are lazily duplicated, so you must never directly change a buffer
8+
from the stack, instead you should `copy` it first.
9+
10+
2. Not all stack items are 32 bytes, so if the operation relies on the stack
11+
item length then you must use `utils.pad(<item>, 32)` first.
1012
11-
not all stack items are 32 bytes, so if the operation relies on the stack
12-
item length then you must use utils.pad(<item>, 32) first.
1313
*/
1414
import { Address, BN } from 'ethereumjs-util'
1515
import { Block } from '@ethereumjs/block'
@@ -23,7 +23,7 @@ import { default as EVM, ExecResult } from './evm/evm'
2323
*/
2424
export interface RunCodeOpts {
2525
/**
26-
* The [`Block`](https://github.com/ethereumjs/ethereumjs-block) the `tx` belongs to. If omitted a blank block will be used
26+
* The `@ethereumjs/block` the `tx` belongs to. If omitted a default blank block will be used.
2727
*/
2828
block?: Block
2929
evm?: EVM
@@ -71,35 +71,28 @@ export interface RunCodeOpts {
7171
* @ignore
7272
*/
7373
export default function runCode(this: VM, opts: RunCodeOpts): Promise<ExecResult> {
74-
if (!opts.block) {
75-
opts.block = new Block()
76-
}
74+
const block = opts.block ?? Block.fromBlockData({}, { common: this._common })
7775

7876
// Backwards compatibility
79-
if (!opts.txContext) {
80-
opts.txContext = new TxContext(
81-
opts.gasPrice || new BN(0),
82-
opts.origin || opts.caller || Address.zero()
83-
)
84-
}
85-
if (!opts.message) {
86-
opts.message = new Message({
77+
const txContext =
78+
opts.txContext ??
79+
new TxContext(opts.gasPrice ?? new BN(0), opts.origin ?? opts.caller ?? Address.zero())
80+
81+
const message =
82+
opts.message ??
83+
new Message({
8784
code: opts.code,
8885
data: opts.data,
8986
gasLimit: opts.gasLimit,
90-
to: opts.address || Address.zero(),
87+
to: opts.address ?? Address.zero(),
9188
caller: opts.caller,
9289
value: opts.value,
93-
depth: opts.depth || 0,
94-
selfdestruct: opts.selfdestruct || {},
95-
isStatic: opts.isStatic || false,
90+
depth: opts.depth ?? 0,
91+
selfdestruct: opts.selfdestruct ?? {},
92+
isStatic: opts.isStatic ?? false,
9693
})
97-
}
9894

99-
let evm = opts.evm
100-
if (!evm) {
101-
evm = new EVM(this, opts.txContext, opts.block)
102-
}
95+
const evm = opts.evm ?? new EVM(this, txContext, block)
10396

104-
return evm.runInterpreter(opts.message, { pc: opts.pc })
97+
return evm.runInterpreter(message, { pc: opts.pc })
10598
}

packages/vm/lib/runTx.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import TxContext from './evm/txContext'
1212
*/
1313
export interface RunTxOpts {
1414
/**
15-
* The block to which the `tx` belongs
15+
* The `@ethereumjs/block` the `tx` belongs to. If omitted a default blank block will be used.
1616
*/
1717
block?: Block
1818
/**
19-
* An [`@ethereumjs/tx`](https://github.com/ethereumjs/ethereumjs-vm/tree/master/packages/tx) to run
19+
* An `@ethereumjs/tx` to run
2020
*/
2121
tx: Transaction
2222
/**
@@ -57,10 +57,7 @@ export default async function runTx(this: VM, opts: RunTxOpts): Promise<RunTxRes
5757
}
5858

5959
// create a reasonable default if no block is given
60-
if (!opts.block) {
61-
const common = opts.tx.common
62-
opts.block = Block.fromBlockData({}, { common })
63-
}
60+
opts.block = opts.block ?? Block.fromBlockData({}, { common: opts.tx.common })
6461

6562
if (opts.block.header.gasLimit.lt(opts.tx.gasLimit)) {
6663
throw new Error('tx has a higher gas limit than the block')

0 commit comments

Comments
 (0)