sync_block.c (4379B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2026 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file util/sync_block.c 18 * @brief shared logic for block JSON parsing and serialization 19 * @author Iván Ávalos 20 */ 21 #include "platform.h" 22 #include <gnunet/gnunet_common.h> 23 #include <gnunet/gnunet_json_lib.h> 24 #include <jansson.h> 25 #include <taler/taler_json_lib.h> 26 #include <sync/sync_util.h> 27 28 29 void 30 SYNC_block_nonce_generate ( 31 struct SYNC_BlockNonce *nonce) 32 { 33 GNUNET_CRYPTO_random_block (nonce, 34 sizeof (struct SYNC_BlockNonce)); 35 } 36 37 38 enum GNUNET_GenericReturnValue 39 SYNC_block_entry_parse ( 40 json_t *input, 41 struct SYNC_BlockListEntry *entry) 42 { 43 bool missing_prev; 44 bool missing_next; 45 struct GNUNET_JSON_Specification espec[] = { 46 GNUNET_JSON_spec_fixed_auto ("nonce", 47 &entry->nonce), 48 GNUNET_JSON_spec_fixed_auto ("block_hash", 49 &entry->hash), 50 GNUNET_JSON_spec_varsize ("data", 51 &entry->data, 52 &entry->data_size), 53 GNUNET_JSON_spec_mark_optional ( 54 GNUNET_JSON_spec_fixed_auto ("prev_nonce", 55 &entry->prev_nonce), 56 &missing_prev), 57 GNUNET_JSON_spec_mark_optional ( 58 GNUNET_JSON_spec_fixed_auto ("next_nonce", 59 &entry->next_nonce), 60 &missing_next), 61 GNUNET_JSON_spec_fixed_auto ("upload_sig", 62 &entry->upload_sig), 63 GNUNET_JSON_spec_fixed_auto ("old_hash", 64 &entry->old_hash), 65 GNUNET_JSON_spec_fixed_auto ("refs_hash", 66 &entry->refs_hash), 67 GNUNET_JSON_spec_end () 68 }; 69 enum GNUNET_GenericReturnValue res; 70 const char *ename; 71 unsigned int eline; 72 73 res = GNUNET_JSON_parse (input, 74 espec, 75 &ename, 76 &eline); 77 if (GNUNET_OK != res) 78 { 79 GNUNET_break_op (0); 80 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 81 "Failed to parse block entry at field %s\n", 82 ename); 83 return GNUNET_SYSERR; 84 } 85 /* The optional-field tracking flag reports whether the field is 86 *missing*; the entry records the inverse. */ 87 entry->have_prev_nonce = ! missing_prev; 88 entry->have_next_nonce = ! missing_next; 89 return GNUNET_OK; 90 } 91 92 93 void 94 SYNC_block_entry_clear ( 95 struct SYNC_BlockListEntry *entry) 96 { 97 GNUNET_free (entry->data); 98 entry->data = NULL; 99 entry->data_size = 0; 100 } 101 102 103 json_t * 104 SYNC_block_entry_serialize ( 105 const struct SYNC_BlockNonce *nonce, 106 const struct GNUNET_HashCode *hash, 107 const struct SYNC_BlockNonce *prev_nonce, 108 const struct SYNC_BlockNonce *next_nonce, 109 size_t data_size, 110 const void *data, 111 const struct SYNC_AccountSignatureP *upload_sig, 112 const struct GNUNET_HashCode *old_hash, 113 const struct GNUNET_HashCode *refs_hash) 114 { 115 return GNUNET_JSON_PACK ( 116 GNUNET_JSON_pack_data_auto ("nonce", 117 nonce), 118 GNUNET_JSON_pack_data_auto ("block_hash", 119 hash), 120 GNUNET_JSON_pack_data_varsize ("data", 121 data, 122 data_size), 123 GNUNET_JSON_pack_allow_null ( 124 GNUNET_JSON_pack_data_auto ("prev_nonce", 125 prev_nonce)), 126 GNUNET_JSON_pack_allow_null ( 127 GNUNET_JSON_pack_data_auto ("next_nonce", 128 next_nonce)), 129 GNUNET_JSON_pack_data_auto ("upload_sig", 130 upload_sig), 131 GNUNET_JSON_pack_data_auto ("old_hash", 132 old_hash), 133 GNUNET_JSON_pack_data_auto ("refs_hash", 134 refs_hash)); 135 }