Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Move tier two managers initialization from init.cpp to tiertwo/init.cpp #2684

Merged
merged 5 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ set(SERVER_SOURCES
./src/httpserver.cpp
./src/indirectmap.h
./src/init.cpp
./src/tiertwo/init.cpp
./src/interfaces/handler.cpp
./src/interfaces/wallet.cpp
./src/dbwrapper.cpp
Expand Down
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ BITCOIN_CORE_H = \
httpserver.h \
indirectmap.h \
init.h \
tiertwo/init.h \
interfaces/handler.h \
interfaces/wallet.h \
invalid.h \
Expand Down Expand Up @@ -350,6 +351,7 @@ libbitcoin_server_a_SOURCES = \
httprpc.cpp \
httpserver.cpp \
init.cpp \
tiertwo/init.cpp \
dbwrapper.cpp \
legacy/validation_zerocoin_legacy.cpp \
sapling/sapling_validation.cpp \
Expand Down Expand Up @@ -729,6 +731,7 @@ CLEANFILES = $(EXTRA_LIBRARIES)

CLEANFILES += *.gcda *.gcno
CLEANFILES += budget/*.gcda budget/*.gcno
CLEANFILES += bls/*.gcda bls/*.gcno
CLEANFILES += compat/*.gcda compat/*.gcno
CLEANFILES += consensus/*.gcda consensus/*.gcno
CLEANFILES += crc32c/src/*.gcda crc32c/src/*.gcno
Expand All @@ -744,6 +747,7 @@ CLEANFILES += sapling/*.gcda sapling/*.gcno
CLEANFILES += script/*.gcda script/*.gcno
CLEANFILES += support/*.gcda support/*.gcno
CLEANFILES += test/librust/*.gcda test/librust/*.gcno
CLEANFILES += tiertwo/*.gcda tiertwo/*.gcno
CLEANFILES += univalue/*.gcda univalue/*.gcno
CLEANFILES += util/*.gcda util/*.gcno
CLEANFILES += wallet/*.gcda wallet/*.gcno
Expand Down
2 changes: 0 additions & 2 deletions src/budget/budgetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1308,8 +1308,6 @@ bool CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData

int CBudgetManager::ProcessMessageInner(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
{
// lite mode is not supported
if (fLiteMode) return 0;
if (!masternodeSync.IsBlockchainSynced()) return 0;

if (strCommand == NetMsgType::BUDGETVOTESYNC) {
Expand Down
125 changes: 9 additions & 116 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include "addrman.h"
#include "amount.h"
#include "bls/bls_wrapper.h"
#include "budget/budgetdb.h"
#include "budget/budgetmanager.h"
#include "checkpoints.h"
#include "compat/sanity.h"
#include "consensus/upgrades.h"
Expand All @@ -31,9 +29,7 @@
#include "invalid.h"
#include "key.h"
#include "mapport.h"
#include "masternode-payments.h"
#include "masternodeconfig.h"
#include "masternodeman.h"
#include "miner.h"
#include "netbase.h"
#include "net_processing.h"
Expand All @@ -48,6 +44,7 @@
#include "spork.h"
#include "sporkdb.h"
#include "evo/evodb.h"
#include "tiertwo/init.h"
#include "txdb.h"
#include "torcontrol.h"
#include "guiinterface.h"
Expand Down Expand Up @@ -92,7 +89,6 @@ static const bool DEFAULT_PROXYRANDOMIZE = true;
static const bool DEFAULT_REST_ENABLE = false;
static const bool DEFAULT_DISABLE_SAFEMODE = false;
static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
static const bool DEFAULT_MASTERNODE = false;
static const bool DEFAULT_MNCONFLOCK = true;

std::unique_ptr<CConnman> g_connman;
Expand Down Expand Up @@ -227,9 +223,7 @@ void Shutdown()
g_connman.reset();
peerLogic.reset();

DumpMasternodes();
DumpBudgets(g_budgetman);
DumpMasternodePayments();
DumpTierTwo();
if (::mempool.IsLoaded() && gArgs.GetBoolArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
DumpMempool(::mempool);
}
Expand Down Expand Up @@ -519,7 +513,6 @@ std::string HelpMessage(HelpMessageMode mode)
}
strUsage += HelpMessageOpt("-shrinkdebugfile", "Shrink debug.log file on client startup (default: 1 when no -debug)");
AppendParamsHelpMessages(strUsage, showDebug);
strUsage += HelpMessageOpt("-litemode=<n>", strprintf("Disable all PIVX specific functionality (Masternodes, Budgeting) (0-1, default: %u)", 0));

strUsage += HelpMessageGroup("Masternode options:");
strUsage += HelpMessageOpt("-masternode=<n>", strprintf("Enable the client to act as a masternode (0-1, default: %u)", DEFAULT_MASTERNODE));
Expand Down Expand Up @@ -947,19 +940,6 @@ static std::string ResolveErrMsg(const char * const optname, const std::string&
return strprintf(_("Cannot resolve -%s address: '%s'"), optname, strBind);
}

// Sets the last CACHED_BLOCK_HASHES hashes into masternode manager cache
static void LoadBlockHashesCache(CMasternodeMan& man)
{
LOCK(cs_main);
const CBlockIndex* pindex = chainActive.Tip();
unsigned int inserted = 0;
while (pindex && inserted < CACHED_BLOCK_HASHES) {
man.CacheBlockHash(pindex);
pindex = pindex->pprev;
++inserted;
}
}

void InitLogging()
{
g_logger->m_print_to_file = !gArgs.IsArgNegated("-debuglogfile");
Expand Down Expand Up @@ -1742,91 +1722,12 @@ bool AppInitMain()

// ********************************************************* Step 10: setup layer 2 data

uiInterface.InitMessage(_("Loading masternode cache..."));

mnodeman.SetBestHeight(chain_active_height);
LoadBlockHashesCache(mnodeman);
CMasternodeDB mndb;
CMasternodeDB::ReadResult readResult = mndb.Read(mnodeman);
if (readResult == CMasternodeDB::FileError)
LogPrintf("Missing masternode cache file - mncache.dat, will try to recreate\n");
else if (readResult != CMasternodeDB::Ok) {
LogPrintf("Error reading mncache.dat - cached data discarded\n");
}

uiInterface.InitMessage(_("Loading budget cache..."));

CBudgetDB budgetdb;
const bool fDryRun = (chain_active_height <= 0);
if (!fDryRun) g_budgetman.SetBestHeight(chain_active_height);
CBudgetDB::ReadResult readResult2 = budgetdb.Read(g_budgetman, fDryRun);

if (readResult2 == CBudgetDB::FileError)
LogPrintf("Missing budget cache - budget.dat, will try to recreate\n");
else if (readResult2 != CBudgetDB::Ok) {
LogPrintf("Error reading budget.dat - cached data discarded\n");
}

//flag our cached items so we send them to our peers
g_budgetman.ResetSync();
g_budgetman.ReloadMapSeen();

RegisterValidationInterface(&g_budgetman);

uiInterface.InitMessage(_("Loading masternode payment cache..."));

CMasternodePaymentDB mnpayments;
CMasternodePaymentDB::ReadResult readResult3 = mnpayments.Read(masternodePayments);

RegisterValidationInterface(&masternodePayments);
LoadTierTwo(chain_active_height);
if (!InitActiveMN()) return false;
RegisterTierTwoValidationInterface();

if (readResult3 == CMasternodePaymentDB::FileError)
LogPrintf("Missing masternode payment cache - mnpayments.dat, will try to recreate\n");
else if (readResult3 != CMasternodePaymentDB::Ok) {
LogPrintf("Error reading mnpayments.dat - cached data discarded\n");
}

fMasterNode = gArgs.GetBoolArg("-masternode", DEFAULT_MASTERNODE);

if ((fMasterNode || masternodeConfig.getCount() > -1) && fTxIndex == false) {
return UIError(strprintf(_("Enabling Masternode support requires turning on transaction indexing."
"Please add %s to your configuration and start with %s"), "txindex=1", "-reindex"));
}

if (fMasterNode) {
const std::string& mnoperatorkeyStr = gArgs.GetArg("-mnoperatorprivatekey", "");
const bool fDeterministic = !mnoperatorkeyStr.empty();
LogPrintf("IS %sMASTERNODE\n", (fDeterministic ? "DETERMINISTIC " : ""));

if (fDeterministic) {
// Check enforcement
if (!deterministicMNManager->IsDIP3Enforced()) {
const std::string strError = strprintf(_("Cannot start deterministic masternode before enforcement. Remove %s to start as legacy masternode"), "-mnoperatorprivatekey");
LogPrintf("-- ERROR: %s\n", strError);
return UIError(strError);
}
// Create and register activeMasternodeManager
activeMasternodeManager = new CActiveDeterministicMasternodeManager();
auto res = activeMasternodeManager->SetOperatorKey(mnoperatorkeyStr);
if (!res) { return UIError(res.getError()); }
RegisterValidationInterface(activeMasternodeManager);
// Init active masternode
const CBlockIndex* pindexTip = WITH_LOCK(cs_main, return chainActive.Tip(); );
activeMasternodeManager->Init(pindexTip);
} else {
// Check enforcement
if (deterministicMNManager->LegacyMNObsolete()) {
const std::string strError = strprintf(_("Legacy masternode system disabled. Use %s to start as deterministic masternode"), "-mnoperatorprivatekey");
LogPrintf("-- ERROR: %s\n", strError);
return UIError(strError);
}
auto res = initMasternode(gArgs.GetArg("-masternodeprivkey", ""), gArgs.GetArg("-masternodeaddr", ""), true);
if (!res) { return UIError(res.getError()); }
}
}

//get the mode of budget voting for this masternode
g_budgetman.strBudgetMode = gArgs.GetArg("-budgetvotemode", "auto");
// set the mode of budget voting for this node
SetBudgetFinMode(gArgs.GetArg("-budgetvotemode", "auto"));

#ifdef ENABLE_WALLET
// !TODO: remove after complete transition to DMN
Expand All @@ -1853,16 +1754,8 @@ bool AppInitMain()
}
#endif

// lite mode disables all Masternode related functionality
fLiteMode = gArgs.GetBoolArg("-litemode", false);
if (fMasterNode && fLiteMode) {
return UIError(_("You can not start a masternode in litemode"));
}

LogPrintf("fLiteMode %d\n", fLiteMode);
LogPrintf("Budget Mode %s\n", g_budgetman.strBudgetMode.c_str());

threadGroup.create_thread(std::bind(&ThreadCheckMasternodes));
// Start tier two threads and jobs
StartTierTwoThreadsAndScheduleJobs(threadGroup, scheduler);

if (ShutdownRequested()) {
LogPrintf("Shutdown requested. Exiting.\n");
Expand Down
5 changes: 2 additions & 3 deletions src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,14 @@ bool CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::st
{
if (!masternodeSync.IsBlockchainSynced()) return true;

if (fLiteMode) return true; //disable all Masternode related functionality

// Skip after legacy obsolete. !TODO: remove when transition to DMN is complete
if (deterministicMNManager->LegacyMNObsolete()) {
LogPrint(BCLog::MASTERNODE, "mnw - skip obsolete message %s\n", strCommand);
return true;
}

if (strCommand == NetMsgType::GETMNWINNERS) { //Masternode Payments Request Sync
if (strCommand == NetMsgType::GETMNWINNERS) {
//Masternode Payments Request Sync
int nCountNeeded;
vRecv >> nCountNeeded;

Expand Down
3 changes: 0 additions & 3 deletions src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,6 @@ bool CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData

int CMasternodeMan::ProcessMessageInner(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
{
if (fLiteMode) return 0; //disable all Masternode related functionality
if (!masternodeSync.IsBlockchainSynced()) return 0;

// Skip after legacy obsolete. !TODO: remove when transition to DMN is complete
Expand Down Expand Up @@ -1156,8 +1155,6 @@ bool CMasternodeMan::IsWithinDepth(const uint256& nHash, int depth) const

void ThreadCheckMasternodes()
{
if (fLiteMode) return; //disable all Masternode related functionality

// Make this thread recognisable as the wallet flushing thread
util::ThreadRename("pivx-masternodeman");
LogPrintf("Masternodes thread started\n");
Expand Down
2 changes: 0 additions & 2 deletions src/spork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ void CSporkManager::LoadSporksFromDB()

bool CSporkManager::ProcessSpork(CNode* pfrom, std::string& strCommand, CDataStream& vRecv, int& dosScore)
{
if (fLiteMode) return true; // disable all masternode related functionality

if (strCommand == NetMsgType::SPORK) {
dosScore = ProcessSporkMsg(vRecv);
return dosScore == 0;
Expand Down