Browse Source

BF size changed

cl-refactor
Vlad Gluhovsky 10 years ago
parent
commit
eb995ebde5
  1. 4
      libwhisper/Common.h
  2. 8
      libwhisper/WhisperHost.cpp
  3. 2
      libwhisper/WhisperHost.h
  4. 8
      libwhisper/WhisperPeer.cpp
  5. 2
      libwhisper/WhisperPeer.h
  6. 27
      test/libwhisper/bloomFilter.cpp

4
libwhisper/Common.h

@ -54,11 +54,11 @@ enum WhisperPacket
{ {
StatusPacket = 0, StatusPacket = 0,
MessagesPacket, MessagesPacket,
UpdateTopicFilterPacket, TopicFilterPacket,
PacketCount PacketCount
}; };
enum { TopicBloomFilterSize = 8 }; enum { TopicBloomFilterSize = 64 };
using AbridgedTopic = FixedHash<4>; using AbridgedTopic = FixedHash<4>;
using Topic = h256; using Topic = h256;

8
libwhisper/WhisperHost.cpp

@ -85,10 +85,10 @@ void WhisperHost::inject(Envelope const& _m, WhisperPeer* _p)
} }
} }
void WhisperHost::advertizeTopicsOfInterest() void WhisperHost::advertiseTopicsOfInterest()
{ {
for (auto i: peerSessions()) for (auto i: peerSessions())
i.first->cap<WhisperPeer>().get()->advertizeTopicsOfInterest(); i.first->cap<WhisperPeer>().get()->advertiseTopicsOfInterest();
} }
void WhisperHost::noteChanged(h256 _messageHash, h256 _filter) void WhisperHost::noteChanged(h256 _messageHash, h256 _filter)
@ -120,7 +120,7 @@ unsigned WhisperHost::installWatch(shh::Topics const& _t)
m_filters.insert(make_pair(h, f)); m_filters.insert(make_pair(h, f));
m_bloom.addRaw(f.filter.exportBloom()); m_bloom.addRaw(f.filter.exportBloom());
advertizeTopicsOfInterest(); advertiseTopicsOfInterest();
return installWatchOnId(h); return installWatchOnId(h);
} }
@ -165,7 +165,7 @@ void WhisperHost::uninstallWatch(unsigned _i)
m_filters.erase(fit); m_filters.erase(fit);
m_bloom.removeRaw(fit->second.filter.exportBloom()); m_bloom.removeRaw(fit->second.filter.exportBloom());
advertizeTopicsOfInterest(); advertiseTopicsOfInterest();
} }
} }

2
libwhisper/WhisperHost.h

@ -74,7 +74,7 @@ private:
void streamMessage(h256 _m, RLPStream& _s) const; void streamMessage(h256 _m, RLPStream& _s) const;
void noteChanged(h256 _messageHash, h256 _filter); void noteChanged(h256 _messageHash, h256 _filter);
void advertizeTopicsOfInterest(); void advertiseTopicsOfInterest();
mutable dev::SharedMutex x_messages; mutable dev::SharedMutex x_messages;
std::map<h256, Envelope> m_messages; std::map<h256, Envelope> m_messages;

8
libwhisper/WhisperPeer.cpp

@ -63,7 +63,7 @@ bool WhisperPeer::interpret(unsigned _id, RLP const& _r)
{ {
if (_r.itemCount() > 1) // for backwards compatibility if (_r.itemCount() > 1) // for backwards compatibility
m_bloom = (FixedHash<TopicBloomFilterSize>)_r[1]; m_bloom = (FixedHash<TopicBloomFilterSize>)_r[1];
advertizeTopicsOfInterest(); advertiseTopicsOfInterest();
} }
for (auto const& m: host()->all()) for (auto const& m: host()->all())
@ -73,7 +73,7 @@ bool WhisperPeer::interpret(unsigned _id, RLP const& _r)
sendMessages(); sendMessages();
break; break;
} }
case UpdateTopicFilterPacket: case TopicFilterPacket:
{ {
m_bloom = (FixedHash<TopicBloomFilterSize>)_r[0]; m_bloom = (FixedHash<TopicBloomFilterSize>)_r[0];
break; break;
@ -119,10 +119,10 @@ void WhisperPeer::noteNewMessage(h256 _h, Envelope const& _m)
m_unseen.insert(make_pair(rating(_m), _h)); m_unseen.insert(make_pair(rating(_m), _h));
} }
void WhisperPeer::advertizeTopicsOfInterest() void WhisperPeer::advertiseTopicsOfInterest()
{ {
RLPStream s; RLPStream s;
prep(s, UpdateTopicFilterPacket, 1); prep(s, TopicFilterPacket, 1);
s << host()->bloom(); s << host()->bloom();
sealAndSend(s); sealAndSend(s);
} }

2
libwhisper/WhisperPeer.h

@ -56,7 +56,7 @@ public:
static u256 version() { return 2; } static u256 version() { return 2; }
static unsigned messageCount() { return PacketCount; } static unsigned messageCount() { return PacketCount; }
FixedHash<TopicBloomFilterSize> const& bloom() const { return m_bloom; } FixedHash<TopicBloomFilterSize> const& bloom() const { return m_bloom; }
void advertizeTopicsOfInterest(); ///< sends our bloom filter to remote peer void advertiseTopicsOfInterest(); ///< sends our bloom filter to remote peer
private: private:
virtual bool interpret(unsigned _id, RLP const&) override; virtual bool interpret(unsigned _id, RLP const&) override;

27
test/libwhisper/bloomFilter.cpp

@ -28,8 +28,7 @@ using namespace dev;
using namespace dev::shh; using namespace dev::shh;
using TopicBloomFilterShort = TopicBloomFilterBase<4>; using TopicBloomFilterShort = TopicBloomFilterBase<4>;
using TopicBloomFilterLong = TopicBloomFilterBase<8>; using TopicBloomFilterTest = TopicBloomFilterBase<TopicBloomFilterSize>;
using TopicBloomFilterTest = TopicBloomFilterLong;
void testAddNonExisting(TopicBloomFilterShort& _f, AbridgedTopic const& _h) void testAddNonExisting(TopicBloomFilterShort& _f, AbridgedTopic const& _h)
{ {
@ -59,7 +58,7 @@ void testRemoveExistingBloom(TopicBloomFilterShort& _f, AbridgedTopic const& _h)
BOOST_REQUIRE(!_f.containsBloom(_h)); BOOST_REQUIRE(!_f.containsBloom(_h));
} }
int calculateExpected(TopicBloomFilterTest const& f, int const n) double calculateExpected(TopicBloomFilterTest const& f, int const n)
{ {
int const m = f.size * 8; // number of bits in the bloom int const m = f.size * 8; // number of bits in the bloom
int const k = f.BitsPerBloom; // number of hash functions (e.g. bits set to 1 in every bloom) int const k = f.BitsPerBloom; // number of hash functions (e.g. bits set to 1 in every bloom)
@ -77,10 +76,10 @@ int calculateExpected(TopicBloomFilterTest const& f, int const n)
for (int i = 0; i < k; ++i) for (int i = 0; i < k; ++i)
kBitsSet *= single; kBitsSet *= single;
return static_cast<int>(kBitsSet * 100 + 0.5); // in percents, rounded up return kBitsSet;
} }
void testFalsePositiveRate(TopicBloomFilterTest const& f, int const inserted, Topic& x) double testFalsePositiveRate(TopicBloomFilterTest const& f, int const inserted, Topic& x)
{ {
int const c_sampleSize = 1000; int const c_sampleSize = 1000;
int falsePositive = 0; int falsePositive = 0;
@ -93,12 +92,14 @@ void testFalsePositiveRate(TopicBloomFilterTest const& f, int const inserted, To
++falsePositive; ++falsePositive;
} }
falsePositive /= (c_sampleSize / 100); // in percents double res = double(falsePositive) / double(c_sampleSize);
int expected = calculateExpected(f, inserted);
int allowed = expected + (expected / 5); // allow deviations ~20%
//cnote << "Inserted: " << inserted << ", False Positive Rate: " << falsePositive << ", Expected: " << expected; double expected = calculateExpected(f, inserted);
BOOST_REQUIRE(falsePositive <= allowed); double allowed = expected * 1.2 + 0.05; // allow deviations ~25%
//cnote << "Inserted: " << inserted << ", False Positive Rate: " << res << ", Expected: " << expected;
BOOST_REQUIRE(res <= allowed);
return expected;
} }
BOOST_AUTO_TEST_SUITE(bloomFilter) BOOST_AUTO_TEST_SUITE(bloomFilter)
@ -111,11 +112,13 @@ BOOST_AUTO_TEST_CASE(falsePositiveRate)
TopicBloomFilterTest f; TopicBloomFilterTest f;
Topic x(0xC0DEFEED); // deterministic pseudorandom value Topic x(0xC0DEFEED); // deterministic pseudorandom value
for (int i = 1; i < 21; ++i) double expectedRate = 0;
for (int i = 1; i < 50 && expectedRate < 0.5; ++i)
{ {
x = sha3(x); x = sha3(x);
f.addBloom(AbridgedTopic(x)); f.addBloom(AbridgedTopic(x));
testFalsePositiveRate(f, i, x); expectedRate = testFalsePositiveRate(f, i, x);
} }
} }

Loading…
Cancel
Save