Repository navigation
Conversation
AddBalances() reports Err("overflow") on overflow, but the return value was
ignored in SumAllTransfers(). The sum therefore silently stayed at the first
amount while the destination accounts were credited with the full amounts -
up to INT64_MAX (~92.2 billion DFI) of DFI created from nothing per
transaction.
SumAllTransfers() now returns ResVal<CBalances> and propagates the error.
The 12 call sites are adapted in the following commits.
Adapt the three consensus call sites of SumAllTransfers() to its new ResVal<CBalances> return type and propagate the error explicitly. The error has to be checked explicitly: ResVal has an implicit conversion operator to T, so the previous one-liners would still compile and would dereference an empty optional in release builds (see Issue 0006). - AccountToAccount: the source account is now debited with the real sum instead of a truncated one. On overflow the transaction is rejected. - AnyAccountsToAccounts: both sums are checked individually. The attacker controls both sides, so a single check would not be enough. - UtxosToAccount: unchanged behaviour, more precise error message.
Finish the adaptation of SumAllTransfers() to its ResVal<CBalances> return type. All call sites now handle the error explicitly - the error must not be ignored, otherwise the sum would silently be truncated again. Error handling follows the role of each call site: - consensus (AddPoolLiquidity): reject the transaction - account selection for masternode transfers: no selection - RPC endpoints: reject the request with the error message - decodecustomtx output: report the error instead of failing silently
SumAllTransfers() ignored the return value of AddBalances(): when the sum exceeded INT64_MAX, SafeAdd() reported "overflow" and Add() aborted before the balances were mutated - the sum silently stayed at the first amount. The source account was debited with the truncated sum while every destination was credited with the full amount. The difference was newly created DFI. The test builds the transaction raw because ParseFixedPoint() rejects amounts with more than 18 significant digits, and the destinations have to be ordered by their script bytes because CAccounts is a std::map<CScript, CBalances>. The test asserts that - the transaction is rejected with "overflow" - no DFI is created on either destination account - the control case (sum exactly INT64_MAX, no overflow) is still rejected because the source account cannot cover it Verified in both directions: fails with "No exception raised" on the unpatched code (the transaction is accepted there) and passes with the fix.
The fix from 0001 (overflow in the transfer sum) changes the consensus rule globally: the strict SumAllTransfers() rule applies implicitly from block 1. Every history in which a transfer sum overflows would therefore be invalidated - the extent of the reset would be undetermined. With an activation height: - below the height the historical behaviour (bit-exact like v4.2.5), - from that height on the strict rule. Main change: - new consensus parameter nBalanceOverflowHeight (mainnet 6564035) - SumAllTransfersLegacy() reproduces the old behaviour bit-exactly. Decisive: the loop continues after an overflow, because CBalances::Add aborts before the mutation. A break would change the historical sum and invalidate already mined blocks. - CCustomTxVisitor::SumTransfers() selects between both behaviours based on txCtx.GetHeight()/GetConsensus(). The mainnet value is measured: a scan of the range up to block 6582242 found exactly three overflowing transactions (6564035, 6565374, 6566081). Test: feature_balance_overflow_height.py covers both sides of the activation height (strict rejection / legacy acceptance with the historical balances).
From consensus.nMNVersionGateHeight on (mainnet 6564035), all mined blocks must signal the overflow-fix capability in block.nVersion (VERSION_BIT_BALANCE_OVERFLOW, bit 24). Blocks minted by outdated nodes (e.g. v4.2.5 without this bit) are rejected with 'bad-version' in ContextualCheckBlockHeader. This ensures outdated masternodes cannot mint invalid blocks or slow down block production on the updated network. Test: feature_mn_version_gate.py verifies legacy acceptance below the gate height and rejection of outdated blocks at/above the gate height.
Author
🔬 20-Node Heterogeneous Cluster Test Results: Masternode Version Gate VerificationTo rigorously verify the consensus safety of the Masternode Version Gate under real P2P network conditions, we conducted an automated end-to-end test on a 20-node cluster in full mesh topology. Test Configuration:
Test Phases & Observed Behavior:
Cluster Monitoring Snapshot at Block 201:Conclusion:The Masternode Version Gate functions reliably under cluster conditions:
|
Merged
3 of 5 tasks
Author
|
Due to the upstream repository appearing inactive, this consensus fix (including activation height & Masternode Version Gate) has also been submitted for active review and integration on the community fork: ms-01#1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SumAllTransfers()sums the amounts of all transfer destinations but ignoresthe return value of
AddBalances(). When the sum of the transfer amountsexceeds
INT64_MAX,SafeAdd()reportsErr("overflow")andAdd()abortsbefore mutating the balances — the sum silently stays at the first amount.
The source account is then debited with the truncated sum while every
destination is credited with its full amount. The difference is newly
created DFI.
This is a consensus-level bug. It affects the account transfer types
AccountToAccount('B') andAnyAccountsToAccounts('a').To prevent an unannounced hardfork, keep historical blocks valid, and ensure a
clean network transition, this PR introduces:
nBalanceOverflowHeight):SumAllTransfersLegacy()retains the historicalbit-exact behavior (swallowing the overflow, continuing accumulator additions).
transactions with overflowing sums are rejected.
nMNVersionGateHeight):VERSION_BIT_BALANCE_OVERFLOW(bit 24 inblock.nVersion).strictly rejected with
bad-versionat the header level.delayed block times on the updated network.
Root cause
src/dfi/balances.h:265SafeAdd()(src/amount.h:96) detects the overflow correctly:CBalances::Add()aborts before mutating the balances, so the sum stays at thefirst amount — but nobody looks at the returned error.
The debit path uses that truncated sum, the crediting path uses the full
amounts:
src/dfi/consensus/accounts.cpp:80(AccountToAccount)src/dfi/consensus/accounts.cpp:96(AnyAccountsToAccounts) has the samedefect:
sumFromandsumToare both truncated, so thesumFrom != sumTocheck passes.The balance check itself (
CTokenAmount::Sub,src/amount.h:163) is correct —it is simply never presented with the real amount.
Impact
INT64_MAX≈ 92.2 billion DFIAccountToAccount('B'),AnyAccountsToAccounts('a')Two conditions have to be met by the attacker:
CAccountsis astd::map<CScript, CBalances>, so theiteration follows the script bytes. The destination with the smaller script
has to carry the smaller amount, otherwise the truncated sum is too large to
pass the balance check.
ParseFixedPoint(src/util/strencodings.cpp:457)rejects amounts with more than 18 significant digits, so the value cannot be
expressed through the RPC interface.
The fix, Activation Height & Version Gate
1. Error Propagation
Propagate the error in
SumAllTransfers:2. Historical Compatibility & Consensus Gate
To keep already-mined blocks valid,
SumAllTransfersLegacy()retains the historicalbehavior (loop continues without breaking even if overflow occurs), and
CCustomTxVisitor::SumTransfers()gates between the legacy and strict paths:3. Masternode Version Gate
In
ContextualCheckBlockHeader, blocks at or abovenMNVersionGateHeightmust carrythe overflow-fix signal bit (
VERSION_BIT_BALANCE_OVERFLOW = 1 << 24innVersion):4. Activation Heights per Network
nBalanceOverflowHeightnMNVersionGateHeight65640356564035110000000-mnversiongateheight.INT_MAXINT_MAXINT_MAXINT_MAXINT_MAXINT_MAXNote for reviewers
ResVal<T>has an implicit conversion operator toT(
src/dfi/res.h:72), so the call sites are not enforced by the compiler —the old one-liners still compile. The adaptation has to be done explicitly;
otherwise the error would be ignored again, and in release builds
(
NDEBUG) dereferencing the emptystd::optionalis undefined behaviour.This is worth addressing separately, but it is not required for this fix.
Verification
Three functional tests are included and registered in
test_runner.py:test/functional/feature_account_overflow.py:overflow.test/functional/feature_balance_overflow_height.py:overflow.test/functional/feature_mn_version_gate.py:nVersion=0x20000000) are accepted.bad-version, while updated nodes mint successfully.Additionally, existing functional test suites pass and
defidbuilds cleanly.Consensus & Coordination
6564035continue to validate cleanly under the legacy path.security@defichain.com, perSECURITY.md) before this pull request was opened.