| 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 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include "chaingen.h" |
| 32 | #include "v2_tests.h" |
| 33 | |
| 34 | using namespace epee; |
| 35 | using namespace crypto; |
| 36 | using namespace cryptonote; |
| 37 | |
| 38 | //---------------------------------------------------------------------------------------------------------------------- |
| 39 | // Tests |
| 40 | |
| 41 | bool gen_v2_tx_validation_base::generate_with(std::vector<test_event_entry>& events, const int *out_idx, int mixin, uint64_t amount_paid, bool valid) const |
| 42 | { |
| 43 | uint64_t ts_start = 1338224400; |
| 44 | |
| 45 | GENERATE_ACCOUNT(miner_account); |
| 46 | MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start); |
| 47 | |
| 48 | // create 4 miner accounts, and have them mine the next 4 blocks |
| 49 | cryptonote::account_base miner_accounts[4]; |
| 50 | const cryptonote::block *prev_block = &blk_0; |
| 51 | cryptonote::block blocks[4]; |
| 52 | for (size_t n = 0; n < 4; ++n) { |
| 53 | miner_accounts[n].generate(); |
| 54 | CHECK_AND_ASSERT_MES(generator.construct_block_manually(blocks[n], *prev_block, miner_accounts[n], |
| 55 | test_generator::bf_major_ver | test_generator::bf_minor_ver | test_generator::bf_timestamp, |
| 56 | 2, 2, prev_block->timestamp + DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN * 2, // v2 has blocks twice as long |
| 57 | crypto::hash(), 0, transaction(), std::vector<crypto::hash>(), 0, 0), |
| 58 | false, "Failed to generate block"); |
| 59 | events.push_back(blocks[n]); |
| 60 | prev_block = blocks + n; |
| 61 | } |
| 62 | |
| 63 | // rewind |
| 64 | cryptonote::block blk_r; |
| 65 | { |
| 66 | cryptonote::block blk_last = blocks[3]; |
| 67 | for (size_t i = 0; i < CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW; ++i) |
| 68 | { |
| 69 | cryptonote::block blk; |
| 70 | CHECK_AND_ASSERT_MES(generator.construct_block_manually(blk, blk_last, miner_account, |
| 71 | test_generator::bf_major_ver | test_generator::bf_minor_ver | test_generator::bf_timestamp, |
| 72 | 2, 2, blk_last.timestamp + DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN * 2, // v2 has blocks twice as long |
| 73 | crypto::hash(), 0, transaction(), std::vector<crypto::hash>(), 0, 0), |
| 74 | false, "Failed to generate block"); |
| 75 | events.push_back(blk); |
| 76 | blk_last = blk; |
| 77 | } |
| 78 | blk_r = blk_last; |
| 79 | } |
| 80 | |
| 81 | // create a tx with the Nth outputs of miner's block reward |
| 82 | std::vector<tx_source_entry> sources; |
| 83 | for (size_t out_idx_idx = 0; out_idx[out_idx_idx] >= 0; ++out_idx_idx) { |
| 84 | sources.resize(sources.size()+1); |
| 85 | tx_source_entry& src = sources.back(); |
| 86 | |
| 87 | src.amount = blocks[0].miner_tx.vout[out_idx[out_idx_idx]].amount; |
| 88 | for (int m = 0; m <= mixin; ++m) { |
| 89 | int idx; |
| 90 | if (is_valid_decomposed_amount(src.amount)) |
| 91 | idx = m+1; // one out of that size per miner tx, including genesis |
| 92 | else |
| 93 | idx = 0; // dusty, no other output of that size |
| 94 | src.push_output(idx, boost::get<txout_to_key>(blocks[m].miner_tx.vout[out_idx[out_idx_idx]].target).key, src.amount); |
| 95 | } |
| 96 | src.real_out_tx_key = cryptonote::get_tx_pub_key_from_extra(blocks[0].miner_tx); |
| 97 | src.real_output = 0; |
| 98 | src.rct = false; |
| 99 | src.real_output_in_tx_index = out_idx[out_idx_idx]; |
| 100 | } |
| 101 | |
| 102 | //fill outputs entry |
| 103 | tx_destination_entry td; |
| 104 | td.addr = miner_account.get_keys().m_account_address; |
| 105 | td.amount = amount_paid; |
| 106 | std::vector<tx_destination_entry> destinations; |
| 107 | destinations.push_back(td); |
| 108 | |
| 109 | transaction tx; |
| 110 | bool r = construct_tx(miner_accounts[0].get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tx); |
| 111 | CHECK_AND_ASSERT_MES(r, false, "failed to construct transaction"); |
| 112 | if (!valid) |
| 113 | DO_CALLBACK(events, "mark_invalid_tx"); |
| 114 | events.push_back(tx); |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | bool gen_v2_tx_mixable_0_mixin::generate(std::vector<test_event_entry>& events) const |
| 120 | { |
| 121 | const int mixin = 0; |
| 122 | const int out_idx[] = {1, -1}; |
| 123 | const uint64_t amount_paid = 10000; |
| 124 | return generate_with(events, out_idx, mixin, amount_paid, false); |
| 125 | } |
| 126 | |
| 127 | bool gen_v2_tx_mixable_low_mixin::generate(std::vector<test_event_entry>& events) const |
| 128 | { |
| 129 | const int mixin = 1; |
| 130 | const int out_idx[] = {1, -1}; |
| 131 | const uint64_t amount_paid = 10000; |
| 132 | return generate_with(events, out_idx, mixin, amount_paid, false); |
| 133 | } |
| 134 | |
| 135 | bool gen_v2_tx_unmixable_only::generate(std::vector<test_event_entry>& events) const |
| 136 | { |
| 137 | const int mixin = 0; |
| 138 | const int out_idx[] = {0, -1}; |
| 139 | const uint64_t amount_paid = 10000; |
| 140 | return generate_with(events, out_idx, mixin, amount_paid, true); |
| 141 | } |
| 142 | |
| 143 | bool gen_v2_tx_unmixable_one::generate(std::vector<test_event_entry>& events) const |
| 144 | { |
| 145 | const int mixin = 0; |
| 146 | const int out_idx[] = {0, 1, -1}; |
| 147 | const uint64_t amount_paid = 10000; |
| 148 | return generate_with(events, out_idx, mixin, amount_paid, true); |
| 149 | } |
| 150 | |
| 151 | bool gen_v2_tx_unmixable_two::generate(std::vector<test_event_entry>& events) const |
| 152 | { |
| 153 | const int mixin = 0; |
| 154 | const int out_idx[] = {0, 1, 2, -1}; |
| 155 | const uint64_t amount_paid = 10000; |
| 156 | return generate_with(events, out_idx, mixin, amount_paid, false); |
| 157 | } |
| 158 | |
| 159 | bool gen_v2_tx_dust::generate(std::vector<test_event_entry>& events) const |
| 160 | { |
| 161 | const int mixin = 2; |
| 162 | const int out_idx[] = {1, -1}; |
| 163 | const uint64_t amount_paid = 10001; |
| 164 | return generate_with(events, out_idx, mixin, amount_paid, false); |
| 165 | } |