Skip to content

Commit 1aff1f7

Browse files
authored
Merge pull request #1699 from evoskuil/master
Update header.version processing, style, refactor.
2 parents 14d1e65 + d89fe93 commit 1aff1f7

File tree

9 files changed

+51
-37
lines changed

9 files changed

+51
-37
lines changed

include/bitcoin/system/chain/context.hpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,30 @@ class BC_API context final
3131
{
3232
public:
3333
/// Determine if the flag is active for this block.
34-
bool is_enabled(chain::flags flag) const NOEXCEPT;
34+
inline bool is_enabled(chain::flags flag) const NOEXCEPT
35+
{
36+
return to_bool(flag & flags);
37+
}
38+
39+
// ************************************************************************
40+
// CONSENSUS: Soft forks imposed minimum block versioning using a signed
41+
// interpretation of header.version, which would otherwise be unsigned.
42+
// ************************************************************************
43+
inline bool is_insufficient_version(uint32_t version) const NOEXCEPT
44+
{
45+
return is_nonzero(minimum_block_version) &&
46+
to_signed(version) < to_signed(minimum_block_version);
47+
}
48+
49+
inline bool is_anachronistic_timestamp(uint32_t time_stamp) const NOEXCEPT
50+
{
51+
return time_stamp <= median_time_past;
52+
}
53+
54+
inline bool is_invalid_work(uint32_t bits) const NOEXCEPT
55+
{
56+
return bits != work_required;
57+
}
3558

3659
/// Header context within chain.
3760
uint32_t flags;

include/bitcoin/system/chain/header.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ class BC_API header
129129

130130
bool is_invalid_proof_of_work(uint32_t proof_of_work_limit,
131131
bool scrypt=false) const NOEXCEPT;
132-
bool is_invalid_timestamp(uint32_t timestamp_limit_seconds) const NOEXCEPT;
132+
bool is_futuristic_timestamp(uint32_t timestamp_limit_seconds) const NOEXCEPT;
133133

134134
/// Accept (relative to chain_state).
135135
/// -----------------------------------------------------------------------
136136

137-
// error::invalid_block_version
138-
// error::timestamp_too_early
137+
// error::insufficient_block_version
138+
// error::anachronistic_timestamp
139139
// error::incorrect_proof_of_work
140140

141141
private:

include/bitcoin/system/error/block_error_t.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ enum block_error_t : uint8_t
3939

4040
// accept header
4141
checkpoint_conflict,
42-
invalid_block_version,
43-
timestamp_too_early,
42+
insufficient_block_version,
43+
anachronistic_timestamp,
4444
incorrect_proof_of_work,
4545

4646
// confirm header

src/chain/chain_state.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ inline uint32_t bits_high(const chain_state::data& values) NOEXCEPT
106106
chain_state::activations chain_state::activation(const data& values,
107107
const forks& forks, const system::settings& settings) NOEXCEPT
108108
{
109-
// Initialize activation results with genesis values.
110-
activations result{ flags::no_rules, settings.first_version };
109+
// There are no constraints on block version before bip34.
110+
activations result{ flags::no_rules, 0 };
111111

112112
// regtest is only activated via configuration.
113113
if (forks.retarget)
@@ -231,10 +231,6 @@ chain_state::activations chain_state::activation(const data& values,
231231
{
232232
result.minimum_block_version = settings.bip34_version;
233233
}
234-
else
235-
{
236-
result.minimum_block_version = settings.first_version;
237-
}
238234

239235
// TODO: bip9 historical activation.
240236

src/chain/context.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ namespace libbitcoin {
2525
namespace system {
2626
namespace chain {
2727

28-
bool context::is_enabled(chain::flags flag) const NOEXCEPT
29-
{
30-
return to_bool(flag & flags);
31-
}
32-
3328
bool operator==(const context& left, const context& right) NOEXCEPT
3429
{
3530
return left.flags == right.flags

src/chain/header.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ bool header::is_invalid_proof_of_work(uint32_t proof_of_work_limit,
304304
}
305305

306306
// ****************************************************************************
307-
// CONSENSUS: bitcoin 32bit unix time: en.wikipedia.org/wiki/Year_2038_problem
307+
// CONSENSUS: 32 bit unsigned unix time overflows in 2106.
308308
// ****************************************************************************
309-
bool header::is_invalid_timestamp(
309+
bool header::is_futuristic_timestamp(
310310
uint32_t timestamp_limit_seconds) const NOEXCEPT
311311
{
312312
using namespace std::chrono;
@@ -324,7 +324,7 @@ code header::check(uint32_t timestamp_limit_seconds,
324324
{
325325
if (is_invalid_proof_of_work(proof_of_work_limit, scrypt))
326326
return error::invalid_proof_of_work;
327-
if (is_invalid_timestamp(timestamp_limit_seconds))
327+
if (is_futuristic_timestamp(timestamp_limit_seconds))
328328
return error::futuristic_timestamp;
329329

330330
return error::block_success;
@@ -339,11 +339,11 @@ code header::check(uint32_t timestamp_limit_seconds,
339339
// All other work comparisons performed on expanded/normalized bits values.
340340
code header::accept(const context& ctx) const NOEXCEPT
341341
{
342-
if (version_ < ctx.minimum_block_version)
343-
return error::invalid_block_version;
344-
if (timestamp_ <= ctx.median_time_past)
345-
return error::timestamp_too_early;
346-
if (bits_ != ctx.work_required)
342+
if (ctx.is_insufficient_version(version_))
343+
return error::insufficient_block_version;
344+
if (ctx.is_anachronistic_timestamp(timestamp_))
345+
return error::anachronistic_timestamp;
346+
if (ctx.is_invalid_work(bits_))
347347
return error::incorrect_proof_of_work;
348348

349349
return error::block_success;

src/error/block_error_t.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ DEFINE_ERROR_T_MESSAGE_MAP(block_error)
3535

3636
// accept header
3737
{ checkpoint_conflict, "block hash rejected by checkpoint" },
38-
{ invalid_block_version, "block version rejected at current height" },
39-
{ timestamp_too_early, "block timestamp is too early" },
38+
{ insufficient_block_version, "block version rejected at current height" },
39+
{ anachronistic_timestamp, "block timestamp is too early" },
4040
{ incorrect_proof_of_work, "proof of work does not match bits field" },
4141

4242
// confirm header

test/chain/header.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class accessor
5050
return header::is_invalid_proof_of_work(proof_of_work_limit, scrypt);
5151
}
5252

53-
bool is_invalid_timestamp(uint32_t timestamp_limit_seconds) const
53+
bool is_futuristic_timestamp(uint32_t timestamp_limit_seconds) const
5454
{
55-
return header::is_invalid_timestamp(timestamp_limit_seconds);
55+
return header::is_futuristic_timestamp(timestamp_limit_seconds);
5656
}
5757
};
5858

@@ -328,21 +328,21 @@ BOOST_AUTO_TEST_CASE(header__is_invalid_proof_of_work__scrypt_hash_less_than_bit
328328
BOOST_REQUIRE(!instance.is_invalid_proof_of_work(settings.proof_of_work_limit, true));
329329
}
330330

331-
BOOST_AUTO_TEST_CASE(header__is_invalid_timestamp__timestamp_less_than_2_hours_from_now__false)
331+
BOOST_AUTO_TEST_CASE(header__is_futuristic_timestamp__timestamp_less_than_2_hours_from_now__false)
332332
{
333333
const auto now = std::chrono::system_clock::now();
334334
const auto now_time = possible_narrow_and_sign_cast<uint32_t>(std::chrono::system_clock::to_time_t(now));
335335
const accessor instance{ {}, hash_digest{}, {}, now_time, {}, {} };
336-
BOOST_REQUIRE(!instance.is_invalid_timestamp(settings().timestamp_limit_seconds));
336+
BOOST_REQUIRE(!instance.is_futuristic_timestamp(settings().timestamp_limit_seconds));
337337
}
338338

339-
BOOST_AUTO_TEST_CASE(header__is_invalid_timestamp__timestamp_greater_than_2_hours_from_now__true)
339+
BOOST_AUTO_TEST_CASE(header__is_futuristic_timestamp__timestamp_greater_than_2_hours_from_now__true)
340340
{
341341
const auto now = std::chrono::system_clock::now();
342342
const auto duration = std::chrono::hours(3);
343343
const auto future = possible_narrow_and_sign_cast<uint32_t>(std::chrono::system_clock::to_time_t(now + duration));
344344
const accessor instance{ {}, hash_digest{}, {}, future, {}, {} };
345-
BOOST_REQUIRE(instance.is_invalid_timestamp(settings().timestamp_limit_seconds));
345+
BOOST_REQUIRE(instance.is_futuristic_timestamp(settings().timestamp_limit_seconds));
346346
}
347347

348348
// json

test/error/block_error_t.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,18 @@ BOOST_AUTO_TEST_CASE(block_error_t__code__checkpoint_conflict__true_exected_mess
6262
BOOST_REQUIRE_EQUAL(ec.message(), "block hash rejected by checkpoint");
6363
}
6464

65-
BOOST_AUTO_TEST_CASE(block_error_t__code__invalid_block_version__true_exected_message)
65+
BOOST_AUTO_TEST_CASE(block_error_t__code__insufficient_block_version__true_exected_message)
6666
{
67-
constexpr auto value = error::invalid_block_version;
67+
constexpr auto value = error::insufficient_block_version;
6868
const auto ec = code(value);
6969
BOOST_REQUIRE(ec);
7070
BOOST_REQUIRE(ec == value);
7171
BOOST_REQUIRE_EQUAL(ec.message(), "block version rejected at current height");
7272
}
7373

74-
BOOST_AUTO_TEST_CASE(block_error_t__code__timestamp_too_early__true_exected_message)
74+
BOOST_AUTO_TEST_CASE(block_error_t__code__anachronistic_timestamp__true_exected_message)
7575
{
76-
constexpr auto value = error::timestamp_too_early;
76+
constexpr auto value = error::anachronistic_timestamp;
7777
const auto ec = code(value);
7878
BOOST_REQUIRE(ec);
7979
BOOST_REQUIRE(ec == value);

0 commit comments

Comments
 (0)