10#include <boost/test/unit_test.hpp>
12#define SKIPLIST_LENGTH 300000
21 vIndex[i].nHeight = i;
22 vIndex[i].pprev = (i == 0) ?
nullptr : &vIndex[i - 1];
23 vIndex[i].BuildSkip();
28 BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]);
35 for (
int i=0; i < 1000; i++) {
40 BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
41 BOOST_CHECK(vIndex[from].GetAncestor(0) == vIndex.data());
48 std::vector<uint256> vHashMain(100000);
49 std::vector<CBlockIndex> vBlocksMain(100000);
50 for (
unsigned int i=0; i<vBlocksMain.size(); i++) {
52 vBlocksMain[i].nHeight = i;
53 vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] :
nullptr;
54 vBlocksMain[i].phashBlock = &vHashMain[i];
55 vBlocksMain[i].BuildSkip();
57 BOOST_CHECK(vBlocksMain[i].pprev ==
nullptr || vBlocksMain[i].
nHeight == vBlocksMain[i].pprev->nHeight + 1);
61 std::vector<uint256> vHashSide(50000);
62 std::vector<CBlockIndex> vBlocksSide(50000);
63 for (
unsigned int i=0; i<vBlocksSide.size(); i++) {
65 vBlocksSide[i].nHeight = i + 50000;
66 vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : (vBlocksMain.data()+49999);
67 vBlocksSide[i].phashBlock = &vHashSide[i];
68 vBlocksSide[i].BuildSkip();
70 BOOST_CHECK(vBlocksSide[i].pprev ==
nullptr || vBlocksSide[i].
nHeight == vBlocksSide[i].pprev->nHeight + 1);
75 chain.
SetTip(&vBlocksMain.back());
78 for (
int n=0; n<100; n++) {
80 CBlockIndex* tip = (r < 100000) ? &vBlocksMain[r] : &vBlocksSide[r - 100000];
88 for (
unsigned int i = 1; i < 12 && i < locator.
vHave.size() - 1; i++) {
93 unsigned int dist = 2;
94 for (
unsigned int i = 12; i < locator.
vHave.size() - 1; i++) {
103 std::vector<uint256> vHashMain(100000);
104 std::vector<CBlockIndex> vBlocksMain(100000);
105 for (
unsigned int i=0; i<vBlocksMain.size(); i++) {
107 vBlocksMain[i].nHeight = i;
108 vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] :
nullptr;
109 vBlocksMain[i].phashBlock = &vHashMain[i];
110 vBlocksMain[i].BuildSkip();
112 vBlocksMain[i].nTime = i;
113 vBlocksMain[i].nTimeMax = i;
116 int64_t medianTimePast = vBlocksMain[i].GetMedianTimePast();
118 vBlocksMain[i].nTime = r + medianTimePast;
119 vBlocksMain[i].nTimeMax = std::max(vBlocksMain[i].nTime, vBlocksMain[i-1].nTimeMax);
123 unsigned int curTimeMax = 0;
124 for (
unsigned int i=0; i<vBlocksMain.size(); ++i) {
125 curTimeMax = std::max(curTimeMax, vBlocksMain[i].nTime);
126 BOOST_CHECK(curTimeMax == vBlocksMain[i].nTimeMax);
131 chain.
SetTip(&vBlocksMain.back());
134 for (
unsigned int i=0; i<10000; ++i) {
137 int64_t test_time = vBlocksMain[r].nTime;
147 std::list<CBlockIndex> blocks;
148 for (
const unsigned int timeMax : {100, 100, 100, 200, 200, 200, 300, 300, 300}) {
149 CBlockIndex* prev = blocks.empty() ? nullptr : &blocks.back();
150 blocks.emplace_back();
152 blocks.back().pprev = prev;
154 blocks.back().nTimeMax = timeMax;
158 chain.
SetTip(&blocks.back());
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
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.
CBlockLocator GetLocator(const CBlockIndex *pindex=nullptr) const
Return a CBlockLocator that refers to a block in this chain (by default the 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...
void SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
256-bit unsigned big integer.
uint64_t GetLow64() const
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
static uint64_t InsecureRandRange(uint64_t range)
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