PIVX Core  5.6.99
P2P Digital Currency
checkblock_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013-2014 The Bitcoin Core developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 //
6 // Unit tests for block.CheckBlock()
7 //
8 
9 #include "test/test_pivx.h"
10 
11 #include "clientversion.h"
12 #include "fs.h"
13 #include "utiltime.h"
14 #include "validation.h"
15 
16 #include <cstdio>
17 
18 #include <boost/test/unit_test.hpp>
19 
20 
22 
23 bool read_block(const std::string& filename, CBlock& block)
24 {
25  fs::path testFile = fs::current_path() / "data" / filename;
26 #ifdef TEST_DATA_DIR
27  if (!fs::exists(testFile))
28  {
29  testFile = fs::path(BOOST_PP_STRINGIZE(TEST_DATA_DIR)) / filename;
30  }
31 #endif
32  FILE* fp = fopen(testFile.string().c_str(), "rb");
33  if (!fp) return false;
34 
35  fseek(fp, 8, SEEK_SET); // skip msgheader/size
36 
37  CAutoFile filein(fp, SER_DISK, CLIENT_VERSION);
38  if (filein.IsNull()) return false;
39 
40  filein >> block;
41 
42  return true;
43 }
44 
46 {
47  // Putting a 1MB binary file in the git repository is not a great
48  // idea, so this test is only run if you manually download
49  // test/data/Mar12Fork.dat from
50  // http://sourceforge.net/projects/bitcoin/files/Bitcoin/blockchain/Mar12Fork.dat/download
51  unsigned int tMay15 = 1368576000;
52  SetMockTime(tMay15); // Test as if it was right at May 15
53 
54  CBlock forkingBlock;
55  if (read_block("Mar12Fork.dat", forkingBlock))
56  {
57  LOCK(cs_main);
58  CValidationState state;
59 
60  // After May 15'th, big blocks are OK:
61  forkingBlock.nTime = tMay15; // Invalidates PoW
62  BOOST_CHECK(CheckBlock(forkingBlock, state, false, false));
63  }
64 
65  SetMockTime(0);
66 }
67 
bool read_block(const std::string &filename, CBlock &block)
BOOST_AUTO_TEST_CASE(May15)
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:452
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:501
uint32_t nTime
Definition: block.h:30
Definition: block.h:80
Capture information about block/transaction validation.
Definition: validation.h:24
BOOST_AUTO_TEST_SUITE_END()
@ LOCK
Definition: lockunlock.h:16
bool CheckBlock(int nHeight, const uint256 &hash, bool fMatchesCheckpoint)
Returns true if block passes checkpoint checks.
Definition: checkpoints.cpp:29
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:23
Definition: uint256.h:212
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:80
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ SER_DISK
Definition: serialize.h:175
Basic testing setup.
Definition: test_pivx.h:51
void SetMockTime(int64_t nMockTimeIn)
For testing.
Definition: utiltime.cpp:51