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