sync

Backup service to store encrypted wallet databases (experimental)
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_block_update.c (5882B)


      1 /*
      2   This file is part of SYNC
      3   Copyright (C) 2014-2026 Taler Systems SA
      4 
      5   SYNC is free software; you can redistribute it and/or modify
      6   it under the terms of the GNU General Public License as
      7   published by the Free Software Foundation; either version 3, or
      8   (at your option) any later version.
      9 
     10   SYNC 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
     13   GNU General Public License for more details.
     14 
     15   You should have received a copy of the GNU General Public
     16   License along with SYNC; see the file COPYING.  If not, see
     17   <http://www.gnu.org/licenses/>
     18 */
     19 /**
     20  * @file testing/testing_api_cmd_block_update.c
     21  * @brief command to update a block in the sync backend service.
     22  * @author Iván Ávalos
     23  */
     24 #include "testing_api_cmd_block_common.h"
     25 
     26 
     27 /**
     28  * Process a reference for an update: inherit keys, nonce
     29  * (if not explicitly set), and old_data_hash from the ref's
     30  * curr_hash trait.
     31  *
     32  * @param[in,out] bus state to populate
     33  * @param ref reference command to inherit from
     34  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
     35  */
     36 static enum GNUNET_GenericReturnValue
     37 update_process_ref (
     38   struct BlockUploadState *bus,
     39   const struct TALER_TESTING_Command *ref)
     40 {
     41   if (GNUNET_OK !=
     42       SYNC_TESTING_inherit_account_keys_ (bus,
     43                                           ref))
     44     return GNUNET_SYSERR;
     45 
     46   /* Inherit block nonce if not explicitly set */
     47   if (GNUNET_is_zero (&bus->nonce))
     48   {
     49     const struct SYNC_BlockNonce *nonce;
     50 
     51     if (GNUNET_OK ==
     52         TALER_TESTING_get_trait_block_nonce (ref,
     53                                              &nonce))
     54       bus->nonce = *nonce;
     55   }
     56 
     57   /* Inherit old_data_hash from curr_hash for update */
     58   if (GNUNET_is_zero (&bus->old_data_hash))
     59   {
     60     const struct GNUNET_HashCode *h;
     61 
     62     if (GNUNET_OK ==
     63         TALER_TESTING_get_trait_curr_hash (ref,
     64                                            &h))
     65       bus->old_data_hash = *h;
     66   }
     67 
     68   return GNUNET_OK;
     69 }
     70 
     71 
     72 /**
     73  * Start a block update operation.
     74  *
     75  * @param ctx for HTTP client request processing
     76  * @param sync_url base URL of the Sync server
     77  * @param sync_priv private key of the account
     78  * @param nonce nonce identifying the block
     79  * @param prev_nonce nonce of the preceding block, or NULL
     80  * @param next_nonce nonce of the succeeding block, or NULL
     81  * @param old_data_hash hash of the existing block data
     82  * @param block_data_size number of bytes in @a block_data
     83  * @param block_data the block data
     84  * @param refs_len number of entries in @a object_uids and @a object_incs
     85  * @param object_uids array of object UIDs referenced by this block
     86  * @param object_incs array of reference count adjustments
     87  * @param prev_entry unused for updates (the links do not change)
     88  * @param cb function to call with the result
     89  * @param cb_cls closure for @a cb
     90  * @return handle for the operation
     91  */
     92 static struct SYNC_BlockOperation *
     93 update_start_upload (
     94   struct GNUNET_CURL_Context *ctx,
     95   const char *sync_url,
     96   const struct SYNC_AccountPrivateKeyP *sync_priv,
     97   const struct SYNC_BlockNonce *nonce,
     98   const struct SYNC_BlockNonce *prev_nonce,
     99   const struct SYNC_BlockNonce *next_nonce,
    100   const struct GNUNET_HashCode *old_data_hash,
    101   size_t block_data_size,
    102   const void *block_data,
    103   size_t refs_len,
    104   const struct SYNC_ObjectUID *object_uids,
    105   const int16_t *object_incs,
    106   const struct SYNC_BlockListEntry *prev_entry,
    107   SYNC_BlockOperationCallback cb,
    108   void *cb_cls)
    109 {
    110   struct SYNC_BlockOperationArgs args = {
    111     .nonce = nonce,
    112     .prev_nonce = prev_nonce,
    113     .next_nonce = next_nonce,
    114     .old_data_hash = old_data_hash,
    115     .block_data_size = block_data_size,
    116     .block_data = block_data,
    117     .refs_len = refs_len,
    118     .object_uids = object_uids,
    119     .object_incs = object_incs,
    120   };
    121 
    122   (void) prev_entry;
    123   return SYNC_block_update (ctx,
    124                             sync_url,
    125                             sync_priv,
    126                             &args,
    127                             cb,
    128                             cb_cls);
    129 }
    130 
    131 
    132 struct TALER_TESTING_Command
    133 SYNC_TESTING_cmd_block_update (const char *label,
    134                                const char *sync_url,
    135                                const char *upload_reference,
    136                                const struct SYNC_BlockNonce *nonce,
    137                                const struct GNUNET_HashCode *old_data_hash,
    138                                unsigned int http_status,
    139                                const void *block_data,
    140                                size_t block_data_size,
    141                                unsigned int refs_len,
    142                                const struct SYNC_ObjectUID *object_uids,
    143                                const int16_t *object_incs)
    144 {
    145   struct BlockUploadState *bus;
    146 
    147   GNUNET_assert (refs_len <= MAX_BLOCK_OBJECT_REFS);
    148   bus = GNUNET_new (struct BlockUploadState);
    149   bus->sync_url = sync_url;
    150   bus->upload_reference = upload_reference;
    151   if (NULL != nonce)
    152     bus->nonce = *nonce;
    153   if (NULL != old_data_hash)
    154     bus->old_data_hash = *old_data_hash;
    155   bus->http_status = http_status;
    156   bus->block_data = block_data;
    157   bus->block_data_size = block_data_size;
    158   bus->refs_len = refs_len;
    159   if (0 < refs_len)
    160   {
    161     GNUNET_memcpy (bus->object_uids,
    162                    object_uids,
    163                    refs_len * sizeof (struct SYNC_ObjectUID));
    164     GNUNET_memcpy (bus->object_incs,
    165                    object_incs,
    166                    refs_len * sizeof (int16_t));
    167   }
    168   bus->process_ref = &update_process_ref;
    169   bus->start_upload = &update_start_upload;
    170   {
    171     struct TALER_TESTING_Command cmd = {
    172       .cls = bus,
    173       .label = label,
    174       .run = &SYNC_TESTING_block_upload_run_,
    175       .cleanup = &SYNC_TESTING_block_upload_cleanup_,
    176       .traits = &SYNC_TESTING_block_upload_traits_
    177     };
    178 
    179     return cmd;
    180   }
    181 }