Start to persist zindex stats to disk

This commit is contained in:
Duke Leto
2021-06-15 00:47:56 -04:00
parent d3f12a6b7f
commit 8c25b745b3
2 changed files with 102 additions and 0 deletions

View File

@@ -572,6 +572,96 @@ namespace {
} // anon namespace
// CZindexDB
CZindexDB::CZindexDB()
{
pathAddr = GetDataDir() / "zindex.dat";
}
bool CZindexDB::Read(CZindexStats& zstats)
{
// open input file, and associate with CAutoFile
FILE *file = fopen(pathAddr.string().c_str(), "rb");
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
if (filein.IsNull())
return error("%s: Failed to open file %s", __func__, pathAddr.string());
// use file size to size memory buffer
int fileSize = boost::filesystem::file_size(pathAddr);
int dataSize = fileSize - sizeof(uint256);
// Don't try to resize to a negative number if file is small
if (dataSize < 0)
dataSize = 0;
vector<unsigned char> vchData;
vchData.resize(dataSize);
uint256 hashIn;
// read data and checksum from file
try {
filein.read((char *)&vchData[0], dataSize);
filein >> hashIn;
}
catch (const std::exception& e) {
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
}
filein.fclose();
CDataStream ssZstats(vchData, SER_DISK, CLIENT_VERSION);
// verify stored checksum matches input data
uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
if (hashIn != hashTmp)
return error("%s: Checksum mismatch, data corrupted", __func__);
unsigned char pchMsgTmp[4];
try {
// de-serialize file header (network specific magic number) and ..
ssZstats >> FLATDATA(pchMsgTmp);
// ... verify the network matches ours
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
return error("%s: Invalid network magic number", __func__);
// de-serialize address data into one CAddrMan object
ssZstats >> addr;
}
catch (const std::exception& e) {
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
}
return true;
}
bool CZindexDB::Write(const CZindexStats& zstats)
{
// Generate random temporary filename
unsigned short randv = 0;
GetRandBytes((unsigned char*)&randv, sizeof(randv));
std::string tmpfn = strprintf("zindex.dat.%04x", randv);
// serialize zstats, checksum data up to that point, then append checksum
CDataStream ssZstats(SER_DISK, CLIENT_VERSION);
ssZstats << FLATDATA(Params().MessageStart());
ssZstats << zstats;
uint256 hash = Hash(ssZstats.begin(), ssZstats.end());
ssZstats << hash;
// Write and commit header, data
try {
fileout << ssPeers;
} catch (const std::exception& e) {
return error("%s: Serialize or I/O error - %s", __func__, e.what());
}
FileCommit(fileout.Get());
fileout.fclose();
// replace existing zindex.dat, if any, with new zindex.dat.XXXX
if (!RenameOver(pathTmp, pathAddr))
return error("%s: Rename-into-place failed", __func__);
return true;
}
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
LOCK(cs_main);
CNodeState *state = State(nodeid);
@@ -4603,6 +4693,8 @@ bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBl
}
}
// TODO: Update zindex.dat on disk
if (fZindex)
fprintf(stderr, "ht.%d, ShieldedPayments=%d, ShieldedTx=%d, ShieldedOutputs=%d, FullyShieldedTx=%d, ntz=%d\n",
pindexNew->GetHeight(), nShieldedPayments, nShieldedTx, nShieldedOutputs, nFullyShieldedTx, nNotarizations );