From 40bd639bd891a67b3c5b4f23ed6d0f69bbbf50e5 Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Tue, 15 Jun 2021 21:58:18 -0400 Subject: [PATCH] Read zdb on startup and serialize every 150s and on shutdown --- src/net.cpp | 35 +++++++++++++++++++++++++++++++++-- src/net.h | 1 + 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 808df788c..1dd1cce37 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -48,6 +48,9 @@ using namespace hush; // Satoshi originally used 10 seconds(!), did they know something Peter Wuille didn't? #define DUMP_ADDRESSES_INTERVAL 300 +// This is every 2 blocks, on avg, on HUSH3 +#define DUMP_ZINDEX_INTERVAL 150 + #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL) #define MSG_NOSIGNAL 0 #endif @@ -1400,6 +1403,16 @@ void DumpAddresses() LogPrint("net", "Flushed %d addresses to peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart); } +void DumpZindexStats() +{ + int64_t nStart = GetTimeMillis(); + + CZindexDB zdb; + zdb.Write(zstats); + + LogPrintf("Flushed %d items to zindex.dat %dms\n", zstats.size(), GetTimeMillis() - nStart); +} + void static ProcessOneShot() { string strDest; @@ -1911,6 +1924,19 @@ void static Discover(boost::thread_group& threadGroup) void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler) { + if (fZindex) { + uiInterface.InitMessage(_("Loading zindex stats...")); + int64_t nStart = GetTimeMillis(); + { + CZindexDB zdb; + if (!zdb.Read(zstats)) { + LogPrintf("Invalid or missing zindex.dat! Stats may be incorrect!!!\n"); + // TODO: move invalid files out of the way? + } + } + LogPrintf("Loaded %i items from zindex.dat %dms\n", zstats.size(), GetTimeMillis() - nStart); + } + uiInterface.InitMessage(_("Loading addresses...")); // Load addresses for peers.dat int64_t nStart = GetTimeMillis(); @@ -1919,8 +1945,7 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler) if (!adb.Read(addrman)) LogPrintf("Invalid or missing peers.dat! This can happen when upgrading. Whatevz, recreating\n"); } - LogPrintf("Loaded %i addresses from peers.dat %dms\n", - addrman.size(), GetTimeMillis() - nStart); + LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart); fAddressesInitialized = true; if (semOutbound == NULL) { @@ -1968,6 +1993,9 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler) // Dump network addresses scheduler.scheduleEvery(&DumpAddresses, DUMP_ADDRESSES_INTERVAL); + + // Dump zindex stats + scheduler.scheduleEvery(&DumpZstats, DUMP_ZINDEX_INTERVAL); } bool StopNode() @@ -1977,6 +2005,9 @@ bool StopNode() for (int i=0; i<(MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS); i++) semOutbound->post(); + // persist current zindex stats to disk before we exit + DumpZindexStats(); + if (HUSH_NSPV_FULLNODE && fAddressesInitialized) { DumpAddresses(); diff --git a/src/net.h b/src/net.h index d55d500e1..e0437421f 100644 --- a/src/net.h +++ b/src/net.h @@ -174,6 +174,7 @@ extern bool fListen; extern uint64_t nLocalServices; extern uint64_t nLocalHostNonce; extern CAddrMan addrman; +extern CZindexStats zstats; /** Maximum number of connections to simultaneously allow (aka connection slots) */ extern int nMaxConnections;