Skip to content

Commit 2bce932

Browse files
author
MarcoFalke
committed
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
2 parents f385ad7 + fafb68a commit 2bce932

File tree

8 files changed

+104
-85
lines changed

8 files changed

+104
-85
lines changed

src/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ BITCOIN_CORE_H = \
199199
rpc/blockchain.h \
200200
rpc/client.h \
201201
rpc/mining.h \
202+
rpc/net.h \
202203
rpc/protocol.h \
203204
rpc/rawtransaction_util.h \
204205
rpc/register.h \

src/net.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,7 @@ void CConnman::ProcessAddrFetch()
17321732
}
17331733
}
17341734

1735-
bool CConnman::GetTryNewOutboundPeer()
1735+
bool CConnman::GetTryNewOutboundPeer() const
17361736
{
17371737
return m_try_another_outbound_peer;
17381738
}
@@ -1749,7 +1749,7 @@ void CConnman::SetTryNewOutboundPeer(bool flag)
17491749
// Also exclude peers that haven't finished initial connection handshake yet
17501750
// (so that we don't decide we're over our desired connection limit, and then
17511751
// evict some peer that has finished the handshake)
1752-
int CConnman::GetExtraFullOutboundCount()
1752+
int CConnman::GetExtraFullOutboundCount() const
17531753
{
17541754
int full_outbound_peers = 0;
17551755
{
@@ -1763,7 +1763,7 @@ int CConnman::GetExtraFullOutboundCount()
17631763
return std::max(full_outbound_peers - m_max_outbound_full_relay, 0);
17641764
}
17651765

1766-
int CConnman::GetExtraBlockRelayCount()
1766+
int CConnman::GetExtraBlockRelayCount() const
17671767
{
17681768
int block_relay_peers = 0;
17691769
{
@@ -2061,7 +2061,7 @@ std::vector<CAddress> CConnman::GetCurrentBlockRelayOnlyConns() const
20612061
return ret;
20622062
}
20632063

2064-
std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
2064+
std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
20652065
{
20662066
std::vector<AddedNodeInfo> ret;
20672067

@@ -2671,7 +2671,7 @@ CConnman::~CConnman()
26712671
Stop();
26722672
}
26732673

2674-
std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct)
2674+
std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct) const
26752675
{
26762676
std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct);
26772677
if (m_banman) {
@@ -2746,7 +2746,7 @@ bool CConnman::RemoveAddedNode(const std::string& strNode)
27462746
return false;
27472747
}
27482748

2749-
size_t CConnman::GetNodeCount(ConnectionDirection flags)
2749+
size_t CConnman::GetNodeCount(ConnectionDirection flags) const
27502750
{
27512751
LOCK(cs_vNodes);
27522752
if (flags == ConnectionDirection::Both) // Shortcut if we want total
@@ -2762,7 +2762,7 @@ size_t CConnman::GetNodeCount(ConnectionDirection flags)
27622762
return nNum;
27632763
}
27642764

2765-
void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
2765+
void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats) const
27662766
{
27672767
vstats.clear();
27682768
LOCK(cs_vNodes);
@@ -2839,18 +2839,18 @@ void CConnman::RecordBytesSent(uint64_t bytes)
28392839
nMaxOutboundTotalBytesSentInCycle += bytes;
28402840
}
28412841

2842-
uint64_t CConnman::GetMaxOutboundTarget()
2842+
uint64_t CConnman::GetMaxOutboundTarget() const
28432843
{
28442844
LOCK(cs_totalBytesSent);
28452845
return nMaxOutboundLimit;
28462846
}
28472847

2848-
std::chrono::seconds CConnman::GetMaxOutboundTimeframe()
2848+
std::chrono::seconds CConnman::GetMaxOutboundTimeframe() const
28492849
{
28502850
return MAX_UPLOAD_TIMEFRAME;
28512851
}
28522852

2853-
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle()
2853+
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const
28542854
{
28552855
LOCK(cs_totalBytesSent);
28562856
if (nMaxOutboundLimit == 0)
@@ -2864,7 +2864,7 @@ std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle()
28642864
return (cycleEndTime < now) ? 0s : cycleEndTime - now;
28652865
}
28662866

2867-
bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
2867+
bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const
28682868
{
28692869
LOCK(cs_totalBytesSent);
28702870
if (nMaxOutboundLimit == 0)
@@ -2884,7 +2884,7 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
28842884
return false;
28852885
}
28862886

2887-
uint64_t CConnman::GetOutboundTargetBytesLeft()
2887+
uint64_t CConnman::GetOutboundTargetBytesLeft() const
28882888
{
28892889
LOCK(cs_totalBytesSent);
28902890
if (nMaxOutboundLimit == 0)
@@ -2893,13 +2893,13 @@ uint64_t CConnman::GetOutboundTargetBytesLeft()
28932893
return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
28942894
}
28952895

2896-
uint64_t CConnman::GetTotalBytesRecv()
2896+
uint64_t CConnman::GetTotalBytesRecv() const
28972897
{
28982898
LOCK(cs_totalBytesRecv);
28992899
return nTotalBytesRecv;
29002900
}
29012901

2902-
uint64_t CConnman::GetTotalBytesSent()
2902+
uint64_t CConnman::GetTotalBytesSent() const
29032903
{
29042904
LOCK(cs_totalBytesSent);
29052905
return nTotalBytesSent;

src/net.h

+17-17
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ class CConnman
923923
};
924924

925925
// Addrman functions
926-
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct);
926+
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct) const;
927927
/**
928928
* Cache is used to minimize topology leaks, so it should
929929
* be used for all non-trusted calls, for example, p2p.
@@ -935,7 +935,7 @@ class CConnman
935935
// This allows temporarily exceeding m_max_outbound_full_relay, with the goal of finding
936936
// a peer that is better than all our current peers.
937937
void SetTryNewOutboundPeer(bool flag);
938-
bool GetTryNewOutboundPeer();
938+
bool GetTryNewOutboundPeer() const;
939939

940940
void StartExtraBlockRelayPeers() {
941941
LogPrint(BCLog::NET, "net: enabling extra block-relay-only peers\n");
@@ -948,13 +948,13 @@ class CConnman
948948
// return a value less than (num_outbound_connections - num_outbound_slots)
949949
// in cases where some outbound connections are not yet fully connected, or
950950
// not yet fully disconnected.
951-
int GetExtraFullOutboundCount();
951+
int GetExtraFullOutboundCount() const;
952952
// Count the number of block-relay-only peers we have over our limit.
953-
int GetExtraBlockRelayCount();
953+
int GetExtraBlockRelayCount() const;
954954

955955
bool AddNode(const std::string& node);
956956
bool RemoveAddedNode(const std::string& node);
957-
std::vector<AddedNodeInfo> GetAddedNodeInfo();
957+
std::vector<AddedNodeInfo> GetAddedNodeInfo() const;
958958

959959
/**
960960
* Attempts to open a connection. Currently only used from tests.
@@ -969,8 +969,8 @@ class CConnman
969969
*/
970970
bool AddConnection(const std::string& address, ConnectionType conn_type);
971971

972-
size_t GetNodeCount(ConnectionDirection);
973-
void GetNodeStats(std::vector<CNodeStats>& vstats);
972+
size_t GetNodeCount(ConnectionDirection) const;
973+
void GetNodeStats(std::vector<CNodeStats>& vstats) const;
974974
bool DisconnectNode(const std::string& node);
975975
bool DisconnectNode(const CSubNet& subnet);
976976
bool DisconnectNode(const CNetAddr& addr);
@@ -984,24 +984,24 @@ class CConnman
984984
//! that peer during `net_processing.cpp:PushNodeVersion()`.
985985
ServiceFlags GetLocalServices() const;
986986

987-
uint64_t GetMaxOutboundTarget();
988-
std::chrono::seconds GetMaxOutboundTimeframe();
987+
uint64_t GetMaxOutboundTarget() const;
988+
std::chrono::seconds GetMaxOutboundTimeframe() const;
989989

990990
//! check if the outbound target is reached
991991
//! if param historicalBlockServingLimit is set true, the function will
992992
//! response true if the limit for serving historical blocks has been reached
993-
bool OutboundTargetReached(bool historicalBlockServingLimit);
993+
bool OutboundTargetReached(bool historicalBlockServingLimit) const;
994994

995995
//! response the bytes left in the current max outbound cycle
996996
//! in case of no limit, it will always response 0
997-
uint64_t GetOutboundTargetBytesLeft();
997+
uint64_t GetOutboundTargetBytesLeft() const;
998998

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

1003-
uint64_t GetTotalBytesRecv();
1004-
uint64_t GetTotalBytesSent();
1003+
uint64_t GetTotalBytesRecv() const;
1004+
uint64_t GetTotalBytesSent() const;
10051005

10061006
/** Get a unique deterministic randomizer. */
10071007
CSipHasher GetDeterministicRandomizer(uint64_t id) const;
@@ -1106,8 +1106,8 @@ class CConnman
11061106
static bool NodeFullyConnected(const CNode* pnode);
11071107

11081108
// Network usage totals
1109-
RecursiveMutex cs_totalBytesRecv;
1110-
RecursiveMutex cs_totalBytesSent;
1109+
mutable RecursiveMutex cs_totalBytesRecv;
1110+
mutable RecursiveMutex cs_totalBytesSent;
11111111
uint64_t nTotalBytesRecv GUARDED_BY(cs_totalBytesRecv) {0};
11121112
uint64_t nTotalBytesSent GUARDED_BY(cs_totalBytesSent) {0};
11131113

@@ -1133,7 +1133,7 @@ class CConnman
11331133
std::deque<std::string> m_addr_fetches GUARDED_BY(m_addr_fetches_mutex);
11341134
RecursiveMutex m_addr_fetches_mutex;
11351135
std::vector<std::string> vAddedNodes GUARDED_BY(cs_vAddedNodes);
1136-
RecursiveMutex cs_vAddedNodes;
1136+
mutable RecursiveMutex cs_vAddedNodes;
11371137
std::vector<CNode*> vNodes GUARDED_BY(cs_vNodes);
11381138
std::list<CNode*> vNodesDisconnected;
11391139
mutable RecursiveMutex cs_vNodes;

src/net_processing.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class PeerManagerImpl final : public PeerManager
246246

247247
/** Implement PeerManager */
248248
void CheckForStaleTipAndEvictPeers() override;
249-
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) override;
249+
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override;
250250
bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
251251
void SendPings() override;
252252
void RelayTransaction(const uint256& txid, const uint256& wtxid) override;
@@ -1103,7 +1103,7 @@ PeerRef PeerManagerImpl::RemovePeer(NodeId id)
11031103
return ret;
11041104
}
11051105

1106-
bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
1106+
bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const
11071107
{
11081108
{
11091109
LOCK(cs_main);

src/net_processing.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
4343
virtual ~PeerManager() { }
4444

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

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

src/rpc/mining.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <pow.h>
1919
#include <rpc/blockchain.h>
2020
#include <rpc/mining.h>
21+
#include <rpc/net.h>
2122
#include <rpc/server.h>
2223
#include <rpc/util.h>
2324
#include <script/descriptor.h>
@@ -671,11 +672,9 @@ static RPCHelpMan getblocktemplate()
671672
if (strMode != "template")
672673
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
673674

674-
if(!node.connman)
675-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
676-
677675
if (!Params().IsTestChain()) {
678-
if (node.connman->GetNodeCount(ConnectionDirection::Both) == 0) {
676+
const CConnman& connman = EnsureConnman(node);
677+
if (connman.GetNodeCount(ConnectionDirection::Both) == 0) {
679678
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
680679
}
681680

0 commit comments

Comments
 (0)