Skip to content

Commit f5b2ea3

Browse files
author
MarcoFalke
committed
Merge #20217: net: Remove g_relay_txes
34e33ab Remove g_relay_txes (John Newbery) 68334b3 [net processing] Add m_ignores_incoming_txs to PeerManager and use internally (John Newbery) 4d510aa [init] Use MakeUnique<> to construct peerman (John Newbery) f3f61d0 [net processing] Add IgnoresIncomingTxs() function to PeerManager (John Newbery) 5805b82 [net processing] Move PushNodeVersion into PeerManager (John Newbery) Pull request description: `g_relay_txes` is only required inside net_processing and is set only once at startup. Instead of having a global, move it to be a const member of PeerManager. This requires moving `PushNodeVersion()` into `PeerManager`, which also allows us to remove the `connman` argument. ACKs for top commit: narula: utACK 34e33ab MarcoFalke: re-ACK 34e33ab 💐 Tree-SHA512: 33f75b522e5f34b243731932eb96cd6c8ce9db69b5186395e3718858bc715cec1711a663c6afc5880462812cbc15040930e2dc648b2acad6bc6502ad1397c5e3
2 parents 0547106 + 34e33ab commit f5b2ea3

File tree

8 files changed

+66
-47
lines changed

8 files changed

+66
-47
lines changed

src/init.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,10 +1373,9 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
13731373
// is not yet setup and may end up being set up twice if we
13741374
// need to reindex later.
13751375

1376-
// see Step 2: parameter interactions for more information about these
13771376
fListen = args.GetBoolArg("-listen", DEFAULT_LISTEN);
13781377
fDiscover = args.GetBoolArg("-discover", true);
1379-
g_relay_txes = !args.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY);
1378+
const bool ignores_incoming_txs{args.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY)};
13801379

13811380
assert(!node.banman);
13821381
node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, args.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
@@ -1386,7 +1385,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
13861385
assert(!node.fee_estimator);
13871386
// Don't initialize fee estimation with old data if we don't relay transactions,
13881387
// as they would never get updated.
1389-
if (g_relay_txes) node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
1388+
if (!ignores_incoming_txs) node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
13901389

13911390
assert(!node.mempool);
13921391
int check_ratio = std::min<int>(std::max<int>(args.GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
@@ -1396,7 +1395,9 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
13961395
node.chainman = &g_chainman;
13971396
ChainstateManager& chainman = *Assert(node.chainman);
13981397

1399-
node.peerman.reset(new PeerManager(chainparams, *node.connman, node.banman.get(), *node.scheduler, chainman, *node.mempool));
1398+
assert(!node.peerman);
1399+
node.peerman = std::make_unique<PeerManager>(chainparams, *node.connman, node.banman.get(),
1400+
*node.scheduler, chainman, *node.mempool, ignores_incoming_txs);
14001401
RegisterValidationInterface(node.peerman.get());
14011402

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

src/net.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ static const uint64_t RANDOMIZER_ID_ADDRCACHE = 0x1cf2e4ddd306dda9ULL; // SHA256
111111
//
112112
bool fDiscover = true;
113113
bool fListen = true;
114-
bool g_relay_txes = !DEFAULT_BLOCKSONLY;
115114
RecursiveMutex cs_mapLocalHost;
116115
std::map<CNetAddr, LocalServiceInfo> mapLocalHost GUARDED_BY(cs_mapLocalHost);
117116
static bool vfLimited[NET_MAX] GUARDED_BY(cs_mapLocalHost) = {};

src/net.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,6 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
665665

666666
extern bool fDiscover;
667667
extern bool fListen;
668-
extern bool g_relay_txes;
669668

670669
/** Subversion as sent to the P2P network in `version` messages */
671670
extern std::string strSubVersion;

src/net_processing.cpp

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -432,32 +432,6 @@ static void UpdatePreferredDownload(const CNode& node, CNodeState* state) EXCLUS
432432
nPreferredDownload += state->fPreferredDownload;
433433
}
434434

435-
static void PushNodeVersion(CNode& pnode, CConnman& connman, int64_t nTime)
436-
{
437-
// Note that pnode->GetLocalServices() is a reflection of the local
438-
// services we were offering when the CNode object was created for this
439-
// peer.
440-
ServiceFlags nLocalNodeServices = pnode.GetLocalServices();
441-
uint64_t nonce = pnode.GetLocalNonce();
442-
int nNodeStartingHeight = pnode.GetMyStartingHeight();
443-
NodeId nodeid = pnode.GetId();
444-
CAddress addr = pnode.addr;
445-
446-
CAddress addrYou = addr.IsRoutable() && !IsProxy(addr) && addr.IsAddrV1Compatible() ?
447-
addr :
448-
CAddress(CService(), addr.nServices);
449-
CAddress addrMe = CAddress(CService(), nLocalNodeServices);
450-
451-
connman.PushMessage(&pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
452-
nonce, strSubVersion, nNodeStartingHeight, ::g_relay_txes && pnode.m_tx_relay != nullptr));
453-
454-
if (fLogIPs) {
455-
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);
456-
} else {
457-
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
458-
}
459-
}
460-
461435
// Returns a bool indicating whether we requested this block.
462436
// Also used if a block was /not/ received and timed out or started with another peer
463437
static bool MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
@@ -708,6 +682,32 @@ static void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vec
708682

709683
} // namespace
710684

685+
void PeerManager::PushNodeVersion(CNode& pnode, int64_t nTime)
686+
{
687+
// Note that pnode->GetLocalServices() is a reflection of the local
688+
// services we were offering when the CNode object was created for this
689+
// peer.
690+
ServiceFlags nLocalNodeServices = pnode.GetLocalServices();
691+
uint64_t nonce = pnode.GetLocalNonce();
692+
int nNodeStartingHeight = pnode.GetMyStartingHeight();
693+
NodeId nodeid = pnode.GetId();
694+
CAddress addr = pnode.addr;
695+
696+
CAddress addrYou = addr.IsRoutable() && !IsProxy(addr) && addr.IsAddrV1Compatible() ?
697+
addr :
698+
CAddress(CService(), addr.nServices);
699+
CAddress addrMe = CAddress(CService(), nLocalNodeServices);
700+
701+
m_connman.PushMessage(&pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
702+
nonce, strSubVersion, nNodeStartingHeight, !m_ignore_incoming_txs && pnode.m_tx_relay != nullptr));
703+
704+
if (fLogIPs) {
705+
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);
706+
} else {
707+
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
708+
}
709+
}
710+
711711
void PeerManager::AddTxAnnouncement(const CNode& node, const GenTxid& gtxid, std::chrono::microseconds current_time)
712712
{
713713
AssertLockHeld(::cs_main); // For m_txrequest
@@ -759,7 +759,7 @@ void PeerManager::InitializeNode(CNode *pnode) {
759759
m_peer_map.emplace_hint(m_peer_map.end(), nodeid, std::move(peer));
760760
}
761761
if (!pnode->IsInboundConn()) {
762-
PushNodeVersion(*pnode, m_connman, GetTime());
762+
PushNodeVersion(*pnode, GetTime());
763763
}
764764
}
765765

@@ -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));
@@ -2312,9 +2314,9 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
23122314
SeenLocal(addrMe);
23132315
}
23142316

2315-
// Be shy and don't send version until we hear
2316-
if (pfrom.IsInboundConn())
2317-
PushNodeVersion(pfrom, m_connman, GetAdjustedTime());
2317+
// Inbound peers send us their version message when they connect.
2318+
// We send our version message in response.
2319+
if (pfrom.IsInboundConn()) PushNodeVersion(pfrom, GetAdjustedTime());
23182320

23192321
// Change version
23202322
const int greatest_common_version = std::min(nVersion, PROTOCOL_VERSION);
@@ -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: 11 additions & 1 deletion
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.
@@ -138,6 +139,9 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
138139
/** Get statistics from node state */
139140
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats);
140141

142+
/** Whether this node ignores txs received over p2p. */
143+
bool IgnoresIncomingTxs() {return m_ignore_incoming_txs;};
144+
141145
private:
142146
/** Get a shared pointer to the Peer object.
143147
* May return an empty shared_ptr if the Peer object can't be found. */
@@ -186,6 +190,9 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
186190
void AddTxAnnouncement(const CNode& node, const GenTxid& gtxid, std::chrono::microseconds current_time)
187191
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
188192

193+
/** Send a version message to a peer */
194+
void PushNodeVersion(CNode& pnode, int64_t nTime);
195+
189196
const CChainParams& m_chainparams;
190197
CConnman& m_connman;
191198
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
@@ -196,6 +203,9 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
196203

197204
int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
198205

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

src/rpc/net.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,9 @@ static RPCHelpMan getnetworkinfo()
578578
obj.pushKV("localservices", strprintf("%016x", services));
579579
obj.pushKV("localservicesnames", GetServicesNames(services));
580580
}
581-
obj.pushKV("localrelay", g_relay_txes);
581+
if (node.peerman) {
582+
obj.pushKV("localrelay", !node.peerman->IgnoresIncomingTxs());
583+
}
582584
obj.pushKV("timeoffset", GetTimeOffset());
583585
if (node.connman) {
584586
obj.pushKV("networkactive", node.connman->GetNetworkActive());

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)