From a2e1f27b32442bcea92eeda3ac09432f6c5ec2c5 Mon Sep 17 00:00:00 2001 From: bitflash-maintainer Date: Wed, 2 Sep 2026 08:56:46 +0000 Subject: payout: record the full funding block hash and require it to agree pool_payout_id keeps only the first sixteen hex digits of the block hash, which is the end proof-of-work fills with zeros -- so twin rounds at one height can carry the SAME id, and one twin's payment then settles the other's debt through the structured probe: the same-height collision the comment match had, one layer down. New sends record the full hash as pool_payout_block and a structured match must agree with it. A transaction without the key predates it and is matched by id as before; anything stricter would make every payment already sent look unpaid and pay it again. --- src/rpc.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 76 insertions(+), 20 deletions(-) (limited to 'src/rpc.cpp') diff --git a/src/rpc.cpp b/src/rpc.cpp index f9471c2..dbbf06d 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -1373,10 +1373,15 @@ static void MarkPoolRoundForfeited(const std::string& id, // // Two kinds of evidence, in order of quality. // -// Structured (pool_payout_id / pool_payout_address / pool_payout_amount) is -// upstream's, and it is exact: the id is derived from the block hash, so it -// names one round and one round only. Every transaction this build sends -// carries it. +// Structured (pool_payout_id / pool_payout_address / pool_payout_amount, +// plus pool_payout_block since this series) is upstream's, extended. The id +// is derived from the block hash, but only from the FIRST sixteen hex digits +// of it -- and proof-of-work concentrates zeros exactly there, so the id can +// name two same-height rounds identically. New sends therefore record the +// full hash as pool_payout_block, and a structured match must agree with it +// wherever both sides have one; a transaction without the key was sent +// before it existed, and for those the id is the best evidence there is. +// Every transaction this build sends carries all four keys. // // But NO transaction this pool has ever sent carries it. Every payout in the // existing wallet has only a "comment". So on the first boot after this @@ -1437,6 +1442,7 @@ enum PayoutProbeResult { static PayoutProbeResult FindRecordedPoolPayout(const std::string& id, int blockHeight, + const uint256& blockHash, const std::string& address, int64 amount, const std::set& excluded, @@ -1482,6 +1488,19 @@ static PayoutProbeResult FindRecordedPoolPayout(const std::string& id, map::const_iterator amtIt = wtx.mapValue.find("pool_payout_amount"); if (amtIt == wtx.mapValue.end() || amtIt->second != strprintf("%lld", amount)) continue; + // The id alone cannot tell twin rounds at one height apart + // (see above); the full hash can, where the transaction + // recorded one. Where it did not, the id decides, as it + // always has -- refusing those transactions would make every + // payment sent before the key existed look unpaid and pay it + // again. + if (blockHash != 0) { + map::const_iterator blkIt = + wtx.mapValue.find("pool_payout_block"); + if (blkIt != wtx.mapValue.end() + && blkIt->second != blockHash.ToString()) + continue; // a twin round's payment, not this round's + } txidRet = wtx.GetHash().GetHex(); return PAYOUT_PROBE_STRUCTURED; } @@ -2125,7 +2144,8 @@ static void PayoutThreadFn(void*) claimKey, excluded); r.fProbeWallet = false; PayoutProbeResult probe = FindRecordedPoolPayout( - id, r.blockHeight, r.address, r.amount, excluded, recovered); + id, r.blockHeight, r.blockHash, r.address, r.amount, + excluded, recovered); if (probe != PAYOUT_PROBE_NONE) { if (probe == PAYOUT_PROBE_LEGACY) { // Loud on purpose: this is the upgrade path, where the @@ -2219,6 +2239,11 @@ static void PayoutThreadFn(void*) wtx.mapValue["pool_payout_id"] = id; wtx.mapValue["pool_payout_address"] = r.address; wtx.mapValue["pool_payout_amount"] = strprintf("%lld", r.amount); + // The full hash, not only the id's sixteen-digit prefix: the + // prefix of a proof-of-work hash is where the zeros live, and + // the probe needs to tell twin rounds at one height apart. + if (r.blockHash != 0) + wtx.mapValue["pool_payout_block"] = r.blockHash.ToString(); SendMoneyOutcome outcome; if (SendMoney(sc, r.amount, wtx, outcome)) { @@ -3285,7 +3310,8 @@ static std::string PayoutTestAddWalletTx(const std::string& comment, int64 amount, int nonce, const std::string& structuredId, - bool fConfirmed = true) + bool fConfirmed = true, + const std::string& structuredBlock = "") { CWalletTx wtx; wtx.vin.resize(1); @@ -3300,6 +3326,8 @@ static std::string PayoutTestAddWalletTx(const std::string& comment, wtx.mapValue["pool_payout_id"] = structuredId; wtx.mapValue["pool_payout_address"] = address; wtx.mapValue["pool_payout_amount"] = strprintf("%lld", amount); + if (!structuredBlock.empty()) + wtx.mapValue["pool_payout_block"] = structuredBlock; } uint256 hash = wtx.GetHash(); if (fConfirmed) { @@ -3853,30 +3881,30 @@ int RunPayoutLedgerSelfTest() std::set none; std::string found; - check(FindRecordedPoolPayout(id, height, addrA, amount, none, found) + check(FindRecordedPoolPayout(id, height, 0, addrA, amount, none, found) == PAYOUT_PROBE_NONE, "an empty wallet is not evidence that anything was paid"); const std::string legacyTxid = PayoutTestAddWalletTx(comment, addrA, amount, 1, ""); - check(FindRecordedPoolPayout(id, height, addrA, amount, none, found) + check(FindRecordedPoolPayout(id, height, 0, addrA, amount, none, found) == PAYOUT_PROBE_LEGACY && found == legacyTxid, "a payment carrying only a pre-upgrade comment is recognised as" " already paid"); - check(FindRecordedPoolPayout(id, height, addrB, amount, none, found) + check(FindRecordedPoolPayout(id, height, 0, addrB, amount, none, found) == PAYOUT_PROBE_NONE, "the legacy match requires the output to pay THIS recipient"); - check(FindRecordedPoolPayout(id, height, addrA, amount + COIN, none, found) + check(FindRecordedPoolPayout(id, height, 0, addrA, amount + COIN, none, found) == PAYOUT_PROBE_NONE, "the legacy match requires the output to pay exactly this amount"); - check(FindRecordedPoolPayout(id, height + 1, addrA, amount, none, found) + check(FindRecordedPoolPayout(id, height + 1, 0, addrA, amount, none, found) == PAYOUT_PROBE_NONE, "the legacy match requires the comment for this block height"); std::set spent; spent.insert(legacyTxid); - check(FindRecordedPoolPayout(id, height, addrA, amount, spent, found) + check(FindRecordedPoolPayout(id, height, 0, addrA, amount, spent, found) == PAYOUT_PROBE_NONE, "one legacy transaction cannot settle a second debt"); @@ -3885,7 +3913,7 @@ int RunPayoutLedgerSelfTest() // exactly what the structured metadata exists to prevent. const std::string otherId = PayoutIdForBlock(height, 0) + "-other"; PayoutTestAddWalletTx(comment, addrB, amount, 2, otherId); - check(FindRecordedPoolPayout(id, height, addrB, amount, none, found) + check(FindRecordedPoolPayout(id, height, 0, addrB, amount, none, found) == PAYOUT_PROBE_NONE, "another round's structured payment is not legacy evidence for" " this one"); @@ -3893,7 +3921,7 @@ int RunPayoutLedgerSelfTest() // The structured path still wins where it applies. const std::string structuredTxid = PayoutTestAddWalletTx(comment, addrB, amount, 3, id); - check(FindRecordedPoolPayout(id, height, addrB, amount, none, found) + check(FindRecordedPoolPayout(id, height, 0, addrB, amount, none, found) == PAYOUT_PROBE_STRUCTURED && found == structuredTxid, "structured payout metadata is matched in preference to a comment"); @@ -3907,7 +3935,7 @@ int RunPayoutLedgerSelfTest() PayoutTestClearBlockIndex(); const std::string addrC = PayoutTestAddress(0x33); PayoutTestAddWalletTx(comment, addrC, amount, 4, "", false); - check(FindRecordedPoolPayout(id, height, addrC, amount, none, found) + check(FindRecordedPoolPayout(id, height, 0, addrC, amount, none, found) == PAYOUT_PROBE_NONE, "a pre-upgrade payout that never confirmed is not read as" " payment"); @@ -3917,7 +3945,7 @@ int RunPayoutLedgerSelfTest() mapWallet.clear(); const std::string confirmedTxid = PayoutTestAddWalletTx(comment, addrC, amount, 4, "", true); - check(FindRecordedPoolPayout(id, height, addrC, amount, none, found) + check(FindRecordedPoolPayout(id, height, 0, addrC, amount, none, found) == PAYOUT_PROBE_LEGACY && found == confirmedTxid, "the same payout, confirmed, is"); @@ -3928,7 +3956,7 @@ int RunPayoutLedgerSelfTest() PayoutTestAddWalletTx(comment, addrC, amount, 5, "", false); std::set spentFirst; spentFirst.insert(confirmedTxid); - check(FindRecordedPoolPayout(id, height, addrC, amount, spentFirst, found) + check(FindRecordedPoolPayout(id, height, 0, addrC, amount, spentFirst, found) == PAYOUT_PROBE_LEGACY && found == alsoConfirmed, "an unconfirmed transaction does not hide a confirmed one behind" " it"); @@ -4484,10 +4512,10 @@ int RunPayoutLedgerSelfTest() std::set none; std::string found; PayoutTestAddWalletTx(PayoutComment(height, hashA), addrA, amount, 1, ""); - check(FindRecordedPoolPayout(idB, height, addrA, amount, none, found) + check(FindRecordedPoolPayout(idB, height, hashB, addrA, amount, none, found) == PAYOUT_PROBE_NONE, "a new-format comment is never legacy evidence for a twin round"); - check(FindRecordedPoolPayout(idA, height, addrA, amount, none, found) + check(FindRecordedPoolPayout(idA, height, hashA, addrA, amount, none, found) == PAYOUT_PROBE_NONE, "nor a match channel for its own round -- structured metadata is"); @@ -4499,10 +4527,38 @@ int RunPayoutLedgerSelfTest() const std::string paidA = PayoutTestAddWalletTx(PayoutComment(height, hashA), addrA, amount, 2, idA); - check(FindRecordedPoolPayout(idA, height, addrA, amount, none, found) + check(FindRecordedPoolPayout(idA, height, hashA, addrA, amount, none, found) == PAYOUT_PROBE_STRUCTURED && found == paidA, "the winning twin's own payment is found by its structured id"); + // The id is derived from the FIRST sixteen hex digits of the hash -- + // the end proof-of-work fills with zeros -- so twin hashes that agree + // there collide in id, and a payment carrying no pool_payout_block + // answers for either twin. That is the accepted trade for payments + // sent before the key existed, pinned here so it stays a decision. + check(idA == idB, + "twin hashes differing only past the sixteenth hex digit collide" + " in id -- the reason the full hash must decide"); + check(FindRecordedPoolPayout(idB, height, hashB, addrA, amount, none, found) + == PAYOUT_PROBE_STRUCTURED, + "a payment without the block key answers for a colliding twin --" + " the accepted residual for pre-key transactions"); + + // With the full hash recorded -- as every send now records it -- the + // twins are told apart and the re-won height is paid. + CRITICAL_BLOCK(cs_mapWallet) + mapWallet.clear(); + PayoutTestClearBlockIndex(); + const std::string keyedA = + PayoutTestAddWalletTx(PayoutComment(height, hashA), addrA, amount, + 3, idA, true, hashA.ToString()); + check(FindRecordedPoolPayout(idA, height, hashA, addrA, amount, none, found) + == PAYOUT_PROBE_STRUCTURED && found == keyedA, + "a payment carrying the full block hash settles its own round"); + check(FindRecordedPoolPayout(idB, height, hashB, addrA, amount, none, found) + == PAYOUT_PROBE_NONE, + "and is no evidence for the twin, so the re-won height is paid"); + CRITICAL_BLOCK(cs_mapWallet) mapWallet.clear(); PayoutTestClearBlockIndex(); -- cgit v1.2.3