Skip to content

Commit 751c9e6

Browse files
MarcoFalkePastaPastaPasta
authored andcommitted
Merge bitcoin#21719: refactor: Add and use EnsureConnman in rpc code
fafb68a refactor: Add and use EnsureConnman in rpc code (MarcoFalke) faabeb8 refactor: Mark member functions const (MarcoFalke) Pull request description: This removes the 10 occurrences of `throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");` and replaces them with `EnsureConnman`. ACKs for top commit: jarolrod: re-ACK fafb68a theStack: ACK fafb68a ryanofsky: Code review ACK fafb68a Tree-SHA512: 84c63cfe31e548645d906f7191a3526c7bea99ed0d54c2a75c2041452a44fe149ede343d8e1943b0e7770816c828bb047dfec8bc541a1f2b89920a126ee54d68
1 parent 74b20eb commit 751c9e6

File tree

9 files changed

+107
-89
lines changed

9 files changed

+107
-89
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ BITCOIN_CORE_H = \
286286
rpc/blockchain.h \
287287
rpc/client.h \
288288
rpc/mining.h \
289+
rpc/net.h \
289290
rpc/protocol.h \
290291
rpc/rawtransaction_util.h \
291292
rpc/register.h \

src/net.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,7 +2256,7 @@ void CConnman::ProcessAddrFetch()
22562256
}
22572257
}
22582258

2259-
bool CConnman::GetTryNewOutboundPeer()
2259+
bool CConnman::GetTryNewOutboundPeer() const
22602260
{
22612261
return m_try_another_outbound_peer;
22622262
}
@@ -2273,7 +2273,7 @@ void CConnman::SetTryNewOutboundPeer(bool flag)
22732273
// Also exclude peers that haven't finished initial connection handshake yet
22742274
// (so that we don't decide we're over our desired connection limit, and then
22752275
// evict some peer that has finished the handshake)
2276-
int CConnman::GetExtraFullOutboundCount()
2276+
int CConnman::GetExtraFullOutboundCount() const
22772277
{
22782278
int full_outbound_peers = 0;
22792279
{
@@ -2291,7 +2291,7 @@ int CConnman::GetExtraFullOutboundCount()
22912291
return std::max(full_outbound_peers - m_max_outbound_full_relay, 0);
22922292
}
22932293

2294-
int CConnman::GetExtraBlockRelayCount()
2294+
int CConnman::GetExtraBlockRelayCount() const
22952295
{
22962296
int block_relay_peers = 0;
22972297
{
@@ -2619,7 +2619,7 @@ std::vector<CAddress> CConnman::GetCurrentBlockRelayOnlyConns() const
26192619
return ret;
26202620
}
26212621

2622-
std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
2622+
std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
26232623
{
26242624
std::vector<AddedNodeInfo> ret;
26252625

@@ -3621,7 +3621,7 @@ CConnman::~CConnman()
36213621
Stop();
36223622
}
36233623

3624-
std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network)
3624+
std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
36253625
{
36263626
std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct, network);
36273627
if (m_banman) {
@@ -3842,7 +3842,7 @@ void CConnman::AddPendingProbeConnections(const std::set<uint256> &proTxHashes)
38423842
masternodePendingProbes.insert(proTxHashes.begin(), proTxHashes.end());
38433843
}
38443844

3845-
size_t CConnman::GetNodeCount(ConnectionDirection flags)
3845+
size_t CConnman::GetNodeCount(ConnectionDirection flags) const
38463846
{
38473847
LOCK(cs_vNodes);
38483848

@@ -3869,7 +3869,7 @@ size_t CConnman::GetMaxOutboundNodeCount()
38693869
return m_max_outbound;
38703870
}
38713871

3872-
void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
3872+
void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats) const
38733873
{
38743874
vstats.clear();
38753875
LOCK(cs_vNodes);
@@ -4008,18 +4008,18 @@ void CConnman::RecordBytesSent(uint64_t bytes)
40084008
nMaxOutboundTotalBytesSentInCycle += bytes;
40094009
}
40104010

4011-
uint64_t CConnman::GetMaxOutboundTarget()
4011+
uint64_t CConnman::GetMaxOutboundTarget() const
40124012
{
40134013
LOCK(cs_totalBytesSent);
40144014
return nMaxOutboundLimit;
40154015
}
40164016

4017-
std::chrono::seconds CConnman::GetMaxOutboundTimeframe()
4017+
std::chrono::seconds CConnman::GetMaxOutboundTimeframe() const
40184018
{
40194019
return MAX_UPLOAD_TIMEFRAME;
40204020
}
40214021

4022-
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle()
4022+
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const
40234023
{
40244024
LOCK(cs_totalBytesSent);
40254025
if (nMaxOutboundLimit == 0)
@@ -4033,7 +4033,7 @@ std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle()
40334033
return (cycleEndTime < now) ? 0s : cycleEndTime - now;
40344034
}
40354035

4036-
bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
4036+
bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const
40374037
{
40384038
LOCK(cs_totalBytesSent);
40394039
if (nMaxOutboundLimit == 0)
@@ -4053,7 +4053,7 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
40534053
return false;
40544054
}
40554055

4056-
uint64_t CConnman::GetOutboundTargetBytesLeft()
4056+
uint64_t CConnman::GetOutboundTargetBytesLeft() const
40574057
{
40584058
LOCK(cs_totalBytesSent);
40594059
if (nMaxOutboundLimit == 0)
@@ -4062,13 +4062,13 @@ uint64_t CConnman::GetOutboundTargetBytesLeft()
40624062
return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
40634063
}
40644064

4065-
uint64_t CConnman::GetTotalBytesRecv()
4065+
uint64_t CConnman::GetTotalBytesRecv() const
40664066
{
40674067
LOCK(cs_totalBytesRecv);
40684068
return nTotalBytesRecv;
40694069
}
40704070

4071-
uint64_t CConnman::GetTotalBytesSent()
4071+
uint64_t CConnman::GetTotalBytesSent() const
40724072
{
40734073
LOCK(cs_totalBytesSent);
40744074
return nTotalBytesSent;

src/net.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ friend class CNode;
11281128
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
11291129
* @param[in] network Select only addresses of this network (nullopt = all).
11301130
*/
1131-
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network);
1131+
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;
11321132

11331133
/**
11341134
* Cache is used to minimize topology leaks, so it should
@@ -1141,7 +1141,7 @@ friend class CNode;
11411141
// This allows temporarily exceeding m_max_outbound_full_relay, with the goal of finding
11421142
// a peer that is better than all our current peers.
11431143
void SetTryNewOutboundPeer(bool flag);
1144-
bool GetTryNewOutboundPeer();
1144+
bool GetTryNewOutboundPeer() const;
11451145

11461146
void StartExtraBlockRelayPeers() {
11471147
LogPrint(BCLog::NET, "net: enabling extra block-relay-only peers\n");
@@ -1154,13 +1154,13 @@ friend class CNode;
11541154
// return a value less than (num_outbound_connections - num_outbound_slots)
11551155
// in cases where some outbound connections are not yet fully connected, or
11561156
// not yet fully disconnected.
1157-
int GetExtraFullOutboundCount();
1157+
int GetExtraFullOutboundCount() const;
11581158
// Count the number of block-relay-only peers we have over our limit.
1159-
int GetExtraBlockRelayCount();
1159+
int GetExtraBlockRelayCount() const;
11601160

11611161
bool AddNode(const std::string& node);
11621162
bool RemoveAddedNode(const std::string& node);
1163-
std::vector<AddedNodeInfo> GetAddedNodeInfo();
1163+
std::vector<AddedNodeInfo> GetAddedNodeInfo() const;
11641164

11651165
/**
11661166
* Attempts to open a connection. Currently only used from tests.
@@ -1187,9 +1187,9 @@ friend class CNode;
11871187
bool IsMasternodeQuorumRelayMember(const uint256& protxHash);
11881188
void AddPendingProbeConnections(const std::set<uint256>& proTxHashes);
11891189

1190-
size_t GetNodeCount(ConnectionDirection);
1190+
size_t GetNodeCount(ConnectionDirection) const;
11911191
size_t GetMaxOutboundNodeCount();
1192-
void GetNodeStats(std::vector<CNodeStats>& vstats);
1192+
void GetNodeStats(std::vector<CNodeStats>& vstats) const;
11931193
bool DisconnectNode(const std::string& node);
11941194
bool DisconnectNode(const CSubNet& subnet);
11951195
bool DisconnectNode(const CNetAddr& addr);
@@ -1203,24 +1203,24 @@ friend class CNode;
12031203
//! that peer during `net_processing.cpp:PushNodeVersion()`.
12041204
ServiceFlags GetLocalServices() const;
12051205

1206-
uint64_t GetMaxOutboundTarget();
1207-
std::chrono::seconds GetMaxOutboundTimeframe();
1206+
uint64_t GetMaxOutboundTarget() const;
1207+
std::chrono::seconds GetMaxOutboundTimeframe() const;
12081208

12091209
//! check if the outbound target is reached
12101210
//! if param historicalBlockServingLimit is set true, the function will
12111211
//! response true if the limit for serving historical blocks has been reached
1212-
bool OutboundTargetReached(bool historicalBlockServingLimit);
1212+
bool OutboundTargetReached(bool historicalBlockServingLimit) const;
12131213

12141214
//! response the bytes left in the current max outbound cycle
12151215
//! in case of no limit, it will always response 0
1216-
uint64_t GetOutboundTargetBytesLeft();
1216+
uint64_t GetOutboundTargetBytesLeft() const;
12171217

12181218
//! returns the time left in the current max outbound cycle
12191219
//! in case of no limit, it will always return 0
1220-
std::chrono::seconds GetMaxOutboundTimeLeftInCycle();
1220+
std::chrono::seconds GetMaxOutboundTimeLeftInCycle() const;
12211221

1222-
uint64_t GetTotalBytesRecv();
1223-
uint64_t GetTotalBytesSent();
1222+
uint64_t GetTotalBytesRecv() const;
1223+
uint64_t GetTotalBytesSent() const;
12241224

12251225
/** Get a unique deterministic randomizer. */
12261226
CSipHasher GetDeterministicRandomizer(uint64_t id) const;
@@ -1344,8 +1344,8 @@ friend class CNode;
13441344
void UnregisterEvents(CNode* pnode);
13451345

13461346
// Network usage totals
1347-
RecursiveMutex cs_totalBytesRecv;
1348-
RecursiveMutex cs_totalBytesSent;
1347+
mutable RecursiveMutex cs_totalBytesRecv;
1348+
mutable RecursiveMutex cs_totalBytesSent;
13491349
uint64_t nTotalBytesRecv GUARDED_BY(cs_totalBytesRecv) {0};
13501350
uint64_t nTotalBytesSent GUARDED_BY(cs_totalBytesSent) {0};
13511351

@@ -1371,7 +1371,7 @@ friend class CNode;
13711371
std::deque<std::string> m_addr_fetches GUARDED_BY(m_addr_fetches_mutex);
13721372
RecursiveMutex m_addr_fetches_mutex;
13731373
std::vector<std::string> vAddedNodes GUARDED_BY(cs_vAddedNodes);
1374-
RecursiveMutex cs_vAddedNodes;
1374+
mutable RecursiveMutex cs_vAddedNodes;
13751375
std::vector<uint256> vPendingMasternodes;
13761376
mutable RecursiveMutex cs_vPendingMasternodes;
13771377
std::map<std::pair<Consensus::LLMQType, uint256>, std::set<uint256>> masternodeQuorumNodes GUARDED_BY(cs_vPendingMasternodes);

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ class PeerManagerImpl final : public PeerManager
355355

356356
/** Implement PeerManager */
357357
void CheckForStaleTipAndEvictPeers() override;
358-
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) override;
358+
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override;
359359
bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
360360
void SendPings() override;
361361
void RelayTransaction(const uint256& txid) override;
@@ -1440,7 +1440,7 @@ PeerRef PeerManagerImpl::RemovePeer(NodeId id)
14401440
return ret;
14411441
}
14421442

1443-
bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
1443+
bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const
14441444
{
14451445
{
14461446
LOCK(cs_main);

src/net_processing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
6363
virtual ~PeerManager() { }
6464

6565
/** Get statistics from node state */
66-
virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) = 0;
66+
virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
6767

6868
/** Whether this node ignores txs received over p2p. */
6969
virtual bool IgnoresIncomingTxs() = 0;

src/rpc/mining.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <pow.h>
2727
#include <rpc/blockchain.h>
2828
#include <rpc/mining.h>
29+
#include <rpc/net.h>
2930
#include <rpc/server.h>
3031
#include <rpc/util.h>
3132
#include <script/descriptor.h>
@@ -731,10 +732,8 @@ static RPCHelpMan getblocktemplate()
731732
if (strMode != "template")
732733
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
733734

734-
if(!node.connman)
735-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
736-
737-
if (node.connman->GetNodeCount(ConnectionDirection::Both) == 0)
735+
const CConnman& connman = EnsureConnman(node);
736+
if (connman.GetNodeCount(ConnectionDirection::Both) == 0)
738737
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
739738

740739
if (active_chainstate.IsInitialBlockDownload())

src/rpc/misc.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <net.h>
2020
#include <node/context.h>
2121
#include <rpc/blockchain.h>
22+
#include <rpc/net.h>
2223
#include <rpc/server.h>
2324
#include <rpc/util.h>
2425
#include <scheduler.h>
@@ -213,15 +214,13 @@ static RPCHelpMan sporkupdate()
213214
}
214215

215216
const NodeContext& node = EnsureAnyNodeContext(request.context);
216-
if (!node.connman) {
217-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
218-
}
217+
CConnman& connman = EnsureConnman(node);
219218

220219
// SPORK VALUE
221220
int64_t nValue = request.params[1].get_int64();
222221

223222
// broadcast new spork
224-
if (node.sporkman->UpdateSpork(nSporkID, nValue, *node.connman)) {
223+
if (node.sporkman->UpdateSpork(nSporkID, nValue, connman)) {
225224
return "success";
226225
}
227226

0 commit comments

Comments
 (0)