@relay-federation/bridge 0.3.13 → 0.3.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -65,6 +65,16 @@ export class BSVNodeClient extends EventEmitter {
65
65
  // Track best height across all peers
66
66
  this._bestHeight = this._checkpoint.height
67
67
  this._bestHash = this._checkpoint.hash
68
+
69
+ // Shared header store — all peers reference these instead of creating their own
70
+ this._sharedHeaderHashes = new Map()
71
+ this._sharedHashToHeight = new Map()
72
+ this._sharedHeaderHashes.set(this._checkpoint.height, this._checkpoint.hash)
73
+ this._sharedHashToHeight.set(this._checkpoint.hash, this._checkpoint.height)
74
+ if (this._checkpoint.prevHash) {
75
+ this._sharedHeaderHashes.set(this._checkpoint.height - 1, this._checkpoint.prevHash)
76
+ this._sharedHashToHeight.set(this._checkpoint.prevHash, this._checkpoint.height - 1)
77
+ }
68
78
  }
69
79
 
70
80
  /**
@@ -231,7 +241,9 @@ export class BSVNodeClient extends EventEmitter {
231
241
  const peer = new BSVPeer({
232
242
  checkpoint: this._checkpoint,
233
243
  syncIntervalMs: this._syncIntervalMs,
234
- pingIntervalMs: this._pingIntervalMs
244
+ pingIntervalMs: this._pingIntervalMs,
245
+ headerHashes: this._sharedHeaderHashes,
246
+ hashToHeight: this._sharedHashToHeight
235
247
  })
236
248
 
237
249
  this._peers.set(host, peer)
package/lib/bsv-peer.js CHANGED
@@ -118,6 +118,8 @@ export class BSVPeer extends EventEmitter {
118
118
  * @param {{ height: number, hash: string, prevHash: string }} [opts.checkpoint]
119
119
  * @param {number} [opts.syncIntervalMs] — Header sync interval (default 30s)
120
120
  * @param {number} [opts.pingIntervalMs] — Keepalive ping interval (default 120s)
121
+ * @param {Map} [opts.headerHashes] — Shared height→hash Map (from BSVNodeClient)
122
+ * @param {Map} [opts.hashToHeight] — Shared hash→height Map (from BSVNodeClient)
121
123
  */
122
124
  constructor (opts = {}) {
123
125
  super()
@@ -136,13 +138,21 @@ export class BSVPeer extends EventEmitter {
136
138
  this._syncTimer = null
137
139
  this._pingTimer = null
138
140
 
139
- // Header tracking
141
+ // Header tracking — use shared Maps if provided, otherwise create own
140
142
  this._bestHeight = this._checkpoint.height
141
143
  this._bestHash = this._checkpoint.hash
142
- this._headerHashes = new Map()
143
- this._headerHashes.set(this._checkpoint.height, this._checkpoint.hash)
144
- if (this._checkpoint.prevHash) {
145
- this._headerHashes.set(this._checkpoint.height - 1, this._checkpoint.prevHash)
144
+ if (opts.headerHashes && opts.hashToHeight) {
145
+ this._headerHashes = opts.headerHashes
146
+ this._hashToHeight = opts.hashToHeight
147
+ } else {
148
+ this._headerHashes = new Map()
149
+ this._hashToHeight = new Map()
150
+ this._headerHashes.set(this._checkpoint.height, this._checkpoint.hash)
151
+ this._hashToHeight.set(this._checkpoint.hash, this._checkpoint.height)
152
+ if (this._checkpoint.prevHash) {
153
+ this._headerHashes.set(this._checkpoint.height - 1, this._checkpoint.prevHash)
154
+ this._hashToHeight.set(this._checkpoint.prevHash, this._checkpoint.height - 1)
155
+ }
146
156
  }
147
157
 
148
158
  // Peer info
@@ -248,6 +258,7 @@ export class BSVPeer extends EventEmitter {
248
258
  */
249
259
  seedHeader (height, hash) {
250
260
  this._headerHashes.set(height, hash)
261
+ this._hashToHeight.set(hash, height)
251
262
  if (height > this._bestHeight) {
252
263
  this._bestHeight = height
253
264
  this._bestHash = hash
@@ -509,13 +520,8 @@ export class BSVPeer extends EventEmitter {
509
520
  const blockHash = internalToHash(sha256d(rawHeader))
510
521
  const prevHash = internalToHash(prevHashBuf)
511
522
 
512
- let height = -1
513
- for (const [h, hash] of this._headerHashes) {
514
- if (hash === prevHash) {
515
- height = h + 1
516
- break
517
- }
518
- }
523
+ const prevHeight = this._hashToHeight.get(prevHash)
524
+ let height = prevHeight !== undefined ? prevHeight + 1 : -1
519
525
 
520
526
  if (height < 0) {
521
527
  offset += HEADER_BYTES
@@ -527,6 +533,7 @@ export class BSVPeer extends EventEmitter {
527
533
  }
528
534
 
529
535
  this._headerHashes.set(height, blockHash)
536
+ this._hashToHeight.set(blockHash, height)
530
537
  if (height > this._bestHeight) {
531
538
  this._bestHeight = height
532
539
  this._bestHash = blockHash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@relay-federation/bridge",
3
- "version": "0.3.13",
3
+ "version": "0.3.14",
4
4
  "description": "Bridge server — WebSocket peering, header sync, tx relay, CLI",
5
5
  "type": "module",
6
6
  "bin": {