Skip to content

dash: getblocktemplate txids follow the Dash Core v24 stable-txid rule for asset unlocks - #2363

Merged
frstrtr merged 4 commits into
masterfrom
dash/gbt-stable-txid
Nov 28, 2026
Merged

frstrtr merged 4 commits into
masterfrom
dash/gbt-stable-txid

Conversation

@frstrtr

@frstrtr frstrtr commented Nov 28, 2026

Copy link
Copy Markdown
Owner

What this changes

1. The txids of a dashd getblocktemplate reply (coin/gbt_transactions.hpp, used by NodeRPC::getwork).
Each transaction's id is now the id the block's merkle root commits to:

  • the entry has a "hash": dashd's "hash". It is Core's own GetHash() of the very bytes the block body carries, so it is the id the validator recomputes. When the bytes also deserialize completely, dash_txid of the parsed tx is cross-checked against it; a disagreement can only mean a defect in dash_txid (which the embedded build uses too), so it is logged as an error once per txid, with the running count of disagreeing entries;
  • no "hash", bytes that deserialize completely (no trailing bytes): dash_txid of the parsed tx — the same derivation the embedded build and every other tx-hash site of the node already use;
  • neither: the SHA256d of the bytes (logged).

An entry without a "data" string, or with an empty or non-hex body, fails the whole read and leaves the work data untouched (a template that silently dropped one of dashd's txs would no longer be dashd's template). A "hash" that is not a 64-char hex id is treated as absent and reported; a "txid" that differs from "hash" is reported ("hash" is served). Disagreements are logged as one summary line per template read plus one detail line per txid the first time; the remembered set stops growing at its cap instead of being cleared, so a systemic defect cannot flood the log.

Before, the reader looked for a "txid" key, which Dash getblocktemplate does not send (its entries carry data, hash, depends, fee, sigops), and so always used the SHA256d of the entry's bytes.

2. The producer-job body memo (TemplateBodiesMemo, mint_runloop.hpp).
It reused a stored body vector whenever the tx-hash list and the body count matched. It now also requires byte equality of every body whose txid may leave bytes out (every type-9 body), through bodies_match_under_equal_txids in coin/rpc_data.hpp. The template store's reuse rule (template_tx_hex_reusable) already applied this check and now calls the same predicate (behaviour unchanged).

3. The producer job cache (ProducerJobCache::find, mint_runloop.hpp).
A hit now also needs the template's CbTx payload to equal the cached job's. A version 2 unlock that dashd re-signs at the same tip keeps its txid, so tip, height, payment amount and tx-hash list all stay equal while the cached gentx's merkleRootAssetUnlocks commits the previous instance; served with the current bodies, a found block would be rejected as bad-cbtx-assetunlockmerkleroot. The payload commits the instances, so a re-signed unlock is a miss and the job is rebuilt.

4. The finder's block bodies (v36_finder_block_bodies, reconstruct_won_block.hpp).
A body whose txid may leave bytes out (every type-9 body) must parse completely and its dash_txid must equal the listed txid; every other body still hashes to it. With stable txids in the template, the redundant re-broadcast arm refused every block carrying a version 2 unlock (the stratum arm still submitted).

5. The embedded-vs-dashd tx-root cross-check (work_source.cpp, compute_merkle_root(m_tx_hashes) on both sides) now folds the same ids on both sides; before, a dashd template carrying a version 2 unlock folded instance hashes against the embedded build's txids.

Why

From Dash Core v24, the txid (CTransaction::GetHash) of a version 2 asset unlock is computed with its 132 bytes of quorum signing info zeroed, so that every re-signed instance of one withdrawal keeps one txid. The SHA256d of its full bytes is its instance hash (committed separately by the CbTx v4 merkleRootAssetUnlocks). m_tx_hashes feeds the stratum merkle branches, so every job built over a dashd template that carried a version 2 unlock had a header root dashd rejects with bad-txnmrklroot. A block found on such a job would be lost; it cannot be repaired at submit time because the proof of work covers the header.

On the v24 testnet shadow, every dashd-template proposal that carried version 2 unlocks was rejected bad-txnmrklroot (34 of 34), while embedded templates and dashd templates without unlocks were accepted. A live capture proposed the same block body twice: with the node's served root, bad-txnmrklroot; with the root folded over dashd's txids, accepted.

The memo change is required by (1): once dashd templates are read with the stable txid, two templates over the same ids can differ in an unlock's bytes (a re-signed instance), and the memo could otherwise hand a job the previous instance's bytes.

Review fixes

An independent review asked for (3) and (4) above, the reader hardening (entries without bytes fail the read, malformed "hash", differing "txid"), the bounded disagreement log, and comment fixes at main_dash.cpp (cache hit rule), block_producer.hpp (coinbase txid) and reconstruct_won_block.hpp (body rule). All are in the last commit.

Follow-up (not in this PR): the share-relay known-tx store (known_txs_retention.hpp, register_template_txs) keys bodies by txid and keeps the first body it saw for a template; a re-signed version 2 unlock under the same txid can leave the previous instance's bytes there for relay.

Exposure

  • Affected: nodes that serve a dashd getblocktemplate — the dashd-fallback arm of an embedded node, or a node without the embedded arm — at heights where dashd's template holds a version 2 asset unlock. On an embedded node this is systematic, not occasional: the embedded build leaves pending asset unlocks out, the creditPool cross-check then declines, and the dashd template is served at exactly those heights.
  • When: mainnet from v24 ACTIVE, once version 2 unlocks reach mempools. Before v24 a version 2 unlock is invalid (bad-assetunlocktx-version-2), and a version 1 unlock's txid is the SHA256d of its bytes, so mainnet is unaffected today.
  • Not affected: daemonless / embedded templates (they derive ids with dash_txid already).

Tests

All in test_dash_share_hash_link (allowlisted target), on two real Dash Core 24.0.2 testnet blocks stored with the existing generator (tools/dash/gen_replay_kat.py block): test/data/dash_block_testnet_1599900.inc (16 txs, 11 version 2 unlocks) and test/data/dash_block_testnet_1599924.inc (48 txs, 9 version 2 unlocks; the block that closed the height of the live capture above), with dashd's own txid lists and merkle roots in test/data/dash_block_testnet_txids_1599900_1599924.inc.

KAT (DashGbtStableTxid.*) What it pins
FixturesAreRealV2UnlockBlocks the codec round-trips both blocks byte for byte; dash_txid of every tx equals dashd's txid; the SHA256d of the bytes differs from it exactly for the v2 unlocks (11 and 9)
Block1599900EntriesReadWithDashdTxids every m_tx_hashes[i] == dashd's hash (e.g. 1dcf9b62…c422, not the instance hash ec92ca6b…8e3a); [coinbase txid] + ids and the stratum branch ascent == the committed root abd5d782cd7d0071a6f63472426074053eb5703dd74d70fff500da4d22c0f827
Block1599924EntriesReadWithDashdTxids the same over 48 txs; root af091c1bccc019d924511cdfa4f4925bd80752a23fa18694b4dbfea02acf71a2
OldRuleFoldsTheRejectedRootFixedRuleTheCommittedOne the old rule folds the instance-hash roots (257a0908…6989, f9282862…59ea), not the header root; the fixed reader folds the header root
WithoutGbtHashTheStableTxidIsDerived entries without hash still get dashd's txids (derived with dash_txid)
UnparsableBytesUseDashdHash truncated bytes and trailing bytes take dashd's hash; with no hash either, the SHA256d of the bytes; both reported; the body is kept verbatim
BareStringEntriesReadAndEntriesWithoutBytesFailTheRead bare-hex entries are read; an entry without "data", with non-string, empty, odd-length or non-hex "data", or a non-entry value fails the read and leaves the work data untouched
MalformedHashIsTreatedAsAbsentAndReported a "hash" that is short, long, non-hex or not a string is ignored (the bytes decide) and reported
TxidThatDiffersFromHashIsReportedHashServed "hash" is served; a differing "txid" is reported, an equal one is not
DisagreeingHashIsReportedAndDashdHashDecides a hash that differs from the bytes' dash_txid is served, and reported with the derived txid next to it; an agreeing entry is not reported; without a hash the bytes decide
EmbeddedAndDashdBodyFoldsAgreeOverTheSameTxs the embedded build's ids and the dashd reader's ids fold to the same body root (the cross-check compares like with like)
ResignedUnlockSharesTheTxidNotTheBytes a re-signed instance of a real v2 unlock keeps its stable txid and changes its instance hash and bytes
BodiesMemoNeverServesAResignedUnlockWithOldBytes while the first instance's vector is still held, the memo returns each instance's own bytes
TemplateStoreReuseRejectsAResignedUnlock the store's reuse rule shares one vector for equal bodies and refuses a re-signed one
ProducerJobCacheMissesOnAResignedUnlock the real CbTx v4 payload of block 1599900 commits its unlocks' instance hashes; the same block with one unlock re-signed (same txids, recomputed merkleRootAssetUnlocks) is a miss (drift); the original template still hits
DashV36FinderBlock.FinderBodiesAcceptRealV2Unlocks (test_dash_node) block 1599900's bodies pass v36_finder_block_bodies under a merkle link over dashd's txids; a type-9 body with trailing bytes is refused

Mutants (each must turn named KATs RED)

Mutant RED KATs
M1 the old rule: txid = SHA256d of the bytes all reader KATs (10 of 12; the two memo KATs fail on their equal-txid precondition)
M2 dashd's hash ignored when the bytes do not deserialize UnparsableBytesUseDashdHash
M3 trailing bytes after a tx count as a complete parse UnparsableBytesUseDashdHash
M4 a disagreeing hash is not reported DisagreeingHashIsReportedAndDashdHashDecides
M7 the bytes decide on a disagreement (dash_txid served over dashd's hash) DisagreeingHashIsReportedAndDashdHashDecides
M8 the cache's old hit rule (no CbTx payload equality) MUTANT_M8
M9 the finder bodies' old rule (SHA256d of every body) MUTANT_M9
M10 a malformed "hash" is accepted MUTANT_M10
M11 an entry with an empty body is skipped MUTANT_M11
M12 a differing "txid" is not reported MUTANT_M12
M5 the memo without the byte compare (the previous memo) BodiesMemoNeverServesAResignedUnlockWithOldBytes
M6 the memo returns stale unlock bytes (unlock bodies not recognised by txid_may_omit_body_bytes) ResignedUnlockSharesTheTxidNotTheBytes, BodiesMemoNeverServesAResignedUnlockWithOldBytes, TemplateStoreReuseRejectsAResignedUnlock

Suites

Release, BLS on. 0 warnings in the touched files.

Suite Result
test_dash_share_hash_link (incl. the 12 new KATs) 112 passed
test_dash_stratum_work_source 172 passed
test_dash_work_source 98 passed
test_dash_get_work 4 passed
test_dash_embedded_gbt 375 passed
test_dash_mempool 236 passed
test_dash_node 197 passed
test_dash_node_coin_state 124 passed
test_dash_block_producer 9 passed
core_test 869 passed

The dashd-fallback template took each transaction's id from a "txid" key
that Dash getblocktemplate does not send, so it always fell back to the
SHA256d of the entry's bytes. From Dash Core v24 that is not the txid of a
version 2 asset unlock: GetHash zeroes the unlock's quorum signing info,
and the SHA256d of the whole bytes is its instance hash. m_tx_hashes feeds
the stratum merkle branches, so every job over a dashd template carrying a
v2 unlock had a header root dashd rejects (bad-txnmrklroot), and a block
found on it would be lost.

The per-entry rule now lives in coin/gbt_transactions.hpp:
- bytes that deserialize completely: dash_txid of the parsed tx, the same
  derivation the embedded build and every other hash site use; dashd's
  "hash" is cross-checked and a disagreement is logged once per txid;
- bytes that do not deserialize (or leave trailing bytes): dashd's "hash";
- neither: the SHA256d of the bytes, logged.

The embedded-vs-dashd tx-root cross-check now folds the same ids on both
sides.

Tests (test_dash_share_hash_link): two real testnet blocks with v2
unlocks, 1599900 and 1599924, fed through the reader as {data, hash}
entries: every id equals dashd's txid, the fold and the stratum branch
ascent equal the committed header root, and the old rule folds the
instance-hash root instead; plus the no-"hash", unparsable-bytes and
disagreeing-hash cases.
TemplateBodiesMemo (build_producer_job) handed out a stored body vector
whenever a template's tx-hash list and body count matched, on the
premise that a txid is the hash of its whole body. A v24 version 2 asset
unlock breaks that: every re-signed instance of one withdrawal shares one
txid with other bytes. Now that dashd templates are read with that txid
too, two templates over the same ids could differ in an unlock's bytes,
and the memo could serve the previous instance's bytes under the new
template (an expired unlock in the block body, and a CbTx
merkleRootAssetUnlocks over other bytes).

The memo now also requires byte equality of every body whose txid may
leave bytes out (txid_may_omit_body_bytes, every type-9 body), through
bodies_match_under_equal_txids in coin/rpc_data.hpp. The template
store's reuse rule (template_tx_hex_reusable) already applied that check
and now calls the same predicate; its behaviour is unchanged.

Tests (test_dash_share_hash_link): a re-signed instance of a real v2
unlock from testnet block 1599900 keeps its stable txid and changes its
instance hash; the memo returns each instance's own bytes while the
first instance's vector is still held, and the store's reuse rule
refuses the re-signed body.
…sh_txid

An entry's "hash" is Core's own GetHash of the very bytes the block body
carries, so it is the id the validator recomputes. When it is present it
is now the id served. dash_txid of the parsed bytes is still computed and
cross-checked: a disagreement can only mean a defect in dash_txid (which
the embedded build and every other hash site use), so it is logged as an
error once per txid with the running count of disagreeing entries read
since start. Without a "hash", dash_txid of the parsed bytes stays the
rule, and the SHA256d of the bytes the last resort.

Tests: DisagreeingHashIsReportedAndDashdHashDecides (renamed from
DisagreeingHashIsReportedAndTheBytesDecide) serves the disagreeing "hash",
reports the derived txid next to it, and keeps an agreeing entry
unreported; gbt_entry_txid without a "hash" still uses the bytes.
…der bodies

Producer job cache (mint_runloop.hpp ProducerJobCache::find): a hit now
also needs the template's CbTx payload to equal the cached job's. A v24
version 2 asset unlock that dashd re-signs at the same tip keeps its txid,
so the tip, height, payment amount and tx-hash list all stay equal, while
the cached gentx's merkleRootAssetUnlocks commits the previous instance.
Served with the current bodies, a found block would be rejected as
bad-cbtx-assetunlockmerkleroot. The payload commits the instances, so a
re-signed unlock is now a miss (a drift) and the job is rebuilt.

Finder bodies (reconstruct_won_block.hpp v36_finder_block_bodies): a body
whose txid may leave bytes out (every type-9 body) must parse completely
and its dash_txid must equal the listed txid; every other body still
hashes to it. With stable txids in the template, the re-broadcast arm
refused every block that carried a version 2 unlock.

getblocktemplate reader (gbt_transactions.hpp):
- an entry without a "data" string, or with an empty or non-hex body,
  fails the whole read and leaves the work data untouched instead of
  being skipped;
- a "hash" that is not a 64-char hex id is treated as absent and
  reported;
- a "txid" that differs from "hash" is reported ("hash" is served).
rpc.cpp logs one summary line per template read with disagreements and a
detail line per txid the first time; the remembered set stops growing at
its cap instead of being cleared.

Comments updated at main_dash.cpp (cache hit rule) and block_producer.hpp
(coinbase txid).

Tests: ProducerJobCacheMissesOnAResignedUnlock (the real CbTx v4 payload of
testnet block 1599900 against the same block with one unlock re-signed),
FinderBodiesAcceptRealV2Unlocks (block 1599900 through
v36_finder_block_bodies; a type-9 body with trailing bytes is refused),
and reader KATs for entries without bytes, malformed hashes and a
differing txid.
@frstrtr

frstrtr commented Nov 28, 2026

Copy link
Copy Markdown
Owner Author

Gate run on .198 for head b811235 (Release, BLS on, -j6), completed 08:31 UTC.

All suites rc=0, 0 warnings in touched files:

Suite Passed
DashGbtStableTxid 15/15
DashV36FinderBlock 6/6
test_dash_share_hash_link 115
test_dash_stratum_work_source 172
test_dash_work_source 98
test_dash_get_work 4
test_dash_embedded_gbt 375
test_dash_mempool 236
test_dash_node 198
test_dash_node_coin_state 124
test_dash_block_producer 9
core_test 869

Mutants. Each was applied alone and rebuilt, and each was expected to turn a test RED:

# Mutant Result Failing test
M1 txid = SHA256d(bytes), the old rule RED Block1599900EntriesReadWithDashdTxids, Block1599924EntriesReadWithDashdTxids, …
M2 GBT "hash" ignored when the bytes do not parse RED UnparsableBytesUseDashdHash
M3 trailing bytes accepted as a complete parse RED UnparsableBytesUseDashdHash
M4 disagreement not reported RED DisagreeingHashIsReportedAndDashdHashDecides
M5 bodies memo without the byte compare RED BodiesMemoNeverServesAResignedUnlockWithOldBytes
M6 memo returns stale unlock bytes RED ResignedUnlockSharesTheTxidNotTheBytes, BodiesMemoNeverServesAResignedUnlockWithOldBytes, …
M7 bytes decide on disagreement RED DisagreeingHashIsReportedAndDashdHashDecides
M8 producer-job cache hit rule without the CbTx payload check RED ProducerJobCacheMissesOnAResignedUnlock
M9 finder bodies checked with plain SHA256d RED DashV36FinderBlock.FinderBodiesAcceptRealV2Unlocks
M10 malformed "hash" accepted RED MalformedHashIsTreatedAsAbsentAndReported
M11 entry without bytes skipped RED BareStringEntriesReadAndEntriesWithoutBytesFailTheRead
M12 "txid" that differs from "hash" not reported RED TxidThatDiffersFromHashIsReportedHashServed

12/12 RED, no survivors. A clean rebuild afterwards passes again: DashGbtStableTxid 15/15, DashV36FinderBlock 6/6.

@frstrtr
frstrtr merged commit 9b239a3 into master Nov 28, 2026
39 checks passed
frstrtr added a commit that referenced this pull request Nov 28, 2026
master brings #2353 (one scrypt per DGB submit: the vardiff gate's digest
reused by mining_submit for byte-equal headers), #2361, #2357, #2352 and
#2363. work_source.cpp keeps both sides; its include block takes both
<atomic> and <cctype>.
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.

1 participant