plugin_block_consensus.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2017 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file consensus/plugin_block_consensus.c
  18. * @brief consensus block, either nested block or marker
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "consensus_protocol.h"
  23. #include "gnunet_block_plugin.h"
  24. #include "gnunet_block_group_lib.h"
  25. /**
  26. * Function called to validate a reply or a request. For
  27. * request evaluation, simply pass "NULL" for the reply_block.
  28. *
  29. * @param cls closure
  30. * @param ctx context
  31. * @param type block type
  32. * @param group block group to use
  33. * @param eo control flags
  34. * @param query original query (hash)
  35. * @param xquery extrended query data (can be NULL, depending on type)
  36. * @param xquery_size number of bytes in xquery
  37. * @param reply_block response to validate
  38. * @param reply_block_size number of bytes in reply block
  39. * @return characterization of result
  40. */
  41. static enum GNUNET_BLOCK_EvaluationResult
  42. block_plugin_consensus_evaluate (void *cls,
  43. struct GNUNET_BLOCK_Context *ctx,
  44. enum GNUNET_BLOCK_Type type,
  45. struct GNUNET_BLOCK_Group *group,
  46. enum GNUNET_BLOCK_EvaluationOptions eo,
  47. const struct GNUNET_HashCode *query,
  48. const void *xquery,
  49. size_t xquery_size,
  50. const void *reply_block,
  51. size_t reply_block_size)
  52. {
  53. if (reply_block_size < sizeof (struct ConsensusElement))
  54. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  55. const struct ConsensusElement *ce = reply_block;
  56. if ( (0 != ce->marker) ||
  57. (0 == ce->payload_type ) )
  58. return GNUNET_BLOCK_EVALUATION_OK_MORE;
  59. return GNUNET_BLOCK_evaluate (ctx,
  60. type,
  61. group,
  62. eo,
  63. query,
  64. xquery,
  65. xquery_size,
  66. &ce[1],
  67. reply_block_size - sizeof (struct ConsensusElement));
  68. }
  69. /**
  70. * Function called to obtain the key for a block.
  71. *
  72. * @param cls closure
  73. * @param type block type
  74. * @param block block to get the key for
  75. * @param block_size number of bytes in block
  76. * @param key set to the key (query) for the given block
  77. * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
  78. * (or if extracting a key from a block of this type does not work)
  79. */
  80. static int
  81. block_plugin_consensus_get_key (void *cls,
  82. enum GNUNET_BLOCK_Type type,
  83. const void *block,
  84. size_t block_size,
  85. struct GNUNET_HashCode *key)
  86. {
  87. return GNUNET_SYSERR;
  88. }
  89. /**
  90. * Entry point for the plugin.
  91. */
  92. void *
  93. libgnunet_plugin_block_consensus_init (void *cls)
  94. {
  95. static enum GNUNET_BLOCK_Type types[] =
  96. {
  97. GNUNET_BLOCK_TYPE_CONSENSUS_ELEMENT,
  98. GNUNET_BLOCK_TYPE_ANY /* end of list */
  99. };
  100. struct GNUNET_BLOCK_PluginFunctions *api;
  101. api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
  102. api->evaluate = &block_plugin_consensus_evaluate;
  103. api->get_key = &block_plugin_consensus_get_key;
  104. api->types = types;
  105. return api;
  106. }
  107. /**
  108. * Exit point from the plugin.
  109. */
  110. void *
  111. libgnunet_plugin_block_consensus_done (void *cls)
  112. {
  113. struct GNUNET_BLOCK_PluginFunctions *api = cls;
  114. GNUNET_free (api);
  115. return NULL;
  116. }
  117. /* end of plugin_block_consensus.c */