Skip to content

Commit

Permalink
Merge bitcoin#21719: refactor: Add and use EnsureConnman in rpc code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MarcoFalke authored and PastaPastaPasta committed Apr 23, 2024
1 parent 74b20eb commit 751c9e6
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 89 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ BITCOIN_CORE_H = \
rpc/blockchain.h \
rpc/client.h \
rpc/mining.h \
rpc/net.h \
rpc/protocol.h \
rpc/rawtransaction_util.h \
rpc/register.h \
Expand Down
28 changes: 14 additions & 14 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2256,7 +2256,7 @@ void CConnman::ProcessAddrFetch()
}
}

bool CConnman::GetTryNewOutboundPeer()
bool CConnman::GetTryNewOutboundPeer() const
{
return m_try_another_outbound_peer;
}
Expand All @@ -2273,7 +2273,7 @@ void CConnman::SetTryNewOutboundPeer(bool flag)
// Also exclude peers that haven't finished initial connection handshake yet
// (so that we don't decide we're over our desired connection limit, and then
// evict some peer that has finished the handshake)
int CConnman::GetExtraFullOutboundCount()
int CConnman::GetExtraFullOutboundCount() const
{
int full_outbound_peers = 0;
{
Expand All @@ -2291,7 +2291,7 @@ int CConnman::GetExtraFullOutboundCount()
return std::max(full_outbound_peers - m_max_outbound_full_relay, 0);
}

int CConnman::GetExtraBlockRelayCount()
int CConnman::GetExtraBlockRelayCount() const
{
int block_relay_peers = 0;
{
Expand Down Expand Up @@ -2619,7 +2619,7 @@ std::vector<CAddress> CConnman::GetCurrentBlockRelayOnlyConns() const
return ret;
}

std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
{
std::vector<AddedNodeInfo> ret;

Expand Down Expand Up @@ -3621,7 +3621,7 @@ CConnman::~CConnman()
Stop();
}

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

size_t CConnman::GetNodeCount(ConnectionDirection flags)
size_t CConnman::GetNodeCount(ConnectionDirection flags) const
{
LOCK(cs_vNodes);

Expand All @@ -3869,7 +3869,7 @@ size_t CConnman::GetMaxOutboundNodeCount()
return m_max_outbound;
}

void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats) const
{
vstats.clear();
LOCK(cs_vNodes);
Expand Down Expand Up @@ -4008,18 +4008,18 @@ void CConnman::RecordBytesSent(uint64_t bytes)
nMaxOutboundTotalBytesSentInCycle += bytes;
}

uint64_t CConnman::GetMaxOutboundTarget()
uint64_t CConnman::GetMaxOutboundTarget() const
{
LOCK(cs_totalBytesSent);
return nMaxOutboundLimit;
}

std::chrono::seconds CConnman::GetMaxOutboundTimeframe()
std::chrono::seconds CConnman::GetMaxOutboundTimeframe() const
{
return MAX_UPLOAD_TIMEFRAME;
}

std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle()
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const
{
LOCK(cs_totalBytesSent);
if (nMaxOutboundLimit == 0)
Expand All @@ -4033,7 +4033,7 @@ std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle()
return (cycleEndTime < now) ? 0s : cycleEndTime - now;
}

bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const
{
LOCK(cs_totalBytesSent);
if (nMaxOutboundLimit == 0)
Expand All @@ -4053,7 +4053,7 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
return false;
}

uint64_t CConnman::GetOutboundTargetBytesLeft()
uint64_t CConnman::GetOutboundTargetBytesLeft() const
{
LOCK(cs_totalBytesSent);
if (nMaxOutboundLimit == 0)
Expand All @@ -4062,13 +4062,13 @@ uint64_t CConnman::GetOutboundTargetBytesLeft()
return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
}

uint64_t CConnman::GetTotalBytesRecv()
uint64_t CConnman::GetTotalBytesRecv() const
{
LOCK(cs_totalBytesRecv);
return nTotalBytesRecv;
}

uint64_t CConnman::GetTotalBytesSent()
uint64_t CConnman::GetTotalBytesSent() const
{
LOCK(cs_totalBytesSent);
return nTotalBytesSent;
Expand Down
34 changes: 17 additions & 17 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ friend class CNode;
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
* @param[in] network Select only addresses of this network (nullopt = all).
*/
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network);
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;

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

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

bool AddNode(const std::string& node);
bool RemoveAddedNode(const std::string& node);
std::vector<AddedNodeInfo> GetAddedNodeInfo();
std::vector<AddedNodeInfo> GetAddedNodeInfo() const;

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

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

uint64_t GetMaxOutboundTarget();
std::chrono::seconds GetMaxOutboundTimeframe();
uint64_t GetMaxOutboundTarget() const;
std::chrono::seconds GetMaxOutboundTimeframe() const;

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

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

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

uint64_t GetTotalBytesRecv();
uint64_t GetTotalBytesSent();
uint64_t GetTotalBytesRecv() const;
uint64_t GetTotalBytesSent() const;

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

// Network usage totals
RecursiveMutex cs_totalBytesRecv;
RecursiveMutex cs_totalBytesSent;
mutable RecursiveMutex cs_totalBytesRecv;
mutable RecursiveMutex cs_totalBytesSent;
uint64_t nTotalBytesRecv GUARDED_BY(cs_totalBytesRecv) {0};
uint64_t nTotalBytesSent GUARDED_BY(cs_totalBytesSent) {0};

Expand All @@ -1371,7 +1371,7 @@ friend class CNode;
std::deque<std::string> m_addr_fetches GUARDED_BY(m_addr_fetches_mutex);
RecursiveMutex m_addr_fetches_mutex;
std::vector<std::string> vAddedNodes GUARDED_BY(cs_vAddedNodes);
RecursiveMutex cs_vAddedNodes;
mutable RecursiveMutex cs_vAddedNodes;
std::vector<uint256> vPendingMasternodes;
mutable RecursiveMutex cs_vPendingMasternodes;
std::map<std::pair<Consensus::LLMQType, uint256>, std::set<uint256>> masternodeQuorumNodes GUARDED_BY(cs_vPendingMasternodes);
Expand Down
4 changes: 2 additions & 2 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class PeerManagerImpl final : public PeerManager

/** Implement PeerManager */
void CheckForStaleTipAndEvictPeers() override;
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) override;
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override;
bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
void SendPings() override;
void RelayTransaction(const uint256& txid) override;
Expand Down Expand Up @@ -1440,7 +1440,7 @@ PeerRef PeerManagerImpl::RemovePeer(NodeId id)
return ret;
}

bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const
{
{
LOCK(cs_main);
Expand Down
2 changes: 1 addition & 1 deletion src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
virtual ~PeerManager() { }

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

/** Whether this node ignores txs received over p2p. */
virtual bool IgnoresIncomingTxs() = 0;
Expand Down
7 changes: 3 additions & 4 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <pow.h>
#include <rpc/blockchain.h>
#include <rpc/mining.h>
#include <rpc/net.h>
#include <rpc/server.h>
#include <rpc/util.h>
#include <script/descriptor.h>
Expand Down Expand Up @@ -731,10 +732,8 @@ static RPCHelpMan getblocktemplate()
if (strMode != "template")
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");

if(!node.connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");

if (node.connman->GetNodeCount(ConnectionDirection::Both) == 0)
const CConnman& connman = EnsureConnman(node);
if (connman.GetNodeCount(ConnectionDirection::Both) == 0)
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");

if (active_chainstate.IsInitialBlockDownload())
Expand Down
7 changes: 3 additions & 4 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <net.h>
#include <node/context.h>
#include <rpc/blockchain.h>
#include <rpc/net.h>
#include <rpc/server.h>
#include <rpc/util.h>
#include <scheduler.h>
Expand Down Expand Up @@ -213,15 +214,13 @@ static RPCHelpMan sporkupdate()
}

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

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

// broadcast new spork
if (node.sporkman->UpdateSpork(nSporkID, nValue, *node.connman)) {
if (node.sporkman->UpdateSpork(nSporkID, nValue, connman)) {
return "success";
}

Expand Down
Loading

0 comments on commit 751c9e6

Please sign in to comment.