-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblockchain_tests.cpp
More file actions
109 lines (87 loc) · 3.9 KB
/
blockchain_tests.cpp
File metadata and controls
109 lines (87 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <boost/test/unit_test.hpp>
#include "main.h"
#include "miner.h"
#include "txdb.h"
#include <fs.h>
#include <test/test_runner.h>
BOOST_AUTO_TEST_SUITE(blockchain_tests)
void addBlocksToChain(int toBlockHeight)
{
int currentHeight = pindexBest->nHeight;
while(currentHeight < toBlockHeight - 1)
{
currentHeight = pindexBest->nHeight;
CBlockIndex *pindexPrev = pindexBest;
unsigned int nExtraNonce = 0;
int64_t nFees = 0;
CPubKey newCoinbaseKey;
ptestWallet->GetKeyFromPool(newCoinbaseKey);
auto_ptr<CBlock> pblocktemplate(CreateNewBlock(newCoinbaseKey, false, &nFees));
CBlock *pblock = pblocktemplate.get();
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
uint256 hash;
while (true)
{
hash = pblock->GetPoWHash();
if (hash <= hashTarget)
{
if (CheckWork(pblock, *ptestWallet))
{
break;
}
}
++pblock->nNonce;
if (pblock->nTime > FutureDrift((int64_t)pblock->vtx[0].nTime))
{
break; //coinbase timestamp vs block timestamp will be out of sync and cause the block to be rejected, so we must regenerate it occasionally.
}
}
}
}
/*
BOOST_AUTO_TEST_CASE(add_block_to_chain)
{
//check current tip is the genesis block
CBlockIndex *pgenesisBlockIndex = mapBlockIndex[hashBestChain];
CBlockIndex *pblockindex = mapBlockIndex[hashBestChain];
BOOST_TEST_MESSAGE(pblockindex->phashBlock->ToString());
BOOST_CHECK(pblockindex->phashBlock->ToString() == GENESIS_BLOCK_HASH);
//mine new block
//check the current tip is not the genesis block
pblockindex = mapBlockIndex[hashBestChain];
BOOST_TEST_MESSAGE(pblockindex->phashBlock->ToString());
BOOST_CHECK(pblockindex->phashBlock->ToString() != GENESIS_BLOCK_HASH);
//disconnect tip block from chain
CTxDB txdb;
pblock->DisconnectBlock(txdb, pblockindex); //only updates disk representation, in memory must be done manually.
if (!txdb.WriteHashBestChain(pblockindex->pprev->GetBlockHash()))
{
BOOST_FAIL("error updating hash best chain on disk");
}
pblockindex->pprev->pnext = NULL;
hashBestChain = *(pgenesisBlockIndex->phashBlock);
//check the current tip is the genesis block
pblockindex = mapBlockIndex[hashBestChain];
BOOST_TEST_MESSAGE(pblockindex->phashBlock->ToString());
BOOST_CHECK(pblockindex->phashBlock->ToString() == GENESIS_BLOCK_HASH);
}
*/
BOOST_AUTO_TEST_CASE(crisp_simple_test)
{
/* works somtimes, but then breaks and re-running the test doesn't work...
CBlockIndex *pblockindex = mapBlockIndex[hashBestChain];
BOOST_CHECK(pblockindex->phashBlock->ToString() == GENESIS_BLOCK_HASH);
addBlocksToChain(250);
pblockindex = mapBlockIndex[hashBestChain];
BOOST_CHECK(pblockindex->phashBlock->ToString() != GENESIS_BLOCK_HASH);
BOOST_CHECK(pblockindex->nHeight == 250);
CBlockIndex* pcrispBlockIndex = mapBlockIndex[hashBestChain];
while (pcrispBlockIndex->nHeight > 225)
pcrispBlockIndex = pcrispBlockIndex->pprev;
CBlock crispBlock;
crispBlock.ReadFromDisk(pcrispBlockIndex, true);
BOOST_CHECK(crispBlock.addressBalances.size() > 1);
*/
}
BOOST_AUTO_TEST_SUITE_END()