Skip to content

Commit a38d807

Browse files
authored
fix: use car block validator (#75)
* fix: use car block validtor * chore: update car block validator
1 parent 5df6fcf commit a38d807

File tree

6 files changed

+240
-14
lines changed

6 files changed

+240
-14
lines changed

package-lock.json

+130-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@ipld/car": "4.1.6",
2626
"@ipld/dag-cbor": "7.0.3",
2727
"@ipld/dag-pb": "2.1.18",
28+
"@web3-storage/car-block-validator": "^1.0.1",
2829
"dag-jose": "1.0.0",
2930
"dotenv": "16.0.3",
3031
"hdr-histogram-js": "3.0.0",
@@ -43,7 +44,8 @@
4344
"eslint-plugin-promise": "6.1.0",
4445
"pino-pretty": "9.1.1",
4546
"prettier": "2.7.1",
46-
"tap": "16.3.0"
47+
"tap": "16.3.0",
48+
"uint8arrays": "3.1.1"
4749
},
4850
"engines": {
4951
"node": "16"

src/lib/block.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const { validateBlock } = require('@web3-storage/car-block-validator')
4+
35
const config = require('../config')
46
const { queuedTasks } = require('./util')
57
const { serializeError } = require('./logging')
@@ -66,6 +68,18 @@ async function writeBlocksBatch({ blocks, car, logger }) {
6668
block.key = cidToKey(block.cid)
6769
}
6870

71+
// Validate block
72+
try {
73+
await validateBlock({
74+
cid: block.cid,
75+
bytes: block.data
76+
})
77+
} catch (error) {
78+
logger.error({ error: serializeError(error) }, 'Error validating block')
79+
console.log('not valid block', error)
80+
continue
81+
}
82+
6983
// HOTFIX
7084
if (keys.includes(block.key)) {
7185
continue

src/lib/iterator.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ class CarIterator {
2020
const offset = (this.position = this.reader.pos)
2121
const { cid, length, blockLength } = await readBlockHead(this.reader)
2222
this.currentCid = cid
23-
// disabled decodeBlocks https://github.com/elastic-ipfs/indexer-lambda/pull/54#discussion_r913665164
24-
// const data = this.decodeBlocks && cid.code !== RAW_BLOCK_CODEC ? await this.reader.exactly(blockLength) : undefined
23+
const data = await this.reader.exactly(blockLength)
2524

2625
yield {
2726
cid,
2827
blockLength,
2928
blockOffset: this.reader.pos,
3029
offset,
3130
length,
32-
data: undefined
31+
data
3332
}
3433

3534
this.reader.seek(blockLength)

tap-snapshots/test/index.test.js.test.cjs

+60
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,66 @@ Object {
186186
}
187187
`
188188

189+
exports[`test/index.test.js TAP handler indexes a new car file but not invalid blocks > must match snapshot 1`] = `
190+
Object {
191+
"dynamo": Object {
192+
"batchCreates": Array [
193+
Object {
194+
"RequestItems": Object {
195+
"v1-blocks-cars-position": Array [],
196+
},
197+
},
198+
],
199+
"creates": Array [
200+
Object {
201+
"Item": Object {
202+
"bucket": Object {
203+
"S": "cars",
204+
},
205+
"bucketRegion": Object {
206+
"S": "us-east-2",
207+
},
208+
"createdAt": Object {
209+
"S": "2022-06-24T15:15:17.401Z",
210+
},
211+
"key": Object {
212+
"S": "file3.car",
213+
},
214+
"path": Object {
215+
"S": "us-east-2/cars/file3.car",
216+
},
217+
"roots": Object {
218+
"L": Array [
219+
Object {
220+
"S": "bafkreiadsbmmn4waznesyuz3bjgrj33xzqhxrk6mz3ksq7meugrachh3qe",
221+
},
222+
],
223+
},
224+
"version": Object {
225+
"N": "1",
226+
},
227+
},
228+
"TableName": "v1-cars",
229+
},
230+
],
231+
},
232+
"sqs": Object {
233+
"batchPublishes": Array [
234+
Object {
235+
"Entries": Array [
236+
Object {
237+
"Id": "zQmNaZbPKs9E11qawNuEs7mSW7KXQ7TQ6Y2X2ovcvE1sUJk",
238+
"MessageBody": "zQmNaZbPKs9E11qawNuEs7mSW7KXQ7TQ6Y2X2ovcvE1sUJk",
239+
},
240+
],
241+
"QueueUrl": "publishingQueue",
242+
},
243+
],
244+
"publishes": Array [],
245+
},
246+
}
247+
`
248+
189249
exports[`test/index.test.js TAP handler indexes a new car file with unsupported blocks > must match snapshot 1`] = `
190250
Object {
191251
"dynamo": Object {

0 commit comments

Comments
 (0)