Skip to content

Commit d8c4475

Browse files
committed
Separate creation and initialisation of auxpow's.
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.
1 parent 5fe7f42 commit d8c4475

File tree

5 files changed

+42
-29
lines changed

5 files changed

+42
-29
lines changed

src/auxpow.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <validation.h>
2121

2222
#include <algorithm>
23-
#include <memory>
23+
#include <cassert>
2424

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

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

197196
/* Build a minimal coinbase script input for merge-mining. */
198197
const uint256 blockHash = header.GetHash ();
@@ -224,5 +223,20 @@ CAuxPow::initAuxPow (CBlockHeader& header)
224223
assert (auxpow->vMerkleBranch.empty ());
225224
auxpow->nIndex = 0;
226225
auxpow->parentBlock = parent;
227-
header.SetAuxpow (std::move (auxpow));
226+
227+
return auxpow;
228+
}
229+
230+
CPureBlockHeader&
231+
CAuxPow::initAuxPow (CBlockHeader& header)
232+
{
233+
/* Set auxpow flag right now, since we take the block hash below when creating
234+
the minimal auxpow for header. */
235+
header.SetAuxpowVersion(true);
236+
237+
std::unique_ptr<CAuxPow> apow = createAuxPow (header);
238+
CPureBlockHeader& result = apow->parentBlock;
239+
header.SetAuxpow (std::move (apow));
240+
241+
return result;
228242
}

src/auxpow.h

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <serialize.h>
1414
#include <uint256.h>
1515

16+
#include <memory>
1617
#include <vector>
1718

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

165-
/* Accessors for the parent block. */
166-
167-
inline CPureBlockHeader&
168-
getParentBlock ()
169-
{
170-
return parentBlock;
171-
}
172-
173-
inline const CPureBlockHeader&
174-
getParentBlock () const
166+
/**
167+
* Returns the parent block hash. This is used to validate the PoW.
168+
*/
169+
inline uint256
170+
getParentBlockHash () const
175171
{
176-
return parentBlock;
172+
return parentBlock.GetHash ();
177173
}
178174

179175
/**
@@ -195,13 +191,19 @@ class CAuxPow : public CMerkleTx
195191
int nIndex);
196192

197193
/**
198-
* Initialise the auxpow of the given block header. This constructs
199-
* a minimal CAuxPow object with a minimal parent block and sets
200-
* it on the block header. The auxpow is not necessarily valid, but
201-
* can be "mined" to make it valid.
202-
* @param header The header to set the auxpow on.
194+
* Constructs a minimal CAuxPow object for the given block header and
195+
* returns it. The caller should make sure to set the auxpow flag on the
196+
* header already, since the block hash to which the auxpow commits depends
197+
* on that!
198+
*/
199+
static std::unique_ptr<CAuxPow> createAuxPow (const CPureBlockHeader& header);
200+
201+
/**
202+
* Initialises the auxpow of the given block header. This builds a minimal
203+
* auxpow object like createAuxPow and sets it on the block header. Returns
204+
* a reference to the parent header so it can be mined as a follow-up.
203205
*/
204-
static void initAuxPow (CBlockHeader& header);
206+
static CPureBlockHeader& initAuxPow (CBlockHeader& header);
205207

206208
};
207209

src/rpc/mining.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGen
128128
LOCK(cs_main);
129129
IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce);
130130
}
131-
CAuxPow::initAuxPow(*pblock);
132-
CPureBlockHeader& miningHeader = pblock->auxpow->getParentBlock();
131+
auto& miningHeader = CAuxPow::initAuxPow(*pblock);
133132
while (nMaxTries > 0 && miningHeader.nNonce < nInnerLoopCount && !CheckProofOfWork(miningHeader.GetHash(), pblock->nBits, Params().GetConsensus())) {
134133
++miningHeader.nNonce;
135134
--nMaxTries;

src/test/validation_block_tests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock)
7272
{
7373
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
7474

75-
CAuxPow::initAuxPow(*pblock);
76-
CPureBlockHeader& miningHeader = pblock->auxpow->getParentBlock();
77-
75+
auto& miningHeader = CAuxPow::initAuxPow(*pblock);
7876
while (!CheckProofOfWork(miningHeader.GetHash(), pblock->nBits, Params().GetConsensus())) {
7977
++(miningHeader.nNonce);
8078
}

src/validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ bool CheckProofOfWork(const CBlockHeader& block, const Consensus::Params& params
11031103

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

11091109
return true;

0 commit comments

Comments
 (0)