Skip to content

Commit 68334b3

Browse files
committed
[net processing] Add m_ignores_incoming_txs to PeerManager and use internally
1 parent 4d510aa commit 68334b3

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,8 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
13971397
ChainstateManager& chainman = *Assert(node.chainman);
13981398

13991399
assert(!node.peerman);
1400-
node.peerman = MakeUnique<PeerManager>(chainparams, *node.connman, node.banman.get(), *node.scheduler, chainman, *node.mempool);
1400+
node.peerman = std::make_unique<PeerManager>(chainparams, *node.connman, node.banman.get(),
1401+
*node.scheduler, chainman, *node.mempool, !g_relay_txes);
14011402
RegisterValidationInterface(node.peerman.get());
14021403

14031404
// sanitize comments per BIP-0014, format user agent and check total size

src/net_processing.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ void PeerManager::PushNodeVersion(CNode& pnode, int64_t nTime)
699699
CAddress addrMe = CAddress(CService(), nLocalNodeServices);
700700

701701
m_connman.PushMessage(&pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
702-
nonce, strSubVersion, nNodeStartingHeight, ::g_relay_txes && pnode.m_tx_relay != nullptr));
702+
nonce, strSubVersion, nNodeStartingHeight, !m_ignore_incoming_txs && pnode.m_tx_relay != nullptr));
703703

704704
if (fLogIPs) {
705705
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), nodeid);
@@ -1124,13 +1124,15 @@ static bool BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Para
11241124
}
11251125

11261126
PeerManager::PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
1127-
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool)
1127+
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
1128+
bool ignore_incoming_txs)
11281129
: m_chainparams(chainparams),
11291130
m_connman(connman),
11301131
m_banman(banman),
11311132
m_chainman(chainman),
11321133
m_mempool(pool),
1133-
m_stale_tip_check_time(0)
1134+
m_stale_tip_check_time(0),
1135+
m_ignore_incoming_txs(ignore_incoming_txs)
11341136
{
11351137
// Initialize global variables that cannot be constructed at startup.
11361138
recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
@@ -2624,7 +2626,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
26242626

26252627
// We won't accept tx inv's if we're in blocks-only mode, or this is a
26262628
// block-relay-only peer
2627-
bool fBlocksOnly = !g_relay_txes || (pfrom.m_tx_relay == nullptr);
2629+
bool fBlocksOnly = m_ignore_incoming_txs || (pfrom.m_tx_relay == nullptr);
26282630

26292631
// Allow peers with relay permission to send data other than blocks in blocks only mode
26302632
if (pfrom.HasPermission(PF_RELAY)) {
@@ -2901,7 +2903,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
29012903
// Stop processing the transaction early if
29022904
// 1) We are in blocks only mode and peer has no relay permission
29032905
// 2) This peer is a block-relay-only peer
2904-
if ((!g_relay_txes && !pfrom.HasPermission(PF_RELAY)) || (pfrom.m_tx_relay == nullptr))
2906+
if ((m_ignore_incoming_txs && !pfrom.HasPermission(PF_RELAY)) || (pfrom.m_tx_relay == nullptr))
29052907
{
29062908
LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom.GetId());
29072909
pfrom.fDisconnect = true;

src/net_processing.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ using PeerRef = std::shared_ptr<Peer>;
7676
class PeerManager final : public CValidationInterface, public NetEventsInterface {
7777
public:
7878
PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
79-
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool);
79+
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
80+
bool ignore_incoming_txs);
8081

8182
/**
8283
* Overridden from CValidationInterface.
@@ -139,7 +140,7 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
139140
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats);
140141

141142
/** Whether this node ignores txs received over p2p. */
142-
bool IgnoresIncomingTxs() {return !::g_relay_txes;};
143+
bool IgnoresIncomingTxs() {return m_ignore_incoming_txs;};
143144

144145
private:
145146
/** Get a shared pointer to the Peer object.
@@ -202,6 +203,9 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
202203

203204
int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
204205

206+
//* Whether this node is running in blocks only mode */
207+
const bool m_ignore_incoming_txs;
208+
205209
/** Protects m_peer_map */
206210
mutable Mutex m_peer_mutex;
207211
/**

src/test/denialofservice_tests.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
8080
{
8181
const CChainParams& chainparams = Params();
8282
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
83-
auto peerLogic = MakeUnique<PeerManager>(chainparams, *connman, nullptr, *m_node.scheduler, *m_node.chainman, *m_node.mempool);
83+
auto peerLogic = std::make_unique<PeerManager>(chainparams, *connman, nullptr, *m_node.scheduler,
84+
*m_node.chainman, *m_node.mempool, false);
8485

8586
// Mock an outbound peer
8687
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
@@ -149,7 +150,8 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
149150
{
150151
const CChainParams& chainparams = Params();
151152
auto connman = MakeUnique<CConnmanTest>(0x1337, 0x1337);
152-
auto peerLogic = MakeUnique<PeerManager>(chainparams, *connman, nullptr, *m_node.scheduler, *m_node.chainman, *m_node.mempool);
153+
auto peerLogic = std::make_unique<PeerManager>(chainparams, *connman, nullptr, *m_node.scheduler,
154+
*m_node.chainman, *m_node.mempool, false);
153155

154156
constexpr int max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS;
155157
CConnman::Options options;
@@ -222,7 +224,8 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
222224
const CChainParams& chainparams = Params();
223225
auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
224226
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
225-
auto peerLogic = MakeUnique<PeerManager>(chainparams, *connman, banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool);
227+
auto peerLogic = std::make_unique<PeerManager>(chainparams, *connman, banman.get(), *m_node.scheduler,
228+
*m_node.chainman, *m_node.mempool, false);
226229

227230
banman->ClearBanned();
228231
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
@@ -268,7 +271,8 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
268271
const CChainParams& chainparams = Params();
269272
auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
270273
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
271-
auto peerLogic = MakeUnique<PeerManager>(chainparams, *connman, banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool);
274+
auto peerLogic = std::make_unique<PeerManager>(chainparams, *connman, banman.get(), *m_node.scheduler,
275+
*m_node.chainman, *m_node.mempool, false);
272276

273277
banman->ClearBanned();
274278
int64_t nStartTime = GetTime();

src/test/util/setup_common.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
192192

193193
m_node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
194194
m_node.connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
195-
m_node.peerman = MakeUnique<PeerManager>(chainparams, *m_node.connman, m_node.banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool);
195+
m_node.peerman = std::make_unique<PeerManager>(chainparams, *m_node.connman, m_node.banman.get(),
196+
*m_node.scheduler, *m_node.chainman, *m_node.mempool,
197+
false);
196198
{
197199
CConnman::Options options;
198200
options.m_msgproc = m_node.peerman.get();

0 commit comments

Comments
 (0)