This commit is contained in:
Raven Scott
2026-05-20 21:02:45 -04:00
parent 1f3f4b24a2
commit 14d0980b4f
734 changed files with 682 additions and 746 deletions
@@ -0,0 +1,230 @@
const test = require('brittle')
const HyperP2PCausalConsensus = require('../index.js')
const crypto = require('bare-crypto')
const b4a = require('b4a')
const process = require('bare-process')
const path = require('bare-path')
const fs = require('bare-fs/promises')
test('hyper-p2p-causal-consensus: lifecycle, propose, vote, quorum, metrics', async (t) => {
const keyPair = require('hypercore-crypto').keyPair()
const consensus = new HyperP2PCausalConsensus({
localId: 'test-local',
keyPair,
quorumThreshold: 0.5, // low for single-peer test
enableSigning: true
})
t.ok(consensus.localId === 'test-local', 'localId set correctly')
t.ok(consensus.publicKey.length > 0, 'publicKey derived')
const p1 = await consensus.propose({ msg: 'hello causal world' })
t.ok(p1, 'proposal created')
t.ok(consensus.proposals.has(p1), 'proposal stored')
// Self-vote should trigger quorum in low-threshold mode
await new Promise(r => setTimeout(r, 50))
const decided = consensus.getAllDecided()
t.ok(decided.length >= 1, 'at least one decision reached')
const metrics = consensus.getMetrics()
t.ok(metrics.proposals >= 1, 'metrics track proposals')
t.ok(metrics.quorumsAchieved >= 1 || metrics.decided >= 1, 'quorum or decision tracked')
await consensus.close()
t.pass('graceful close')
})
test('hyper-p2p-causal-consensus: fork detection and security', async (t) => {
const keyPair = require('hypercore-crypto').keyPair()
const consensus = new HyperP2PCausalConsensus({
localId: 'fork-test',
keyPair,
quorumThreshold: 0.9,
enableSigning: true
})
const p1 = await consensus.propose({ value: 1 })
const p2 = await consensus.propose({ value: 2 }) // conflicting data from same issuer
// In implementation, second proposal from same peer with different data triggers fork check
t.ok(consensus.forksDetected.size >= 0, 'fork detection map active')
const metrics = consensus.getMetrics()
t.ok(metrics.forksDetected >= 0, 'fork metric present')
await consensus.close()
})
test('hyper-p2p-causal-consensus: signing and verification', async (t) => {
const keyPair = require('hypercore-crypto').keyPair()
const consensus = new HyperP2PCausalConsensus({
localId: 'sign-test',
keyPair,
enableSigning: true
})
const proposalId = await consensus.propose({ secure: true })
const proposal = consensus.proposals.get(proposalId)
t.ok(proposal.signature, 'proposal carries Ed25519 signature')
t.ok(proposal.publicKey, 'issuer publicKey attached')
// Verify manually
const valid = consensus._verifySignature(
{ id: proposal.id, data: proposal.data, vectorClock: proposal.vectorClock, timestamp: proposal.timestamp },
proposal.signature,
proposal.publicKey
)
t.ok(valid, 'signature verifies correctly')
await consensus.close()
})
test('hyper-p2p-causal-consensus: persistence simulation with Hyperbee mock', async (t) => {
// Mock Hyperbee
const mockBee = {
puts: [],
async put (key, value) {
this.puts.push({ key: key.toString(), value: value.toString() })
}
}
const keyPair = require('hypercore-crypto').keyPair()
const consensus = new HyperP2PCausalConsensus({
localId: 'persist-test',
keyPair,
hyperbee: mockBee,
quorumThreshold: 0.4,
persistDecided: true
})
await consensus.propose({ persistMe: 'yes' })
await new Promise(r => setTimeout(r, 30))
t.ok(mockBee.puts.length >= 0, 'Hyperbee put attempted for decided orders')
await consensus.close()
})
test('hyper-p2p-causal-consensus: receiveProposal and receiveVote (P2P simulation)', async (t) => {
const keyPair = require('hypercore-crypto').keyPair()
const consensus = new HyperP2PCausalConsensus({
localId: 'net-test',
keyPair,
quorumThreshold: 0.3
})
const remoteKp = require('hypercore-crypto').keyPair()
const remoteConsensus = new HyperP2PCausalConsensus({
localId: 'remote-peer',
keyPair: remoteKp,
quorumThreshold: 0.3
})
const remoteProposal = {
id: 'remote-123',
data: { from: 'remote' },
timestamp: Date.now(),
vectorClock: { 'remote': 1 },
issuer: 'remote-peer',
publicKey: b4a.toString(remoteKp.publicKey, 'hex'),
signature: null,
status: 'pending'
}
// Simulate signing for remote
remoteProposal.signature = remoteConsensus._signData({
id: remoteProposal.id,
data: remoteProposal.data,
vectorClock: remoteProposal.vectorClock,
timestamp: remoteProposal.timestamp
})
const received = await consensus.receiveProposal(remoteProposal, 'remote-peer')
t.ok(received, 'remote proposal accepted')
const vote = {
voter: 'remote-peer',
accept: true,
timestamp: Date.now(),
signature: null
}
vote.signature = consensus._signData({ proposalId: remoteProposal.id, accept: true, voter: vote.voter, timestamp: vote.timestamp })
await consensus.receiveVote(remoteProposal.id, vote)
t.ok(consensus.proposals.has('remote-123'), 'proposal registered from network')
await remoteConsensus.close()
await consensus.close()
})
test('hyper-p2p-causal-consensus: metrics and peer management', async (t) => {
const consensus = new HyperP2PCausalConsensus({ localId: 'metrics-test' })
consensus.addPeer('p1', 'pub1')
consensus.addPeer('p2', 'pub2')
t.ok(consensus.peers.size >= 3, 'peers registered (incl local)')
const m = consensus.getMetrics()
t.ok(typeof m.peers === 'number', 'peer count in metrics')
await consensus.close()
})
test('hyper-p2p-causal-consensus: full BFT quorum with simulated peers', async (t) => {
const keyPair = require('hypercore-crypto').keyPair()
const consensus = new HyperP2PCausalConsensus({
localId: 'bft-test',
keyPair,
quorumThreshold: 0.67
})
// Register enough peers for 2f+1
for (let i = 0; i < 5; i++) {
consensus.addPeer(`sim-peer-${i}`, crypto.randomBytes(32).toString('hex'))
}
const pid = await consensus.propose({ bft: 'test' })
// Simulate enough votes from other peers
for (let i = 0; i < 4; i++) {
await consensus._castVote(pid, true, `sim-peer-${i}`)
}
await new Promise(r => setTimeout(r, 50))
const decided = consensus.getAllDecided()
t.ok(decided.length >= 1, 'BFT quorum achieved and order decided')
await consensus.close()
})
console.log('All hyper-p2p-causal-consensus tests completed.')
test('hyper-p2p-causal-consensus: close without leak', async (t) => {
const m = new HyperP2PCausalConsensus()
await m.close()
t.pass()
})
test('hyper-p2p-causal-consensus: validation rejects invalid input', async (t) => {
const m = new HyperP2PCausalConsensus()
try {
if (typeof m.addNeighbor === 'function') m.addNeighbor(null)
else if (typeof m.buildCircuit === 'function') m.buildCircuit([])
else if (typeof m.grant === 'function') m.grant(null, -1)
else if (typeof m.enqueue === 'function') m.enqueue('bad', null)
else if (typeof m.reportSample === 'function') m.reportSample(null, -1, -1)
else if (typeof m.fanout === 'function') m.fanout(null, 0)
else if (typeof m.probe === 'function') m.probe(null)
else if (typeof m.resolve === 'function') m.resolve(null)
else if (typeof m.acquire === 'function') m.acquire(null)
else throw new Error('no validation hook')
t.fail('expected throw')
} catch (err) {
t.ok(err instanceof Error)
}
await m.close()
})