| 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 "double_spend.h" |
| 33 | |
| 34 | using namespace epee; |
| 35 | using namespace cryptonote; |
| 36 | |
| 37 | |
| 38 | //====================================================================================================================== |
| 39 | |
| 40 | gen_double_spend_in_different_chains::gen_double_spend_in_different_chains() |
| 41 | { |
| 42 | REGISTER_CALLBACK_METHOD(gen_double_spend_in_different_chains, check_double_spend); |
| 43 | } |
| 44 | |
| 45 | bool gen_double_spend_in_different_chains::generate(std::vector<test_event_entry>& events) const |
| 46 | { |
| 47 | INIT_DOUBLE_SPEND_TEST(); |
| 48 | |
| 49 | SET_EVENT_VISITOR_SETT(events, event_visitor_settings::set_txs_keeped_by_block); |
| 50 | MAKE_TX(events, tx_1, bob_account, alice_account, send_amount / 2 - TESTS_DEFAULT_FEE, blk_1); |
| 51 | events.pop_back(); |
| 52 | MAKE_TX(events, tx_2, bob_account, alice_account, send_amount - TESTS_DEFAULT_FEE, blk_1); |
| 53 | events.pop_back(); |
| 54 | |
| 55 | // Main chain |
| 56 | events.push_back(tx_1); |
| 57 | MAKE_NEXT_BLOCK_TX1(events, blk_2, blk_1r, miner_account, tx_1); |
| 58 | |
| 59 | // Alternative chain |
| 60 | events.push_back(tx_2); |
| 61 | MAKE_NEXT_BLOCK_TX1(events, blk_3, blk_1r, miner_account, tx_2); |
| 62 | // Switch to alternative chain |
| 63 | MAKE_NEXT_BLOCK(events, blk_4, blk_3, miner_account); |
| 64 | //CHECK_AND_NO_ASSERT_MES(expected_blockchain_height == get_block_height(blk_4) + 1, false, "expected_blockchain_height has invalid value"); |
| 65 | if ((expected_blockchain_height != get_block_height(blk_4) + 1)) LOG_ERROR("oops"); |
| 66 | |
| 67 | DO_CALLBACK(events, "check_double_spend"); |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool gen_double_spend_in_different_chains::check_double_spend(cryptonote::core& c, size_t /*ev_index*/, const std::vector<test_event_entry>& events) |
| 73 | { |
| 74 | DEFINE_TESTS_ERROR_CONTEXT("gen_double_spend_in_different_chains::check_double_spend"); |
| 75 | |
| 76 | std::vector<block> block_list; |
| 77 | bool r = c.get_blocks(0, 100 + 2 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW, block_list); |
| 78 | CHECK_TEST_CONDITION(r); |
| 79 | |
| 80 | std::vector<block> blocks(block_list.begin(), block_list.end()); |
| 81 | //CHECK_EQ(expected_blockchain_height, blocks.size()); |
| 82 | if (expected_blockchain_height != blocks.size()) LOG_ERROR ("oops"); |
| 83 | |
| 84 | CHECK_EQ(1, c.get_pool_transactions_count()); |
| 85 | CHECK_EQ(1, c.get_alternative_blocks_count()); |
| 86 | |
| 87 | cryptonote::account_base bob_account = boost::get<cryptonote::account_base>(events[1]); |
| 88 | cryptonote::account_base alice_account = boost::get<cryptonote::account_base>(events[2]); |
| 89 | |
| 90 | std::vector<cryptonote::block> chain; |
| 91 | map_hash2tx_t mtx; |
| 92 | r = find_block_chain(events, chain, mtx, get_block_hash(blocks.back())); |
| 93 | CHECK_TEST_CONDITION(r); |
| 94 | CHECK_EQ(0, get_balance(bob_account, blocks, mtx)); |
| 95 | CHECK_EQ(send_amount - TESTS_DEFAULT_FEE, get_balance(alice_account, blocks, mtx)); |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 |