2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @brief implementation of a block group using a Bloom filter
23 * to drop duplicate blocks
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_block_group_lib.h"
29 #include "gnunet_block_plugin.h"
33 * Internal data structure for a block group.
35 struct BfGroupInternals
38 * A Bloom filter to weed out duplicate replies probabilistically.
40 struct GNUNET_CONTAINER_BloomFilter *bf;
43 * Set from the nonce to mingle the hashes before going into the @e bf.
56 * Serialize state of a block group.
58 * @param bg group to serialize
59 * @param[out] nonce set to the nonce of the @a bg
60 * @param[out] raw_data set to the serialized state
61 * @param[out] raw_data_size set to the number of bytes in @a raw_data
62 * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not
63 * supported, #GNUNET_SYSERR on error
66 bf_group_serialize_cb (struct GNUNET_BLOCK_Group *bg,
69 size_t *raw_data_size)
71 struct BfGroupInternals *gi = bg->internal_cls;
74 raw = GNUNET_malloc (gi->bf_size);
76 GNUNET_CONTAINER_bloomfilter_get_raw_data (gi->bf,
83 *nonce = gi->bf_mutator;
85 *raw_data_size = gi->bf_size;
91 * Mark elements as "seen" using a hash of the element. Not supported
92 * by all block plugins.
94 * @param bg group to update
95 * @param seen_results results already seen
96 * @param seen_results_count number of entries in @a seen_results
99 bf_group_mark_seen_cb (struct GNUNET_BLOCK_Group *bg,
100 const struct GNUNET_HashCode *seen_results,
101 unsigned int seen_results_count)
103 struct BfGroupInternals *gi = bg->internal_cls;
105 for (unsigned int i=0;i<seen_results_count;i++)
107 struct GNUNET_HashCode mhash;
109 GNUNET_BLOCK_mingle_hash (&seen_results[i],
112 GNUNET_CONTAINER_bloomfilter_add (gi->bf,
119 * Merge two groups, if possible. Not supported by all block plugins,
120 * can also fail if the nonces were different.
122 * @param bg1 group to update
123 * @param bg2 group to merge into @a bg1
124 * @return #GNUNET_OK on success, #GNUNET_NO if the nonces were different and thus
128 bf_group_merge_cb (struct GNUNET_BLOCK_Group *bg1,
129 const struct GNUNET_BLOCK_Group *bg2)
131 struct BfGroupInternals *gi1 = bg1->internal_cls;
132 struct BfGroupInternals *gi2 = bg2->internal_cls;
134 if (gi1->bf_mutator != gi2->bf_mutator)
136 if (gi1->bf_size != gi2->bf_size)
138 GNUNET_CONTAINER_bloomfilter_or2 (gi1->bf,
145 * Destroy resources used by a block group.
147 * @param bg group to destroy, NULL is allowed
150 bf_group_destroy_cb (struct GNUNET_BLOCK_Group *bg)
152 struct BfGroupInternals *gi = bg->internal_cls;
154 GNUNET_CONTAINER_bloomfilter_free (gi->bf);
161 * Create a new block group that filters duplicates using a Bloom filter.
163 * @param ctx block context in which the block group is created
164 * @param bf_size size of the Bloom filter
165 * @param bf_k K-value for the Bloom filter
166 * @param type block type
167 * @param nonce random value used to seed the group creation
168 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
169 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
170 * @return block group handle, NULL if block groups are not supported
171 * by this @a type of block (this is not an error)
173 struct GNUNET_BLOCK_Group *
174 GNUNET_BLOCK_GROUP_bf_create (void *cls,
177 enum GNUNET_BLOCK_Type type,
179 const void *raw_data,
180 size_t raw_data_size)
182 struct BfGroupInternals *gi;
183 struct GNUNET_BLOCK_Group *bg;
185 gi = GNUNET_new (struct BfGroupInternals);
186 gi->bf = GNUNET_CONTAINER_bloomfilter_init ((bf_size != raw_data_size) ? NULL : raw_data,
189 gi->bf_mutator = nonce;
190 gi->bf_size = bf_size;
191 bg = GNUNET_new (struct GNUNET_BLOCK_Group);
193 bg->serialize_cb = &bf_group_serialize_cb;
194 bg->mark_seen_cb = &bf_group_mark_seen_cb;
195 bg->merge_cb = &bf_group_merge_cb;
196 bg->destroy_cb = &bf_group_destroy_cb;
197 bg->internal_cls = gi;
203 * Test if @a hc is contained in the Bloom filter of @a bg. If so,
204 * return #GNUNET_YES. If not, add @a hc to the Bloom filter and
207 * @param bg block group to use for testing
208 * @param hc hash of element to evaluate
209 * @return #GNUNET_YES if @a hc is (likely) a duplicate
210 * #GNUNET_NO if @a hc was definitively not in @bg (but now is)
213 GNUNET_BLOCK_GROUP_bf_test_and_set (struct GNUNET_BLOCK_Group *bg,
214 const struct GNUNET_HashCode *hc)
216 struct BfGroupInternals *gi;
217 struct GNUNET_HashCode mhash;
221 gi = bg->internal_cls;
222 GNUNET_BLOCK_mingle_hash (hc,
226 GNUNET_CONTAINER_bloomfilter_test (gi->bf,
229 GNUNET_CONTAINER_bloomfilter_add (gi->bf,
236 * How many bytes should a bloomfilter be if we have already seen
237 * entry_count responses? Sized so that do not have to
238 * re-size the filter too often (to keep it cheap).
240 * Since other peers will also add entries but not resize the filter,
241 * we should generally pick a slightly larger size than what the
242 * strict math would suggest.
244 * @param entry_count expected number of entries in the Bloom filter
245 * @param k number of bits set per entry
246 * @return must be a power of two and smaller or equal to 2^15.
249 GNUNET_BLOCK_GROUP_compute_bloomfilter_size (unsigned int entry_count,
253 unsigned int ideal = (entry_count * k) / 4;
254 uint16_t max = 1 << 15;
256 if (entry_count > max)
259 while ((size < max) && (size < ideal))