From 51c2f47281d8a123a493ca737b024d0903a0e8a9 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Tue, 27 Feb 2024 01:11:02 -0500 Subject: [PATCH] Move chain_state.cumulative_work to chain_state.data.cumulative_work. --- include/bitcoin/system/chain/chain_state.hpp | 3 +++ src/chain/chain_state.cpp | 14 ++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/bitcoin/system/chain/chain_state.hpp b/include/bitcoin/system/chain/chain_state.hpp index 64948a218f..d12f486de4 100644 --- a/include/bitcoin/system/chain/chain_state.hpp +++ b/include/bitcoin/system/chain/chain_state.hpp @@ -103,6 +103,9 @@ class BC_API chain_state /// Hash of the bip9_bit1 block or null_hash if unrequested. hash_digest bip9_bit1_hash; + /// Sum of all work from genesis to block height. + uint256_t cumulative_work; + /// Values must be ordered by height with high (block - 1) last. struct { diff --git a/src/chain/chain_state.cpp b/src/chain/chain_state.cpp index 0fd0cc13df..f271538f96 100644 --- a/src/chain/chain_state.cpp +++ b/src/chain/chain_state.cpp @@ -584,15 +584,13 @@ chain_state::data chain_state::to_pool(const chain_state& top, // Top to pool. // This generates a state for the pool above the presumed top block state. -// Work is not acculuated for a pool state. chain_state::chain_state(const chain_state& top, const system::settings& settings) NOEXCEPT : data_(to_pool(top, settings)), forks_(top.forks_), active_(activation(data_, forks_, settings)), work_required_(work_required(data_, forks_, settings)), - median_time_past_(median_time_past(data_, forks_)), - cumulative_work_(top.cumulative_work()) + median_time_past_(median_time_past(data_, forks_)) { } @@ -609,6 +607,7 @@ chain_state::data chain_state::to_block(const chain_state& pool, data.bits.self = header.bits(); data.version.self = header.version(); data.timestamp.self = header.timestamp(); + data.cumulative_work += header.proof(); // Cache hash of bip9 bit0 height block, otherwise use preceding state. if (data.height == settings.bip9_bit0_active_checkpoint.height()) @@ -629,8 +628,7 @@ chain_state::chain_state(const chain_state& pool, const block& block, forks_(pool.forks_), active_(activation(data_, forks_, settings)), work_required_(work_required(data_, forks_, settings)), - median_time_past_(median_time_past(data_, forks_)), - cumulative_work_(pool.cumulative_work() + block.header().proof()) + median_time_past_(median_time_past(data_, forks_)) { } @@ -648,6 +646,7 @@ chain_state::data chain_state::to_header(const chain_state& parent, data.bits.self = header.bits(); data.version.self = header.version(); data.timestamp.self = header.timestamp(); + data.cumulative_work += header.proof(); // Cache hash of bip9 bit0 height block, otherwise use preceding state. if (data.height == settings.bip9_bit0_active_checkpoint.height()) @@ -668,8 +667,7 @@ chain_state::chain_state(const chain_state& parent, const header& header, forks_(parent.forks_), active_(activation(data_, forks_, settings)), work_required_(work_required(data_, forks_, settings)), - median_time_past_(median_time_past(data_, forks_)), - cumulative_work_(parent.cumulative_work() + header.proof()) + median_time_past_(median_time_past(data_, forks_)) { } @@ -707,7 +705,7 @@ const hash_digest& chain_state::hash() const NOEXCEPT const uint256_t& chain_state::cumulative_work() const NOEXCEPT { - return cumulative_work_; + return data_.cumulative_work; } uint32_t chain_state::minimum_block_version() const NOEXCEPT