Skip to content

Commit

Permalink
feat: starting tipWatcher when peer height is heigher then local height
Browse files Browse the repository at this point in the history
  • Loading branch information
silkroadnomad committed Jan 21, 2025
1 parent af4edd0 commit 103f4ce
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion relay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libp2p-relay",
"version": "0.12.37",
"version": "0.12.39",
"private": true,
"scripts": {
"start:no-restart": "node src/relay.js",
Expand Down
1 change: 0 additions & 1 deletion relay/src/pinner/tipWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { EventEmitter } from 'events';
import logger from '../logger.js';
import fs from 'fs';
import path from 'path';
import moment from 'moment/moment.js';

class TipWatcher extends EventEmitter {
constructor(electrumClient) {
Expand Down
19 changes: 16 additions & 3 deletions relay/src/pubsubHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function setupPubsub(
orbitdb,
electrumClient,
fsHelia,
contentTopic
contentTopic,
tipWatcher
) {
const pinningService = new PinningService(helia, orbitdb, electrumClient);
helia.libp2p.services.pubsub.subscribe(contentTopic);
Expand All @@ -47,8 +48,20 @@ export function setupPubsub(
console.log('Received message:', messageObject);
if (messageObject.type == 'LIST') {
console.log('Received LIST request:', messageObject);
const { dateString, pageSize, from, filter } = messageObject;
const pageSizeValue = parseInt(pageSize, 10) || 10; // Default to 100 if not specified
const { dateString, pageSize, from, filter, height } = messageObject;
const pageSizeValue = parseInt(pageSize, 10) || 10;

// Check if peer height is higher than our current tip
if (height) {
console.log("height", height)
const currentTip = tipWatcher.getCurrentTip();
console.log("currentTip", currentTip)
if (currentTip && height > currentTip.height) {
logger.info(`Peer height ${height} is higher than our tip ${currentTip.height}, requesting new blocks`);
tipWatcher.handleNewTip({ height });
}
}

await handleListRequest(
dateString,
pageSizeValue,
Expand Down
5 changes: 3 additions & 2 deletions relay/src/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,16 @@ const { helia, orbitdb, electrumClient } = await createNode(
logger.info('Helia and OrbitDB are running');
const fsHelia = unixfs(helia);

const tipWatcher = new TipWatcher(electrumClient);

setupPubsub(
helia,
orbitdb,
electrumClient,
fsHelia,
CONTENT_TOPIC
CONTENT_TOPIC,
tipWatcher
);
const tipWatcher = new TipWatcher(electrumClient);
createHttpServer(helia, orbitdb, electrumClient, tipWatcher);

if (!argv['disable-scanning']) {
Expand Down

0 comments on commit 103f4ce

Please sign in to comment.