Debian Codegraph @

tests/core_tests/block_reward.cpp

raw symbols SCIP
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 "block_reward.h"
33
34 using namespace epee;
35 using namespace cryptonote;
36
37 namespace
38 {
39 bool construct_miner_tx_by_weight(transaction& miner_tx, uint64_t height, uint64_t already_generated_coins,
40 const account_public_address& miner_address, std::vector<size_t>& block_weights, size_t target_tx_weight,
41 size_t target_block_weight, uint64_t fee = 0)
42 {
43 if (!construct_miner_tx(height, misc_utils::median(block_weights), already_generated_coins, target_block_weight, fee, miner_address, miner_tx))
44 return false;
45
46 size_t current_weight = get_transaction_weight(miner_tx);
47 size_t try_count = 0;
48 while (target_tx_weight != current_weight)
49 {
50 ++try_count;
51 if (10 < try_count)
52 return false;
53
54 if (target_tx_weight < current_weight)
55 {
56 size_t diff = current_weight - target_tx_weight;
57 if (diff <= miner_tx.extra.size())
58 miner_tx.extra.resize(miner_tx.extra.size() - diff);
59 else
60 return false;
61 }
62 else
63 {
64 size_t diff = target_tx_weight - current_weight;
65 miner_tx.extra.resize(miner_tx.extra.size() + diff);
66 }
67
68 current_weight = get_transaction_weight(miner_tx);
69 }
70
71 return true;
72 }
73
74 bool construct_max_weight_block(test_generator& generator, block& blk, const block& blk_prev, const account_base& miner_account,
75 size_t median_block_count = CRYPTONOTE_REWARD_BLOCKS_WINDOW)
76 {
77 std::vector<size_t> block_weights;
78 generator.get_last_n_block_weights(block_weights, get_block_hash(blk_prev), median_block_count);
79
80 size_t median = misc_utils::median(block_weights);
81 median = std::max(median, static_cast<size_t>(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1));
82
83 transaction miner_tx;
84 bool r = construct_miner_tx_by_weight(miner_tx, get_block_height(blk_prev) + 1, generator.get_already_generated_coins(blk_prev),
85 miner_account.get_keys().m_account_address, block_weights, 2 * median, 2 * median);
86 if (!r)
87 return false;
88
89 return generator.construct_block_manually(blk, blk_prev, miner_account, test_generator::bf_miner_tx, 0, 0, 0,
90 crypto::hash(), 0, miner_tx);
91 }
92
93 bool rewind_blocks(std::vector<test_event_entry>& events, test_generator& generator, block& blk, const block& blk_prev,
94 const account_base& miner_account, size_t block_count)
95 {
96 blk = blk_prev;
97 for (size_t i = 0; i < block_count; ++i)
98 {
99 block blk_i;
100 if (!construct_max_weight_block(generator, blk_i, blk, miner_account))
101 return false;
102
103 events.push_back(blk_i);
104 blk = blk_i;
105 }
106
107 return true;
108 }
109
110 uint64_t get_tx_out_amount(const transaction& tx)
111 {
112 uint64_t amount = 0;
113 BOOST_FOREACH(auto& o, tx.vout)
114 amount += o.amount;
115 return amount;
116 }
117 }
118
119 gen_block_reward::gen_block_reward()
120 : m_invalid_block_index(0)
121 {
122 REGISTER_CALLBACK_METHOD(gen_block_reward, mark_invalid_block);
123 REGISTER_CALLBACK_METHOD(gen_block_reward, mark_checked_block);
124 REGISTER_CALLBACK_METHOD(gen_block_reward, check_block_rewards);
125 }
126
127 bool gen_block_reward::generate(std::vector<test_event_entry>& events) const
128 {
129 uint64_t ts_start = 1338224400;
130
131 GENERATE_ACCOUNT(miner_account);
132 MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
133 DO_CALLBACK(events, "mark_checked_block");
134 MAKE_ACCOUNT(events, bob_account);
135
136 // Test: miner transactions without outputs (block reward == 0)
137 block blk_0r;
138 if (!rewind_blocks(events, generator, blk_0r, blk_0, miner_account, CRYPTONOTE_REWARD_BLOCKS_WINDOW))
139 return false;
140
141 // Test: block reward is calculated using median of the latest CRYPTONOTE_REWARD_BLOCKS_WINDOW blocks
142 DO_CALLBACK(events, "mark_invalid_block");
143 block blk_1_bad_1;
144 if (!construct_max_weight_block(generator, blk_1_bad_1, blk_0r, miner_account, CRYPTONOTE_REWARD_BLOCKS_WINDOW + 1))
145 return false;
146 events.push_back(blk_1_bad_1);
147
148 DO_CALLBACK(events, "mark_invalid_block");
149 block blk_1_bad_2;
150 if (!construct_max_weight_block(generator, blk_1_bad_2, blk_0r, miner_account, CRYPTONOTE_REWARD_BLOCKS_WINDOW - 1))
151 return false;
152 events.push_back(blk_1_bad_2);
153
154 block blk_1;
155 if (!construct_max_weight_block(generator, blk_1, blk_0r, miner_account))
156 return false;
157 events.push_back(blk_1);
158
159 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
160
161 MAKE_NEXT_BLOCK(events, blk_2, blk_1, miner_account);
162 DO_CALLBACK(events, "mark_checked_block");
163 MAKE_NEXT_BLOCK(events, blk_3, blk_2, miner_account);
164 DO_CALLBACK(events, "mark_checked_block");
165 MAKE_NEXT_BLOCK(events, blk_4, blk_3, miner_account);
166 DO_CALLBACK(events, "mark_checked_block");
167 MAKE_NEXT_BLOCK(events, blk_5, blk_4, miner_account);
168 DO_CALLBACK(events, "mark_checked_block");
169
170 block blk_5r;
171 if (!rewind_blocks(events, generator, blk_5r, blk_5, miner_account, CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW))
172 return false;
173
174 // Test: fee increases block reward
175 transaction tx_0(construct_tx_with_fee(events, blk_5, miner_account, bob_account, MK_COINS(1), 3 * TESTS_DEFAULT_FEE));
176 MAKE_NEXT_BLOCK_TX1(events, blk_6, blk_5r, miner_account, tx_0);
177 DO_CALLBACK(events, "mark_checked_block");
178
179 // Test: fee from all block transactions increase block reward
180 std::list<transaction> txs_0;
181 txs_0.push_back(construct_tx_with_fee(events, blk_5, miner_account, bob_account, MK_COINS(1), 5 * TESTS_DEFAULT_FEE));
182 txs_0.push_back(construct_tx_with_fee(events, blk_5, miner_account, bob_account, MK_COINS(1), 7 * TESTS_DEFAULT_FEE));
183 MAKE_NEXT_BLOCK_TX_LIST(events, blk_7, blk_6, miner_account, txs_0);
184 DO_CALLBACK(events, "mark_checked_block");
185
186 // Test: block reward == transactions fee
187 {
188 transaction tx_1 = construct_tx_with_fee(events, blk_5, miner_account, bob_account, MK_COINS(1), 11 * TESTS_DEFAULT_FEE);
189 transaction tx_2 = construct_tx_with_fee(events, blk_5, miner_account, bob_account, MK_COINS(1), 13 * TESTS_DEFAULT_FEE);
190 size_t txs_1_weight = get_transaction_weight(tx_1) + get_transaction_weight(tx_2);
191 uint64_t txs_fee = get_tx_fee(tx_1) + get_tx_fee(tx_2);
192
193 std::vector<size_t> block_weights;
194 generator.get_last_n_block_weights(block_weights, get_block_hash(blk_7), CRYPTONOTE_REWARD_BLOCKS_WINDOW);
195 size_t median = misc_utils::median(block_weights);
196
197 transaction miner_tx;
198 bool r = construct_miner_tx_by_weight(miner_tx, get_block_height(blk_7) + 1, generator.get_already_generated_coins(blk_7),
199 miner_account.get_keys().m_account_address, block_weights, 2 * median - txs_1_weight, 2 * median, txs_fee);
200 if (!r)
201 return false;
202
203 std::vector<crypto::hash> txs_1_hashes;
204 txs_1_hashes.push_back(get_transaction_hash(tx_1));
205 txs_1_hashes.push_back(get_transaction_hash(tx_2));
206
207 block blk_8;
208 generator.construct_block_manually(blk_8, blk_7, miner_account, test_generator::bf_miner_tx | test_generator::bf_tx_hashes,
209 0, 0, 0, crypto::hash(), 0, miner_tx, txs_1_hashes, txs_1_weight);
210
211 events.push_back(blk_8);
212 DO_CALLBACK(events, "mark_checked_block");
213 }
214
215 DO_CALLBACK(events, "check_block_rewards");
216
217 return true;
218 }
219
220 bool gen_block_reward::check_block_verification_context(const cryptonote::block_verification_context& bvc, size_t event_idx, const cryptonote::block& /*blk*/)
221 {
222 if (m_invalid_block_index == event_idx)
223 {
224 m_invalid_block_index = 0;
225 return bvc.m_verifivation_failed;
226 }
227 else
228 {
229 return !bvc.m_verifivation_failed;
230 }
231 }
232
233 bool gen_block_reward::mark_invalid_block(cryptonote::core& /*c*/, size_t ev_index, const std::vector<test_event_entry>& /*events*/)
234 {
235 m_invalid_block_index = ev_index + 1;
236 return true;
237 }
238
239 bool gen_block_reward::mark_checked_block(cryptonote::core& /*c*/, size_t ev_index, const std::vector<test_event_entry>& /*events*/)
240 {
241 m_checked_blocks_indices.push_back(ev_index - 1);
242 return true;
243 }
244
245 bool gen_block_reward::check_block_rewards(cryptonote::core& /*c*/, size_t /*ev_index*/, const std::vector<test_event_entry>& events)
246 {
247 DEFINE_TESTS_ERROR_CONTEXT("gen_block_reward_without_txs::check_block_rewards");
248
249 std::array<uint64_t, 7> blk_rewards;
250 blk_rewards[0] = MONEY_SUPPLY >> EMISSION_SPEED_FACTOR_PER_MINUTE;
251 uint64_t cumulative_reward = blk_rewards[0];
252 for (size_t i = 1; i < blk_rewards.size(); ++i)
253 {
254 blk_rewards[i] = (MONEY_SUPPLY - cumulative_reward) >> EMISSION_SPEED_FACTOR_PER_MINUTE;
255 cumulative_reward += blk_rewards[i];
256 }
257
258 for (size_t i = 0; i < 5; ++i)
259 {
260 block blk_i = boost::get<block>(events[m_checked_blocks_indices[i]]);
261 CHECK_EQ(blk_rewards[i], get_tx_out_amount(blk_i.miner_tx));
262 }
263
264 block blk_n1 = boost::get<block>(events[m_checked_blocks_indices[5]]);
265 CHECK_EQ(blk_rewards[5] + 3 * TESTS_DEFAULT_FEE, get_tx_out_amount(blk_n1.miner_tx));
266
267 block blk_n2 = boost::get<block>(events[m_checked_blocks_indices[6]]);
268 CHECK_EQ(blk_rewards[6] + (5 + 7) * TESTS_DEFAULT_FEE, get_tx_out_amount(blk_n2.miner_tx));
269
270 block blk_n3 = boost::get<block>(events[m_checked_blocks_indices[7]]);
271 CHECK_EQ((11 + 13) * TESTS_DEFAULT_FEE, get_tx_out_amount(blk_n3.miner_tx));
272
273 return true;
274 }