From d3cb2a49ce45934f9328e30bf240ff516ebfbc0c Mon Sep 17 00:00:00 2001 From: Alexey Poghilenkov Date: Thu, 6 Feb 2020 23:16:51 +0900 Subject: [PATCH 1/2] fix bug --- src/vbk/pop_service/pop_service_impl.cpp | 9 +++- src/vbk/test/unit/pop_service_tests.cpp | 63 +++++++++++++----------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/vbk/pop_service/pop_service_impl.cpp b/src/vbk/pop_service/pop_service_impl.cpp index bb0425059ab27..77e5c0a547315 100644 --- a/src/vbk/pop_service/pop_service_impl.cpp +++ b/src/vbk/pop_service/pop_service_impl.cpp @@ -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)) { diff --git a/src/vbk/test/unit/pop_service_tests.cpp b/src/vbk/test/unit/pop_service_tests.cpp index 77aaf6b120743..11ec54d367ee7 100644 --- a/src/vbk/test/unit/pop_service_tests.cpp +++ b/src/vbk/test/unit/pop_service_tests.cpp @@ -37,7 +37,16 @@ struct PopServiceFixture : public TestChain100Setup { 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); } }; @@ -61,17 +70,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; { @@ -97,36 +95,39 @@ 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_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())); - int prevHeight = ChainActive().Height(); - - std::shared_ptr view; - { - LOCK(cs_main); - view = std::make_shared(&ChainstateActive().CoinsTip()); - } + int prevHeight = endorsedBlockIndex->nHeight; BlockValidationState state; - InvalidateBlock(state, Params(), endorsedBlockIndex); - ActivateBestChain(state, Params()); + 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(); @@ -139,16 +140,17 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_from_main_cha { 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(); } -BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_merkleroot, 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())); - endorsedBlock.hashMerkleRoot.SetHex("fffff"); + endorsedBlock.hashPrevBlock.SetHex("ff"); CBlock block = createBlockWithPopTx(*this); @@ -161,12 +163,11 @@ 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; { + 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(); } @@ -195,6 +196,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(); } @@ -224,6 +226,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(); } From 15642ace86fc643be9b47604a1002a25b96f6560 Mon Sep 17 00:00:00 2001 From: Alexey Poghilenkov Date: Fri, 7 Feb 2020 13:19:47 +0900 Subject: [PATCH 2/2] fix --- src/vbk/test/unit/pop_service_tests.cpp | 42 +++++++++++++------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/vbk/test/unit/pop_service_tests.cpp b/src/vbk/test/unit/pop_service_tests.cpp index 11ec54d367ee7..bb9330b82c4e1 100644 --- a/src/vbk/test/unit/pop_service_tests.cpp +++ b/src/vbk/test/unit/pop_service_tests.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -32,6 +33,7 @@ struct PopServiceFixture : public TestChain100Setup { PopServiceFixture() { + AbortShutdown(); VeriBlock::InitUtilService(); VeriBlock::InitConfig(); Fake(Method(pop_service_impl_mock, addPayloads)); @@ -108,26 +110,15 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_index, PopServiceFixture) Verify_Method(Method(pop_service_impl_mock, removePayloads)).Once(); } -BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_from_chain, PopServiceFixture) +BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_known_orphan_block, PopServiceFixture) { - CBlockIndex* endorsedBlockIndex = ChainActive().Tip()->pprev->pprev; + CBlockIndex* endorsedBlockIndex = ChainActive().Tip(); CBlock endorsedBlock; BOOST_CHECK(ReadBlockFromDisk(endorsedBlock, endorsedBlockIndex, Params().GetConsensus())); - 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); + endorsedBlock.hashPrevBlock.SetHex("ff"); CBlock block = createBlockWithPopTx(*this); - BOOST_CHECK(ChainActive().Height() > prevHeight); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); stream << endorsedBlock.GetBlockHeader(); auto& config = VeriBlock::getService(); @@ -138,22 +129,34 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_from_chain, P }); { + 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"); + 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_endorsed_block_not_known_orphan_block, PopServiceFixture) +BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_from_chain, PopServiceFixture) { - CBlockIndex* endorsedBlockIndex = ChainActive().Tip(); + CBlockIndex* endorsedBlockIndex = ChainActive().Tip()->pprev->pprev; CBlock endorsedBlock; BOOST_CHECK(ReadBlockFromDisk(endorsedBlock, endorsedBlockIndex, Params().GetConsensus())); - endorsedBlock.hashPrevBlock.SetHex("ff"); + 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(); @@ -164,10 +167,9 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_known_orphan_ }); { - 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"); + BOOST_CHECK_EQUAL(state.GetRejectReason(), "pop-tx-endorsed-block-not-from-this-chain"); } Verify_Method(Method(pop_service_impl_mock, removePayloads)).Once(); }