Skip to content

Commit

Permalink
Merge pull request #538 from evoskuil/master
Browse files Browse the repository at this point in the history
Comments, style, tests, adapt to system changes.
  • Loading branch information
evoskuil authored Feb 18, 2024
2 parents bd12e45 + 7f41379 commit 7d4e1a4
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 34 deletions.
4 changes: 0 additions & 4 deletions console/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,10 +791,6 @@ void executor::read_test() const
//// state.median_time_past = ctx.mtp;
//// state.timestamp = block->header().timestamp();
////
//// // hack in bit0 late and _bit1(segwit) on schedule.
//// state.forks |= (chain::forks::bip9_bit0_group |
//// chain::forks::bip9_bit1_group);
////
//// // split from accept.
//// if ((ec = block->check(state)))
//// {
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class full_node;
/// Events are passed between chasers using the full_node shared notifier.
/// Notifications are bounced from sink (e.g. chaser) to its strand, and
/// notify bounces from source (e.g. chaser) to network strand.
/// Unlike protocols chasers can stop the node.
class BCN_API chaser
: public network::reporter
{
Expand Down
4 changes: 4 additions & 0 deletions include/bitcoin/node/chasers/chaser_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class BCN_API chaser_header
/// Move tree header to database and push to top of candidate chain.
virtual bool push(const system::hash_digest& key) NOEXCEPT;

/// Properties.
virtual const network::wall_clock::duration& currency_window() const NOEXCEPT;
virtual bool use_currency_window() const NOEXCEPT;

private:
void do_handle_event(const code& ec, chase event_, link value) NOEXCEPT;
void do_organize(const system::chain::header::cptr& header,
Expand Down
27 changes: 19 additions & 8 deletions src/chasers/chaser_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ chaser_header::chaser_header(full_node& node) NOEXCEPT
{
}

// protected
const network::wall_clock::duration&
chaser_header::currency_window() const NOEXCEPT
{
return currency_window_;
}

// protected
bool chaser_header::use_currency_window() const NOEXCEPT
{
return use_currency_window_;
}

// protected
code chaser_header::start() NOEXCEPT
{
Expand Down Expand Up @@ -190,11 +203,12 @@ void chaser_header::do_organize(const chain::header::cptr& header,
// protected
bool chaser_header::is_current(const chain::header& header) const NOEXCEPT
{
if (!use_currency_window_)
if (!use_currency_window())
return true;

// en.wikipedia.org/wiki/Time_formatting_and_storage_bugs#Year_2106
const auto time = wall_clock::from_time_t(header.timestamp());
const auto current = wall_clock::now() - currency_window_;
const auto current = wall_clock::now() - currency_window();
return time >= current;
}

Expand All @@ -211,8 +225,7 @@ bool chaser_header::get_branch_work(uint256_t& work, size_t& point,
const auto& query = archive();

// Sum all branch work from tree.
for (auto it = tree_.find(*previous);
it != tree_.end();
for (auto it = tree_.find(*previous); it != tree_.end();
it = tree_.find(*previous))
{
previous = &it->second.header->previous_block_hash();
Expand All @@ -222,8 +235,7 @@ bool chaser_header::get_branch_work(uint256_t& work, size_t& point,

// Sum branch work from store.
database::height_link link{};
for (link = query.to_header(*previous);
!query.is_candidate_block(link);
for (link = query.to_header(*previous); !query.is_candidate_block(link);
link = query.to_parent(link))
{
uint32_t bits{};
Expand Down Expand Up @@ -287,8 +299,7 @@ database::header_link chaser_header::push(const chain::header::cptr& header,
const chain::context& context) const NOEXCEPT
{
auto& query = archive();
const auto link = query.set_link(*header,
database::context
const auto link = query.set_link(*header, database::context
{
possible_narrow_cast<flags_t>(context.forks),
possible_narrow_cast<height_t>(context.height),
Expand Down
12 changes: 6 additions & 6 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,18 @@ options_metadata parser::load_settings() THROWS
)
// [version properties excluded here]
(
"bitcoin.activation_threshold",
value<size_t>(&configured.bitcoin.activation_threshold),
"bitcoin.bip34_activation_threshold",
value<size_t>(&configured.bitcoin.bip34_activation_threshold),
"The number of new version blocks required for bip34 style soft fork activation, defaults to 750."
)
(
"bitcoin.enforcement_threshold",
value<size_t>(&configured.bitcoin.enforcement_threshold),
"bitcoin.bip34_enforcement_threshold",
value<size_t>(&configured.bitcoin.bip34_enforcement_threshold),
"The number of new version blocks required for bip34 style soft fork enforcement, defaults to 950."
)
(
"bitcoin.activation_sample",
value<size_t>(&configured.bitcoin.activation_sample),
"bitcoin.bip34_activation_sample",
value<size_t>(&configured.bitcoin.bip34_activation_sample),
"The number of blocks considered for bip34 style soft fork activation, defaults to 1000."
)
(
Expand Down
4 changes: 0 additions & 4 deletions src/protocols/protocol_block_in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,6 @@ bool protocol_block_in::handle_receive_block(const code& ec,

////auto& query = archive();
////const auto context = state_->context();
////
////// TODO: ensure soft forks activated in chain_state.
//////// context.forks |= (chain::forks::bip9_bit0_group | chain::forks::bip9_bit1_group);
////
////const auto link = query.set_link(block, context);
////if (link.is_terminal())
////{
Expand Down
11 changes: 1 addition & 10 deletions src/protocols/protocol_header_in_31800.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,8 @@ void protocol_header_in_31800::start() NOEXCEPT

// header sync is always CANDIDATEs.
state_ = archive().get_candidate_chain_state(config().bitcoin);
BC_ASSERT_MSG(state_, "Store not initialized.");

if (!state_)
{
LOGF("protocol_header_in_31800, state not initialized.");
return;
}

// There is one persistent common headers subscription.
SUBSCRIBE_CHANNEL2(headers, handle_receive_headers, _1, _2);
SEND1(create_get_headers(), handle_send, _1);
protocol::start();
Expand Down Expand Up @@ -123,10 +117,7 @@ bool protocol_header_in_31800::handle_receive_headers(const code& ec,
BC_POP_WARNING()

auto context = state_->context();
// TODO: ensure soft forks activated in chain_state.
//// context.forks |= (chain::forks::bip9_bit0_group | chain::forks::bip9_bit1_group);
error = header.accept(context);

if (error)
{
LOGR("Invalid header (accept) [" << encode_hash(hash)
Expand Down
125 changes: 123 additions & 2 deletions test/chasers/chaser_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,130 @@

BOOST_AUTO_TEST_SUITE(chaser_header_tests)

BOOST_AUTO_TEST_CASE(chaser_header_test)
class mock_chaser_header
: chaser_header
{
BOOST_REQUIRE(true);
public:
using chaser_header::chaser_header;

const auto& tree() NOEXCEPT
{
return tree_;
}

const network::wall_clock::duration& currency_window() const NOEXCEPT
{
return chaser_header::currency_window();
}

bool use_currency_window() const NOEXCEPT
{
return chaser_header::use_currency_window();
}

code start() NOEXCEPT override
{
return chaser_header::start();
}

void handle_event(const code& ec, chase event_,
link value) NOEXCEPT override
{
return chaser_header::handle_event(ec, event_, value);
}

bool get_branch_work(uint256_t& work, size_t& point,
system::hashes& tree_branch, header_links& store_branch,
const system::chain::header& header) const NOEXCEPT override
{
return chaser_header::get_branch_work(work, point, tree_branch,
store_branch, header);
}

bool get_is_strong(bool& strong, const uint256_t& work,
size_t point) const NOEXCEPT override
{
return chaser_header::get_is_strong(strong, work, point);
}

bool is_current(
const system::chain::header& header) const NOEXCEPT override
{
return chaser_header::is_current(header);
}

void save(const system::chain::header::cptr& header,
const system::chain::context& context) NOEXCEPT override
{
return chaser_header::save(header, context);
}

database::header_link push(const system::chain::header::cptr& header,
const system::chain::context& context) const NOEXCEPT override
{
return chaser_header::push(header, context);
}

bool push(const system::hash_digest& key) NOEXCEPT override
{
return chaser_header::push(key);
}
};

BOOST_AUTO_TEST_CASE(chaser_header_test__currency_window__zero__use_currency_window_false)
{
const network::logger log{};
node::configuration config(system::chain::selection::mainnet);
config.node.currency_window_minutes = 0;

full_node::store store(config.database);
full_node::query query(store);
full_node node(query, config, log);
mock_chaser_header instance(node);
BOOST_REQUIRE(!instance.use_currency_window());
}

BOOST_AUTO_TEST_CASE(chaser_header_test__currency_window__nonzero__use_currency_window_true)
{
const network::logger log{};
node::configuration config(system::chain::selection::mainnet);
config.node.currency_window_minutes = 60;

full_node::store store(config.database);
full_node::query query(store);
full_node node(query, config, log);
mock_chaser_header instance(node);
BOOST_REQUIRE(instance.use_currency_window());
}

BOOST_AUTO_TEST_CASE(chaser_header_test__is_current__zero_currency_window__true)
{
const network::logger log{};
node::configuration config(system::chain::selection::mainnet);
config.node.currency_window_minutes = 0;

full_node::store store(config.database);
full_node::query query(store);
full_node node(query, config, log);
mock_chaser_header instance(node);

BOOST_REQUIRE(instance.is_current(system::chain::header{ {}, {}, {}, 0, {}, {} }));
BOOST_REQUIRE(instance.is_current(system::chain::header{ {}, {}, {}, max_uint32, {}, {} }));
}

BOOST_AUTO_TEST_CASE(chaser_header_test__is_current__one_minute_currency_window__expected)
{
const network::logger log{};
node::configuration config(system::chain::selection::mainnet);
config.node.currency_window_minutes = 1;

full_node::store store(config.database);
full_node::query query(store);
full_node node(query, config, log);
mock_chaser_header instance(node);

BOOST_REQUIRE(!instance.is_current(system::chain::header{ {}, {}, {}, 0, {}, {} }));
BOOST_REQUIRE(instance.is_current(system::chain::header{ {}, {}, {}, max_uint32, {}, {} }));
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 7d4e1a4

Please sign in to comment.