Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add missing endpoint #5310

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion libs/remix-simulator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* [X] eth_getCode
* [~] eth_sign
* [X] eth_sendTransaction
* [_] eth_sendRawTransaction
* [x] eth_sendRawTransaction
* [X] eth_call
* [~] eth_estimateGas
* [X] eth_getBlockByHash
Expand Down
12 changes: 9 additions & 3 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 All @@ -13,9 +13,11 @@ export class Web3Accounts {
accounts: Record<string, AccountType>
accountsKeys: Record<string, string>
vmContext
options

constructor (vmContext) {
constructor (vmContext, options) {
this.vmContext = vmContext
this.options = options
// TODO: make it random and/or use remix-libs

this.accounts = {}
Expand Down Expand Up @@ -92,6 +94,8 @@ export class Web3Accounts {
eth_getBalance (payload, cb) {
const address = payload.params[0]
this.vmContext.vm().stateManager.getAccount(Address.fromString(address)).then((account) => {
if (!account) return cb(null, toBigInt(0).toString(10))
if (!account.balance) return cb(null, toBigInt(0).toString(10))
cb(null, toBigInt(account.balance).toString(10))
}).catch((error) => {
cb(error)
Expand All @@ -114,7 +118,9 @@ export class Web3Accounts {
}

eth_chainId (_payload, cb) {
return cb(null, '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()
}
}
18 changes: 18 additions & 0 deletions libs/remix-simulator/src/methods/miner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export class Miner {
vmContext

constructor (vmContext) {
this.vmContext = vmContext
}

methods () {
return {
miner_start: this.miner_start.bind(this),
miner_stop: this.miner_stop.bind(this)
}
}

miner_start (payload, cb) { cb() }

miner_stop (payload, cb) { cb() }
}
34 changes: 19 additions & 15 deletions libs/remix-simulator/src/methods/net.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
export function methods (): Record<string, unknown> {
return {
net_version: net_version,
net_listening: net_listening,
net_peerCount: net_peerCount
export class Net {
vmContext
options

constructor (vmContext, options) {
this.vmContext = vmContext
this.options = options
}
}

export function net_version (payload, cb): void {
// should be configured networkId
cb(null, 1337)
}
methods () {
return {
net_version: this.net_version.bind(this),
net_listening: this.net_listening.bind(this),
net_peerCount: this.net_peerCount.bind(this)
}
}

export function net_listening (payload, cb): void {
cb(null, true)
}
net_version (payload, cb) { cb(null, 1337) }

net_listening (payload, cb) { cb(null, true)}

export function net_peerCount (payload, cb): void {
cb(null, 0)
net_peerCount (payload, cb) { cb(null, 0)}
}

15 changes: 12 additions & 3 deletions libs/remix-simulator/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import merge from 'merge'
import { Web3Accounts } from './methods/accounts'
import { Filters } from './methods/filters'
import { methods as miscMethods } from './methods/misc'
import { methods as netMethods } from './methods/net'
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 All @@ -30,6 +32,7 @@ export type JSONRPCResponseCallback = (err: Error, result?: JSONRPCResponsePaylo
export type State = Record<string, string>

export type ProviderOptions = {
chainId?: number
fork?: string,
nodeUrl?: string,
blockNumber?: number | 'latest',
Expand All @@ -47,27 +50,32 @@ 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'])

this.Accounts = new Web3Accounts(this.vmContext)
this.Accounts = new Web3Accounts(this.vmContext, options)
this.Transactions = new Transactions(this.vmContext)

this.methods = {}
this.methods = merge(this.methods, this.Accounts.methods())
this.methods = merge(this.methods, (new Blocks(this.vmContext, options)).methods())
this.methods = merge(this.methods, miscMethods())
this.methods = merge(this.methods, (new Filters(this.vmContext)).methods())
this.methods = merge(this.methods, netMethods())
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 @@ -80,6 +88,7 @@ export class Provider {
})
this.pendingRequests = []
}
this.initializing = false
}

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