Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move chain_state.cumulative_work to chain_state.data.cumulative_work. #1412

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/bitcoin/system/chain/chain_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
14 changes: 6 additions & 8 deletions src/chain/chain_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_))
{
}

Expand All @@ -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())
Expand All @@ -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_))
{
}

Expand All @@ -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())
Expand All @@ -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_))
{
}

Expand Down Expand Up @@ -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
Expand Down