Read zdb on startup and serialize every 150s and on shutdown

This commit is contained in:
Duke Leto
2021-06-15 21:58:18 -04:00
parent 8c25b745b3
commit 40bd639bd8
2 changed files with 34 additions and 2 deletions

View File

@@ -48,6 +48,9 @@ using namespace hush;
// Satoshi originally used 10 seconds(!), did they know something Peter Wuille didn't? // Satoshi originally used 10 seconds(!), did they know something Peter Wuille didn't?
#define DUMP_ADDRESSES_INTERVAL 300 #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) #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
#define MSG_NOSIGNAL 0 #define MSG_NOSIGNAL 0
#endif #endif
@@ -1400,6 +1403,16 @@ void DumpAddresses()
LogPrint("net", "Flushed %d addresses to peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart); 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() void static ProcessOneShot()
{ {
string strDest; string strDest;
@@ -1911,6 +1924,19 @@ void static Discover(boost::thread_group& threadGroup)
void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler) 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...")); uiInterface.InitMessage(_("Loading addresses..."));
// Load addresses for peers.dat // Load addresses for peers.dat
int64_t nStart = GetTimeMillis(); int64_t nStart = GetTimeMillis();
@@ -1919,8 +1945,7 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler)
if (!adb.Read(addrman)) if (!adb.Read(addrman))
LogPrintf("Invalid or missing peers.dat! This can happen when upgrading. Whatevz, recreating\n"); LogPrintf("Invalid or missing peers.dat! This can happen when upgrading. Whatevz, recreating\n");
} }
LogPrintf("Loaded %i addresses from peers.dat %dms\n", LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart);
addrman.size(), GetTimeMillis() - nStart);
fAddressesInitialized = true; fAddressesInitialized = true;
if (semOutbound == NULL) { if (semOutbound == NULL) {
@@ -1968,6 +1993,9 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler)
// Dump network addresses // Dump network addresses
scheduler.scheduleEvery(&DumpAddresses, DUMP_ADDRESSES_INTERVAL); scheduler.scheduleEvery(&DumpAddresses, DUMP_ADDRESSES_INTERVAL);
// Dump zindex stats
scheduler.scheduleEvery(&DumpZstats, DUMP_ZINDEX_INTERVAL);
} }
bool StopNode() bool StopNode()
@@ -1977,6 +2005,9 @@ bool StopNode()
for (int i=0; i<(MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS); i++) for (int i=0; i<(MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS); i++)
semOutbound->post(); semOutbound->post();
// persist current zindex stats to disk before we exit
DumpZindexStats();
if (HUSH_NSPV_FULLNODE && fAddressesInitialized) if (HUSH_NSPV_FULLNODE && fAddressesInitialized)
{ {
DumpAddresses(); DumpAddresses();

View File

@@ -174,6 +174,7 @@ extern bool fListen;
extern uint64_t nLocalServices; extern uint64_t nLocalServices;
extern uint64_t nLocalHostNonce; extern uint64_t nLocalHostNonce;
extern CAddrMan addrman; extern CAddrMan addrman;
extern CZindexStats zstats;
/** Maximum number of connections to simultaneously allow (aka connection slots) */ /** Maximum number of connections to simultaneously allow (aka connection slots) */
extern int nMaxConnections; extern int nMaxConnections;