From 43f0112456b7f5d56f746f00f1eb3c1cea0616ea Mon Sep 17 00:00:00 2001 From: Vlad Gluhovsky Date: Mon, 29 Jun 2015 12:38:06 +0200 Subject: [PATCH 1/2] Mutex & reference counter --- libwhisper/WhisperHost.cpp | 5 ++++- libwhisper/WhisperPeer.cpp | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/libwhisper/WhisperHost.cpp b/libwhisper/WhisperHost.cpp index 1a47a56d1..2d037133c 100644 --- a/libwhisper/WhisperHost.cpp +++ b/libwhisper/WhisperHost.cpp @@ -94,8 +94,11 @@ unsigned WhisperHost::installWatch(shh::Topics const& _t) DEV_GUARDED(m_filterLock) { - if (!m_filters.count(h)) + auto it = m_filters.find(h); + if (m_filters.end() == it) m_filters.insert(make_pair(h, f)); + else + it->second.refCount++; ret = m_watches.size() ? m_watches.rbegin()->first + 1 : 0; m_watches[ret] = ClientWatch(h); diff --git a/libwhisper/WhisperPeer.cpp b/libwhisper/WhisperPeer.cpp index 665364f49..1f48468df 100644 --- a/libwhisper/WhisperPeer.cpp +++ b/libwhisper/WhisperPeer.cpp @@ -58,7 +58,10 @@ bool WhisperPeer::interpret(unsigned _id, RLP const& _r) disable("Invalid protocol version."); for (auto const& m: host()->all()) + { + Guard l(x_unseen); m_unseen.insert(make_pair(0, m.first)); + } if (session()->id() < host()->host()->id()) sendMessages(); From c50437a5f95be117e23ff5b6f5d341628e74d680 Mon Sep 17 00:00:00 2001 From: Vlad Gluhovsky Date: Mon, 29 Jun 2015 13:01:05 +0200 Subject: [PATCH 2/2] uninstallWatch() fixed + test --- libwhisper/WhisperHost.cpp | 7 +++---- test/libwhisper/whisperTopic.cpp | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/libwhisper/WhisperHost.cpp b/libwhisper/WhisperHost.cpp index 2d037133c..150d5cd63 100644 --- a/libwhisper/WhisperHost.cpp +++ b/libwhisper/WhisperHost.cpp @@ -100,11 +100,10 @@ unsigned WhisperHost::installWatch(shh::Topics const& _t) else it->second.refCount++; + m_bloom.addRaw(f.filter.exportBloom()); ret = m_watches.size() ? m_watches.rbegin()->first + 1 : 0; m_watches[ret] = ClientWatch(h); cwatshh << "+++" << ret << h; - - m_bloom.addRaw(f.filter.exportBloom()); } noteAdvertiseTopicsOfInterest(); @@ -141,16 +140,16 @@ void WhisperHost::uninstallWatch(unsigned _i) auto it = m_watches.find(_i); if (it == m_watches.end()) return; + auto id = it->second.id; m_watches.erase(it); auto fit = m_filters.find(id); if (fit != m_filters.end()) { + m_bloom.removeRaw(fit->second.filter.exportBloom()); if (!--fit->second.refCount) m_filters.erase(fit); - - m_bloom.removeRaw(fit->second.filter.exportBloom()); } } diff --git a/test/libwhisper/whisperTopic.cpp b/test/libwhisper/whisperTopic.cpp index a152f756e..47d17503c 100644 --- a/test/libwhisper/whisperTopic.cpp +++ b/test/libwhisper/whisperTopic.cpp @@ -320,7 +320,7 @@ BOOST_AUTO_TEST_CASE(topicAdvertising) Host host2("second", NetworkPreferences("127.0.0.1", 30305, false)); host2.setIdealPeerCount(1); auto whost2 = host2.registerCapability(new WhisperHost()); - whost2->installWatch(BuildTopicMask("test2")); + unsigned w2 = whost2->installWatch(BuildTopicMask("test2")); host2.start(); while (!host2.haveNetwork()) @@ -354,7 +354,7 @@ BOOST_AUTO_TEST_CASE(topicAdvertising) BOOST_REQUIRE(bf1); BOOST_REQUIRE(!whost1->bloom()); - whost1->installWatch(BuildTopicMask("test1")); + unsigned w1 = whost1->installWatch(BuildTopicMask("test1")); for (int i = 0; i < 600; ++i) { @@ -372,6 +372,16 @@ BOOST_AUTO_TEST_CASE(topicAdvertising) bf1 = whost1->bloom(); BOOST_REQUIRE_EQUAL(bf1, bf2); BOOST_REQUIRE(bf1); + + unsigned random = 0xC0FFEE; + whost1->uninstallWatch(w1); + whost1->uninstallWatch(random); + whost1->uninstallWatch(w1); + whost1->uninstallWatch(random); + whost2->uninstallWatch(random); + whost2->uninstallWatch(w2); + whost2->uninstallWatch(random); + whost2->uninstallWatch(w2); } BOOST_AUTO_TEST_SUITE_END()