Skip to content

Commit 197561e

Browse files
EVM/VM refactor (ethereumjs#1892)
* VM -> EVM/VM Refactor: new EVMOpts EVM options dict, move common and supported HFs to EVM * VM -> EVM/VM Refactor: standalone DEBUG property in EVM and Interpreter * VM -> EVM/VM Refactor: standalone EVM stateManager option and property * VM -> EVM/VM Refactor: moved runCall()/runCode() to EVM, deleted dedicated files, top-level EVM instantiation in VM * VM -> EVM/VM Refactor: made EVM AsyncEventEmitter, moved beforeMessage, afterMessage, newContract, step events to EVM * VM -> EVM/VM Refactor: moved allowUnlimitedContractSize option to EVM * VM -> EVM/VM Refactor: moved opcode and gas handler functionality and options to EVM, removed EVM/Precompiles VM dependency * Client: fix cliqueActiveSigners method assignments * VM: refactor TxContext to an interface * VM: improve Message class types and defaults handling * VM: refactor runCall and executeMessage into unified runCall * VM: fix gas refund reset to 0 after tx finishes and fix parenthesis in runBlock receipt ternary * VM: reimplement transient storage clear method * VM: use VM async create method instead of constructor * VM: unify interpreter and evm DEBUG property Co-authored-by: Holger Drewes <[email protected]>
1 parent 984a030 commit 197561e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+870
-738
lines changed

packages/client/lib/rpc/modules/eth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ export class Eth {
483483
value: toType(value, TypeOutput.BigInt),
484484
data: data ? toBuffer(data) : undefined,
485485
}
486-
const { execResult } = await vm.runCall(runCallOpts)
486+
const { execResult } = await vm.evm.runCall(runCallOpts)
487487
return bufferToHex(execResult.returnValue)
488488
} catch (error: any) {
489489
throw {

packages/client/test/integration/merge.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import tape from 'tape'
2-
import Blockchain from '@ethereumjs/blockchain'
2+
import Blockchain, { CliqueConsensus } from '@ethereumjs/blockchain'
33
import Common, {
44
Chain as ChainCommon,
55
ConsensusType,
@@ -78,7 +78,7 @@ tape('[Integration:Merge]', async (t) => {
7878
validateBlocks: false,
7979
validateConsensus: false,
8080
})
81-
blockchain.cliqueActiveSigners = () => [accounts[0][0]] // stub
81+
;(blockchain.consensus as CliqueConsensus).cliqueActiveSigners = () => [accounts[0][0]] // stub
8282
const serviceConfig = new Config({
8383
common,
8484
servers: [server as any],
@@ -105,7 +105,9 @@ tape('[Integration:Merge]', async (t) => {
105105
height: 0,
106106
common: commonPoA,
107107
})
108-
remoteService.chain.blockchain.cliqueActiveSigners = () => [accounts[0][0]] // stub
108+
;(remoteService.chain.blockchain.consensus as CliqueConsensus).cliqueActiveSigners = () => [
109+
accounts[0][0],
110+
] // stub
109111
await server.discover('remotePeer1', '127.0.0.2')
110112
const targetTTD = BigInt(5)
111113
remoteService.config.events.on(Event.SYNC_SYNCHRONIZED, async () => {

packages/client/test/integration/miner.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import tape from 'tape'
2-
import Blockchain from '@ethereumjs/blockchain'
2+
import Blockchain, { CliqueConsensus } from '@ethereumjs/blockchain'
33
import Common, {
44
Chain as ChainCommon,
55
ConsensusType,
@@ -51,7 +51,7 @@ tape('[Integration:Miner]', async (t) => {
5151
validateBlocks: false,
5252
validateConsensus: false,
5353
})
54-
blockchain.cliqueActiveSigners = () => [accounts[0][0]] // stub
54+
;(blockchain.consensus as CliqueConsensus).cliqueActiveSigners = () => [accounts[0][0]] // stub
5555
const chain = new Chain({ config, blockchain })
5656
const serviceConfig = new Config({
5757
common,
@@ -82,7 +82,9 @@ tape('[Integration:Miner]', async (t) => {
8282
height: 0,
8383
common,
8484
})
85-
remoteService.chain.blockchain.cliqueActiveSigners = () => [accounts[0][0]] // stub
85+
;(remoteService.chain.blockchain.consensus as CliqueConsensus).cliqueActiveSigners = () => [
86+
accounts[0][0],
87+
] // stub
8688
;(remoteService as FullEthereumService).execution.run = async () => 1 // stub
8789
await server.discover('remotePeer1', '127.0.0.2')
8890
const targetHeight = BigInt(5)

packages/client/test/miner/miner.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ tape('[Miner]', async (t) => {
163163
await txPool.add(txA01)
164164

165165
// disable consensus to skip PoA block signer validation
166-
;(vm.blockchain as any)._validateConsensus = false
166+
;(vm.blockchain.consensus as CliqueConsensus).cliqueActiveSigners = () => [A.address] // stub
167167

168168
chain.putBlocks = (blocks: Block[]) => {
169169
t.equal(blocks[0].transactions.length, 1, 'new block should include tx')

packages/vm/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ const PUSH1 = '60'
3030
// Note that numbers added are hex values, so '20' would be '32' as decimal e.g.
3131
const code = [PUSH1, '03', PUSH1, '05', ADD, STOP]
3232

33-
vm.on('step', function (data) {
33+
vm.evm.on('step', function (data) {
3434
// Note that data.stack is not immutable, i.e. it is a reference to the vm's internal stack object
3535
console.log(`Opcode: ${data.opcode.name}\tStack: ${data.stack}`)
3636
})
3737

38-
vm.runCode({
39-
code: Buffer.from(code.join(''), 'hex'),
40-
gasLimit: new BN(0xffff),
41-
})
38+
vm.evm
39+
.runCode({
40+
code: Buffer.from(code.join(''), 'hex'),
41+
gasLimit: new BN(0xffff),
42+
})
4243
.then((results) => {
4344
console.log(`Returned: ${results.returnValue.toString('hex')}`)
4445
console.log(`gasUsed : ${results.gasUsed.toString()}`)

packages/vm/examples/run-code-browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const run = async () => {
2222
// Note that numbers added are hex values, so '20' would be '32' as decimal e.g.
2323
const code = [PUSH1, '03', PUSH1, '05', ADD, STOP]
2424

25-
vm.on('step', function (data) {
25+
vm.evm.on('step', function (data) {
2626
console.log(`Opcode: ${data.opcode.name}\tStack: ${data.stack}`)
2727
})
2828

packages/vm/examples/run-solidity-contract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ async function setGreeting(
135135
async function getGreeting(vm: VM, contractAddress: Address, caller: Address) {
136136
const sigHash = new Interface(['function greet()']).getSighash('greet')
137137

138-
const greetResult = await vm.runCall({
138+
const greetResult = await vm.evm.runCall({
139139
to: contractAddress,
140140
caller: caller,
141141
origin: caller, // The tx.origin is also the caller here

packages/vm/src/evm/eei.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export default class EEI {
9898
*/
9999
useGas(amount: bigint, context?: string): void {
100100
this._gasLeft -= amount
101-
if (this._evm._vm.DEBUG) {
101+
if (this._evm.DEBUG) {
102102
debugGas(`${context ? context + ': ' : ''}used ${amount} gas (-> ${this._gasLeft})`)
103103
}
104104
if (this._gasLeft < BigInt(0)) {
@@ -113,7 +113,7 @@ export default class EEI {
113113
* @param context - Usage context for debugging
114114
*/
115115
refundGas(amount: bigint, context?: string): void {
116-
if (this._evm._vm.DEBUG) {
116+
if (this._evm.DEBUG) {
117117
debugGas(`${context ? context + ': ' : ''}refund ${amount} gas (-> ${this._evm._refund})`)
118118
}
119119
this._evm._refund += amount
@@ -125,7 +125,7 @@ export default class EEI {
125125
* @param context - Usage context for debugging
126126
*/
127127
subRefund(amount: bigint, context?: string): void {
128-
if (this._evm._vm.DEBUG) {
128+
if (this._evm.DEBUG) {
129129
debugGas(`${context ? context + ': ' : ''}sub gas refund ${amount} (-> ${this._evm._refund})`)
130130
}
131131
this._evm._refund -= amount
@@ -140,7 +140,7 @@ export default class EEI {
140140
* @param amount - Amount to add
141141
*/
142142
addStipend(amount: bigint): void {
143-
if (this._evm._vm.DEBUG) {
143+
if (this._evm.DEBUG) {
144144
debugGas(`add stipend ${amount} (-> ${this._gasLeft})`)
145145
}
146146
this._gasLeft += amount
@@ -587,7 +587,7 @@ export default class EEI {
587587
return BigInt(0)
588588
}
589589

590-
const results = await this._evm.executeMessage(msg)
590+
const results = await this._evm.runCall({ message: msg })
591591

592592
if (results.execResult.logs) {
593593
this._result.logs = this._result.logs.concat(results.execResult.logs)
@@ -618,30 +618,18 @@ export default class EEI {
618618
/**
619619
* Creates a new contract with a given value.
620620
*/
621-
async create(
622-
gasLimit: bigint,
623-
value: bigint,
624-
data: Buffer,
625-
salt: Buffer | null = null
626-
): Promise<bigint> {
621+
async create(gasLimit: bigint, value: bigint, data: Buffer, salt?: Buffer): Promise<bigint> {
627622
const selfdestruct = { ...this._result.selfdestruct }
628-
const msg = new Message({
629-
caller: this._env.address,
630-
gasLimit,
631-
value,
632-
data,
633-
salt,
634-
depth: this._env.depth + 1,
635-
selfdestruct,
636-
})
623+
const caller = this._env.address
624+
const depth = this._env.depth + 1
637625

638626
// empty the return data buffer
639627
this._lastReturned = Buffer.alloc(0)
640628

641629
// Check if account has enough ether and max depth not exceeded
642630
if (
643631
this._env.depth >= Number(this._common.param('vm', 'stackLimit')) ||
644-
(msg.delegatecall !== true && this._env.contract.balance < msg.value)
632+
this._env.contract.balance < value
645633
) {
646634
return BigInt(0)
647635
}
@@ -655,12 +643,22 @@ export default class EEI {
655643
await this._state.putAccount(this._env.address, this._env.contract)
656644

657645
if (this._common.isActivatedEIP(3860)) {
658-
if (msg.data.length > Number(this._common.param('vm', 'maxInitCodeSize'))) {
646+
if (data.length > Number(this._common.param('vm', 'maxInitCodeSize'))) {
659647
return BigInt(0)
660648
}
661649
}
662650

663-
const results = await this._evm.executeMessage(msg)
651+
const message = new Message({
652+
caller,
653+
gasLimit,
654+
value,
655+
data,
656+
salt,
657+
depth,
658+
selfdestruct,
659+
})
660+
661+
const results = await this._evm.runCall({ message })
664662

665663
if (results.execResult.logs) {
666664
this._result.logs = this._result.logs.concat(results.execResult.logs)

0 commit comments

Comments
 (0)