| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | #ifndef BLOCKCHAIN_DB_H |
| 29 | #define BLOCKCHAIN_DB_H |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include <string> |
| 34 | #include <exception> |
| 35 | #include <boost/program_options.hpp> |
| 36 | #include "common/command_line.h" |
| 37 | #include "crypto/hash.h" |
| 38 | #include "cryptonote_basic/blobdatatype.h" |
| 39 | #include "cryptonote_basic/cryptonote_basic.h" |
| 40 | #include "cryptonote_basic/difficulty.h" |
| 41 | #include "cryptonote_basic/hardfork.h" |
| 42 | #include "cryptonote_protocol/enums.h" |
| 43 | |
| 44 | /** \file |
| 45 | * Cryptonote Blockchain Database Interface |
| 46 | * |
| 47 | * The DB interface is a store for the canonical block chain. |
| 48 | * It serves as a persistent storage for the blockchain. |
| 49 | * |
| 50 | * For the sake of efficiency, a concrete implementation may also |
| 51 | * store some blockchain data outside of the blocks, such as spent |
| 52 | * transfer key images, unspent transaction outputs, etc. |
| 53 | * |
| 54 | * Examples are as follows: |
| 55 | * |
| 56 | * Transactions are duplicated so that we don't have to fetch a whole block |
| 57 | * in order to fetch a transaction from that block. |
| 58 | * |
| 59 | * Spent key images are duplicated outside of the blocks so it is quick |
| 60 | * to verify an output hasn't already been spent |
| 61 | * |
| 62 | * Unspent transaction outputs are duplicated to quickly gather random |
| 63 | * outputs to use for mixins |
| 64 | * |
| 65 | * Indices and Identifiers: |
| 66 | * The word "index" is used ambiguously throughout this code. It is |
| 67 | * particularly confusing when talking about the output or transaction |
| 68 | * tables since their indexing can refer to themselves or each other. |
| 69 | * I have attempted to clarify these usages here: |
| 70 | * |
| 71 | * Blocks, transactions, and outputs are all identified by a hash. |
| 72 | * For storage efficiency, a 64-bit integer ID is used instead of the hash |
| 73 | * inside the DB. Tables exist to map between hash and ID. A block ID is |
| 74 | * also referred to as its "height". Transactions and outputs generally are |
| 75 | * not referred to by ID outside of this module, but the tx ID is returned |
| 76 | * by tx_exists() and used by get_tx_amount_output_indices(). Like their |
| 77 | * corresponding hashes, IDs are globally unique. |
| 78 | * |
| 79 | * The remaining uses of the word "index" refer to local offsets, and are |
| 80 | * not globally unique. An "amount output index" N refers to the Nth output |
| 81 | * of a specific amount. An "output local index" N refers to the Nth output |
| 82 | * of a specific tx. |
| 83 | * |
| 84 | * Exceptions: |
| 85 | * DB_ERROR -- generic |
| 86 | * DB_OPEN_FAILURE |
| 87 | * DB_CREATE_FAILURE |
| 88 | * DB_SYNC_FAILURE |
| 89 | * BLOCK_DNE |
| 90 | * BLOCK_PARENT_DNE |
| 91 | * BLOCK_EXISTS |
| 92 | * BLOCK_INVALID -- considering making this multiple errors |
| 93 | * TX_DNE |
| 94 | * TX_EXISTS |
| 95 | * OUTPUT_DNE |
| 96 | * OUTPUT_EXISTS |
| 97 | * KEY_IMAGE_EXISTS |
| 98 | */ |
| 99 | |
| 100 | namespace cryptonote |
| 101 | { |
| 102 | |
| 103 | /** a pair of <transaction hash, output index>, typedef for convenience */ |
| 104 | typedef std::pair<crypto::hash, uint64_t> tx_out_index; |
| 105 | |
| 106 | extern const command_line::arg_descriptor<std::string> arg_db_sync_mode; |
| 107 | extern const command_line::arg_descriptor<bool, false> arg_db_salvage; |
| 108 | |
| 109 | enum class relay_category : uint8_t |
| 110 | { |
| 111 | broadcasted = 0,//!< Public txes received via block/fluff |
| 112 | relayable, //!< Every tx not marked `relay_method::none` |
| 113 | legacy, //!< `relay_category::broadcasted` + `relay_method::none` for rpc relay requests or historical reasons |
| 114 | all //!< Everything in the db |
| 115 | }; |
| 116 | |
| 117 | bool matches_category(relay_method method, relay_category category) noexcept; |
| 118 | |
| 119 | #pragma pack(push, 1) |
| 120 | |
| 121 | /** |
| 122 | * @brief a struct containing output metadata |
| 123 | */ |
| 124 | struct output_data_t |
| 125 | { |
| 126 | crypto::public_key pubkey; //!< the output's public key (for spend verification) |
| 127 | uint64_t unlock_time; //!< the output's unlock time (or height) |
| 128 | uint64_t height; //!< the height of the block which created the output |
| 129 | rct::key commitment; //!< the output's amount commitment (for spend verification) |
| 130 | }; |
| 131 | #pragma pack(pop) |
| 132 | |
| 133 | #pragma pack(push, 1) |
| 134 | struct tx_data_t |
| 135 | { |
| 136 | uint64_t tx_id; |
| 137 | uint64_t unlock_time; |
| 138 | uint64_t block_id; |
| 139 | }; |
| 140 | #pragma pack(pop) |
| 141 | |
| 142 | struct alt_block_data_t |
| 143 | { |
| 144 | uint64_t height; |
| 145 | uint64_t cumulative_weight; |
| 146 | uint64_t cumulative_difficulty_low; |
| 147 | uint64_t cumulative_difficulty_high; |
| 148 | uint64_t already_generated_coins; |
| 149 | }; |
| 150 | |
| 151 | /** |
| 152 | * @brief a struct containing txpool per transaction metadata |
| 153 | */ |
| 154 | struct txpool_tx_meta_t |
| 155 | { |
| 156 | crypto::hash max_used_block_id; |
| 157 | crypto::hash last_failed_id; |
| 158 | uint64_t weight; |
| 159 | uint64_t fee; |
| 160 | uint64_t max_used_block_height; |
| 161 | uint64_t last_failed_height; |
| 162 | uint64_t receive_time; |
| 163 | uint64_t last_relayed_time; //!< If received over i2p/tor, randomized forward time. If Dandelion++stem, randomized embargo time. Otherwise, last relayed timestamp |
| 164 | // 112 bytes |
| 165 | uint8_t kept_by_block; |
| 166 | uint8_t relayed; |
| 167 | uint8_t do_not_relay; |
| 168 | uint8_t double_spend_seen: 1; |
| 169 | uint8_t pruned: 1; |
| 170 | uint8_t is_local: 1; |
| 171 | uint8_t dandelionpp_stem : 1; |
| 172 | uint8_t is_forwarding: 1; |
| 173 | uint8_t bf_padding: 3; |
| 174 | |
| 175 | uint8_t padding[76]; // till 192 bytes |
| 176 | |
| 177 | void set_relay_method(relay_method method) noexcept; |
| 178 | relay_method get_relay_method() const noexcept; |
| 179 | |
| 180 | //! \return True if `get_relay_method()` now returns `method`. |
| 181 | bool upgrade_relay_method(relay_method method) noexcept; |
| 182 | |
| 183 | //! See `relay_category` description |
| 184 | bool matches(const relay_category category) const noexcept |
| 185 | { |
| 186 | return matches_category(get_relay_method(), category); |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | |
| 191 | #define DBF_SAFE 1 |
| 192 | #define DBF_FAST 2 |
| 193 | #define DBF_FASTEST 4 |
| 194 | #define DBF_RDONLY 8 |
| 195 | #define DBF_SALVAGE 0x10 |
| 196 | |
| 197 | /*********************************** |
| 198 | * Exception Definitions |
| 199 | ***********************************/ |
| 200 | |
| 201 | /** |
| 202 | * @brief A base class for BlockchainDB exceptions |
| 203 | */ |
| 204 | class DB_EXCEPTION : public std::exception |
| 205 | { |
| 206 | private: |
| 207 | std::string m; |
| 208 | |
| 209 | protected: |
| 210 | DB_EXCEPTION(const char *s) : m(s) { } |
| 211 | |
| 212 | public: |
| 213 | virtual ~DB_EXCEPTION() { } |
| 214 | |
| 215 | const char* what() const throw() |
| 216 | { |
| 217 | return m.c_str(); |
| 218 | } |
| 219 | }; |
| 220 | |
| 221 | /** |
| 222 | * @brief A generic BlockchainDB exception |
| 223 | */ |
| 224 | class DB_ERROR : public DB_EXCEPTION |
| 225 | { |
| 226 | public: |
| 227 | DB_ERROR() : DB_EXCEPTION("Generic DB Error") { } |
| 228 | DB_ERROR(const char* s) : DB_EXCEPTION(s) { } |
| 229 | }; |
| 230 | |
| 231 | /** |
| 232 | * @brief thrown when there is an error starting a DB transaction |
| 233 | */ |
| 234 | class DB_ERROR_TXN_START : public DB_EXCEPTION |
| 235 | { |
| 236 | public: |
| 237 | DB_ERROR_TXN_START() : DB_EXCEPTION("DB Error in starting txn") { } |
| 238 | DB_ERROR_TXN_START(const char* s) : DB_EXCEPTION(s) { } |
| 239 | }; |
| 240 | |
| 241 | /** |
| 242 | * @brief thrown when opening the BlockchainDB fails |
| 243 | */ |
| 244 | class DB_OPEN_FAILURE : public DB_EXCEPTION |
| 245 | { |
| 246 | public: |
| 247 | DB_OPEN_FAILURE() : DB_EXCEPTION("Failed to open the db") { } |
| 248 | DB_OPEN_FAILURE(const char* s) : DB_EXCEPTION(s) { } |
| 249 | }; |
| 250 | |
| 251 | /** |
| 252 | * @brief thrown when creating the BlockchainDB fails |
| 253 | */ |
| 254 | class DB_CREATE_FAILURE : public DB_EXCEPTION |
| 255 | { |
| 256 | public: |
| 257 | DB_CREATE_FAILURE() : DB_EXCEPTION("Failed to create the db") { } |
| 258 | DB_CREATE_FAILURE(const char* s) : DB_EXCEPTION(s) { } |
| 259 | }; |
| 260 | |
| 261 | /** |
| 262 | * @brief thrown when synchronizing the BlockchainDB to disk fails |
| 263 | */ |
| 264 | class DB_SYNC_FAILURE : public DB_EXCEPTION |
| 265 | { |
| 266 | public: |
| 267 | DB_SYNC_FAILURE() : DB_EXCEPTION("Failed to sync the db") { } |
| 268 | DB_SYNC_FAILURE(const char* s) : DB_EXCEPTION(s) { } |
| 269 | }; |
| 270 | |
| 271 | /** |
| 272 | * @brief thrown when a requested block does not exist |
| 273 | */ |
| 274 | class BLOCK_DNE : public DB_EXCEPTION |
| 275 | { |
| 276 | public: |
| 277 | BLOCK_DNE() : DB_EXCEPTION("The block requested does not exist") { } |
| 278 | BLOCK_DNE(const char* s) : DB_EXCEPTION(s) { } |
| 279 | }; |
| 280 | |
| 281 | /** |
| 282 | * @brief thrown when a block's parent does not exist (and it needed to) |
| 283 | */ |
| 284 | class BLOCK_PARENT_DNE : public DB_EXCEPTION |
| 285 | { |
| 286 | public: |
| 287 | BLOCK_PARENT_DNE() : DB_EXCEPTION("The parent of the block does not exist") { } |
| 288 | BLOCK_PARENT_DNE(const char* s) : DB_EXCEPTION(s) { } |
| 289 | }; |
| 290 | |
| 291 | /** |
| 292 | * @brief thrown when a block exists, but shouldn't, namely when adding a block |
| 293 | */ |
| 294 | class BLOCK_EXISTS : public DB_EXCEPTION |
| 295 | { |
| 296 | public: |
| 297 | BLOCK_EXISTS() : DB_EXCEPTION("The block to be added already exists!") { } |
| 298 | BLOCK_EXISTS(const char* s) : DB_EXCEPTION(s) { } |
| 299 | }; |
| 300 | |
| 301 | /** |
| 302 | * @brief thrown when something is wrong with the block to be added |
| 303 | */ |
| 304 | class BLOCK_INVALID : public DB_EXCEPTION |
| 305 | { |
| 306 | public: |
| 307 | BLOCK_INVALID() : DB_EXCEPTION("The block to be added did not pass validation!") { } |
| 308 | BLOCK_INVALID(const char* s) : DB_EXCEPTION(s) { } |
| 309 | }; |
| 310 | |
| 311 | /** |
| 312 | * @brief thrown when a requested transaction does not exist |
| 313 | */ |
| 314 | class TX_DNE : public DB_EXCEPTION |
| 315 | { |
| 316 | public: |
| 317 | TX_DNE() : DB_EXCEPTION("The transaction requested does not exist") { } |
| 318 | TX_DNE(const char* s) : DB_EXCEPTION(s) { } |
| 319 | }; |
| 320 | |
| 321 | /** |
| 322 | * @brief thrown when a transaction exists, but shouldn't, namely when adding a block |
| 323 | */ |
| 324 | class TX_EXISTS : public DB_EXCEPTION |
| 325 | { |
| 326 | public: |
| 327 | TX_EXISTS() : DB_EXCEPTION("The transaction to be added already exists!") { } |
| 328 | TX_EXISTS(const char* s) : DB_EXCEPTION(s) { } |
| 329 | }; |
| 330 | |
| 331 | /** |
| 332 | * @brief thrown when a requested output does not exist |
| 333 | */ |
| 334 | class OUTPUT_DNE : public DB_EXCEPTION |
| 335 | { |
| 336 | public: |
| 337 | OUTPUT_DNE() : DB_EXCEPTION("The output requested does not exist!") { } |
| 338 | OUTPUT_DNE(const char* s) : DB_EXCEPTION(s) { } |
| 339 | }; |
| 340 | |
| 341 | /** |
| 342 | * @brief thrown when an output exists, but shouldn't, namely when adding a block |
| 343 | */ |
| 344 | class OUTPUT_EXISTS : public DB_EXCEPTION |
| 345 | { |
| 346 | public: |
| 347 | OUTPUT_EXISTS() : DB_EXCEPTION("The output to be added already exists!") { } |
| 348 | OUTPUT_EXISTS(const char* s) : DB_EXCEPTION(s) { } |
| 349 | }; |
| 350 | |
| 351 | /** |
| 352 | * @brief thrown when a spent key image exists, but shouldn't, namely when adding a block |
| 353 | */ |
| 354 | class KEY_IMAGE_EXISTS : public DB_EXCEPTION |
| 355 | { |
| 356 | public: |
| 357 | KEY_IMAGE_EXISTS() : DB_EXCEPTION("The spent key image to be added already exists!") { } |
| 358 | KEY_IMAGE_EXISTS(const char* s) : DB_EXCEPTION(s) { } |
| 359 | }; |
| 360 | |
| 361 | /*********************************** |
| 362 | * End of Exception Definitions |
| 363 | ***********************************/ |
| 364 | |
| 365 | |
| 366 | /** |
| 367 | * @brief The BlockchainDB backing store interface declaration/contract |
| 368 | * |
| 369 | * This class provides a uniform interface for using BlockchainDB to store |
| 370 | * a blockchain. Any implementation of this class will also implement all |
| 371 | * functions exposed here, so one can use this class without knowing what |
| 372 | * implementation is being used. Refer to each pure virtual function's |
| 373 | * documentation here when implementing a BlockchainDB subclass. |
| 374 | * |
| 375 | * A subclass which encounters an issue should report that issue by throwing |
| 376 | * a DB_EXCEPTION which adequately conveys the issue. |
| 377 | */ |
| 378 | class BlockchainDB |
| 379 | { |
| 380 | private: |
| 381 | /********************************************************************* |
| 382 | * private virtual members |
| 383 | *********************************************************************/ |
| 384 | |
| 385 | /** |
| 386 | * @brief add the block and metadata to the db |
| 387 | * |
| 388 | * The subclass implementing this will add the specified block and |
| 389 | * block metadata to its backing store. This does not include its |
| 390 | * transactions, those are added in a separate step. |
| 391 | * |
| 392 | * If any of this cannot be done, the subclass should throw the corresponding |
| 393 | * subclass of DB_EXCEPTION |
| 394 | * |
| 395 | * @param blk the block to be added |
| 396 | * @param block_weight the weight of the block (transactions and all) |
| 397 | * @param long_term_block_weight the long term block weight of the block (transactions and all) |
| 398 | * @param cumulative_difficulty the accumulated difficulty after this block |
| 399 | * @param coins_generated the number of coins generated total after this block |
| 400 | * @param blk_hash the hash of the block |
| 401 | */ |
| 402 | virtual void add_block( const block& blk |
| 403 | , size_t block_weight |
| 404 | , uint64_t long_term_block_weight |
| 405 | , const difficulty_type& cumulative_difficulty |
| 406 | , const uint64_t& coins_generated |
| 407 | , uint64_t num_rct_outs |
| 408 | , const crypto::hash& blk_hash |
| 409 | ) = 0; |
| 410 | |
| 411 | /** |
| 412 | * @brief remove data about the top block |
| 413 | * |
| 414 | * The subclass implementing this will remove the block data from the top |
| 415 | * block in the chain. The data to be removed is that which was added in |
| 416 | * BlockchainDB::add_block(const block& blk, size_t block_weight, uint64_t long_term_block_weight, const difficulty_type& cumulative_difficulty, const uint64_t& coins_generated, const crypto::hash& blk_hash) |
| 417 | * |
| 418 | * If any of this cannot be done, the subclass should throw the corresponding |
| 419 | * subclass of DB_EXCEPTION |
| 420 | */ |
| 421 | virtual void remove_block() = 0; |
| 422 | |
| 423 | /** |
| 424 | * @brief store the transaction and its metadata |
| 425 | * |
| 426 | * The subclass implementing this will add the specified transaction data |
| 427 | * to its backing store. This includes only the transaction blob itself |
| 428 | * and the other data passed here, not the separate outputs of the |
| 429 | * transaction. |
| 430 | * |
| 431 | * It returns a tx ID, which is a mapping from the tx_hash. The tx ID |
| 432 | * is used in #add_tx_amount_output_indices(). |
| 433 | * |
| 434 | * If any of this cannot be done, the subclass should throw the corresponding |
| 435 | * subclass of DB_EXCEPTION |
| 436 | * |
| 437 | * @param blk_hash the hash of the block containing the transaction |
| 438 | * @param tx the transaction to be added |
| 439 | * @param tx_hash the hash of the transaction |
| 440 | * @param tx_prunable_hash the hash of the prunable part of the transaction |
| 441 | * @return the transaction ID |
| 442 | */ |
| 443 | virtual uint64_t add_transaction_data(const crypto::hash& blk_hash, const std::pair<transaction, blobdata_ref>& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prunable_hash) = 0; |
| 444 | |
| 445 | /** |
| 446 | * @brief remove data about a transaction |
| 447 | * |
| 448 | * The subclass implementing this will remove the transaction data |
| 449 | * for the passed transaction. The data to be removed was added in |
| 450 | * add_transaction_data(). Additionally, current subclasses have behavior |
| 451 | * which requires the transaction itself as a parameter here. Future |
| 452 | * implementations should note that this parameter is subject to be removed |
| 453 | * at a later time. |
| 454 | * |
| 455 | * If any of this cannot be done, the subclass should throw the corresponding |
| 456 | * subclass of DB_EXCEPTION |
| 457 | * |
| 458 | * @param tx_hash the hash of the transaction to be removed |
| 459 | * @param tx the transaction |
| 460 | */ |
| 461 | virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx) = 0; |
| 462 | |
| 463 | /** |
| 464 | * @brief store an output |
| 465 | * |
| 466 | * The subclass implementing this will add the output data passed to its |
| 467 | * backing store in a suitable manner. In addition, the subclass is responsible |
| 468 | * for keeping track of the global output count in some manner, so that |
| 469 | * outputs may be indexed by the order in which they were created. In the |
| 470 | * future, this tracking (of the number, at least) should be moved to |
| 471 | * this class, as it is necessary and the same among all BlockchainDB. |
| 472 | * |
| 473 | * It returns an amount output index, which is the index of the output |
| 474 | * for its specified amount. |
| 475 | * |
| 476 | * This data should be stored in such a manner that the only thing needed to |
| 477 | * reverse the process is the tx_out. |
| 478 | * |
| 479 | * If any of this cannot be done, the subclass should throw the corresponding |
| 480 | * subclass of DB_EXCEPTION |
| 481 | * |
| 482 | * @param tx_hash hash of the transaction the output was created by |
| 483 | * @param tx_output the output |
| 484 | * @param local_index index of the output in its transaction |
| 485 | * @param unlock_time unlock time/height of the output |
| 486 | * @param commitment the rct commitment to the output amount |
| 487 | * @return amount output index |
| 488 | */ |
| 489 | virtual uint64_t add_output(const crypto::hash& tx_hash, const tx_out& tx_output, const uint64_t& local_index, const uint64_t unlock_time, const rct::key *commitment) = 0; |
| 490 | |
| 491 | /** |
| 492 | * @brief store amount output indices for a tx's outputs |
| 493 | * |
| 494 | * The subclass implementing this will add the amount output indices to its |
| 495 | * backing store in a suitable manner. The tx_id will be the same one that |
| 496 | * was returned from #add_output(). |
| 497 | * |
| 498 | * If any of this cannot be done, the subclass should throw the corresponding |
| 499 | * subclass of DB_EXCEPTION |
| 500 | * |
| 501 | * @param tx_id ID of the transaction containing these outputs |
| 502 | * @param amount_output_indices the amount output indices of the transaction |
| 503 | */ |
| 504 | virtual void add_tx_amount_output_indices(const uint64_t tx_id, const std::vector<uint64_t>& amount_output_indices) = 0; |
| 505 | |
| 506 | /** |
| 507 | * @brief store a spent key |
| 508 | * |
| 509 | * The subclass implementing this will store the spent key image. |
| 510 | * |
| 511 | * If any of this cannot be done, the subclass should throw the corresponding |
| 512 | * subclass of DB_EXCEPTION |
| 513 | * |
| 514 | * @param k_image the spent key image to store |
| 515 | */ |
| 516 | virtual void add_spent_key(const crypto::key_image& k_image) = 0; |
| 517 | |
| 518 | /** |
| 519 | * @brief remove a spent key |
| 520 | * |
| 521 | * The subclass implementing this will remove the key image. |
| 522 | * |
| 523 | * If any of this cannot be done, the subclass should throw the corresponding |
| 524 | * subclass of DB_EXCEPTION |
| 525 | * |
| 526 | * @param k_image the spent key image to remove |
| 527 | */ |
| 528 | virtual void remove_spent_key(const crypto::key_image& k_image) = 0; |
| 529 | |
| 530 | |
| 531 | /********************************************************************* |
| 532 | * private concrete members |
| 533 | *********************************************************************/ |
| 534 | /** |
| 535 | * @brief private version of pop_block, for undoing if an add_block fails |
| 536 | * |
| 537 | * This function simply calls pop_block(block& blk, std::vector<transaction>& txs) |
| 538 | * with dummy parameters, as the returns-by-reference can be discarded. |
| 539 | */ |
| 540 | void pop_block(); |
| 541 | |
| 542 | // helper function to remove transaction from blockchain |
| 543 | /** |
| 544 | * @brief helper function to remove transaction from the blockchain |
| 545 | * |
| 546 | * This function encapsulates aspects of removing a transaction. |
| 547 | * |
| 548 | * @param tx_hash the hash of the transaction to be removed |
| 549 | */ |
| 550 | void remove_transaction(const crypto::hash& tx_hash); |
| 551 | |
| 552 | uint64_t num_calls = 0; //!< a performance metric |
| 553 | uint64_t time_blk_hash = 0; //!< a performance metric |
| 554 | uint64_t time_add_block1 = 0; //!< a performance metric |
| 555 | uint64_t time_add_transaction = 0; //!< a performance metric |
| 556 | |
| 557 | |
| 558 | protected: |
| 559 | |
| 560 | /** |
| 561 | * @brief helper function for add_transactions, to add each individual transaction |
| 562 | * |
| 563 | * This function is called by add_transactions() for each transaction to be |
| 564 | * added. |
| 565 | * |
| 566 | * @param blk_hash hash of the block which has the transaction |
| 567 | * @param tx the transaction to add |
| 568 | * @param tx_hash_ptr the hash of the transaction, if already calculated |
| 569 | * @param tx_prunable_hash_ptr the hash of the prunable part of the transaction, if already calculated |
| 570 | */ |
| 571 | void add_transaction(const crypto::hash& blk_hash, const std::pair<transaction, blobdata_ref>& tx, const crypto::hash* tx_hash_ptr = NULL, const crypto::hash* tx_prunable_hash_ptr = NULL); |
| 572 | |
| 573 | mutable uint64_t time_tx_exists = 0; //!< a performance metric |
| 574 | uint64_t time_commit1 = 0; //!< a performance metric |
| 575 | bool m_auto_remove_logs = true; //!< whether or not to automatically remove old logs |
| 576 | |
| 577 | HardFork* m_hardfork; |
| 578 | |
| 579 | public: |
| 580 | |
| 581 | /** |
| 582 | * @brief An empty constructor. |
| 583 | */ |
| 584 | BlockchainDB(): m_hardfork(NULL), m_open(false) { } |
| 585 | |
| 586 | /** |
| 587 | * @brief An empty destructor. |
| 588 | */ |
| 589 | virtual ~BlockchainDB() { }; |
| 590 | |
| 591 | /** |
| 592 | * @brief init command line options |
| 593 | */ |
| 594 | static void init_options(boost::program_options::options_description& desc); |
| 595 | |
| 596 | /** |
| 597 | * @brief reset profiling stats |
| 598 | */ |
| 599 | void reset_stats(); |
| 600 | |
| 601 | /** |
| 602 | * @brief show profiling stats |
| 603 | * |
| 604 | * This function prints current performance/profiling data to whichever |
| 605 | * log file(s) are set up (possibly including stdout or stderr) |
| 606 | */ |
| 607 | void show_stats(); |
| 608 | |
| 609 | /** |
| 610 | * @brief open a db, or create it if necessary. |
| 611 | * |
| 612 | * This function opens an existing database or creates it if it |
| 613 | * does not exist. |
| 614 | * |
| 615 | * The subclass implementing this will handle all file opening/creation, |
| 616 | * and is responsible for maintaining its state. |
| 617 | * |
| 618 | * The parameter <filename> may not refer to a file name, necessarily, but |
| 619 | * could be an IP:PORT for a database which needs it, and so on. Calling it |
| 620 | * <filename> is convenient and should be descriptive enough, however. |
| 621 | * |
| 622 | * For now, db_flags are |
| 623 | * specific to the subclass being instantiated. This is subject to change, |
| 624 | * and the db_flags parameter may be deprecated. |
| 625 | * |
| 626 | * If any of this cannot be done, the subclass should throw the corresponding |
| 627 | * subclass of DB_EXCEPTION |
| 628 | * |
| 629 | * @param filename a string referring to the BlockchainDB to open |
| 630 | * @param db_flags flags relevant to how to open/use the BlockchainDB |
| 631 | */ |
| 632 | virtual void open(const std::string& filename, const int db_flags = 0) = 0; |
| 633 | |
| 634 | /** |
| 635 | * @brief Gets the current open/ready state of the BlockchainDB |
| 636 | * |
| 637 | * @return true if open/ready, otherwise false |
| 638 | */ |
| 639 | bool is_open() const; |
| 640 | |
| 641 | /** |
| 642 | * @brief close the BlockchainDB |
| 643 | * |
| 644 | * At minimum, this call ensures that further use of the BlockchainDB |
| 645 | * instance will not have effect. In any case where it is necessary |
| 646 | * to do so, a subclass implementing this will sync with disk. |
| 647 | * |
| 648 | * If any of this cannot be done, the subclass should throw the corresponding |
| 649 | * subclass of DB_EXCEPTION |
| 650 | */ |
| 651 | virtual void close() = 0; |
| 652 | |
| 653 | /** |
| 654 | * @brief sync the BlockchainDB with disk |
| 655 | * |
| 656 | * This function should write any changes to whatever permanent backing |
| 657 | * store the subclass uses. Example: a BlockchainDB instance which |
| 658 | * keeps the whole blockchain in RAM won't need to regularly access a |
| 659 | * disk, but should write out its state when this is called. |
| 660 | * |
| 661 | * If any of this cannot be done, the subclass should throw the corresponding |
| 662 | * subclass of DB_EXCEPTION |
| 663 | */ |
| 664 | virtual void sync() = 0; |
| 665 | |
| 666 | /** |
| 667 | * @brief toggle safe syncs for the DB |
| 668 | * |
| 669 | * Used to switch DBF_SAFE on or off after starting up with DBF_FAST. |
| 670 | */ |
| 671 | virtual void safesyncmode(const bool onoff) = 0; |
| 672 | |
| 673 | /** |
| 674 | * @brief Remove everything from the BlockchainDB |
| 675 | * |
| 676 | * This function should completely remove all data from a BlockchainDB. |
| 677 | * |
| 678 | * Use with caution! |
| 679 | * |
| 680 | * If any of this cannot be done, the subclass should throw the corresponding |
| 681 | * subclass of DB_EXCEPTION |
| 682 | */ |
| 683 | virtual void reset() = 0; |
| 684 | |
| 685 | /** |
| 686 | * @brief get all files used by the BlockchainDB (if any) |
| 687 | * |
| 688 | * This function is largely for ease of automation, namely for unit tests. |
| 689 | * |
| 690 | * The subclass implementation should return all filenames it uses. |
| 691 | * |
| 692 | * @return a list of filenames |
| 693 | */ |
| 694 | virtual std::vector<std::string> get_filenames() const = 0; |
| 695 | |
| 696 | /** |
| 697 | * @brief remove file(s) storing the database |
| 698 | * |
| 699 | * This function is for resetting the database (for core tests, functional tests, etc). |
| 700 | * The function reset() is not usable because it needs to open the database file first |
| 701 | * which can fail if the existing database file is in an incompatible format. |
| 702 | * As such, this function needs to be called before calling open(). |
| 703 | * |
| 704 | * @param folder The path of the folder containing the database file(s) which must not end with slash '/'. |
| 705 | * |
| 706 | * @return true if the operation is succesfull |
| 707 | */ |
| 708 | virtual bool remove_data_file(const std::string& folder) const = 0; |
| 709 | |
| 710 | // return the name of the folder the db's file(s) should reside in |
| 711 | /** |
| 712 | * @brief gets the name of the folder the BlockchainDB's file(s) should be in |
| 713 | * |
| 714 | * The subclass implementation should return the name of the folder in which |
| 715 | * it stores files, or an empty string if there is none. |
| 716 | * |
| 717 | * @return the name of the folder with the BlockchainDB's files, if any. |
| 718 | */ |
| 719 | virtual std::string get_db_name() const = 0; |
| 720 | |
| 721 | |
| 722 | // FIXME: these are just for functionality mocking, need to implement |
| 723 | // RAII-friendly and multi-read one-write friendly locking mechanism |
| 724 | // |
| 725 | // acquire db lock |
| 726 | /** |
| 727 | * @brief acquires the BlockchainDB lock |
| 728 | * |
| 729 | * This function is a stub until such a time as locking is implemented at |
| 730 | * this level. |
| 731 | * |
| 732 | * The subclass implementation should return true unless implementing a |
| 733 | * locking scheme of some sort, in which case it should return true upon |
| 734 | * acquisition of the lock and block until then. |
| 735 | * |
| 736 | * If any of this cannot be done, the subclass should throw the corresponding |
| 737 | * subclass of DB_EXCEPTION |
| 738 | * |
| 739 | * @return true, unless at a future time false makes sense (timeout, etc) |
| 740 | */ |
| 741 | virtual bool lock() = 0; |
| 742 | |
| 743 | // release db lock |
| 744 | /** |
| 745 | * @brief This function releases the BlockchainDB lock |
| 746 | * |
| 747 | * The subclass, should it have implemented lock(), will release any lock |
| 748 | * held by the calling thread. In the case of recursive locking, it should |
| 749 | * release one instance of a lock. |
| 750 | * |
| 751 | * If any of this cannot be done, the subclass should throw the corresponding |
| 752 | * subclass of DB_EXCEPTION |
| 753 | */ |
| 754 | virtual void unlock() = 0; |
| 755 | |
| 756 | /** |
| 757 | * @brief tells the BlockchainDB to start a new "batch" of blocks |
| 758 | * |
| 759 | * If the subclass implements a batching method of caching blocks in RAM to |
| 760 | * be added to a backing store in groups, it should start a batch which will |
| 761 | * end either when <batch_num_blocks> has been added or batch_stop() has |
| 762 | * been called. In either case, it should end the batch and write to its |
| 763 | * backing store. |
| 764 | * |
| 765 | * If a batch is already in-progress, this function must return false. |
| 766 | * If a batch was started by this call, it must return true. |
| 767 | * |
| 768 | * If any of this cannot be done, the subclass should throw the corresponding |
| 769 | * subclass of DB_EXCEPTION |
| 770 | * |
| 771 | * @param batch_num_blocks number of blocks to batch together |
| 772 | * |
| 773 | * @return true if we started the batch, false if already started |
| 774 | */ |
| 775 | virtual bool batch_start(uint64_t batch_num_blocks=0, uint64_t batch_bytes=0) = 0; |
| 776 | |
| 777 | /** |
| 778 | * @brief ends a batch transaction |
| 779 | * |
| 780 | * If the subclass implements batching, this function should store the |
| 781 | * batch it is currently on and mark it finished. |
| 782 | * |
| 783 | * If no batch is in-progress, this function should throw a DB_ERROR. |
| 784 | * This exception may change in the future if it is deemed necessary to |
| 785 | * have a more granular exception type for this scenario. |
| 786 | * |
| 787 | * If any of this cannot be done, the subclass should throw the corresponding |
| 788 | * subclass of DB_EXCEPTION |
| 789 | */ |
| 790 | virtual void batch_stop() = 0; |
| 791 | |
| 792 | /** |
| 793 | * @brief aborts a batch transaction |
| 794 | * |
| 795 | * If the subclass implements batching, this function should abort the |
| 796 | * batch it is currently on. |
| 797 | * |
| 798 | * If no batch is in-progress, this function should throw a DB_ERROR. |
| 799 | * This exception may change in the future if it is deemed necessary to |
| 800 | * have a more granular exception type for this scenario. |
| 801 | * |
| 802 | * If any of this cannot be done, the subclass should throw the corresponding |
| 803 | * subclass of DB_EXCEPTION |
| 804 | */ |
| 805 | virtual void batch_abort() = 0; |
| 806 | |
| 807 | /** |
| 808 | * @brief sets whether or not to batch transactions |
| 809 | * |
| 810 | * If the subclass implements batching, this function tells it to begin |
| 811 | * batching automatically. |
| 812 | * |
| 813 | * If the subclass implements batching and has a batch in-progress, a |
| 814 | * parameter of false should disable batching and call batch_stop() to |
| 815 | * store the current batch. |
| 816 | * |
| 817 | * If any of this cannot be done, the subclass should throw the corresponding |
| 818 | * subclass of DB_EXCEPTION |
| 819 | * |
| 820 | * @param bool batch whether or not to use batch transactions. |
| 821 | */ |
| 822 | virtual void set_batch_transactions(bool) = 0; |
| 823 | |
| 824 | virtual void block_wtxn_start() = 0; |
| 825 | virtual void block_wtxn_stop() = 0; |
| 826 | virtual void block_wtxn_abort() = 0; |
| 827 | virtual bool block_rtxn_start() const = 0; |
| 828 | virtual void block_rtxn_stop() const = 0; |
| 829 | virtual void block_rtxn_abort() const = 0; |
| 830 | |
| 831 | virtual void set_hard_fork(HardFork* hf); |
| 832 | |
| 833 | // adds a block with the given metadata to the top of the blockchain, returns the new height |
| 834 | /** |
| 835 | * @brief handles the addition of a new block to BlockchainDB |
| 836 | * |
| 837 | * This function organizes block addition and calls various functions as |
| 838 | * necessary. |
| 839 | * |
| 840 | * NOTE: subclass implementations of this (or the functions it calls) need |
| 841 | * to handle undoing any partially-added blocks in the event of a failure. |
| 842 | * |
| 843 | * If any of this cannot be done, the subclass should throw the corresponding |
| 844 | * subclass of DB_EXCEPTION |
| 845 | * |
| 846 | * @param blk the block to be added |
| 847 | * @param block_weight the size of the block (transactions and all) |
| 848 | * @param long_term_block_weight the long term weight of the block (transactions and all) |
| 849 | * @param cumulative_difficulty the accumulated difficulty after this block |
| 850 | * @param coins_generated the number of coins generated total after this block |
| 851 | * @param txs the transactions in the block |
| 852 | * |
| 853 | * @return the height of the chain post-addition |
| 854 | */ |
| 855 | virtual uint64_t add_block( const std::pair<block, blobdata>& blk |
| 856 | , size_t block_weight |
| 857 | , uint64_t long_term_block_weight |
| 858 | , const difficulty_type& cumulative_difficulty |
| 859 | , const uint64_t& coins_generated |
| 860 | , const std::vector<std::pair<transaction, blobdata>>& txs |
| 861 | ); |
| 862 | |
| 863 | /** |
| 864 | * @brief checks if a block exists |
| 865 | * |
| 866 | * @param h the hash of the requested block |
| 867 | * @param height if non NULL, returns the block's height if found |
| 868 | * |
| 869 | * @return true of the block exists, otherwise false |
| 870 | */ |
| 871 | virtual bool block_exists(const crypto::hash& h, uint64_t *height = NULL) const = 0; |
| 872 | |
| 873 | /** |
| 874 | * @brief fetches the block with the given hash |
| 875 | * |
| 876 | * The subclass should return the requested block. |
| 877 | * |
| 878 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 879 | * |
| 880 | * @param h the hash to look for |
| 881 | * |
| 882 | * @return the block requested |
| 883 | */ |
| 884 | virtual cryptonote::blobdata get_block_blob(const crypto::hash& h) const = 0; |
| 885 | |
| 886 | /** |
| 887 | * @brief fetches the block with the given hash |
| 888 | * |
| 889 | * Returns the requested block. |
| 890 | * |
| 891 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 892 | * |
| 893 | * @param h the hash to look for |
| 894 | * |
| 895 | * @return the block requested |
| 896 | */ |
| 897 | virtual block get_block(const crypto::hash& h) const; |
| 898 | |
| 899 | /** |
| 900 | * @brief gets the height of the block with a given hash |
| 901 | * |
| 902 | * The subclass should return the requested height. |
| 903 | * |
| 904 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 905 | * |
| 906 | * @param h the hash to look for |
| 907 | * |
| 908 | * @return the height |
| 909 | */ |
| 910 | virtual uint64_t get_block_height(const crypto::hash& h) const = 0; |
| 911 | |
| 912 | /** |
| 913 | * @brief fetch a block header |
| 914 | * |
| 915 | * The subclass should return the block header from the block with |
| 916 | * the given hash. |
| 917 | * |
| 918 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 919 | * |
| 920 | * @param h the hash to look for |
| 921 | * |
| 922 | * @return the block header |
| 923 | */ |
| 924 | virtual block_header get_block_header(const crypto::hash& h) const = 0; |
| 925 | |
| 926 | /** |
| 927 | * @brief fetch a block blob by height |
| 928 | * |
| 929 | * The subclass should return the block at the given height. |
| 930 | * |
| 931 | * If the block does not exist, that is to say if the blockchain is not |
| 932 | * that high, then the subclass should throw BLOCK_DNE |
| 933 | * |
| 934 | * @param height the height to look for |
| 935 | * |
| 936 | * @return the block blob |
| 937 | */ |
| 938 | virtual cryptonote::blobdata get_block_blob_from_height(const uint64_t& height) const = 0; |
| 939 | |
| 940 | /** |
| 941 | * @brief fetch a block by height |
| 942 | * |
| 943 | * If the block does not exist, that is to say if the blockchain is not |
| 944 | * that high, then the subclass should throw BLOCK_DNE |
| 945 | * |
| 946 | * @param height the height to look for |
| 947 | * |
| 948 | * @return the block |
| 949 | */ |
| 950 | virtual block get_block_from_height(const uint64_t& height) const; |
| 951 | |
| 952 | /** |
| 953 | * @brief fetch a block's timestamp |
| 954 | * |
| 955 | * The subclass should return the timestamp of the block with the |
| 956 | * given height. |
| 957 | * |
| 958 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 959 | * |
| 960 | * @param height the height requested |
| 961 | * |
| 962 | * @return the timestamp |
| 963 | */ |
| 964 | virtual uint64_t get_block_timestamp(const uint64_t& height) const = 0; |
| 965 | |
| 966 | /** |
| 967 | * @brief fetch a block's cumulative number of rct outputs |
| 968 | * |
| 969 | * The subclass should return the numer of rct outputs in the blockchain |
| 970 | * up to the block with the given height (inclusive). |
| 971 | * |
| 972 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 973 | * |
| 974 | * @param height the height requested |
| 975 | * |
| 976 | * @return the cumulative number of rct outputs |
| 977 | */ |
| 978 | virtual std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const = 0; |
| 979 | |
| 980 | /** |
| 981 | * @brief fetch the top block's timestamp |
| 982 | * |
| 983 | * The subclass should return the timestamp of the most recent block. |
| 984 | * |
| 985 | * @return the top block's timestamp |
| 986 | */ |
| 987 | virtual uint64_t get_top_block_timestamp() const = 0; |
| 988 | |
| 989 | /** |
| 990 | * @brief fetch a block's weight |
| 991 | * |
| 992 | * The subclass should return the weight of the block with the |
| 993 | * given height. |
| 994 | * |
| 995 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 996 | * |
| 997 | * @param height the height requested |
| 998 | * |
| 999 | * @return the weight |
| 1000 | */ |
| 1001 | virtual size_t get_block_weight(const uint64_t& height) const = 0; |
| 1002 | |
| 1003 | /** |
| 1004 | * @brief fetch the last N blocks' weights |
| 1005 | * |
| 1006 | * If there are fewer than N blocks, the returned array will be smaller than N |
| 1007 | * |
| 1008 | * @param count the number of blocks requested |
| 1009 | * |
| 1010 | * @return the weights |
| 1011 | */ |
| 1012 | virtual std::vector<uint64_t> get_block_weights(uint64_t start_height, size_t count) const = 0; |
| 1013 | |
| 1014 | /** |
| 1015 | * @brief fetch a block's cumulative difficulty |
| 1016 | * |
| 1017 | * The subclass should return the cumulative difficulty of the block with the |
| 1018 | * given height. |
| 1019 | * |
| 1020 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 1021 | * |
| 1022 | * @param height the height requested |
| 1023 | * |
| 1024 | * @return the cumulative difficulty |
| 1025 | */ |
| 1026 | virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const = 0; |
| 1027 | |
| 1028 | /** |
| 1029 | * @brief fetch a block's difficulty |
| 1030 | * |
| 1031 | * The subclass should return the difficulty of the block with the |
| 1032 | * given height. |
| 1033 | * |
| 1034 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 1035 | * |
| 1036 | * @param height the height requested |
| 1037 | * |
| 1038 | * @return the difficulty |
| 1039 | */ |
| 1040 | virtual difficulty_type get_block_difficulty(const uint64_t& height) const = 0; |
| 1041 | |
| 1042 | /** |
| 1043 | * @brief correct blocks cumulative difficulties that were incorrectly calculated due to the 'difficulty drift' bug |
| 1044 | * |
| 1045 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 1046 | * |
| 1047 | * @param start_height the height where the drift starts |
| 1048 | * @param new_cumulative_difficulties new cumulative difficulties to be stored |
| 1049 | */ |
| 1050 | virtual void correct_block_cumulative_difficulties(const uint64_t& start_height, const std::vector<difficulty_type>& new_cumulative_difficulties) = 0; |
| 1051 | |
| 1052 | /** |
| 1053 | * @brief fetch a block's already generated coins |
| 1054 | * |
| 1055 | * The subclass should return the total coins generated as of the block |
| 1056 | * with the given height. |
| 1057 | * |
| 1058 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 1059 | * |
| 1060 | * @param height the height requested |
| 1061 | * |
| 1062 | * @return the already generated coins |
| 1063 | */ |
| 1064 | virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const = 0; |
| 1065 | |
| 1066 | /** |
| 1067 | * @brief fetch a block's long term weight |
| 1068 | * |
| 1069 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 1070 | * |
| 1071 | * @param height the height requested |
| 1072 | * |
| 1073 | * @return the long term weight |
| 1074 | */ |
| 1075 | virtual uint64_t get_block_long_term_weight(const uint64_t& height) const = 0; |
| 1076 | |
| 1077 | /** |
| 1078 | * @brief fetch the last N blocks' long term weights |
| 1079 | * |
| 1080 | * If there are fewer than N blocks, the returned array will be smaller than N |
| 1081 | * |
| 1082 | * @param count the number of blocks requested |
| 1083 | * |
| 1084 | * @return the weights |
| 1085 | */ |
| 1086 | virtual std::vector<uint64_t> get_long_term_block_weights(uint64_t start_height, size_t count) const = 0; |
| 1087 | |
| 1088 | /** |
| 1089 | * @brief fetch a block's hash |
| 1090 | * |
| 1091 | * The subclass should return hash of the block with the |
| 1092 | * given height. |
| 1093 | * |
| 1094 | * If the block does not exist, the subclass should throw BLOCK_DNE |
| 1095 | * |
| 1096 | * @param height the height requested |
| 1097 | * |
| 1098 | * @return the hash |
| 1099 | */ |
| 1100 | virtual crypto::hash get_block_hash_from_height(const uint64_t& height) const = 0; |
| 1101 | |
| 1102 | /** |
| 1103 | * @brief fetch a list of blocks |
| 1104 | * |
| 1105 | * The subclass should return a vector of blocks with heights starting at |
| 1106 | * h1 and ending at h2, inclusively. |
| 1107 | * |
| 1108 | * If the height range requested goes past the end of the blockchain, |
| 1109 | * the subclass should throw BLOCK_DNE. (current implementations simply |
| 1110 | * don't catch this exception as thrown by methods called within) |
| 1111 | * |
| 1112 | * @param h1 the start height |
| 1113 | * @param h2 the end height |
| 1114 | * |
| 1115 | * @return a vector of blocks |
| 1116 | */ |
| 1117 | virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const = 0; |
| 1118 | |
| 1119 | /** |
| 1120 | * @brief fetch a list of block hashes |
| 1121 | * |
| 1122 | * The subclass should return a vector of block hashes from blocks with |
| 1123 | * heights starting at h1 and ending at h2, inclusively. |
| 1124 | * |
| 1125 | * If the height range requested goes past the end of the blockchain, |
| 1126 | * the subclass should throw BLOCK_DNE. (current implementations simply |
| 1127 | * don't catch this exception as thrown by methods called within) |
| 1128 | * |
| 1129 | * @param h1 the start height |
| 1130 | * @param h2 the end height |
| 1131 | * |
| 1132 | * @return a vector of block hashes |
| 1133 | */ |
| 1134 | virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const = 0; |
| 1135 | |
| 1136 | /** |
| 1137 | * @brief fetch the top block's hash |
| 1138 | * |
| 1139 | * The subclass should return the hash of the most recent block |
| 1140 | * |
| 1141 | * @param block_height if non NULL, returns the height of that block (ie, the blockchain height minus 1) |
| 1142 | * |
| 1143 | * @return the top block's hash |
| 1144 | */ |
| 1145 | virtual crypto::hash top_block_hash(uint64_t *block_height = NULL) const = 0; |
| 1146 | |
| 1147 | /** |
| 1148 | * @brief fetch the top block |
| 1149 | * |
| 1150 | * The subclass should return most recent block |
| 1151 | * |
| 1152 | * @return the top block |
| 1153 | */ |
| 1154 | virtual block get_top_block() const = 0; |
| 1155 | |
| 1156 | /** |
| 1157 | * @brief fetch the current blockchain height |
| 1158 | * |
| 1159 | * The subclass should return the current blockchain height |
| 1160 | * |
| 1161 | * @return the current blockchain height |
| 1162 | */ |
| 1163 | virtual uint64_t height() const = 0; |
| 1164 | |
| 1165 | |
| 1166 | /** |
| 1167 | * <!-- |
| 1168 | * TODO: Rewrite (if necessary) such that all calls to remove_* are |
| 1169 | * done in concrete members of this base class. |
| 1170 | * --> |
| 1171 | * |
| 1172 | * @brief pops the top block off the blockchain |
| 1173 | * |
| 1174 | * The subclass should remove the most recent block from the blockchain, |
| 1175 | * along with all transactions, outputs, and other metadata created as |
| 1176 | * a result of its addition to the blockchain. Most of this is handled |
| 1177 | * by the concrete members of the base class provided the subclass correctly |
| 1178 | * implements remove_* functions. |
| 1179 | * |
| 1180 | * The subclass should return by reference the popped block and |
| 1181 | * its associated transactions |
| 1182 | * |
| 1183 | * @param blk return-by-reference the block which was popped |
| 1184 | * @param txs return-by-reference the transactions from the popped block |
| 1185 | */ |
| 1186 | virtual void pop_block(block& blk, std::vector<transaction>& txs); |
| 1187 | |
| 1188 | |
| 1189 | /** |
| 1190 | * @brief check if a transaction with a given hash exists |
| 1191 | * |
| 1192 | * The subclass should check if a transaction is stored which has the |
| 1193 | * given hash and return true if so, false otherwise. |
| 1194 | * |
| 1195 | * @param h the hash to check against |
| 1196 | * @param tx_id (optional) returns the tx_id for the tx hash |
| 1197 | * |
| 1198 | * @return true if the transaction exists, otherwise false |
| 1199 | */ |
| 1200 | virtual bool tx_exists(const crypto::hash& h) const = 0; |
| 1201 | virtual bool tx_exists(const crypto::hash& h, uint64_t& tx_id) const = 0; |
| 1202 | |
| 1203 | // return unlock time of tx with hash <h> |
| 1204 | /** |
| 1205 | * @brief fetch a transaction's unlock time/height |
| 1206 | * |
| 1207 | * The subclass should return the stored unlock time for the transaction |
| 1208 | * with the given hash. |
| 1209 | * |
| 1210 | * If no such transaction exists, the subclass should throw TX_DNE. |
| 1211 | * |
| 1212 | * @param h the hash of the requested transaction |
| 1213 | * |
| 1214 | * @return the unlock time/height |
| 1215 | */ |
| 1216 | virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const = 0; |
| 1217 | |
| 1218 | // return tx with hash <h> |
| 1219 | // throw if no such tx exists |
| 1220 | /** |
| 1221 | * @brief fetches the transaction with the given hash |
| 1222 | * |
| 1223 | * If the transaction does not exist, the subclass should throw TX_DNE. |
| 1224 | * |
| 1225 | * @param h the hash to look for |
| 1226 | * |
| 1227 | * @return the transaction with the given hash |
| 1228 | */ |
| 1229 | virtual transaction get_tx(const crypto::hash& h) const; |
| 1230 | |
| 1231 | /** |
| 1232 | * @brief fetches the transaction base with the given hash |
| 1233 | * |
| 1234 | * If the transaction does not exist, the subclass should throw TX_DNE. |
| 1235 | * |
| 1236 | * @param h the hash to look for |
| 1237 | * |
| 1238 | * @return the transaction with the given hash |
| 1239 | */ |
| 1240 | virtual transaction get_pruned_tx(const crypto::hash& h) const; |
| 1241 | |
| 1242 | /** |
| 1243 | * @brief fetches the transaction with the given hash |
| 1244 | * |
| 1245 | * If the transaction does not exist, the subclass should return false. |
| 1246 | * |
| 1247 | * @param h the hash to look for |
| 1248 | * |
| 1249 | * @return true iff the transaction was found |
| 1250 | */ |
| 1251 | virtual bool get_tx(const crypto::hash& h, transaction &tx) const; |
| 1252 | |
| 1253 | /** |
| 1254 | * @brief fetches the transaction base with the given hash |
| 1255 | * |
| 1256 | * If the transaction does not exist, the subclass should return false. |
| 1257 | * |
| 1258 | * @param h the hash to look for |
| 1259 | * |
| 1260 | * @return true iff the transaction was found |
| 1261 | */ |
| 1262 | virtual bool get_pruned_tx(const crypto::hash& h, transaction &tx) const; |
| 1263 | |
| 1264 | /** |
| 1265 | * @brief fetches the transaction blob with the given hash |
| 1266 | * |
| 1267 | * The subclass should return the transaction stored which has the given |
| 1268 | * hash. |
| 1269 | * |
| 1270 | * If the transaction does not exist, the subclass should return false. |
| 1271 | * |
| 1272 | * @param h the hash to look for |
| 1273 | * |
| 1274 | * @return true iff the transaction was found |
| 1275 | */ |
| 1276 | virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0; |
| 1277 | |
| 1278 | /** |
| 1279 | * @brief fetches the pruned transaction blob with the given hash |
| 1280 | * |
| 1281 | * The subclass should return the pruned transaction stored which has the given |
| 1282 | * hash. |
| 1283 | * |
| 1284 | * If the transaction does not exist, the subclass should return false. |
| 1285 | * |
| 1286 | * @param h the hash to look for |
| 1287 | * |
| 1288 | * @return true iff the transaction was found |
| 1289 | */ |
| 1290 | virtual bool get_pruned_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0; |
| 1291 | |
| 1292 | /** |
| 1293 | * @brief fetches a number of pruned transaction blob from the given hash, in canonical blockchain order |
| 1294 | * |
| 1295 | * The subclass should return the pruned transactions stored from the one with the given |
| 1296 | * hash. |
| 1297 | * |
| 1298 | * If the first transaction does not exist, the subclass should return false. |
| 1299 | * If the first transaction exists, but there are fewer transactions starting with it |
| 1300 | * than requested, the subclass should return false. |
| 1301 | * |
| 1302 | * @param h the hash to look for |
| 1303 | * |
| 1304 | * @return true iff the transactions were found |
| 1305 | */ |
| 1306 | virtual bool get_pruned_tx_blobs_from(const crypto::hash& h, size_t count, std::vector<cryptonote::blobdata> &bd) const = 0; |
| 1307 | |
| 1308 | /** |
| 1309 | * @brief fetches a variable number of blocks and transactions from the given height, in canonical blockchain order |
| 1310 | * |
| 1311 | * The subclass should return the blocks and transactions stored from the one with the given |
| 1312 | * height. The number of blocks returned is variable, based on the max_size passed. |
| 1313 | * |
| 1314 | * @param start_height the height of the first block |
| 1315 | * @param min_block_count the minimum number of blocks to return, if they exist |
| 1316 | * @param max_block_count the maximum number of blocks to return |
| 1317 | * @param max_tx_count the maximum number of txes to return |
| 1318 | * @param max_size the maximum size of block/transaction data to return (will be exceeded by one blocks's worth at most, if min_count is met) |
| 1319 | * @param blocks the returned block/transaction data |
| 1320 | * @param pruned whether to return full or pruned tx data |
| 1321 | * @param skip_coinbase whether to return or skip coinbase transactions (they're in blocks regardless) |
| 1322 | * @param get_miner_tx_hash whether to calculate and return the miner (coinbase) tx hash |
| 1323 | * |
| 1324 | * The call will return at least min_block_count if possible, even if this contravenes max_tx_count |
| 1325 | * |
| 1326 | * @return true iff the blocks and transactions were found |
| 1327 | */ |
| 1328 | virtual bool get_blocks_from(uint64_t start_height, size_t min_block_count, size_t max_block_count, size_t max_tx_count, size_t max_size, std::vector<std::pair<std::pair<cryptonote::blobdata, crypto::hash>, std::vector<std::pair<crypto::hash, cryptonote::blobdata>>>>& blocks, bool pruned, bool skip_coinbase, bool get_miner_tx_hash) const = 0; |
| 1329 | |
| 1330 | /** |
| 1331 | * @brief fetches the prunable transaction blob with the given hash |
| 1332 | * |
| 1333 | * The subclass should return the prunable transaction stored which has the given |
| 1334 | * hash. |
| 1335 | * |
| 1336 | * If the transaction does not exist, or if we do not have that prunable data, |
| 1337 | * the subclass should return false. |
| 1338 | * |
| 1339 | * @param h the hash to look for |
| 1340 | * |
| 1341 | * @return true iff the transaction was found and we have its prunable data |
| 1342 | */ |
| 1343 | virtual bool get_prunable_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const = 0; |
| 1344 | |
| 1345 | /** |
| 1346 | * @brief fetches the prunable transaction hash |
| 1347 | * |
| 1348 | * The subclass should return the hash of the prunable transaction data. |
| 1349 | * |
| 1350 | * If the transaction hash does not exist, the subclass should return false. |
| 1351 | * |
| 1352 | * @param h the tx hash to look for |
| 1353 | * |
| 1354 | * @return true iff the transaction was found |
| 1355 | */ |
| 1356 | virtual bool get_prunable_tx_hash(const crypto::hash& tx_hash, crypto::hash &prunable_hash) const = 0; |
| 1357 | |
| 1358 | /** |
| 1359 | * @brief fetches the total number of transactions ever |
| 1360 | * |
| 1361 | * The subclass should return a count of all the transactions from |
| 1362 | * all blocks. |
| 1363 | * |
| 1364 | * @return the number of transactions in the blockchain |
| 1365 | */ |
| 1366 | virtual uint64_t get_tx_count() const = 0; |
| 1367 | |
| 1368 | /** |
| 1369 | * @brief fetches a list of transactions based on their hashes |
| 1370 | * |
| 1371 | * The subclass should attempt to fetch each transaction referred to by |
| 1372 | * the hashes passed. |
| 1373 | * |
| 1374 | * Currently, if any of the transactions is not in BlockchainDB, the call |
| 1375 | * to get_tx in the implementation will throw TX_DNE. |
| 1376 | * |
| 1377 | * <!-- TODO: decide if this behavior is correct for missing transactions --> |
| 1378 | * |
| 1379 | * @param hlist a list of hashes |
| 1380 | * |
| 1381 | * @return the list of transactions |
| 1382 | */ |
| 1383 | virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const = 0; |
| 1384 | |
| 1385 | // returns height of block that contains transaction with hash <h> |
| 1386 | /** |
| 1387 | * @brief fetches the height of a transaction's block |
| 1388 | * |
| 1389 | * The subclass should attempt to return the height of the block containing |
| 1390 | * the transaction with the given hash. |
| 1391 | * |
| 1392 | * If the transaction cannot be found, the subclass should throw TX_DNE. |
| 1393 | * |
| 1394 | * @param h the hash of the transaction |
| 1395 | * |
| 1396 | * @return the height of the transaction's block |
| 1397 | */ |
| 1398 | virtual uint64_t get_tx_block_height(const crypto::hash& h) const = 0; |
| 1399 | |
| 1400 | // returns the total number of outputs of amount <amount> |
| 1401 | /** |
| 1402 | * @brief fetches the number of outputs of a given amount |
| 1403 | * |
| 1404 | * The subclass should return a count of outputs of the given amount, |
| 1405 | * or zero if there are none. |
| 1406 | * |
| 1407 | * <!-- TODO: should outputs spent with a low mixin (especially 0) be |
| 1408 | * excluded from the count? --> |
| 1409 | * |
| 1410 | * @param amount the output amount being looked up |
| 1411 | * |
| 1412 | * @return the number of outputs of the given amount |
| 1413 | */ |
| 1414 | virtual uint64_t get_num_outputs(const uint64_t& amount) const = 0; |
| 1415 | |
| 1416 | /** |
| 1417 | * @brief return index of the first element (should be hidden, but isn't) |
| 1418 | * |
| 1419 | * @return the index |
| 1420 | */ |
| 1421 | virtual uint64_t get_indexing_base() const { return 0; } |
| 1422 | |
| 1423 | /** |
| 1424 | * @brief get some of an output's data |
| 1425 | * |
| 1426 | * The subclass should return the public key, unlock time, and block height |
| 1427 | * for the output with the given amount and index, collected in a struct. |
| 1428 | * |
| 1429 | * If the output cannot be found, the subclass should throw OUTPUT_DNE. |
| 1430 | * |
| 1431 | * If any of these parts cannot be found, but some are, the subclass |
| 1432 | * should throw DB_ERROR with a message stating as much. |
| 1433 | * |
| 1434 | * @param amount the output amount |
| 1435 | * @param index the output's index (indexed by amount) |
| 1436 | * |
| 1437 | * @return the requested output data |
| 1438 | */ |
| 1439 | virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index, bool include_commitmemt = true) const = 0; |
| 1440 | |
| 1441 | /** |
| 1442 | * @brief gets an output's tx hash and index |
| 1443 | * |
| 1444 | * The subclass should return the hash of the transaction which created the |
| 1445 | * output with the global index given, as well as its index in that transaction. |
| 1446 | * |
| 1447 | * @param index an output's global index |
| 1448 | * |
| 1449 | * @return the tx hash and output index |
| 1450 | */ |
| 1451 | virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const = 0; |
| 1452 | |
| 1453 | /** |
| 1454 | * @brief gets an output's tx hash and index |
| 1455 | * |
| 1456 | * The subclass should return the hash of the transaction which created the |
| 1457 | * output with the amount and index given, as well as its index in that |
| 1458 | * transaction. |
| 1459 | * |
| 1460 | * @param amount an output amount |
| 1461 | * @param index an output's amount-specific index |
| 1462 | * |
| 1463 | * @return the tx hash and output index |
| 1464 | */ |
| 1465 | virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const = 0; |
| 1466 | |
| 1467 | /** |
| 1468 | * @brief gets some outputs' tx hashes and indices |
| 1469 | * |
| 1470 | * This function is a mirror of |
| 1471 | * get_output_tx_and_index(const uint64_t& amount, const uint64_t& index), |
| 1472 | * but for a list of outputs rather than just one. |
| 1473 | * |
| 1474 | * @param amount an output amount |
| 1475 | * @param offsets a list of amount-specific output indices |
| 1476 | * @param indices return-by-reference a list of tx hashes and output indices (as pairs) |
| 1477 | */ |
| 1478 | virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices) const = 0; |
| 1479 | |
| 1480 | /** |
| 1481 | * @brief gets outputs' data |
| 1482 | * |
| 1483 | * This function is a mirror of |
| 1484 | * get_output_data(const uint64_t& amount, const uint64_t& index) |
| 1485 | * but for a list of outputs rather than just one. |
| 1486 | * |
| 1487 | * @param amounts an output amount, or as many as offsets |
| 1488 | * @param offsets a list of amount-specific output indices |
| 1489 | * @param outputs return-by-reference a list of outputs' metadata |
| 1490 | */ |
| 1491 | virtual void get_output_key(const epee::span<const uint64_t> &amounts, const std::vector<uint64_t> &offsets, std::vector<output_data_t> &outputs, bool allow_partial = false) const = 0; |
| 1492 | |
| 1493 | /* |
| 1494 | * FIXME: Need to check with git blame and ask what this does to |
| 1495 | * document it |
| 1496 | */ |
| 1497 | virtual bool can_thread_bulk_indices() const = 0; |
| 1498 | |
| 1499 | /** |
| 1500 | * @brief gets output indices (amount-specific) for a transaction's outputs |
| 1501 | * |
| 1502 | * The subclass should fetch the amount-specific output indices for each |
| 1503 | * output in the transaction with the given ID. |
| 1504 | * |
| 1505 | * If the transaction does not exist, the subclass should throw TX_DNE. |
| 1506 | * |
| 1507 | * If an output cannot be found, the subclass should throw OUTPUT_DNE. |
| 1508 | * |
| 1509 | * @param tx_id a transaction ID |
| 1510 | * @param n_txes how many txes to get data for, starting with tx_id |
| 1511 | * |
| 1512 | * @return a list of amount-specific output indices |
| 1513 | */ |
| 1514 | virtual std::vector<std::vector<uint64_t>> get_tx_amount_output_indices(const uint64_t tx_id, size_t n_txes = 1) const = 0; |
| 1515 | |
| 1516 | /** |
| 1517 | * @brief check if a key image is stored as spent |
| 1518 | * |
| 1519 | * @param img the key image to check for |
| 1520 | * |
| 1521 | * @return true if the image is present, otherwise false |
| 1522 | */ |
| 1523 | virtual bool has_key_image(const crypto::key_image& img) const = 0; |
| 1524 | |
| 1525 | /** |
| 1526 | * @brief add a txpool transaction |
| 1527 | * |
| 1528 | * @param details the details of the transaction to add |
| 1529 | */ |
| 1530 | virtual void add_txpool_tx(const crypto::hash &txid, const cryptonote::blobdata_ref &blob, const txpool_tx_meta_t& details) = 0; |
| 1531 | |
| 1532 | /** |
| 1533 | * @brief update a txpool transaction's metadata |
| 1534 | * |
| 1535 | * @param txid the txid of the transaction to update |
| 1536 | * @param details the details of the transaction to update |
| 1537 | */ |
| 1538 | virtual void update_txpool_tx(const crypto::hash &txid, const txpool_tx_meta_t& details) = 0; |
| 1539 | |
| 1540 | /** |
| 1541 | * @brief get the number of transactions in the txpool |
| 1542 | */ |
| 1543 | virtual uint64_t get_txpool_tx_count(relay_category tx_category = relay_category::broadcasted) const = 0; |
| 1544 | |
| 1545 | /** |
| 1546 | * @brief check whether a txid is in the txpool and meets tx_category requirements |
| 1547 | */ |
| 1548 | virtual bool txpool_has_tx(const crypto::hash &txid, relay_category tx_category) const = 0; |
| 1549 | |
| 1550 | /** |
| 1551 | * @brief remove a txpool transaction |
| 1552 | * |
| 1553 | * @param txid the transaction id of the transation to remove |
| 1554 | */ |
| 1555 | virtual void remove_txpool_tx(const crypto::hash& txid) = 0; |
| 1556 | |
| 1557 | /** |
| 1558 | * @brief get a txpool transaction's metadata |
| 1559 | * |
| 1560 | * @param txid the transaction id of the transation to lookup |
| 1561 | * @param meta the metadata to return |
| 1562 | * |
| 1563 | * @return true if the tx meta was found, false otherwise |
| 1564 | */ |
| 1565 | virtual bool get_txpool_tx_meta(const crypto::hash& txid, txpool_tx_meta_t &meta) const = 0; |
| 1566 | |
| 1567 | /** |
| 1568 | * @brief get a txpool transaction's blob |
| 1569 | * |
| 1570 | * @param txid the transaction id of the transation to lookup |
| 1571 | * @param bd the blob to return |
| 1572 | * @param tx_category for filtering out hidden/private txes |
| 1573 | * |
| 1574 | * @return True iff `txid` is in the pool and meets `tx_category` requirements |
| 1575 | */ |
| 1576 | virtual bool get_txpool_tx_blob(const crypto::hash& txid, cryptonote::blobdata &bd, relay_category tx_category) const = 0; |
| 1577 | |
| 1578 | /** |
| 1579 | * @brief get a txpool transaction's blob |
| 1580 | * |
| 1581 | * @param txid the transaction id of the transation to lookup |
| 1582 | * |
| 1583 | * @return the blob for that transaction |
| 1584 | */ |
| 1585 | virtual cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid, relay_category tx_category) const = 0; |
| 1586 | |
| 1587 | /** |
| 1588 | * @brief Check if `tx_hash` relay status is in `category`. |
| 1589 | * |
| 1590 | * @param tx_hash hash of the transaction to lookup |
| 1591 | * @param category relay status category to test against |
| 1592 | * |
| 1593 | * @return True if `tx_hash` latest relay status is in `category`. |
| 1594 | */ |
| 1595 | bool txpool_tx_matches_category(const crypto::hash& tx_hash, relay_category category); |
| 1596 | |
| 1597 | /** |
| 1598 | * @brief prune output data for the given amount |
| 1599 | * |
| 1600 | * @param amount the amount for which to prune data |
| 1601 | */ |
| 1602 | virtual void prune_outputs(uint64_t amount) = 0; |
| 1603 | |
| 1604 | /** |
| 1605 | * @brief get the blockchain pruning seed |
| 1606 | * @return the blockchain pruning seed |
| 1607 | */ |
| 1608 | virtual uint32_t get_blockchain_pruning_seed() const = 0; |
| 1609 | |
| 1610 | /** |
| 1611 | * @brief prunes the blockchain |
| 1612 | * @param pruning_seed the seed to use, 0 for default (highly recommended) |
| 1613 | * @return success iff true |
| 1614 | */ |
| 1615 | virtual bool prune_blockchain(uint32_t pruning_seed = 0) = 0; |
| 1616 | |
| 1617 | /** |
| 1618 | * @brief prunes recent blockchain changes as needed, iff pruning is enabled |
| 1619 | * @return success iff true |
| 1620 | */ |
| 1621 | virtual bool update_pruning() = 0; |
| 1622 | |
| 1623 | /** |
| 1624 | * @brief checks pruning was done correctly, iff enabled |
| 1625 | * @return success iff true |
| 1626 | */ |
| 1627 | virtual bool check_pruning() = 0; |
| 1628 | |
| 1629 | /** |
| 1630 | * @brief get the max block size |
| 1631 | */ |
| 1632 | virtual uint64_t get_max_block_size() = 0; |
| 1633 | |
| 1634 | /** |
| 1635 | * @brief add a new max block size |
| 1636 | * |
| 1637 | * The max block size will be the maximum of sz and the current block size |
| 1638 | * |
| 1639 | * @param: sz the block size |
| 1640 | */ |
| 1641 | virtual void add_max_block_size(uint64_t sz) = 0; |
| 1642 | |
| 1643 | /** |
| 1644 | * @brief add a new alternative block |
| 1645 | * |
| 1646 | * @param: blkid the block hash |
| 1647 | * @param: data: the metadata for the block |
| 1648 | * @param: blob: the block's blob |
| 1649 | */ |
| 1650 | virtual void add_alt_block(const crypto::hash &blkid, const cryptonote::alt_block_data_t &data, const cryptonote::blobdata_ref &blob) = 0; |
| 1651 | |
| 1652 | /** |
| 1653 | * @brief get an alternative block by hash |
| 1654 | * |
| 1655 | * @param: blkid the block hash |
| 1656 | * @param: data: the metadata for the block |
| 1657 | * @param: blob: the block's blob |
| 1658 | * |
| 1659 | * @return true if the block was found in the alternative blocks list, false otherwise |
| 1660 | */ |
| 1661 | virtual bool get_alt_block(const crypto::hash &blkid, alt_block_data_t *data, cryptonote::blobdata *blob) = 0; |
| 1662 | |
| 1663 | /** |
| 1664 | * @brief remove an alternative block |
| 1665 | * |
| 1666 | * @param: blkid the block hash |
| 1667 | */ |
| 1668 | virtual void remove_alt_block(const crypto::hash &blkid) = 0; |
| 1669 | |
| 1670 | /** |
| 1671 | * @brief get the number of alternative blocks stored |
| 1672 | */ |
| 1673 | virtual uint64_t get_alt_block_count() = 0; |
| 1674 | |
| 1675 | /** |
| 1676 | * @brief drop all alternative blocks |
| 1677 | */ |
| 1678 | virtual void drop_alt_blocks() = 0; |
| 1679 | |
| 1680 | /** |
| 1681 | * @brief runs a function over all txpool transactions |
| 1682 | * |
| 1683 | * The subclass should run the passed function for each txpool tx it has |
| 1684 | * stored, passing the tx id and metadata as its parameters. |
| 1685 | * |
| 1686 | * If any call to the function returns false, the subclass should return |
| 1687 | * false. Otherwise, the subclass returns true. |
| 1688 | * |
| 1689 | * @param std::function fn the function to run |
| 1690 | * |
| 1691 | * @return false if the function returns false for any transaction, otherwise true |
| 1692 | */ |
| 1693 | virtual bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata_ref*)>, bool include_blob = false, relay_category category = relay_category::broadcasted) const = 0; |
| 1694 | |
| 1695 | /** |
| 1696 | * @brief runs a function over all key images stored |
| 1697 | * |
| 1698 | * The subclass should run the passed function for each key image it has |
| 1699 | * stored, passing the key image as its parameter. |
| 1700 | * |
| 1701 | * If any call to the function returns false, the subclass should return |
| 1702 | * false. Otherwise, the subclass returns true. |
| 1703 | * |
| 1704 | * @param std::function fn the function to run |
| 1705 | * |
| 1706 | * @return false if the function returns false for any key image, otherwise true |
| 1707 | */ |
| 1708 | virtual bool for_all_key_images(std::function<bool(const crypto::key_image&)>) const = 0; |
| 1709 | |
| 1710 | /** |
| 1711 | * @brief runs a function over a range of blocks |
| 1712 | * |
| 1713 | * The subclass should run the passed function for each block in the |
| 1714 | * specified range, passing (block_height, block_hash, block) as its parameters. |
| 1715 | * |
| 1716 | * If any call to the function returns false, the subclass should return |
| 1717 | * false. Otherwise, the subclass returns true. |
| 1718 | * |
| 1719 | * The subclass should throw DB_ERROR if any of the expected values are |
| 1720 | * not found. Current implementations simply return false. |
| 1721 | * |
| 1722 | * @param h1 the start height |
| 1723 | * @param h2 the end height |
| 1724 | * @param std::function fn the function to run |
| 1725 | * |
| 1726 | * @return false if the function returns false for any block, otherwise true |
| 1727 | */ |
| 1728 | virtual bool for_blocks_range(const uint64_t& h1, const uint64_t& h2, std::function<bool(uint64_t, const crypto::hash&, const cryptonote::block&)>) const = 0; |
| 1729 | |
| 1730 | /** |
| 1731 | * @brief runs a function over all transactions stored |
| 1732 | * |
| 1733 | * The subclass should run the passed function for each transaction it has |
| 1734 | * stored, passing (transaction_hash, transaction) as its parameters. |
| 1735 | * |
| 1736 | * If any call to the function returns false, the subclass should return |
| 1737 | * false. Otherwise, the subclass returns true. |
| 1738 | * |
| 1739 | * The subclass should throw DB_ERROR if any of the expected values are |
| 1740 | * not found. Current implementations simply return false. |
| 1741 | * |
| 1742 | * @param std::function fn the function to run |
| 1743 | * @param bool pruned whether to only get pruned tx data, or the whole |
| 1744 | * |
| 1745 | * @return false if the function returns false for any transaction, otherwise true |
| 1746 | */ |
| 1747 | virtual bool for_all_transactions(std::function<bool(const crypto::hash&, const cryptonote::transaction&)>, bool pruned) const = 0; |
| 1748 | |
| 1749 | /** |
| 1750 | * @brief runs a function over all outputs stored |
| 1751 | * |
| 1752 | * The subclass should run the passed function for each output it has |
| 1753 | * stored, passing (amount, transaction_hash, tx_local_output_index) |
| 1754 | * as its parameters. |
| 1755 | * |
| 1756 | * If any call to the function returns false, the subclass should return |
| 1757 | * false. Otherwise, the subclass returns true. |
| 1758 | * |
| 1759 | * The subclass should throw DB_ERROR if any of the expected values are |
| 1760 | * not found. Current implementations simply return false. |
| 1761 | * |
| 1762 | * @param std::function f the function to run |
| 1763 | * |
| 1764 | * @return false if the function returns false for any output, otherwise true |
| 1765 | */ |
| 1766 | virtual bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, uint64_t height, size_t tx_idx)> f) const = 0; |
| 1767 | virtual bool for_all_outputs(uint64_t amount, const std::function<bool(uint64_t height)> &f) const = 0; |
| 1768 | |
| 1769 | /** |
| 1770 | * @brief runs a function over all alternative blocks stored |
| 1771 | * |
| 1772 | * The subclass should run the passed function for each alt block it has |
| 1773 | * stored, passing (blkid, data, blob) as its parameters. |
| 1774 | * |
| 1775 | * If any call to the function returns false, the subclass should return |
| 1776 | * false. Otherwise, the subclass returns true. |
| 1777 | * |
| 1778 | * The subclass should throw DB_ERROR if any of the expected values are |
| 1779 | * not found. Current implementations simply return false. |
| 1780 | * |
| 1781 | * @param std::function f the function to run |
| 1782 | * |
| 1783 | * @return false if the function returns false for any output, otherwise true |
| 1784 | */ |
| 1785 | virtual bool for_all_alt_blocks(std::function<bool(const crypto::hash &blkid, const alt_block_data_t &data, const cryptonote::blobdata_ref *blob)> f, bool include_blob = false) const = 0; |
| 1786 | |
| 1787 | |
| 1788 | // |
| 1789 | // Hard fork related storage |
| 1790 | // |
| 1791 | |
| 1792 | /** |
| 1793 | * @brief sets which hardfork version a height is on |
| 1794 | * |
| 1795 | * @param height the height |
| 1796 | * @param version the version |
| 1797 | */ |
| 1798 | virtual void set_hard_fork_version(uint64_t height, uint8_t version) = 0; |
| 1799 | |
| 1800 | /** |
| 1801 | * @brief checks which hardfork version a height is on |
| 1802 | * |
| 1803 | * @param height the height |
| 1804 | * |
| 1805 | * @return the version |
| 1806 | */ |
| 1807 | virtual uint8_t get_hard_fork_version(uint64_t height) const = 0; |
| 1808 | |
| 1809 | /** |
| 1810 | * @brief verify hard fork info in database |
| 1811 | */ |
| 1812 | virtual void check_hard_fork_info() = 0; |
| 1813 | |
| 1814 | /** |
| 1815 | * @brief delete hard fork info from database |
| 1816 | */ |
| 1817 | virtual void drop_hard_fork_info() = 0; |
| 1818 | |
| 1819 | /** |
| 1820 | * @brief return a histogram of outputs on the blockchain |
| 1821 | * |
| 1822 | * @param amounts optional set of amounts to lookup |
| 1823 | * @param unlocked whether to restrict count to unlocked outputs |
| 1824 | * @param recent_cutoff timestamp to determine whether an output is recent |
| 1825 | * @param min_count return only amounts with at least that many instances |
| 1826 | * |
| 1827 | * @return a set of amount/instances |
| 1828 | */ |
| 1829 | virtual std::map<uint64_t, std::tuple<uint64_t, uint64_t, uint64_t>> get_output_histogram(const std::vector<uint64_t> &amounts, bool unlocked, uint64_t recent_cutoff, uint64_t min_count) const = 0; |
| 1830 | |
| 1831 | virtual bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, std::vector<uint64_t> &distribution, uint64_t &base) const = 0; |
| 1832 | |
| 1833 | /** |
| 1834 | * @brief is BlockchainDB in read-only mode? |
| 1835 | * |
| 1836 | * @return true if in read-only mode, otherwise false |
| 1837 | */ |
| 1838 | virtual bool is_read_only() const = 0; |
| 1839 | |
| 1840 | /** |
| 1841 | * @brief get disk space requirements |
| 1842 | * |
| 1843 | * @return the size required |
| 1844 | */ |
| 1845 | virtual uint64_t get_database_size() const = 0; |
| 1846 | |
| 1847 | // TODO: this should perhaps be (or call) a series of functions which |
| 1848 | // progressively update through version updates |
| 1849 | /** |
| 1850 | * @brief fix up anything that may be wrong due to past bugs |
| 1851 | */ |
| 1852 | virtual void fixup(); |
| 1853 | |
| 1854 | /** |
| 1855 | * @brief set whether or not to automatically remove logs |
| 1856 | * |
| 1857 | * This function is only relevant for one implementation (BlockchainBDB), but |
| 1858 | * is here to keep BlockchainDB users implementation-agnostic. |
| 1859 | * |
| 1860 | * @param auto_remove whether or not to auto-remove logs |
| 1861 | */ |
| 1862 | void set_auto_remove_logs(bool auto_remove) { m_auto_remove_logs = auto_remove; } |
| 1863 | |
| 1864 | bool m_open; //!< Whether or not the BlockchainDB is open/ready for use |
| 1865 | mutable epee::critical_section m_synchronization_lock; //!< A lock, currently for when BlockchainLMDB needs to resize the backing db file |
| 1866 | |
| 1867 | }; // class BlockchainDB |
| 1868 | |
| 1869 | class db_txn_guard |
| 1870 | { |
| 1871 | public: |
| 1872 | db_txn_guard(BlockchainDB *db, bool readonly): db(db), readonly(readonly), active(false) |
| 1873 | { |
| 1874 | if (readonly) |
| 1875 | { |
| 1876 | active = db->block_rtxn_start(); |
| 1877 | } |
| 1878 | else |
| 1879 | { |
| 1880 | db->block_wtxn_start(); |
| 1881 | active = true; |
| 1882 | } |
| 1883 | } |
| 1884 | virtual ~db_txn_guard() |
| 1885 | { |
| 1886 | stop(); |
| 1887 | } |
| 1888 | void stop() |
| 1889 | { |
| 1890 | if (active) |
| 1891 | { |
| 1892 | if (readonly) |
| 1893 | db->block_rtxn_stop(); |
| 1894 | else |
| 1895 | db->block_wtxn_stop(); |
| 1896 | active = false; |
| 1897 | } |
| 1898 | } |
| 1899 | void abort() |
| 1900 | { |
| 1901 | if (readonly) |
| 1902 | db->block_rtxn_abort(); |
| 1903 | else |
| 1904 | db->block_wtxn_abort(); |
| 1905 | active = false; |
| 1906 | } |
| 1907 | |
| 1908 | private: |
| 1909 | BlockchainDB *db; |
| 1910 | bool readonly; |
| 1911 | bool active; |
| 1912 | }; |
| 1913 | |
| 1914 | class db_rtxn_guard: public db_txn_guard { public: db_rtxn_guard(BlockchainDB *db): db_txn_guard(db, true) {} }; |
| 1915 | class db_wtxn_guard: public db_txn_guard { public: db_wtxn_guard(BlockchainDB *db): db_txn_guard(db, false) {} }; |
| 1916 | |
| 1917 | BlockchainDB *new_db(); |
| 1918 | |
| 1919 | } // namespace cryptonote |
| 1920 | |
| 1921 | #endif // BLOCKCHAIN_DB_H |