sync_block.c (4154B)
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 struct GNUNET_JSON_Specification espec[] = { 44 GNUNET_JSON_spec_fixed_auto ("nonce", 45 &entry->nonce), 46 GNUNET_JSON_spec_fixed_auto ("block_hash", 47 &entry->hash), 48 GNUNET_JSON_spec_varsize ("data", 49 &entry->data, 50 &entry->data_size), 51 GNUNET_JSON_spec_mark_optional ( 52 GNUNET_JSON_spec_fixed_auto ("prev_nonce", 53 &entry->prev_nonce), 54 &entry->have_prev_nonce), 55 GNUNET_JSON_spec_mark_optional ( 56 GNUNET_JSON_spec_fixed_auto ("next_nonce", 57 &entry->next_nonce), 58 &entry->have_next_nonce), 59 GNUNET_JSON_spec_fixed_auto ("upload_sig", 60 &entry->upload_sig), 61 GNUNET_JSON_spec_fixed_auto ("old_hash", 62 &entry->old_hash), 63 GNUNET_JSON_spec_fixed_auto ("refs_hash", 64 &entry->refs_hash), 65 GNUNET_JSON_spec_end () 66 }; 67 enum GNUNET_GenericReturnValue res; 68 const char *ename; 69 unsigned int eline; 70 71 res = GNUNET_JSON_parse (input, 72 espec, 73 &ename, 74 &eline); 75 if (GNUNET_OK != res) 76 { 77 GNUNET_break_op (0); 78 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 79 "Failed to parse block entry at field %s\n", 80 ename); 81 return GNUNET_SYSERR; 82 } 83 return GNUNET_OK; 84 } 85 86 87 void 88 SYNC_block_entry_clear ( 89 struct SYNC_BlockListEntry *entry) 90 { 91 GNUNET_free (entry->data); 92 entry->data = NULL; 93 entry->data_size = 0; 94 } 95 96 97 json_t * 98 SYNC_block_entry_serialize ( 99 const struct SYNC_BlockNonce *nonce, 100 const struct GNUNET_HashCode *hash, 101 const struct SYNC_BlockNonce *prev_nonce, 102 const struct SYNC_BlockNonce *next_nonce, 103 size_t data_size, 104 const void *data, 105 const struct SYNC_AccountSignatureP *upload_sig, 106 const struct GNUNET_HashCode *old_hash, 107 const struct GNUNET_HashCode *refs_hash) 108 { 109 return GNUNET_JSON_PACK ( 110 GNUNET_JSON_pack_data_auto ("nonce", 111 nonce), 112 GNUNET_JSON_pack_data_auto ("block_hash", 113 hash), 114 GNUNET_JSON_pack_data_varsize ("data", 115 data, 116 data_size), 117 GNUNET_JSON_pack_allow_null ( 118 GNUNET_JSON_pack_data_auto ("prev_nonce", 119 prev_nonce)), 120 GNUNET_JSON_pack_allow_null ( 121 GNUNET_JSON_pack_data_auto ("next_nonce", 122 next_nonce)), 123 GNUNET_JSON_pack_data_auto ("upload_sig", 124 upload_sig), 125 GNUNET_JSON_pack_data_auto ("old_hash", 126 old_hash), 127 GNUNET_JSON_pack_data_auto ("refs_hash", 128 refs_hash)); 129 }