Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8574 from EOSIO/duty-cycle-last-block-1.8
Browse files Browse the repository at this point in the history
cpu effort last block - 1.8
  • Loading branch information
heifner authored Feb 6, 2020
2 parents 66c545e + 4851c85 commit 816328a
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,9 @@ class producer_plugin_impl : public std::enable_shared_from_this<producer_plugin

fc::time_point calculate_pending_block_time() const;
fc::time_point calculate_block_deadline( const fc::time_point& ) const;
void schedule_delayed_production_loop(const std::weak_ptr<producer_plugin_impl>& weak_this, const block_timestamp_type& current_block_time);
void schedule_delayed_production_loop(const std::weak_ptr<producer_plugin_impl>& weak_this, optional<fc::time_point> wake_up_time);
optional<fc::time_point> calculate_producer_wake_up_time( const block_timestamp_type& ref_block_time ) const;

};

void new_chain_banner(const eosio::chain::controller& db)
Expand Down Expand Up @@ -1358,6 +1360,7 @@ producer_plugin_impl::start_block_result producer_plugin_impl::start_block() {
const fc::time_point now = fc::time_point::now();
const fc::time_point block_time = calculate_pending_block_time();

const pending_block_mode previous_pending_mode = _pending_block_mode;
_pending_block_mode = pending_block_mode::producing;

// Not our turn
Expand Down Expand Up @@ -1412,10 +1415,16 @@ producer_plugin_impl::start_block_result producer_plugin_impl::start_block() {
const auto start_block_time = block_time - fc::microseconds( config::block_interval_us );
if( now < start_block_time ) {
fc_dlog(_log, "Not producing block waiting for production window ${n} ${bt}", ("n", hbs->block_num + 1)("bt", block_time) );
// start_block_time instead of block_time because scheduled_delayed_production_loop calculates next block time from given time
schedule_delayed_production_loop(weak_from_this(), start_block_time);
// start_block_time instead of block_time because schedule_delayed_production_loop calculates next block time from given time
schedule_delayed_production_loop(weak_from_this(), calculate_producer_wake_up_time(start_block_time));
return start_block_result::waiting_for_production;
}
} else if (previous_pending_mode == pending_block_mode::producing) {
// just produced our last block of our round
const auto start_block_time = block_time - fc::microseconds( config::block_interval_us );
fc_dlog(_log, "Not starting speculative block until ${bt}", ("bt", start_block_time) );
schedule_delayed_production_loop( weak_from_this(), start_block_time);
return start_block_result::waiting_for_production;
}

fc_dlog(_log, "Starting block #${n} ${bt} at ${time}", ("n", hbs->block_num + 1)("bt", block_time)("time", now));
Expand Down Expand Up @@ -1839,7 +1848,7 @@ void producer_plugin_impl::schedule_production_loop() {
} else if (result == start_block_result::waiting_for_block){
if (!_producers.empty() && !production_disabled_by_policy()) {
fc_dlog(_log, "Waiting till another block is received and scheduling Speculative/Production Change");
schedule_delayed_production_loop(weak_this, calculate_pending_block_time());
schedule_delayed_production_loop(weak_this, calculate_producer_wake_up_time(calculate_pending_block_time()));
} else {
fc_dlog(_log, "Waiting till another block is received");
// nothing to do until more blocks arrive
Expand Down Expand Up @@ -1889,16 +1898,15 @@ void producer_plugin_impl::schedule_production_loop() {
} else if (_pending_block_mode == pending_block_mode::speculating && !_producers.empty() && !production_disabled_by_policy()){
fc_dlog(_log, "Speculative Block Created; Scheduling Speculative/Production Change");
EOS_ASSERT( chain.is_building_block(), missing_pending_block_state, "speculating without pending_block_state" );
schedule_delayed_production_loop(weak_this, chain.pending_block_time());
schedule_delayed_production_loop(weak_this, calculate_producer_wake_up_time(chain.pending_block_time()));
} else {
fc_dlog(_log, "Speculative Block Created");
}
}

void producer_plugin_impl::schedule_delayed_production_loop(const std::weak_ptr<producer_plugin_impl>& weak_this, const block_timestamp_type& ref_block_time) {
optional<fc::time_point> producer_plugin_impl::calculate_producer_wake_up_time( const block_timestamp_type& ref_block_time ) const {
// if we have any producers then we should at least set a timer for our next available slot
optional<fc::time_point> wake_up_time;
chain::account_name producer;
for (const auto& p : _producers) {
auto next_producer_block_time = calculate_next_block_time(p, ref_block_time);
if (next_producer_block_time) {
Expand All @@ -1907,17 +1915,22 @@ void producer_plugin_impl::schedule_delayed_production_loop(const std::weak_ptr<
// wake up with a full block interval to the deadline
if( producer_wake_up_time < *wake_up_time ) {
wake_up_time = producer_wake_up_time;
producer = p;
}
} else {
wake_up_time = producer_wake_up_time;
producer = p;
}
}
}
if( !wake_up_time ) {
fc_dlog(_log, "Not Scheduling Speculative/Production, no local producers had valid wake up times");
}

return wake_up_time;
}

void producer_plugin_impl::schedule_delayed_production_loop(const std::weak_ptr<producer_plugin_impl>& weak_this, optional<fc::time_point> wake_up_time) {
if (wake_up_time) {
fc_dlog(_log, "Scheduling Speculative/Production Change for ${p} at ${time}", ("p", producer)("time", wake_up_time));
fc_dlog(_log, "Scheduling Speculative/Production Change at ${time}", ("time", wake_up_time));
static const boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
_timer.expires_at(epoch + boost::posix_time::microseconds(wake_up_time->time_since_epoch().count()));
_timer.async_wait( app().get_priority_queue().wrap( priority::high,
Expand All @@ -1927,8 +1940,6 @@ void producer_plugin_impl::schedule_delayed_production_loop(const std::weak_ptr<
self->schedule_production_loop();
}
} ) );
} else {
fc_dlog(_log, "Not Scheduling Speculative/Production, no local producers had valid wake up times");
}
}

Expand Down

0 comments on commit 816328a

Please sign in to comment.