Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / consensus / plugin_block_consensus.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2017 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
19 /**
20  * @file consensus/plugin_block_consensus.c
21  * @brief consensus block, either nested block or marker
22  * @author Christian Grothoff
23  */
24
25 #include "platform.h"
26 #include "consensus_protocol.h"
27 #include "gnunet_block_plugin.h"
28 #include "gnunet_block_group_lib.h"
29
30
31 /**
32  * Function called to validate a reply or a request.  For
33  * request evaluation, simply pass "NULL" for the reply_block.
34  *
35  * @param cls closure
36  * @param ctx context
37  * @param type block type
38  * @param group block group to use
39  * @param eo control flags
40  * @param query original query (hash)
41  * @param xquery extrended query data (can be NULL, depending on type)
42  * @param xquery_size number of bytes in xquery
43  * @param reply_block response to validate
44  * @param reply_block_size number of bytes in reply block
45  * @return characterization of result
46  */
47 static enum GNUNET_BLOCK_EvaluationResult
48 block_plugin_consensus_evaluate (void *cls,
49                                  struct GNUNET_BLOCK_Context *ctx,
50                                  enum GNUNET_BLOCK_Type type,
51                                  struct GNUNET_BLOCK_Group *group,
52                                  enum GNUNET_BLOCK_EvaluationOptions eo,
53                                  const struct GNUNET_HashCode *query,
54                                  const void *xquery,
55                                  size_t xquery_size,
56                                  const void *reply_block,
57                                  size_t reply_block_size)
58 {
59   if (reply_block_size < sizeof (struct ConsensusElement))
60     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
61
62   const struct ConsensusElement *ce = reply_block;
63
64   if ( (0 != ce->marker) ||
65        (0 == ce->payload_type ) )
66     return GNUNET_BLOCK_EVALUATION_OK_MORE;
67
68   return GNUNET_BLOCK_evaluate (ctx,
69                                 type,
70                                 group,
71                                 eo,
72                                 query,
73                                 xquery,
74                                 xquery_size,
75                                 &ce[1],
76                                 reply_block_size - sizeof (struct ConsensusElement));
77 }
78
79
80 /**
81  * Function called to obtain the key for a block.
82  *
83  * @param cls closure
84  * @param type block type
85  * @param block block to get the key for
86  * @param block_size number of bytes in block
87  * @param key set to the key (query) for the given block
88  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
89  *         (or if extracting a key from a block of this type does not work)
90  */
91 static int
92 block_plugin_consensus_get_key (void *cls,
93                                enum GNUNET_BLOCK_Type type,
94                                const void *block,
95                                size_t block_size,
96                                struct GNUNET_HashCode *key)
97 {
98   return GNUNET_SYSERR;
99 }
100
101
102 /**
103  * Entry point for the plugin.
104  */
105 void *
106 libgnunet_plugin_block_consensus_init (void *cls)
107 {
108   static enum GNUNET_BLOCK_Type types[] =
109   {
110     GNUNET_BLOCK_TYPE_CONSENSUS_ELEMENT,
111     GNUNET_BLOCK_TYPE_ANY       /* end of list */
112   };
113   struct GNUNET_BLOCK_PluginFunctions *api;
114
115   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
116   api->evaluate = &block_plugin_consensus_evaluate;
117   api->get_key = &block_plugin_consensus_get_key;
118   api->types = types;
119   return api;
120 }
121
122
123 /**
124  * Exit point from the plugin.
125  */
126 void *
127 libgnunet_plugin_block_consensus_done (void *cls)
128 {
129   struct GNUNET_BLOCK_PluginFunctions *api = cls;
130
131   GNUNET_free (api);
132   return NULL;
133 }
134
135 /* end of plugin_block_consensus.c */