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

add feed.audit #180

Merged
merged 1 commit into from
Nov 7, 2018
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
add feed.audit
  • Loading branch information
mafintosh committed Nov 6, 2018
commit 4790cf98bc60dcb0d971c55d6ab8d6fb6b136524
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,22 @@ Fully close this feed.

Calls the callback with `(err)` when all storage has been closed.

#### `feed.audit([callback])`

Audit all data in the feed. Will check that all current data stored
matches the hashes in the merkle tree and clear the bitfield if not.

When done a report is passed to the callback that looks like this:

```js
{
valid: 10, // how many data blocks matches the hashes
invalid: 0, // how many did not
}
```

If a block does not match the hash it is cleared from the data bitfield.

#### `feed.on('ready')`

Emitted when the feed is ready and all properties have been populated.
Expand Down
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,52 @@ Feed.prototype._roots = function (index, cb) {
}
}

Feed.prototype.audit = function (cb) {
if (!cb) cb = noop

var self = this
var report = {
valid: 0,
invalid: 0
}

this.ready(function (err) {
if (err) return cb(err)

var block = 0
var max = self.length

next()

function onnode (err, node) {
if (err) return ondata(null, null)
self._storage.getData(block, ondata)

function ondata (_, data) {
var verified = data && crypto.data(data).equals(node.hash)
if (verified) report.valid++
else report.invalid++
self.bitfield.set(block, verified)
block++
next()
}
}

function next () {
while (block < max && !self.bitfield.get(block)) block++
if (block >= max) return done()
self._storage.getNode(2 * block, onnode)
}

function done () {
self._sync(null, function (err) {
if (err) return cb(err)
cb(null, report)
})
}
})
}

function noop () {}

function verifyNode (trusted, node) {
Expand Down
36 changes: 36 additions & 0 deletions test/audit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var tape = require('tape')
var create = require('./helpers/create')

tape('basic audit', function (t) {
var feed = create()

feed.append('hello')
feed.append('world', function () {
feed.audit(function (err, report) {
t.error(err, 'no error')
t.same(report, { valid: 2, invalid: 0 })
t.end()
})
})
})

tape('basic audit with bad data', function (t) {
var feed = create()

feed.append('hello')
feed.append('world', function () {
feed._storage.data.write(0, Buffer.from('H'), function () {
t.ok(feed.has(0))
feed.audit(function (err, report) {
t.error(err, 'no error')
t.same(report, { valid: 1, invalid: 1 })
t.notOk(feed.has(0))
feed.audit(function (err, report) {
t.error(err, 'no error')
t.same(report, { valid: 1, invalid: 0 })
t.end()
})
})
})
})
})