|
| 1 | +import { Block } from '@ethereumjs/block' |
| 2 | +import { VMExecution } from '../sync/execution' |
| 3 | + |
| 4 | +/** |
| 5 | + * Generates a code snippet which can be used to replay an erraneous block |
| 6 | + * locally in the VM |
| 7 | + * |
| 8 | + * @param block |
| 9 | + */ |
| 10 | +export async function debugCodeReplayBlock(execution: VMExecution, block: Block) { |
| 11 | + const code = ` |
| 12 | +/** |
| 13 | + * Script for locally executing a block in the EthereumJS VM, |
| 14 | + * meant to be used from packages/vm directory within the |
| 15 | + * https://github.com/ethereumjs/ethereumjs-monorepo repository. |
| 16 | + * |
| 17 | + * Block: ${block.header.number} |
| 18 | + * Hardfork: ${execution.hardfork} |
| 19 | + * |
| 20 | + * Run with: DEBUG=vm:*:*,vm:*,-vm:ops:* ts-node [SCRIPT_NAME].ts |
| 21 | + * |
| 22 | + */ |
| 23 | +
|
| 24 | +const level = require('level') |
| 25 | +import Common from '@ethereumjs/common' |
| 26 | +import { Block } from '@ethereumjs/block' |
| 27 | +import VM from './lib' |
| 28 | +import { SecureTrie as Trie } from '@ethereumjs/trie' |
| 29 | +import { DefaultStateManager } from './lib/state' |
| 30 | +import Blockchain from '@ethereumjs/blockchain' |
| 31 | +
|
| 32 | +const main = async () => { |
| 33 | + const common = new Common({ chain: '${execution.config.execCommon.chainName()}', hardfork: '${ |
| 34 | + execution.hardfork |
| 35 | + }' }) |
| 36 | + const block = Block.fromRLPSerializedBlock(Buffer.from('${block |
| 37 | + .serialize() |
| 38 | + .toString('hex')}', 'hex'), { common }) |
| 39 | +
|
| 40 | + const stateDB = level('${execution.config.getStateDataDirectory()}') |
| 41 | + const trie = new Trie(stateDB) |
| 42 | + const stateManager = new DefaultStateManager({ trie, common }) |
| 43 | + // Ensure we run on the right root |
| 44 | + stateManager.setStateRoot(Buffer.from('${( |
| 45 | + await execution.vm.stateManager.getStateRoot(true) |
| 46 | + ).toString('hex')}', 'hex')) |
| 47 | +
|
| 48 | +
|
| 49 | + const chainDB = level('${execution.config.getChainDataDirectory()}') |
| 50 | + const blockchain = await Blockchain.create({ |
| 51 | + db: chainDB, |
| 52 | + common, |
| 53 | + validateBlocks: true, |
| 54 | + validateConsensus: false, |
| 55 | + }) |
| 56 | + const vm = new VM({ stateManager, blockchain, common }) |
| 57 | +
|
| 58 | + await vm.runBlock({ block }) |
| 59 | +} |
| 60 | +
|
| 61 | +main() |
| 62 | + ` |
| 63 | + |
| 64 | + execution.config.logger.info(code) |
| 65 | +} |
0 commit comments