11 #include <boost/test/unit_test.hpp> 13 #define SKIPLIST_LENGTH 300000 22 vIndex[i].nHeight = i;
23 vIndex[i].pprev = (i == 0) ?
nullptr : &vIndex[i - 1];
24 vIndex[i].BuildSkip();
29 BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]);
36 for (
int i=0; i < 1000; i++) {
41 BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
42 BOOST_CHECK(vIndex[from].GetAncestor(0) == vIndex.data());
49 std::vector<uint256> vHashMain(100000);
50 std::vector<CBlockIndex> vBlocksMain(100000);
51 for (
unsigned int i=0; i<vBlocksMain.size(); i++) {
53 vBlocksMain[i].nHeight = i;
54 vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] :
nullptr;
55 vBlocksMain[i].phashBlock = &vHashMain[i];
56 vBlocksMain[i].BuildSkip();
58 BOOST_CHECK(vBlocksMain[i].pprev ==
nullptr || vBlocksMain[i].
nHeight == vBlocksMain[i].pprev->nHeight + 1);
62 std::vector<uint256> vHashSide(50000);
63 std::vector<CBlockIndex> vBlocksSide(50000);
64 for (
unsigned int i=0; i<vBlocksSide.size(); i++) {
66 vBlocksSide[i].nHeight = i + 50000;
67 vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : (vBlocksMain.data()+49999);
68 vBlocksSide[i].phashBlock = &vHashSide[i];
69 vBlocksSide[i].BuildSkip();
71 BOOST_CHECK(vBlocksSide[i].pprev ==
nullptr || vBlocksSide[i].
nHeight == vBlocksSide[i].pprev->nHeight + 1);
76 chain.
SetTip(vBlocksMain.back());
79 for (
int n=0; n<100; n++) {
81 CBlockIndex* tip = (r < 100000) ? &vBlocksMain[r] : &vBlocksSide[r - 100000];
89 for (
unsigned int i = 1; i < 12 && i < locator.
vHave.size() - 1; i++) {
94 unsigned int dist = 2;
95 for (
unsigned int i = 12; i < locator.
vHave.size() - 1; i++) {
104 std::vector<uint256> vHashMain(100000);
105 std::vector<CBlockIndex> vBlocksMain(100000);
106 for (
unsigned int i=0; i<vBlocksMain.size(); i++) {
108 vBlocksMain[i].nHeight = i;
109 vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] :
nullptr;
110 vBlocksMain[i].phashBlock = &vHashMain[i];
111 vBlocksMain[i].BuildSkip();
113 vBlocksMain[i].nTime = i;
114 vBlocksMain[i].nTimeMax = i;
117 int64_t medianTimePast = vBlocksMain[i].GetMedianTimePast();
119 vBlocksMain[i].nTime = uint32_t(r + medianTimePast);
120 vBlocksMain[i].nTimeMax = std::max(vBlocksMain[i].nTime, vBlocksMain[i-1].nTimeMax);
124 unsigned int curTimeMax = 0;
125 for (
unsigned int i=0; i<vBlocksMain.size(); ++i) {
126 curTimeMax = std::max(curTimeMax, vBlocksMain[i].nTime);
127 BOOST_CHECK(curTimeMax == vBlocksMain[i].nTimeMax);
132 chain.
SetTip(vBlocksMain.back());
135 for (
unsigned int i=0; i<10000; ++i) {
138 int64_t test_time = vBlocksMain[r].nTime;
148 std::list<CBlockIndex> blocks;
149 for (
const unsigned int timeMax : {100, 100, 100, 200, 200, 200, 300, 300, 300}) {
150 CBlockIndex* prev = blocks.empty() ? nullptr : &blocks.back();
151 blocks.emplace_back();
153 blocks.back().pprev = prev;
155 blocks.back().nTimeMax = timeMax;
159 chain.
SetTip(blocks.back());
185 BOOST_CHECK(ret1->nTimeMax >= 100 && ret1->nHeight == 2);
188 BOOST_CHECK(ret2->nTimeMax >= 200 && ret2->nHeight == 4);
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
An in-memory indexed chain of blocks.
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
arith_uint256 UintToArith256(const uint256 &a)
uint256 GetBlockHash() const
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...
static uint64_t InsecureRandRange(uint64_t range)
BOOST_AUTO_TEST_SUITE_END()
void BuildSkip()
Build the skiplist pointer for this entry.
BOOST_AUTO_TEST_CASE(skiplist_test)
std::vector< uint256 > vHave
256-bit unsigned big integer.
uint256 ArithToUint256(const arith_uint256 &a)
#define BOOST_CHECK_EQUAL(v1, v2)
The block chain is a tree shaped structure starting with the genesis block at the root...
uint64_t GetLow64() const
CBlockLocator GetLocator(const CBlockIndex *index)
Get a locator for a block index entry.
int nHeight
height of the entry in the chain. The genesis block has height 0
#define BOOST_CHECK(expr)