aboutsummaryrefslogtreecommitdiff
path: root/src/fs/plugin_block_fs.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-02-20 17:19:47 +0100
committerChristian Grothoff <christian@grothoff.org>2017-02-20 17:19:47 +0100
commitf6f7fbbe98c110867febbcca647da8308be123c7 (patch)
treeaf69447cf4f08c417197685855c097c132aea8a1 /src/fs/plugin_block_fs.c
parenta3882b58f1c5976677aa65b0af8a48e8e946b06e (diff)
downloadgnunet-f6f7fbbe98c110867febbcca647da8308be123c7.tar.gz
gnunet-f6f7fbbe98c110867febbcca647da8308be123c7.zip
completed big block refactoring in preparation for SET-BLOCK integration
Diffstat (limited to 'src/fs/plugin_block_fs.c')
-rw-r--r--src/fs/plugin_block_fs.c95
1 files changed, 63 insertions, 32 deletions
diff --git a/src/fs/plugin_block_fs.c b/src/fs/plugin_block_fs.c
index 038734082..6c574fca2 100644
--- a/src/fs/plugin_block_fs.c
+++ b/src/fs/plugin_block_fs.c
@@ -28,6 +28,7 @@
28#include "gnunet_fs_service.h" 28#include "gnunet_fs_service.h"
29#include "block_fs.h" 29#include "block_fs.h"
30#include "gnunet_signatures.h" 30#include "gnunet_signatures.h"
31#include "gnunet_constants.h"
31#include "gnunet_block_group_lib.h" 32#include "gnunet_block_group_lib.h"
32 33
33 34
@@ -37,10 +38,36 @@
37 */ 38 */
38#define BLOOMFILTER_K 16 39#define BLOOMFILTER_K 16
39 40
41
40/** 42/**
41 * How big is the BF we use for FS blocks? 43 * How many bytes should a bloomfilter be if we have already seen
44 * entry_count responses? Note that #GNUNET_CONSTANTS_BLOOMFILTER_K
45 * gives us the number of bits set per entry. Furthermore, we should
46 * not re-size the filter too often (to keep it cheap).
47 *
48 * Since other peers will also add entries but not resize the filter,
49 * we should generally pick a slightly larger size than what the
50 * strict math would suggest.
51 *
52 * @param entry_count expected number of entries in the Bloom filter
53 * @return must be a power of two and smaller or equal to 2^15.
42 */ 54 */
43#define FS_BF_SIZE 8 55static size_t
56compute_bloomfilter_size (unsigned int entry_count)
57{
58 size_t size;
59 unsigned int ideal = (entry_count * GNUNET_CONSTANTS_BLOOMFILTER_K) / 4;
60 uint16_t max = 1 << 15;
61
62 if (entry_count > max)
63 return max;
64 size = 8;
65 while ((size < max) && (size < ideal))
66 size *= 2;
67 if (size > max)
68 return max;
69 return size;
70}
44 71
45 72
46/** 73/**
@@ -51,16 +78,21 @@
51 * @param nonce random value used to seed the group creation 78 * @param nonce random value used to seed the group creation
52 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh 79 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
53 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh 80 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
81 * @param va variable arguments specific to @a type
54 * @return block group handle, NULL if block groups are not supported 82 * @return block group handle, NULL if block groups are not supported
55 * by this @a type of block (this is not an error) 83 * by this @a type of block (this is not an error)
56 */ 84 */
57static struct GNUNET_BLOCK_Group * 85static struct GNUNET_BLOCK_Group *
58block_plugin_fs_create_group (void *cls, 86block_plugin_fs_create_group (void *cls,
59 enum GNUNET_BLOCK_Type type, 87 enum GNUNET_BLOCK_Type type,
60 uint32_t nonce, 88 uint32_t nonce,
61 const void *raw_data, 89 const void *raw_data,
62 size_t raw_data_size) 90 size_t raw_data_size,
91 va_list va)
63{ 92{
93 unsigned int size;
94 const char *guard;
95
64 switch (type) 96 switch (type)
65 { 97 {
66 case GNUNET_BLOCK_TYPE_FS_DBLOCK: 98 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
@@ -68,8 +100,23 @@ block_plugin_fs_create_group (void *cls,
68 case GNUNET_BLOCK_TYPE_FS_IBLOCK: 100 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
69 return NULL; 101 return NULL;
70 case GNUNET_BLOCK_TYPE_FS_UBLOCK: 102 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
103 guard = va_arg (va, const char *);
104 if (0 != memcmp (guard,
105 "fs-seen-set-size",
106 strlen ("fs-seen-set-size")))
107 {
108 /* va-args invalid! bad bug, complain! */
109 GNUNET_break (0);
110 size = 8;
111 }
112 else
113 {
114 size = compute_bloomfilter_size (va_arg (va, unsigned int));
115 }
116 if (0 == size)
117 size = raw_data_size; /* not for us to determine, use what we got! */
71 return GNUNET_BLOCK_GROUP_bf_create (cls, 118 return GNUNET_BLOCK_GROUP_bf_create (cls,
72 FS_BF_SIZE, 119 size,
73 BLOOMFILTER_K, 120 BLOOMFILTER_K,
74 type, 121 type,
75 nonce, 122 nonce,
@@ -91,10 +138,9 @@ block_plugin_fs_create_group (void *cls,
91 * 138 *
92 * @param cls closure 139 * @param cls closure
93 * @param type block type 140 * @param type block type
141 * @param bg group to use for evaluation
94 * @param eo control flags 142 * @param eo control flags
95 * @param query original query (hash) 143 * @param query original query (hash)
96 * @param bf pointer to bloom filter associated with query; possibly updated (!)
97 * @param bf_mutator mutation value for @a bf
98 * @param xquery extrended query data (can be NULL, depending on type) 144 * @param xquery extrended query data (can be NULL, depending on type)
99 * @param xquery_size number of bytes in @a xquery 145 * @param xquery_size number of bytes in @a xquery
100 * @param reply_block response to validate 146 * @param reply_block response to validate
@@ -104,10 +150,9 @@ block_plugin_fs_create_group (void *cls,
104static enum GNUNET_BLOCK_EvaluationResult 150static enum GNUNET_BLOCK_EvaluationResult
105block_plugin_fs_evaluate (void *cls, 151block_plugin_fs_evaluate (void *cls,
106 enum GNUNET_BLOCK_Type type, 152 enum GNUNET_BLOCK_Type type,
153 struct GNUNET_BLOCK_Group *bg,
107 enum GNUNET_BLOCK_EvaluationOptions eo, 154 enum GNUNET_BLOCK_EvaluationOptions eo,
108 const struct GNUNET_HashCode *query, 155 const struct GNUNET_HashCode *query,
109 struct GNUNET_CONTAINER_BloomFilter **bf,
110 int32_t bf_mutator,
111 const void *xquery, 156 const void *xquery,
112 size_t xquery_size, 157 size_t xquery_size,
113 const void *reply_block, 158 const void *reply_block,
@@ -116,7 +161,6 @@ block_plugin_fs_evaluate (void *cls,
116 const struct UBlock *ub; 161 const struct UBlock *ub;
117 struct GNUNET_HashCode hc; 162 struct GNUNET_HashCode hc;
118 struct GNUNET_HashCode chash; 163 struct GNUNET_HashCode chash;
119 struct GNUNET_HashCode mhash;
120 164
121 switch (type) 165 switch (type)
122 { 166 {
@@ -170,26 +214,13 @@ block_plugin_fs_evaluate (void *cls,
170 GNUNET_break_op (0); 214 GNUNET_break_op (0);
171 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 215 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
172 } 216 }
173 if (NULL != bf) 217 GNUNET_CRYPTO_hash (reply_block,
174 { 218 reply_block_size,
175 GNUNET_CRYPTO_hash (reply_block, 219 &chash);
176 reply_block_size, 220 if (GNUNET_YES ==
177 &chash); 221 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
178 GNUNET_BLOCK_mingle_hash (&chash, 222 &chash))
179 bf_mutator, 223 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
180 &mhash);
181 if (NULL != *bf)
182 {
183 if (GNUNET_YES ==
184 GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
185 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
186 }
187 else
188 {
189 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
190 }
191 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
192 }
193 return GNUNET_BLOCK_EVALUATION_OK_MORE; 224 return GNUNET_BLOCK_EVALUATION_OK_MORE;
194 default: 225 default:
195 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; 226 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;