From 6690a28606d6ada23355de7d9a9878f841b21e67 Mon Sep 17 00:00:00 2001 From: Jesse Cohen Date: Fri, 8 Jun 2018 15:09:20 -0400 Subject: [PATCH] Restrict as much as possible in net_processing to translation unit Mark everything else static or in an anonymous namespace. --- src/net_processing.cpp | 53 +++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 4d1ded60c72..77aa5939d92 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -37,9 +37,9 @@ #endif /** Expiration time for orphan transactions in seconds */ -static const int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60; +static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60; /** Minimum time between orphan transactions expire time checks in seconds */ -static const int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60; +static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60; /** Headers download timeout expressed in microseconds * Timeout = base + per_header * (expected number of headers) */ static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_BASE = 15 * 60 * 1000000; // 15 minutes @@ -56,27 +56,14 @@ static constexpr int64_t STALE_CHECK_INTERVAL = 10 * 60; // 10 minutes static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL = 45; /** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict, in seconds */ static constexpr int64_t MINIMUM_CONNECT_TIME = 30; - -static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL; // SHA256("main address relay")[0:8] - +/** SHA256("main address relay")[0:8] */ +static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL; /// Age after which a stale block will no longer be served if requested as /// protection against fingerprinting. Set to one month, denominated in seconds. -static const int STALE_RELAY_AGE_LIMIT = 30 * 24 * 60 * 60; - +static constexpr int STALE_RELAY_AGE_LIMIT = 30 * 24 * 60 * 60; /// Age after which a block is considered historical for purposes of rate /// limiting block relay. Set to one week, denominated in seconds. -static const int HISTORICAL_BLOCK_AGE = 7 * 24 * 60 * 60; - -std::atomic nTimeBestReceived(0); // Used only to inform the wallet of when we last received a block - -struct IteratorComparator -{ - template - bool operator()(const I& a, const I& b) const - { - return &(*a) < &(*b); - } -}; +static constexpr int HISTORICAL_BLOCK_AGE = 7 * 24 * 60 * 60; struct COrphanTx { // When modifying, adapt the copy of this definition in tests/DoS_tests. @@ -86,14 +73,12 @@ struct COrphanTx { }; static CCriticalSection g_cs_orphans; std::map mapOrphanTransactions GUARDED_BY(g_cs_orphans); -std::map::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(g_cs_orphans); + void EraseOrphansFor(NodeId peer); + /** Increase a node's misbehavior score. */ void Misbehaving(NodeId nodeid, int howmuch, const std::string& message=""); -static size_t vExtraTxnForCompactIt GUARDED_BY(g_cs_orphans) = 0; -static std::vector> vExtraTxnForCompact GUARDED_BY(g_cs_orphans); - // Internal stuff namespace { /** Enable BIP61 (sending reject messages) */ @@ -163,10 +148,24 @@ namespace { MapRelay mapRelay; /** Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main). */ std::deque> vRelayExpiration; + + std::atomic nTimeBestReceived(0); // Used only to inform the wallet of when we last received a block + + struct IteratorComparator + { + template + bool operator()(const I& a, const I& b) const + { + return &(*a) < &(*b); + } + }; + std::map::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(g_cs_orphans); + + static size_t vExtraTxnForCompactIt GUARDED_BY(g_cs_orphans) = 0; + static std::vector> vExtraTxnForCompact GUARDED_BY(g_cs_orphans); } // namespace namespace { - struct CBlockReject { unsigned char chRejectCode; std::string strRejectReason; @@ -293,10 +292,10 @@ struct CNodeState { }; /** Map maintaining per-node state. Requires cs_main. */ -std::map mapNodeState; +static std::map mapNodeState; // Requires cs_main. -CNodeState *State(NodeId pnode) { +static CNodeState *State(NodeId pnode) { std::map::iterator it = mapNodeState.find(pnode); if (it == mapNodeState.end()) return nullptr; @@ -3229,6 +3228,7 @@ void PeerLogicValidation::CheckForStaleTipAndEvictPeers(const Consensus::Params } } +namespace { class CompareInvMempoolOrder { CTxMemPool *mp; @@ -3245,6 +3245,7 @@ public: return mp->CompareDepthAndScore(*b, *a); } }; +} bool PeerLogicValidation::SendMessages(CNode* pto, std::atomic& interruptMsgProc) {