From: CryptoManiac Date: Mon, 2 May 2016 21:56:53 +0000 (+0300) Subject: Improve incapsulation. Yep, finally. X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=e6bfe9cb90f52486b97b80bd0528626042ea1582 Improve incapsulation. Yep, finally. --- diff --git a/src/main.cpp b/src/main.cpp index f2cc876..13a958f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3087,23 +3087,26 @@ string GetWarnings(string strFor) bool static AlreadyHave(CTxDB& txdb, const CInv& inv) { - switch (inv.type) + int nType = inv.GetType(); + auto nHash = inv.GetHash(); + + switch (nType) { case MSG_TX: { bool txInMap = false; { LOCK(mempool.cs); - txInMap = (mempool.exists(inv.hash)); + txInMap = (mempool.exists(nHash)); } return txInMap || - mapOrphanTransactions.count(inv.hash) || - txdb.ContainsTx(inv.hash); + mapOrphanTransactions.count(nHash) || + txdb.ContainsTx(nHash); } case MSG_BLOCK: - return mapBlockIndex.count(inv.hash) || - mapOrphanBlocks.count(inv.hash); + return mapBlockIndex.count(nHash) || + mapOrphanBlocks.count(nHash); } // Don't know what it is, just say we already got one return true; @@ -3344,10 +3347,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) } // find last block in inv vector - size_t nLastBlock = std::numeric_limits::max(); + int nLastBlock = -1; for (size_t nInv = 0; nInv < vInv.size(); nInv++) { - if (vInv[vInv.size() - 1 - nInv].type == MSG_BLOCK) { - nLastBlock = vInv.size() - 1 - nInv; + if (vInv[vInv.size() - 1 - nInv].GetType() == MSG_BLOCK) { + nLastBlock = (int) (vInv.size() - 1 - nInv); break; } } @@ -3355,6 +3358,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) for (size_t nInv = 0; nInv < vInv.size(); nInv++) { const CInv &inv = vInv[nInv]; + int nType = inv.GetType(); + auto nHash = inv.GetHash(); if (fShutdown) return true; @@ -3366,19 +3371,19 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) if (!fAlreadyHave) pfrom->AskFor(inv); - else if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash)) { - pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(mapOrphanBlocks[inv.hash])); - } else if (nInv == nLastBlock) { + else if (nType == MSG_BLOCK && mapOrphanBlocks.count(nHash)) { + pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(mapOrphanBlocks[nHash])); + } else if (nType == nLastBlock) { // In case we are on a very long side-chain, it is possible that we already have // the last block in an inv bundle sent in response to getblocks. Try to detect // this situation and push another getblocks to continue. - pfrom->PushGetBlocks(mapBlockIndex[inv.hash], uint256(0)); + pfrom->PushGetBlocks(mapBlockIndex[nHash], uint256(0)); if (fDebug) printf("force request: %s\n", inv.ToString().c_str()); } // Track requests for our stuff - Inventory(inv.hash); + Inventory(nHash); } } @@ -3403,10 +3408,13 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) if (fDebugNet || (vInv.size() == 1)) printf("received getdata for: %s\n", inv.ToString().c_str()); - if (inv.type == MSG_BLOCK) + int nType = inv.GetType(); + auto nHash = inv.GetHash(); + + if (nType == MSG_BLOCK) { // Send block from disk - auto mi = mapBlockIndex.find(inv.hash); + auto mi = mapBlockIndex.find(nHash); if (mi != mapBlockIndex.end()) { CBlock block; @@ -3414,7 +3422,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) pfrom->PushMessage("block", block); // Trigger them to send a getblocks request for the next batch of inventory - if (inv.hash == pfrom->hashContinue) + if (nHash == pfrom->hashContinue) { // ppcoin: send latest proof-of-work block to allow the // download node to accept as orphan (proof-of-stake @@ -3438,10 +3446,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) pushed = true; } } - if (!pushed && inv.type == MSG_TX) { + if (!pushed && nType == MSG_TX) { LOCK(mempool.cs); - if (mempool.exists(inv.hash)) { - CTransaction tx = mempool.lookup(inv.hash); + if (mempool.exists(nHash)) { + CTransaction tx = mempool.lookup(nHash); CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); ss.reserve(1000); ss << tx; @@ -3451,7 +3459,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) } // Track requests for our stuff - Inventory(inv.hash); + Inventory(nHash); } } @@ -3558,11 +3566,13 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) bool fMissingInputs = false; if (tx.AcceptToMemoryPool(txdb, true, &fMissingInputs)) { + auto nHash = inv.GetHash(); + SyncWithWallets(tx, NULL, true); - RelayTransaction(tx, inv.hash); + RelayTransaction(tx, nHash); mapAlreadyAskedFor.erase(inv); - vWorkQueue.push_back(inv.hash); - vEraseQueue.push_back(inv.hash); + vWorkQueue.push_back(nHash); + vEraseQueue.push_back(nHash); // Recursively process any orphan transactions that depended on this one for (unsigned int i = 0; i < vWorkQueue.size(); i++) @@ -3973,13 +3983,13 @@ bool SendMessages(CNode* pto) continue; // trickle out tx inv to protect privacy - if (inv.type == MSG_TX && !fSendTrickle) + if (inv.GetType() == MSG_TX && !fSendTrickle) { // 1/4 of tx invs blast to all immediately static uint256 hashSalt; if (hashSalt == 0) hashSalt = GetRandHash(); - uint256 hashRand = inv.hash ^ hashSalt; + uint256 hashRand = inv.GetHash() ^ hashSalt; hashRand = Hash(hashRand.begin(), hashRand.end()); bool fTrickleWait = ((hashRand & 3) != 0); diff --git a/src/protocol.cpp b/src/protocol.cpp index d4b2808..676718d 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -65,7 +65,7 @@ bool CMessageHeader::IsValid() const CAddress::CAddress() : CService(), nServices(NODE_NETWORK), nTime(100000000), nLastTry(0) { } CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn), nServices(nServicesIn), nTime(100000000), nLastTry(0) { } -CInv::CInv() : type(0), hash(0) { } +CInv::CInv() { } CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) { } CInv::CInv(const std::string& strType, const uint256& hashIn) : hash(hashIn) { diff --git a/src/protocol.h b/src/protocol.h index 69072b1..352a247 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -118,10 +118,13 @@ class CInv const char* GetCommand() const; std::string ToString() const; + int GetType() const { return type; } + uint256 GetHash() const { return hash; } + // TODO: make private (improves encapsulation) - public: - int type; - uint256 hash; + private: + int type = 0; + uint256 hash = 0; }; #endif // __INCLUDED_PROTOCOL_H__