*Add timestamp checking to not store old data

This commit is contained in:
Derrick Hammer 2022-11-19 20:34:57 -05:00
parent 78f3edd71d
commit 15b088f481
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 17 additions and 5 deletions

View File

@ -78,6 +78,7 @@ export default class DHTDataBase extends EventEmitter {
getPeerData(id: Buffer | string) {
return this.getPeerField(id, "data");
}
getPeerTimestamp(id: Buffer | string) {
return this.getPeerField(id, "timestamp");
}
@ -159,12 +160,18 @@ export default class DHTDataBase extends EventEmitter {
signature &&
crypto.verify(b4a.from(`${timestamp}${rawData}`), signature, id)
) {
if ((timestamp || 0) <= this.getPeerTimestamp(id)) {
debug(`Received old data for peer ${id}`);
return;
}
const data = rawData ? orderedJSON.parse(rawData) : null;
this._setPeer(id, data, timestamp, signature);
this.emit("peer-data", data, id);
this._recalculate();
return;
}
debug(`Invalid signature received for peer ${id}`);
} else if (type === Type.CONNECTED) {
const { id: toId } = decoded;
@ -281,11 +288,16 @@ export default class DHTDataBase extends EventEmitter {
// If we're already tracking them
if (this._hasSeenPeer(id)) {
// See what data we already have for them
// Add their existing data to what we got from the bootstrap
const existingPeerData = this.getPeerData(id);
peerData = { ...existingPeerData, ...peerData };
this._setPeer(id, peerData, timestamp, signature);
// Ensure we don't have old data
if ((timestamp || 0) > this.getPeerTimestamp(id)) {
// See what data we already have for them
// Add their existing data to what we got from the bootstrap
const existingPeerData = this.getPeerData(id);
peerData = { ...existingPeerData, ...peerData };
this._setPeer(id, peerData, timestamp, signature);
} else {
debug(`Received old data for peer ${id}`);
}
}
} else {
debug(`Invalid signature received for peer ${id}`);