Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/client/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { bytesToHex } from '@ethereumjs/util'
export * from './inclineClient.ts'
export * from './parse.ts'
export * from './rpc.ts'
export * from './purge.ts'
// See: https://stackoverflow.com/a/50053801
const __dirname = dirname(fileURLToPath(import.meta.url))

Expand Down
73 changes: 73 additions & 0 deletions packages/client/src/util/purge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { DBOp } from '@ethereumjs/blockchain'
import { type BatchDBOp, type DelBatch, concatBytes, intToBytes } from '@ethereumjs/util'
import { Level } from 'level'
import { DBKey } from './metaDBManager.ts'

export const DBTarget = {
NumberToHash: 4,
Body: 6,
Header: 7,
Receipts: 8,
} as const

async function initDBs(dataDir: string, chain: string) {
const chainDir = `${dataDir}/${chain}`

Check warning on line 14 in packages/client/src/util/purge.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/util/purge.ts#L13-L14

Added lines #L13 - L14 were not covered by tests
// Chain DB
const chainDataDir = `${chainDir}/chain`
const chainDB = new Level<string | Uint8Array, string | Uint8Array>(chainDataDir)
await chainDB.open()

Check warning on line 18 in packages/client/src/util/purge.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/util/purge.ts#L16-L18

Added lines #L16 - L18 were not covered by tests
// Meta DB (receipts, logs, indexes, skeleton chain)
const metaDataDir = `${chainDir}/meta`
const metaDB = new Level<string | Uint8Array, string | Uint8Array>(metaDataDir)
await metaDB.open()

Check warning on line 22 in packages/client/src/util/purge.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/util/purge.ts#L20-L22

Added lines #L20 - L22 were not covered by tests

return { chainDB, metaDB }
}

Check warning on line 25 in packages/client/src/util/purge.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/util/purge.ts#L24-L25

Added lines #L24 - L25 were not covered by tests

export async function purgeHistory(
dataDir: string,
chain: string = 'mainnet',
before: bigint = 15537393n,
headers: boolean = false,
) {
const { chainDB, metaDB } = await initDBs(dataDir, chain)
const dbOps: DBOp[] = []
const metaDBOps: {

Check warning on line 35 in packages/client/src/util/purge.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/util/purge.ts#L27-L35

Added lines #L27 - L35 were not covered by tests
type: 'del'
key: Uint8Array
}[] = []
let blockNumber = before
while (blockNumber > 0n) {
const blockHashDBOp = DBOp.get(DBTarget.NumberToHash, { blockNumber })
const blockHash = await chainDB.get(blockHashDBOp.baseDBOp.key, {
keyEncoding: blockHashDBOp.baseDBOp.keyEncoding,
valueEncoding: blockHashDBOp.baseDBOp.valueEncoding,
})
if (!(blockHash instanceof Uint8Array)) {
blockNumber--
continue
}
dbOps.push(DBOp.del(DBTarget.Body, { blockHash, blockNumber }))
if (headers) {
dbOps.push(DBOp.del(DBTarget.Header, { blockHash, blockNumber }))
}
const receiptsKey = concatBytes(intToBytes(DBKey.Receipts), blockHash)
metaDBOps.push({
type: 'del',
key: receiptsKey,
})
blockNumber--
}
const convertedOps: BatchDBOp[] = dbOps.map((op) => {
const convertedOp = {
key: op.baseDBOp.key,
type: 'del',
opts: {
keyEncoding: op.baseDBOp.keyEncoding,
},
}
return convertedOp as DelBatch
})
await chainDB.batch(convertedOps)
await metaDB.batch(metaDBOps, { keyEncoding: 'view' })
}

Check warning on line 73 in packages/client/src/util/purge.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/util/purge.ts#L38-L73

Added lines #L38 - L73 were not covered by tests
Loading