Debian Codegraph @

tests/core_tests/block_validation.h

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 #pragma once
32 #include "chaingen.h"
33
34 template<size_t invalid_block_idx = 0>
35 class gen_block_verification_base : public test_chain_unit_base
36 {
37 public:
38 gen_block_verification_base()
39 {
40 REGISTER_CALLBACK("check_block_purged", gen_block_verification_base<invalid_block_idx>::check_block_purged);
41 }
42
43 bool check_block_verification_context(const cryptonote::block_verification_context& bvc, size_t event_idx, const cryptonote::block& /*blk*/)
44 {
45 if (invalid_block_idx == event_idx)
46 return bvc.m_verifivation_failed;
47 else
48 return !bvc.m_verifivation_failed;
49 }
50
51 bool check_block_purged(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events)
52 {
53 DEFINE_TESTS_ERROR_CONTEXT("gen_block_verification_base::check_block_purged");
54
55 CHECK_TEST_CONDITION(invalid_block_idx < ev_index);
56 CHECK_EQ(0, c.get_pool_transactions_count());
57 CHECK_EQ(invalid_block_idx, c.get_current_blockchain_height());
58
59 return true;
60 }
61 };
62
63 template<size_t expected_blockchain_height>
64 struct gen_block_accepted_base : public test_chain_unit_base
65 {
66 gen_block_accepted_base()
67 {
68 REGISTER_CALLBACK("check_block_accepted", gen_block_accepted_base::check_block_accepted);
69 }
70
71 bool check_block_accepted(cryptonote::core& c, size_t /*ev_index*/, const std::vector<test_event_entry>& /*events*/)
72 {
73 DEFINE_TESTS_ERROR_CONTEXT("gen_block_accepted_base::check_block_accepted");
74
75 CHECK_EQ(0, c.get_pool_transactions_count());
76 CHECK_EQ(expected_blockchain_height, c.get_current_blockchain_height());
77
78 return true;
79 }
80 };
81
82 struct gen_block_big_major_version : public gen_block_verification_base<1>
83 {
84 bool generate(std::vector<test_event_entry>& events) const;
85 };
86
87 struct gen_block_big_minor_version : public gen_block_accepted_base<2>
88 {
89 bool generate(std::vector<test_event_entry>& events) const;
90 };
91
92 struct gen_block_ts_not_checked : public gen_block_accepted_base<BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW>
93 {
94 bool generate(std::vector<test_event_entry>& events) const;
95 };
96
97 struct gen_block_ts_in_past : public gen_block_verification_base<BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW>
98 {
99 bool generate(std::vector<test_event_entry>& events) const;
100 };
101
102 struct gen_block_ts_in_future : public gen_block_verification_base<1>
103 {
104 bool generate(std::vector<test_event_entry>& events) const;
105 };
106
107 struct gen_block_invalid_prev_id : public gen_block_verification_base<1>
108 {
109 bool generate(std::vector<test_event_entry>& events) const;
110 bool check_block_verification_context(const cryptonote::block_verification_context& bvc, size_t event_idx, const cryptonote::block& /*blk*/);
111 };
112
113 struct gen_block_invalid_nonce : public gen_block_verification_base<3>
114 {
115 bool generate(std::vector<test_event_entry>& events) const;
116 };
117
118 struct gen_block_no_miner_tx : public gen_block_verification_base<1>
119 {
120 bool generate(std::vector<test_event_entry>& events) const;
121 };
122
123 struct gen_block_unlock_time_is_low : public gen_block_verification_base<1>
124 {
125 bool generate(std::vector<test_event_entry>& events) const;
126 };
127
128 struct gen_block_unlock_time_is_high : public gen_block_verification_base<1>
129 {
130 bool generate(std::vector<test_event_entry>& events) const;
131 };
132
133 struct gen_block_unlock_time_is_timestamp_in_past : public gen_block_verification_base<1>
134 {
135 bool generate(std::vector<test_event_entry>& events) const;
136 };
137
138 struct gen_block_unlock_time_is_timestamp_in_future : public gen_block_verification_base<1>
139 {
140 bool generate(std::vector<test_event_entry>& events) const;
141 };
142
143 struct gen_block_height_is_low : public gen_block_verification_base<1>
144 {
145 bool generate(std::vector<test_event_entry>& events) const;
146 };
147
148 struct gen_block_height_is_high : public gen_block_verification_base<1>
149 {
150 bool generate(std::vector<test_event_entry>& events) const;
151 };
152
153 struct gen_block_miner_tx_has_2_tx_gen_in : public gen_block_verification_base<1>
154 {
155 bool generate(std::vector<test_event_entry>& events) const;
156 };
157
158 struct gen_block_miner_tx_has_2_in : public gen_block_verification_base<CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW + 1>
159 {
160 bool generate(std::vector<test_event_entry>& events) const;
161 };
162
163 struct gen_block_miner_tx_with_txin_to_key : public gen_block_verification_base<CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW + 2>
164 {
165 bool generate(std::vector<test_event_entry>& events) const;
166 };
167
168 struct gen_block_miner_tx_out_is_small : public gen_block_verification_base<1>
169 {
170 bool generate(std::vector<test_event_entry>& events) const;
171 };
172
173 struct gen_block_miner_tx_out_is_big : public gen_block_verification_base<1>
174 {
175 bool generate(std::vector<test_event_entry>& events) const;
176 };
177
178 struct gen_block_miner_tx_has_no_out : public gen_block_verification_base<1>
179 {
180 bool generate(std::vector<test_event_entry>& events) const;
181 };
182
183 struct gen_block_miner_tx_has_out_to_alice : public gen_block_accepted_base<2>
184 {
185 bool generate(std::vector<test_event_entry>& events) const;
186 };
187
188 struct gen_block_has_invalid_tx : public gen_block_verification_base<1>
189 {
190 bool generate(std::vector<test_event_entry>& events) const;
191 };
192
193 struct gen_block_is_too_big : public gen_block_verification_base<1>
194 {
195 bool generate(std::vector<test_event_entry>& events) const;
196 };
197
198 struct gen_block_invalid_binary_format : public test_chain_unit_base
199 {
200 gen_block_invalid_binary_format();
201 bool generate(std::vector<test_event_entry>& events) const;
202 bool check_block_verification_context(const cryptonote::block_verification_context& bvc, size_t event_idx, const cryptonote::block& /*blk*/);
203 bool check_all_blocks_purged(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
204 bool corrupt_blocks_boundary(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
205
206 private:
207 size_t m_corrupt_blocks_begin_idx;
208 };
209
210 struct gen_block_late_v1_coinbase_tx : public gen_block_verification_base<1>
211 {
212 bool generate(std::vector<test_event_entry>& events) const;
213 };
214 template<>
215 struct get_test_options<gen_block_late_v1_coinbase_tx> {
216 const std::pair<uint8_t, uint64_t> hard_forks[3] = {std::make_pair(1, 0), std::make_pair(HF_VERSION_MIN_V2_COINBASE_TX, 1), std::make_pair(0, 0)};
217 const cryptonote::test_options test_options = {
218 hard_forks, 0
219 };
220 };
221
222 struct gen_block_low_coinbase : public gen_block_verification_base<1>
223 {
224 bool generate(std::vector<test_event_entry>& events) const;
225 };
226 template<>
227 struct get_test_options<gen_block_low_coinbase> {
228 const std::pair<uint8_t, uint64_t> hard_forks[3] = {std::make_pair(1, 0), std::make_pair(HF_VERSION_EXACT_COINBASE, 1), std::make_pair(0, 0)};
229 const cryptonote::test_options test_options = {
230 hard_forks, 0
231 };
232 };
233
234 struct gen_block_miner_tx_out_has_no_view_tag_before_hf_view_tags : public gen_block_accepted_base<2>
235 {
236 bool generate(std::vector<test_event_entry>& events) const;
237 };
238
239 struct gen_block_miner_tx_out_has_no_view_tag_from_hf_view_tags : public gen_block_verification_base<1>
240 {
241 bool generate(std::vector<test_event_entry>& events) const;
242 };
243 template<>
244 struct get_test_options<gen_block_miner_tx_out_has_no_view_tag_from_hf_view_tags> {
245 const std::pair<uint8_t, uint64_t> hard_forks[3] = {std::make_pair(1, 0), std::make_pair(HF_VERSION_VIEW_TAGS+1, 1), std::make_pair(0, 0)};
246 const cryptonote::test_options test_options = {
247 hard_forks, 0
248 };
249 };
250
251 struct gen_block_miner_tx_out_has_view_tag_before_hf_view_tags : public gen_block_verification_base<1>
252 {
253 bool generate(std::vector<test_event_entry>& events) const;
254 };
255
256 struct gen_block_miner_tx_out_has_view_tag_from_hf_view_tags : public gen_block_accepted_base<2>
257 {
258 bool generate(std::vector<test_event_entry>& events) const;
259 };
260 template<>
261 struct get_test_options<gen_block_miner_tx_out_has_view_tag_from_hf_view_tags> {
262 const std::pair<uint8_t, uint64_t> hard_forks[3] = {std::make_pair(1, 0), std::make_pair(HF_VERSION_VIEW_TAGS, 1), std::make_pair(0, 0)};
263 const cryptonote::test_options test_options = {
264 hard_forks, 0
265 };
266 };