aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--net.cpp11
-rw-r--r--peer.cpp78
-rw-r--r--whisperTopic.cpp49
3 files changed, 108 insertions, 30 deletions
diff --git a/net.cpp b/net.cpp
index 67c50dae..7f30ed03 100644
--- a/net.cpp
+++ b/net.cpp
@@ -30,7 +30,7 @@ using namespace dev::p2p;
namespace ba = boost::asio;
namespace bi = ba::ip;
-BOOST_AUTO_TEST_SUITE(p2p)
+BOOST_AUTO_TEST_SUITE(net)
/**
* Only used for testing. Not useful beyond tests.
@@ -190,6 +190,9 @@ BOOST_AUTO_TEST_CASE(kademlia)
node.populateAll();
clog << "NodeTable:\n" << *node.nodeTable.get() << endl;
+ auto nodes = node.nodeTable->nodes();
+ nodes.sort();
+
node.nodeTable->reset();
clog << "NodeTable:\n" << *node.nodeTable.get() << endl;
@@ -199,6 +202,12 @@ BOOST_AUTO_TEST_CASE(kademlia)
node.nodeTable->join();
this_thread::sleep_for(chrono::milliseconds(2000));
clog << "NodeTable:\n" << *node.nodeTable.get() << endl;
+
+ BOOST_REQUIRE_EQUAL(node.nodeTable->size(), 8);
+
+ auto netNodes = node.nodeTable->nodes();
+ netNodes.sort();
+
}
BOOST_AUTO_TEST_CASE(test_udp_once)
diff --git a/peer.cpp b/peer.cpp
index a99ce720..7f3c19e1 100644
--- a/peer.cpp
+++ b/peer.cpp
@@ -20,6 +20,7 @@
* Peer Network test functions.
*/
+#include <boost/test/unit_test.hpp>
#include <chrono>
#include <thread>
#include <libp2p/Host.h>
@@ -27,12 +28,74 @@ using namespace std;
using namespace dev;
using namespace dev::p2p;
+BOOST_AUTO_TEST_SUITE(p2p)
+
+BOOST_AUTO_TEST_CASE(host)
+{
+ NetworkPreferences host1prefs(30301, "127.0.0.1", true, true);
+ NetworkPreferences host2prefs(30302, "127.0.0.1", true, true);
+
+ Host host1("Test", host1prefs);
+ host1.start();
+
+ Host host2("Test", host2prefs);
+ auto node2 = host2.id();
+ host2.start();
+
+ host1.addNode(node2, "127.0.0.1", host2prefs.listenPort, host2prefs.listenPort);
+
+ this_thread::sleep_for(chrono::seconds(1));
+
+ BOOST_REQUIRE_EQUAL(host1.peerCount(), 1);
+ BOOST_REQUIRE_EQUAL(host2.peerCount(), host1.peerCount());
+}
+
+BOOST_AUTO_TEST_CASE(save_nodes)
+{
+ std::list<Host*> hosts;
+ for (auto i:{0,1,2,3,4,5})
+ {
+ Host* h = new Host("Test", NetworkPreferences(30300 + i, "127.0.0.1", true, true));
+ // starting host is required so listenport is available
+ h->start();
+ while (!h->isStarted())
+ this_thread::sleep_for(chrono::milliseconds(2));
+ hosts.push_back(h);
+ }
+
+ Host& host = *hosts.front();
+ for (auto const& h: hosts)
+ host.addNode(h->id(), "127.0.0.1", h->listenPort(), h->listenPort());
+
+ Host& host2 = *hosts.back();
+ for (auto const& h: hosts)
+ host2.addNode(h->id(), "127.0.0.1", h->listenPort(), h->listenPort());
+
+ this_thread::sleep_for(chrono::milliseconds(1000));
+ bytes firstHostNetwork(host.saveNetwork());
+ bytes secondHostNetwork(host.saveNetwork());
+
+ BOOST_REQUIRE_EQUAL(sha3(firstHostNetwork), sha3(secondHostNetwork));
+
+ BOOST_CHECK_EQUAL(host.peerCount(), 5);
+ BOOST_CHECK_EQUAL(host2.peerCount(), 5);
+
+ RLP r(firstHostNetwork);
+ BOOST_REQUIRE(r.itemCount() == 3);
+ BOOST_REQUIRE(r[0].toInt<int>() == 1);
+ BOOST_REQUIRE_EQUAL(r[1].toBytes().size(), 32); // secret
+ BOOST_REQUIRE_EQUAL(r[2].itemCount(), 5);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
int peerTest(int argc, char** argv)
{
+ Public remoteAlias;
short listenPort = 30303;
string remoteHost;
short remotePort = 30303;
-
+
for (int i = 1; i < argc; ++i)
{
string arg = argv[i];
@@ -42,21 +105,18 @@ int peerTest(int argc, char** argv)
remoteHost = argv[++i];
else if (arg == "-p" && i + 1 < argc)
remotePort = (short)atoi(argv[++i]);
+ else if (arg == "-ra" && i + 1 < argc)
+ remoteAlias = Public(dev::fromHex(argv[++i]));
else
remoteHost = argv[i];
}
Host ph("Test", NetworkPreferences(listenPort));
- if (!remoteHost.empty())
- ph.connect(remoteHost, remotePort);
+ if (!remoteHost.empty() && !remoteAlias)
+ ph.addNode(remoteAlias, remoteHost, remotePort, remotePort);
- for (int i = 0; ; ++i)
- {
- this_thread::sleep_for(chrono::milliseconds(100));
- if (!(i % 10))
- ph.pingAll();
- }
+ this_thread::sleep_for(chrono::milliseconds(200));
return 0;
}
diff --git a/whisperTopic.cpp b/whisperTopic.cpp
index 11937336..9c88d1b2 100644
--- a/whisperTopic.cpp
+++ b/whisperTopic.cpp
@@ -36,25 +36,35 @@ BOOST_AUTO_TEST_CASE(topic)
auto oldLogVerbosity = g_logVerbosity;
g_logVerbosity = 0;
+ Host phOther("Test", NetworkPreferences(30303, "127.0.0.1", false, true));
+ auto whOther = phOther.registerCapability(new WhisperHost());
+ phOther.start();
+
+ Host ph("Test", NetworkPreferences(30300, "127.0.0.1", false, true));
+ auto wh = ph.registerCapability(new WhisperHost());
+ ph.start();
+
+ this_thread::sleep_for(chrono::milliseconds(100));
+ ph.addNode(phOther.id(), "127.0.0.1", 30303, 30303);
+
+ this_thread::sleep_for(chrono::milliseconds(500));
+
bool started = false;
unsigned result = 0;
std::thread listener([&]()
{
setThreadName("other");
-
- Host ph("Test", NetworkPreferences(50303, "", false, true));
- auto wh = ph.registerCapability(new WhisperHost());
- ph.start();
+ started = true;
/// Only interested in odd packets
- auto w = wh->installWatch(BuildTopicMask("odd"));
+ auto w = whOther->installWatch(BuildTopicMask("odd"));
started = true;
set<unsigned> received;
for (int iterout = 0, last = 0; iterout < 200 && last < 81; ++iterout)
{
- for (auto i: wh->checkWatch(w))
+ for (auto i: whOther->checkWatch(w))
{
Message msg = wh->envelope(i).open(wh->fullTopic(w));
last = RLP(msg.payload()).toInt<unsigned>();
@@ -66,17 +76,11 @@ BOOST_AUTO_TEST_CASE(topic)
}
this_thread::sleep_for(chrono::milliseconds(50));
}
+
});
while (!started)
- this_thread::sleep_for(chrono::milliseconds(50));
-
- Host ph("Test", NetworkPreferences(50300, "", false, true));
- shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost());
- this_thread::sleep_for(chrono::milliseconds(500));
- ph.start();
- this_thread::sleep_for(chrono::milliseconds(500));
- ph.connect("127.0.0.1", 50303);
+ this_thread::sleep_for(chrono::milliseconds(2));
KeyPair us = KeyPair::create();
for (int i = 0; i < 10; ++i)
@@ -101,6 +105,7 @@ BOOST_AUTO_TEST_CASE(forwarding)
bool done = false;
bool startedListener = false;
+ Public phid;
std::thread listener([&]()
{
setThreadName("listener");
@@ -110,6 +115,7 @@ BOOST_AUTO_TEST_CASE(forwarding)
ph.setIdealPeerCount(0);
auto wh = ph.registerCapability(new WhisperHost());
ph.start();
+ phid = ph.id();
startedListener = true;
@@ -130,6 +136,7 @@ BOOST_AUTO_TEST_CASE(forwarding)
});
bool startedForwarder = false;
+ Public fwderid;
std::thread forwarder([&]()
{
setThreadName("forwarder");
@@ -143,9 +150,10 @@ BOOST_AUTO_TEST_CASE(forwarding)
auto wh = ph.registerCapability(new WhisperHost());
this_thread::sleep_for(chrono::milliseconds(500));
ph.start();
+ fwderid = ph.id();
this_thread::sleep_for(chrono::milliseconds(500));
- ph.connect("127.0.0.1", 50303);
+ ph.addNode(phid, "127.0.0.1", 50303, 50303);
startedForwarder = true;
@@ -172,7 +180,7 @@ BOOST_AUTO_TEST_CASE(forwarding)
this_thread::sleep_for(chrono::milliseconds(500));
ph.start();
this_thread::sleep_for(chrono::milliseconds(500));
- ph.connect("127.0.0.1", 50305);
+ ph.addNode(fwderid, "127.0.0.1", 50305, 50305);
KeyPair us = KeyPair::create();
wh->post(us.sec(), RLPStream().append(1).out(), BuildTopic("test"));
@@ -195,6 +203,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
unsigned result = 0;
bool done = false;
+ Public listenerid;
bool startedForwarder = false;
std::thread forwarder([&]()
{
@@ -206,9 +215,9 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
auto wh = ph.registerCapability(new WhisperHost());
this_thread::sleep_for(chrono::milliseconds(500));
ph.start();
-
+
this_thread::sleep_for(chrono::milliseconds(500));
- ph.connect("127.0.0.1", 50303);
+// ph.addNode("127.0.0.1", 50303, 50303);
startedForwarder = true;
@@ -236,7 +245,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
this_thread::sleep_for(chrono::milliseconds(500));
ph.start();
this_thread::sleep_for(chrono::milliseconds(500));
- ph.connect("127.0.0.1", 50305);
+// ph.addNode("127.0.0.1", 50305, 50305);
KeyPair us = KeyPair::create();
wh->post(us.sec(), RLPStream().append(1).out(), BuildTopic("test"));
@@ -250,7 +259,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
this_thread::sleep_for(chrono::milliseconds(500));
ph.start();
this_thread::sleep_for(chrono::milliseconds(500));
- ph.connect("127.0.0.1", 50305);
+// ph.addNode("127.0.0.1", 50305, 50305);
/// Only interested in odd packets
auto w = wh->installWatch(BuildTopicMask("test"));