diff options
Diffstat (limited to 'src/block/plugin_block_test.c')
-rw-r--r-- | src/block/plugin_block_test.c | 99 |
1 files changed, 74 insertions, 25 deletions
diff --git a/src/block/plugin_block_test.c b/src/block/plugin_block_test.c index b692d6230..e359acd7f 100644 --- a/src/block/plugin_block_test.c +++ b/src/block/plugin_block_test.c | |||
@@ -27,7 +27,7 @@ | |||
27 | 27 | ||
28 | #include "platform.h" | 28 | #include "platform.h" |
29 | #include "gnunet_block_plugin.h" | 29 | #include "gnunet_block_plugin.h" |
30 | 30 | #include "gnunet_block_group_lib.h" | |
31 | 31 | ||
32 | /** | 32 | /** |
33 | * Number of bits we set per entry in the bloomfilter. | 33 | * Number of bits we set per entry in the bloomfilter. |
@@ -36,15 +36,68 @@ | |||
36 | #define BLOOMFILTER_K 16 | 36 | #define BLOOMFILTER_K 16 |
37 | 37 | ||
38 | /** | 38 | /** |
39 | * How big is the BF we use for DHT blocks? | ||
40 | */ | ||
41 | #define TEST_BF_SIZE 8 | ||
42 | |||
43 | |||
44 | /** | ||
45 | * Create a new block group. | ||
46 | * | ||
47 | * @param ctx block context in which the block group is created | ||
48 | * @param type type of the block for which we are creating the group | ||
49 | * @param nonce random value used to seed the group creation | ||
50 | * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh | ||
51 | * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh | ||
52 | * @param va variable arguments specific to @a type | ||
53 | * @return block group handle, NULL if block groups are not supported | ||
54 | * by this @a type of block (this is not an error) | ||
55 | */ | ||
56 | static struct GNUNET_BLOCK_Group * | ||
57 | block_plugin_test_create_group (void *cls, | ||
58 | enum GNUNET_BLOCK_Type type, | ||
59 | uint32_t nonce, | ||
60 | const void *raw_data, | ||
61 | size_t raw_data_size, | ||
62 | va_list va) | ||
63 | { | ||
64 | unsigned int bf_size; | ||
65 | const char *guard; | ||
66 | |||
67 | guard = va_arg (va, const char *); | ||
68 | if (0 == strcmp (guard, | ||
69 | "seen-set-size")) | ||
70 | bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int), | ||
71 | BLOOMFILTER_K); | ||
72 | else if (0 == strcmp (guard, | ||
73 | "filter-size")) | ||
74 | bf_size = va_arg (va, unsigned int); | ||
75 | else | ||
76 | { | ||
77 | GNUNET_break (0); | ||
78 | bf_size = TEST_BF_SIZE; | ||
79 | } | ||
80 | GNUNET_break (NULL == va_arg (va, const char *)); | ||
81 | return GNUNET_BLOCK_GROUP_bf_create (cls, | ||
82 | bf_size, | ||
83 | BLOOMFILTER_K, | ||
84 | type, | ||
85 | nonce, | ||
86 | raw_data, | ||
87 | raw_data_size); | ||
88 | } | ||
89 | |||
90 | |||
91 | /** | ||
39 | * Function called to validate a reply or a request. For | 92 | * Function called to validate a reply or a request. For |
40 | * request evaluation, simply pass "NULL" for the reply_block. | 93 | * request evaluation, simply pass "NULL" for the reply_block. |
41 | * | 94 | * |
42 | * @param cls closure | 95 | * @param cls closure |
96 | * @param ctx block context | ||
43 | * @param type block type | 97 | * @param type block type |
98 | * @param group group to check against | ||
44 | * @param eo control flags | 99 | * @param eo control flags |
45 | * @param query original query (hash) | 100 | * @param query original query (hash) |
46 | * @param bf pointer to bloom filter associated with query; possibly updated (!) | ||
47 | * @param bf_mutator mutation value for @a bf | ||
48 | * @param xquery extrended query data (can be NULL, depending on type) | 101 | * @param xquery extrended query data (can be NULL, depending on type) |
49 | * @param xquery_size number of bytes in @a xquery | 102 | * @param xquery_size number of bytes in @a xquery |
50 | * @param reply_block response to validate | 103 | * @param reply_block response to validate |
@@ -53,21 +106,23 @@ | |||
53 | */ | 106 | */ |
54 | static enum GNUNET_BLOCK_EvaluationResult | 107 | static enum GNUNET_BLOCK_EvaluationResult |
55 | block_plugin_test_evaluate (void *cls, | 108 | block_plugin_test_evaluate (void *cls, |
109 | struct GNUNET_BLOCK_Context *ctx, | ||
56 | enum GNUNET_BLOCK_Type type, | 110 | enum GNUNET_BLOCK_Type type, |
111 | struct GNUNET_BLOCK_Group *group, | ||
57 | enum GNUNET_BLOCK_EvaluationOptions eo, | 112 | enum GNUNET_BLOCK_EvaluationOptions eo, |
58 | const struct GNUNET_HashCode *query, | 113 | const struct GNUNET_HashCode *query, |
59 | struct GNUNET_CONTAINER_BloomFilter **bf, | ||
60 | int32_t bf_mutator, | ||
61 | const void *xquery, | 114 | const void *xquery, |
62 | size_t xquery_size, | 115 | size_t xquery_size, |
63 | const void *reply_block, | 116 | const void *reply_block, |
64 | size_t reply_block_size) | 117 | size_t reply_block_size) |
65 | { | 118 | { |
66 | struct GNUNET_HashCode chash; | 119 | struct GNUNET_HashCode chash; |
67 | struct GNUNET_HashCode mhash; | ||
68 | 120 | ||
69 | if ( GNUNET_BLOCK_TYPE_TEST != type) | 121 | if ( GNUNET_BLOCK_TYPE_TEST != type) |
122 | { | ||
123 | GNUNET_break (0); | ||
70 | return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; | 124 | return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; |
125 | } | ||
71 | if (0 != xquery_size) | 126 | if (0 != xquery_size) |
72 | { | 127 | { |
73 | GNUNET_break_op (0); | 128 | GNUNET_break_op (0); |
@@ -75,22 +130,13 @@ block_plugin_test_evaluate (void *cls, | |||
75 | } | 130 | } |
76 | if (NULL == reply_block) | 131 | if (NULL == reply_block) |
77 | return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; | 132 | return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; |
78 | 133 | GNUNET_CRYPTO_hash (reply_block, | |
79 | if (NULL != bf) | 134 | reply_block_size, |
80 | { | 135 | &chash); |
81 | GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash); | 136 | if (GNUNET_YES == |
82 | GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash); | 137 | GNUNET_BLOCK_GROUP_bf_test_and_set (group, |
83 | if (NULL != *bf) | 138 | &chash)) |
84 | { | 139 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; |
85 | if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash)) | ||
86 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; | ||
87 | } | ||
88 | else | ||
89 | { | ||
90 | *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K); | ||
91 | } | ||
92 | GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash); | ||
93 | } | ||
94 | return GNUNET_BLOCK_EVALUATION_OK_MORE; | 140 | return GNUNET_BLOCK_EVALUATION_OK_MORE; |
95 | } | 141 | } |
96 | 142 | ||
@@ -107,9 +153,11 @@ block_plugin_test_evaluate (void *cls, | |||
107 | * (or if extracting a key from a block of this type does not work) | 153 | * (or if extracting a key from a block of this type does not work) |
108 | */ | 154 | */ |
109 | static int | 155 | static int |
110 | block_plugin_test_get_key (void *cls, enum GNUNET_BLOCK_Type type, | 156 | block_plugin_test_get_key (void *cls, |
111 | const void *block, size_t block_size, | 157 | enum GNUNET_BLOCK_Type type, |
112 | struct GNUNET_HashCode * key) | 158 | const void *block, |
159 | size_t block_size, | ||
160 | struct GNUNET_HashCode *key) | ||
113 | { | 161 | { |
114 | /* always fails since there is no fixed relationship between | 162 | /* always fails since there is no fixed relationship between |
115 | * keys and values for test values */ | 163 | * keys and values for test values */ |
@@ -136,6 +184,7 @@ libgnunet_plugin_block_test_init (void *cls) | |||
136 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); | 184 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); |
137 | api->evaluate = &block_plugin_test_evaluate; | 185 | api->evaluate = &block_plugin_test_evaluate; |
138 | api->get_key = &block_plugin_test_get_key; | 186 | api->get_key = &block_plugin_test_get_key; |
187 | api->create_group = &block_plugin_test_create_group; | ||
139 | api->types = types; | 188 | api->types = types; |
140 | return api; | 189 | return api; |
141 | } | 190 | } |