Implement novel hyper-p2p-causal-consensus BFT causal ordering primitive (v0.1.0) with full production code, tests, docs, examples, Mermaid architecture; update workspace README.md Active Modules + This Run section with research notes and roadmap; mandatory Bare builtin scan (10 modules compliant); continuous novel primitive development
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
const HyperP2PCausalConsensus = require('../index.js')
|
||||
const crypto = require('bare-crypto')
|
||||
const process = require('bare-process')
|
||||
|
||||
async function runExample () {
|
||||
console.log('=== hyper-p2p-causal-consensus Basic Usage Example ===')
|
||||
|
||||
const keyPair = crypto.keyPair()
|
||||
|
||||
const consensus = new HyperP2PCausalConsensus({
|
||||
localId: 'example-peer',
|
||||
keyPair,
|
||||
quorumThreshold: 0.6, // relaxed for demo (3 peers)
|
||||
enableSigning: true
|
||||
})
|
||||
|
||||
// Simulate adding peers for quorum (in real: discovered via Hyperswarm)
|
||||
consensus.addPeer('peer-alpha', crypto.keyPair().publicKey.toString('hex'))
|
||||
consensus.addPeer('peer-beta', crypto.keyPair().publicKey.toString('hex'))
|
||||
|
||||
// Listen for consensus decisions
|
||||
consensus.on('consensus', (decided) => {
|
||||
console.log(`[CONSENSUS] Order #${decided.order} decided:`, JSON.stringify(decided.data))
|
||||
console.log(' VectorClock:', decided.vectorClock)
|
||||
})
|
||||
|
||||
consensus.on('fork-detected', (info) => {
|
||||
console.warn('[SECURITY] Fork/equivocation detected:', info)
|
||||
})
|
||||
|
||||
// Propose first event
|
||||
const p1 = await consensus.propose({ type: 'user-action', payload: { user: 'alice', action: 'login' } })
|
||||
console.log('Proposed #1:', p1)
|
||||
|
||||
// Simulate remote votes (in real P2P these arrive via protomux)
|
||||
await consensus.vote(p1, true) // self already voted
|
||||
|
||||
// Second proposal
|
||||
const p2 = await consensus.propose({ type: 'state-update', payload: { balance: 100 } })
|
||||
console.log('Proposed #2:', p2)
|
||||
|
||||
await consensus.vote(p2, true)
|
||||
|
||||
// Wait briefly for any async finalization
|
||||
await new Promise(resolve => setTimeout(resolve, 100))
|
||||
|
||||
console.log('\n--- Metrics ---')
|
||||
console.log(consensus.getMetrics())
|
||||
|
||||
console.log('\n--- Decided Orders ---')
|
||||
console.log(consensus.getAllDecided())
|
||||
|
||||
await consensus.close()
|
||||
console.log('\nExample completed successfully. All decisions tamper-proof and causally ordered.')
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
runExample().catch(err => {
|
||||
console.error('Example failed:', err)
|
||||
process.exit(1)
|
||||
})
|
||||
Reference in New Issue
Block a user