This commit is contained in:
Raven Scott
2026-05-21 02:00:00 -04:00
parent 8971182b3f
commit 3b75e74dbd
107 changed files with 1060 additions and 211 deletions
@@ -3,6 +3,7 @@ const EventEmitter = require('bare-events')
const b4a = require('b4a')
const crypto = require('hypercore-crypto')
const { assertCore } = require('../../_shared/storage-gossip-base.js')
const { coreStats } = require('../../_shared/hypercore-base.js')
const PROTOCOL = 'core-audit-chain/v1'
function hashEntry (prevHash, event) {
@@ -58,6 +59,19 @@ class HyperP2PCoreAuditChain extends EventEmitter {
return { ok: true, length: this._chain.length, head: this._headHash }
}
chainLength () {
return this._chain.length
}
headEntry () {
return this._chain.length ? { ...this._chain[this._chain.length - 1] } : null
}
findByType (type) {
if (type == null || type === '') throw new Error('type required')
return this._chain.filter((e) => e.type === type).map((e) => ({ ...e }))
}
tail (n = 10) {
if (n < 0) throw new Error('n must be non-negative')
if (n === 0) return []
@@ -80,13 +94,11 @@ class HyperP2PCoreAuditChain extends EventEmitter {
}
getStats () {
return {
...this._stats,
return coreStats(this._stats, PROTOCOL, {
length: this._chain.length,
head: this._headHash,
coreLength: this.core ? this.core.length : 0,
protocol: PROTOCOL
}
coreLength: this.core ? this.core.length : 0
})
}
async ready () { return this }
@@ -1,6 +1,6 @@
{
"name": "hyper-p2p-core-audit-chain",
"version": "0.3.1",
"version": "0.3.2",
"description": "Append-only audit chain on cores.",
"main": "index.js",
"type": "commonjs",
@@ -29,12 +29,23 @@ test('validation', async (t) => {
await m.close()
})
test('headEntry findByType chainLength', async (t) => {
const m = new HyperP2PCoreAuditChain()
m.appendAudit({ type: 'a', n: 1 })
m.appendAudit({ type: 'b', n: 2 })
t.is(m.chainLength(), 2)
t.is(m.headEntry().type, 'b')
t.is(m.findByType('a').length, 1)
await m.close()
})
test('getStats', async (t) => {
const m = new HyperP2PCoreAuditChain()
m.appendAudit({ x: 1 })
const s = m.getStats()
t.is(s.protocol, 'core-audit-chain/v1')
t.is(s.length, 1)
t.is(s.mode, 'hypercore')
await m.close()
})