Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug #18

Merged
merged 2 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/vbk/pop_service/pop_service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,14 @@ bool txPopValidation(PopServiceImpl& pop, const CTransactionRef& tx, const CBloc

AssertLockHeld(cs_main);
const CBlockIndex* popEndorsementIdnex = LookupBlockIndex(popEndorsementHeader.GetHash());
if (popEndorsementIdnex == nullptr || !ChainActive().Contains(popEndorsementIdnex)) {
return state.Invalid(TxValidationResult::TX_BAD_POP_DATA, "pop-tx-endorsed-block-not-from-this-main-chain", strprintf("[%s] can not find endorsed block in the main chain: %s", tx->GetHash().ToString(), popEndorsementHeader.GetHash().ToString()));
if (popEndorsementIdnex == nullptr) {
return state.Invalid(TxValidationResult::TX_BAD_POP_DATA, "pop-tx-endorsed-block-not-known-orphan-block", strprintf("[%s] can not find endorsed block index: %s", tx->GetHash().ToString(), popEndorsementHeader.GetHash().ToString()));
}
const CBlockIndex* ancestor = pindexPrev.GetAncestor(popEndorsementIdnex->nHeight);
if (ancestor == nullptr || ancestor->GetBlockHash() != popEndorsementIdnex->GetBlockHash()) {
return state.Invalid(TxValidationResult::TX_BAD_POP_DATA, "pop-tx-endorsed-block-not-from-this-chain", strprintf("[%s] can not find endorsed block in the chain: %s", tx->GetHash().ToString(), popEndorsementHeader.GetHash().ToString()));
}


CBlock popEndorsementBlock;
if (!ReadBlockFromDisk(popEndorsementBlock, popEndorsementIdnex, params)) {
Expand Down
71 changes: 38 additions & 33 deletions src/vbk/test/unit/pop_service_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <boost/test/unit_test.hpp>
#include <consensus/validation.h>
#include <shutdown.h>
#include <test/util/setup_common.h>
#include <validation.h>

Expand Down Expand Up @@ -32,12 +33,22 @@ struct PopServiceFixture : public TestChain100Setup {

PopServiceFixture()
{
AbortShutdown();
VeriBlock::InitUtilService();
VeriBlock::InitConfig();
Fake(Method(pop_service_impl_mock, addPayloads));
Fake(Method(pop_service_impl_mock, removePayloads));
Fake(Method(pop_service_impl_mock, clearTemporaryPayloads));
When(OverloadedMethod(pop_service_impl_mock, parsePopTx, bool(const CTransactionRef&, ScriptError*, VeriBlock::Publications*, VeriBlock::Context*, VeriBlock::PopTxType*))).AlwaysReturn(true);
When(OverloadedMethod(pop_service_impl_mock, parsePopTx, bool(const CTransactionRef&, ScriptError*, VeriBlock::Publications*, VeriBlock::Context*, VeriBlock::PopTxType*)))
.Do([](const CTransactionRef&, ScriptError* serror, VeriBlock::Publications*, VeriBlock::Context*, VeriBlock::PopTxType* type) -> bool {
if (type != nullptr) {
*type = VeriBlock::PopTxType::PUBLICATIONS;
}
if (serror != nullptr) {
*serror = ScriptError::SCRIPT_ERR_OK;
}
return true;
});
When(Method(pop_service_impl_mock, determineATVPlausibilityWithBTCRules)).AlwaysReturn(true);
}
};
Expand All @@ -61,17 +72,6 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test, PopServiceFixture)
setPublicationData(publicationData, stream, config.index.unwrap());
});

When(OverloadedMethod(pop_service_impl_mock, parsePopTx, bool(const CTransactionRef&, ScriptError*, VeriBlock::Publications*, VeriBlock::Context*, VeriBlock::PopTxType*)))
.Do([](const CTransactionRef&, ScriptError* serror, VeriBlock::Publications*, VeriBlock::Context*, VeriBlock::PopTxType* type) -> bool {
if (type != nullptr) {
*type = VeriBlock::PopTxType::PUBLICATIONS;
}
if (serror != nullptr) {
*serror = ScriptError::SCRIPT_ERR_OK;
}
return true;
});


BlockValidationState state;
{
Expand All @@ -97,33 +97,25 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_index, PopServiceFixture)
setPublicationData(publicationData, stream, -1);
});

When(Method(pop_service_impl_mock, determineATVPlausibilityWithBTCRules)).Return(false);
When(Method(pop_service_impl_mock, determineATVPlausibilityWithBTCRules)).AlwaysDo([](VeriBlock::AltchainId altChainIdentifier, const CBlockHeader& popEndorsementHeader, const Consensus::Params& params, TxValidationState& state) -> bool {
return VeriBlock::PopServiceImpl().determineATVPlausibilityWithBTCRules(altChainIdentifier, popEndorsementHeader, params, state);
});

BlockValidationState state;
{
LOCK(cs_main);
BOOST_CHECK(!blockPopValidationImpl(pop_service_impl_mock.get(), block, *ChainActive().Tip()->pprev, Params().GetConsensus(), state));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "pop-tx-altchain-id");
}
Verify_Method(Method(pop_service_impl_mock, removePayloads)).Once();
}

BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_from_main_chain, PopServiceFixture)
BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_known_orphan_block, PopServiceFixture)
{
CBlockIndex* endorsedBlockIndex = ChainActive().Tip()->pprev->pprev->pprev;
CBlockIndex* endorsedBlockIndex = ChainActive().Tip();
CBlock endorsedBlock;
BOOST_CHECK(ReadBlockFromDisk(endorsedBlock, endorsedBlockIndex, Params().GetConsensus()));
int prevHeight = ChainActive().Height();

std::shared_ptr<CCoinsViewCache> view;
{
LOCK(cs_main);
view = std::make_shared<CCoinsViewCache>(&ChainstateActive().CoinsTip());
}

BlockValidationState state;
InvalidateBlock(state, Params(), endorsedBlockIndex);
ActivateBestChain(state, Params());
BOOST_CHECK(ChainActive().Height() < prevHeight);
endorsedBlock.hashPrevBlock.SetHex("ff");

CBlock block = createBlockWithPopTx(*this);

Expand All @@ -137,21 +129,34 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_from_main_cha
});

{
BlockValidationState state;
LOCK(cs_main);
BOOST_CHECK(!blockPopValidationImpl(pop_service_impl_mock.get(), block, *ChainActive().Tip()->pprev, Params().GetConsensus(), state));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "pop-tx-endorsed-block-not-known-orphan-block");
}
Verify_Method(Method(pop_service_impl_mock, removePayloads)).Once();
}

BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_merkleroot, PopServiceFixture)
BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_from_chain, PopServiceFixture)
{
CBlockIndex* endorsedBlockIndex = ChainActive().Tip()->pprev->pprev->pprev;
CBlockIndex* endorsedBlockIndex = ChainActive().Tip()->pprev->pprev;
CBlock endorsedBlock;
BOOST_CHECK(ReadBlockFromDisk(endorsedBlock, endorsedBlockIndex, Params().GetConsensus()));
endorsedBlock.hashMerkleRoot.SetHex("fffff");
int prevHeight = endorsedBlockIndex->nHeight;

BlockValidationState state;
BOOST_CHECK(InvalidateBlock(state, Params(), endorsedBlockIndex->pprev));
BOOST_CHECK(ActivateBestChain(state, Params()));
BOOST_CHECK(ChainActive().Height() < prevHeight);

CScript scriptPubKey = CScript() << OP_CHECKSIG;
CreateAndProcessBlock({}, scriptPubKey);
CreateAndProcessBlock({}, scriptPubKey);
CreateAndProcessBlock({}, scriptPubKey);

CBlock block = createBlockWithPopTx(*this);

BOOST_CHECK(ChainActive().Height() > prevHeight);
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << endorsedBlock.GetBlockHeader();
auto& config = VeriBlock::getService<VeriBlock::Config>();
Expand All @@ -161,12 +166,10 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_merkleroot, PopServiceFixt
setPublicationData(publicationData, stream, config.index.unwrap());
});

When(Method(pop_service_impl_mock, determineATVPlausibilityWithBTCRules)).AlwaysReturn(true);

BlockValidationState state;
{
LOCK(cs_main);
BOOST_CHECK(!blockPopValidationImpl(pop_service_impl_mock.get(), block, *ChainActive().Tip()->pprev, Params().GetConsensus(), state));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "pop-tx-endorsed-block-not-from-this-chain");
}
Verify_Method(Method(pop_service_impl_mock, removePayloads)).Once();
}
Expand Down Expand Up @@ -195,6 +198,7 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_settlement_interval, PopSe
{
LOCK(cs_main);
BOOST_CHECK(!blockPopValidationImpl(pop_service_impl_mock.get(), block, *ChainActive().Tip()->pprev, Params().GetConsensus(), state));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "pop-tx-endorsed-block-too-old");
}
Verify_Method(Method(pop_service_impl_mock, removePayloads)).Once();
}
Expand Down Expand Up @@ -224,6 +228,7 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_addPayloads, PopServiceFix
{
LOCK(cs_main);
BOOST_CHECK(!blockPopValidationImpl(pop_service_impl_mock.get(), block, *ChainActive().Tip()->pprev, Params().GetConsensus(), state));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "pop-tx-add-payloads-failed");
}
Verify_Method(Method(pop_service_impl_mock, removePayloads)).Once();
}
Expand Down