Skip to content

Commit

Permalink
Merge branch 'GH-169-prods-beta1' into GH-169-eosio-system-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed May 27, 2024
2 parents 7218c52 + 5ab1552 commit e7afeb9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 36 deletions.
58 changes: 26 additions & 32 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4253,44 +4253,38 @@ struct controller_impl {
}

void update_producers_authority() {
// this is not called when hotstuff is activated
auto& bb = std::get<building_block>(pending->_block_stage);
bb.apply_l<void>([this](building_block::building_block_legacy& legacy_header) {
pending_block_header_state_legacy& pbhs = legacy_header.pending_block_header_state;
const auto& producers = pbhs.active_schedule.producers;

auto update_permission = [&](auto& permission, auto threshold) {
auto auth = authority(threshold, {}, {});
for (auto& p : producers) {
auth.accounts.push_back({
{p.producer_name, config::active_name},
1
});
}

if (permission.auth != auth) {
db.modify(permission, [&](auto& po) { po.auth = auth; });
}
};
const auto& producers = bb.active_producers().producers;

auto update_permission = [&](auto& permission, auto threshold) {
auto auth = authority(threshold, {}, {});
for (auto& p : producers) {
auth.accounts.push_back({
{p.producer_name, config::active_name},
1
});
}

uint32_t num_producers = producers.size();
auto calculate_threshold = [=](uint32_t numerator, uint32_t denominator) {
return ((num_producers * numerator) / denominator) + 1;
};
if (permission.auth != auth) {
db.modify(permission, [&](auto& po) { po.auth = auth; });
}
};

update_permission(authorization.get_permission({config::producers_account_name, config::active_name}),
calculate_threshold(2, 3) /* more than two-thirds */);
uint32_t num_producers = producers.size();
auto calculate_threshold = [=](uint32_t numerator, uint32_t denominator) {
return ((num_producers * numerator) / denominator) + 1;
};

update_permission(
authorization.get_permission({config::producers_account_name, config::majority_producers_permission_name}),
calculate_threshold(1, 2) /* more than one-half */);
update_permission(authorization.get_permission({config::producers_account_name, config::active_name}),
calculate_threshold(2, 3) /* more than two-thirds */);

update_permission(
authorization.get_permission({config::producers_account_name, config::minority_producers_permission_name}),
calculate_threshold(1, 3) /* more than one-third */);
update_permission(
authorization.get_permission({config::producers_account_name, config::majority_producers_permission_name}),
calculate_threshold(1, 2) /* more than one-half */);

// TODO: Add tests
});
update_permission(
authorization.get_permission({config::producers_account_name, config::minority_producers_permission_name}),
calculate_threshold(1, 3) /* more than one-third */);
}

void create_block_summary(const block_id_type& id) {
Expand Down
18 changes: 17 additions & 1 deletion plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2527,7 +2527,8 @@ namespace eosio {
} else {
if (!blk_applied) {
if (blk_num >= c->sync_last_requested_block) {
peer_dlog(c, "calling cancel_wait, block ${b}", ("b", blk_num));
peer_dlog(c, "calling cancel_wait, block ${b}, sync_last_requested_block ${lrb}",
("b", blk_num)("lrb", c->sync_last_requested_block));
c->cancel_wait();
} else {
peer_dlog(c, "calling sync_wait, block ${b}", ("b", blk_num));
Expand All @@ -2536,6 +2537,7 @@ namespace eosio {

if (sync_last_requested_num == 0) { // block was rejected
sync_next_expected_num = my_impl->get_chain_lib_num() + 1;
peer_dlog(c, "Reset sync_next_expected_num to ${n}", ("n", sync_next_expected_num));
} else {
if (blk_num == sync_next_expected_num) {
++sync_next_expected_num;
Expand All @@ -2544,6 +2546,8 @@ namespace eosio {
}
}
if (blk_num >= sync_known_lib_num) {
peer_dlog(c, "received non-applied block ${bn} > ${kn}, will send handshakes when caught up",
("bn", blk_num)("kn", sync_known_lib_num));
send_handshakes_when_synced = true;
}
}
Expand All @@ -2554,9 +2558,20 @@ namespace eosio {
fc_dlog(logger, "Requesting range ahead, head: ${h} blk_num: ${bn} sync_next_expected_num ${nen} sync_last_requested_num: ${lrn}",
("h", head)("bn", blk_num)("nen", sync_next_expected_num)("lrn", sync_last_requested_num));
request_next_chunk();
return;
}
}

if (!blk_applied && blk_num >= c->sync_last_requested_block) {
// block was not applied, possibly because we already have the block
// We didn't request the next chunk of blocks, request them anyway because we might need them to resolve a fork
fc_dlog(logger, "Requesting blocks, head: ${h} blk_num: ${bn} sync_next_expected_num ${nen} "
"sync_last_requested_num: ${lrn}, sync_last_requested_block: ${lrb}",
("h", head)("bn", blk_num)("nen", sync_next_expected_num)
("lrn", sync_last_requested_num)("lrb", c->sync_last_requested_block));
request_next_chunk();
}

}
} else {
send_handshakes_if_synced(blk_latency);
Expand All @@ -2566,6 +2581,7 @@ namespace eosio {
// thread safe
void sync_manager::send_handshakes_if_synced(const fc::microseconds& blk_latency) {
if (blk_latency.count() < config::block_interval_us && send_handshakes_when_synced) {
fc_dlog(logger, "Block latency within block interval, synced, sending handshakes");
send_handshakes();
send_handshakes_when_synced = false;
}
Expand Down
20 changes: 17 additions & 3 deletions unittests/producer_schedule_if_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <eosio/chain/global_property_object.hpp>
#include <eosio/chain/authorization_manager.hpp>
#include <eosio/testing/tester.hpp>

#include <boost/test/unit_test.hpp>
Expand Down Expand Up @@ -26,19 +27,32 @@ BOOST_FIXTURE_TEST_CASE( verify_producer_schedule_after_instant_finality_activat
const uint32_t check_duration = 100; // number of blocks
bool scheduled_changed_to_new = false;
for (uint32_t i = 0; i < check_duration; ++i) {
const auto current_schedule = control->active_producers();
if (new_prod_schd == current_schedule.producers) {
const auto current_schedule = control->active_producers().producers;
if (new_prod_schd == current_schedule) {
scheduled_changed_to_new = true;
if (expected_block_num != 0)
BOOST_TEST(control->head_block_num() == expected_block_num);

// verify eosio.prods updated
const name usr = config::producers_account_name;
const name active_permission = config::active_name;
const auto* perm = control->db().template find<permission_object, by_owner>(boost::make_tuple(usr, active_permission));
for (auto account : perm->auth.accounts) {
auto act = account.permission.actor;
auto itr = std::find_if( current_schedule.begin(), current_schedule.end(), [&](const auto& p) {
return p.producer_name == act;
});
bool found = itr != current_schedule.end();
BOOST_TEST(found);
}
}

auto b = produce_block();
BOOST_TEST( b->confirmed == 0); // must be 0 after instant finality is enabled

// Check if the producer is the same as what we expect
const auto block_time = control->head_block_time();
const auto& expected_producer = get_expected_producer(current_schedule.producers, block_time);
const auto& expected_producer = get_expected_producer(current_schedule, block_time);
BOOST_TEST(control->head_block_producer() == expected_producer);

if (scheduled_changed_to_new)
Expand Down
13 changes: 13 additions & 0 deletions unittests/producer_schedule_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <eosio/chain/global_property_object.hpp>
#include <eosio/chain/authorization_manager.hpp>
#include <eosio/testing/tester.hpp>

#include <boost/test/unit_test.hpp>
Expand Down Expand Up @@ -31,6 +32,18 @@ BOOST_FIXTURE_TEST_CASE( verify_producer_schedule, validating_tester ) try {
const auto current_schedule = control->active_producers().producers;
if (new_prod_schd == current_schedule) {
scheduled_changed_to_new = true;
// verify eosio.prods updated
const name usr = config::producers_account_name;
const name active_permission = config::active_name;
const auto* perm = control->db().template find<permission_object, by_owner>(boost::make_tuple(usr, active_permission));
for (auto account : perm->auth.accounts) {
auto act = account.permission.actor;
auto itr = std::find_if( current_schedule.begin(), current_schedule.end(), [&](const auto& p) {
return p.producer_name == act;
});
bool found = itr != current_schedule.end();
BOOST_TEST(found);
}
}

// Produce block
Expand Down

0 comments on commit e7afeb9

Please sign in to comment.