From da3da1bb1bf4f868642cc4ca5aed6f8e2e0d5f10 Mon Sep 17 00:00:00 2001 From: Daniel Kraft Date: Tue, 16 Mar 2021 15:38:42 +0100 Subject: [PATCH 1/9] Move internal functions into anonymous namespace. Wrap some of the functions used only in main into an anonymous namespace; some of them were already marked as "static", others were not (but they were still not needed from anywhere else). This makes it immediately clear that those functions are only used in the context of main. FindUndoPos had a declaration first and the definition later, but it is fine to just move the definition to where the declaration was. This simplifies the code further (and is a pure move in the context of this commit). --- divi/src/main.cpp | 69 ++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/divi/src/main.cpp b/divi/src/main.cpp index a66520311..717071c42 100755 --- a/divi/src/main.cpp +++ b/divi/src/main.cpp @@ -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; @@ -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)) { @@ -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); @@ -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) { @@ -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); @@ -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. From 4c491ecdb4ffb2645fa758c6d0d780c4cf8cfd38 Mon Sep 17 00:00:00 2001 From: Daniel Kraft Date: Wed, 7 Apr 2021 14:20:40 +0200 Subject: [PATCH 2/9] Clean up some mn config code. This is a bunch of related and straight-forward cleanups to the code around masternode configuration: Pass arguments as const& instead of by value, mark functions as const, and move helper functions from being static in a class and declared inside the header to just being inside an anonymous namespace in the cpp file. --- divi/src/MasternodeBroadcastFactory.cpp | 35 ++++++++------- divi/src/MasternodeBroadcastFactory.h | 60 +++++++------------------ divi/src/masternode-sync.h | 2 +- divi/src/masternode.cpp | 5 ++- divi/src/masternode.h | 16 ++++--- divi/src/masternodeconfig.cpp | 12 ++--- divi/src/masternodeconfig.h | 9 ++-- 7 files changed, 63 insertions(+), 76 deletions(-) diff --git a/divi/src/MasternodeBroadcastFactory.cpp b/divi/src/MasternodeBroadcastFactory.cpp index fac4d3730..7207e8863 100644 --- a/divi/src/MasternodeBroadcastFactory.cpp +++ b/divi/src/MasternodeBroadcastFactory.cpp @@ -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"; @@ -26,7 +29,7 @@ bool CMasternodeBroadcastFactory::checkBlockchainSync(std::string& strErrorRet, } return true; } -bool CMasternodeBroadcastFactory::setMasternodeKeys( +bool setMasternodeKeys( const std::string& strKeyMasternode, std::pair& masternodeKeyPair, std::string& strErrorRet) @@ -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, @@ -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, @@ -95,7 +98,7 @@ bool CMasternodeBroadcastFactory::checkMasternodeCollateral( return true; } -bool CMasternodeBroadcastFactory::createArgumentsFromConfig( +bool createArgumentsFromConfig( const CMasternodeConfig::CMasternodeEntry configEntry, std::string& strErrorRet, bool fOffline, @@ -121,6 +124,8 @@ bool CMasternodeBroadcastFactory::createArgumentsFromConfig( return true; } +} // anonymous namespace + bool CMasternodeBroadcastFactory::Create( const CMasternodeConfig::CMasternodeEntry configEntry, CPubKey pubkeyCollateralAddress, @@ -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) @@ -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, diff --git a/divi/src/MasternodeBroadcastFactory.h b/divi/src/MasternodeBroadcastFactory.h index e7edd478a..2f576f060 100644 --- a/divi/src/MasternodeBroadcastFactory.h +++ b/divi/src/MasternodeBroadcastFactory.h @@ -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); @@ -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& 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& 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& masternodeKeyPair, - std::pair& 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 \ No newline at end of file +#endif //MASTERNODE_BROADCAST_FACTORY_H diff --git a/divi/src/masternode-sync.h b/divi/src/masternode-sync.h index 1ce4a3b33..c15d852e1 100644 --- a/divi/src/masternode-sync.h +++ b/divi/src/masternode-sync.h @@ -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; }; diff --git a/divi/src/masternode.cpp b/divi/src/masternode.cpp index d2391d2c2..cf457f42b 100644 --- a/divi/src/masternode.cpp +++ b/divi/src/masternode.cpp @@ -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; diff --git a/divi/src/masternode.h b/divi/src/masternode.h index f987234c1..b561097fc 100644 --- a/divi/src/masternode.h +++ b/divi/src/masternode.h @@ -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; diff --git a/divi/src/masternodeconfig.cpp b/divi/src/masternodeconfig.cpp index 91b9debb9..c48f8304d 100644 --- a/divi/src/masternodeconfig.cpp +++ b/divi/src/masternodeconfig.cpp @@ -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) @@ -94,15 +95,16 @@ CMasternodeConfig::CMasternodeConfig() { entries = std::vector(); } + const std::vector& 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; diff --git a/divi/src/masternodeconfig.h b/divi/src/masternodeconfig.h index d5ac185d9..2d34edb4a 100644 --- a/divi/src/masternodeconfig.h +++ b/divi/src/masternodeconfig.h @@ -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; @@ -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& getEntries() const; - int getCount(); + int getCount() const; private: std::vector entries; From 6803639c9aaebdf8e3311a066e1905c9cc6b7faf Mon Sep 17 00:00:00 2001 From: Daniel Kraft Date: Thu, 8 Oct 2020 15:56:35 +0200 Subject: [PATCH 3/9] Introduce rewardScript for masternodes. This adds a new field, CScript rewardScript, to the masternode data structures. For now, we just add it, return it from the RPCs, and set it to the script corresponding to the collateral pubkey on creation. We also serialise the new field as part of CMasternode, which is used e.g. in the on-disk cache, but not the wire protocol (where CMasternodeBroadcast is used instead). Thus this change invalidates the mncache format, but has no other effects on compatibility. In the future, we will implement a protocol upgrade and also network fork that will use this new field so masternodes can specify an arbitrary script to which they want to receive their payments, rather than being paid always to the collateral address. --- divi/src/MasternodeModule.cpp | 2 ++ divi/src/MasternodeModule.h | 6 +++++- divi/src/masternode.cpp | 10 +++++++++- divi/src/masternode.h | 17 +++++++++++++++++ divi/src/masternodeman.cpp | 1 + divi/src/rpcmasternode.cpp | 4 +++- 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/divi/src/MasternodeModule.cpp b/divi/src/MasternodeModule.cpp index 118e979ec..1e29011a4 100644 --- a/divi/src/MasternodeModule.cpp +++ b/divi/src/MasternodeModule.cpp @@ -241,6 +241,7 @@ std::vector GetMasternodeList(std::string strFilter) entry.outputIndex = oIdx; entry.status = strStatus; entry.collateralAddress = CBitcoinAddress(masternode.pubKeyCollateralAddress.GetID()).ToString(); + entry.rewardScript = HexStr(masternode.rewardScript); entry.protocolVersion = masternode.protocolVersion; entry.lastSeenTime = (int64_t)masternode.lastPing.sigTime; entry.activeTime = (int64_t)(masternode.lastPing.sigTime - masternode.sigTime); @@ -262,6 +263,7 @@ ActiveMasternodeStatus GetActiveMasternodeStatus() result.outputIndex = std::to_string(activeMasternode.vin.prevout.n); result.netAddress = activeMasternode.service.ToString(); result.collateralAddress = CBitcoinAddress(pmn->pubKeyCollateralAddress.GetID()).ToString(); + result.rewardScript = HexStr(pmn->rewardScript); result.statusCode = std::to_string(activeMasternode.status); result.statusMessage = activeMasternode.GetStatus(); result.activeMasternodeFound = true; diff --git a/divi/src/MasternodeModule.h b/divi/src/MasternodeModule.h index 787871d21..98f7819e3 100644 --- a/divi/src/MasternodeModule.h +++ b/divi/src/MasternodeModule.h @@ -46,6 +46,7 @@ struct ActiveMasternodeStatus std::string outputIndex; std::string netAddress; std::string collateralAddress; + std::string rewardScript; std::string statusCode; std::string statusMessage; ActiveMasternodeStatus( @@ -54,6 +55,7 @@ struct ActiveMasternodeStatus , outputIndex() , netAddress() , collateralAddress() + , rewardScript() , statusCode() , statusMessage() {} @@ -66,6 +68,7 @@ struct MasternodeListEntry uint64_t outputIndex; std::string status; std::string collateralAddress; + std::string rewardScript; int protocolVersion; int64_t signatureTime; int64_t lastSeenTime; @@ -78,6 +81,7 @@ struct MasternodeListEntry , outputIndex() , status() , collateralAddress() + , rewardScript() , protocolVersion() , signatureTime() , lastSeenTime() @@ -104,4 +108,4 @@ bool LoadMasternodeDataFromDisk(UIMessenger& uiMessenger,std::string pathToDataD void DumpMasternodeDataToDisk(); CMasternodePayments& GetMasternodePayments(); bool InitializeMasternodeIfRequested(const Settings& settings, bool transactionIndexEnabled, std::string& errorMessage); -#endif //MASTERNODE_MODULE_H \ No newline at end of file +#endif //MASTERNODE_MODULE_H diff --git a/divi/src/masternode.cpp b/divi/src/masternode.cpp index cf457f42b..a50469c15 100644 --- a/divi/src/masternode.cpp +++ b/divi/src/masternode.cpp @@ -6,7 +6,6 @@ #include "masternode.h" #include -#include #include #include #include