Fabcoin Core  0.16.2
P2P Digital Currency
whisperTopic.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
21 #include <functional>
22 
23 #include <boost/test/unit_test.hpp>
24 
25 #include <libp2p/Host.h>
26 #include <libp2p/Session.h>
27 #include <libwhisper/WhisperPeer.h>
28 #include <libwhisper/WhisperHost.h>
30 
31 using namespace std;
32 using namespace dev;
33 using namespace dev::test;
34 using namespace dev::p2p;
35 using namespace dev::shh;
36 
37 struct P2PFixture: public TestOutputHelper
38 {
41  TestOutputHelper testHelper;
42 };
43 
45 
46 //
47 // Disabled tests as they are unstable and tend to stall the test suite.
48 //
49 
50 //BOOST_AUTO_TEST_CASE(topic)
51 //{
52 // cnote << "Testing Whisper...";
53 // VerbosityHolder setTemporaryLevel(0);
54 
55 // Host host1("Test", NetworkPreferences("127.0.0.1", 0, false));
56 // host1.setIdealPeerCount(1);
57 // auto whost1 = host1.registerCapability(make_shared<WhisperHost>());
58 // host1.start();
59 // auto port1 = host1.listenPort();
60 // BOOST_REQUIRE(port1);
61 
62 // bool host1Ready = false;
63 // unsigned result = 0;
64 // unsigned const step = 10;
65 
66 // std::thread listener([&]()
67 // {
68 // setThreadName("other");
69 
70 // /// Only interested in odd packets
71 // auto w = whost1->installWatch(BuildTopicMask("odd"));
72 // host1Ready = true;
73 // set<unsigned> received;
74 // for (int iterout = 0, last = 0; iterout < 200 && last < 81; ++iterout)
75 // {
76 // for (auto i: whost1->checkWatch(w))
77 // {
78 // Message msg = whost1->envelope(i).open(whost1->fullTopics(w));
79 // last = RLP(msg.payload()).toInt<unsigned>();
80 // if (received.count(last))
81 // continue;
82 // received.insert(last);
83 // cnote << "New message from:" << msg.from() << RLP(msg.payload()).toInt<unsigned>();
84 // result += last;
85 // }
86 // this_thread::sleep_for(chrono::milliseconds(50));
87 // }
88 // });
89 
90 // Host host2("Test", NetworkPreferences("127.0.0.1", 0, false));
91 // host2.setIdealPeerCount(1);
92 // auto whost2 = host2.registerCapability(make_shared<WhisperHost>());
93 // host2.start();
94 // auto port2 = host2.listenPort();
95 // BOOST_REQUIRE(port2);
96 // BOOST_REQUIRE_NE(port1, port2);
97 
98 // for (unsigned i = 0; i < 3000 && (!host1.haveNetwork() || !host2.haveNetwork()); i += step)
99 // this_thread::sleep_for(chrono::milliseconds(step));
100 
101 // BOOST_REQUIRE(host1.haveNetwork());
102 // BOOST_REQUIRE(host2.haveNetwork());
103 
104 // host2.addNode(host1.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), port1, port1));
105 
106 // for (unsigned i = 0; i < 3000 && (!host1.peerCount() || !host2.peerCount()); i += step)
107 // this_thread::sleep_for(chrono::milliseconds(step));
108 
109 // BOOST_REQUIRE(host1.peerCount());
110 // BOOST_REQUIRE(host2.peerCount());
111 
112 // for (unsigned i = 0; i < 3000 && !host1Ready; i += step)
113 // this_thread::sleep_for(chrono::milliseconds(step));
114 
115 // BOOST_REQUIRE(host1Ready);
116 
117 // KeyPair us = KeyPair::create();
118 // for (int i = 0; i < 10; ++i)
119 // {
120 // whost2->post(us.secret(), RLPStream().append(i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even"));
121 // this_thread::sleep_for(chrono::milliseconds(50));
122 // }
123 
124 // listener.join();
125 // BOOST_REQUIRE_EQUAL(result, 1 + 9 + 25 + 49 + 81);
126 //}
127 
128 //BOOST_AUTO_TEST_CASE(forwarding)
129 //{
130 // cnote << "Testing Whisper forwarding...";
131 // VerbosityHolder setTemporaryLevel(0);
132 
133 // // Host must be configured not to share peers.
134 // Host host1("Listner", NetworkPreferences("127.0.0.1", 0, false));
135 // host1.setIdealPeerCount(1);
136 // auto whost1 = host1.registerCapability(make_shared<WhisperHost>());
137 // host1.start();
138 // auto port1 = host1.listenPort();
139 // BOOST_REQUIRE(port1);
140 
141 // while (!host1.haveNetwork())
142 // this_thread::sleep_for(chrono::milliseconds(2));
143 
144 // unsigned result = 0;
145 // bool done = false;
146 
147 // bool startedListener = false;
148 // std::thread listener([&]()
149 // {
150 // setThreadName("listener");
151 
152 // startedListener = true;
153 
154 // /// Only interested in odd packets
155 // auto w = whost1->installWatch(BuildTopicMask("test"));
156 
157 // for (int i = 0; i < 200 && !result; ++i)
158 // {
159 // for (auto i: whost1->checkWatch(w))
160 // {
161 // Message msg = whost1->envelope(i).open(whost1->fullTopics(w));
162 // unsigned last = RLP(msg.payload()).toInt<unsigned>();
163 // cnote << "New message from:" << msg.from() << RLP(msg.payload()).toInt<unsigned>();
164 // result = last;
165 // }
166 // this_thread::sleep_for(chrono::milliseconds(50));
167 // }
168 // });
169 
170 
171 // // Host must be configured not to share peers.
172 // Host host2("Forwarder", NetworkPreferences("127.0.0.1", 0, false));
173 // host2.setIdealPeerCount(1);
174 // auto whost2 = host2.registerCapability(make_shared<WhisperHost>());
175 // host2.start();
176 // auto port2 = host2.listenPort();
177 // BOOST_REQUIRE(port2);
178 // BOOST_REQUIRE_NE(port1, port2);
179 
180 // while (!host2.haveNetwork())
181 // this_thread::sleep_for(chrono::milliseconds(2));
182 
183 // Public fwderid;
184 // bool startedForwarder = false;
185 // std::thread forwarder([&]()
186 // {
187 // setThreadName("forwarder");
188 
189 // while (!startedListener)
190 // this_thread::sleep_for(chrono::milliseconds(50));
191 
192 // this_thread::sleep_for(chrono::milliseconds(500));
193 // host2.addNode(host1.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), port1, port1));
194 
195 // startedForwarder = true;
196 
197 // /// Only interested in odd packets
198 // auto w = whost2->installWatch(BuildTopicMask("test"));
199 
200 // while (!done)
201 // {
202 // for (auto i: whost2->checkWatch(w))
203 // {
204 // Message msg = whost2->envelope(i).open(whost2->fullTopics(w));
205 // cnote << "New message from:" << msg.from() << RLP(msg.payload()).toInt<unsigned>();
206 // }
207 // this_thread::sleep_for(chrono::milliseconds(50));
208 // }
209 // });
210 
211 // while (!startedForwarder)
212 // this_thread::sleep_for(chrono::milliseconds(50));
213 
214 // Host ph("Sender", NetworkPreferences("127.0.0.1", 0, false));
215 // ph.setIdealPeerCount(1);
216 // shared_ptr<WhisperHost> wh = ph.registerCapability(make_shared<WhisperHost>());
217 // ph.start();
218 // BOOST_REQUIRE(ph.listenPort());
219 
220 // ph.addNode(host2.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), port2, port2));
221 // while (!ph.haveNetwork())
222 // this_thread::sleep_for(chrono::milliseconds(10));
223 
224 // while (!ph.peerCount())
225 // this_thread::sleep_for(chrono::milliseconds(10));
226 
227 // KeyPair us = KeyPair::create();
228 // wh->post(us.secret(), RLPStream().append(1).out(), BuildTopic("test"));
229 // this_thread::sleep_for(chrono::milliseconds(250));
230 
231 // listener.join();
232 // done = true;
233 // forwarder.join();
234 // BOOST_REQUIRE_EQUAL(result, 1);
235 //}
236 
237 //BOOST_AUTO_TEST_CASE(asyncforwarding)
238 //{
239 // cnote << "Testing Whisper async forwarding...";
240 // VerbosityHolder setTemporaryLevel(2);
241 
242 // unsigned const TestValue = 8456;
243 // unsigned result = 0;
244 // bool done = false;
245 
246 // // Host must be configured not to share peers.
247 // Host host1("Forwarder", NetworkPreferences("127.0.0.1", 0, false));
248 // host1.setIdealPeerCount(1);
249 // auto whost1 = host1.registerCapability(make_shared<WhisperHost>());
250 // host1.start();
251 // auto port1 = host1.listenPort();
252 // BOOST_REQUIRE(port1);
253 
254 // while (!host1.haveNetwork())
255 // this_thread::sleep_for(chrono::milliseconds(2));
256 
257 // auto w = whost1->installWatch(BuildTopicMask("test")); // only interested in odd packets
258 // bool startedForwarder = false;
259 // std::thread forwarder([&]()
260 // {
261 // setThreadName("forwarder");
262 // this_thread::sleep_for(chrono::milliseconds(50));
263 // startedForwarder = true;
264 // while (!done)
265 // {
266 // for (auto i: whost1->checkWatch(w))
267 // {
268 // Message msg = whost1->envelope(i).open(whost1->fullTopics(w));
269 // cnote << "New message from:" << msg.from() << RLP(msg.payload()).toInt<unsigned>();
270 // }
271 // this_thread::sleep_for(chrono::milliseconds(50));
272 // }
273 // });
274 
275 // while (!startedForwarder)
276 // this_thread::sleep_for(chrono::milliseconds(2));
277 
278 // {
279 // Host host2("Sender", NetworkPreferences("127.0.0.1", 0, false));
280 // host2.setIdealPeerCount(1);
281 // shared_ptr<WhisperHost> whost2 = host2.registerCapability(make_shared<WhisperHost>());
282 // host2.start();
283 // auto port2 = host2.listenPort();
284 // BOOST_REQUIRE(port2);
285 // BOOST_REQUIRE_NE(port1, port2);
286 
287 // while (!host2.haveNetwork())
288 // this_thread::sleep_for(chrono::milliseconds(2));
289 
290 // host2.requirePeer(host1.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), port1, port1));
291 // while (!host2.peerCount() || !host1.peerCount())
292 // this_thread::sleep_for(chrono::milliseconds(5));
293 
294 // KeyPair us = KeyPair::create();
295 // whost2->post(us.secret(), RLPStream().append(TestValue).out(), BuildTopic("test"), 777000);
296 // this_thread::sleep_for(chrono::milliseconds(250));
297 // }
298 
299 // {
300 // Host ph("Listener", NetworkPreferences("127.0.0.1", 0, false));
301 // ph.setIdealPeerCount(1);
302 // shared_ptr<WhisperHost> wh = ph.registerCapability(make_shared<WhisperHost>());
303 // ph.start();
304 // BOOST_REQUIRE(ph.listenPort());
305 
306 // while (!ph.haveNetwork())
307 // this_thread::sleep_for(chrono::milliseconds(2));
308 
309 // auto w = wh->installWatch(BuildTopicMask("test")); // only interested in odd packets
310 // ph.requirePeer(host1.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), port1, port1));
311 
312 // for (int i = 0; i < 200 && !result; ++i)
313 // {
314 // for (auto i: wh->checkWatch(w))
315 // {
316 // Message msg = wh->envelope(i).open(wh->fullTopics(w));
317 // unsigned last = RLP(msg.payload()).toInt<unsigned>();
318 // cnote << "New message from:" << msg.from() << RLP(msg.payload()).toInt<unsigned>();
319 // result = last;
320 // }
321 // this_thread::sleep_for(chrono::milliseconds(50));
322 // }
323 // }
324 
325 // done = true;
326 // forwarder.join();
327 // BOOST_REQUIRE_EQUAL(result, TestValue);
328 //}
329 
330 //BOOST_AUTO_TEST_CASE(topicAdvertising)
331 //{
332 // cnote << "Testing Topic Advertising...";
333 // VerbosityHolder setTemporaryLevel(2);
334 
335 // Host host1("first", NetworkPreferences("127.0.0.1", 0, false));
336 // host1.setIdealPeerCount(1);
337 // auto whost1 = host1.registerCapability(make_shared<WhisperHost>());
338 // host1.start();
339 // auto port1 = host1.listenPort();
340 // BOOST_REQUIRE(port1);
341 
342 // while (!host1.haveNetwork())
343 // this_thread::sleep_for(chrono::milliseconds(10));
344 
345 // unsigned const step = 10;
346 // Host host2("second", NetworkPreferences("127.0.0.1", 0, false));
347 // host2.setIdealPeerCount(1);
348 // auto whost2 = host2.registerCapability(make_shared<WhisperHost>());
349 // unsigned w2 = whost2->installWatch(BuildTopicMask("test2"));
350 // host2.start();
351 // auto port2 = host2.listenPort();
352 // BOOST_REQUIRE(port2);
353 // BOOST_REQUIRE_NE(port1, port2);
354 
355 // for (unsigned i = 0; i < 3000 && (!host1.haveNetwork() || !host2.haveNetwork()); i += step)
356 // this_thread::sleep_for(chrono::milliseconds(step));
357 
358 // host1.addNode(host2.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), port2, port2));
359 
360 // for (unsigned i = 0; i < 3000 && (!host1.peerCount() || !host2.peerCount()); i += step)
361 // this_thread::sleep_for(chrono::milliseconds(step));
362 
363 // std::vector<std::pair<std::shared_ptr<Session>, std::shared_ptr<Peer>>> sessions;
364 // TopicBloomFilterHash bf1;
365 
366 // for (int i = 0; i < 600 && !bf1; ++i)
367 // {
368 // sessions = whost1->peerSessions();
369 // if (!sessions.empty())
370 // bf1 = sessions.back().first->cap<WhisperPeer>()->bloom();
371 
372 // this_thread::sleep_for(chrono::milliseconds(step));
373 // }
374 
375 // BOOST_REQUIRE(sessions.size());
376 // TopicBloomFilterHash bf2 = whost2->bloom();
377 // BOOST_REQUIRE_EQUAL(bf1, bf2);
378 // BOOST_REQUIRE(bf1);
379 // BOOST_REQUIRE(!whost1->bloom());
380 
381 // unsigned w1 = whost1->installWatch(BuildTopicMask("test1"));
382 // bf2 = TopicBloomFilterHash();
383 
384 // for (int i = 0; i < 600 && !bf2; ++i)
385 // {
386 // sessions = whost2->peerSessions();
387 // if (!sessions.empty())
388 // bf2 = sessions.back().first->cap<WhisperPeer>()->bloom();
389 
390 // this_thread::sleep_for(chrono::milliseconds(step));
391 // }
392 
393 // BOOST_REQUIRE(sessions.size());
394 // BOOST_REQUIRE_EQUAL(sessions.back().second->id, host1.id());
395 // bf1 = whost1->bloom();
396 // BOOST_REQUIRE_EQUAL(bf1, bf2);
397 // BOOST_REQUIRE(bf1);
398 
399 // unsigned random = 0xC0FFEE;
400 // whost1->uninstallWatch(w1);
401 // whost1->uninstallWatch(random);
402 // whost1->uninstallWatch(w1);
403 // whost1->uninstallWatch(random);
404 // whost2->uninstallWatch(random);
405 // whost2->uninstallWatch(w2);
406 // whost2->uninstallWatch(random);
407 // whost2->uninstallWatch(w2);
408 //}
409 
410 //BOOST_AUTO_TEST_CASE(selfAddressed)
411 //{
412 // VerbosityHolder setTemporaryLevel(10);
413 // cnote << "Testing self-addressed messaging with bloom filter matching...";
414 
415 // char const* text = "deterministic pseudorandom test";
416 // BuildTopicMask mask(text);
417 
418 // Host host("first", NetworkPreferences("127.0.0.1", 0, false));
419 // auto wh = host.registerCapability(make_shared<WhisperHost>());
420 // auto watch = wh->installWatch(BuildTopicMask(text));
421 
422 // unsigned const sample = 0xFEED;
423 // KeyPair us = KeyPair::create();
424 // wh->post(us.secret(), RLPStream().append(sample).out(), BuildTopic(text));
425 
426 // TopicBloomFilterHash f = wh->bloom();
427 // Envelope e = Message(RLPStream().append(sample).out()).seal(us.secret(), BuildTopic(text), 50, 50);
428 // bool ok = e.matchesBloomFilter(f);
429 // BOOST_REQUIRE(ok);
430 
431 // this_thread::sleep_for(chrono::milliseconds(50));
432 
433 // unsigned single = 0;
434 // unsigned result = 0;
435 // for (auto j: wh->checkWatch(watch))
436 // {
437 // Message msg = wh->envelope(j).open(wh->fullTopics(watch));
438 // single = RLP(msg.payload()).toInt<unsigned>();
439 // result += single;
440 // }
441 
442 // BOOST_REQUIRE_EQUAL(sample, result);
443 //}
444 
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::hash for asio::adress
Definition: Common.h:323
Definition: Eth.h:45
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
static bool test_allowLocal
Setting true causes isAllowed to return true for all addresses. (Used by test fixtures) ...
Definition: Common.h:185
Helper functions to work with json::spirit and test files.