aboutsummaryrefslogtreecommitdiff
path: root/src/contrib/service/consensus/plugin_block_consensus.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/service/consensus/plugin_block_consensus.c')
-rw-r--r--src/contrib/service/consensus/plugin_block_consensus.c222
1 files changed, 222 insertions, 0 deletions
diff --git a/src/contrib/service/consensus/plugin_block_consensus.c b/src/contrib/service/consensus/plugin_block_consensus.c
new file mode 100644
index 000000000..241d8fc7b
--- /dev/null
+++ b/src/contrib/service/consensus/plugin_block_consensus.c
@@ -0,0 +1,222 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2017, 2021 GNUnet e.V.
4
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
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file consensus/plugin_block_consensus.c
23 * @brief consensus block, either nested block or marker
24 * @author Christian Grothoff
25 */
26
27#include "platform.h"
28#include "consensus_protocol.h"
29#include "gnunet_block_plugin.h"
30#include "gnunet_block_group_lib.h"
31
32
33/**
34 * Our closure.
35 */
36struct BlockContext
37{
38 /**
39 * Configuration to use.
40 */
41 const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43 /**
44 * Lazily initialized block context.
45 */
46 struct GNUNET_BLOCK_Context *bc;
47};
48
49
50
51/**
52 * Function called to validate a query.
53 *
54 * @param cls closure
55 * @param type block type
56 * @param query original query (hash)
57 * @param xquery extrended query data (can be NULL, depending on type)
58 * @param xquery_size number of bytes in @a xquery
59 * @return #GNUNET_OK if the query is fine, #GNUNET_NO if not
60 */
61static enum GNUNET_GenericReturnValue
62block_plugin_consensus_check_query (void *cls,
63 enum GNUNET_BLOCK_Type type,
64 const struct GNUNET_HashCode *query,
65 const void *xquery,
66 size_t xquery_size)
67{
68 /* consensus does not use queries/DHT */
69 GNUNET_break (0);
70 return GNUNET_SYSERR;
71}
72
73
74/**
75 * Function called to validate a block for storage.
76 *
77 * @param cls closure
78 * @param type block type
79 * @param block block data to validate
80 * @param block_size number of bytes in @a block
81 * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not
82 */
83static enum GNUNET_GenericReturnValue
84block_plugin_consensus_check_block (void *cls,
85 enum GNUNET_BLOCK_Type type,
86 const void *block,
87 size_t block_size)
88{
89 struct BlockContext *ctx = cls;
90 const struct ConsensusElement *ce = block;
91
92 if (block_size < sizeof(*ce))
93 {
94 GNUNET_break_op (0);
95 return GNUNET_NO;
96 }
97 if ( (0 != ce->marker) ||
98 (0 == ce->payload_type) )
99 return GNUNET_OK;
100 if (NULL == ctx->bc)
101 ctx->bc = GNUNET_BLOCK_context_create (ctx->cfg);
102 return GNUNET_BLOCK_check_block (ctx->bc,
103 ntohl (ce->payload_type),
104 &ce[1],
105 block_size - sizeof(*ce));
106}
107
108
109/**
110 * Function called to validate a reply to a request. Note that it is assumed
111 * that the reply has already been matched to the key (and signatures checked)
112 * as it would be done with the GetKeyFunction and the
113 * BlockEvaluationFunction.
114 *
115 * @param cls closure
116 * @param type block type
117 * @param group which block group to use for evaluation
118 * @param query original query (hash)
119 * @param xquery extrended query data (can be NULL, depending on type)
120 * @param xquery_size number of bytes in @a xquery
121 * @param reply_block response to validate
122 * @param reply_block_size number of bytes in @a reply_block
123 * @return characterization of result
124 */
125static enum GNUNET_BLOCK_ReplyEvaluationResult
126block_plugin_consensus_check_reply (
127 void *cls,
128 enum GNUNET_BLOCK_Type type,
129 struct GNUNET_BLOCK_Group *group,
130 const struct GNUNET_HashCode *query,
131 const void *xquery,
132 size_t xquery_size,
133 const void *reply_block,
134 size_t reply_block_size)
135{
136 struct BlockContext *ctx = cls;
137 const struct ConsensusElement *ce = reply_block;
138
139 GNUNET_assert (reply_block_size >= sizeof(struct ConsensusElement));
140 if ( (0 != ce->marker) ||
141 (0 == ce->payload_type) )
142 return GNUNET_BLOCK_REPLY_OK_MORE;
143 if (NULL == ctx->bc)
144 ctx->bc = GNUNET_BLOCK_context_create (ctx->cfg);
145 return GNUNET_BLOCK_check_reply (ctx->bc,
146 ntohl (ce->payload_type),
147 group,
148 query,
149 xquery,
150 xquery_size,
151 &ce[1],
152 reply_block_size - sizeof(*ce));
153}
154
155
156/**
157 * Function called to obtain the key for a block.
158 *
159 * @param cls closure
160 * @param type block type
161 * @param block block to get the key for
162 * @param block_size number of bytes in block
163 * @param key set to the key (query) for the given block
164 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
165 * (or if extracting a key from a block of this type does not work)
166 */
167static enum GNUNET_GenericReturnValue
168block_plugin_consensus_get_key (void *cls,
169 enum GNUNET_BLOCK_Type type,
170 const void *block,
171 size_t block_size,
172 struct GNUNET_HashCode *key)
173{
174 return GNUNET_SYSERR;
175}
176
177
178/**
179 * Entry point for the plugin.
180 */
181void *
182libgnunet_plugin_block_consensus_init (void *cls)
183{
184 static const enum GNUNET_BLOCK_Type types[] = {
185 GNUNET_BLOCK_TYPE_CONSENSUS_ELEMENT,
186 GNUNET_BLOCK_TYPE_ANY /* end of list */
187 };
188 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
189 struct BlockContext *ctx;
190 struct GNUNET_BLOCK_PluginFunctions *api;
191
192 ctx = GNUNET_new (struct BlockContext);
193 ctx->cfg = cfg;
194 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
195 api->cls = ctx;
196 api->get_key = &block_plugin_consensus_get_key;
197 api->check_query = &block_plugin_consensus_check_query;
198 api->check_block = &block_plugin_consensus_check_block;
199 api->check_reply = &block_plugin_consensus_check_reply;
200 api->types = types;
201 return api;
202}
203
204
205/**
206 * Exit point from the plugin.
207 */
208void *
209libgnunet_plugin_block_consensus_done (void *cls)
210{
211 struct GNUNET_BLOCK_PluginFunctions *api = cls;
212 struct BlockContext *ctx = api->cls;
213
214 if (NULL != ctx->bc)
215 GNUNET_BLOCK_context_destroy (ctx->bc);
216 GNUNET_free (ctx);
217 GNUNET_free (api);
218 return NULL;
219}
220
221
222/* end of plugin_block_consensus.c */