Fabcoin Core  0.16.2
P2P Digital Currency
BloomFilter.h
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 */
22 #pragma once
23 
24 #include "Common.h"
25 
26 namespace dev
27 {
28 namespace shh
29 {
30 
31 template <unsigned N>
33 {
34 public:
36  TopicBloomFilterBase(FixedHash<N> const& _h): FixedHash<N>(_h) { init(); }
37 
38  void addBloom(AbridgedTopic const& _h) { addRaw(bloom(_h)); }
39  void removeBloom(AbridgedTopic const& _h) { removeRaw(bloom(_h)); }
40  bool containsBloom(AbridgedTopic const& _h) const { return this->contains(bloom(_h)); }
41 
42  void addRaw(FixedHash<N> const& _h);
43  void removeRaw(FixedHash<N> const& _h);
44  bool containsRaw(FixedHash<N> const& _h) const { return this->contains(_h); }
45 
46  static FixedHash<N> bloom(AbridgedTopic const& _h);
47  static void setBit(FixedHash<N>& _h, unsigned index);
48  static bool isBitSet(FixedHash<N> const& _h, unsigned _index);
49 
50 private:
51  void init() { for (unsigned i = 0; i < CounterSize; ++i) m_refCounter[i] = 0; }
52 
53  static const unsigned CounterSize = N * 8;
54  std::array<uint16_t, CounterSize> m_refCounter;
55 };
56 
57 static unsigned const c_powerOfTwoBitMmask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
58 
59 template <unsigned N>
61 {
62  *this |= _h;
63  for (unsigned i = 0; i < CounterSize; ++i)
64  if (isBitSet(_h, i))
65  {
67  m_refCounter[i]++;
68  else
69  BOOST_THROW_EXCEPTION(Overflow());
70  }
71 }
72 
73 template <unsigned N>
75 {
76  for (unsigned i = 0; i < CounterSize; ++i)
77  if (isBitSet(_h, i))
78  {
79  if (m_refCounter[i])
80  m_refCounter[i]--;
81 
82  if (!m_refCounter[i])
83  (*this)[i / 8] &= ~c_powerOfTwoBitMmask[i % 8];
84  }
85 }
86 
87 template <unsigned N>
88 bool TopicBloomFilterBase<N>::isBitSet(FixedHash<N> const& _h, unsigned _index)
89 {
90  unsigned iByte = _index / 8;
91  unsigned iBit = _index % 8;
92  return (_h[iByte] & c_powerOfTwoBitMmask[iBit]) != 0;
93 }
94 
95 template <unsigned N>
97 {
98  unsigned iByte = _index / 8;
99  unsigned iBit = _index % 8;
100  _h[iByte] |= c_powerOfTwoBitMmask[iBit];
101 }
102 
103 template <unsigned N>
105 {
106  // The size of AbridgedTopic is 32 bits, and 27 of them participate in this algorithm.
107 
108  // We need to review the algorithm if any of the following constants will be changed.
109  static_assert(4 == AbridgedTopic::size, "wrong template parameter in TopicBloomFilterBase<N>::bloom()");
110  static_assert(3 == BitsPerBloom, "wrong template parameter in TopicBloomFilterBase<N>::bloom()");
111 
112  FixedHash<N> ret;
113 
114  if (TopicBloomFilterSize == N)
115  for (unsigned i = 0; i < BitsPerBloom; ++i)
116  {
117  unsigned x = _h[i];
118  if (_h[BitsPerBloom] & c_powerOfTwoBitMmask[i])
119  x += 256;
120 
121  setBit(ret, x);
122  }
123  else
124  for (unsigned i = 0; i < BitsPerBloom; ++i)
125  {
126  unsigned x = unsigned(_h[i]) + unsigned(_h[i + 1]);
127  x %= N * 8;
128  setBit(ret, x);
129  }
130 
131  return ret;
132 }
133 
135 
136 }
137 }
138 
139 
140 
141 
142 
143 
std::array< uint16_t, CounterSize > m_refCounter
Definition: BloomFilter.h:54
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
void removeRaw(FixedHash< N > const &_h)
Definition: BloomFilter.h:74
bool contains(FixedHash const &_c) const
Definition: FixedHash.h:116
static const unsigned CounterSize
Definition: BloomFilter.h:53
bool containsRaw(FixedHash< N > const &_h) const
Definition: BloomFilter.h:44
#define x(i)
TopicBloomFilterBase(FixedHash< N > const &_h)
Definition: BloomFilter.h:36
static bool isBitSet(FixedHash< N > const &_h, unsigned _index)
Definition: BloomFilter.h:88
ExecStats::duration max
Definition: ExecStats.cpp:36
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
void removeBloom(AbridgedTopic const &_h)
Definition: BloomFilter.h:39
void addBloom(AbridgedTopic const &_h)
Definition: BloomFilter.h:38
void addRaw(FixedHash< N > const &_h)
Definition: BloomFilter.h:60
static FixedHash< N > bloom(AbridgedTopic const &_h)
Definition: BloomFilter.h:104
bool containsBloom(AbridgedTopic const &_h) const
Definition: BloomFilter.h:40
static void setBit(FixedHash< N > &_h, unsigned index)
Definition: BloomFilter.h:96