Skip to content

Commit

Permalink
fix: Prevent hyperswarm gossip storm (#459)
Browse files Browse the repository at this point in the history
* Re-added batch hash cache

* Removed debug logs

* Fix for node names
  • Loading branch information
macterra authored Dec 3, 2024
1 parent 70d7083 commit 75a04ab
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions services/mediators/hyperswarm/src/hyperswarm-mediator.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,20 @@ let exportQueue = asyncLib.queue(async function (task, callback) {
callback();
}, 1); // concurrency is 1


const batchesSeen = {};

function newBatch(batch) {
const hash = cipher.hashJSON(batch);

if (!batchesSeen[hash]) {
batchesSeen[hash] = true;
return true;
}

return false;
}

async function receiveMsg(conn, name, json) {
let msg;

Expand All @@ -304,19 +318,22 @@ async function receiveMsg(conn, name, json) {

console.log(`received ${msg.type} from: ${shortName(name)} (${msg.node || 'anon'})`);
connectionLastSeen[name] = new Date().getTime();
connectionNodeName[name] = msg.node || 'anon';

if (msg.type === 'batch') {
logBatch(msg.data, msg.node || 'anon');
importQueue.push({ name, msg });
if (newBatch(msg.data)) {
logBatch(msg.data, msg.node || 'anon');
importQueue.push({ name, msg });
}
return;
}

if (msg.type === 'queue') {
importQueue.push({ name, msg });
msg.relays.push(name);
logConnection(msg.relays[0]);
relayMsg(msg);
if (newBatch(msg.data)) {
importQueue.push({ name, msg });
msg.relays.push(name);
logConnection(msg.relays[0]);
relayMsg(msg);
}
return;
}

Expand All @@ -326,6 +343,7 @@ async function receiveMsg(conn, name, json) {
}

if (msg.type === 'ping') {
connectionNodeName[name] = msg.node || 'anon';
return;
}

Expand Down

0 comments on commit 75a04ab

Please sign in to comment.