Skip to content

Commit

Permalink
feat(cli): check block hash consistency when listing blocks (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw authored Dec 8, 2023
1 parent 0144a86 commit 273079f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
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())
}
}

0 comments on commit 273079f

Please sign in to comment.