Browse Source

rating initial version complete

cl-refactor
Vlad Gluhovsky 10 years ago
parent
commit
52f4f289c1
  1. 52
      libwhisper/BloomFilter.h
  2. 2
      libwhisper/Common.cpp
  3. 3
      libwhisper/Message.cpp
  4. 23
      test/libwhisper/bloomFilter.cpp
  5. 2
      test/libwhisper/whisperMessage.cpp

52
libwhisper/BloomFilter.h

@ -35,17 +35,20 @@ public:
TopicBloomFilterBase() { init(); } TopicBloomFilterBase() { init(); }
TopicBloomFilterBase(FixedHash<N> const& _h): FixedHash<N>(_h) { init(); } TopicBloomFilterBase(FixedHash<N> const& _h): FixedHash<N>(_h) { init(); }
void addBloom(dev::shh::AbridgedTopic const& _h) { addRaw(_h.template bloomPart<BitsPerBloom, N>()); } void addBloom(AbridgedTopic const& _h) { addRaw(bloom(_h)); }
void removeBloom(dev::shh::AbridgedTopic const& _h) { removeRaw(_h.template bloomPart<BitsPerBloom, N>()); } void removeBloom(AbridgedTopic const& _h) { removeRaw(bloom(_h)); }
bool containsBloom(dev::shh::AbridgedTopic const& _h) const { return this->contains(_h.template bloomPart<BitsPerBloom, N>()); } bool containsBloom(AbridgedTopic const& _h) const { return this->contains(bloom(_h)); }
void addRaw(FixedHash<N> const& _h); void addRaw(FixedHash<N> const& _h);
void removeRaw(FixedHash<N> const& _h); void removeRaw(FixedHash<N> const& _h);
bool containsRaw(FixedHash<N> const& _h) const { return this->contains(_h); } bool containsRaw(FixedHash<N> const& _h) const { return this->contains(_h); }
static FixedHash<N> bloom(AbridgedTopic const& _h);
static void setBit(FixedHash<N>& _h, unsigned index);
static bool isBitSet(FixedHash<N> const& _h, unsigned _index);
private: private:
void init() { for (unsigned i = 0; i < CounterSize; ++i) m_refCounter[i] = 0; } void init() { for (unsigned i = 0; i < CounterSize; ++i) m_refCounter[i] = 0; }
static bool isBitSet(FixedHash<N> const& _h, unsigned _index);
static const unsigned CounterSize = N * 8; static const unsigned CounterSize = N * 8;
std::array<uint16_t, CounterSize> m_refCounter; std::array<uint16_t, CounterSize> m_refCounter;
@ -85,10 +88,49 @@ template <unsigned N>
bool TopicBloomFilterBase<N>::isBitSet(FixedHash<N> const& _h, unsigned _index) bool TopicBloomFilterBase<N>::isBitSet(FixedHash<N> const& _h, unsigned _index)
{ {
unsigned iByte = _index / 8; unsigned iByte = _index / 8;
unsigned iBit = _index % 8; unsigned iBit = _index & 0x7;
return (_h[iByte] & c_powerOfTwoBitMmask[iBit]) != 0; return (_h[iByte] & c_powerOfTwoBitMmask[iBit]) != 0;
} }
template <unsigned N>
void TopicBloomFilterBase<N>::setBit(FixedHash<N>& _h, unsigned _index)
{
unsigned iByte = _index / 8;
unsigned iBit = _index & 0x7;
_h[iByte] |= c_powerOfTwoBitMmask[iBit];
}
template <unsigned N>
FixedHash<N> TopicBloomFilterBase<N>::bloom(AbridgedTopic const& _h)
{
// The size of AbridgedTopic is 32 bits, and 27 of them participate in this algorithm.
// We need to review the algorithm if any of the following constants will be changed.
static_assert(4 == AbridgedTopic::size, "wrong template parameter in TopicBloomFilterBase<N>::bloom()");
static_assert(3 == BitsPerBloom, "wrong template parameter in TopicBloomFilterBase<N>::bloom()");
FixedHash<N> ret;
if (TopicBloomFilterSize == N)
for (unsigned i = 0; i < BitsPerBloom; ++i)
{
unsigned x = _h[i];
if (_h[BitsPerBloom] & c_powerOfTwoBitMmask[i])
x += 256;
setBit(ret, x);
}
else
for (unsigned i = 0; i < BitsPerBloom; ++i)
{
unsigned x = unsigned(_h[i]) + unsigned(_h[i + 1]);
x %= N * 8;
setBit(ret, x);
}
return ret;
}
using TopicBloomFilter = TopicBloomFilterBase<TopicBloomFilterSize>; using TopicBloomFilter = TopicBloomFilterBase<TopicBloomFilterSize>;
} }

2
libwhisper/Common.cpp

@ -100,7 +100,7 @@ TopicBloomFilterHash TopicFilter::exportBloom() const
TopicBloomFilterHash ret; TopicBloomFilterHash ret;
for (TopicMask const& t: m_topicMasks) for (TopicMask const& t: m_topicMasks)
for (auto const& i: t) for (auto const& i: t)
ret |= i.first.template bloomPart<BitsPerBloom, TopicBloomFilterSize>(); ret |= TopicBloomFilter::bloom(i.first);
return ret; return ret;
} }

3
libwhisper/Message.cpp

@ -20,6 +20,7 @@
*/ */
#include "Message.h" #include "Message.h"
#include "BloomFilter.h"
using namespace std; using namespace std;
using namespace dev; using namespace dev;
@ -185,7 +186,7 @@ void Envelope::proveWork(unsigned _ms)
bool Envelope::matchesBloomFilter(TopicBloomFilterHash const& f) const bool Envelope::matchesBloomFilter(TopicBloomFilterHash const& f) const
{ {
for (AbridgedTopic t: m_topic) for (AbridgedTopic t: m_topic)
if (f.contains(t.template bloomPart<BitsPerBloom, TopicBloomFilterSize>())) if (f.contains(TopicBloomFilter::bloom(t)))
return true; return true;
return false; return false;

23
test/libwhisper/bloomFilter.cpp

@ -244,7 +244,7 @@ BOOST_AUTO_TEST_CASE(bloomFilterRaw)
BOOST_REQUIRE(!f.contains(b00110111)); BOOST_REQUIRE(!f.contains(b00110111));
} }
static const unsigned DistributionTestSize = 8; static const unsigned DistributionTestSize = TopicBloomFilterSize;
static const unsigned TestArrSize = 8 * DistributionTestSize; static const unsigned TestArrSize = 8 * DistributionTestSize;
void updateDistribution(FixedHash<DistributionTestSize> const& _h, array<unsigned, TestArrSize>& _distribution) void updateDistribution(FixedHash<DistributionTestSize> const& _h, array<unsigned, TestArrSize>& _distribution)
@ -271,10 +271,10 @@ BOOST_AUTO_TEST_CASE(distributionRate)
Topic x(0xC0FFEE); // deterministic pseudorandom value Topic x(0xC0FFEE); // deterministic pseudorandom value
for (unsigned i = 0; i < 22000; ++i) for (unsigned i = 0; i < 26000; ++i)
{ {
x = sha3(x); x = sha3(x);
FixedHash<DistributionTestSize> h = x.template bloomPart<BitsPerBloom, DistributionTestSize>(); FixedHash<DistributionTestSize> h = TopicBloomFilter::bloom(abridge(x));
updateDistribution(h, distribution); updateDistribution(h, distribution);
} }
@ -283,16 +283,25 @@ BOOST_AUTO_TEST_CASE(distributionRate)
average += distribution[i]; average += distribution[i];
average /= TestArrSize; average /= TestArrSize;
unsigned deviation = average / 10; // approx. 10% unsigned deviation = average / 3;
unsigned maxAllowed = average + deviation; unsigned maxAllowed = average + deviation;
unsigned minAllowed = average - deviation; unsigned minAllowed = average - deviation;
unsigned maximum = 0;
unsigned minimum = 0xFFFFFFFF;
for (unsigned i = 0; i < TestArrSize; ++i) for (unsigned i = 0; i < TestArrSize; ++i)
{ {
//cnote << i << ":" << distribution[i]; unsigned const& z = distribution[i];
BOOST_REQUIRE(distribution[i] > minAllowed); if (z > maximum)
BOOST_REQUIRE(distribution[i] < maxAllowed); maximum = z;
else if (z < minimum)
minimum = z;
} }
cnote << minimum << average << maximum;
BOOST_REQUIRE(minimum > minAllowed);
BOOST_REQUIRE(maximum < maxAllowed);
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

2
test/libwhisper/whisperMessage.cpp

@ -104,7 +104,7 @@ BOOST_AUTO_TEST_CASE(work)
Message m(payload); Message m(payload);
Envelope e = m.seal(zero, topics, 1, 50); Envelope e = m.seal(zero, topics, 1, 50);
unsigned x = e.workProved(); unsigned x = e.workProved();
cnote << x; //cnote << x;
BOOST_REQUIRE(x > 4); BOOST_REQUIRE(x > 4);
} }
} }

Loading…
Cancel
Save