Changeset View
Changeset View
Standalone View
Standalone View
src/test/interfaces_tests.cpp
// Copyright (c) 2020 The Bitcoin Core developers | // Copyright (c) 2020 The Bitcoin Core 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 <chainparams.h> | #include <chainparams.h> | ||||
#include <config.h> | |||||
#include <consensus/validation.h> | |||||
#include <interfaces/chain.h> | #include <interfaces/chain.h> | ||||
#include <script/standard.h> | |||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <validation.h> | #include <validation.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
using interfaces::FoundBlock; | using interfaces::FoundBlock; | ||||
BOOST_FIXTURE_TEST_SUITE(interfaces_tests, TestChain100Setup) | BOOST_FIXTURE_TEST_SUITE(interfaces_tests, TestChain100Setup) | ||||
Show All 30 Lines | BOOST_AUTO_TEST_CASE(findBlock) { | ||||
int64_t mtp_time = -1; | int64_t mtp_time = -1; | ||||
BOOST_CHECK(chain->findBlock(active[60]->GetBlockHash(), | BOOST_CHECK(chain->findBlock(active[60]->GetBlockHash(), | ||||
FoundBlock().mtpTime(mtp_time))); | FoundBlock().mtpTime(mtp_time))); | ||||
BOOST_CHECK_EQUAL(mtp_time, active[60]->GetMedianTimePast()); | BOOST_CHECK_EQUAL(mtp_time, active[60]->GetMedianTimePast()); | ||||
BOOST_CHECK(!chain->findBlock(BlockHash(), FoundBlock())); | BOOST_CHECK(!chain->findBlock(BlockHash(), FoundBlock())); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(findAncestorByHeight) { | |||||
auto chain = interfaces::MakeChain(m_node, Params()); | |||||
auto &active = ChainActive(); | |||||
BlockHash hash; | |||||
BOOST_CHECK(chain->findAncestorByHeight(active[20]->GetBlockHash(), 10, | |||||
FoundBlock().hash(hash))); | |||||
BOOST_CHECK_EQUAL(hash, active[10]->GetBlockHash()); | |||||
BOOST_CHECK(!chain->findAncestorByHeight(active[10]->GetBlockHash(), 20)); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(findAncestorByHash) { | BOOST_AUTO_TEST_CASE(findAncestorByHash) { | ||||
auto chain = interfaces::MakeChain(m_node, Params()); | auto chain = interfaces::MakeChain(m_node, Params()); | ||||
auto &active = ChainActive(); | auto &active = ChainActive(); | ||||
int height = -1; | int height = -1; | ||||
BOOST_CHECK(chain->findAncestorByHash(active[20]->GetBlockHash(), | BOOST_CHECK(chain->findAncestorByHash(active[20]->GetBlockHash(), | ||||
active[10]->GetBlockHash(), | active[10]->GetBlockHash(), | ||||
FoundBlock().height(height))); | FoundBlock().height(height))); | ||||
BOOST_CHECK_EQUAL(height, 10); | BOOST_CHECK_EQUAL(height, 10); | ||||
BOOST_CHECK(!chain->findAncestorByHash(active[10]->GetBlockHash(), | BOOST_CHECK(!chain->findAncestorByHash(active[10]->GetBlockHash(), | ||||
active[20]->GetBlockHash())); | active[20]->GetBlockHash())); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(findCommonAncestor) { | |||||
auto chain = interfaces::MakeChain(m_node, Params()); | |||||
auto &active = ChainActive(); | |||||
auto *orig_tip = active.Tip(); | |||||
for (int i = 0; i < 10; ++i) { | |||||
BlockValidationState state; | |||||
ChainstateActive().InvalidateBlock(GetConfig(), state, active.Tip()); | |||||
} | |||||
BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight - 10); | |||||
coinbaseKey.MakeNewKey(true); | |||||
for (int i = 0; i < 20; ++i) { | |||||
CreateAndProcessBlock({}, | |||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | |||||
} | |||||
BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight + 10); | |||||
BlockHash fork_hash; | |||||
int fork_height; | |||||
int orig_height; | |||||
BOOST_CHECK(chain->findCommonAncestor( | |||||
orig_tip->GetBlockHash(), active.Tip()->GetBlockHash(), | |||||
FoundBlock().height(fork_height).hash(fork_hash), | |||||
FoundBlock().height(orig_height))); | |||||
BOOST_CHECK_EQUAL(orig_height, orig_tip->nHeight); | |||||
BOOST_CHECK_EQUAL(fork_height, orig_tip->nHeight - 10); | |||||
BOOST_CHECK_EQUAL(fork_hash, active[fork_height]->GetBlockHash()); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |