13#include <boost/test/unit_test.hpp>
15#define SKIPLIST_LENGTH 300000
24 vIndex[i].nHeight = i;
25 vIndex[i].pprev = (i == 0) ?
nullptr : &vIndex[i - 1];
26 vIndex[i].BuildSkip();
31 BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]);
38 for (
int i=0; i < 1000; i++) {
40 int to = m_rng.randrange(from + 1);
43 BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
44 BOOST_CHECK(vIndex[from].GetAncestor(0) == vIndex.data());
51 std::vector<uint256> vHashMain(100000);
52 std::vector<CBlockIndex> vBlocksMain(100000);
53 for (
unsigned int i=0; i<vBlocksMain.size(); i++) {
55 vBlocksMain[i].nHeight = i;
56 vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] :
nullptr;
57 vBlocksMain[i].phashBlock = &vHashMain[i];
58 vBlocksMain[i].BuildSkip();
60 BOOST_CHECK(vBlocksMain[i].pprev ==
nullptr || vBlocksMain[i].
nHeight == vBlocksMain[i].pprev->nHeight + 1);
64 std::vector<uint256> vHashSide(50000);
65 std::vector<CBlockIndex> vBlocksSide(50000);
66 for (
unsigned int i=0; i<vBlocksSide.size(); i++) {
68 vBlocksSide[i].nHeight = i + 50000;
69 vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : (vBlocksMain.data()+49999);
70 vBlocksSide[i].phashBlock = &vHashSide[i];
71 vBlocksSide[i].BuildSkip();
73 BOOST_CHECK(vBlocksSide[i].pprev ==
nullptr || vBlocksSide[i].
nHeight == vBlocksSide[i].pprev->nHeight + 1);
78 chain.
SetTip(vBlocksMain.back());
81 for (
int n=0; n<100; n++) {
82 int r = m_rng.randrange(150000);
83 CBlockIndex* tip = (r < 100000) ? &vBlocksMain[r] : &vBlocksSide[r - 100000];
91 for (
unsigned int i = 1; i < 12 && i < locator.
vHave.size() - 1; i++) {
96 unsigned int dist = 2;
97 for (
unsigned int i = 12; i < locator.
vHave.size() - 1; i++) {
106 std::vector<uint256> vHashMain(100000);
107 std::vector<CBlockIndex> vBlocksMain(100000);
108 for (
unsigned int i=0; i<vBlocksMain.size(); i++) {
110 vBlocksMain[i].nHeight = i;
111 vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] :
nullptr;
112 vBlocksMain[i].phashBlock = &vHashMain[i];
113 vBlocksMain[i].BuildSkip();
115 vBlocksMain[i].nTime = i;
116 vBlocksMain[i].nTimeMax = i;
119 int64_t medianTimePast = vBlocksMain[i].GetMedianTimePast();
120 int r{int(m_rng.randrange(medianTimePast))};
121 vBlocksMain[i].nTime = uint32_t(r + medianTimePast);
122 vBlocksMain[i].nTimeMax = std::max(vBlocksMain[i].nTime, vBlocksMain[i-1].nTimeMax);
126 unsigned int curTimeMax = 0;
127 for (
unsigned int i=0; i<vBlocksMain.size(); ++i) {
128 curTimeMax = std::max(curTimeMax, vBlocksMain[i].nTime);
129 BOOST_CHECK(curTimeMax == vBlocksMain[i].nTimeMax);
134 chain.
SetTip(vBlocksMain.back());
137 for (
unsigned int i=0; i<10000; ++i) {
139 int r = m_rng.randrange(vBlocksMain.size());
140 int64_t test_time = vBlocksMain[r].nTime;
150 std::list<CBlockIndex> blocks;
151 for (
const unsigned int timeMax : {100, 100, 100, 200, 200, 200, 300, 300, 300}) {
152 CBlockIndex* prev = blocks.empty() ? nullptr : &blocks.back();
153 blocks.emplace_back();
155 blocks.back().pprev = prev;
157 blocks.back().nTimeMax = timeMax;
161 chain.
SetTip(blocks.back());
196 const std::pair<int, int> TEST_DATA[]{
226 { 0b0001011101101000 ,
227 0b0001011101100000 },
228 { 0b0001011101101001 ,
229 0b0001011101000001 },
230 { 0b0110101101011000 ,
231 0b0110101101010000 },
232 { 0b0110101101011001 ,
233 0b0110101101000001 },
265 const auto max_test_input{std::ranges::max_element(TEST_DATA, [](
auto& a,
auto& b) {
return a.first < b.first; })};
266 const int chain_size{max_test_input->first + 1};
267 std::vector<CBlockIndex> block_index(chain_size);
268 for (
auto i{0}; i < chain_size; ++i) {
270 block_index[i].pprev = i == 0 ? nullptr : &block_index[i - 1];
271 block_index[i].nHeight = i;
272 block_index[i].BuildSkip();
276 for (
auto& [input, expected] : TEST_DATA) {
277 BOOST_REQUIRE_LT(input, chain_size);
278 BOOST_REQUIRE_GT(input, 0);
279 BOOST_REQUIRE_LT(expected, input);
280 BOOST_REQUIRE(block_index[input].pskip);
282 const int skip_height{block_index[input].pskip->nHeight};
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
CBlockLocator GetLocator(const CBlockIndex *index)
Get a locator for a block index entry.
The block chain is a tree shaped structure starting with the genesis block at the root,...
void BuildSkip()
Build the skiplist pointer for this entry.
unsigned int nTimeMax
(memory only) Maximum nTime in the chain up to and including this block.
uint256 GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
An in-memory indexed chain of blocks.
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
CBlockIndex * FindEarliestAtLeast(int64_t nTime, int height) const
Find the earliest block with timestamp equal or greater than the given time and height equal or great...
256-bit unsigned big integer.
uint64_t GetLow64() const
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
BOOST_AUTO_TEST_CASE(skiplist_test)
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
std::vector< uint256 > vHave