Skip to content

Commit acff033

Browse files
author
ScottyPoi
committed
client: write eraSync function
1 parent b3c3dad commit acff033

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

packages/client/src/sync/erasync.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

packages/client/src/sync/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// need this weird re-export for vitest to be able to mock reverseblockfetcher in test/sync/beaconsync.spec.ts
55
export * from '../service/skeleton.js'
66
export * from './beaconsync.js'
7+
export * from './erasync.js'
78
export * from './fullsync.js'
89
export * from './snapsync.js'
910
export * from './sync.js'

0 commit comments

Comments
 (0)