Debian Codegraph @

src/cryptonote_core/tx_sanity_check.cpp

raw symbols SCIP
1 // Copyright (c) 2019-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
29 #include <stdint.h>
30 #include <vector>
31 #include "cryptonote_basic/cryptonote_basic.h"
32 #include "cryptonote_basic/cryptonote_format_utils.h"
33 #include "blockchain.h"
34 #include "tx_sanity_check.h"
35
36 #undef MONERO_DEFAULT_LOG_CATEGORY
37 #define MONERO_DEFAULT_LOG_CATEGORY "verify"
38
39 namespace cryptonote
40 {
41
42 bool tx_sanity_check(const cryptonote::blobdata &tx_blob, uint64_t rct_outs_available)
43 {
44 cryptonote::transaction tx;
45
46 if (!cryptonote::parse_and_validate_tx_from_blob(tx_blob, tx))
47 {
48 MERROR("Failed to parse transaction");
49 return false;
50 }
51
52 if (cryptonote::is_coinbase(tx))
53 {
54 MERROR("Transaction is coinbase");
55 return false;
56 }
57 std::set<uint64_t> rct_indices;
58 size_t n_indices = 0;
59
60 for (const auto &txin : tx.vin)
61 {
62 if (txin.type() != typeid(cryptonote::txin_to_key))
63 continue;
64 const cryptonote::txin_to_key &in_to_key = boost::get<cryptonote::txin_to_key>(txin);
65 if (in_to_key.amount != 0)
66 continue;
67 const std::vector<uint64_t> absolute = cryptonote::relative_output_offsets_to_absolute(in_to_key.key_offsets);
68 for (uint64_t offset: absolute)
69 rct_indices.insert(offset);
70 n_indices += in_to_key.key_offsets.size();
71 }
72
73 return tx_sanity_check(rct_indices, n_indices, rct_outs_available);
74 }
75
76 bool tx_sanity_check(const std::set<uint64_t> &rct_indices, size_t n_indices, uint64_t rct_outs_available)
77 {
78 if (n_indices <= 10)
79 {
80 MDEBUG("n_indices is only " << n_indices << ", not checking");
81 return true;
82 }
83
84 if (rct_outs_available < 10000)
85 return true;
86
87 if (rct_indices.size() < n_indices * 8 / 10)
88 {
89 MERROR("amount of unique indices is too low (amount of rct indices is " << rct_indices.size() << ", out of total " << n_indices << "indices.");
90 return false;
91 }
92
93 std::vector<uint64_t> offsets(rct_indices.begin(), rct_indices.end());
94 uint64_t median = epee::misc_utils::median(offsets);
95 if (median < rct_outs_available * 6 / 10)
96 {
97 MERROR("median offset index is too low (median is " << median << " out of total " << rct_outs_available << "offsets). Transactions should contain a higher fraction of recent outputs.");
98 return false;
99 }
100
101 return true;
102 }
103
104 }