diff --git a/src/validation.cpp b/src/validation.cpp index ebc31654acb059..a2719a540163a8 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2494,6 +2494,8 @@ bool CChainState::DisconnectTip(BlockValidationState& state, const CChainParams& } } + VeriBlock::getService().removePayloads(pindexDelete); + m_chain.SetTip(pindexDelete->pprev); UpdateTip(pindexDelete->pprev, chainparams); @@ -2620,6 +2622,9 @@ bool CChainState::ConnectTip(BlockValidationState& state, const CChainParams& ch // Remove conflicting transactions from the mempool.; mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight); disconnectpool.removeForBlock(blockConnecting.vtx); + + VeriBlock::getService().addPayloads(pindexNew, blockConnecting); + // Update m_chain & related variables. m_chain.SetTip(pindexNew); UpdateTip(pindexNew, chainparams); diff --git a/src/vbk/pop_service.hpp b/src/vbk/pop_service.hpp index 38e93d72e62884..cd961aa88a90f1 100644 --- a/src/vbk/pop_service.hpp +++ b/src/vbk/pop_service.hpp @@ -52,7 +52,11 @@ struct PopService { virtual bool determineATVPlausibilityWithBTCRules(AltchainId altChainIdentifier, const CBlockHeader& popEndorsementHeader, const Consensus::Params& params, TxValidationState& state) = 0; virtual void addPayloads(std::string blockHash, const int& nHeight, const Publications& publications) = 0; + virtual void addPayloads(const CBlockIndex & blockIndex, const CBlock & block) = 0; + + virtual void removePayloads(const CBlockIndex & block) = 0; virtual void removePayloads(std::string blockHash, const int& blockHeight) = 0; + virtual void setConfig() = 0; }; } // namespace VeriBlock diff --git a/src/vbk/pop_service/pop_service_impl.cpp b/src/vbk/pop_service/pop_service_impl.cpp index 904b005e12ee51..8d4532705238da 100644 --- a/src/vbk/pop_service/pop_service_impl.cpp +++ b/src/vbk/pop_service/pop_service_impl.cpp @@ -130,6 +130,51 @@ void PopServiceImpl::addPayloads(std::string blockHash, const int& nHeight, cons } } +void addPayloads(const CBlockIndex & blockIndex, const CBlock & block) +{ + std::unique_lock lock(mutex); + AddPayloadsDataRequest request; + + auto* index = new BlockIndex(); + index->set_height(blockIndex.nHeight); + index->set_hash(blockIndex.GetBlockHash().ToString()); + request.set_allocated_blockindex(index); + + for (const auto & tx : block.vtx) { + if (!isPopTx(*tx)) { + continue; + } + + Publications publications; + PopTxType type; + ScriptError serror; + assert(parsePopTx(tx, &serror, &publications, nullptr, &type) && "scriptSig of pop tx is invalid in addPayloads"); + + // skip non-publication transactions + if (type != PopTxType::PUBLICATIONS) { + continue; + } + + // insert payloads + request.add_altpublications(publications.atv.data(), publications.atv.size()); + for (const auto& vtb : publications.vtbs) { + request.add_veriblockpublications(vtb.data(), vtb.size()); + } + } + + EmptyReply reply; + ClientContext context; + Status status = grpcPopService->AddPayloads(&context, request, &reply); + if (!status.ok()) { + throw PopServiceException(status); + } +} + +void PopServiceImpl::removePayloads(const CBlockIndex & block) +{ + removePayloads(block.GetBlockHash().ToString(), block.nHeight); +} + void PopServiceImpl::removePayloads(std::string blockHash, const int& nHeight) { std::unique_lock lock(mutex); @@ -155,9 +200,6 @@ void PopServiceImpl::savePopTxToDatabase(const CBlock& block, const int& nHeight SaveBlockPopTxRequest request; EmptyReply reply; - AddPayloadsDataRequest payloads; - - auto* b1 = new AltChainBlock(); BlockToProtoAltChainBlock(block, nHeight, *b1); request.set_allocated_containingblock(b1); @@ -179,12 +221,6 @@ void PopServiceImpl::savePopTxToDatabase(const CBlock& block, const int& nHeight continue; } - // insert payloads - payloads.add_altpublications(publications.atv.data(), publications.atv.size()); - for (const auto& vtb : publications.vtbs) { - payloads.add_veriblockpublications(vtb.data(), vtb.size()); - } - // Fill the proto objects with pop data popTxData->set_poptxhash(tx->GetHash().ToString()); popTxData->set_altpublication(publications.atv.data(), publications.atv.size()); @@ -212,18 +248,6 @@ void PopServiceImpl::savePopTxToDatabase(const CBlock& block, const int& nHeight } if (request.popdata_size() != 0) { - // first, addPayloads - { - auto* index = new BlockIndex(); - index->set_height(nHeight); - index->set_hash(block.GetHash().ToString()); - payloads.set_allocated_blockindex(index); - ClientContext context; - Status status = grpcPopService->AddPayloads(&context, payloads, &reply); - if (!status.ok()) { - throw PopServiceException(status); - } - } // then, save pop txes to database { ClientContext context; diff --git a/src/vbk/pop_service/pop_service_impl.hpp b/src/vbk/pop_service/pop_service_impl.hpp index 3ba2d971a388cd..8637b620949d45 100644 --- a/src/vbk/pop_service/pop_service_impl.hpp +++ b/src/vbk/pop_service/pop_service_impl.hpp @@ -51,8 +51,10 @@ class PopServiceImpl : public PopService bool determineATVPlausibilityWithBTCRules(AltchainId altChainIdentifier, const CBlockHeader& popEndorsementHeader, const Consensus::Params& params, TxValidationState& state) override; void addPayloads(std::string blockHash, const int& nHeight, const Publications& publications) override; + void addPayloads(const CBlockIndex & blockIndex, const CBlock & block) override; - void removePayloads(std::string blockHash, const int& nHeight) override; + void removePayloads(const CBlockIndex & blockIndex) override; + void removePayloads(std::string blockHash, const int& blockHeight) override; void setConfig() override; diff --git a/src/vbk/test/unit/block_validation_tests.cpp b/src/vbk/test/unit/block_validation_tests.cpp index 5aac9b9d27c2b2..d3ff5ecd300d44 100644 --- a/src/vbk/test/unit/block_validation_tests.cpp +++ b/src/vbk/test/unit/block_validation_tests.cpp @@ -54,8 +54,8 @@ struct BlockValidationFixture : public TestChain100Setup { return VeriBlock::EvalScriptImpl(tx->vin[0].scriptSig, stack, serror, pub, ctx, type, false); }); When(Method(pop_impl_mock, determineATVPlausibilityWithBTCRules)).AlwaysReturn(true); - Fake(Method(pop_impl_mock, addPayloads)); - Fake(Method(pop_impl_mock, removePayloads)); + Fake(OverloadedMethod(pop_impl_mock, addPayloads, void(std::string, const int&, const VeriBlock::Publications&))); + Fake(OverloadedMethod(pop_impl_mock, removePayloads, void(std::string, const int&))); Fake(Method(pop_impl_mock, updateContext)); Fake(Method(pop_impl_mock, clearTemporaryPayloads)); @@ -122,4 +122,4 @@ BOOST_FIXTURE_TEST_CASE(BlockWithBothPopTxes, BlockValidationFixture) Verify_Method(Method(pop_impl_mock, updateContext)).Exactly(1); } -BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/vbk/test/unit/pop_service_tests.cpp b/src/vbk/test/unit/pop_service_tests.cpp index bb9330b82c4e10..3d98eafbe62f93 100644 --- a/src/vbk/test/unit/pop_service_tests.cpp +++ b/src/vbk/test/unit/pop_service_tests.cpp @@ -36,8 +36,8 @@ struct PopServiceFixture : public TestChain100Setup { AbortShutdown(); VeriBlock::InitUtilService(); VeriBlock::InitConfig(); - Fake(Method(pop_service_impl_mock, addPayloads)); - Fake(Method(pop_service_impl_mock, removePayloads)); + Fake(OverloadedMethod(pop_service_impl_mock, addPayloads, void(std::string, const int&, const VeriBlock::Publications&))); + Fake(OverloadedMethod(pop_service_impl_mock, removePayloads, void(std::string, const int&))); Fake(Method(pop_service_impl_mock, clearTemporaryPayloads)); 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 { @@ -107,7 +107,7 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_index, PopServiceFixture) 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(); + Verify_Method(OverloadedMethod(pop_service_impl_mock, removePayloads, void(std::string, const int&))).Once(); } BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_known_orphan_block, PopServiceFixture) @@ -134,7 +134,7 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_known_orphan_ 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(); + Verify_Method(OverloadedMethod(pop_service_impl_mock, removePayloads, void(std::string, const int&))).Once(); } BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_from_chain, PopServiceFixture) @@ -171,7 +171,7 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_endorsed_block_not_from_chain, P 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(); + Verify_Method(OverloadedMethod(pop_service_impl_mock, removePayloads, void(std::string, const int&))).Once(); } BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_settlement_interval, PopServiceFixture) @@ -200,7 +200,7 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_settlement_interval, PopSe 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(); + Verify_Method(OverloadedMethod(pop_service_impl_mock, removePayloads, void(std::string, const int&))).Once(); } BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_addPayloads, PopServiceFixture) @@ -220,7 +220,7 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_addPayloads, PopServiceFix setPublicationData(publicationData, stream, config.index.unwrap()); }); - When(Method(pop_service_impl_mock, addPayloads)).AlwaysDo([](std::string hash, const int& nHeight, const VeriBlock::Publications& publications) -> void { + When(OverloadedMethod(pop_service_impl_mock, addPayloads, void(std::string, const int&, const VeriBlock::Publications&))).AlwaysDo([](std::string hash, const int& nHeight, const VeriBlock::Publications& publications) -> void { throw VeriBlock::PopServiceException("fail"); }); @@ -230,6 +230,6 @@ BOOST_FIXTURE_TEST_CASE(blockPopValidation_test_wrong_addPayloads, PopServiceFix 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(); + Verify_Method(OverloadedMethod(pop_service_impl_mock, removePayloads, void(std::string, const int&))).Once(); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/vbk/test/unit/updated_mempool_tests.cpp b/src/vbk/test/unit/updated_mempool_tests.cpp index 3ccaaa9125b96d..e6fcd9e3297e60 100644 --- a/src/vbk/test/unit/updated_mempool_tests.cpp +++ b/src/vbk/test/unit/updated_mempool_tests.cpp @@ -283,8 +283,8 @@ BOOST_FIXTURE_TEST_CASE(check_the_pop_tx_limits_in_block, TestingSetup) BOOST_FIXTURE_TEST_CASE(check_CreateNewBlock_with_blockPopValidation_fail, TestingSetup) { - Fake(Method(pop_service_mock, addPayloads)); - Fake(Method(pop_service_mock, removePayloads)); + Fake(OverloadedMethod(pop_service_mock, addPayloads, void(std::string, const int&, const VeriBlock::Publications&))); + Fake(OverloadedMethod(pop_service_mock, removePayloads, void(std::string, const int&))); Fake(Method(pop_service_mock, clearTemporaryPayloads)); When(Method(pop_service_mock, determineATVPlausibilityWithBTCRules)).AlwaysReturn(true);