Skip to content

Commit

Permalink
Separate creation and initialisation of auxpow's.
Browse files Browse the repository at this point in the history
Instead of both creating and already setting the "minimal" auxpow in
initAuxPow, this logic is split into two parts:  The construction of the
auxpow itself, and the logic that modifies the header.

Furthermore, we now return a reference to the parent block header from
initAuxPow, so it can be more easily mined.  This allows us to get rid
of the general accessor getParentBlockHeader, and replace it by just a
function returning the parent block hash (as before) to verify a PoW.
  • Loading branch information
domob1812 committed Jun 25, 2018
1 parent 5fe7f42 commit d8c4475
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
26 changes: 20 additions & 6 deletions src/auxpow.cpp
Expand Up @@ -20,7 +20,7 @@
#include <validation.h>

#include <algorithm>
#include <memory>
#include <cassert>

/* Moved from wallet.cpp. CMerkleTx is necessary for auxpow, independent
of an enabled (or disabled) wallet. Always include the code. */
Expand Down Expand Up @@ -188,11 +188,10 @@ CAuxPow::CheckMerkleBranch (uint256 hash,
return hash;
}

void
CAuxPow::initAuxPow (CBlockHeader& header)
std::unique_ptr<CAuxPow>
CAuxPow::createAuxPow (const CPureBlockHeader& header)
{
/* Set auxpow flag right now, since we take the block hash below. */
header.SetAuxpowVersion(true);
assert (header.IsAuxpow ());

/* Build a minimal coinbase script input for merge-mining. */
const uint256 blockHash = header.GetHash ();
Expand Down Expand Up @@ -224,5 +223,20 @@ CAuxPow::initAuxPow (CBlockHeader& header)
assert (auxpow->vMerkleBranch.empty ());
auxpow->nIndex = 0;
auxpow->parentBlock = parent;
header.SetAuxpow (std::move (auxpow));

return auxpow;
}

CPureBlockHeader&
CAuxPow::initAuxPow (CBlockHeader& header)
{
/* Set auxpow flag right now, since we take the block hash below when creating
the minimal auxpow for header. */
header.SetAuxpowVersion(true);

std::unique_ptr<CAuxPow> apow = createAuxPow (header);
CPureBlockHeader& result = apow->parentBlock;
header.SetAuxpow (std::move (apow));

return result;
}
36 changes: 19 additions & 17 deletions src/auxpow.h
Expand Up @@ -13,6 +13,7 @@
#include <serialize.h>
#include <uint256.h>

#include <memory>
#include <vector>

class CBlock;
Expand Down Expand Up @@ -162,18 +163,13 @@ class CAuxPow : public CMerkleTx
bool check (const uint256& hashAuxBlock, int nChainId,
const Consensus::Params& params) const;

/* Accessors for the parent block. */

inline CPureBlockHeader&
getParentBlock ()
{
return parentBlock;
}

inline const CPureBlockHeader&
getParentBlock () const
/**
* Returns the parent block hash. This is used to validate the PoW.
*/
inline uint256
getParentBlockHash () const
{
return parentBlock;
return parentBlock.GetHash ();
}

/**
Expand All @@ -195,13 +191,19 @@ class CAuxPow : public CMerkleTx
int nIndex);

/**
* Initialise the auxpow of the given block header. This constructs
* a minimal CAuxPow object with a minimal parent block and sets
* it on the block header. The auxpow is not necessarily valid, but
* can be "mined" to make it valid.
* @param header The header to set the auxpow on.
* Constructs a minimal CAuxPow object for the given block header and
* returns it. The caller should make sure to set the auxpow flag on the
* header already, since the block hash to which the auxpow commits depends
* on that!
*/
static std::unique_ptr<CAuxPow> createAuxPow (const CPureBlockHeader& header);

/**
* Initialises the auxpow of the given block header. This builds a minimal
* auxpow object like createAuxPow and sets it on the block header. Returns
* a reference to the parent header so it can be mined as a follow-up.
*/
static void initAuxPow (CBlockHeader& header);
static CPureBlockHeader& initAuxPow (CBlockHeader& header);

};

Expand Down
3 changes: 1 addition & 2 deletions src/rpc/mining.cpp
Expand Up @@ -128,8 +128,7 @@ UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGen
LOCK(cs_main);
IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce);
}
CAuxPow::initAuxPow(*pblock);
CPureBlockHeader& miningHeader = pblock->auxpow->getParentBlock();
auto& miningHeader = CAuxPow::initAuxPow(*pblock);
while (nMaxTries > 0 && miningHeader.nNonce < nInnerLoopCount && !CheckProofOfWork(miningHeader.GetHash(), pblock->nBits, Params().GetConsensus())) {
++miningHeader.nNonce;
--nMaxTries;
Expand Down
4 changes: 1 addition & 3 deletions src/test/validation_block_tests.cpp
Expand Up @@ -72,9 +72,7 @@ std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock)
{
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);

CAuxPow::initAuxPow(*pblock);
CPureBlockHeader& miningHeader = pblock->auxpow->getParentBlock();

auto& miningHeader = CAuxPow::initAuxPow(*pblock);
while (!CheckProofOfWork(miningHeader.GetHash(), pblock->nBits, Params().GetConsensus())) {
++(miningHeader.nNonce);
}
Expand Down
2 changes: 1 addition & 1 deletion src/validation.cpp
Expand Up @@ -1103,7 +1103,7 @@ bool CheckProofOfWork(const CBlockHeader& block, const Consensus::Params& params

if (!block.auxpow->check(block.GetHash(), block.GetChainId(), params))
return error("%s : AUX POW is not valid", __func__);
if (!CheckProofOfWork(block.auxpow->getParentBlock().GetHash(), block.nBits, params))
if (!CheckProofOfWork(block.auxpow->getParentBlockHash(), block.nBits, params))
return error("%s : AUX proof of work failed", __func__);

return true;
Expand Down

0 comments on commit d8c4475

Please sign in to comment.