|
| 1 | +import { blockFromTuple, parseBlockTuple, readBinaryFile, readERA1 } from '@ethereumjs/era' |
| 2 | +import { readdirSync } from 'fs' |
| 3 | + |
| 4 | +import { DBKey } from '../util/metaDBManager.js' |
| 5 | + |
| 6 | +import type { Config, EthereumClient } from '../index.js' |
| 7 | +// import type { Block } from '@ethereumjs/block' |
| 8 | + |
| 9 | +export async function eraSync( |
| 10 | + client: EthereumClient, |
| 11 | + config: Config, |
| 12 | + args: { loadBlocksFromEra1: string }, |
| 13 | +) { |
| 14 | + await client.chain.open() |
| 15 | + const service = client.service |
| 16 | + await service.execution.open() |
| 17 | + const eraDir = readdirSync(args.loadBlocksFromEra1) |
| 18 | + .filter((file) => file.endsWith('.era1')) |
| 19 | + .sort() |
| 20 | + for (const file of eraDir) { |
| 21 | + config.logger.info(`Loading era1 file ${file}`) |
| 22 | + const era1File = readBinaryFile(args.loadBlocksFromEra1 + '/' + file) |
| 23 | + const blockTuples = await readERA1(era1File) |
| 24 | + for await (const tuple of blockTuples) { |
| 25 | + const { header, body, receipts } = await parseBlockTuple(tuple) |
| 26 | + const block = blockFromTuple({ header, body }) |
| 27 | + await client.chain.blockchain.putBlock(block) |
| 28 | + if (config.saveReceipts && service.execution.receiptsManager) { |
| 29 | + await service.execution.receiptsManager?.put(DBKey.Receipts, block.hash(), receipts) |
| 30 | + void service.execution.receiptsManager['updateIndex'](0, 0, block) |
| 31 | + } |
| 32 | + } |
| 33 | + await client.chain.update(false) |
| 34 | + await service.execution.run() |
| 35 | + } |
| 36 | +} |
0 commit comments