Skip to content

Commit 1519aa5

Browse files
committed
[DHT] Add re-announce map to prevent duplicates within the hour
1 parent 9f9c703 commit 1519aa5

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/dht/mutable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ std::string CMutableData::Value() const
6767

6868
std::string CMutableData::ToString() const
6969
{
70-
return strprintf("CMutableData(InfoHash = %s\n, PublicKey = %s\n, Salt = %s\n, Signature = %s\n, Value = %s)\n",
71-
InfoHash(), PublicKey(), Salt(), Signature(), Value());
70+
return strprintf("CMutableData(\nInfoHash = %s\n, PublicKey = %s\n, Salt = %s\n, Seq = %d\n, Signature = %s\n, Value = %s)\n",
71+
InfoHash(), PublicKey(), Salt(), SequenceNumber, Signature(), Value());
7272
}

src/dht/session.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ static SessionThreadGroup arraySessions;
5454
static std::shared_ptr<std::thread> pDHTTorrentThread;
5555
static std::shared_ptr<boost::thread> pReannounceThread = nullptr;
5656
static std::map<HashRecordKey, uint32_t> mPutCommands;
57+
// <InfoHash , pair<seq , epoch>
58+
static std::map<std::string, std::pair<int64_t, int64_t>> mReannouceInfoHashes;
5759
static uint64_t nPutRecords = 0;
5860
static uint64_t nPutPieces = 0;
5961
static uint64_t nPutBytes = 0;
@@ -220,12 +222,30 @@ void ReannounceEntries()
220222
if (InitMemoryMap()) {
221223
try {
222224
while (fReannounceStarted) {
225+
// cleanup mReannouceInfoHashes map, make sure it doesn't get too big.
223226
MilliSleep(nReannouceSleepMilliSleep);
224227
boost::this_thread::interruption_point();
225228
CMutableData randomMutableItem;
226229
// select one local item at random.
227230
if (SelectRandomMutableItem(randomMutableItem)) {
228231
// Check if already re-annouced item
232+
int64_t nCurrentTime = GetTime();
233+
std::map<std::string, std::pair<int64_t, int64_t>>::iterator it = mReannouceInfoHashes.find(randomMutableItem.InfoHash());
234+
if (it == mReannouceInfoHashes.end()) {
235+
mReannouceInfoHashes[randomMutableItem.InfoHash()] = std::make_pair(randomMutableItem.SequenceNumber, nCurrentTime);
236+
} else {
237+
// check if we have a higher sequence number
238+
if (randomMutableItem.SequenceNumber > it->second.first) {
239+
mReannouceInfoHashes[randomMutableItem.InfoHash()] = std::make_pair(randomMutableItem.SequenceNumber, nCurrentTime);
240+
// check if we re-annouced over an hour ago
241+
} else if (nCurrentTime - it->second.second > (60 * 60)) {
242+
mReannouceInfoHashes[randomMutableItem.InfoHash()] = std::make_pair(randomMutableItem.SequenceNumber, nCurrentTime);
243+
} else {
244+
// already re-annouced entry within the hour with the same sequence number. Do not re-annouce entry.
245+
continue;
246+
}
247+
}
248+
// TODO: Remove debug.log printing below.
229249
// Check if fewer than 8 nodes returned the item with the most recent sequence number
230250
LogPrintf("%s -- Re-annoucing item %s\n", __func__, randomMutableItem.ToString());
231251
libtorrent::dht::item mut_item;

0 commit comments

Comments
 (0)