121 lines
4.1 KiB
TypeScript
121 lines
4.1 KiB
TypeScript
/**
|
|
* Integration Guide: Swapping WebSocket for Braid-HTTP
|
|
*
|
|
* This shows the minimal changes needed in consensus-service/src/consensus/service.ts
|
|
* to use Braid-HTTP transport instead of WebSocket.
|
|
*
|
|
* Changes needed:
|
|
* 1. Replace PeerManager with BraidPeerManager
|
|
* 2. Replace StateSync with BraidStateSync
|
|
* 3. Mount Braid router on Express app
|
|
* 4. Add BraidStatePublisher for Simpleton clients
|
|
* 5. Call notifyChange() after document mutations
|
|
*
|
|
* Everything else (ConsensusNode, ConsensusDocument, RollupAggregator)
|
|
* stays exactly the same.
|
|
*/
|
|
|
|
// ============================================================
|
|
// BEFORE (WebSocket-based) — in consensus/service.ts
|
|
// ============================================================
|
|
//
|
|
// import { PeerManager } from '../sync/peer-manager.js';
|
|
// import { StateSync } from '../sync/state-sync.js';
|
|
//
|
|
// // In start():
|
|
// this.peerManager = new PeerManager({
|
|
// nodeId, publicKey, privateKey, host, port, redisUrl,
|
|
// });
|
|
// await this.peerManager.start(); // starts WebSocket server on port+1000
|
|
//
|
|
// this.stateSync = new StateSync(this.document, this.peerManager);
|
|
// this.stateSync.start(); // starts polling timer
|
|
|
|
// ============================================================
|
|
// AFTER (Braid-HTTP) — in consensus/service.ts
|
|
// ============================================================
|
|
|
|
import express from 'express';
|
|
import {
|
|
BraidPeerManager,
|
|
BraidStateSync,
|
|
BraidStatePublisher,
|
|
} from '@rspace/braid-transport';
|
|
|
|
// --- In constructor or start() ---
|
|
|
|
const app = express();
|
|
app.use(express.json());
|
|
|
|
// 1. Create Braid peer manager (replaces WebSocket PeerManager)
|
|
const braidPeerManager = new BraidPeerManager({
|
|
nodeId: 'node-1',
|
|
publicKey: '...',
|
|
privateKey: new Uint8Array([]),
|
|
host: 'consensus-service',
|
|
port: 3007,
|
|
redisUrl: 'redis://redis:6379',
|
|
});
|
|
|
|
// 2. Mount Braid routes on Express (no separate WebSocket server!)
|
|
app.use(braidPeerManager.createRouter());
|
|
|
|
// 3. Create push-based state sync (replaces polling StateSync)
|
|
const document = null as any; // your ConsensusDocument
|
|
const braidStateSync = new BraidStateSync(document, braidPeerManager);
|
|
|
|
// 4. Create state publisher for Simpleton light clients
|
|
const statePublisher = new BraidStatePublisher(document);
|
|
app.use(statePublisher.createRouter());
|
|
|
|
// 5. Start everything
|
|
await braidPeerManager.start();
|
|
braidStateSync.start();
|
|
|
|
// 6. After any document mutation, notify both sync systems:
|
|
// braidStateSync.notifyChange(); // push to peer nodes
|
|
// statePublisher.notifyChange(); // push to simpleton clients
|
|
|
|
// --- Wire up consensus events (same as before) ---
|
|
// braidPeerManager.on('message:propose', ...) — same handlers
|
|
// braidPeerManager.on('message:vote', ...) — same handlers
|
|
// braidPeerManager.on('message:commit', ...) — same handlers
|
|
// braidPeerManager.on('peer:connected', ...) — same handlers
|
|
// braidPeerManager.on('peer:disconnected', ...) — same handlers
|
|
|
|
// ============================================================
|
|
// Simpleton Client Usage (e.g., in wallet-service)
|
|
// ============================================================
|
|
|
|
import { SimpletonClient } from '@rspace/braid-transport';
|
|
|
|
async function walletServiceExample() {
|
|
// Create a light client — no Automerge dependency needed!
|
|
const client = new SimpletonClient({
|
|
endpoint: 'http://consensus-service:3007',
|
|
});
|
|
|
|
// Watch specific addresses for balance changes
|
|
client.watchAddress('0x1234...');
|
|
|
|
client.on('balance:changed', (address, balances) => {
|
|
console.log(`Balance changed for ${address}:`, balances);
|
|
});
|
|
|
|
client.on('state:updated', (state) => {
|
|
console.log('State version:', state.version);
|
|
console.log('Wallet count:', Object.keys(state.wallets).length);
|
|
});
|
|
|
|
// Connect — opens Braid-HTTP subscription
|
|
await client.connect();
|
|
|
|
// One-shot balance query (doesn't need subscription)
|
|
const balance = await client.fetchBalance('0x1234...');
|
|
console.log('Balance:', balance);
|
|
|
|
// Read from cached state (instant, no network call)
|
|
const cachedBalance = client.getBalance('0x1234...', '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913');
|
|
console.log('Cached USDC balance:', cachedBalance);
|
|
}
|