Skip to content

Commit

Permalink
add export method to client
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyPoi committed Jan 29, 2025
1 parent bf9f0b6 commit 1d6e4f7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
18 changes: 9 additions & 9 deletions packages/client/src/rpc/modules/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@ethereumjs/util'
import { type VM, encodeReceipt, runTx } from '@ethereumjs/vm'

// import { exportHistoryAsEra1 } from '../../util/exportHistory.js'
import { exportHistoryAsEra1 } from '../../util/exportHistory.js'
import { INTERNAL_ERROR, INVALID_PARAMS } from '../error-code.js'
import { callWithStackTrace, getBlockByOption } from '../helpers.js'
import { middleware, validators } from '../validation.js'
Expand Down Expand Up @@ -155,11 +155,11 @@ export class Debug {
this.verbosity = middleware(callWithStackTrace(this.verbosity.bind(this), this._rpcDebug), 1, [
[validators.unsignedInteger],
])
// this.exportHistoryAsEra1 = middleware(
// callWithStackTrace(this.exportHistoryAsEra1.bind(this), this._rpcDebug),
// 0,
// [],
// )
this.exportHistoryAsEra1 = middleware(
callWithStackTrace(this.exportHistoryAsEra1.bind(this), this._rpcDebug),
0,
[],
)
}

/**
Expand Down Expand Up @@ -493,7 +493,7 @@ export class Debug {
}
}

// async exportHistoryAsEra1() {
// return exportHistoryAsEra1(this.client)
// }
async exportHistoryAsEra1() {
return exportHistoryAsEra1(this.client)
}
}
82 changes: 82 additions & 0 deletions packages/client/src/util/exportHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { formatEra1 } from '@ethereumjs/era'
import { RLP } from '@ethereumjs/rlp'
import { writeFileSync } from 'fs'

import { DBKey } from './metaDBManager.js'

import type { EthereumClient } from '../index.js'
import type { Block } from '@ethereumjs/block'

export async function getEpochBlockRecords(
blocks: Block[],
blockReceipts: Uint8Array[],
td: bigint,
) {
const blockTuples: {
header: Uint8Array
body: Uint8Array
receipts: Uint8Array
totalDifficulty: bigint
}[] = []
const headerRecords: {
blockHash: Uint8Array
totalDifficulty: bigint
}[] = []
for (const [i, block] of blocks.entries()) {
td += block.header.difficulty
headerRecords.push({
blockHash: block.hash(),
totalDifficulty: td,
})
const receipts = blockReceipts[i]

const body = [
block.transactions.map((tx) => tx.serialize()),
block.uncleHeaders.map((uh) => uh.raw()),
]
if (block.withdrawals) {
body.push(block.withdrawals.map((w) => w.raw()))
}
blockTuples.push({
header: block.header.serialize(),
body: RLP.encode(body),
receipts,
totalDifficulty: td,
})
}
return {
headerRecords,
blockTuples,
totalDifficulty: td,
}
}

export async function exportEpochAsEra1(client: EthereumClient, epoch: number, td: bigint) {
const epochBlocks = await client.chain.getBlocks(BigInt(epoch) * 8192n, 8192)
const epochReceipts = []
for (const b of epochBlocks) {
const receipts =
(await client.service.execution.receiptsManager?.get(DBKey.Receipts, b.hash())) ??
RLP.encode([])
epochReceipts.push(receipts)
}

const { headerRecords, blockTuples, totalDifficulty } = await getEpochBlockRecords(
epochBlocks,
epochReceipts,
td,
)
const era1 = await formatEra1(blockTuples, headerRecords, epoch)
return { era1, totalDifficulty }
}

export async function exportHistoryAsEra1(client: EthereumClient, outputDir: string = '.') {
const epochs = Math.floor(Number(client.chain.blocks.height) / 8192)
let td = 0n
for (let i = 0; i < epochs; i++) {
const { era1, totalDifficulty } = await exportEpochAsEra1(client, i, td)
td = totalDifficulty
writeFileSync(`${outputDir}/epoch-${i}.era1`, era1)
}
return epochs
}
2 changes: 1 addition & 1 deletion packages/client/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { platform } from 'os'
import { dirname, join as joinPath } from 'path'
import { fileURLToPath } from 'url'

export * from './exportHistory.js'
export * from './inclineClient.js'
export * from './parse.js'
export * from './rpc.js'

// See: https://stackoverflow.com/a/50053801
const __dirname = dirname(fileURLToPath(import.meta.url))

Expand Down

0 comments on commit 1d6e4f7

Please sign in to comment.