|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | +<title>bitcoin-kernel/browser-node — mesh: browser↔browser block propagation</title> |
| 7 | +<style> |
| 8 | + :root { --bg:#fff; --fg:#16181d; --mut:#5b6470; --bd:#e6e8eb; --pan:#fafbfc; --ac:#e8830c; --ac2:#0969da; --good:#1a7f37; --bad:#cf222e; --mono:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace; color-scheme:light; } |
| 9 | + body { background:var(--bg); color:var(--fg); font:14px/1.5 var(--mono); max-width:760px; margin:24px auto; padding:0 16px; } |
| 10 | + h1 { font-size:18px; } a { color:var(--ac2); } |
| 11 | + button { background:var(--pan); color:var(--fg); border:1px solid var(--bd); border-radius:6px; padding:9px 16px; cursor:pointer; font:inherit; } |
| 12 | + button:hover { background:#f0f2f4; border-color:var(--ac); } button:disabled { opacity:.45; cursor:default; } |
| 13 | + .grid { display:grid; grid-template-columns:repeat(3,1fr); gap:10px; margin:14px 0; } |
| 14 | + .card { border:1px solid var(--bd); border-radius:8px; padding:10px 12px; background:var(--pan); } |
| 15 | + .card .k { color:var(--mut); font-size:12px; text-transform:uppercase; letter-spacing:.04em; } |
| 16 | + .card .v { font-size:20px; color:var(--fg); margin-top:3px; } |
| 17 | + .card .s { color:var(--mut); font-size:12px; } |
| 18 | + #log { background:var(--pan); border:1px solid var(--bd); border-radius:6px; padding:12px; height:200px; overflow:auto; white-space:pre-wrap; margin-top:12px; } |
| 19 | + .ok{color:var(--good)} .warn{color:var(--ac)} .err{color:var(--bad)} .dim{color:var(--mut)} |
| 20 | + .dot{ display:inline-block; width:9px; height:9px; border-radius:50%; background:var(--mut); margin-right:6px; vertical-align:middle; } |
| 21 | + .dot.live{ background:var(--good); animation:pulse 2s ease-in-out infinite; } |
| 22 | + @keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.35} } |
| 23 | +</style> |
| 24 | +</head> |
| 25 | +<body> |
| 26 | +<h1>bitcoin-kernel / browser-node — mesh <span class="dim">(block propagation · PoC)</span></h1> |
| 27 | +<p class="dim"><b>Open this page in two tabs</b> (or send the link to a friend). The two tabs become Bitcoin peers to each other |
| 28 | +over <b>WebRTC — no server in the data path</b> — meeting in a room on a signaling server. Then one tab <b>relays a block</b> to |
| 29 | +the other: it announces it (<code>inv</code>), the other requests it (<code>getdata</code>), receives the block, and |
| 30 | +<b>validates it</b> (proof-of-work + merkle root). That's block propagation, in a browser mesh. |
| 31 | +<a href="node.html">the node</a> · <a href="how-it-works.html">how it works</a></p> |
| 32 | + |
| 33 | +<p> |
| 34 | + <button id="connect">▶ Connect to the mesh</button> |
| 35 | + <button id="share" disabled>📤 Relay block #26,000 to the peer</button> |
| 36 | + <span id="status" class="dim">idle</span> |
| 37 | +</p> |
| 38 | + |
| 39 | +<div class="grid"> |
| 40 | + <div class="card"><div class="k"><span class="dot" id="dot"></span>mesh</div><div class="v" id="mesh">—</div><div class="s" id="role">browser ↔ browser</div></div> |
| 41 | + <div class="card"><div class="k">blocks relayed</div><div class="v" id="relayed">0</div><div class="s">inv → getdata → block</div></div> |
| 42 | + <div class="card"><div class="k">last block validated</div><div class="v" id="last">—</div><div class="s" id="lasthash">PoW + merkle</div></div> |
| 43 | +</div> |
| 44 | +<div id="log"></div> |
| 45 | + |
| 46 | +<p class="dim" style="margin-top:14px; border-top:1px solid var(--bd); padding-top:10px"> |
| 47 | +PoC scope: the relayed block is a bundled sample (<code>data/block-26000.hex</code>), validated for structure + proof-of-work |
| 48 | +(no UTXO set, so not full-consensus). Next steps: source the block live from the network (via the bridge), relay across more |
| 49 | +than two peers, and stream the full chain. Default room <code>c0ffeebabe123456</code>; override with |
| 50 | +<code>?room=<hex></code> and the signaling server with <code>?signal=wss://your.pod/.webrtc</code>.</p> |
| 51 | + |
| 52 | +<script type="module"> |
| 53 | +import { Codec } from './engine/codec/codec.js'; |
| 54 | +import { P2pEngine } from './engine/codec/p2p.js'; |
| 55 | +import { BlockEngine } from './engine/codec/blocks.js'; |
| 56 | +import { MeshPeer } from './peer-mesh.js'; |
| 57 | + |
| 58 | +const $ = (id) => document.getElementById(id); |
| 59 | +const log = (m, c='') => { const t = new Date().toISOString().slice(11,19); $('log').innerHTML += `<span class="dim">${t}</span> <span class="${c}">${m}</span>\n`; $('log').scrollTop = $('log').scrollHeight; }; |
| 60 | +const fmt = (n) => Number(n).toLocaleString(); |
| 61 | +const setStatus = (m, c='') => $('status').innerHTML = `<span class="${c}">${m}</span>`; |
| 62 | +const MSG_BLOCK = 2; |
| 63 | + |
| 64 | +const q = new URLSearchParams(location.search); |
| 65 | +const signalUrl = q.get('signal') || 'wss://melvincarvalho.com/.webrtc'; |
| 66 | +const room = q.get('room') || 'c0ffeebabe123456'; // mesh's own room (distinct from the bridge's room) |
| 67 | + |
| 68 | +let codec, p2p, be, peer = null; |
| 69 | +const have = new Map(); // blockHash -> hex (blocks we can serve) |
| 70 | +let relayed = 0; |
| 71 | + |
| 72 | +async function loadEngine() { |
| 73 | + const jl = (n) => fetch(`./engine/schema/${n}.jsonld`).then(r => r.json()); |
| 74 | + const [core, proof, p2pS, chain, validate, script] = await Promise.all(['core','proof','p2p','chain','validate','script'].map(jl)); |
| 75 | + codec = new Codec(core, proof, p2pS); |
| 76 | + p2p = P2pEngine.fromSchemas(codec, p2pS, chain, 'btc:testnet4'); |
| 77 | + be = BlockEngine.fromSchemas(codec, chain, validate, script, 'btc:testnet4'); |
| 78 | +} |
| 79 | + |
| 80 | +// Handle a Bitcoin message arriving from the mesh peer. |
| 81 | +function onMessage(msg) { |
| 82 | + if (msg.command === 'inv') { |
| 83 | + for (const it of msg.payload?.items ?? []) { |
| 84 | + if (!have.has(it.hash)) { log(`← inv: peer has block ${it.hash.slice(0,16)}… → requesting (getdata)`); peer.send('getdata', { items: [{ type: MSG_BLOCK, hash: it.hash }] }); } |
| 85 | + } |
| 86 | + } else if (msg.command === 'getdata') { |
| 87 | + for (const it of msg.payload?.items ?? []) { |
| 88 | + const hex = have.get(it.hash); |
| 89 | + if (hex) { log(`← getdata for ${it.hash.slice(0,16)}… → sending the block`); peer.send('block', hex); } |
| 90 | + } |
| 91 | + } else if (msg.command === 'block') { |
| 92 | + const block = msg.payload; |
| 93 | + const hash = codec.blockHash(block.header); |
| 94 | + const failed = be.validateBlockStructure(block).results.filter(r => r.ok === false).map(r => r.rule); |
| 95 | + if (failed.length) { log(`← block ${hash.slice(0,16)}… ✗ INVALID: ${failed.join(', ')}`, 'err'); return; } |
| 96 | + const reHex = Array.from(codec.encode('Block', block), (b) => b.toString(16).padStart(2, '0')).join(''); |
| 97 | + have.set(hash, reHex); // now we hold it too — could relay onward (mesh) |
| 98 | + relayed++; $('relayed').textContent = fmt(relayed); |
| 99 | + $('last').textContent = '#26,000'; $('lasthash').textContent = hash.slice(0, 22) + '…'; |
| 100 | + log(`✅ received + validated block ${hash.slice(0,16)}… (${block.transactions.length} txs, PoW + merkle ✓) — from a browser peer, no server`, 'ok'); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +$('connect').onclick = async () => { |
| 105 | + $('connect').disabled = true; |
| 106 | + try { |
| 107 | + setStatus('loading engine…', 'warn'); |
| 108 | + if (!be) await loadEngine(); |
| 109 | + setStatus(`joining the mesh room ${room.slice(0,10)}…`, 'warn'); |
| 110 | + log(`connecting to the mesh (signaling ${signalUrl})…`); |
| 111 | + peer = new MeshPeer(p2p, codec, { log, onMessage, onOpen: (role) => { |
| 112 | + $('dot').classList.add('live'); $('mesh').textContent = 'connected'; |
| 113 | + $('role').textContent = `you are the ${role}`; |
| 114 | + $('share').disabled = false; |
| 115 | + setStatus('● connected to a peer — relay a block', 'ok'); |
| 116 | + log(`mesh connected (you are the ${role}) — a direct browser↔browser channel, no server in the path`, 'ok'); |
| 117 | + } }); |
| 118 | + await peer.connect(signalUrl, { room }); |
| 119 | + } catch (e) { |
| 120 | + setStatus('mesh error: ' + e.message, 'err'); |
| 121 | + log('mesh: ' + e.message + ' — is the page open in a second tab? (one tab waits for the other)', 'err'); |
| 122 | + $('connect').disabled = false; |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +$('share').onclick = async () => { |
| 127 | + try { |
| 128 | + const hex = (await (await fetch('./data/block-26000.hex')).text()).trim(); |
| 129 | + const block = codec.decode('Block', hex); |
| 130 | + const hash = codec.blockHash(block.header); |
| 131 | + have.set(hash, hex); |
| 132 | + log(`announcing block #26,000 ${hash.slice(0,16)}… to the peer (inv)`, 'ok'); |
| 133 | + peer.send('inv', { items: [{ type: MSG_BLOCK, hash }] }); |
| 134 | + } catch (e) { log('share failed: ' + e.message, 'err'); } |
| 135 | +}; |
| 136 | + |
| 137 | +log(`idle — press ▶ Connect, then open this page in a second tab. Room ${room.slice(0,10)}… via ${signalUrl}.`, 'dim'); |
| 138 | +</script> |
| 139 | +</body> |
| 140 | +</html> |
0 commit comments