Electroneum
Loading...
Searching...
No Matches
long_term_block_weight.cpp File Reference
#include "gtest/gtest.h"
#include "cryptonote_core/blockchain.h"
#include "cryptonote_core/tx_pool.h"
#include "cryptonote_core/cryptonote_core.h"
#include "blockchain_db/testdb.h"
Include dependency graph for long_term_block_weight.cpp:

Go to the source code of this file.

Macros

#define IN_UNIT_TESTS
#define TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW   5000
#define PREFIX_WINDOW(hf_version, window)
#define PREFIX(hf_version)

Functions

 TEST (long_term_block_weight, empty_short)
 TEST (long_term_block_weight, identical_before_fork)
 TEST (long_term_block_weight, identical_after_fork_before_long_term_window)
 TEST (long_term_block_weight, ceiling_at_30000000)
 TEST (long_term_block_weight, multi_pop)
 TEST (long_term_block_weight, multiple_updates)
 TEST (long_term_block_weight, pop_invariant_max)
 TEST (long_term_block_weight, pop_invariant_random)
 TEST (long_term_block_weight, long_growth_spike_and_drop)

Macro Definition Documentation

◆ IN_UNIT_TESTS

#define IN_UNIT_TESTS

Definition at line 29 of file long_term_block_weight.cpp.

◆ PREFIX

#define PREFIX ( hf_version)
Value:
#define PREFIX_WINDOW(hf_version, window)
#define TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW

Definition at line 125 of file long_term_block_weight.cpp.

◆ PREFIX_WINDOW

#define PREFIX_WINDOW ( hf_version,
window )
Value:
std::unique_ptr<cryptonote::Blockchain> bc; \
bc.reset(new cryptonote::Blockchain(txpool)); \
struct get_test_options { \
const std::pair<uint8_t, uint64_t> hard_forks[3]; \
const cryptonote::test_options test_options = { \
hard_forks, \
window, \
}; \
get_test_options(): hard_forks{std::make_pair(1, (uint64_t)0), std::make_pair((uint8_t)hf_version, (uint64_t)1), std::make_pair((uint8_t)0, (uint64_t)0)} {} \
} opts; \
cryptonote::Blockchain *blockchain = bc.get(); \
bool r = blockchain->init(new TestDB(), cryptonote::FAKECHAIN, true, &opts.test_options, 0, NULL); \
bool init(BlockchainDB *db, const network_type nettype=MAINNET, bool offline=false, const cryptonote::test_options *test_options=NULL, difficulty_type fixed_difficulty=0, const GetCheckpointsCallback &get_checkpoints=nullptr, bool ignore_bsig=false, bool fallback_to_pow=false)
Initialize the Blockchain state.
Transaction pool, handles transactions which are not part of a block.
Definition tx_pool.h:95
#define ASSERT_TRUE(condition)
Definition gtest.h:1865
STL namespace.
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136

Definition at line 109 of file long_term_block_weight.cpp.

109#define PREFIX_WINDOW(hf_version,window) \
110 std::unique_ptr<cryptonote::Blockchain> bc; \
111 cryptonote::tx_memory_pool txpool(*bc); \
112 bc.reset(new cryptonote::Blockchain(txpool)); \
113 struct get_test_options { \
114 const std::pair<uint8_t, uint64_t> hard_forks[3]; \
115 const cryptonote::test_options test_options = { \
116 hard_forks, \
117 window, \
118 }; \
119 get_test_options(): hard_forks{std::make_pair(1, (uint64_t)0), std::make_pair((uint8_t)hf_version, (uint64_t)1), std::make_pair((uint8_t)0, (uint64_t)0)} {} \
120 } opts; \
121 cryptonote::Blockchain *blockchain = bc.get(); \
122 bool r = blockchain->init(new TestDB(), cryptonote::FAKECHAIN, true, &opts.test_options, 0, NULL); \
123 ASSERT_TRUE(r)

◆ TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW

#define TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW   5000

Definition at line 37 of file long_term_block_weight.cpp.

Function Documentation

◆ TEST() [1/9]

TEST ( long_term_block_weight ,
ceiling_at_30000000  )

Definition at line 173 of file long_term_block_weight.cpp.

174{
176
178 {
179 size_t w = h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V8 : bc->get_current_cumulative_block_weight_limit();
180 uint64_t ltw = bc->get_next_long_term_block_weight(w);
181 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
182 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
183 }
184 ASSERT_EQ(bc->get_current_cumulative_block_weight_median(), 15000000);
185 ASSERT_EQ(bc->get_current_cumulative_block_weight_limit(), 30000000);
186}
#define HF_VERSION_LONG_TERM_BLOCK_WEIGHT
#define CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V8
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1956
#define PREFIX(hf_version)

◆ TEST() [2/9]

TEST ( long_term_block_weight ,
empty_short  )

Definition at line 127 of file long_term_block_weight.cpp.

128{
129 PREFIX(8);
130
131 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
132
133 ASSERT_EQ(bc->get_current_cumulative_block_weight_median(), CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V8);
134 ASSERT_EQ(bc->get_current_cumulative_block_weight_limit(), CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V8 * 2);
135}

◆ TEST() [3/9]

TEST ( long_term_block_weight ,
identical_after_fork_before_long_term_window  )

Definition at line 154 of file long_term_block_weight.cpp.

155{
157
158 if(bc->get_current_hard_fork_version() < HF_VERSION_LONG_TERM_BLOCK_WEIGHT) return;
159
160 for (uint64_t h = 1; h <= TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h)
161 {
162 size_t w = h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : bc->get_current_cumulative_block_weight_limit();
163 uint64_t ltw = bc->get_next_long_term_block_weight(w);
164 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
165 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
166 }
167 for (uint64_t h = 0; h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h)
168 {
169 ASSERT_EQ(bc->get_db().get_block_long_term_weight(h), bc->get_db().get_block_weight(h));
170 }
171}
#define CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5

◆ TEST() [4/9]

TEST ( long_term_block_weight ,
identical_before_fork  )

Definition at line 137 of file long_term_block_weight.cpp.

138{
139 PREFIX(8);
140
141 for (uint64_t h = 1; h < 10 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h)
142 {
143 size_t w = h < CRYPTONOTE_REWARD_BLOCKS_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : bc->get_current_cumulative_block_weight_limit();
144 uint64_t ltw = bc->get_next_long_term_block_weight(w);
145 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
146 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
147 }
148 for (uint64_t h = 0; h < 10 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h)
149 {
150 ASSERT_EQ(bc->get_db().get_block_long_term_weight(h), bc->get_db().get_block_weight(h));
151 }
152}
#define CRYPTONOTE_REWARD_BLOCKS_WINDOW

◆ TEST() [5/9]

TEST ( long_term_block_weight ,
long_growth_spike_and_drop  )

Definition at line 356 of file long_term_block_weight.cpp.

357{
359
360 uint64_t long_term_effective_median_block_weight;
361
362 // constant init
363 for (uint64_t h = 0; h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h)
364 {
366 uint64_t ltw = bc->get_next_long_term_block_weight(w);
367 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
368 ASSERT_TRUE(bc->update_next_cumulative_weight_limit(&long_term_effective_median_block_weight));
369 }
370 ASSERT_EQ(long_term_effective_median_block_weight, 300000);
371
372 // slow 10% yearly for a year (scaled down by 100000 / TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW) -> 8% change
373 for (uint64_t h = 0; h < 365 * 720 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW / 100000; ++h)
374 {
375 //size_t w = bc->get_current_cumulative_block_weight_median() * rate;
376 float t = h / float(365 * 720 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW / 100000);
377 size_t w = 300000 + t * 30000;
378 uint64_t ltw = bc->get_next_long_term_block_weight(w);
379 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
380 ASSERT_TRUE(bc->update_next_cumulative_weight_limit(&long_term_effective_median_block_weight));
381 }
382 ASSERT_GT(long_term_effective_median_block_weight, 300000 * 1.07);
383 ASSERT_LT(long_term_effective_median_block_weight, 300000 * 1.09);
384
385 // spike over three weeks - does not move much
386 for (uint64_t h = 0; h < 21 * 720 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW / 100000; ++h)
387 {
388 size_t w = bc->get_current_cumulative_block_weight_limit();
389 uint64_t ltw = bc->get_next_long_term_block_weight(w);
390 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
391 ASSERT_TRUE(bc->update_next_cumulative_weight_limit(&long_term_effective_median_block_weight));
392 }
393 ASSERT_GT(long_term_effective_median_block_weight, 300000 * 1.07);
394 ASSERT_LT(long_term_effective_median_block_weight, 300000 * 1.09);
395
396 // drop - does not move much
397 for (uint64_t h = 0; h < 21 * 720 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW / 100000; ++h)
398 {
399 size_t w = bc->get_current_cumulative_block_weight_median() * .25;
400 uint64_t ltw = bc->get_next_long_term_block_weight(w);
401 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
402 ASSERT_TRUE(bc->update_next_cumulative_weight_limit(&long_term_effective_median_block_weight));
403 }
404 ASSERT_GT(long_term_effective_median_block_weight, 300000 * 1.07);
405 ASSERT_LT(long_term_effective_median_block_weight, 300000 * 1.09);
406}
#define ASSERT_GT(val1, val2)
Definition gtest.h:1976
#define ASSERT_LT(val1, val2)
Definition gtest.h:1968

◆ TEST() [6/9]

TEST ( long_term_block_weight ,
multi_pop  )

Definition at line 188 of file long_term_block_weight.cpp.

189{
191
192 for (uint64_t h = 1; h <= TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW + 20; ++h)
193 {
194 size_t w = h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V8 : bc->get_current_cumulative_block_weight_limit();
195 uint64_t ltw = bc->get_next_long_term_block_weight(w);
196 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
197 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
198 }
199
200 const uint64_t effective_median = bc->get_current_cumulative_block_weight_median();
201 const uint64_t effective_limit = bc->get_current_cumulative_block_weight_limit();
202
203 const uint64_t num_pop = 4;
204 for (uint64_t h = 0; h < num_pop; ++h)
205 {
206 size_t w = bc->get_current_cumulative_block_weight_limit();
207 uint64_t ltw = bc->get_next_long_term_block_weight(w);
208 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
209 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
210 }
211
213 std::vector<cryptonote::transaction> txs;
214 for (uint64_t h = 0; h < num_pop; ++h)
215 bc->get_db().pop_block(b, txs);
216 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
217
218 ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median());
219 ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit());
220}

◆ TEST() [7/9]

TEST ( long_term_block_weight ,
multiple_updates  )

Definition at line 222 of file long_term_block_weight.cpp.

223{
225
226 for (uint64_t h = 1; h <= 3 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW; ++h)
227 {
228 size_t w = h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V8 : bc->get_current_cumulative_block_weight_limit();
229 uint64_t ltw = bc->get_next_long_term_block_weight(w);
230 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
231 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
232 const uint64_t effective_median = bc->get_current_cumulative_block_weight_median();
233 const uint64_t effective_limit = bc->get_current_cumulative_block_weight_limit();
234 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
235 ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median());
236 ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit());
237 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
238 ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median());
239 ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit());
240 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
241 ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median());
242 ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit());
243 }
244}

◆ TEST() [8/9]

TEST ( long_term_block_weight ,
pop_invariant_max  )

Definition at line 246 of file long_term_block_weight.cpp.

247{
249
250 for (uint64_t h = 1; h < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW - 10; ++h)
251 {
252 size_t w = bc->get_db().height() < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V8 : bc->get_current_cumulative_block_weight_limit();
253 uint64_t ltw = bc->get_next_long_term_block_weight(w);
254 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
255 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
256 }
257
258 for (int n = 0; n < 1000; ++n)
259 {
260 // pop some blocks, then add some more
261 int remove = 1 + (n * 17) % 8;
262 int add = (n * 23) % 12;
263
264 // save long term block weights we're about to remove
265 uint64_t old_ltbw[16], h0 = bc->get_db().height() - remove - 1;
266 for (int i = -2; i < remove; ++i)
267 {
268 old_ltbw[i + 2] = bc->get_db().get_block_long_term_weight(h0 + i);
269 }
270
271 for (int i = 0; i < remove; ++i)
272 {
274 std::vector<cryptonote::transaction> txs;
275 bc->get_db().pop_block(b, txs);
276 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
277 }
278 for (int i = 0; i < add; ++i)
279 {
280 size_t w = bc->get_db().height() < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V8 : bc->get_current_cumulative_block_weight_limit();
281 uint64_t ltw = bc->get_next_long_term_block_weight(w);
282 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, bc->get_db().height(), bc->get_db().height(), {});
283 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
284 }
285
286 // check the new values are the same as the old ones
287 for (int i = -2; i < std::min(add, remove); ++i)
288 {
289 ASSERT_EQ(bc->get_db().get_block_long_term_weight(h0 + i), old_ltbw[i + 2]);
290 }
291 }
292}

◆ TEST() [9/9]

TEST ( long_term_block_weight ,
pop_invariant_random  )

Definition at line 294 of file long_term_block_weight.cpp.

295{
297
298 for (uint64_t h = 1; h < 2 * TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW - 10; ++h)
299 {
300 lcg_seed = bc->get_db().height();
301 uint32_t r = lcg();
302 size_t w = bc->get_db().height() < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V8 : (r % bc->get_current_cumulative_block_weight_limit());
303 uint64_t ltw = bc->get_next_long_term_block_weight(w);
304 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, h, h, {});
305 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
306 }
307
308 for (int n = 0; n < 1000; ++n)
309 {
310 // pop some blocks, then add some more
311 int remove = 1 + (n * 17) % 8;
312 int add = (n * 23) % 123;
313
314 // save long term block weights we're about to remove
315 uint64_t old_ltbw[16], h0 = bc->get_db().height() - remove - 1;
316 for (int i = -2; i < remove; ++i)
317 {
318 old_ltbw[i + 2] = bc->get_db().get_block_long_term_weight(h0 + i);
319 }
320
321 for (int i = 0; i < remove; ++i)
322 {
324 std::vector<cryptonote::transaction> txs;
325 bc->get_db().pop_block(b, txs);
326 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
327 const uint64_t effective_median = bc->get_current_cumulative_block_weight_median();
328 const uint64_t effective_limit = bc->get_current_cumulative_block_weight_limit();
329 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
330 ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median());
331 ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit());
332 }
333 for (int i = 0; i < add; ++i)
334 {
335 lcg_seed = bc->get_db().height();
336 uint32_t r = lcg();
337 size_t w = bc->get_db().height() < TEST_LONG_TERM_BLOCK_WEIGHT_WINDOW ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V8 : (r % bc->get_current_cumulative_block_weight_limit());
338 uint64_t ltw = bc->get_next_long_term_block_weight(w);
339 bc->get_db().add_block(std::make_pair(cryptonote::block(), ""), w, ltw, bc->get_db().height(), bc->get_db().height(), {});
340 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
341 const uint64_t effective_median = bc->get_current_cumulative_block_weight_median();
342 const uint64_t effective_limit = bc->get_current_cumulative_block_weight_limit();
343 ASSERT_TRUE(bc->update_next_cumulative_weight_limit());
344 ASSERT_EQ(effective_median, bc->get_current_cumulative_block_weight_median());
345 ASSERT_EQ(effective_limit, bc->get_current_cumulative_block_weight_limit());
346 }
347
348 // check the new values are the same as the old ones
349 for (int i = -2; i < std::min(add, remove); ++i)
350 {
351 ASSERT_EQ(bc->get_db().get_block_long_term_weight(h0 + i), old_ltbw[i + 2]);
352 }
353 }
354}
unsigned int uint32_t
Definition stdint.h:126