|
|
|
@ -5,8 +5,10 @@ import { EventManager } from '../eventManager' |
|
|
|
|
import { rlp, keccak, bufferToHex } from 'ethereumjs-util' |
|
|
|
|
import { Web3VmProvider } from '../web3Provider/web3VmProvider' |
|
|
|
|
import { LogsManager } from './logsManager' |
|
|
|
|
const EthJSVM = require('ethereumjs-vm').default |
|
|
|
|
const StateManager = require('ethereumjs-vm/dist/state/stateManager').default |
|
|
|
|
import VM from '@ethereumjs/vm' |
|
|
|
|
import Common from '@ethereumjs/common' |
|
|
|
|
import StateManager from '@ethereumjs/vm/dist/state/stateManager' |
|
|
|
|
import { StorageDump } from '@ethereumjs/vm/dist/state/interface' |
|
|
|
|
|
|
|
|
|
declare let ethereum: any |
|
|
|
|
let web3 |
|
|
|
@ -23,21 +25,21 @@ if (typeof window !== 'undefined' && typeof window['ethereum'] !== 'undefined') |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
class StateManagerCommonStorageDump extends StateManager { |
|
|
|
|
constructor (arg) { |
|
|
|
|
super(arg) |
|
|
|
|
keyHashes |
|
|
|
|
constructor () { |
|
|
|
|
super() |
|
|
|
|
this.keyHashes = {} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
putContractStorage (address, key, value, cb) { |
|
|
|
|
this.keyHashes[keccak(key).toString('hex')] = bufferToHex(key) |
|
|
|
|
super.putContractStorage(address, key, value, cb) |
|
|
|
|
} |
|
|
|
|
S |
|
|
|
|
putContractStorage (address, key, value) { |
|
|
|
|
this.keyHashes[keccak(key).toString('hex')] = bufferToHex(key) |
|
|
|
|
return super.putContractStorage(address, key, value) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
dumpStorage (address, cb) { |
|
|
|
|
this._getStorageTrie(address, (err, trie) => { |
|
|
|
|
if (err) { |
|
|
|
|
return cb(err) |
|
|
|
|
} |
|
|
|
|
dumpStorage (address) { |
|
|
|
|
return new Promise<StorageDump>((resolve, reject) => { |
|
|
|
|
this._getStorageTrie(address).then((trie) => { |
|
|
|
|
const storage = {} |
|
|
|
|
const stream = trie.createReadStream() |
|
|
|
|
stream.on('data', (val) => { |
|
|
|
@ -48,28 +50,52 @@ class StateManagerCommonStorageDump extends StateManager { |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
stream.on('end', function () { |
|
|
|
|
cb(storage) |
|
|
|
|
resolve(storage) |
|
|
|
|
}) |
|
|
|
|
}).catch((error) => { |
|
|
|
|
reject(error) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async getStateRoot (force: boolean = false): Promise<Buffer> { |
|
|
|
|
if (!force && this._checkpointCount !== 0) { |
|
|
|
|
// throw new Error('Cannot get state root with uncommitted checkpoints')
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getStateRoot (cb) { |
|
|
|
|
const checkpoint = this._checkpointCount |
|
|
|
|
this._checkpointCount = 0 |
|
|
|
|
super.getStateRoot((err, stateRoot) => { |
|
|
|
|
this._checkpointCount = checkpoint |
|
|
|
|
cb(err, stateRoot) |
|
|
|
|
}) |
|
|
|
|
try { |
|
|
|
|
await this._cache.flush() |
|
|
|
|
} catch (e) { |
|
|
|
|
console.error(e) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setStateRoot (stateRoot, cb) { |
|
|
|
|
const checkpoint = this._checkpointCount |
|
|
|
|
this._checkpointCount = 0 |
|
|
|
|
super.setStateRoot(stateRoot, (err) => { |
|
|
|
|
this._checkpointCount = checkpoint |
|
|
|
|
cb(err) |
|
|
|
|
}) |
|
|
|
|
const stateRoot = this._trie.root |
|
|
|
|
return stateRoot |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async setStateRoot (stateRoot: Buffer): Promise<void> { |
|
|
|
|
if (this._checkpointCount !== 0) { |
|
|
|
|
// throw new Error('Cannot set state root with uncommitted checkpoints')
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
await this._cache.flush() |
|
|
|
|
|
|
|
|
|
if (stateRoot === this._trie.EMPTY_TRIE_ROOT) { |
|
|
|
|
this._trie.root = stateRoot |
|
|
|
|
this._cache.clear() |
|
|
|
|
this._storageTries = {} |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const hasRoot = await this._trie.checkRoot(stateRoot) |
|
|
|
|
if (!hasRoot) { |
|
|
|
|
throw new Error('State trie does not contain state root') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this._trie.root = stateRoot |
|
|
|
|
this._cache.clear() |
|
|
|
|
this._storageTries = {} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
@ -96,7 +122,7 @@ export class ExecutionContext { |
|
|
|
|
this.executionContext = null |
|
|
|
|
this.blockGasLimitDefault = 4300000 |
|
|
|
|
this.blockGasLimit = this.blockGasLimitDefault |
|
|
|
|
this.currentFork = 'muirGlacier' |
|
|
|
|
this.currentFork = 'berlin' |
|
|
|
|
this.vms = { |
|
|
|
|
/* |
|
|
|
|
byzantium: createVm('byzantium'), |
|
|
|
@ -104,7 +130,7 @@ export class ExecutionContext { |
|
|
|
|
petersburg: createVm('petersburg'), |
|
|
|
|
istanbul: createVm('istanbul'), |
|
|
|
|
*/ |
|
|
|
|
muirGlacier: this.createVm('muirGlacier') |
|
|
|
|
berlin: this.createVm('berlin') |
|
|
|
|
} |
|
|
|
|
this.mainNetGenesisHash = '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3' |
|
|
|
|
this.customNetWorks = {} |
|
|
|
@ -123,18 +149,18 @@ export class ExecutionContext { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
createVm (hardfork) { |
|
|
|
|
const stateManager = new StateManagerCommonStorageDump({}) |
|
|
|
|
stateManager.checkpoint(() => {}) |
|
|
|
|
const vm = new EthJSVM({ |
|
|
|
|
const stateManager = new StateManagerCommonStorageDump() |
|
|
|
|
stateManager.checkpoint() |
|
|
|
|
const common = new Common({ chain: 'mainnet', hardfork }) |
|
|
|
|
const vm = new VM({ |
|
|
|
|
common, |
|
|
|
|
activatePrecompiles: true, |
|
|
|
|
blockchain: stateManager.blockchain, |
|
|
|
|
stateManager: stateManager, |
|
|
|
|
hardfork: hardfork |
|
|
|
|
stateManager: stateManager |
|
|
|
|
}) |
|
|
|
|
vm.blockchain.validate = false |
|
|
|
|
|
|
|
|
|
const web3vm = new Web3VmProvider() |
|
|
|
|
web3vm.setVM(vm) |
|
|
|
|
return { vm, web3vm, stateManager } |
|
|
|
|
return { vm, web3vm, stateManager, common } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
askPermission () { |
|
|
|
@ -210,6 +236,10 @@ export class ExecutionContext { |
|
|
|
|
return this.vms[this.currentFork].vm |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
vmObject () { |
|
|
|
|
return this.vms[this.currentFork] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setContext (context, endPointUrl, confirmCb, infoCb) { |
|
|
|
|
this.executionContext = context |
|
|
|
|
this.executionContextChange(context, endPointUrl, confirmCb, infoCb, null) |
|
|
|
@ -221,8 +251,8 @@ export class ExecutionContext { |
|
|
|
|
if (!infoCb) infoCb = () => {} |
|
|
|
|
if (context === 'vm') { |
|
|
|
|
this.executionContext = context |
|
|
|
|
this.vms[this.currentFork].stateManager.revert(() => { |
|
|
|
|
this.vms[this.currentFork].stateManager.checkpoint(() => {}) |
|
|
|
|
this.vms[this.currentFork].stateManager.revert().then(() => { |
|
|
|
|
this.vms[this.currentFork].stateManager.checkpoint() |
|
|
|
|
}) |
|
|
|
|
this.event.trigger('contextChanged', ['vm']) |
|
|
|
|
return cb() |
|
|
|
|