index: Fix coinstatsindex overflow issue

The index originally stored cumulative values in a CAmount type but this allowed for
potential overflow issues which were observed on Signet. Fix this by
storing the values that are in danger of overflowing in a arith_uint256.

Also turns an unnecessary copy into a reference in RevertBlock and
CustomAppend and gets
rid of the explicit total unspendable tracking which can be calculated
by adding the four categories of unspendables together.
This commit is contained in:
Fabian Jahr
2025-08-04 15:25:15 +02:00
parent 84e813a02b
commit 431a076ae6
6 changed files with 98 additions and 94 deletions

View File

@@ -5,6 +5,7 @@
#ifndef BITCOIN_KERNEL_COINSTATS_H
#define BITCOIN_KERNEL_COINSTATS_H
#include <arith_uint256.h>
#include <consensus/amount.h>
#include <crypto/muhash.h>
#include <streams.h>
@@ -50,14 +51,6 @@ struct CCoinsStats {
//! Total cumulative amount of block subsidies up to and including this block
CAmount total_subsidy{0};
//! Total cumulative amount of unspendable coins up to and including this block
CAmount total_unspendable_amount{0};
//! Total cumulative amount of prevouts spent up to and including this block
CAmount total_prevout_spent_amount{0};
//! Total cumulative amount of outputs created up to and including this block
CAmount total_new_outputs_ex_coinbase_amount{0};
//! Total cumulative amount of coinbase outputs up to and including this block
CAmount total_coinbase_amount{0};
//! The unspendable coinbase amount from the genesis block
CAmount total_unspendables_genesis_block{0};
//! The two unspendable coinbase outputs total amount caused by BIP30
@@ -67,6 +60,15 @@ struct CCoinsStats {
//! Total cumulative amount of coins lost due to unclaimed miner rewards up to and including this block
CAmount total_unspendables_unclaimed_rewards{0};
// Despite containing amounts the following values use a uint256 type to prevent overflowing
//! Total cumulative amount of prevouts spent up to and including this block
arith_uint256 total_prevout_spent_amount{0};
//! Total cumulative amount of outputs created up to and including this block
arith_uint256 total_new_outputs_ex_coinbase_amount{0};
//! Total cumulative amount of coinbase outputs up to and including this block
arith_uint256 total_coinbase_amount{0};
CCoinsStats() = default;
CCoinsStats(int block_height, const uint256& block_hash);
};