From 4ba500f466bf8a76ec3ece21e5a7931a9f1d8c55 Mon Sep 17 00:00:00 2001 From: yann300 Date: Tue, 22 Oct 2024 10:39:06 +0200 Subject: [PATCH] add missing remix-simulator endpoint --- libs/remix-lib/src/execution/txRunnerVM.ts | 29 ++++++- libs/remix-simulator/src/methods/accounts.ts | 6 +- libs/remix-simulator/src/methods/evm.ts | 79 ++++++++++++++++++++ libs/remix-simulator/src/methods/miner.ts | 2 +- libs/remix-simulator/src/provider.ts | 6 ++ 5 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 libs/remix-simulator/src/methods/evm.ts diff --git a/libs/remix-lib/src/execution/txRunnerVM.ts b/libs/remix-lib/src/execution/txRunnerVM.ts index 6c4c40b27b..51e52af768 100644 --- a/libs/remix-lib/src/execution/txRunnerVM.ts +++ b/libs/remix-lib/src/execution/txRunnerVM.ts @@ -75,6 +75,33 @@ export class TxRunnerVM { } } + runEmptyBlock (callback: VMExecutionCallBack) { + const EIP1559 = this.commonContext.hardfork() !== 'berlin' // berlin is the only pre eip1559 fork that we handle. + const coinbases = ['0x0e9281e9c6a0808672eaba6bd1220e144c9bb07a', '0x8945a1288dc78a6d8952a92c77aee6730b414778', '0x94d76e24f818426ae84aa404140e8d5f60e10e7e'] + const difficulties = [69762765929000, 70762765929000, 71762765929000] + const difficulty = this.commonContext.consensusType() === ConsensusType.ProofOfStake ? 0 : difficulties[this.blocks.length % difficulties.length] + const block = Block.fromBlockData({ + header: { + timestamp: new Date().getTime() / 1000 | 0, + number: this.blocks.length, + coinbase: coinbases[this.blocks.length % coinbases.length], + difficulty, + gasLimit: 0, + baseFeePerGas: EIP1559 ? '0x1' : undefined, + parentHash: this.blockParentHash + } + }, { common: this.commonContext }) + + this.blockParentHash = block.hash() + this.runBlockInVm(null, block, async (err, result) => { + if (!err) { + this.getVMObject().vm.blockchain.putBlock(block) + this.blocks.push(block.serialize()) + } + callback(err, result) + }) + } + async runInVm (tx: InternalTransaction, callback: VMExecutionCallBack) { const { to, data, value, gasLimit, useCall, signed } = tx let { from } = tx @@ -183,7 +210,7 @@ export class TxRunnerVM { const result: RunTxResult = results.results[0] callback(null, { result, - transactionHash: bytesToHex(Buffer.from(tx.hash())), + transactionHash: tx ? bytesToHex(Buffer.from(tx.hash())) : null, block, tx }) diff --git a/libs/remix-simulator/src/methods/accounts.ts b/libs/remix-simulator/src/methods/accounts.ts index 316ccf2b26..1fff17faf4 100644 --- a/libs/remix-simulator/src/methods/accounts.ts +++ b/libs/remix-simulator/src/methods/accounts.ts @@ -1,7 +1,7 @@ import { signTypedData, SignTypedDataVersion, TypedMessage, MessageTypes } from '@metamask/eth-sig-util' import { privateToAddress, toChecksumAddress, isValidPrivate, Address, toBytes, bytesToHex, Account } from '@ethereumjs/util' import { privateKeyToAccount } from 'web3-eth-accounts' -import { toBigInt } from 'web3-utils' +import { toBigInt, toHex } from 'web3-utils' import * as crypto from 'crypto' type AccountType = { @@ -118,7 +118,9 @@ export class Web3Accounts { } eth_chainId (_payload, cb) { - return cb(null, this.options.chainId || '0x539') // 0x539 is hex of 1337 + if (!this.options.chainId) return cb(null, '0x539') // 0x539 is hex of 1337 + const id = (typeof this.options.chainId === 'number') ? toHex(this.options.chainId) : this.options.chainId + return cb(null, id) } eth_signTypedData_v4 (payload, cb) { diff --git a/libs/remix-simulator/src/methods/evm.ts b/libs/remix-simulator/src/methods/evm.ts new file mode 100644 index 0000000000..22f348e5b9 --- /dev/null +++ b/libs/remix-simulator/src/methods/evm.ts @@ -0,0 +1,79 @@ +import { Block } from '@ethereumjs/block' +import { ConsensusType } from '@ethereumjs/common' +import type { VMContext } from '../vm-context' +import type { Transactions } from '../methods/transactions' + +export class EVM { + vmContext: VMContext + transactions: Transactions + + constructor (vmContext: VMContext, transactions: Transactions) { + this.vmContext = vmContext + this.transactions = transactions + } + + methods () { + return { + evm_setAutomine: this.evm_setAutomine.bind(this), + evm_setIntervalMining: this.evm_setIntervalMining.bind(this), + evm_snapshot: this.evm_snapshot.bind(this), + evm_revert: this.evm_revert.bind(this), + evm_increaseTime: this.evm_increaseTime.bind(this), + evm_setNextBlockTimestamp: this.evm_setNextBlockTimestamp.bind(this), + evm_setBlockGasLimit: this.evm_setBlockGasLimit.bind(this), + evm_mine: this.evm_mine.bind(this) + } + } + + evm_setAutomine (payload, cb) { + // always on + cb() + } + + evm_setIntervalMining (payload, cb) { + cb() + } + + evm_snapshot (payload, cb) { + cb() + } + + evm_revert (payload, cb) { + cb() + } + + evm_increaseTime (payload, cb) { + cb() + } + + evm_setNextBlockTimestamp (payload, cb) { + cb() + } + + evm_setBlockGasLimit (payload, cb) { + cb() + } + + async evm_mine (payload, cb) { + const runEmptyBlock = () => { + return new Promise((resolve, reject) => { + this.transactions.txRunnerVMInstance.runEmptyBlock((error, result) => { + if (error) { + reject(error) + return + } + this.vmContext.addBlock(result.block, false, true) + resolve(result) + }) + }) + } + + let blocks = payload.params[0].blocks + + for (let b = 0; b < Number(blocks); b++) { + await runEmptyBlock() + console.log('mining...', b, this.vmContext.latestBlockNumber) + } + cb() + } +} diff --git a/libs/remix-simulator/src/methods/miner.ts b/libs/remix-simulator/src/methods/miner.ts index 2d8994c2bf..b44058f5a7 100644 --- a/libs/remix-simulator/src/methods/miner.ts +++ b/libs/remix-simulator/src/methods/miner.ts @@ -14,5 +14,5 @@ export class Miner { miner_start (payload, cb) { cb() } - miner_stop (payload, cb) { cb()} + miner_stop (payload, cb) { cb() } } diff --git a/libs/remix-simulator/src/provider.ts b/libs/remix-simulator/src/provider.ts index c28257018e..8fae322c6b 100644 --- a/libs/remix-simulator/src/provider.ts +++ b/libs/remix-simulator/src/provider.ts @@ -10,6 +10,7 @@ import { Net } from './methods/net' import { Transactions } from './methods/transactions' import { Miner } from './methods/miner' import { Debug } from './methods/debug' +import { EVM } from './methods/evm' import { VMContext } from './vm-context' import { Web3PluginBase } from 'web3' @@ -49,9 +50,11 @@ export class Provider { methods connected: boolean initialized: boolean + initializing: boolean pendingRequests: Array constructor (options: ProviderOptions = {} as ProviderOptions) { + console.log(options) this.options = options this.connected = true this.vmContext = new VMContext(options['fork'], options['nodeUrl'], options['blockNumber'], options['stateDb'], options['blocks']) @@ -67,10 +70,12 @@ export class Provider { this.methods = merge(this.methods, (new Net(this.vmContext, options)).methods()) this.methods = merge(this.methods, this.Transactions.methods()) this.methods = merge(this.methods, (new Debug(this.vmContext)).methods()) + this.methods = merge(this.methods, (new EVM(this.vmContext, this.Transactions)).methods()) this.methods = merge(this.methods, (new Miner(this.vmContext)).methods()) } async init () { + this.initializing = true this.initialized = false this.pendingRequests = [] await this.vmContext.init() @@ -83,6 +88,7 @@ export class Provider { }) this.pendingRequests = [] } + this.initializing = false } _send(payload: JSONRPCRequestPayload, callback: (err: Error, result?: JSONRPCResponsePayload) => void) {