Skip to content

feat: AuxPoW merge-mining (v30.4.0, activates at height 80000) - #3

Merged
Blocklechain merged 5 commits into
masterfrom
feat/auxpow
Oct 20, 2026
Merged

Blocklechain merged 5 commits into
masterfrom
feat/auxpow

Conversation

@Blocklechain

Copy link
Copy Markdown
Owner

Adds Namecoin/Dogecoin-style AuxPoW merge-mining so large SHA-256 pools can keep Perbug alive at no cost to their miners (Andy's suggestion #2). Activates at height 80,000 (chain id 0x0062); before that nothing changes.

What's in it

  • Pure-header split (primitives/pureheader.*) + the version/chain-ID/aux-flag scheme.
  • CAuxPow (auxpow.*): parent coinbase + merkle branches + parent header, the wire format, and check() — merkle-branch verification + merged-mining magic scan + size/nonce index.
  • CBlockHeader now derives CPureBlockHeader and carries an optional auxpow (serialised after the pure header iff the aux flag is set).
  • PoW: CheckProofOfWork(header) resolves AuxPoW (parent-chain PoW + commitment); else normal PoW.
  • Validation: no AuxPoW before 80,000; chain id enforced after.
  • Miner: templates carry the chain id in the version from the fork (so even non-merge-mined pool blocks validate).
  • RPC: createauxblock / submitauxblock.
  • Mainnet fork 80,000 / chain id 0x0062; regtest fork 1 + non-strict for tests.

Verification

  • auxpow_tests unit tests: valid proof accepted; wrong hash, stripped commitment, bad merkle root, coinbase-not-at-0 all rejected.
  • Live regtest: miner tags blocks 0x00620004, validation accepts them, createauxblock returns a valid template.
  • Live merge-mine round-trip (contrib/auxpow/auxpow_roundtrip.py): createauxblock → build a real AuxPoW → submitauxblock → block accepted, chain extends. ✅

Before activation

HARD FORK: all nodes/pools must run v30.4.0 before 80,000. Authoritative wire-format check = a live merge-mine test against Andy's real SHA-256 pool (planned). Also carries the LWMA fixes already on master.

bonkcoins and others added 5 commits October 20, 2026 09:24
…ow + check()

Foundation for merge-mining (Namecoin/Dogecoin-compatible), targeting fork
height 80000 as v30.4.0. NOT yet wired into the block/validation/RPC paths.

- primitives/pureheader.{h,cpp}: CPureBlockHeader (the 80-byte PoW-committed
  header) + version helpers (chain id in bits 16..31, AuxPoW flag bit 8).
- auxpow.{h,cpp}: CAuxPow (parent coinbase + merkle branches + parent header),
  the wire format, CAuxPow::check() (coinbase-merkle + chain-merkle + the
  merged-mining magic scan + size/nonce index), CheckMerkleBranch helper.

Remaining: CBlockHeader refactor to derive CPureBlockHeader + carry auxpow,
pow.cpp CheckProofOfWork(header) integration, chainparams aux params,
ContextualCheckBlockHeader version enforcement, getauxblock/submitauxblock RPC,
auxpow_tests, CMakeLists. Gated behind those tests + live merge-mine interop
testing with Andy before the 80000 activation.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hi9Fj21YnCdsu73fPDkw6o
…core builds)

- CBlockHeader now derives CPureBlockHeader and carries an optional CAuxPow,
  serialised after the pure header iff the version has the AuxPoW flag.
- pow: CheckProofOfWork(header) resolves AuxPoW — the parent-chain block must
  meet our nBits and commit this block's hash; else normal PoW. Call sites in
  CheckBlockHeader + HasValidProofOfWork updated.
- validation: ContextualCheckBlockHeader rejects AuxPoW before nAuxpowStartHeight
  and enforces the chain id from the fork onward.
- chainparams: mainnet AuxPoW fork at 80000, chain id 0x0062; regtest fork at 1,
  non-strict chain id for round-trip tests. consensus/params.h gains
  nAuxpowChainId / nAuxpowStartHeight / fStrictChainId.
- CMake: auxpow.cpp + primitives/pureheader.cpp in perbug_consensus.

perbugd builds + links + runs with this. Remaining: createauxblock/submitauxblock
RPC + getblocktemplate chain-id, auxpow_tests, regtest merge-mine round-trip.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hi9Fj21YnCdsu73fPDkw6o
…erified)

CreateAuxPow builds a minimal valid merge-mining proof (single-slot chain tree,
single-tx parent) for tests + the regtest round-trip. auxpow_tests exercises
CAuxPow::check(): a valid proof is accepted; a wrong aux hash, a stripped
coinbase commitment, a corrupted parent merkle root, and a coinbase not at
index 0 are all rejected. Plus a CheckMerkleBranch identity check. All green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hi9Fj21YnCdsu73fPDkw6o
…bmitauxblock RPC

- node/miner.cpp: from nAuxpowStartHeight every template carries our chain id in
  the version (SetBaseVersion), so even non-merge-mined blocks from the existing
  pool validate post-fork.
- rpc/mining.cpp: createauxblock returns the hash to commit in the parent
  coinbase (+ chain id, bits, target); submitauxblock takes that hash + the
  serialised AuxPoW, attaches it, and submits via ProcessNewBlock. A small
  template cache maps the committed hash back to the block.

perbugd + perbug-cli build clean; both commands register.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hi9Fj21YnCdsu73fPDkw6o
… round-trip passes

- createauxblock: getBlock() leaves hashMerkleRoot unfinalised, so the submitted
  block was rejected bad-txnmrklroot; recompute BlockMerkleRoot before caching +
  hashing the template.
- contrib/auxpow/auxpow_roundtrip.py: a standalone regtest pool simulator —
  createauxblock -> build a valid AuxPoW (parent coinbase committing the aux
  hash, solved to target) -> submitauxblock. Verified live: block accepted,
  chain 20 -> 21.
- Dropped the unit submit test (TestChain100Setup's mock clock vs Perbug's 150s
  spacing trips the future-time check inside BlockAssembler); the live round-trip
  covers that path instead. check()/merkle unit tests remain.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hi9Fj21YnCdsu73fPDkw6o
@Blocklechain
Blocklechain merged commit 4ee7a09 into master Oct 20, 2026
4 of 25 checks passed
@hypothesis-next

Copy link
Copy Markdown

Bug report (issues are disabled on this repo, so posting here on the change that added merge-mining)

v30.4.2: merge-mined blocks break restart, wallet rescan, and header serving (assert auxpow != nullptr)

Thanks for adding merge-mining. Running v30.4.2 (tag at 77ea9a5, built from source on Linux x86_64), we hit two problems once the chain is past the AuxPoW activation height (80,000). Both look like they would affect any node that restarts, including pool and explorer nodes.

1. Restart fails, and rescans skip every merge-mined block

Steps: sync a fresh node past 80,000, stop it cleanly, start it again.

Result: start-up aborts with Error loading block database. Separately, a wallet rescan (e.g. after importdescriptors) cannot read any block from about 80,009 on, so imported keys miss every payment since merge-mining began.

Cause (as far as we can tell): src/node/blockstorage.cpp checks a merge-mined block's own hash against nBits in two places:

  • BlockTreeDB::LoadBlockIndexGuts — CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, ...)
  • BlockManager::ReadBlock — CheckProofOfWork(block_hash, block.nBits, ...)

For an AuxPoW block that hash never meets the target (the work is in the parent block), so both checks fail.

Local fix we tested (doesn't change which blocks are accepted): skip the hash-only check for blocks with the AuxPoW version flag when loading the index, and use the header overload, which resolves the AuxPoW, in ReadBlock:

--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -134,7 +134,8 @@ bool BlockTreeDB::LoadBlockIndexGuts(...)
-                if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
+                if (!(pindexNew->nVersion & CPureBlockHeader::VERSION_AUXPOW) &&
+                    !CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
@@ -1017,8 +1018,8 @@ bool BlockManager::ReadBlock(...)
-    if (!CheckProofOfWork(block_hash, block.nBits, GetConsensus())) {
+    if (!CheckProofOfWork(static_cast<const CBlockHeader&>(block), GetConsensus())) {

With this, the node restarts cleanly and loads its index.

2. Node aborts on obj.auxpow != nullptr once at the tip

Steps: with the fix above applied, let the node sync to the tip and talk to a peer.

Result: within about a second it aborts on the assertion obj.auxpow != nullptr in CBlockHeader serialisation.

Cause (as far as we can tell): headers built from the block index (the getheaders reply and headers announcements in net_processing; getblockheader hex and REST probably too) carry the AuxPoW version flag but no AuxPoW, because the index doesn't store it. Namecoin handles this by reading the full header from disk on those paths when the flag is set. We haven't patched this part.

Nodes that have run nonstop since before block 80,000 probably haven't seen either problem yet, but will after their next restart.

Happy to test a fixed release. Thanks!

@Blocklechain

Copy link
Copy Markdown
Owner Author

Both confirmed, both fixed, shipped in v30.4.3. Thank you — the diagnosis was correct on both counts and detailed enough to patch from directly.

Two things worth adding to your report.

This was already happening in production, not just theoretically. Our own mainnet node had been crash-looping on exactly this, 20,034 restarts, on exactly the error you predicted:

[error] LoadBlockIndexGuts: CheckProofOfWork failed: CBlockIndex(... nHeight=80011 ...)
: Error loading block database.

80,011 is the first merge-mined block after activation. It took the block explorer's sync down with it. Your line about nodes that have run nonstop "probably haven't seen either problem yet, but will after their next restart" was exactly right, and for us that restart had already happened.

Your second problem had already been patched by @colneech-dev in #4, opened three days before your report and covering all five affected call sites including the ones you flagged as unpatched. That PR sat unreviewed because GitHub gates workflow runs for first-time contributors and nobody approved it. That is on us, not on either of you.

What shipped

ReadBlock now uses the CBlockHeader overload, which resolves the AuxPoW and checks the parent against nBits. Worth being explicit that this is stronger than the check it replaces, not a relaxation — identical for ordinary blocks, and an actual verification for merge-mined ones where there was previously a guaranteed failure.

LoadBlockIndexGuts skips the hash-only check for AuxPoW-flagged entries, as you proposed. Unavoidable there: CBlockIndex does not store the AuxPoW, so the real check is impossible. It does not change which blocks are accepted — this is the local index being reloaded, every entry was fully validated when first connected, and anything arriving over the network still goes through the complete check.

For the header path, a new ReadBlockHeader(const CBlockIndex&) returns std::optional<CBlockHeader> and reads the proof back from disk. One detail that shapes the fix: the AuxPoW flag lives in nVersion, which is part of the block hash, so it cannot simply be cleared to make a header serialisable — that would be a different block. Either the proof is loaded or no header goes out. Used by the getheaders reply (which stops, since headers must be contiguous), both headers-announcement paths (fall back to an inv), getblockheader's hex output, and both REST header endpoints.

Upgrading

No reindex is needed. It is a code-only fix; existing datadirs load as they are. The -reindex the old binary suggests is not necessary and on a large datadir would cost hours for nothing.

Verified on live mainnet against a datadir that had been accumulating merge-mined blocks since the fork: loads its index, reaches the tip, stays up with peers connected with zero assert hits, getblock reads block 80,011, and getblockheader <hash> false returns hex.

Upgrade urgency is high for anyone on v30.4.x. A node that has not restarted since before block 80,000 has not hit this yet but will the moment it stops — including after a crash or a reboot.

You offered to test a fixed release: that would be genuinely useful, particularly the headers path under real peer traffic, which is the part we have exercised least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants