Browse Source

rating initial version complete

cl-refactor
Vlad Gluhovsky 10 years ago
parent
commit
52f4f289c1
  1. 54
      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

54
libwhisper/BloomFilter.h

@ -35,17 +35,20 @@ public:
TopicBloomFilterBase() { init(); }
TopicBloomFilterBase(FixedHash<N> const& _h): FixedHash<N>(_h) { init(); }
void addBloom(dev::shh::AbridgedTopic const& _h) { addRaw(_h.template bloomPart<BitsPerBloom, N>()); }
void removeBloom(dev::shh::AbridgedTopic const& _h) { removeRaw(_h.template bloomPart<BitsPerBloom, N>()); }
bool containsBloom(dev::shh::AbridgedTopic const& _h) const { return this->contains(_h.template bloomPart<BitsPerBloom, N>()); }
void addBloom(AbridgedTopic const& _h) { addRaw(bloom(_h)); }
void removeBloom(AbridgedTopic const& _h) { removeRaw(bloom(_h)); }
bool containsBloom(AbridgedTopic const& _h) const { return this->contains(bloom(_h)); }
void addRaw(FixedHash<N> const& _h);
void removeRaw(FixedHash<N> const& _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:
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;
std::array<uint16_t, CounterSize> m_refCounter;
@ -83,12 +86,51 @@ void TopicBloomFilterBase<N>::removeRaw(FixedHash<N> const& _h)
template <unsigned N>
bool TopicBloomFilterBase<N>::isBitSet(FixedHash<N> const& _h, unsigned _index)
{
{
unsigned iByte = _index / 8;
unsigned iBit = _index % 8;
unsigned iBit = _index & 0x7;
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>;
}

2
libwhisper/Common.cpp

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

3
libwhisper/Message.cpp

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

23
test/libwhisper/bloomFilter.cpp

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

2
test/libwhisper/whisperMessage.cpp

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

Loading…
Cancel
Save