Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): check block hash consistency when listing blocks #157

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ cli
cli
.command('blocks [car]')
.describe('List block CIDs from a CAR.')
.option('--verify', 'Verify block hash consistency.', true)
.action(createAction('./cmd/blocks.js'))

cli
Expand Down
22 changes: 16 additions & 6 deletions cmd/blocks.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import fs from 'fs'
import { CarCIDIterator } from '@ipld/car/iterator'
import { validateBlock } from '@web3-storage/car-block-validator'
import { CarBlockIterator } from '@ipld/car/iterator'

/** @param {string} carPath */
export default async function blocksList (carPath) {
const cids = await CarCIDIterator.fromIterable(carPath ? fs.createReadStream(carPath) : process.stdin)
for await (const cid of cids) {
console.log(cid.toString())
/**
* @param {string} carPath
* @param {object} [opts]
* @param {boolean} [opts.verify]
*/
export default async function blocksList (carPath, opts) {
const blocks = await CarBlockIterator.fromIterable(carPath ? fs.createReadStream(carPath) : process.stdin)
for await (const block of blocks) {
/* c8 ignore next */
if (opts?.verify || opts?.verify == null) {
// @ts-expect-error
await validateBlock(block)
}
console.log(block.cid.toString())
}
}
Loading