Changeset View
Changeset View
Standalone View
Standalone View
src/test/blockindex_tests.cpp
// Copyright (c) 2018 The Bitcoin developers | // Copyright (c) 2018 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "blockvalidity.h" | #include "blockvalidity.h" | ||||
#include "chain.h" | #include "chain.h" | ||||
#include "diskblockpos.h" | #include "diskblockpos.h" | ||||
#include "uint256.h" | #include "uint256.h" | ||||
#include "validation.h" | |||||
#include "test/test_bitcoin.h" | #include "test/test_bitcoin.h" | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <limits> | #include <limits> | ||||
BOOST_FIXTURE_TEST_SUITE(blockindex_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(blockindex_tests, BasicTestingSetup) | ||||
▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | std::generate(hashBytes.begin(), hashBytes.end(), | ||||
[]() { return uint8_t(rand() % 255); }); | []() { return uint8_t(rand() % 255); }); | ||||
const uint256 randomHash = uint256(hashBytes); | const uint256 randomHash = uint256(hashBytes); | ||||
index.phashBlock = &randomHash; | index.phashBlock = &randomHash; | ||||
hash = index.GetBlockHash(); | hash = index.GetBlockHash(); | ||||
BOOST_CHECK(hash == randomHash); | BOOST_CHECK(hash == randomHash); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(received_time) { | BOOST_AUTO_TEST_CASE(header_received_time) { | ||||
// Set to UINT32_MAX because that's the maximum value header.nTime can hold | // Set to UINT32_MAX because that's the maximum value header.nTime can hold | ||||
const int64_t expectedBlockTime = std::numeric_limits<uint32_t>::max(); | const int64_t expectedBlockTime = std::numeric_limits<uint32_t>::max(); | ||||
CBlockHeader header; | CBlockHeader header; | ||||
header.nTime = uint32_t(expectedBlockTime); | header.nTime = uint32_t(expectedBlockTime); | ||||
CBlockIndex index = CBlockIndex(header); | CBlockIndex index = CBlockIndex(header); | ||||
// nTimeReceived defaults to block time | // nTimeReceived defaults to block time | ||||
BOOST_CHECK(index.nTimeReceived == expectedBlockTime); | BOOST_CHECK(index.nTimeHeaderReceived == expectedBlockTime); | ||||
// nTimeReceived can be updated to the actual received time, which may | // nTimeReceived can be updated to the actual received time, which may | ||||
// be before or after the miner's time. | // be before or after the miner's time. | ||||
for (int64_t receivedTime = expectedBlockTime - 10; | for (int64_t receivedTime = expectedBlockTime - 10; | ||||
// Make sure that receivedTime is tested beyond 32-bit values. | // Make sure that receivedTime is tested beyond 32-bit values. | ||||
receivedTime <= expectedBlockTime + 10; receivedTime++) { | receivedTime <= expectedBlockTime + 10; receivedTime++) { | ||||
index.nTimeReceived = receivedTime; | index.nTimeHeaderReceived = receivedTime; | ||||
BOOST_CHECK(index.GetBlockTime() == expectedBlockTime); | BOOST_CHECK(index.GetBlockTime() == expectedBlockTime); | ||||
BOOST_CHECK(index.GetHeaderReceivedTime() == receivedTime); | BOOST_CHECK(index.GetHeaderReceivedTime() == receivedTime); | ||||
BOOST_CHECK(index.GetReceivedTimeDiff() == | BOOST_CHECK(index.GetReceivedTimeDiff() == | ||||
receivedTime - expectedBlockTime); | receivedTime - expectedBlockTime); | ||||
} | } | ||||
} | } | ||||
BOOST_FIXTURE_TEST_CASE(block_received_time, TestChain100Setup) { | |||||
// The time in the BlockHeader is the max of the MTP-11 and GetTime(). | |||||
// So we rewind the time, and check the received time before the blocktime.s | |||||
time_t mockedTime = GetTime() - 600; | |||||
SetMockTime(mockedTime); | |||||
CBlock block = CreateAndProcessBlock({}, CScript() << OP_TRUE); | |||||
BOOST_CHECK(chainActive.Tip()->GetBlockHash() == block.GetHash()); | |||||
CBlockIndex *pindex = nullptr; | |||||
BOOST_CHECK(mapBlockIndex.count(block.GetHash()) == 1); | |||||
pindex = mapBlockIndex[block.GetHash()]; | |||||
BOOST_CHECK(pindex->GetBlockReceivedTime() == mockedTime); | |||||
// We don't know the exact MTP-11, but it should be AT LEAST 590 seconds | |||||
// ahead. | |||||
BOOST_CHECK(pindex->GetBlockTime() > mockedTime + 590); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(median_time_past) { | BOOST_AUTO_TEST_CASE(median_time_past) { | ||||
std::array<CBlockIndex, 12> indices; | std::array<CBlockIndex, 12> indices; | ||||
// times in this test are pairs of <blockTime, MTP> | // times in this test are pairs of <blockTime, MTP> | ||||
// Check that MTP is correctly calculated for all cases when block times | // Check that MTP is correctly calculated for all cases when block times | ||||
// are consecutive and greater than previous block times: | // are consecutive and greater than previous block times: | ||||
// 1) All cases where the number of blocks is < 11 | // 1) All cases where the number of blocks is < 11 | ||||
▲ Show 20 Lines • Show All 281 Lines • Show Last 20 Lines |