Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
eu321 committed Feb 14, 2020
1 parent 584ba77 commit 9a3fb96
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 35 deletions.
5 changes: 5 additions & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,8 @@ bool CChainState::DisconnectTip(BlockValidationState& state, const CChainParams&
}
}

VeriBlock::getService<VeriBlock::PopService>().removePayloads(pindexDelete);

m_chain.SetTip(pindexDelete->pprev);

UpdateTip(pindexDelete->pprev, chainparams);
Expand Down Expand Up @@ -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<VeriBlock::PopService>().addPayloads(pindexNew, blockConnecting);

// Update m_chain & related variables.
m_chain.SetTip(pindexNew);
UpdateTip(pindexNew, chainparams);
Expand Down
4 changes: 4 additions & 0 deletions src/vbk/pop_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 45 additions & 21 deletions src/vbk/pop_service/pop_service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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<std::mutex> lock(mutex);
Expand All @@ -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);
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/vbk/pop_service/pop_service_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/vbk/test/unit/block_validation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -122,4 +122,4 @@ BOOST_FIXTURE_TEST_CASE(BlockWithBothPopTxes, BlockValidationFixture)
Verify_Method(Method(pop_impl_mock, updateContext)).Exactly(1);
}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
16 changes: 8 additions & 8 deletions src/vbk/test/unit/pop_service_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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");
});

Expand All @@ -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()
4 changes: 2 additions & 2 deletions src/vbk/test/unit/updated_mempool_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 9a3fb96

Please sign in to comment.