| 1 | // Copyright (c) 2023, 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 | |
| 29 | #pragma once |
| 30 | |
| 31 | #include "common/data_cache.h" |
| 32 | #include "cryptonote_basic/cryptonote_basic.h" |
| 33 | #include "cryptonote_basic/verification_context.h" |
| 34 | |
| 35 | namespace cryptonote |
| 36 | { |
| 37 | |
| 38 | /** |
| 39 | * @brief Get the maximum transaction weight for a given hardfork |
| 40 | * |
| 41 | * @param hf_version hard fork version |
| 42 | * @return the maximum unconditional transaction weight |
| 43 | */ |
| 44 | uint64_t get_transaction_weight_limit(uint8_t hf_version); |
| 45 | |
| 46 | // Modifying this value should not affect consensus. You can adjust it for performance needs |
| 47 | static constexpr const size_t RCT_VER_CACHE_SIZE = 8192; |
| 48 | |
| 49 | using rct_ver_cache_t = ::tools::data_cache<::crypto::hash, RCT_VER_CACHE_SIZE>; |
| 50 | |
| 51 | /** |
| 52 | * @brief Cached version of rct::verRctNonSemanticsSimple |
| 53 | * |
| 54 | * This function will not affect how the transaction is serialized and it will never modify the |
| 55 | * transaction prefix. |
| 56 | * |
| 57 | * The reference to tx is mutable since the transaction's ring signatures may be expanded by |
| 58 | * Blockchain::expand_transaction_2. However, on cache hits, the transaction will not be |
| 59 | * expanded. This means that the caller does not need to call expand_transaction_2 on this |
| 60 | * transaction before passing it; the transaction will not successfully verify with "old" RCT data |
| 61 | * if the transaction has been otherwise modified since the last verification. |
| 62 | * |
| 63 | * But, if cryptonote::get_transaction_hash(tx) returns a "stale" hash, this function is not |
| 64 | * guaranteed to work. So make sure that the cryptonote::transaction passed has not had |
| 65 | * modifications to it since the last time its hash was fetched without properly invalidating the |
| 66 | * hashes. |
| 67 | * |
| 68 | * rct_type_to_cache can be any RCT version value as long as rct::verRctNonSemanticsSimple works for |
| 69 | * this RCT version, but for most applications, it doesn't make sense to not make this version |
| 70 | * the "current" RCT version (i.e. the version that transactions in the mempool are). |
| 71 | * |
| 72 | * @param tx transaction which contains RCT signature to verify |
| 73 | * @param mix_ring mixring referenced by this tx. THIS DATA MUST BE PREVIOUSLY VALIDATED |
| 74 | * @param cache saves tx+mixring hashes used to cache calls |
| 75 | * @param rct_type_to_cache Only RCT sigs with version (e.g. RCTTypeBulletproofPlus) will be cached |
| 76 | * @return true when verRctNonSemanticsSimple() w/ expanded tx.rct_signatures would return true |
| 77 | * @return false when verRctNonSemanticsSimple() w/ expanded tx.rct_signatures would return false |
| 78 | */ |
| 79 | bool ver_rct_non_semantics_simple_cached |
| 80 | ( |
| 81 | transaction& tx, |
| 82 | const rct::ctkeyM& mix_ring, |
| 83 | rct_ver_cache_t& cache, |
| 84 | std::uint8_t rct_type_to_cache |
| 85 | ); |
| 86 | |
| 87 | /** |
| 88 | * @brief Verify the semantics of a group of RingCT signatures as a batch (if applicable) |
| 89 | * |
| 90 | * Coinbase txs or other transaction with a RingCT type of RCTTypeNull will fail to verify. |
| 91 | * |
| 92 | * @param rvv list of signatures to verify |
| 93 | * @return true if all signatures verified semantics successfully, false otherwise |
| 94 | */ |
| 95 | bool ver_mixed_rct_semantics(std::vector<const rct::rctSig*> rvv); |
| 96 | |
| 97 | /** |
| 98 | * @brief Used to provide transaction info that skips the mempool to block handling code |
| 99 | */ |
| 100 | struct pool_supplement |
| 101 | { |
| 102 | // Map of supplemental tx info that we might need to validate a block |
| 103 | // Maps TXID -> transaction and blob |
| 104 | std::unordered_map<crypto::hash, std::pair<transaction, blobdata>> txs_by_txid; |
| 105 | // If non-zero, then consider all the txs' non-input consensus (NIC) rules verified for this |
| 106 | // hard fork. User: If you add an unverified transaction to txs_by_txid, set this field to zero! |
| 107 | mutable std::uint8_t nic_verified_hf_version = 0; |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * @brief Verify every non-input consensus rule for a group of non-coinbase transactions |
| 112 | * |
| 113 | * List of checks that we do for each transaction: |
| 114 | * 1. Check tx blob size < get_max_tx_size() |
| 115 | * 2. Check tx version != 0 |
| 116 | * 3. Check tx version is less than maximum for given hard fork version |
| 117 | * 4. Check tx weight < get_transaction_weight_limit() |
| 118 | * 5. Passes core::check_tx_semantic() |
| 119 | * 6. Passes Blockchain::check_tx_outputs() |
| 120 | * 7. Passes ver_mixed_rct_semantics() [Uses batch RingCT verification when applicable] |
| 121 | * |
| 122 | * For pool_supplement input: |
| 123 | * We assume the structure of the pool supplement is already correct: for each value entry, the |
| 124 | * cryptonote::transaction matches its corresponding blobdata and the TXID map key is correctly |
| 125 | * calculated for that transaction. We use the .nic_verified_hf_version field to skip verification |
| 126 | * for the pool supplement if hf_version matches, and we cache that version on success. |
| 127 | * |
| 128 | * @param tx single transaction to verify |
| 129 | * @param pool_supplement pool supplement to verify |
| 130 | * @param tvc relevant flags will be set for if/why verification failed |
| 131 | * @param hf_version Hard fork version to run rules against |
| 132 | * @return true if all relevant transactions verify, false otherwise |
| 133 | */ |
| 134 | bool ver_non_input_consensus(const transaction& tx, tx_verification_context& tvc, |
| 135 | std::uint8_t hf_version); |
| 136 | |
| 137 | bool ver_non_input_consensus(const pool_supplement& ps, tx_verification_context& tvc, |
| 138 | std::uint8_t hf_version); |
| 139 | |
| 140 | } // namespace cryptonote |