Skip to content

General cleanups for masternodes and main #77

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

Merged
merged 2 commits into from
Apr 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions divi/src/MasternodeBroadcastFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ extern CChain chainActive;
extern bool fReindex;
extern bool fImporting;

bool CMasternodeBroadcastFactory::checkBlockchainSync(std::string& strErrorRet, bool fOffline)
namespace
{

bool checkBlockchainSync(std::string& strErrorRet, bool fOffline)
{
if (!fOffline && !IsBlockchainSynced()) {
strErrorRet = "Sync in progress. Must wait until sync is complete to start Masternode";
Expand All @@ -26,7 +29,7 @@ bool CMasternodeBroadcastFactory::checkBlockchainSync(std::string& strErrorRet,
}
return true;
}
bool CMasternodeBroadcastFactory::setMasternodeKeys(
bool setMasternodeKeys(
const std::string& strKeyMasternode,
std::pair<CKey,CPubKey>& masternodeKeyPair,
std::string& strErrorRet)
Expand All @@ -38,7 +41,7 @@ bool CMasternodeBroadcastFactory::setMasternodeKeys(
}
return true;
}
bool CMasternodeBroadcastFactory::setMasternodeCollateralKeys(
bool setMasternodeCollateralKeys(
const std::string& txHash,
const std::string& outputIndex,
const std::string& service,
Expand All @@ -63,7 +66,7 @@ bool CMasternodeBroadcastFactory::setMasternodeCollateralKeys(
return true;
}

bool CMasternodeBroadcastFactory::checkMasternodeCollateral(
bool checkMasternodeCollateral(
const CTxIn& txin,
const std::string& txHash,
const std::string& outputIndex,
Expand Down Expand Up @@ -95,7 +98,7 @@ bool CMasternodeBroadcastFactory::checkMasternodeCollateral(
return true;
}

bool CMasternodeBroadcastFactory::createArgumentsFromConfig(
bool createArgumentsFromConfig(
const CMasternodeConfig::CMasternodeEntry configEntry,
std::string& strErrorRet,
bool fOffline,
Expand All @@ -121,6 +124,8 @@ bool CMasternodeBroadcastFactory::createArgumentsFromConfig(
return true;
}

} // anonymous namespace

bool CMasternodeBroadcastFactory::Create(
const CMasternodeConfig::CMasternodeEntry configEntry,
CPubKey pubkeyCollateralAddress,
Expand Down Expand Up @@ -278,10 +283,10 @@ CMasternodePing createDelayedMasternodePing(const CMasternodeBroadcast& mnb)
} // anonymous namespace

void CMasternodeBroadcastFactory::createWithoutSignatures(
CTxIn txin,
CService service,
CPubKey pubKeyCollateralAddressNew,
CPubKey pubKeyMasternodeNew,
const CTxIn& txin,
const CService& service,
const CPubKey& pubKeyCollateralAddressNew,
const CPubKey& pubKeyMasternodeNew,
const MasternodeTier nMasternodeTier,
bool deferRelay,
CMasternodeBroadcast& mnbRet)
Expand All @@ -299,12 +304,12 @@ void CMasternodeBroadcastFactory::createWithoutSignatures(
}

bool CMasternodeBroadcastFactory::Create(
CTxIn txin,
CService service,
CKey keyCollateralAddressNew,
CPubKey pubKeyCollateralAddressNew,
CKey keyMasternodeNew,
CPubKey pubKeyMasternodeNew,
const CTxIn& txin,
const CService& service,
const CKey& keyCollateralAddressNew,
const CPubKey& pubKeyCollateralAddressNew,
const CKey& keyMasternodeNew,
const CPubKey& pubKeyMasternodeNew,
const MasternodeTier nMasternodeTier,
std::string& strErrorRet,
CMasternodeBroadcast& mnbRet,
Expand Down
60 changes: 15 additions & 45 deletions divi/src/MasternodeBroadcastFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class CMasternodeBroadcastFactory
std::string& strErrorRet);
private:
static void createWithoutSignatures(
CTxIn txin,
CService service,
CPubKey pubKeyCollateralAddressNew,
CPubKey pubKeyMasternodeNew,
const CTxIn& txin,
const CService& service,
const CPubKey& pubKeyCollateralAddressNew,
const CPubKey& pubKeyMasternodeNew,
MasternodeTier nMasternodeTier,
bool deferRelay,
CMasternodeBroadcast& mnbRet);
Expand All @@ -57,45 +57,15 @@ class CMasternodeBroadcastFactory
CMasternodeBroadcast& mnb,
std::string& strErrorRet);

static bool Create(CTxIn vin,
CService service,
CKey keyCollateralAddressNew,
CPubKey pubKeyCollateralAddressNew,
CKey keyMasternodeNew,
CPubKey pubKeyMasternodeNew,
MasternodeTier nMasternodeTier,
std::string& strErrorRet,
CMasternodeBroadcast& mnbRet,
bool deferRelay);
static bool checkBlockchainSync(
std::string& strErrorRet, bool fOffline);
static bool setMasternodeKeys(
const std::string& strKeyMasternode,
std::pair<CKey,CPubKey>& masternodeKeyPair,
std::string& strErrorRet);
static bool setMasternodeCollateralKeys(
const std::string& txHash,
const std::string& outputIndex,
const std::string& service,
bool collateralPrivKeyIsRemote,
CTxIn& txin,
std::pair<CKey,CPubKey>& masternodeCollateralKeyPair,
std::string& error);
static bool checkMasternodeCollateral(
const CTxIn& txin,
const std::string& txHash,
const std::string& outputIndex,
const std::string& service,
MasternodeTier& nMasternodeTier,
std::string& strErrorRet);
static bool createArgumentsFromConfig(
const CMasternodeConfig::CMasternodeEntry configEntry,
std::string& strErrorRet,
bool fOffline,
bool collateralPrivKeyIsRemote,
CTxIn& txin,
std::pair<CKey,CPubKey>& masternodeKeyPair,
std::pair<CKey,CPubKey>& masternodeCollateralKeyPair,
MasternodeTier& nMasternodeTier);
static bool Create(const CTxIn& vin,
const CService& service,
const CKey& keyCollateralAddressNew,
const CPubKey& pubKeyCollateralAddressNew,
const CKey& keyMasternodeNew,
const CPubKey& pubKeyMasternodeNew,
MasternodeTier nMasternodeTier,
std::string& strErrorRet,
CMasternodeBroadcast& mnbRet,
bool deferRelay);
};
#endif //MASTERNODE_BROADCAST_FACTORY_H
#endif //MASTERNODE_BROADCAST_FACTORY_H
69 changes: 36 additions & 33 deletions divi/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,10 @@ void Misbehaving(NodeId pnode, int howmuch)
LogPrintf("Misbehaving: %s (%d -> %d)\n", state->name, state->nMisbehavior - howmuch, state->nMisbehavior);
}

void static InvalidChainFound(CBlockIndex* pindexNew)
namespace
{

void InvalidChainFound(CBlockIndex* pindexNew)
{
if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
pindexBestInvalid = pindexNew;
Expand All @@ -1217,7 +1220,7 @@ void static InvalidChainFound(CBlockIndex* pindexNew)
CheckForkWarningConditions();
}

void static InvalidBlockFound(CBlockIndex* pindex, const CValidationState& state)
void InvalidBlockFound(CBlockIndex* pindex, const CValidationState& state)
{
int nDoS = 0;
if (state.IsInvalid(nDoS)) {
Expand All @@ -1237,7 +1240,7 @@ void static InvalidBlockFound(CBlockIndex* pindex, const CValidationState& state
}
}

void static FlushBlockFile(bool fFinalize = false)
void FlushBlockFile(bool fFinalize = false)
{
LOCK(cs_LastBlockFile);

Expand All @@ -1260,9 +1263,35 @@ void static FlushBlockFile(bool fFinalize = false)
}
}

bool FindUndoPos(CValidationState& state, int nFile, CDiskBlockPos& pos, unsigned int nAddSize);
bool FindUndoPos(CValidationState& state, int nFile, CDiskBlockPos& pos, unsigned int nAddSize)
{
pos.nFile = nFile;

LOCK(cs_LastBlockFile);

unsigned int nNewSize;
pos.nPos = vinfoBlockFile[nFile].nUndoSize;
nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
setDirtyFileInfo.insert(nFile);

unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
if (nNewChunks > nOldChunks) {
if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
FILE* file = OpenUndoFile(pos);
if (file) {
LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
fclose(file);
}
} else
return state.Error("out of disk space");
}

return true;
}

static int64_t nTimeTotal = 0;
int64_t nTimeTotal = 0;

void VerifyBestBlockIsAtPreviousBlock(const CBlockIndex* pindex, CCoinsViewCache& view)
{
Expand Down Expand Up @@ -1391,6 +1420,8 @@ bool UpdateDBIndicesForNewBlock(
return true;
}

} // anonymous namespace

bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck, bool fAlreadyChecked)
{
AssertLockHeld(cs_main);
Expand Down Expand Up @@ -2140,34 +2171,6 @@ bool FindBlockPos(CValidationState& state, CDiskBlockPos& pos, unsigned int nAdd
return true;
}

bool FindUndoPos(CValidationState& state, int nFile, CDiskBlockPos& pos, unsigned int nAddSize)
{
pos.nFile = nFile;

LOCK(cs_LastBlockFile);

unsigned int nNewSize;
pos.nPos = vinfoBlockFile[nFile].nUndoSize;
nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
setDirtyFileInfo.insert(nFile);

unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
if (nNewChunks > nOldChunks) {
if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
FILE* file = OpenUndoFile(pos);
if (file) {
LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
fclose(file);
}
} else
return state.Error("out of disk space");
}

return true;
}

bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckMerkleRoot)
{
// These are checks that are independent of context.
Expand Down
2 changes: 1 addition & 1 deletion divi/src/masternode-sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class CMasternodeSync
bool MasternodeWinnersListIsSync(CNode* pnode, const int64_t now);
void Process(bool networkIsRegtest);
bool IsSynced() const;
bool IsMasternodeListSynced() { return RequestedMasternodeAssets > MASTERNODE_SYNC_LIST; }
bool IsMasternodeListSynced() const { return RequestedMasternodeAssets > MASTERNODE_SYNC_LIST; }
void AskForMN(CNode* pnode, const CTxIn& vin) const;
};

Expand Down
5 changes: 4 additions & 1 deletion divi/src/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ bool CMasternode::IsValidNetAddr() const
(IsReachable(addr) && addr.IsRoutable());
}

CMasternodeBroadcast::CMasternodeBroadcast(CService newAddr, CTxIn newVin, CPubKey pubKeyCollateralAddressNew, CPubKey pubKeyMasternodeNew, const MasternodeTier nMasternodeTier, int protocolVersionIn)
CMasternodeBroadcast::CMasternodeBroadcast(
const CService& newAddr, const CTxIn& newVin,
const CPubKey& pubKeyCollateralAddressNew, const CPubKey& pubKeyMasternodeNew,
const MasternodeTier nMasternodeTier, const int protocolVersionIn)
{
vin = newVin;
addr = newAddr;
Expand Down
16 changes: 10 additions & 6 deletions divi/src/masternode.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,19 @@ class CMasternode

class CMasternodeBroadcast : public CMasternode
{
public:
CMasternodeBroadcast() = default;
private:
CMasternodeBroadcast(
CService newAddr,
CTxIn newVin,
CPubKey pubKeyCollateralAddress,
CPubKey pubKeyMasternode,
const CService& newAddr,
const CTxIn& newVin,
const CPubKey& pubKeyCollateralAddress,
const CPubKey& pubKeyMasternode,
MasternodeTier nMasternodeTier,
int protocolVersionIn);

friend class CMasternodeBroadcastFactory;

public:
CMasternodeBroadcast() = default;
CMasternodeBroadcast(const CMasternode& mn);

void Relay() const;
Expand Down
12 changes: 7 additions & 5 deletions divi/src/masternodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ boost::filesystem::path GetMasternodeConfigFile()
if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir() / pathConfigFile;
return pathConfigFile;
}
void CMasternodeConfig::add(std::string alias, std::string ip, std::string privKey, std::string txHash, std::string outputIndex)

void CMasternodeConfig::add(const std::string& alias, const std::string& ip, const std::string& privKey,
const std::string& txHash, const std::string& outputIndex)
{
CMasternodeEntry cme(alias, ip, privKey, txHash, outputIndex);
entries.push_back(cme);
entries.emplace_back(alias, ip, privKey, txHash, outputIndex);
}

bool CMasternodeConfig::read(std::string& strErr)
Expand Down Expand Up @@ -94,15 +95,16 @@ CMasternodeConfig::CMasternodeConfig()
{
entries = std::vector<CMasternodeEntry>();
}

const std::vector<CMasternodeConfig::CMasternodeEntry>& CMasternodeConfig::getEntries() const
{
return entries;
}

int CMasternodeConfig::getCount()
int CMasternodeConfig::getCount() const
{
int c = -1;
BOOST_FOREACH (CMasternodeEntry e, entries) {
for (const auto& e : entries) {
if (e.getAlias() != "") c++;
}
return c;
Expand Down
9 changes: 6 additions & 3 deletions divi/src/masternodeconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class CMasternodeConfig
std::string outputIndex;

public:
CMasternodeEntry(std::string alias, std::string ip, std::string privKey, std::string txHash, std::string outputIndex)
CMasternodeEntry(const std::string& alias, const std::string& ip,
const std::string& privKey,
const std::string& txHash, const std::string& outputIndex)
{
this->alias = alias;
this->ip = ip;
Expand Down Expand Up @@ -91,9 +93,10 @@ class CMasternodeConfig

void clear();
bool read(std::string& strErr);
void add(std::string alias, std::string ip, std::string privKey, std::string txHash, std::string outputIndex);
void add(const std::string& alias, const std::string& ip, const std::string& privKey,
const std::string& txHash, const std::string& outputIndex);
const std::vector<CMasternodeEntry>& getEntries() const;
int getCount();
int getCount() const;

private:
std::vector<CMasternodeEntry> entries;
Expand Down