From 639a6cca09437aa5315a5ddb1dd066cd206ad969 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Tue, 13 Feb 2024 13:21:23 +0100 Subject: [PATCH] Implement fetching blocks data --- apps/remix-ide/src/blockchain/execution-context.js | 9 +++++++-- libs/remix-lib/src/execution/txRunnerVM.ts | 7 +++++-- libs/remix-simulator/src/methods/transactions.ts | 12 ++++++++++-- libs/remix-simulator/src/provider.ts | 7 +++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/apps/remix-ide/src/blockchain/execution-context.js b/apps/remix-ide/src/blockchain/execution-context.js index e6a5eace08..38800a3563 100644 --- a/apps/remix-ide/src/blockchain/execution-context.js +++ b/apps/remix-ide/src/blockchain/execution-context.js @@ -210,16 +210,21 @@ export class ExecutionContext { // TODO: this won't save the state for transactions executed outside of the UI (for instance from a script execution). const root = await this.web3().remix.getStateTrieRoot() const db = await this.web3().remix.getStateDb() + const blocksData = await this.web3().remix.getBlocksData() const state = { root, - db: Object.fromEntries(db._database) + db: Object.fromEntries(db._database), + blocks: blocksData.blocks, + latestBlockNumber: blocksData.latestBlockNumber } const stringifyed = JSON.stringify(state, (key, value) => { if (key === 'root') { return bufferToHex(value) } else if (key === 'db') { return value - } else if (key === '') { + } else if (key === 'blocks') { + return value.map(block => bufferToHex(block)) + }else if (key === '') { return value } return bufferToHex(value) diff --git a/libs/remix-lib/src/execution/txRunnerVM.ts b/libs/remix-lib/src/execution/txRunnerVM.ts index 2eac459330..b050996302 100644 --- a/libs/remix-lib/src/execution/txRunnerVM.ts +++ b/libs/remix-lib/src/execution/txRunnerVM.ts @@ -37,7 +37,7 @@ export class TxRunnerVM { // has a default for now for backwards compatibility this.getVMObject = getVMObject this.commonContext = this.getVMObject().common - this.blockNumber = blockNumber || 0 + this.blockNumber = blockNumber || 0 // TODO: this should be set to the fetched block number count this.pendingTxs = {} this.vmaccounts = vmaccounts this.queusTxs = [] @@ -125,7 +125,10 @@ export class TxRunnerVM { this.blockNumber = this.blockNumber + 1 this.blockParentHash = block.hash() this.runBlockInVm(tx, block, (err, result) => { - if (!err) this.getVMObject().vm.blockchain.putBlock(block) // look at putBlock for saving blocks + if (!err) { + this.getVMObject().vm.blockchain.putBlock(block) + this.blocks.push(block.serialize()) + } callback(err, result) }) } else { diff --git a/libs/remix-simulator/src/methods/transactions.ts b/libs/remix-simulator/src/methods/transactions.ts index 9a47aa0135..f109d17996 100644 --- a/libs/remix-simulator/src/methods/transactions.ts +++ b/libs/remix-simulator/src/methods/transactions.ts @@ -59,7 +59,7 @@ export class Transactions { this.txRunnerInstance = new TxRunner(this.txRunnerVMInstance, {}) this.txRunnerInstance.vmaccounts = accounts } - + methods () { return { eth_sendTransaction: this.eth_sendTransaction.bind(this), @@ -76,7 +76,8 @@ export class Transactions { eth_getHashFromTagBySimulator: this.eth_getHashFromTagBySimulator.bind(this), eth_registerCallId: this.eth_registerCallId.bind(this), eth_getStateTrieRoot: this.eth_getStateTrieRoot.bind(this), - eth_getStateDb: this.eth_getStateDb.bind(this) + eth_getStateDb: this.eth_getStateDb.bind(this), + eth_getBlocksData: this.eth_getBlocksData.bind(this) } } @@ -208,6 +209,13 @@ export class Transactions { cb(null, this.vmContext.currentVm.stateManager.getDb()) } + eth_getBlocksData (_, cb) { + cb(null, { + blocks: this.txRunnerVMInstance.blocks, + latestBlockNumber: this.txRunnerVMInstance.blockNumber + }) + } + eth_call (payload, cb) { // from might be lowercased address (web3) if (payload.params && payload.params.length > 0 && payload.params[0].from) { diff --git a/libs/remix-simulator/src/provider.ts b/libs/remix-simulator/src/provider.ts index 98c358cba7..21757fe008 100644 --- a/libs/remix-simulator/src/provider.ts +++ b/libs/remix-simulator/src/provider.ts @@ -195,4 +195,11 @@ class Web3TestPlugin extends Web3PluginBase { params: [] }) } + + public getBlocksData() { + return this.requestManager.send({ + method: 'eth_getBlocksData', + params: [] + }) + } }