Electroneum
Loading...
Searching...
No Matches
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
35using namespace epee;
36using namespace crypto;
37using namespace cryptonote;
38
39//----------------------------------------------------------------------------------------------------------------------
40// Tests
41
42bool 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
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
121bool 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
129bool 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
137bool 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
145bool 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
153bool 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
161bool 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 MAKE_GENESIS_BLOCK(VEC_EVENTS, BLK_NAME, MINER_ACC, TS)
Definition chaingen.h:833
#define DO_CALLBACK(VEC_EVENTS, CB_NAME)
Definition chaingen.h:820
#define GENERATE_ACCOUNT(account)
Definition chaingen.h:801
crypto::secret_key generate(const crypto::secret_key &recovery_key=crypto::secret_key(), bool recover=false, bool two_random=false)
Definition account.cpp:158
#define DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN
#define CRYPTONOTE_MINED_ETN_UNLOCK_WINDOW
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
crypto namespace.
Definition crypto.cpp:58
POD_CLASS hash
Definition hash.h:50
Holds cryptonote related classes and helpers.
Definition ban.cpp:40
bool is_valid_decomposed_amount(uint64_t amount)
crypto::public_key get_tx_pub_key_from_extra(const std::vector< uint8_t > &tx_extra, size_t pk_index)
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)
std::string print_etn(uint64_t amount, unsigned int decimal_point)
unsigned __int64 uint64_t
Definition stdint.h:136
uint64_t amount
account_public_address addr
crypto::public_key real_out_tx_key
uint64_t amount
bool rct
size_t real_output
void push_output(uint64_t idx, const crypto::public_key &k, uint64_t amount)
size_t real_output_in_tx_index
bool generate(std::vector< test_event_entry > &events) const
Definition v2_tests.cpp:161
bool generate(std::vector< test_event_entry > &events) const
Definition v2_tests.cpp:121
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:145
bool generate(std::vector< test_event_entry > &events) const
Definition v2_tests.cpp:137
bool generate(std::vector< test_event_entry > &events) const
Definition v2_tests.cpp:153
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