From aa78fc08784eacf75ae033b7bf7cc3148056bec9 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Tue, 17 Jan 2023 16:25:27 -0700 Subject: [PATCH] Verify sum of pool balances against chain total supply. Co-authored-by: Jack Grigg --- src/main.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index dd1fe1aba..96d0da200 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3597,6 +3597,26 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin block.vtx[0].GetValueOut(), blockReward), REJECT_INVALID, "bad-cb-amount"); + // Ensure that the total chain supply is consistent with the value in each pool. + if (!fJustCheck && + pindex->nChainTotalSupply && + pindex->nChainTransparentValue && + pindex->nChainSproutValue && + pindex->nChainSaplingValue && + pindex->nChainOrchardValue) + { + auto expectedChainSupply = + pindex->nChainTransparentValue.value() + + pindex->nChainSproutValue.value() + + pindex->nChainSaplingValue.value() + + pindex->nChainOrchardValue.value(); + if (expectedChainSupply != pindex->nChainTotalSupply.value()) { + // This may be added as a rule to ZIP 209 and return a failure in a future soft-fork. + error("ConnectBlock(): chain total supply (%d) does not match sum of pool balances (%d) at height %d", + pindex->nChainTotalSupply.value(), expectedChainSupply, pindex->nHeight); + } + } + // Ensure Sapling authorizations are valid (if we are checking them) if (saplingAuth.has_value() && !saplingAuth.value()->validate()) { return state.DoS(100,