Skip to content

Commit

Permalink
Check auxpow PoW before the auxpow itself.
Browse files Browse the repository at this point in the history
This reverses the order in which the auxpow structure and the PoW on it
are verified.  By checking the PoW first, we make it harder for random
nodes to pass data of their choosing into CAuxPow::check.

Cherry-picked from: namecoin/namecoin-core@b6e3241b

Conflicts resolved:
- CAuxPow member name mismatch
- source code from validation.cpp resides in dogecoin.cpp
  • Loading branch information
domob1812 authored and patricklodder committed Nov 30, 2024
1 parent b4a5d2b commit 51cbc1f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/auxpow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ CAuxPow::check(const uint256& hashAuxBlock, int nChainId,
!= parentBlock.hashMerkleRoot)
return error("Aux POW merkle root incorrect");

// Check that there is at least one input.
if (tx->vin.empty())
return error("Aux POW coinbase has no inputs");

const CScript script = tx->vin[0].scriptSig;

// Check that the same work is not submitted twice to our chain.
Expand Down
5 changes: 3 additions & 2 deletions src/dogecoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ bool CheckAuxPowProofOfWork(const CBlockHeader& block, const Consensus::Params&
if (!block.IsAuxpow())
return error("%s : auxpow on block with non-auxpow version", __func__);

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

if (!block.auxpow->check(block.GetHash(), block.GetChainId(), params))
return error("%s : AUX POW is not valid", __func__);

return true;
}

Expand Down
9 changes: 9 additions & 0 deletions src/test/auxpow_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ BOOST_AUTO_TEST_CASE(check_auxpow)
builder.setCoinbase(scr);
BOOST_CHECK(builder.get().check(hashAux, ourChainId, params));

/* An auxpow without any inputs in the parent coinbase tx should be
handled gracefully (and be considered invalid). */
CMutableTransaction mtx(*builder.parentBlock.vtx[0]);
mtx.vin.clear();
builder.parentBlock.vtx.clear();
builder.parentBlock.vtx.push_back (MakeTransactionRef(std::move (mtx)));
builder.parentBlock.hashMerkleRoot = BlockMerkleRoot(builder.parentBlock);
BOOST_CHECK(!builder.get().check(hashAux, ourChainId, params));

/* Check that the auxpow is invalid if we change either the aux block's
hash or the chain ID. */
uint256 modifiedAux(hashAux);
Expand Down

0 comments on commit 51cbc1f

Please sign in to comment.