Electroneum
v2_tests.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 "v2_tests.h"
34 
35 using namespace epee;
36 using namespace crypto;
37 using namespace cryptonote;
38 
39 //----------------------------------------------------------------------------------------------------------------------
40 // Tests
41 
42 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
43 {
44  uint64_t ts_start = 1338224400;
45 
46  GENERATE_ACCOUNT(miner_account);
47  MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
48 
49  // create 4 miner accounts, and have them mine the next 4 blocks
50  cryptonote::account_base miner_accounts[4];
51  const cryptonote::block *prev_block = &blk_0;
53  for (size_t n = 0; n < 4; ++n) {
54  miner_accounts[n].generate();
55  CHECK_AND_ASSERT_MES(generator.construct_block_manually(blocks[n], *prev_block, miner_accounts[n],
57  2, 2, prev_block->timestamp + DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN * 2, // v2 has blocks twice as long
58  crypto::hash(), 0, transaction(), std::vector<crypto::hash>(), 0, 0),
59  false, "Failed to generate block");
60  events.push_back(blocks[n]);
61  prev_block = blocks + n;
62  }
63 
64  // rewind
65  cryptonote::block blk_r;
66  {
67  cryptonote::block blk_last = blocks[3];
68  for (size_t i = 0; i < CRYPTONOTE_MINED_ETN_UNLOCK_WINDOW; ++i)
69  {
71  CHECK_AND_ASSERT_MES(generator.construct_block_manually(blk, blk_last, miner_account,
73  2, 2, blk_last.timestamp + DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN * 2, // v2 has blocks twice as long
74  crypto::hash(), 0, transaction(), std::vector<crypto::hash>(), 0, 0),
75  false, "Failed to generate block");
76  events.push_back(blk);
77  blk_last = blk;
78  }
79  blk_r = blk_last;
80  }
81 
82  // create a tx with the Nth outputs of miner's block reward
83  std::vector<tx_source_entry> sources;
84  for (size_t out_idx_idx = 0; out_idx[out_idx_idx] >= 0; ++out_idx_idx) {
85  sources.resize(sources.size()+1);
86  tx_source_entry& src = sources.back();
87 
88  src.amount = blocks[0].miner_tx.vout[out_idx[out_idx_idx]].amount;
89  std::cout << "using " << print_etn(src.amount) << " output at index " << out_idx[out_idx_idx] << std::endl;
90  for (int m = 0; m <= mixin; ++m) {
91  int idx;
93  idx = m+1; // one out of that size per miner tx, including genesis
94  else
95  idx = 0; // dusty, no other output of that size
96  src.push_output(idx, boost::get<txout_to_key>(blocks[m].miner_tx.vout[out_idx[out_idx_idx]].target).key, src.amount);
97  }
99  src.real_output = 0;
100  src.rct = false;
101  src.real_output_in_tx_index = out_idx[out_idx_idx];
102  }
103 
104  //fill outputs entry
106  td.addr = miner_account.get_keys().m_account_address;
107  td.amount = amount_paid;
108  std::vector<tx_destination_entry> destinations;
109  destinations.push_back(td);
110 
111  transaction tx;
112  bool r = construct_tx(miner_accounts[0].get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tx, 0);
113  CHECK_AND_ASSERT_MES(r, false, "failed to construct transaction");
114  if (!valid)
115  DO_CALLBACK(events, "mark_invalid_tx");
116  events.push_back(tx);
117 
118  return true;
119 }
120 
121 bool gen_v2_tx_mixable_0_mixin::generate(std::vector<test_event_entry>& events) const
122 {
123  const int mixin = 0;
124  const int out_idx[] = {1, -1};
125  const uint64_t amount_paid = 10000;
126  return generate_with(events, out_idx, mixin, amount_paid, false);
127 }
128 
129 bool gen_v2_tx_mixable_low_mixin::generate(std::vector<test_event_entry>& events) const
130 {
131  const int mixin = 1;
132  const int out_idx[] = {1, -1};
133  const uint64_t amount_paid = 10000;
134  return generate_with(events, out_idx, mixin, amount_paid, false);
135 }
136 
137 bool gen_v2_tx_unmixable_only::generate(std::vector<test_event_entry>& events) const
138 {
139  const int mixin = 0;
140  const int out_idx[] = {0, -1};
141  const uint64_t amount_paid = 10000;
142  return generate_with(events, out_idx, mixin, amount_paid, true);
143 }
144 
145 bool gen_v2_tx_unmixable_one::generate(std::vector<test_event_entry>& events) const
146 {
147  const int mixin = 0;
148  const int out_idx[] = {0, 1, -1};
149  const uint64_t amount_paid = 10000;
150  return generate_with(events, out_idx, mixin, amount_paid, true);
151 }
152 
153 bool gen_v2_tx_unmixable_two::generate(std::vector<test_event_entry>& events) const
154 {
155  const int mixin = 0;
156  const int out_idx[] = {0, 1, 2, -1};
157  const uint64_t amount_paid = 10000;
158  return generate_with(events, out_idx, mixin, amount_paid, false);
159 }
160 
161 bool gen_v2_tx_dust::generate(std::vector<test_event_entry>& events) const
162 {
163  const int mixin = 2;
164  const int out_idx[] = {1, -1};
165  const uint64_t amount_paid = 10001;
166  return generate_with(events, out_idx, mixin, amount_paid, false);
167 }
#define CRYPTONOTE_MINED_ETN_UNLOCK_WINDOW
crypto::public_key real_out_tx_key
std::string print_etn(uint64_t amount, unsigned int decimal_point)
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
bool construct_tx(const account_keys &sender_account_keys, std::vector< tx_source_entry > &sources, const std::vector< tx_destination_entry > &destinations, const boost::optional< cryptonote::account_public_address > &change_addr, const std::vector< uint8_t > &extra, transaction &tx, uint64_t unlock_time)
crypto namespace.
Definition: crypto.cpp:58
bool rct
crypto::secret_key generate(const crypto::secret_key &recovery_key=crypto::secret_key(), bool recover=false, bool two_random=false)
Definition: account.cpp:158
uint64_t amount
size_t real_output
bool generate(std::vector< test_event_entry > &events) const
Definition: v2_tests.cpp:121
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
bool is_valid_decomposed_amount(uint64_t amount)
bool generate_with(std::vector< test_event_entry > &events, const int *out_idx, int mixin, uint64_t amount_paid, bool valid) const
Definition: v2_tests.cpp:42
#define MAKE_GENESIS_BLOCK(VEC_EVENTS, BLK_NAME, MINER_ACC, TS)
Definition: chaingen.h:833
unsigned __int64 uint64_t
Definition: stdint.h:136
bool generate(std::vector< test_event_entry > &events) const
Definition: v2_tests.cpp:161
crypto::public_key get_tx_pub_key_from_extra(const std::vector< uint8_t > &tx_extra, size_t pk_index)
uint64_t amount
void push_output(uint64_t idx, const crypto::public_key &k, uint64_t amount)
account_public_address addr
bool generate(std::vector< test_event_entry > &events) const
Definition: v2_tests.cpp:145
size_t real_output_in_tx_index
bool generate(std::vector< test_event_entry > &events) const
Definition: v2_tests.cpp:129
bool generate(std::vector< test_event_entry > &events) const
Definition: v2_tests.cpp:137
POD_CLASS hash
Definition: hash.h:50
#define DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN
#define GENERATE_ACCOUNT(account)
Definition: chaingen.h:801
#define DO_CALLBACK(VEC_EVENTS, CB_NAME)
Definition: chaingen.h:820
bool generate(std::vector< test_event_entry > &events) const
Definition: v2_tests.cpp:153