donaudb_plugin.h (17245B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2014-2023 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 include/donaudb_plugin.h 18 * @brief Low-level (statement-level) database access for the donau 19 * @author Johannes Casaburi 20 */ 21 #ifndef DONAUDB_PLUGIN_H 22 #define DONAUDB_PLUGIN_H 23 // #include <jansson.h> 24 #include <gnunet/gnunet_db_lib.h> 25 #include <taler/taler_json_lib.h> 26 #include "donau_signatures.h" 27 #include "donau_util.h" 28 29 /** 30 * Meta data about an donau signing key. 31 */ 32 struct DONAUDB_SignkeyMetaData 33 { 34 /** 35 * Start time of the validity period for this key. 36 */ 37 struct GNUNET_TIME_Timestamp valid_from; 38 39 /** 40 * The donau will sign messages with this key between @e start and this time. 41 */ 42 struct GNUNET_TIME_Timestamp expire_sign; 43 44 /** 45 * When do signatures with this sign key become invalid? 46 * After this point, these signatures cannot be used in (legal) 47 * disputes anymore, as the Donau is then allowed to destroy its side 48 * of the evidence. @e expire_legal is expected to be significantly 49 * larger than @e expire_sign (by a year or more). 50 */ 51 struct GNUNET_TIME_Timestamp expire_legal; 52 53 }; 54 55 /** 56 * Meta data about a charity. 57 */ 58 struct DONAUDB_CharityMetaData 59 { 60 /** 61 * Charity public key 62 */ 63 struct DONAU_CharityPublicKeyP charity_pub; 64 65 /** 66 * Charity name 67 */ 68 char *charity_name; 69 70 /** 71 * Charity url 72 */ 73 char *charity_url; 74 75 /** 76 * Charity yearly donation limit 77 */ 78 struct TALER_Amount max_per_year; 79 80 /** 81 * Charity donations received in the current year 82 */ 83 struct TALER_Amount receipts_to_date; 84 85 /** 86 * Current year 87 */ 88 uint32_t current_year; 89 90 }; 91 92 /** 93 * Meta data about issued receipts of a request. 94 */ 95 struct DONAUDB_IssuedReceiptsMetaData 96 { 97 /** 98 * Charity id 99 */ 100 uint64_t charity_id; 101 102 /** 103 * total issued amount of the receipts 104 */ 105 struct TALER_Amount amount; 106 107 /** 108 * number of signatures 109 */ 110 size_t num_sig; 111 112 /** 113 * Array of blinded signatures 114 */ 115 struct DONAU_BlindedDonationUnitSignature *blinded_sigs; 116 117 }; 118 119 /** 120 * @brief All information about a donation unit key. 121 */ 122 struct DONAUDB_DonationUnitKey 123 { 124 /** 125 * The private key of the donation unit. Will be NULL if the private 126 * key is not available. 127 */ 128 struct DONAU_DonationUnitPublicKey donation_unit_priv; 129 130 /** 131 * Decoded donation unit public key. 132 */ 133 struct DONAU_DonationUnitPublicKey donation_unit_pub; 134 135 }; 136 137 138 /** 139 * Signature of a function called with information about the donau's 140 * signing keys. 141 * 142 * @param cls closure with a `struct DH_KeyStateHandle *` 143 * @param donau_pub public key of the donau 144 * @param meta meta data information about the signing type (expirations) 145 */ 146 typedef void 147 (*DONAUDB_IterateActiveSigningKeysCallback)( 148 void *cls, 149 const struct DONAU_DonauPublicKeyP *donau_pub, 150 struct DONAUDB_SignkeyMetaData *meta); 151 152 /** 153 * Return donation units. 154 * 155 * @param cls closure 156 */ 157 typedef enum GNUNET_GenericReturnValue 158 (*DONAUDB_IterateDonationUnitsCallback)( 159 void *cls, 160 const struct DONAU_DonationUnitHashP *h_donation_unit_pub, 161 const struct DONAU_DonationUnitPublicKey *donation_unit_pub, 162 uint64_t validity_year, 163 struct TALER_Amount *value); 164 165 166 /** 167 * Return charities. 168 * 169 * @param cls closure 170 */ 171 typedef enum GNUNET_GenericReturnValue 172 (*DONAUDB_GetCharitiesCallback)( 173 void *cls, 174 uint64_t charity_id, 175 const struct DONAU_CharityPublicKeyP *charity_pub, 176 const char *charity_name, 177 const struct TALER_Amount *max_per_year, 178 uint32_t current_year, 179 const struct TALER_Amount *receipts_to_date); 180 181 /** 182 * Return history. 183 * 184 * @param cls closure 185 */ 186 typedef enum GNUNET_GenericReturnValue 187 (*DONAUDB_GetHistoryCallback)( 188 void *cls, 189 unsigned long long charity_id, 190 struct TALER_Amount final_amount, 191 uint64_t donation_year); 192 193 /** 194 * @brief The plugin API, returned from the plugin's "init" function. 195 * The argument given to "init" is simply a configuration handle. 196 */ 197 struct DONAUDB_Plugin 198 { 199 200 /** 201 * Closure for all callbacks. 202 */ 203 void *cls; 204 205 /** 206 * Name of the library which generated this plugin. Set by the 207 * plugin loader. 208 */ 209 char *library_name; 210 211 212 /** 213 * Drop the Taler tables. This should only be used in testcases. 214 * 215 * @param cls the @e cls of this struct with the plugin-specific state 216 * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure 217 */ 218 enum GNUNET_GenericReturnValue 219 (*drop_tables)(void *cls); 220 221 /** 222 * Create the necessary tables if they are not present 223 * 224 * @param cls the @e cls of this struct with the plugin-specific state 225 * @param support_partitions true to enable partitioning support (disables foreign key constraints) 226 * @param num_partitions number of partitions to create, 227 * (0 to not actually use partitions, 1 to only 228 * setup a default partition, >1 for real partitions) 229 * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure 230 */ 231 enum GNUNET_GenericReturnValue 232 (*create_tables)(void *cls); 233 234 235 /** 236 * Start a transaction. 237 * 238 * @param cls the @e cls of this struct with the plugin-specific state 239 * @param name unique name identifying the transaction (for debugging), 240 * must point to a constant 241 * @return #GNUNET_OK on success 242 */ 243 enum GNUNET_GenericReturnValue 244 (*start)(void *cls, 245 const char *name); 246 247 248 /** 249 * Start a READ COMMITTED transaction. 250 * 251 * @param cls the `struct PostgresClosure` with the plugin-specific state 252 * @param name unique name identifying the transaction (for debugging) 253 * must point to a constant 254 * @return #GNUNET_OK on success 255 */ 256 enum GNUNET_GenericReturnValue 257 (*start_read_committed)(void *cls, 258 const char *name); 259 260 /** 261 * Start a READ ONLY serializable transaction. 262 * 263 * @param cls the `struct PostgresClosure` with the plugin-specific state 264 * @param name unique name identifying the transaction (for debugging) 265 * must point to a constant 266 * @return #GNUNET_OK on success 267 */ 268 enum GNUNET_GenericReturnValue 269 (*start_read_only)(void *cls, 270 const char *name); 271 272 273 /** 274 * Commit a transaction. 275 * 276 * @param cls the @e cls of this struct with the plugin-specific state 277 * @return transaction status 278 */ 279 enum GNUNET_DB_QueryStatus 280 (*commit)(void *cls); 281 282 283 /** 284 * Do a pre-flight check that we are not in an uncommitted transaction. 285 * If we are, try to commit the previous transaction and output a warning. 286 * Does not return anything, as we will continue regardless of the outcome. 287 * 288 * @param cls the `struct PostgresClosure` with the plugin-specific state 289 * @return #GNUNET_OK if everything is fine 290 * #GNUNET_NO if a transaction was rolled back 291 * #GNUNET_SYSERR on hard errors 292 */ 293 enum GNUNET_GenericReturnValue 294 (*preflight)(void *cls); 295 296 297 /** 298 * Abort/rollback a transaction. 299 * 300 * @param cls the @e cls of this struct with the plugin-specific state 301 */ 302 void 303 (*rollback) (void *cls); 304 305 306 /** 307 * Function called to perform "garbage collection" on the 308 * database, expiring records we no longer require. 309 * 310 * @param cls closure 311 * @return #GNUNET_OK on success, 312 * #GNUNET_SYSERR on DB errors 313 */ 314 enum GNUNET_GenericReturnValue 315 (*gc)(void *cls); 316 317 318 /** 319 * Register callback to be invoked on events of type @a es. 320 * 321 * @param cls database context to use 322 * @param timeout how long to wait at most 323 * @param es specification of the event to listen for 324 * @param cb function to call when the event happens, possibly 325 * multiple times (until cancel is invoked) 326 * @param cb_cls closure for @a cb 327 * @return handle useful to cancel the listener 328 */ 329 struct GNUNET_DB_EventHandler * 330 (*event_listen)(void *cls, 331 struct GNUNET_TIME_Relative timeout, 332 const struct GNUNET_DB_EventHeaderP *es, 333 GNUNET_DB_EventCallback cb, 334 void *cb_cls); 335 336 /** 337 * Stop notifications. 338 * 339 * @param cls database context to use 340 * @param eh handle to unregister. 341 */ 342 void 343 (*event_listen_cancel)(void *cls, 344 struct GNUNET_DB_EventHandler *eh); 345 346 347 /** 348 * Notify all that listen on @a es of an event. 349 * 350 * @param cls database context to use 351 * @param es specification of the event to generate 352 * @param extra additional event data provided 353 * @param extra_size number of bytes in @a extra 354 */ 355 void 356 (*event_notify)(void *cls, 357 const struct GNUNET_DB_EventHeaderP *es, 358 const void *extra, 359 size_t extra_size); 360 361 /** 362 * Get charity. 363 * 364 * @param cls closure 365 * @param charity_id 366 * @param meta 367 * @return database transaction status 368 */ 369 enum GNUNET_DB_QueryStatus 370 (*lookup_charity)( 371 void *cls, 372 uint64_t charity_id, 373 struct DONAUDB_CharityMetaData *meta); 374 375 /** 376 * Delete charity. 377 * 378 * @param cls closure 379 * @param charity_id 380 * @param[out] found set to true if the purse was found 381 * (if false, purse could not be deleted) 382 * @return database transaction status 383 */ 384 enum GNUNET_DB_QueryStatus 385 (*do_charity_delete)( 386 void *cls, 387 uint64_t charity_id); 388 389 /** 390 * Get charities. 391 * 392 * @param cls closure 393 * @param cb callback to invoke on each match 394 * @param cb_cls closure for @a cb 395 * @return database transaction status 396 */ 397 enum GNUNET_DB_QueryStatus 398 (*get_charities)( 399 void *cls, 400 DONAUDB_GetCharitiesCallback cb, 401 void *cb_cls); 402 403 404 /** 405 * Insert Charity 406 * 407 * @param cls closure 408 * @param charity_pub charity public key 409 * @param charity_name name of the charity 410 * @param charity_url Web site fo the charity 411 * @param max_per_year yearly donation limit 412 * @param[out] charity_id set to unique ID assigned to this charity, 413 * set to 0 on conflict (@a charity_pub exists, but with different data) 414 * @return database transaction status 415 */ 416 enum GNUNET_DB_QueryStatus 417 (*insert_charity)( 418 void *cls, 419 const struct DONAU_CharityPublicKeyP *charity_pub, 420 const char *charity_name, 421 const char *charity_url, 422 const struct TALER_Amount *max_per_year, 423 uint64_t *charity_id); 424 425 426 /** 427 * Update existing charity meta data. 428 * 429 * @param cls closure 430 * @param charity_id identifier of the charity to update 431 * @param charity_pub new public key 432 * @param charity_name new human-readable name 433 * @param charity_url new URL of the charity 434 * @param max_per_year new yearly donation cap 435 * @return database transaction status 436 */ 437 enum GNUNET_DB_QueryStatus 438 (*update_charity)( 439 void *cls, 440 uint64_t charity_id, 441 const struct DONAU_CharityPublicKeyP *charity_pub, 442 const char *charity_name, 443 const char *charity_url, 444 const struct TALER_Amount *max_per_year); 445 446 447 /** 448 * Iterate donation units. 449 * 450 * @param cls closure 451 * @param cb callback to invoke on each match 452 * @param cb_cls closure for @a cb 453 * @return database transaction status 454 */ 455 enum GNUNET_DB_QueryStatus 456 (*iterate_donation_units)( 457 void *cls, 458 DONAUDB_IterateDonationUnitsCallback cb, 459 void *cb_cls); 460 461 /** 462 * Get history. 463 * 464 * @param cls closure 465 * @param cb callback to invoke on each match 466 * @param cb_cls closure for @a cb 467 * @return database transaction status 468 */ 469 enum GNUNET_DB_QueryStatus 470 (*get_history)( 471 void *cls, 472 DONAUDB_GetHistoryCallback cb, 473 void *cb_cls); 474 475 /** 476 * Lookup history entry. 477 * 478 * @param cls closure 479 * @param cb callback to invoke on each match 480 * @param cb_cls closure for @a cb 481 * @return database transaction status 482 */ 483 enum GNUNET_DB_QueryStatus 484 (*lookup_history_entry)( 485 void *cls, 486 const unsigned long long charity_id, 487 const struct TALER_Amount *final_amount, 488 const uint64_t donation_year); 489 490 /** 491 * Insert donation_unit. 492 * 493 * @param cls closure 494 * @param donation_unit_pub 495 * @return database transaction status 496 */ 497 enum GNUNET_DB_QueryStatus 498 (*insert_donation_unit)( 499 void *cls, 500 const struct DONAU_DonationUnitHashP *h_donation_unit_pub, 501 const struct DONAU_DonationUnitPublicKey *donation_unit_pub, 502 const uint64_t validity_year, 503 const struct TALER_Amount *value); 504 505 /** 506 * Insert history entry of a charity 507 * 508 * @param cls closure 509 * @param charity_id charity id 510 * @param final_amount final donation amount at the end of the donation year 511 * @param donation_year year of the donations 512 * @return transaction status code 513 */ 514 enum GNUNET_DB_QueryStatus 515 (*insert_history_entry)( 516 void *cls, 517 const uint64_t charity_id, 518 const struct TALER_Amount *final_amount, 519 const uint64_t donation_year); 520 521 /** 522 * Insert issued blinded donation receipt to the charity. 523 * 524 * @param cls closure 525 * @param num_blinded_sig 526 * @param signatures blinded signatures 527 * @param charity_id identifier of the charity 528 * @param h_receipt hash of the donation receipt 529 * @param amount_receipts_request donation amount 530 * @param smaller_than_max_per_year new receipts to day smaller than the max? 531 * @return transaction status code 532 */ 533 enum GNUNET_DB_QueryStatus 534 (*insert_issued_receipt)( 535 void *cls, 536 const size_t num_blinded_sig, 537 const struct DONAU_BlindedDonationUnitSignature signatures[num_blinded_sig], 538 const uint64_t charity_id, 539 const struct DONAU_DonationReceiptHashP *h_receipt, 540 const struct TALER_Amount *amount_receipts_request, 541 bool *smaller_than_max_per_year); 542 543 /** 544 * Insert submitted donation receipt from the donor. 545 * 546 * @param cls closure 547 * @param h_donor_tax_id salted hash of the donors tax number 548 * @param nonce nonce that is part of the unique donation identifier 549 * @param donation_unit_pub donation unit public key 550 * @param donau_sig donau signature in case the sign keys changed 551 * @param donation_year year of the donation 552 * @return transaction status code 553 */ 554 enum GNUNET_DB_QueryStatus 555 (*insert_submitted_receipts)( 556 void *cls, 557 struct DONAU_HashDonorTaxId *h_donor_tax_id, 558 size_t num_dr, 559 const struct DONAU_DonationReceipt donation_receipts[static num_dr], 560 uint64_t donation_year); 561 562 /** 563 * Iterate submitted donation receipt. 564 * 565 * @param cls closure 566 * @param value 567 * @return database transaction status 568 */ 569 enum GNUNET_DB_QueryStatus 570 (*iterate_submitted_receipts)( 571 void *cls, 572 const uint64_t donation_year, 573 const struct DONAU_HashDonorTaxId *h_donor_tax_id, 574 struct TALER_Amount *total_donations); 575 576 /** 577 * Lookup amount of the donation unit hash. 578 * 579 * @param cls closure 580 * @param h_donation_unit_pub the hash over the donation unit public key 581 * @param value the amount of the donation unit 582 */ 583 enum GNUNET_DB_QueryStatus 584 (*lookup_donation_unit_amount)( 585 void *cls, 586 const struct DONAU_DonationUnitHashP *h_donation_unit_pub, 587 struct TALER_Amount *value); 588 589 /** 590 * Lookup issued receipts from the charity. 591 * 592 * @param cls closure 593 * @param h_receipts the hash over the blinded unique identifiers 594 * @param meta meta data about an issued request 595 * @return transaction status code 596 */ 597 enum GNUNET_DB_QueryStatus 598 (*lookup_issued_receipts)( 599 void *cls, 600 struct DONAU_DonationReceiptHashP *h_receitps, 601 struct DONAUDB_IssuedReceiptsMetaData *meta); 602 603 /** 604 * Add signing key. 605 * 606 * @param cls closure 607 * @param donau_pub the donau online signing public key 608 * @param meta meta data about @a donau_pub 609 * @return transaction status code 610 */ 611 enum GNUNET_DB_QueryStatus 612 (*insert_signing_key)( 613 void *cls, 614 const struct DONAU_DonauPublicKeyP *donau_pub, 615 struct DONAUDB_SignkeyMetaData *meta); 616 617 /** 618 * Lookup signing key meta data. 619 * 620 * @param cls closure 621 * @param donau_pub the donau signing public key 622 * @param[out] meta meta data about @a donau_pub 623 * @return transaction status code 624 */ 625 enum GNUNET_DB_QueryStatus 626 (*lookup_signing_key)( 627 void *cls, 628 const struct DONAU_DonauPublicKeyP *donau_pub, 629 struct DONAUDB_SignkeyMetaData *meta); 630 631 /** 632 * Iterate donau signing keys. 633 * 634 * @param cls closure 635 * @param cb callback to invoke on each match 636 * @param cb_cls closure for @a cb 637 * @return database transaction status 638 */ 639 enum GNUNET_DB_QueryStatus 640 (*iterate_active_signing_keys)( 641 void *cls, 642 DONAUDB_IterateActiveSigningKeysCallback cb, 643 void *cb_cls); 644 645 }; 646 647 #endif