Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Warchant committed Apr 20, 2020
1 parent 809f134 commit 36e8bc2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
24 changes: 16 additions & 8 deletions src/test/util/setup_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,15 @@ TestChain100Setup::TestChain100Setup(): RegTestingSetup()
assert(ChainActive().Tip()->nHeight == 100);
}

// Create a new block with just given transactions, coinbase paying to
// scriptPubKey, and try to add it to the current chain.
CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey) {
return this->CreateAndProcessBlock(txns, scriptPubKey, nullptr);
}
CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, uint256 prevBlock,
const CScript& scriptPubKey, bool* isBlockValid) {
CBlockIndex* pPrev = nullptr;
{
LOCK(cs_main);
pPrev = LookupBlockIndex(prevBlock);
assert(pPrev && "CreateAndProcessBlock called with unknown prev block");
}

CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey, bool* isBlockValid)
{
const CChainParams& chainparams = Params();
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
CBlock& block = pblocktemplate->block;
Expand All @@ -200,7 +201,7 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
{
LOCK(cs_main);
unsigned int extraNonce = 0;
IncrementExtraNonce(&block, ::ChainActive().Tip(), extraNonce);
IncrementExtraNonce(&block, pPrev, extraNonce);
}

while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
Expand All @@ -216,6 +217,13 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
return result;
}

// Create a new block with just given transactions, coinbase paying to
// scriptPubKey, and try to add it to the current chain.
CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey, bool* isBlockValid)
{
return CreateAndProcessBlock(txns, ChainActive().Tip()->GetBlockHash(), scriptPubKey, isBlockValid);
}

TestChain100Setup::~TestChain100Setup()
{
gArgs.ForceSetArg("-segwitheight", "0");
Expand Down
7 changes: 4 additions & 3 deletions src/test/util/setup_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ struct TestChain100Setup : public RegTestingSetup {
// Create a new block with just given transactions, coinbase paying to
// scriptPubKey, and try to add it to the current chain.
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey);
const CScript& scriptPubKey, bool* isBlockValid = nullptr);

CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, uint256 prevBlock,
const CScript& scriptPubKey, bool* isBlockValid = nullptr);

CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey, bool* isBlockValid);

~TestChain100Setup();

Expand Down
16 changes: 12 additions & 4 deletions src/vbk/test/unit/e2e_poptx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ struct E2eFixture : public TestChain100Setup {
pop = &VeriBlock::getService<VeriBlock::PopService>();
}


CBlock endorseAltBlock(uint256 hash, size_t generateVtbs = 0)
{
return endorseAltBlock(hash, ChainActive().Tip()->GetBlockHash(), generateVtbs);
}

CBlock endorseAltBlock(uint256 hash, uint256 prevBlock, size_t generateVtbs = 0)
{
CBlockIndex* endorsed = nullptr;
{
Expand Down Expand Up @@ -55,7 +61,7 @@ struct E2eFixture : public TestChain100Setup {

auto tx = VeriBlock::MakePopTx(sig);
bool isValid = false;
return CreateAndProcessBlock({tx}, cbKey, &isValid);
return CreateAndProcessBlock({tx}, prevBlock, cbKey, &isValid);
}

VTB endorseVbkTip()
Expand Down Expand Up @@ -130,7 +136,7 @@ struct E2eFixture : public TestChain100Setup {

BOOST_AUTO_TEST_SUITE(e2e_poptx_tests)

BOOST_FIXTURE_TEST_CASE(BlockWith10ValidPopTxes, E2eFixture)
BOOST_FIXTURE_TEST_CASE(ValidBlockIsAccepted, E2eFixture)
{
// altintegration and popminer configured to use BTC/VBK/ALT regtest.
auto tip = ChainActive().Tip();
Expand All @@ -157,9 +163,11 @@ BOOST_FIXTURE_TEST_CASE(BlockWith10ValidPopTxes, E2eFixture)
BOOST_CHECK(vbk == popminer.vbk().getBestChain().tip()->getHash());
}

// create block that is not on main chain
auto fork1tip = CreateAndProcessBlock({}, ChainActive().Tip()->pprev->pprev->GetBlockHash(), cbKey);

// endorse block that is not on main chain
// TODO:
block = endorseAltBlock(uint256(), 1);
block = endorseAltBlock(fork1tip.GetHash(), 1);
BOOST_CHECK(ChainActive().Tip()->GetBlockHash() == lastHash);
}

Expand Down

0 comments on commit 36e8bc2

Please sign in to comment.