Electroneum
double_spend.cpp
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #include "chaingen.h"
33 #include "double_spend.h"
34 
35 using namespace epee;
36 using namespace cryptonote;
37 
38 
39 //======================================================================================================================
40 
42 {
44 }
45 
46 bool gen_double_spend_in_different_chains::generate(std::vector<test_event_entry>& events) const
47 {
49 
51  MAKE_TX(events, tx_1, bob_account, alice_account, send_amount / 2 - TESTS_DEFAULT_FEE, blk_1);
52  events.pop_back();
53  MAKE_TX(events, tx_2, bob_account, alice_account, send_amount - TESTS_DEFAULT_FEE, blk_1);
54  events.pop_back();
55 
56  // Main chain
57  events.push_back(tx_1);
58  MAKE_NEXT_BLOCK_TX1(events, blk_2, blk_1r, miner_account, tx_1);
59 
60  // Alternative chain
61  events.push_back(tx_2);
62  MAKE_NEXT_BLOCK_TX1(events, blk_3, blk_1r, miner_account, tx_2);
63  // Switch to alternative chain
64  MAKE_NEXT_BLOCK(events, blk_4, blk_3, miner_account);
65  //CHECK_AND_NO_ASSERT_MES(expected_blockchain_height == get_block_height(blk_4) + 1, false, "expected_blockchain_height has invalid value");
66  if ((expected_blockchain_height != get_block_height(blk_4) + 1)) LOG_ERROR("oops");
67 
68  DO_CALLBACK(events, "check_double_spend");
69 
70  return true;
71 }
72 
73 bool gen_double_spend_in_different_chains::check_double_spend(cryptonote::core& c, size_t /*ev_index*/, const std::vector<test_event_entry>& events)
74 {
75  DEFINE_TESTS_ERROR_CONTEXT("gen_double_spend_in_different_chains::check_double_spend");
76 
77  std::vector<block> block_list;
78  bool r = c.get_blocks(0, 100 + 2 * CRYPTONOTE_MINED_ETN_UNLOCK_WINDOW, block_list);
80 
81  std::vector<block> blocks(block_list.begin(), block_list.end());
82  //CHECK_EQ(expected_blockchain_height, blocks.size());
83  if (expected_blockchain_height != blocks.size()) LOG_ERROR ("oops");
84 
87 
88  cryptonote::account_base bob_account = boost::get<cryptonote::account_base>(events[1]);
89  cryptonote::account_base alice_account = boost::get<cryptonote::account_base>(events[2]);
90 
91  std::vector<cryptonote::block> chain;
92  map_hash2tx_t mtx;
93  r = find_block_chain(events, chain, mtx, get_block_hash(blocks.back()));
95  CHECK_EQ(0, get_balance(bob_account, blocks, mtx));
96  CHECK_EQ(send_amount - TESTS_DEFAULT_FEE, get_balance(alice_account, blocks, mtx));
97 
98  return true;
99 }
#define CRYPTONOTE_MINED_ETN_UNLOCK_WINDOW
#define MAKE_NEXT_BLOCK_TX1(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC, TX1)
Definition: chaingen.h:849
bool find_block_chain(const std::vector< test_event_entry > &events, std::vector< cryptonote::block > &blockchain, map_hash2tx_t &mtx, const crypto::hash &head)
Definition: chaingen.cpp:1072
#define INIT_DOUBLE_SPEND_TEST()
Definition: double_spend.h:141
#define MAKE_NEXT_BLOCK(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC)
Definition: chaingen.h:839
#define TESTS_DEFAULT_FEE
Definition: chaingen.h:1061
uint64_t get_balance(const cryptonote::account_base &addr, const std::vector< cryptonote::block > &blockchain, const map_hash2tx_t &mtx)
Definition: chaingen.cpp:940
#define SET_EVENT_VISITOR_SETT(VEC_EVENTS, SETT, VAL)
Definition: chaingen.h:951
#define CHECK_TEST_CONDITION(cond)
Definition: chaingen.h:1057
bool get_blocks(uint64_t start_offset, size_t count, std::vector< std::pair< cryptonote::blobdata, block >> &blocks, std::vector< cryptonote::blobdata > &txs) const
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
std::unordered_map< crypto::hash, const cryptonote::transaction * > map_hash2tx_t
Definition: chaingen.h:163
handles core cryptonote functionality
bool generate(std::vector< test_event_entry > &events) const
#define REGISTER_CALLBACK_METHOD(CLASS, METHOD)
Definition: chaingen.h:830
size_t get_pool_transactions_count() const
get the total number of transactions in the pool
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
size_t get_alternative_blocks_count() const
returns the number of alternative blocks stored
#define MAKE_TX(VEC_EVENTS, TX_NAME, FROM, TO, AMOUNT, HEAD)
Definition: chaingen.h:903
crypto::hash get_block_hash(uint64_t height)
#define CHECK_EQ(v1, v2)
Definition: chaingen.h:1058
#define DEFINE_TESTS_ERROR_CONTEXT(text)
Definition: chaingen.h:1056
#define DO_CALLBACK(VEC_EVENTS, CB_NAME)
Definition: chaingen.h:820
bool check_double_spend(cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
uint64_t get_block_height(const block &b)