8000 update BlockAssembler::CreateNewBlock() method by Mr-Leshiy · Pull Request #17 · VeriBlock/vbk-ri-btc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

update BlockAssembler::CreateNewBlock() method #17

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

Merged
merged 2 commits into from
Feb 4, 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
22 changes: 21 additions & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
VeriBlock::getService<VeriBlock::UtilService>().addPopPayoutsIntoCoinbaseTx(coinbaseTx, *pindexPrev, chainparams.GetConsensus());

pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));

// VeriBlock: run the blockPopValidation to delete pop tramsactions if they are invalid
BlockValidationState state;
while (!VeriBlock::getService<VeriBlock::PopService>().blockPopValidation(*pblock, *pindexPrev, Params().GetConsensus(), state)) {
// If the blockPopValidation failes we have to clear transactions vector and fill it again
resetBlock();
pblocktemplate->block.vtx.erase(pblocktemplate->block.vtx.begin() + 1, pblocktemplate->block.vtx.end());
pblocktemplate->vTxFees.clear();
pblocktemplate->vTxSigOpsCost.clear();

pblocktemplate->vTxFees.push_back(-1); // updated at end
pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end

nPackagesSelected = 0;
nDescendantsUpdated = 0;
nPopTx = 0;
addPackageTxs<VeriBlock::poptx_priority<ancestor_score>>(nPackagesSelected, nDescendantsUpdated);
addPackageTxs<ancestor_score>(nPackagesSelected, nDescendantsUpdated);
}

pblocktemplate->vchCoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
pblocktemplate->vTxFees[0] = -nFees;

Expand All @@ -172,7 +192,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
pblock->nNonce = 0;
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);

BlockValidationState state;
state = BlockValidationState();
if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
}
Expand Down
3 changes: 2 additions & 1 deletion src/vbk/pop_service/pop_service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ bool blockPopValidationImpl(PopServiceImpl& pop, const CBlock& block, const CBlo

TxValidationState txstate;
assert(blockIndexBegin <= blockIndexEnd && "oh no, programming error");

if (!txPopValidation(pop, tx, pindexPrev, params, txstate, blockIndexIt++)) {
clearTemporaryPayloadsImpl(pop, getReservedBlockIndexBegin(config), blockIndexIt);
mempool.removeRecursive(*tx, MemPoolRemovalReason::BLOCK);
Expand All @@ -681,7 +682,7 @@ bool blockPopValidationImpl(PopServiceImpl& pop, const CBlock& block, const CBlo
// actual addPayloads call is performed in savePopTxToDatabase
clearTemporaryPayloadsImpl(pop, getReservedBlockIndexBegin(config), blockIndexIt);

return state.IsValid();
return true;
}

bool PopServiceImpl::blockPopValidation(const CBlock& block, const CBlockIndex& pindexPrev, const Consensus::Params& params, BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Expand Down
40 changes: 39 additions & 1 deletion src/vbk/test/unit/updated_mempool_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
#include <validation.h>
#include <wallet/wallet.h>

#include <fakeit.hpp>
#include <vbk/init.hpp>
#include <vbk/pop_service/pop_service_impl.hpp>
#include <vbk/test/util/mock.hpp>
#include <vbk/test/util/tx.hpp>

#include <algorithm>
#include <vector>

using namespace fakeit;

BOOST_AUTO_TEST_SUITE(updated_mempool_tests)

struct BlockAssemblerTest : public BlockAssembler {
Expand All @@ -41,7 +46,6 @@ struct BlockAssemblerTest : public BlockAssembler {
}
};


template <typename name>
static void CheckSort(CTxMemPool& pool, std::vector<std::string>& sortedOrder) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
{
Expand Down Expand Up @@ -277,4 +281,38 @@ BOOST_FIXTURE_TEST_CASE(check_the_pop_tx_limits_in_block, TestingSetup)
BOOST_CHECK(blockAssembler.getBlock().vtx.size() == config.max_pop_tx_amount);
}

BOOST_FIXTURE_TEST_CASE(check_CreateNewBlock_with_blockPopValidation_fail, TestingSetup)
{
Fake(Method(pop_service_mock, addPayloads));
Fake(Method(pop_service_mock, removePayloads));
Fake(Method(pop_service_mock, clearTemporaryPayloads));
When(Method(pop_service_mock, determineATVPlausibilityWithBTCRules)).AlwaysReturn(true);

When(Method(pop_service_mock, blockPopValidation)).AlwaysDo([](const CBlock& block, const CBlockIndex& pindexPrev, const Consensus::Params& params, BlockValidationState& state) -> bool { return VeriBlock::blockPopValidationImpl((VeriBlock::PopServiceImpl&)VeriBlock::getService<VeriBlock::PopService>(), block, pindexPrev, params, state); });
When(Method(pop_service_mock, parsePopTx)).AlwaysDo([](const CTransactionRef&, ScriptError* serror, VeriBlock::Publications*, VeriBlock::Context*, VeriBlock::PopTxType* type) -> bool {
if (type != nullptr) {
*type = VeriBlock::PopTxType::CONTEXT;
}
if (serror != nullptr) {
*serror = ScriptError::SCRIPT_ERR_OK;
}
return true; });

// Simulate that we have 8 invalid popTxs
When(Method(pop_service_mock, updateContext)).Throw(8_Times(VeriBlock::PopServiceException("fail"))).AlwaysDo([](const std::vector<std::vector<uint8_t>>& veriBlockBlocks, const std::vector<std::vector<uint8_t>>& bitcoinBlocks) {});

TestMemPoolEntryHelper entry;
for (size_t i = 0; i < 10; ++i) {
LOCK2(cs_main, mempool.cs);
CMutableTransaction popTx = VeriBlockTest::makePopTx({1}, {std::vector<uint8_t>(100, i)});
mempool.addUnchecked(entry.Fee(0LL).FromTx(popTx));
}

BlockAssembler blkAssembler(Params());
CScript scriptPubKey = CScript() << OP_CHECKSIG;
std::unique_ptr<CBlockTemplate> pblockTemplate = blkAssembler.CreateNewBlock(scriptPubKey);

BOOST_TEST(pblockTemplate->block.vtx.size() == 3);
}

BOOST_AUTO_TEST_SUITE_END()
0