MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / AddPeer

Method AddPeer

src/test/net_peer_connection_tests.cpp:48–84  ·  view source on GitHub ↗

Create a peer and connect to it. If the optional `address` (IP/CJDNS only) isn't passed, a random address is created. */

Source from the content-addressed store, hash-verified

46struct PeerTest : LogIPsTestingSetup {
47/** Create a peer and connect to it. If the optional `address` (IP/CJDNS only) isn't passed, a random address is created. */
48void AddPeer(NodeId& id, std::vector<CNode*>& nodes, PeerManager& peerman, ConnmanTestMsg& connman, ConnectionType conn_type, bool onion_peer = false, std::optional<std::string> address = std::nullopt)
49{
50 CAddress addr{};
51
52 if (address.has_value()) {
53 addr = CAddress{MaybeFlipIPv6toCJDNS(LookupNumeric(address.value(), Params().GetDefaultPort())), NODE_NONE};
54 } else if (onion_peer) {
55 auto tor_addr{m_rng.randbytes(ADDR_TORV3_SIZE)};
56 BOOST_REQUIRE(addr.SetSpecial(OnionToString(tor_addr)));
57 }
58
59 while (!addr.IsLocal() && !addr.IsRoutable()) {
60 addr = CAddress{ip(m_rng.randbits(32)), NODE_NONE};
61 }
62
63 BOOST_REQUIRE(addr.IsValid());
64
65 const bool inbound_onion{onion_peer && conn_type == ConnectionType::INBOUND};
66
67 nodes.emplace_back(new CNode{++id,
68 /*sock=*/nullptr,
69 addr,
70 /*nKeyedNetGroupIn=*/0,
71 /*nLocalHostNonceIn=*/0,
72 CAddress{},
73 /*addrNameIn=*/"",
74 conn_type,
75 /*inbound_onion=*/inbound_onion,
76 /*network_key=*/0});
77 CNode& node = *nodes.back();
78 node.SetCommonVersion(PROTOCOL_VERSION);
79
80 peerman.InitializeNode(node, ServiceFlags(NODE_NETWORK | NODE_WITNESS));
81 node.fSuccessfullyConnected = true;
82
83 connman.AddTestNode(node);
84}
85}; // struct PeerTest
86
87BOOST_FIXTURE_TEST_CASE(test_addnode_getaddednodeinfo_and_connection_detection, PeerTest)

Callers

nothing calls this directly

Calls 15

MaybeFlipIPv6toCJDNSFunction · 0.85
LookupNumericFunction · 0.85
OnionToStringFunction · 0.85
randbytesMethod · 0.80
SetSpecialMethod · 0.80
IsLocalMethod · 0.80
IsRoutableMethod · 0.80
randbitsMethod · 0.80
SetCommonVersionMethod · 0.80
AddTestNodeMethod · 0.80
ipFunction · 0.70
ParamsClass · 0.50

Tested by

no test coverage detected