diff options
Diffstat (limited to 'src/consensus')
-rw-r--r-- | src/consensus/plugin_block_consensus.c | 116 |
1 files changed, 113 insertions, 3 deletions
diff --git a/src/consensus/plugin_block_consensus.c b/src/consensus/plugin_block_consensus.c index cdac12ed5..67309bc79 100644 --- a/src/consensus/plugin_block_consensus.c +++ b/src/consensus/plugin_block_consensus.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet | 2 | This file is part of GNUnet |
3 | Copyright (C) 2017 GNUnet e.V. | 3 | Copyright (C) 2017, 2021 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -80,6 +80,109 @@ block_plugin_consensus_evaluate (void *cls, | |||
80 | 80 | ||
81 | 81 | ||
82 | /** | 82 | /** |
83 | * Function called to validate a query. | ||
84 | * | ||
85 | * @param cls closure | ||
86 | * @param ctx block context | ||
87 | * @param type block type | ||
88 | * @param query original query (hash) | ||
89 | * @param xquery extrended query data (can be NULL, depending on type) | ||
90 | * @param xquery_size number of bytes in @a xquery | ||
91 | * @return #GNUNET_OK if the query is fine, #GNUNET_NO if not | ||
92 | */ | ||
93 | static enum GNUNET_GenericReturnValue | ||
94 | block_plugin_consensus_check_query (void *cls, | ||
95 | enum GNUNET_BLOCK_Type type, | ||
96 | const struct GNUNET_HashCode *query, | ||
97 | const void *xquery, | ||
98 | size_t xquery_size) | ||
99 | { | ||
100 | /* consensus does not use queries/DHT */ | ||
101 | GNUNET_break (0); | ||
102 | return GNUNET_SYSERR; | ||
103 | } | ||
104 | |||
105 | |||
106 | /** | ||
107 | * Function called to validate a block for storage. | ||
108 | * | ||
109 | * @param cls closure | ||
110 | * @param type block type | ||
111 | * @param query key for the block (hash), must match exactly | ||
112 | * @param block block data to validate | ||
113 | * @param block_size number of bytes in @a block | ||
114 | * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not | ||
115 | */ | ||
116 | static enum GNUNET_GenericReturnValue | ||
117 | block_plugin_consensus_check_block (void *cls, | ||
118 | enum GNUNET_BLOCK_Type type, | ||
119 | const struct GNUNET_HashCode *query, | ||
120 | const void *block, | ||
121 | size_t block_size) | ||
122 | { | ||
123 | struct GNUNET_BLOCK_Context *ctx = cls; | ||
124 | const struct ConsensusElement *ce = block; | ||
125 | |||
126 | if (block_size < sizeof(*ce)) | ||
127 | return GNUNET_NO; | ||
128 | if ( (0 != ce->marker) || | ||
129 | (0 == ce->payload_type) ) | ||
130 | return GNUNET_OK; | ||
131 | return GNUNET_BLOCK_check_block (ctx, | ||
132 | ntohl (ce->payload_type), | ||
133 | query, | ||
134 | &ce[1], | ||
135 | block_size - sizeof(*ce)); | ||
136 | } | ||
137 | |||
138 | |||
139 | /** | ||
140 | * Function called to validate a reply to a request. Note that it is assumed | ||
141 | * that the reply has already been matched to the key (and signatures checked) | ||
142 | * as it would be done with the GetKeyFunction and the | ||
143 | * BlockEvaluationFunction. | ||
144 | * | ||
145 | * @param cls closure | ||
146 | * @param type block type | ||
147 | * @param group which block group to use for evaluation | ||
148 | * @param query original query (hash) | ||
149 | * @param xquery extrended query data (can be NULL, depending on type) | ||
150 | * @param xquery_size number of bytes in @a xquery | ||
151 | * @param reply_block response to validate | ||
152 | * @param reply_block_size number of bytes in @a reply_block | ||
153 | * @return characterization of result | ||
154 | */ | ||
155 | static enum GNUNET_BLOCK_ReplyEvaluationResult | ||
156 | block_plugin_consensus_check_reply ( | ||
157 | void *cls, | ||
158 | enum GNUNET_BLOCK_Type type, | ||
159 | struct GNUNET_BLOCK_Group *group, | ||
160 | const struct GNUNET_HashCode *query, | ||
161 | const void *xquery, | ||
162 | size_t xquery_size, | ||
163 | const void *reply_block, | ||
164 | size_t reply_block_size) | ||
165 | { | ||
166 | struct GNUNET_BLOCK_Context *ctx = cls; | ||
167 | const struct ConsensusElement *ce = reply_block; | ||
168 | |||
169 | if (reply_block_size < sizeof(struct ConsensusElement)) | ||
170 | return GNUNET_NO; | ||
171 | if ( (0 != ce->marker) || | ||
172 | (0 == ce->payload_type) ) | ||
173 | return GNUNET_BLOCK_REPLY_OK_MORE; | ||
174 | return GNUNET_BLOCK_check_reply (ctx, | ||
175 | ntohl (ce->payload_type), | ||
176 | group, | ||
177 | query, | ||
178 | xquery, | ||
179 | xquery_size, | ||
180 | &ce[1], | ||
181 | reply_block_size - sizeof(*ce)); | ||
182 | } | ||
183 | |||
184 | |||
185 | /** | ||
83 | * Function called to obtain the key for a block. | 186 | * Function called to obtain the key for a block. |
84 | * | 187 | * |
85 | * @param cls closure | 188 | * @param cls closure |
@@ -90,7 +193,7 @@ block_plugin_consensus_evaluate (void *cls, | |||
90 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported | 193 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported |
91 | * (or if extracting a key from a block of this type does not work) | 194 | * (or if extracting a key from a block of this type does not work) |
92 | */ | 195 | */ |
93 | static int | 196 | static enum GNUNET_GenericReturnValue |
94 | block_plugin_consensus_get_key (void *cls, | 197 | block_plugin_consensus_get_key (void *cls, |
95 | enum GNUNET_BLOCK_Type type, | 198 | enum GNUNET_BLOCK_Type type, |
96 | const void *block, | 199 | const void *block, |
@@ -107,15 +210,20 @@ block_plugin_consensus_get_key (void *cls, | |||
107 | void * | 210 | void * |
108 | libgnunet_plugin_block_consensus_init (void *cls) | 211 | libgnunet_plugin_block_consensus_init (void *cls) |
109 | { | 212 | { |
110 | static enum GNUNET_BLOCK_Type types[] = { | 213 | static const enum GNUNET_BLOCK_Type types[] = { |
111 | GNUNET_BLOCK_TYPE_CONSENSUS_ELEMENT, | 214 | GNUNET_BLOCK_TYPE_CONSENSUS_ELEMENT, |
112 | GNUNET_BLOCK_TYPE_ANY /* end of list */ | 215 | GNUNET_BLOCK_TYPE_ANY /* end of list */ |
113 | }; | 216 | }; |
217 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; | ||
114 | struct GNUNET_BLOCK_PluginFunctions *api; | 218 | struct GNUNET_BLOCK_PluginFunctions *api; |
115 | 219 | ||
116 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); | 220 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); |
221 | api->cls = GNUNET_BLOCK_context_create (cfg); | ||
117 | api->evaluate = &block_plugin_consensus_evaluate; | 222 | api->evaluate = &block_plugin_consensus_evaluate; |
118 | api->get_key = &block_plugin_consensus_get_key; | 223 | api->get_key = &block_plugin_consensus_get_key; |
224 | api->check_query = &block_plugin_consensus_check_query; | ||
225 | api->check_block = &block_plugin_consensus_check_block; | ||
226 | api->check_reply = &block_plugin_consensus_check_reply; | ||
119 | api->types = types; | 227 | api->types = types; |
120 | return api; | 228 | return api; |
121 | } | 229 | } |
@@ -128,7 +236,9 @@ void * | |||
128 | libgnunet_plugin_block_consensus_done (void *cls) | 236 | libgnunet_plugin_block_consensus_done (void *cls) |
129 | { | 237 | { |
130 | struct GNUNET_BLOCK_PluginFunctions *api = cls; | 238 | struct GNUNET_BLOCK_PluginFunctions *api = cls; |
239 | struct GNUNET_BLOCK_Context *bc = api->cls; | ||
131 | 240 | ||
241 | GNUNET_BLOCK_context_destroy (bc); | ||
132 | GNUNET_free (api); | 242 | GNUNET_free (api); |
133 | return NULL; | 243 | return NULL; |
134 | } | 244 | } |