Skip to content

Commit

Permalink
Add chain_state cumulative_work.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Feb 26, 2024
1 parent e76ce71 commit c7fb36a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
2 changes: 2 additions & 0 deletions include/bitcoin/system/chain/chain_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class BC_API chain_state
/// Properties.
chain::context context() const NOEXCEPT;
const hash_digest& hash() const NOEXCEPT;
const uint256_t& cumulative_work() const NOEXCEPT;
uint32_t minimum_block_version() const NOEXCEPT;
uint32_t work_required() const NOEXCEPT;
uint32_t timestamp() const NOEXCEPT;
Expand Down Expand Up @@ -237,6 +238,7 @@ class BC_API chain_state
const activations active_;
const uint32_t work_required_;
const uint32_t median_time_past_;
const uint256_t cumulative_work_;
};

} // namespace chain
Expand Down
41 changes: 25 additions & 16 deletions src/chain/chain_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,13 +584,15 @@ 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_))
median_time_past_(median_time_past(data_, forks_)),
cumulative_work_(top.cumulative_work())
{
}

Expand Down Expand Up @@ -627,7 +629,8 @@ 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_))
median_time_past_(median_time_past(data_, forks_)),
cumulative_work_(pool.cumulative_work() + block.header().proof())
{
}

Expand Down Expand Up @@ -665,7 +668,8 @@ 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_))
median_time_past_(median_time_past(data_, forks_)),
cumulative_work_(parent.cumulative_work() + header.proof())
{
}

Expand All @@ -683,11 +687,29 @@ chain_state::chain_state(data&& values,
// Properties.
// ----------------------------------------------------------------------------

chain::context chain_state::context() const NOEXCEPT
{
return
{
forks(),
timestamp(),
median_time_past(),
possible_narrow_cast<uint32_t>(height()),
minimum_block_version(),
work_required()
};
}

const hash_digest& chain_state::hash() const NOEXCEPT
{
return data_.hash;
}

const uint256_t& chain_state::cumulative_work() const NOEXCEPT
{
return cumulative_work_;
}

uint32_t chain_state::minimum_block_version() const NOEXCEPT
{
return active_.minimum_block_version;
Expand Down Expand Up @@ -720,19 +742,6 @@ size_t chain_state::height() const NOEXCEPT
return data_.height;
}

chain::context chain_state::context() const NOEXCEPT
{
return
{
forks(),
timestamp(),
median_time_past(),
possible_narrow_cast<uint32_t>(height()),
minimum_block_version(),
work_required()
};
}

} // namespace chain
} // namespace system
} // namespace libbitcoin

0 comments on commit c7fb36a

Please sign in to comment.