@@ -39,6 +39,22 @@ const std::vector<std::string> CONNECTION_TYPE_DOC{
39
39
" feeler (short-lived automatic connection for testing addresses)"
40
40
};
41
41
42
+ CConnman& EnsureConnman (const NodeContext& node)
43
+ {
44
+ if (!node.connman ) {
45
+ throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
46
+ }
47
+ return *node.connman ;
48
+ }
49
+
50
+ PeerManager& EnsurePeerman (const NodeContext& node)
51
+ {
52
+ if (!node.peerman ) {
53
+ throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
54
+ }
55
+ return *node.peerman ;
56
+ }
57
+
42
58
static RPCHelpMan getconnectioncount ()
43
59
{
44
60
return RPCHelpMan{" getconnectioncount" ,
@@ -54,10 +70,9 @@ static RPCHelpMan getconnectioncount()
54
70
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
55
71
{
56
72
NodeContext& node = EnsureAnyNodeContext (request.context );
57
- if (!node.connman )
58
- throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
73
+ const CConnman& connman = EnsureConnman (node);
59
74
60
- return (int )node. connman -> GetNodeCount (ConnectionDirection::Both);
75
+ return (int )connman. GetNodeCount (ConnectionDirection::Both);
61
76
},
62
77
};
63
78
}
@@ -77,12 +92,10 @@ static RPCHelpMan ping()
77
92
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
78
93
{
79
94
NodeContext& node = EnsureAnyNodeContext (request.context );
80
- if (!node.peerman ) {
81
- throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
82
- }
95
+ PeerManager& peerman = EnsurePeerman (node);
83
96
84
97
// Request that each node send a ping during next message processing pass
85
- node. peerman -> SendPings ();
98
+ peerman. SendPings ();
86
99
return NullUniValue;
87
100
},
88
101
};
@@ -166,19 +179,18 @@ static RPCHelpMan getpeerinfo()
166
179
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
167
180
{
168
181
NodeContext& node = EnsureAnyNodeContext (request.context );
169
- if (!node.connman || !node.peerman ) {
170
- throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
171
- }
182
+ const CConnman& connman = EnsureConnman (node);
183
+ const PeerManager& peerman = EnsurePeerman (node);
172
184
173
185
std::vector<CNodeStats> vstats;
174
- node. connman -> GetNodeStats (vstats);
186
+ connman. GetNodeStats (vstats);
175
187
176
188
UniValue ret (UniValue::VARR);
177
189
178
190
for (const CNodeStats& stats : vstats) {
179
191
UniValue obj (UniValue::VOBJ);
180
192
CNodeStateStats statestats;
181
- bool fStateStats = node. peerman -> GetNodeStateStats (stats.nodeid , statestats);
193
+ bool fStateStats = peerman. GetNodeStateStats (stats.nodeid , statestats);
182
194
obj.pushKV (" id" , stats.nodeid );
183
195
obj.pushKV (" addr" , stats.addrName );
184
196
if (stats.addrBind .IsValid ()) {
@@ -286,27 +298,28 @@ static RPCHelpMan addnode()
286
298
}
287
299
288
300
NodeContext& node = EnsureAnyNodeContext (request.context );
289
- if (!node.connman )
290
- throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
301
+ CConnman& connman = EnsureConnman (node);
291
302
292
303
std::string strNode = request.params [0 ].get_str ();
293
304
294
305
if (strCommand == " onetry" )
295
306
{
296
307
CAddress addr;
297
- node. connman -> OpenNetworkConnection (addr, false , nullptr , strNode.c_str (), ConnectionType::MANUAL);
308
+ connman. OpenNetworkConnection (addr, false , nullptr , strNode.c_str (), ConnectionType::MANUAL);
298
309
return NullUniValue;
299
310
}
300
311
301
312
if (strCommand == " add" )
302
313
{
303
- if (!node. connman -> AddNode (strNode))
314
+ if (! connman. AddNode (strNode)) {
304
315
throw JSONRPCError (RPC_CLIENT_NODE_ALREADY_ADDED, " Error: Node already added" );
316
+ }
305
317
}
306
318
else if (strCommand == " remove" )
307
319
{
308
- if (!node. connman -> RemoveAddedNode (strNode))
320
+ if (! connman. RemoveAddedNode (strNode)) {
309
321
throw JSONRPCError (RPC_CLIENT_NODE_NOT_ADDED, " Error: Node could not be removed. It has not been added previously." );
322
+ }
310
323
}
311
324
312
325
return NullUniValue;
@@ -351,11 +364,9 @@ static RPCHelpMan addconnection()
351
364
}
352
365
353
366
NodeContext& node = EnsureAnyNodeContext (request.context );
354
- if (!node.connman ) {
355
- throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled." );
356
- }
367
+ CConnman& connman = EnsureConnman (node);
357
368
358
- const bool success = node. connman -> AddConnection (address, conn_type);
369
+ const bool success = connman. AddConnection (address, conn_type);
359
370
if (!success) {
360
371
throw JSONRPCError (RPC_CLIENT_NODE_CAPACITY_REACHED, " Error: Already at capacity for specified connection type." );
361
372
}
@@ -389,20 +400,19 @@ static RPCHelpMan disconnectnode()
389
400
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
390
401
{
391
402
NodeContext& node = EnsureAnyNodeContext (request.context );
392
- if (!node.connman )
393
- throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
403
+ CConnman& connman = EnsureConnman (node);
394
404
395
405
bool success;
396
406
const UniValue &address_arg = request.params [0 ];
397
407
const UniValue &id_arg = request.params [1 ];
398
408
399
409
if (!address_arg.isNull () && id_arg.isNull ()) {
400
410
/* handle disconnect-by-address */
401
- success = node. connman -> DisconnectNode (address_arg.get_str ());
411
+ success = connman. DisconnectNode (address_arg.get_str ());
402
412
} else if (!id_arg.isNull () && (address_arg.isNull () || (address_arg.isStr () && address_arg.get_str ().empty ()))) {
403
413
/* handle disconnect-by-id */
404
414
NodeId nodeid = (NodeId) id_arg.get_int64 ();
405
- success = node. connman -> DisconnectNode (nodeid);
415
+ success = connman. DisconnectNode (nodeid);
406
416
} else {
407
417
throw JSONRPCError (RPC_INVALID_PARAMS, " Only one of address and nodeid should be provided." );
408
418
}
@@ -449,10 +459,9 @@ static RPCHelpMan getaddednodeinfo()
449
459
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
450
460
{
451
461
NodeContext& node = EnsureAnyNodeContext (request.context );
452
- if (!node.connman )
453
- throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
462
+ const CConnman& connman = EnsureConnman (node);
454
463
455
- std::vector<AddedNodeInfo> vInfo = node. connman -> GetAddedNodeInfo ();
464
+ std::vector<AddedNodeInfo> vInfo = connman. GetAddedNodeInfo ();
456
465
457
466
if (!request.params [0 ].isNull ()) {
458
467
bool found = false ;
@@ -520,21 +529,20 @@ static RPCHelpMan getnettotals()
520
529
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
521
530
{
522
531
NodeContext& node = EnsureAnyNodeContext (request.context );
523
- if (!node.connman )
524
- throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
532
+ const CConnman& connman = EnsureConnman (node);
525
533
526
534
UniValue obj (UniValue::VOBJ);
527
- obj.pushKV (" totalbytesrecv" , node. connman -> GetTotalBytesRecv ());
528
- obj.pushKV (" totalbytessent" , node. connman -> GetTotalBytesSent ());
535
+ obj.pushKV (" totalbytesrecv" , connman. GetTotalBytesRecv ());
536
+ obj.pushKV (" totalbytessent" , connman. GetTotalBytesSent ());
529
537
obj.pushKV (" timemillis" , GetTimeMillis ());
530
538
531
539
UniValue outboundLimit (UniValue::VOBJ);
532
- outboundLimit.pushKV (" timeframe" , count_seconds (node. connman -> GetMaxOutboundTimeframe ()));
533
- outboundLimit.pushKV (" target" , node. connman -> GetMaxOutboundTarget ());
534
- outboundLimit.pushKV (" target_reached" , node. connman -> OutboundTargetReached (false ));
535
- outboundLimit.pushKV (" serve_historical_blocks" , !node. connman -> OutboundTargetReached (true ));
536
- outboundLimit.pushKV (" bytes_left_in_cycle" , node. connman -> GetOutboundTargetBytesLeft ());
537
- outboundLimit.pushKV (" time_left_in_cycle" , count_seconds (node. connman -> GetMaxOutboundTimeLeftInCycle ()));
540
+ outboundLimit.pushKV (" timeframe" , count_seconds (connman. GetMaxOutboundTimeframe ()));
541
+ outboundLimit.pushKV (" target" , connman. GetMaxOutboundTarget ());
542
+ outboundLimit.pushKV (" target_reached" , connman. OutboundTargetReached (false ));
543
+ outboundLimit.pushKV (" serve_historical_blocks" , !connman. OutboundTargetReached (true ));
544
+ outboundLimit.pushKV (" bytes_left_in_cycle" , connman. GetOutboundTargetBytesLeft ());
545
+ outboundLimit.pushKV (" time_left_in_cycle" , count_seconds (connman. GetMaxOutboundTimeLeftInCycle ()));
538
546
obj.pushKV (" uploadtarget" , outboundLimit);
539
547
return obj;
540
548
},
@@ -826,13 +834,11 @@ static RPCHelpMan setnetworkactive()
826
834
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
827
835
{
828
836
NodeContext& node = EnsureAnyNodeContext (request.context );
829
- if (!node.connman ) {
830
- throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
831
- }
837
+ CConnman& connman = EnsureConnman (node);
832
838
833
- node. connman -> SetNetworkActive (request.params [0 ].get_bool ());
839
+ connman. SetNetworkActive (request.params [0 ].get_bool ());
834
840
835
- return node. connman -> GetNetworkActive ();
841
+ return connman. GetNetworkActive ();
836
842
},
837
843
};
838
844
}
@@ -864,15 +870,13 @@ static RPCHelpMan getnodeaddresses()
864
870
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
865
871
{
866
872
NodeContext& node = EnsureAnyNodeContext (request.context );
867
- if (!node.connman ) {
868
- throw JSONRPCError (RPC_CLIENT_P2P_DISABLED, " Error: Peer-to-peer functionality missing or disabled" );
869
- }
873
+ const CConnman& connman = EnsureConnman (node);
870
874
871
875
const int count{request.params [0 ].isNull () ? 1 : request.params [0 ].get_int ()};
872
876
if (count < 0 ) throw JSONRPCError (RPC_INVALID_PARAMETER, " Address count out of range" );
873
877
874
878
// returns a shuffled list of CAddress
875
- const std::vector<CAddress> vAddr{node. connman -> GetAddresses (count, /* max_pct */ 0 )};
879
+ const std::vector<CAddress> vAddr{connman. GetAddresses (count, /* max_pct */ 0 )};
876
880
UniValue ret (UniValue::VARR);
877
881
878
882
for (const CAddress& addr : vAddr) {
0 commit comments