@@ -218,9 +218,35 @@ uint32_t header::nonce() const NOEXCEPT
218218 return nonce_;
219219}
220220
221- void header::set_hash (const hash_digest& hash) const NOEXCEPT
221+ // static/computed
222+ uint256_t header::proof (uint32_t bits) NOEXCEPT
222223{
223- hash_ = hash;
224+ auto target = compact::expand (bits);
225+
226+ // *************************************************************************
227+ // CONSENSUS: bits may be overflowed, which is guarded here.
228+ // A target of zero is disallowed so is useful as a sentinel value.
229+ // *************************************************************************
230+ if (is_zero (target))
231+ return target;
232+
233+ // *************************************************************************
234+ // CONSENSUS: If target is (2^256)-1, division would fail, however compact
235+ // compression is lossy, and therefore unable to produce negative one.
236+ // *************************************************************************
237+
238+ // We need to compute 2**256 / (target + 1), but we can't represent 2**256
239+ // as it's too large for uint256. However as 2**256 is at least as large as
240+ // target + 1, it is equal to ((2**256 - target - 1) / (target + 1)) + 1, or
241+ // (~target / (target + 1)) + 1.
242+ return ++(~target / (target + one));
243+ }
244+
245+ // computed
246+ uint256_t header::proof () const NOEXCEPT
247+ {
248+ // Returns zero if bits_ mantissa is less than one or bits_ is overflowed.
249+ return proof (bits_);
224250}
225251
226252// computed
@@ -240,6 +266,14 @@ hash_digest header::hash() const NOEXCEPT
240266 return digest;
241267}
242268
269+ // Cache and metadata.
270+ // ----------------------------------------------------------------------------
271+
272+ void header::set_hash (const hash_digest& hash) const NOEXCEPT
273+ {
274+ hash_ = hash;
275+ }
276+
243277const hash_digest& header::get_hash () const NOEXCEPT
244278{
245279 if (!hash_)
@@ -248,35 +282,14 @@ const hash_digest& header::get_hash() const NOEXCEPT
248282 return *hash_;
249283}
250284
251- // static
252- uint256_t header::proof (uint32_t bits) NOEXCEPT
285+ const chain_state::cptr& header::get_state () const NOEXCEPT
253286{
254- auto target = compact::expand (bits);
255-
256- // *************************************************************************
257- // CONSENSUS: bits may be overflowed, which is guarded here.
258- // A target of zero is disallowed so is useful as a sentinel value.
259- // *************************************************************************
260- if (is_zero (target))
261- return target;
262-
263- // *************************************************************************
264- // CONSENSUS: If target is (2^256)-1, division would fail, however compact
265- // compression is lossy, and therefore unable to produce negative one.
266- // *************************************************************************
267-
268- // We need to compute 2**256 / (target + 1), but we can't represent 2**256
269- // as it's too large for uint256. However as 2**256 is at least as large as
270- // target + 1, it is equal to ((2**256 - target - 1) / (target + 1)) + 1, or
271- // (~target / (target + 1)) + 1.
272- return ++(~target / (target + one));
287+ return state_;
273288}
274289
275- // computed
276- uint256_t header::proof () const NOEXCEPT
290+ void header::set_state (const chain_state::cptr& state) const NOEXCEPT
277291{
278- // Returns zero if bits_ mantissa is less than one or bits_ is overflowed.
279- return proof (bits_);
292+ state_ = state;
280293}
281294
282295// Check.
0 commit comments