| 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 "chain_switch_1.h" |
| 33 | |
| 34 | using namespace epee; |
| 35 | using namespace cryptonote; |
| 36 | |
| 37 | |
| 38 | gen_chain_switch_1::gen_chain_switch_1() |
| 39 | { |
| 40 | REGISTER_CALLBACK("check_split_not_switched", gen_chain_switch_1::check_split_not_switched); |
| 41 | REGISTER_CALLBACK("check_split_switched", gen_chain_switch_1::check_split_switched); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | //----------------------------------------------------------------------------------------------------- |
| 46 | bool gen_chain_switch_1::generate(std::vector<test_event_entry>& events) const |
| 47 | { |
| 48 | uint64_t ts_start = 1338224400; |
| 49 | /* |
| 50 | (0 )-(1 )-(2 ) -(3 )-(4 ) <- main chain, until 7 isn't connected |
| 51 | \ |-(5 )-(6 )-(7 )| <- alt chain, until 7 isn't connected |
| 52 | |
| 53 | transactions ([n] - tx amount, (m) - block): |
| 54 | (1) : miner -[ 5]-> account_1 ( +5 in main chain, +5 in alt chain) |
| 55 | (3) : miner -[ 7]-> account_2 ( +7 in main chain, +0 in alt chain), tx will be in tx pool after switch |
| 56 | (4), (6): miner -[11]-> account_3 (+11 in main chain, +11 in alt chain) |
| 57 | (5) : miner -[13]-> account_4 ( +0 in main chain, +13 in alt chain), tx will be in tx pool before switch |
| 58 | |
| 59 | transactions orders ([n] - tx amount, (m) - block): |
| 60 | miner -[1], [2]-> account_1: in main chain (3), (3), in alt chain (5), (6) |
| 61 | miner -[1], [2]-> account_2: in main chain (3), (4), in alt chain (5), (5) |
| 62 | miner -[1], [2]-> account_3: in main chain (3), (4), in alt chain (6), (5) |
| 63 | miner -[1], [2]-> account_4: in main chain (4), (3), in alt chain (5), (6) |
| 64 | */ |
| 65 | |
| 66 | GENERATE_ACCOUNT(miner_account); |
| 67 | |
| 68 | // events |
| 69 | MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start); // 0 |
| 70 | MAKE_ACCOUNT(events, recipient_account_1); // 1 |
| 71 | MAKE_ACCOUNT(events, recipient_account_2); // 2 |
| 72 | MAKE_ACCOUNT(events, recipient_account_3); // 3 |
| 73 | MAKE_ACCOUNT(events, recipient_account_4); // 4 |
| 74 | REWIND_BLOCKS(events, blk_0r, blk_0, miner_account) // <N blocks> |
| 75 | MAKE_TX(events, tx_00, miner_account, recipient_account_1, MK_COINS(5), blk_0); // 5 + N |
| 76 | MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_account, tx_00); // 6 + N |
| 77 | MAKE_NEXT_BLOCK(events, blk_2, blk_1, miner_account); // 7 + N |
| 78 | REWIND_BLOCKS(events, blk_2r, blk_2, miner_account) // <N blocks> |
| 79 | |
| 80 | // Transactions to test account balances after switch |
| 81 | MAKE_TX_LIST_START(events, txs_blk_3, miner_account, recipient_account_2, MK_COINS(7), blk_2); // 8 + 2N |
| 82 | MAKE_TX_LIST_START(events, txs_blk_4, miner_account, recipient_account_3, MK_COINS(11), blk_2); // 9 + 2N |
| 83 | MAKE_TX_LIST_START(events, txs_blk_5, miner_account, recipient_account_4, MK_COINS(13), blk_2); // 10 + 2N |
| 84 | std::list<transaction> txs_blk_6; |
| 85 | txs_blk_6.push_back(txs_blk_4.front()); |
| 86 | |
| 87 | // Transactions, that has different order in alt block chains |
| 88 | MAKE_TX_LIST(events, txs_blk_3, miner_account, recipient_account_1, MK_COINS(1), blk_2); // 11 + 2N |
| 89 | txs_blk_5.push_back(txs_blk_3.back()); |
| 90 | MAKE_TX_LIST(events, txs_blk_3, miner_account, recipient_account_1, MK_COINS(2), blk_2); // 12 + 2N |
| 91 | txs_blk_6.push_back(txs_blk_3.back()); |
| 92 | |
| 93 | MAKE_TX_LIST(events, txs_blk_3, miner_account, recipient_account_2, MK_COINS(1), blk_2); // 13 + 2N |
| 94 | txs_blk_5.push_back(txs_blk_3.back()); |
| 95 | MAKE_TX_LIST(events, txs_blk_4, miner_account, recipient_account_2, MK_COINS(2), blk_2); // 14 + 2N |
| 96 | txs_blk_5.push_back(txs_blk_4.back()); |
| 97 | |
| 98 | MAKE_TX_LIST(events, txs_blk_3, miner_account, recipient_account_3, MK_COINS(1), blk_2); // 15 + 2N |
| 99 | txs_blk_6.push_back(txs_blk_3.back()); |
| 100 | MAKE_TX_LIST(events, txs_blk_4, miner_account, recipient_account_3, MK_COINS(2), blk_2); // 16 + 2N |
| 101 | txs_blk_5.push_back(txs_blk_4.back()); |
| 102 | |
| 103 | MAKE_TX_LIST(events, txs_blk_4, miner_account, recipient_account_4, MK_COINS(1), blk_2); // 17 + 2N |
| 104 | txs_blk_5.push_back(txs_blk_4.back()); |
| 105 | MAKE_TX_LIST(events, txs_blk_3, miner_account, recipient_account_4, MK_COINS(2), blk_2); // 18 + 2N |
| 106 | txs_blk_6.push_back(txs_blk_3.back()); |
| 107 | |
| 108 | MAKE_NEXT_BLOCK_TX_LIST(events, blk_3, blk_2r, miner_account, txs_blk_3); // 19 + 2N |
| 109 | MAKE_NEXT_BLOCK_TX_LIST(events, blk_4, blk_3, miner_account, txs_blk_4); // 20 + 2N |
| 110 | //split |
| 111 | MAKE_NEXT_BLOCK_TX_LIST(events, blk_5, blk_2r, miner_account, txs_blk_5); // 22 + 2N |
| 112 | MAKE_NEXT_BLOCK_TX_LIST(events, blk_6, blk_5, miner_account, txs_blk_6); // 23 + 2N |
| 113 | DO_CALLBACK(events, "check_split_not_switched"); // 21 + 2N |
| 114 | MAKE_NEXT_BLOCK(events, blk_7, blk_6, miner_account); // 24 + 2N |
| 115 | DO_CALLBACK(events, "check_split_switched"); // 25 + 2N |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | //----------------------------------------------------------------------------------------------------- |
| 122 | bool gen_chain_switch_1::check_split_not_switched(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events) |
| 123 | { |
| 124 | DEFINE_TESTS_ERROR_CONTEXT("gen_chain_switch_1::check_split_not_switched"); |
| 125 | |
| 126 | m_recipient_account_1 = boost::get<account_base>(events[1]); |
| 127 | m_recipient_account_2 = boost::get<account_base>(events[2]); |
| 128 | m_recipient_account_3 = boost::get<account_base>(events[3]); |
| 129 | m_recipient_account_4 = boost::get<account_base>(events[4]); |
| 130 | |
| 131 | std::vector<block> blocks; |
| 132 | bool r = c.get_blocks(0, 10000, blocks); |
| 133 | CHECK_TEST_CONDITION(r); |
| 134 | CHECK_EQ(5 + 2 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW, blocks.size()); |
| 135 | CHECK_TEST_CONDITION(blocks.back() == boost::get<block>(events[20 + 2 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW])); // blk_4 |
| 136 | |
| 137 | CHECK_EQ(2, c.get_alternative_blocks_count()); |
| 138 | |
| 139 | std::vector<cryptonote::block> chain; |
| 140 | map_hash2tx_t mtx; |
| 141 | r = find_block_chain(events, chain, mtx, get_block_hash(blocks.back())); |
| 142 | CHECK_TEST_CONDITION(r); |
| 143 | CHECK_EQ(MK_COINS(8), get_balance(m_recipient_account_1, chain, mtx)); |
| 144 | CHECK_EQ(MK_COINS(10), get_balance(m_recipient_account_2, chain, mtx)); |
| 145 | CHECK_EQ(MK_COINS(14), get_balance(m_recipient_account_3, chain, mtx)); |
| 146 | CHECK_EQ(MK_COINS(3), get_balance(m_recipient_account_4, chain, mtx)); |
| 147 | |
| 148 | std::vector<transaction> tx_pool; |
| 149 | r = c.get_pool_transactions(tx_pool); |
| 150 | CHECK_TEST_CONDITION(r); |
| 151 | CHECK_EQ(1, tx_pool.size()); |
| 152 | |
| 153 | std::vector<size_t> tx_outs; |
| 154 | uint64_t transfered; |
| 155 | lookup_acc_outs(m_recipient_account_4.get_keys(), tx_pool.front(), get_tx_pub_key_from_extra(tx_pool.front()), get_additional_tx_pub_keys_from_extra(tx_pool.front()), tx_outs, transfered); |
| 156 | CHECK_EQ(MK_COINS(13), transfered); |
| 157 | |
| 158 | m_chain_1.swap(blocks); |
| 159 | m_tx_pool.swap(tx_pool); |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | //----------------------------------------------------------------------------------------------------- |
| 165 | bool gen_chain_switch_1::check_split_switched(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events) |
| 166 | { |
| 167 | DEFINE_TESTS_ERROR_CONTEXT("gen_chain_switch_1::check_split_switched"); |
| 168 | |
| 169 | std::vector<block> blocks; |
| 170 | bool r = c.get_blocks(0, 10000, blocks); |
| 171 | CHECK_TEST_CONDITION(r); |
| 172 | CHECK_EQ(6 + 2 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW, blocks.size()); |
| 173 | auto it = blocks.end(); |
| 174 | --it; --it; --it; |
| 175 | CHECK_TEST_CONDITION(std::equal(blocks.begin(), it, m_chain_1.begin())); |
| 176 | CHECK_TEST_CONDITION(blocks.back() == boost::get<block>(events[24 + 2 * CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW])); // blk_7 |
| 177 | |
| 178 | std::vector<block> alt_blocks; |
| 179 | r = c.get_alternative_blocks(alt_blocks); |
| 180 | CHECK_TEST_CONDITION(r); |
| 181 | CHECK_EQ(2, c.get_alternative_blocks_count()); |
| 182 | |
| 183 | // Some blocks that were in main chain are in alt chain now |
| 184 | BOOST_FOREACH(block b, alt_blocks) |
| 185 | { |
| 186 | CHECK_TEST_CONDITION(m_chain_1.end() != std::find(m_chain_1.begin(), m_chain_1.end(), b)); |
| 187 | } |
| 188 | |
| 189 | std::vector<cryptonote::block> chain; |
| 190 | map_hash2tx_t mtx; |
| 191 | r = find_block_chain(events, chain, mtx, get_block_hash(blocks.back())); |
| 192 | CHECK_TEST_CONDITION(r); |
| 193 | CHECK_EQ(MK_COINS(8), get_balance(m_recipient_account_1, chain, mtx)); |
| 194 | CHECK_EQ(MK_COINS(3), get_balance(m_recipient_account_2, chain, mtx)); |
| 195 | CHECK_EQ(MK_COINS(14), get_balance(m_recipient_account_3, chain, mtx)); |
| 196 | CHECK_EQ(MK_COINS(16), get_balance(m_recipient_account_4, chain, mtx)); |
| 197 | |
| 198 | std::vector<transaction> tx_pool; |
| 199 | r = c.get_pool_transactions(tx_pool); |
| 200 | CHECK_TEST_CONDITION(r); |
| 201 | CHECK_EQ(1, tx_pool.size()); |
| 202 | CHECK_TEST_CONDITION(!(tx_pool.front() == m_tx_pool.front())); |
| 203 | |
| 204 | std::vector<size_t> tx_outs; |
| 205 | uint64_t transfered; |
| 206 | lookup_acc_outs(m_recipient_account_2.get_keys(), tx_pool.front(), tx_outs, transfered); |
| 207 | CHECK_EQ(MK_COINS(7), transfered); |
| 208 | |
| 209 | return true; |
| 210 | } |