Skip to content

Commit

Permalink
Write evm db dirty flag to leveldb
Browse files Browse the repository at this point in the history
  • Loading branch information
sieniven committed Jan 17, 2024
1 parent 50bb3cb commit 2d63446
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
12 changes: 12 additions & 0 deletions src/dfi/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,18 @@ void CCustomCSView::SetDbVersion(int version) {
Write(DbVersion::prefix(), version);
}

bool CCustomCSView::GetEvmDirtyFlag() const {
bool dirtyFlag;
if (Read(EvmDirtyFlag::prefix(), dirtyFlag)) {
return dirtyFlag;
}
return false;
}

void CCustomCSView::SetEvmDirtyFlag(bool flag) {
Write(EvmDirtyFlag::prefix(), flag);
}

CTeamView::CTeam CCustomCSView::CalcNextTeam(int height, const uint256 &stakeModifier) {
if (stakeModifier == uint256()) {
return Params().GetGenesisTeam();
Expand Down
8 changes: 7 additions & 1 deletion src/dfi/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,11 @@ class CCustomCSView : public CMasternodesView,
const DCT_ID &id) const override;

void SetDbVersion(int version);

int GetDbVersion() const;

void SetEvmDirtyFlag(bool flag);
bool GetEvmDirtyFlag() const;

uint256 MerkleRoot();

virtual CHistoryWriters &GetHistoryWriters() { return writers; }
Expand All @@ -624,6 +626,10 @@ class CCustomCSView : public CMasternodesView,
struct DbVersion {
static constexpr uint8_t prefix() { return 'D'; }
};

struct EvmDirtyFlag {
static constexpr uint8_t prefix() { return 'p'; }
};
};

std::map<CKeyID, CKey> AmISignerNow(int height, const CAnchorData::CTeam &team);
Expand Down
47 changes: 26 additions & 21 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,7 @@ void Shutdown(InitInterfaces& interfaces)
for (const auto& client : interfaces.chain_clients) {
client->flush();
}
auto res = XResultStatusLogged(ain_rs_stop_network_services(result));
if (!res) {
fEvmDatabaseDirty = true;
}
XResultStatusLogged(ain_rs_stop_network_services(result));
StopMapPort();

// Because these depend on each-other, we make sure that neither can be
Expand Down Expand Up @@ -297,12 +294,11 @@ void Shutdown(InitInterfaces& interfaces)
// next startup faster by avoiding rescan.

ShutdownDfTxGlobalTaskPool();
res = XResultStatusLogged(ain_rs_stop_core_services(result));
auto res = XResultStatusLogged(ain_rs_stop_core_services(result));
if (!res) {
fEvmDatabaseDirty = true;
}

// Save evm database dirty flag to disk
pcustomcsview->SetEvmDirtyFlag(fEvmDatabaseDirty);

LogPrint(BCLog::SPV, "Releasing\n");
spv::pspv.reset();
Expand Down Expand Up @@ -686,9 +682,6 @@ void SetupServerArgs()
RPCMetadata::SetupArgs(gArgs);
// Add the hidden options
gArgs.AddHiddenArgs(hidden_args);

// Set evm datbase dirty flag

}

std::string LicenseInfo()
Expand Down Expand Up @@ -1842,10 +1835,10 @@ bool AppInitMain(InitInterfaces& interfaces)
InitDfTxGlobalTaskPool();

bool fLoaded = false;
fReindex = gArgs.GetBoolArg("-reindex", fEvmDatabaseDirty);
fReindex = gArgs.GetBoolArg("-reindex", false);
bool fReindexChainState = gArgs.GetBoolArg("-reindex-chainstate", false);
while (!fLoaded && !ShutdownRequested()) {
bool fReset = fReindex;
bool fReset = (fReindex || fEvmDatabaseDirty);
std::string strLoadError;

uiInterface.InitMessage(_("Loading block index...").translated);
Expand Down Expand Up @@ -1935,6 +1928,13 @@ bool AppInitMain(InitInterfaces& interfaces)
// Ensure we are on latest DB version
pcustomcsview->SetDbVersion(CCustomCSView::DbVersion);

// Set evm database dirty flag
fEvmDatabaseDirty = pcustomcsview->GetEvmDirtyFlag();
if (!fReset && fEvmDatabaseDirty) {
LogPrintf("Evm database dirty, re-indexing chain state.\n");
break;
}

// make account history db
paccountHistoryDB.reset();
if (gArgs.GetBoolArg("-acindex", DEFAULT_ACINDEX)) {
Expand Down Expand Up @@ -2063,18 +2063,23 @@ bool AppInitMain(InitInterfaces& interfaces)
} while(false);

if (!fLoaded && !ShutdownRequested()) {
// first suggest a reindex
if (!fReset) {
bool fRet = uiInterface.ThreadSafeQuestion(
strLoadError + ".\n\n" + _("Do you want to rebuild the block database now?").translated,
strLoadError + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
if (fRet) {
fReindex = true;
if (fEvmDatabaseDirty) {
// Evm database dirty. Abort shutdown and run re-index pipeline.
AbortShutdown();
} else {
LogPrintf("Aborted block database rebuild. Exiting.\n");
return false;
// first suggest a reindex
bool fRet = uiInterface.ThreadSafeQuestion(
strLoadError + ".\n\n" + _("Do you want to rebuild the block database now?").translated,
strLoadError + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
if (fRet) {
fReindex = true;
AbortShutdown();
} else {
LogPrintf("Aborted block database rebuild. Exiting.\n");
return false;
}
}
} else {
return InitError(strLoadError);
Expand Down

0 comments on commit 2d63446

Please sign in to comment.