diff --git a/libs/remix-lib/src/execution/txRunnerVM.ts b/libs/remix-lib/src/execution/txRunnerVM.ts index ed3562f30e..6a363a3a22 100644 --- a/libs/remix-lib/src/execution/txRunnerVM.ts +++ b/libs/remix-lib/src/execution/txRunnerVM.ts @@ -28,6 +28,7 @@ export class TxRunnerVM { blocks logsManager commonContext + blockParentHash nextNonceForCall: number getVMObject: () => any @@ -37,9 +38,8 @@ export class TxRunnerVM { // has a default for now for backwards compatability this.getVMObject = getVMObject this.commonContext = this.getVMObject().common - this.blockNumber = 0 this.runAsync = true - this.blockNumber = 0 // The VM is running in Homestead mode, which started at this block. + this.blockNumber = 0 this.runAsync = false // We have to run like this cause the VM Event Manager does not support running multiple txs at the same time. this.pendingTxs = {} this.vmaccounts = vmaccounts @@ -52,6 +52,9 @@ export class TxRunnerVM { For this to function we also need to skip nonce validation, in the vm: `{ skipNonce: true }` */ this.nextNonceForCall = 0 + + const vm = this.getVMObject().vm + this.blockParentHash = vm.blockchain.genesisBlock.hash() } execute (args: InternalTransaction, confirmationCb, gasEstimationForceSend, promptCb, callback: VMExecutionCallBack) { @@ -68,12 +71,11 @@ export class TxRunnerVM { } runInVm (from: string, to: string, data: string, value: string, gasLimit: number, useCall: boolean, callback: VMExecutionCallBack) { - const self = this let account - if (!from && useCall && Object.keys(self.vmaccounts).length) { - from = Object.keys(self.vmaccounts)[0] - account = self.vmaccounts[from] - } else account = self.vmaccounts[from] + if (!from && useCall && Object.keys(this.vmaccounts).length) { + from = Object.keys(this.vmaccounts)[0] + account = this.vmaccounts[from] + } else account = this.vmaccounts[from] if (!account) { return callback('Invalid account selected') @@ -106,23 +108,29 @@ export class TxRunnerVM { const coinbases = ['0x0e9281e9c6a0808672eaba6bd1220e144c9bb07a', '0x8945a1288dc78a6d8952a92c77aee6730b414778', '0x94d76e24f818426ae84aa404140e8d5f60e10e7e'] const difficulties = [69762765929000, 70762765929000, 71762765929000] - const difficulty = this.commonContext.consensusType() === ConsensusType.ProofOfStake ? 0 : difficulties[self.blockNumber % difficulties.length] + const difficulty = this.commonContext.consensusType() === ConsensusType.ProofOfStake ? 0 : difficulties[this.blockNumber % difficulties.length] + if (!useCall) ++this.blockNumber + console.log(useCall, this.blockNumber) const block = Block.fromBlockData({ header: { timestamp: new Date().getTime() / 1000 | 0, - number: self.blockNumber, - coinbase: coinbases[self.blockNumber % coinbases.length], + number: this.blockNumber, + coinbase: coinbases[this.blockNumber % coinbases.length], difficulty, gasLimit, - baseFeePerGas: EIP1559 ? '0x1' : undefined + baseFeePerGas: EIP1559 ? '0x1' : undefined, + parentHash: this.blockParentHash }, transactions: [tx] }, { common: this.commonContext }) - if (!useCall) { - ++self.blockNumber - this.runBlockInVm(tx, block, callback) + this.blockParentHash = block.hash() + if (!useCall) { + this.runBlockInVm(tx, block, (err, result) => { + this.getVMObject().vm.blockchain.putBlock(block) + callback(err, result) + }) } else { this.getVMObject().stateManager.checkpoint().then(() => { this.runBlockInVm(tx, block, (err, result) => { diff --git a/libs/remix-simulator/src/genesis.ts b/libs/remix-simulator/src/genesis.ts deleted file mode 100644 index a17ea904c2..0000000000 --- a/libs/remix-simulator/src/genesis.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Block } from '@ethereumjs/block' -import { ConsensusType } from '@ethereumjs/common' - -export function generateBlock (vmContext) { - const common = vmContext.vmObject().common - - const difficulty = common.consensusType() === ConsensusType.ProofOfStake ? 0 : 69762765929000 - - return new Promise((resolve, reject) => { - const block: Block = Block.fromBlockData({ - header: { - timestamp: (new Date().getTime() / 1000 | 0), - number: 0, - coinbase: '0x0e9281e9c6a0808672eaba6bd1220e144c9bb07a', - difficulty, - gasLimit: 8000000 - } - }, { common: vmContext.vmObject().common }) - - vmContext.vm().runBlock({ block: block, generate: true, skipBlockValidation: true, skipBalance: false }).then(() => { - vmContext.addBlock(block) - resolve({}) - }).catch((e) => reject(e)) - }) -} diff --git a/libs/remix-simulator/src/provider.ts b/libs/remix-simulator/src/provider.ts index ebeca2e803..997accddf9 100644 --- a/libs/remix-simulator/src/provider.ts +++ b/libs/remix-simulator/src/provider.ts @@ -9,7 +9,6 @@ import { methods as miscMethods } from './methods/misc' import { methods as netMethods } from './methods/net' import { Transactions } from './methods/transactions' import { Debug } from './methods/debug' -import { generateBlock } from './genesis' import { VMContext } from './vm-context' export interface JSONRPCRequestPayload { @@ -59,7 +58,6 @@ export class Provider { this.initialized = false this.pendingRequests = [] await this.vmContext.init() - await generateBlock(this.vmContext) await this.Accounts.resetAccounts() this.Transactions.init(this.Accounts.accounts) this.initialized = true diff --git a/libs/remix-simulator/src/vm-context.ts b/libs/remix-simulator/src/vm-context.ts index 5ee49e6cc2..751c3008ce 100644 --- a/libs/remix-simulator/src/vm-context.ts +++ b/libs/remix-simulator/src/vm-context.ts @@ -9,7 +9,7 @@ import { execution } from '@remix-project/remix-lib' const { LogsManager } = execution import { VmProxy } from './VmProxy' import { VM } from '@ethereumjs/vm' -import { Common } from '@ethereumjs/common' +import { Common, ConsensusType } from '@ethereumjs/common' import { Trie } from '@ethereumjs/trie' import { DefaultStateManager, StateManager, EthersStateManager, EthersStateManagerOpts } from '@ethereumjs/statemanager' import { StorageDump } from '@ethereumjs/statemanager/dist/interface' @@ -182,7 +182,20 @@ export class VMContext { stateManager = new StateManagerCommonStorageDump() const common = new Common({ chain: 'mainnet', hardfork }) - const blockchain = new (Blockchain as any)({ common }) + + /*const difficulty = common.consensusType() === ConsensusType.ProofOfStake ? 0 : 69762765929000 + + const genesisBlock: Block = Block.fromBlockData({ + header: { + timestamp: (new Date().getTime() / 1000 | 0), + number: 0, + coinbase: '0x0e9281e9c6a0808672eaba6bd1220e144c9bb07a', + difficulty, + gasLimit: 8000000 + } + }, { common })*/ + + const blockchain = await Blockchain.create({ common, validateBlocks: false, validateConsensus: false }) const eei = new EEI(stateManager, common, blockchain) const evm = new EVM({ common, eei, allowUnlimitedContractSize: true }) @@ -198,6 +211,7 @@ export class VMContext { // VmProxy is used to track the EVM execution (to listen on opcode execution, in order for instance to generate the VM trace) const web3vm = new VmProxy(this) web3vm.setVM(vm) + this.addBlock(blockchain.genesisBlock, true) return { vm, web3vm, stateManager, common } } @@ -217,7 +231,7 @@ export class VMContext { return this.currentVm } - addBlock (block: Block) { + addBlock (block: Block, genesis?: Boolean) { let blockNumber = bigIntToHex(block.header.number) if (blockNumber === '0x') { blockNumber = '0x0' @@ -227,7 +241,7 @@ export class VMContext { this.blocks[blockNumber] = block this.latestBlockNumber = blockNumber - this.logsManager.checkBlock(blockNumber, block, this.web3()) + if (!genesis) this.logsManager.checkBlock(blockNumber, block, this.web3()) } trackTx (txHash, block, tx) { diff --git a/libs/remix-ui/run-tab/src/lib/actions/events.ts b/libs/remix-ui/run-tab/src/lib/actions/events.ts index 5fd49279d2..0c73425f1b 100644 --- a/libs/remix-ui/run-tab/src/lib/actions/events.ts +++ b/libs/remix-ui/run-tab/src/lib/actions/events.ts @@ -142,6 +142,7 @@ export const setupEvents = (plugin: RunTab, dispatch: React.Dispatch) => { } const broadcastCompilationResult = async (compilerName: string, plugin: RunTab, dispatch: React.Dispatch, file, source, languageVersion, data, input?) => { + console.log('broadcastCompilationResult') _paq.push(['trackEvent', 'udapp', 'broadcastCompilationResult', compilerName]) // TODO check whether the tab is configured const compiler = new CompilerAbstract(languageVersion, data, source, input)