23 #include <boost/test/unit_test.hpp> 47 static std::string
name() {
return "p2pTestCapability"; }
52 virtual bool interpret(
unsigned _id,
RLP const& _r)
override {
return _id > 0 || _r.
size() > 0; }
67 if (test::Options::get().nonetwork)
75 BOOST_REQUIRE(host1port);
80 BOOST_REQUIRE(host2port);
82 BOOST_REQUIRE_NE(host1port, host2port);
87 auto node2 = host2.
id();
91 for (
unsigned i = 0; i < 6000; i += step)
93 this_thread::sleep_for(chrono::milliseconds(step));
102 for (
unsigned i = 0; i < 6000; i += step)
104 this_thread::sleep_for(chrono::milliseconds(step));
114 for (
unsigned i = 0; i < 24000; i += step)
116 this_thread::sleep_for(chrono::milliseconds(step));
122 BOOST_REQUIRE_EQUAL(host1.
peerCount(), 1);
123 BOOST_REQUIRE_EQUAL(host2.
peerCount(), 1);
128 if (test::Options::get().nonetwork)
135 BOOST_REQUIRE(save.
id() == restore.id());
140 if (test::Options::get().nonetwork)
145 std::list<Host*> hosts;
146 unsigned const c_step = 10;
147 unsigned const c_nodes = 6;
148 unsigned const c_peers = c_nodes - 1;
149 std::set<short unsigned> ports;
151 for (
unsigned i = 0; i < c_nodes; ++i)
157 this_thread::sleep_for(chrono::milliseconds(c_step));
160 bool inserted = ports.insert(h->
listenPort()).second;
161 BOOST_REQUIRE(inserted);
166 Host& host = *hosts.front();
167 for (
auto const&
h: hosts)
170 for (
unsigned i = 0; i < c_peers * 1000 && host.
peerCount() < c_peers; i += c_step)
171 this_thread::sleep_for(chrono::milliseconds(c_step));
173 Host& host2 = *hosts.back();
174 for (
auto const&
h: hosts)
177 for (
unsigned i = 0; i < c_peers * 2000 && host2.
peerCount() < c_peers; i += c_step)
178 this_thread::sleep_for(chrono::milliseconds(c_step));
185 BOOST_REQUIRE_EQUAL(
sha3(firstHostNetwork),
sha3(secondHostNetwork));
187 RLP r(firstHostNetwork);
190 BOOST_REQUIRE_EQUAL(r[1].toBytes().
size(), 32);
191 BOOST_REQUIRE(r[2].itemCount() >= c_nodes);
195 BOOST_REQUIRE(i.itemCount() == 4 || i.itemCount() == 11);
196 BOOST_REQUIRE(i[0].
size() == 4 || i[0].
size() == 16);
199 for (
auto host: hosts)
209 if (test::Options::get().nonetwork)
214 unsigned const step = 10;
215 const char*
const localhost =
"127.0.0.1";
218 Host host1(
"Test", prefs1);
219 Host host2(
"Test", prefs2);
222 auto node2 = host2.
id();
225 BOOST_REQUIRE(port1);
226 BOOST_REQUIRE(port2);
227 BOOST_REQUIRE_NE(port1, port2);
235 for (
unsigned i = 0; i < 12000; i += step)
237 this_thread::sleep_for(chrono::milliseconds(step));
245 BOOST_REQUIRE_EQUAL(host1peerCount, 1);
246 BOOST_REQUIRE_EQUAL(host2peerCount, 1);
251 BOOST_REQUIRE_EQUAL(sis1.size(), 1);
252 BOOST_REQUIRE_EQUAL(sis2.size(), 1);
256 BOOST_REQUIRE_EQUAL(peers1.size(), 1);
257 BOOST_REQUIRE_EQUAL(peers2.size(), 1);
261 BOOST_REQUIRE_EQUAL(disconnect1, disconnect2);
266 for (
unsigned i = 0; i < 6000; i += step)
268 this_thread::sleep_for(chrono::milliseconds(step));
276 BOOST_REQUIRE_EQUAL(host1peerCount, 1);
277 BOOST_REQUIRE_EQUAL(host2peerCount, 1);
282 BOOST_AUTO_TEST_SUITE(peerTypes)
286 if (test::Options::get().nonetwork)
292 std::map<NodeID, std::shared_ptr<Peer>> peers;
297 BOOST_REQUIRE(!p->id);
301 BOOST_REQUIRE(!(!*p));
311 short listenPort = 30304;
313 short remotePort = 30304;
315 for (
int i = 1; i < argc; ++i)
317 string arg = argv[i];
318 if (arg ==
"-l" && i + 1 < argc)
319 listenPort = (short)
atoi(argv[++i]);
320 else if (arg ==
"-r" && i + 1 < argc)
321 remoteHost = argv[++i];
322 else if (arg ==
"-p" && i + 1 < argc)
323 remotePort = (short)
atoi(argv[++i]);
324 else if (arg ==
"-ra" && i + 1 < argc)
327 remoteHost = argv[i];
332 if (!remoteHost.empty() && !remoteAlias)
333 ph.
addNode(remoteAlias,
NodeIPEndpoint(bi::address::from_string(remoteHost), remotePort, remotePort));
335 this_thread::sleep_for(chrono::milliseconds(200));
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
void setIdealPeerCount(unsigned _n)
Set ideal number of peers.
std::pair< std::string, u256 > CapDesc
The Host class Capabilities should be registered prior to startNetwork, since m_capabilities is not t...
std::vector< PeerSessionInfo > PeerSessionInfos
static unsigned messageCount()
unsigned short listenPort() const
Get the port we're listening on currently.
std::hash for asio::adress
virtual bool interpret(unsigned _id, RLP const &_r) override
const unsigned c_protocolVersion
Peer network protocol version.
h512 Public
A public key: 64 bytes.
size_t peerCount() const
Get number of peers connected.
bytes saveNetwork() const
Serialise the set of known peers.
BOOST_AUTO_TEST_CASE(host)
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
NodeID id() const
Get our current node ID.
std::vector< Peer > Peers
PeerSessionInfos peerSessionInfo() const
Get peer information.
std::vector< byte > bytes
const Node UnspecifiedNode
void requirePeer(NodeID const &_node, NodeIPEndpoint const &_endpoint)
Create Peer and attempt keeping peer connected.
vector_ref< byte const > bytesConstRef
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
int peerTest(int argc, char **argv)
Temporary changes system's verbosity for specific function.
void addNode(NodeID const &_node, NodeIPEndpoint const &_endpoint)
Add node as a peer candidate. Node is added if discovery ping is successful and table has capacity...
void start()
Start network. .
std::shared_ptr< T > registerCapability(std::shared_ptr< T > const &_t)
Register a peer-capability; all new peer connections will have this capability.
#define BOOST_FIXTURE_TEST_SUITE(a, b)
uint8_t const size_t const size
#define BOOST_CHECK_EQUAL(v1, v2)
void relinquishPeer(NodeID const &_node)
Note peer as no longer being required.
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
#define BOOST_AUTO_TEST_SUITE_END()
static std::string name()
Representation of connectivity state and all other pertinent Peer metadata.
const NodeIPEndpoint UnspecifiedNodeIPEndpoint
Class for interpreting Recursive Linear-Prefix Data.
TestCap(std::shared_ptr< SessionFace > _s, HostCapabilityFace *_h, unsigned _idOffset, CapDesc const &, uint16_t _capID)
int atoi(const std::string &str)
static bool test_allowLocal
Setting true causes isAllowed to return true for all addresses. (Used by test fixtures) ...
Helper functions to work with json::spirit and test files.