diff --git a/lib/evm/evm.ts b/lib/evm/evm.ts index a917dbd355..80a1a6490c 100644 --- a/lib/evm/evm.ts +++ b/lib/evm/evm.ts @@ -99,7 +99,7 @@ export default class EVM { constructor(vm: any, txContext: TxContext, block: any) { this._vm = vm - this._state = new PStateManager(this._vm.stateManager) + this._state = this._vm.pStateManager this._tx = txContext this._block = block } diff --git a/lib/evm/interpreter.ts b/lib/evm/interpreter.ts index e04cc7e567..a05b7aeca0 100644 --- a/lib/evm/interpreter.ts +++ b/lib/evm/interpreter.ts @@ -57,7 +57,7 @@ export default class Interpreter { constructor(vm: any, eei: EEI) { this._vm = vm // TODO: remove when not needed - this._state = new PStateManager(vm.stateManager) + this._state = vm.pStateManager this._eei = eei this._runState = { programCounter: 0, diff --git a/lib/index.ts b/lib/index.ts index 7d5942cf4b..c7fbc9932b 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -10,6 +10,7 @@ import { default as runBlock, RunBlockOpts, RunBlockResult } from './runBlock' import { EVMResult, ExecResult } from './evm/evm' import { OpcodeList, getOpcodesForHF } from './evm/opcodes' import runBlockchain from './runBlockchain' +import PStateManager from './state/promisified' const promisify = require('util.promisify') const AsyncEventEmitter = require('async-eventemitter') const Trie = require('merkle-patricia-tree/secure.js') @@ -72,6 +73,8 @@ export default class VM extends AsyncEventEmitter { blockchain: Blockchain allowUnlimitedContractSize: boolean _opcodes: OpcodeList + public readonly _emit: (topic: string, data: any) => Promise + public readonly pStateManager: PStateManager /** * Instantiates a new [[VM]] Object. @@ -117,10 +120,14 @@ export default class VM extends AsyncEventEmitter { this.stateManager = new StateManager({ trie, common: this._common }) } + this.pStateManager = new PStateManager(this.stateManager) + this.blockchain = opts.blockchain || new Blockchain({ common: this._common }) this.allowUnlimitedContractSize = opts.allowUnlimitedContractSize === undefined ? false : opts.allowUnlimitedContractSize + + this._emit = promisify(this.emit.bind(this)) } /** @@ -188,8 +195,4 @@ export default class VM extends AsyncEventEmitter { common: this._common, }) } - - async _emit(topic: string, data: any) { - return promisify(this.emit.bind(this))(topic, data) - } } diff --git a/lib/runBlock.ts b/lib/runBlock.ts index 491285bfa4..40555916ec 100644 --- a/lib/runBlock.ts +++ b/lib/runBlock.ts @@ -78,7 +78,7 @@ export default async function runBlock(this: VM, opts: RunBlockOpts): Promise { - const state = new PStateManager(this.stateManager) + const state = this.pStateManager const minerReward = new BN(this._common.param('pow', 'minerReward')) const ommers = block.uncleHeaders // Reward ommers diff --git a/lib/runTx.ts b/lib/runTx.ts index 6e7b351abc..cdf432983e 100644 --- a/lib/runTx.ts +++ b/lib/runTx.ts @@ -72,7 +72,7 @@ export default async function runTx(this: VM, opts: RunTxOpts): Promise { const block = opts.block const tx = opts.tx - const state = new PStateManager(this.stateManager) + const state = this.pStateManager /** * The `beforeTx` event diff --git a/lib/state/promisified.ts b/lib/state/promisified.ts index 1234cba92c..fd7a2c91c7 100644 --- a/lib/state/promisified.ts +++ b/lib/state/promisified.ts @@ -9,87 +9,75 @@ import { default as StateManager, StorageDump } from './stateManager' export default class PStateManager { _wrapped: StateManager + public readonly getAccount: (addr: Buffer) => Promise + public readonly putAccount: (addr: Buffer, account: Account) => Promise + public readonly putContractCode: (addr: Buffer, code: Buffer) => Promise + public readonly getContractCode: (addr: Buffer) => Promise + public readonly getContractStorage: (addr: Buffer, key: Buffer) => Promise + public readonly getOriginalContractStorage: (addr: Buffer, key: Buffer) => Promise + public readonly putContractStorage: (addr: Buffer, key: Buffer, value: Buffer) => Promise + public readonly clearContractStorage: (addr: Buffer) => Promise + public readonly checkpoint: () => Promise + public readonly commit: () => Promise + public readonly revert: () => Promise + public readonly getStateRoot: () => Promise + public readonly setStateRoot: (root: Buffer) => Promise + public readonly dumpStorage: (address: Buffer) => Promise + public readonly hasGenesisState: () => Promise + public readonly generateCanonicalGenesis: () => Promise + public readonly generateGenesis: (initState: any) => Promise + public readonly accountIsEmpty: (address: Buffer) => Promise + public readonly cleanupTouchedAccounts: () => Promise + constructor(wrapped: StateManager) { this._wrapped = wrapped - } - copy(): PStateManager { - return new PStateManager(this._wrapped.copy()) - } + this.getAccount = promisify(this._wrapped.getAccount.bind(this._wrapped)) - getAccount(addr: Buffer): Promise { - return promisify(this._wrapped.getAccount.bind(this._wrapped))(addr) - } + this.putAccount = promisify(this._wrapped.putAccount.bind(this._wrapped)) - putAccount(addr: Buffer, account: Account): Promise { - return promisify(this._wrapped.putAccount.bind(this._wrapped))(addr, account) - } + this.putContractCode = promisify(this._wrapped.putContractCode.bind(this._wrapped)) - putContractCode(addr: Buffer, code: Buffer): Promise { - return promisify(this._wrapped.putContractCode.bind(this._wrapped))(addr, code) - } + this.getContractCode = promisify(this._wrapped.getContractCode.bind(this._wrapped)) - getContractCode(addr: Buffer): Promise { - return promisify(this._wrapped.getContractCode.bind(this._wrapped))(addr) - } + this.getContractStorage = promisify(this._wrapped.getContractStorage.bind(this._wrapped)) - getContractStorage(addr: Buffer, key: Buffer): Promise { - return promisify(this._wrapped.getContractStorage.bind(this._wrapped))(addr, key) - } + this.getOriginalContractStorage = promisify( + this._wrapped.getOriginalContractStorage.bind(this._wrapped), + ) - getOriginalContractStorage(addr: Buffer, key: Buffer): Promise { - return promisify(this._wrapped.getOriginalContractStorage.bind(this._wrapped))(addr, key) - } + this.putContractStorage = promisify(this._wrapped.putContractStorage.bind(this._wrapped)) - putContractStorage(addr: Buffer, key: Buffer, value: Buffer): Promise { - return promisify(this._wrapped.putContractStorage.bind(this._wrapped))(addr, key, value) - } + this.clearContractStorage = promisify(this._wrapped.clearContractStorage.bind(this._wrapped)) - clearContractStorage(addr: Buffer): Promise { - return promisify(this._wrapped.clearContractStorage.bind(this._wrapped))(addr) - } + this.checkpoint = promisify(this._wrapped.checkpoint.bind(this._wrapped)) - checkpoint(): Promise { - return promisify(this._wrapped.checkpoint.bind(this._wrapped))() - } + this.commit = promisify(this._wrapped.commit.bind(this._wrapped)) - commit(): Promise { - return promisify(this._wrapped.commit.bind(this._wrapped))() - } + this.revert = promisify(this._wrapped.revert.bind(this._wrapped)) - revert(): Promise { - return promisify(this._wrapped.revert.bind(this._wrapped))() - } + this.getStateRoot = promisify(this._wrapped.getStateRoot.bind(this._wrapped)) - getStateRoot(): Promise { - return promisify(this._wrapped.getStateRoot.bind(this._wrapped))() - } + this.setStateRoot = promisify(this._wrapped.setStateRoot.bind(this._wrapped)) - setStateRoot(root: Buffer): Promise { - return promisify(this._wrapped.setStateRoot.bind(this._wrapped))(root) - } + this.dumpStorage = promisify(this._wrapped.dumpStorage.bind(this._wrapped)) - dumpStorage(address: Buffer): Promise { - return promisify(this._wrapped.dumpStorage.bind(this._wrapped))(address) - } + this.hasGenesisState = promisify(this._wrapped.hasGenesisState.bind(this._wrapped)) - hasGenesisState(): Promise { - return promisify(this._wrapped.hasGenesisState.bind(this._wrapped))() - } + this.generateCanonicalGenesis = promisify( + this._wrapped.generateCanonicalGenesis.bind(this._wrapped), + ) - generateCanonicalGenesis(): Promise { - return promisify(this._wrapped.generateCanonicalGenesis.bind(this._wrapped))() - } + this.generateGenesis = promisify(this._wrapped.generateGenesis.bind(this._wrapped)) - generateGenesis(initState: any): Promise { - return promisify(this._wrapped.generateGenesis.bind(this._wrapped))(initState) - } + this.accountIsEmpty = promisify(this._wrapped.accountIsEmpty.bind(this._wrapped)) - accountIsEmpty(address: Buffer): Promise { - return promisify(this._wrapped.accountIsEmpty.bind(this._wrapped))(address) + this.cleanupTouchedAccounts = promisify( + this._wrapped.cleanupTouchedAccounts.bind(this._wrapped), + ) } - cleanupTouchedAccounts(): Promise { - return promisify(this._wrapped.cleanupTouchedAccounts.bind(this._wrapped))() + copy(): PStateManager { + return new PStateManager(this._wrapped.copy()) } } diff --git a/tests/api/runBlock.js b/tests/api/runBlock.js index e2b72a2c65..7a375a0ab4 100644 --- a/tests/api/runBlock.js +++ b/tests/api/runBlock.js @@ -4,6 +4,7 @@ const Block = require('ethereumjs-block') const Common = require('ethereumjs-common').default const util = require('ethereumjs-util') const runBlock = require('../../dist/runBlock').default +const PStateManager = require('../../dist/state/promisified').default const { StateManager } = require('../../dist/state') const testData = require('./testdata.json') const { setupVM } = require('./utils') @@ -17,6 +18,7 @@ function setup (vm = null) { const stateManager = new StateManager() vm = { stateManager, + pStateManager: new PStateManager(stateManager), emit: (e, val, cb) => cb(), _emit: (e, val) => new Promise((resolve, reject) => resolve()), runTx: (opts) => new Promise((resolve, reject) => reject(new Error('test'))), diff --git a/tests/api/runBlockchain.js b/tests/api/runBlockchain.js index 8d5e79f81c..fcbe85d275 100644 --- a/tests/api/runBlockchain.js +++ b/tests/api/runBlockchain.js @@ -6,6 +6,7 @@ const Block = require('ethereumjs-block') const Common = require('ethereumjs-common').default const util = require('ethereumjs-util') const runBlockchain = require('../../dist/runBlockchain').default +const PStateManager = require('../../dist/state/promisified').default const { StateManager } = require('../../dist/state') const { createGenesis } = require('./utils') @@ -16,8 +17,10 @@ tape('runBlockchain', (t) => { chain: 'goerli', validate: false }) + const stateManager = new StateManager({ common: new Common('goerli') }); const vm = { - stateManager: new StateManager({ common: new Common('goerli') }), + stateManager, + pStateManager: new PStateManager(stateManager), blockchain: blockchain } diff --git a/tests/api/runTx.js b/tests/api/runTx.js index 498daebc0a..e59e1e1a34 100644 --- a/tests/api/runTx.js +++ b/tests/api/runTx.js @@ -3,14 +3,17 @@ const tape = require('tape') const Transaction = require('ethereumjs-tx').Transaction const ethUtil = require('ethereumjs-util') const runTx = require('../../dist/runTx').default +const PStateManager = require('../../dist/state/promisified').default const { StateManager } = require('../../dist/state') const VM = require('../../dist/index').default const { createAccount } = require('./utils') function setup (vm = null) { if (vm === null) { + const stateManager = new StateManager({ }) vm = { - stateManager: new StateManager({ }), + stateManager, + pStateManager: new PStateManager(stateManager), emit: (e, val, cb) => { cb() }, _emit: (e, val) => new Promise((resolve, reject) => resolve()) }