Skip to content

Commit 3f78562

Browse files
committed
Add DumpMempool and LoadMempool
1 parent ced7c94 commit 3f78562

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

src/init.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ void Shutdown()
207207

208208
StopTorControl();
209209
UnregisterNodeSignals(GetNodeSignals());
210+
DumpMempool();
210211

211212
if (fFeeEstimatesInitialized)
212213
{
@@ -659,6 +660,8 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
659660
LogPrintf("Stopping after block import\n");
660661
StartShutdown();
661662
}
663+
664+
LoadMempool();
662665
}
663666

664667
/** Sanity checks

src/main.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6935,6 +6935,119 @@ int VersionBitsTipStateSinceHeight(const Consensus::Params& params, Consensus::D
69356935
return VersionBitsStateSinceHeight(chainActive.Tip(), params, pos, versionbitscache);
69366936
}
69376937

6938+
static const uint64_t MEMPOOL_DUMP_VERSION = 1;
6939+
6940+
bool LoadMempool(void)
6941+
{
6942+
int64_t nExpiryTimeout = GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
6943+
FILE* filestr = fopen((GetDataDir() / "mempool.dat").string().c_str(), "r");
6944+
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
6945+
if (file.IsNull()) {
6946+
LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
6947+
return false;
6948+
}
6949+
6950+
int64_t count = 0;
6951+
int64_t skipped = 0;
6952+
int64_t failed = 0;
6953+
int64_t nNow = GetTime();
6954+
6955+
try {
6956+
uint64_t version;
6957+
file >> version;
6958+
if (version != MEMPOOL_DUMP_VERSION) {
6959+
return false;
6960+
}
6961+
uint64_t num;
6962+
file >> num;
6963+
double prioritydummy = 0;
6964+
while (num--) {
6965+
CTransaction tx;
6966+
int64_t nTime;
6967+
int64_t nFeeDelta;
6968+
file >> tx;
6969+
file >> nTime;
6970+
file >> nFeeDelta;
6971+
6972+
CAmount amountdelta = nFeeDelta;
6973+
if (amountdelta) {
6974+
mempool.PrioritiseTransaction(tx.GetHash(), tx.GetHash().ToString(), prioritydummy, amountdelta);
6975+
}
6976+
CValidationState state;
6977+
if (nTime + nExpiryTimeout > nNow) {
6978+
LOCK(cs_main);
6979+
AcceptToMemoryPoolWithTime(mempool, state, tx, true, NULL, nTime);
6980+
if (state.IsValid()) {
6981+
++count;
6982+
} else {
6983+
++failed;
6984+
}
6985+
} else {
6986+
++skipped;
6987+
}
6988+
}
6989+
std::map<uint256, CAmount> mapDeltas;
6990+
file >> mapDeltas;
6991+
6992+
for (const auto& i : mapDeltas) {
6993+
mempool.PrioritiseTransaction(i.first, i.first.ToString(), prioritydummy, i.second);
6994+
}
6995+
} catch (const std::exception& e) {
6996+
LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
6997+
return false;
6998+
}
6999+
7000+
LogPrintf("Imported mempool transactions from disk: %i successes, %i failed, %i expired\n", count, failed, skipped);
7001+
return true;
7002+
}
7003+
7004+
void DumpMempool(void)
7005+
{
7006+
int64_t start = GetTimeMicros();
7007+
7008+
std::map<uint256, CAmount> mapDeltas;
7009+
std::vector<TxMempoolInfo> vinfo;
7010+
7011+
{
7012+
LOCK(mempool.cs);
7013+
for (const auto &i : mempool.mapDeltas) {
7014+
mapDeltas[i.first] = i.second.first;
7015+
}
7016+
vinfo = mempool.infoAll();
7017+
}
7018+
7019+
int64_t mid = GetTimeMicros();
7020+
7021+
try {
7022+
FILE* filestr = fopen((GetDataDir() / "mempool.dat.new").string().c_str(), "w");
7023+
if (!filestr) {
7024+
return;
7025+
}
7026+
7027+
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
7028+
7029+
uint64_t version = MEMPOOL_DUMP_VERSION;
7030+
file << version;
7031+
7032+
file << (uint64_t)vinfo.size();
7033+
for (const auto& i : vinfo) {
7034+
file << *(i.tx);
7035+
file << (int64_t)i.nTime;
7036+
file << (int64_t)i.nFeeDelta;
7037+
mapDeltas.erase(i.tx->GetHash());
7038+
}
7039+
7040+
file << mapDeltas;
7041+
FileCommit(file.Get());
7042+
file.fclose();
7043+
RenameOver(GetDataDir() / "mempool.dat.new", GetDataDir() / "mempool.dat");
7044+
int64_t last = GetTimeMicros();
7045+
LogPrintf("Dumped mempool: %gs to copy, %gs to dump\n", (mid-start)*0.000001, (last-mid)*0.000001);
7046+
} catch (const std::exception& e) {
7047+
LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
7048+
}
7049+
}
7050+
69387051
class CMainCleanup
69397052
{
69407053
public:

src/main.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,12 @@ static const unsigned int REJECT_ALREADY_KNOWN = 0x101;
533533
/** Transaction conflicts with a transaction already known */
534534
static const unsigned int REJECT_CONFLICT = 0x102;
535535

536+
/** Dump the mempool to disk. */
537+
void DumpMempool();
538+
539+
/** Load the mempool from disk. */
540+
bool LoadMempool();
541+
536542
// The following things handle network-processing logic
537543
// (and should be moved to a separate file)
538544

0 commit comments

Comments
 (0)