Skip to content

Commit

Permalink
add missing remix-simulator endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
yann300 committed Oct 23, 2024
1 parent e2a0953 commit 4ba500f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 4 deletions.
29 changes: 28 additions & 1 deletion libs/remix-lib/src/execution/txRunnerVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
})
Expand Down
6 changes: 4 additions & 2 deletions libs/remix-simulator/src/methods/accounts.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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) {
Expand Down
79 changes: 79 additions & 0 deletions libs/remix-simulator/src/methods/evm.ts
Original file line number Diff line number Diff line change
@@ -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()
}
}
2 changes: 1 addition & 1 deletion libs/remix-simulator/src/methods/miner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export class Miner {

miner_start (payload, cb) { cb() }

miner_stop (payload, cb) { cb()}
miner_stop (payload, cb) { cb() }
}
6 changes: 6 additions & 0 deletions libs/remix-simulator/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -49,9 +50,11 @@ export class Provider {
methods
connected: boolean
initialized: boolean
initializing: boolean
pendingRequests: Array<any>

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'])
Expand All @@ -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()
Expand All @@ -83,6 +88,7 @@ export class Provider {
})
this.pendingRequests = []
}
this.initializing = false
}

_send(payload: JSONRPCRequestPayload, callback: (err: Error, result?: JSONRPCResponsePayload) => void) {
Expand Down

0 comments on commit 4ba500f

Please sign in to comment.