Skip to content

Commit

Permalink
Merge pull request #1412 from evoskuil/master
Browse files Browse the repository at this point in the history
Move chain_state.cumulative_work to chain_state.data.cumulative_work.
  • Loading branch information
evoskuil authored Feb 27, 2024
2 parents 1c2b65e + 51c2f47 commit 93af70d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
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

0 comments on commit 93af70d

Please sign in to comment.