Skip to content

Commit d3ed4d2

Browse files
committed
fix: use vm common for default new blocks
style: prefer nullish coalescing operator
1 parent 60ed11b commit d3ed4d2

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

packages/vm/lib/runCall.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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)