Attemp to fetch blocks

pull/5370/head
ioedeveloper 9 months ago
parent 6a4d722b10
commit 8fd6b85572
  1. 40
      apps/remix-ide/src/blockchain/providers/vm.ts
  2. 8
      apps/remix-ide/src/blockchain/providers/worker-vm.ts
  3. 8
      libs/remix-simulator/src/provider.ts
  4. 11
      libs/remix-simulator/src/vm-context.ts

@ -2,6 +2,8 @@ import Web3, { FMT_BYTES, FMT_NUMBER, LegacySendAsyncProvider } from 'web3'
import { fromWei, toBigInt } from 'web3-utils' import { fromWei, toBigInt } from 'web3-utils'
import { privateToAddress, hashPersonalMessage, isHexString } from '@ethereumjs/util' import { privateToAddress, hashPersonalMessage, isHexString } from '@ethereumjs/util'
import { extend, JSONRPCRequestPayload, JSONRPCResponseCallback } from '@remix-project/remix-simulator' import { extend, JSONRPCRequestPayload, JSONRPCResponseCallback } from '@remix-project/remix-simulator'
import {toBuffer} from '@ethereumjs/util'
import { Block } from '@ethereumjs/block'
import { ExecutionContext } from '../execution-context' import { ExecutionContext } from '../execution-context'
export class VMProvider { export class VMProvider {
@ -27,7 +29,7 @@ export class VMProvider {
}) })
} }
async resetEnvironment (stringifiedStateDb?: string) { async resetEnvironment (stringifiedState?: string) {
if (this.worker) this.worker.terminate() if (this.worker) this.worker.terminate()
this.worker = new Worker(new URL('./worker-vm', import.meta.url)) this.worker = new Worker(new URL('./worker-vm', import.meta.url))
const provider = this.executionContext.getProviderObject() const provider = this.executionContext.getProviderObject()
@ -74,13 +76,35 @@ export class VMProvider {
} }
} }
}) })
this.worker.postMessage({ if (stringifiedState) {
cmd: 'init', try {
fork: this.executionContext.getCurrentFork(), const blockchainState = JSON.parse(stringifiedState)
nodeUrl: provider?.options['nodeUrl'], const blocks: Block[] = blockchainState.blocks.map(block => Block.fromRLPSerializedBlock(toBuffer(block)))
blockNumber: provider?.options['blockNumber'], const blockNumber = toBigInt(blockchainState.latestBlockNumber).toString(10)
stateDb: stringifiedStateDb const stateDb = {
}) root: toBuffer(blockchainState.root),
db: new Map(Object.entries(blockchainState.db))
}
this.worker.postMessage({
cmd: 'init',
fork: this.executionContext.getCurrentFork(),
nodeUrl: provider?.options['nodeUrl'],
blockNumber,
stateDb,
blocks
})
} catch (e) {
console.error(e)
}
} else {
this.worker.postMessage({
cmd: 'init',
fork: this.executionContext.getCurrentFork(),
nodeUrl: provider?.options['nodeUrl'],
blockNumber: provider?.options['blockNumber']
})
}
}) })
} }

@ -1,5 +1,4 @@
import { Provider } from '@remix-project/remix-simulator' import { Provider } from '@remix-project/remix-simulator'
import {toBuffer} from '@ethereumjs/util'
let provider: Provider = null let provider: Provider = null
self.onmessage = (e: MessageEvent) => { self.onmessage = (e: MessageEvent) => {
@ -7,12 +6,7 @@ self.onmessage = (e: MessageEvent) => {
switch (data.cmd) { switch (data.cmd) {
case 'init': case 'init':
{ {
if (data.stateDb) { provider = new Provider({ fork: data.fork, nodeUrl: data.nodeUrl, blockNumber: data.blockNumber, stateDb: data.stateDb, blocks: data.blocks})
data.stateDb = JSON.parse(data.stateDb)
data.stateDb.root = toBuffer(data.stateDb.root)
data.stateDb.db = new Map(Object.entries(data.stateDb.db))
}
provider = new Provider({ fork: data.fork, nodeUrl: data.nodeUrl, blockNumber: data.blockNumber, stateDb: data.stateDb })
provider.init().then(() => { provider.init().then(() => {
self.postMessage({ self.postMessage({
cmd: 'initiateResult', cmd: 'initiateResult',

@ -11,6 +11,7 @@ import { Transactions } from './methods/transactions'
import { Debug } from './methods/debug' import { Debug } from './methods/debug'
import { VMContext } from './vm-context' import { VMContext } from './vm-context'
import { Web3PluginBase } from 'web3' import { Web3PluginBase } from 'web3'
import { Block } from '@ethereumjs/block'
export interface JSONRPCRequestPayload { export interface JSONRPCRequestPayload {
params: any[]; params: any[];
@ -36,8 +37,9 @@ export type ProviderOptions = {
fork: string, fork: string,
nodeUrl: string, nodeUrl: string,
blockNumber: number | 'latest', blockNumber: number | 'latest',
stateDb: State, stateDb?: State,
logDetails?: boolean logDetails?: boolean
blocks?: Block[]
} }
export class Provider { export class Provider {
@ -53,7 +55,7 @@ export class Provider {
constructor (options: ProviderOptions = {} as ProviderOptions) { constructor (options: ProviderOptions = {} as ProviderOptions) {
this.options = options this.options = options
this.connected = true this.connected = true
this.vmContext = new VMContext(options['fork'], options['nodeUrl'], options['blockNumber'], options['stateDb']) 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)
this.Transactions = new Transactions(this.vmContext) this.Transactions = new Transactions(this.vmContext)
@ -73,7 +75,7 @@ export class Provider {
this.pendingRequests = [] this.pendingRequests = []
await this.vmContext.init() await this.vmContext.init()
await this.Accounts.resetAccounts() await this.Accounts.resetAccounts()
this.Transactions.init(this.Accounts.accounts, this.vmContext.blockNumber) this.Transactions.init(this.Accounts.accounts, this.vmContext.blockNumber, this.vmContext.blocks)
this.initialized = true this.initialized = true
if (this.pendingRequests.length > 0) { if (this.pendingRequests.length > 0) {
this.pendingRequests.map((req) => { this.pendingRequests.map((req) => {

@ -332,8 +332,9 @@ export class VMContext {
nodeUrl: string nodeUrl: string
blockNumber: number | 'latest' blockNumber: number | 'latest'
stateDb: State stateDb: State
blocksData: Block[]
constructor (fork?: string, nodeUrl?: string, blockNumber?: number | 'latest', stateDb?: State) { constructor (fork?: string, nodeUrl?: string, blockNumber?: number | 'latest', stateDb?: State, blocksData?: Block[]) {
this.blockGasLimitDefault = 4300000 this.blockGasLimitDefault = 4300000
this.blockGasLimit = this.blockGasLimitDefault this.blockGasLimit = this.blockGasLimitDefault
this.currentFork = fork || 'merge' this.currentFork = fork || 'merge'
@ -346,6 +347,7 @@ export class VMContext {
this.txByHash = {} this.txByHash = {}
this.exeResults = {} this.exeResults = {}
this.logsManager = new LogsManager() this.logsManager = new LogsManager()
this.blocksData = blocksData
} }
async init () { async init () {
@ -387,8 +389,13 @@ export class VMContext {
gasLimit: 8000000 gasLimit: 8000000
} }
}, { common, hardforkByBlockNumber: false, hardforkByTTD: undefined }) }, { common, hardforkByBlockNumber: false, hardforkByTTD: undefined })
let blockchain
const blockchain = await Blockchain.create({ common, validateBlocks: false, validateConsensus: false, genesisBlock }) if (Array.isArray(this.blocksData) && this.blocksData.length > 0) {
blockchain = await Blockchain.fromBlocksData(this.blocksData, { common, validateBlocks: false, validateConsensus: false })
} else {
blockchain = await Blockchain.create({ common, validateBlocks: false, validateConsensus: false, genesisBlock })
}
const eei = new EEI(stateManager, common, blockchain) const eei = new EEI(stateManager, common, blockchain)
const evm = new EVM({ common, eei, allowUnlimitedContractSize: true }) const evm = new EVM({ common, eei, allowUnlimitedContractSize: true })

Loading…
Cancel
Save