Skip to content

Commit

Permalink
Merge pull request #279 from AntelopeIO/GH-264-test-voting
Browse files Browse the repository at this point in the history
Test: Optimize test by minimizing voting
  • Loading branch information
heifner authored Jun 13, 2024
2 parents c17eb3d + df82aa3 commit 391be54
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 243 deletions.
15 changes: 14 additions & 1 deletion libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ struct controller_impl {
named_thread_pool<chain> thread_pool;
deep_mind_handler* deep_mind_logger = nullptr;
bool okay_to_print_integrity_hash_on_stop = false;
bool allow_voting = true; // used in unit tests to create long forks or simulate not getting votes
my_finalizers_t my_finalizers;
std::atomic<bool> writing_snapshot = false;

Expand Down Expand Up @@ -3698,9 +3699,13 @@ struct controller_impl {
});
}

bool can_vote_on(const signed_block_ptr& b) {
return allow_voting && b->is_proper_svnn_block();
}

// thread safe
void create_and_send_vote_msg(const block_state_ptr& bsp) {
if (!bsp->block->is_proper_svnn_block())
if (!can_vote_on(bsp->block))
return;

// Each finalizer configured on the node which is present in the active finalizer policy may create and sign a vote.
Expand Down Expand Up @@ -4971,6 +4976,14 @@ void controller::commit_block(block_report& br) {
my->commit_block(br, block_status::incomplete);
}

void controller::allow_voting(bool val) {
my->allow_voting = val;
}

bool controller::can_vote_on(const signed_block_ptr& b) {
return my->can_vote_on(b);
}

void controller::maybe_switch_forks(const forked_callback_t& cb, const trx_meta_cache_lookup& trx_lookup) {
validate_db_available_size();
my->maybe_switch_forks(cb, trx_lookup);
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ namespace eosio::chain {
void assemble_and_complete_block( block_report& br, const signer_callback_type& signer_callback );
void sign_block( const signer_callback_type& signer_callback );
void commit_block(block_report& br);
void allow_voting(bool val);
bool can_vote_on(const signed_block_ptr& b);
void maybe_switch_forks(const forked_callback_t& cb, const trx_meta_cache_lookup& trx_lookup);

// thread-safe
Expand Down
7 changes: 4 additions & 3 deletions libraries/testing/include/eosio/testing/tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,10 @@ namespace eosio::testing {
return true;
}

void allow_voting(bool val) {
control->allow_voting(val);
}

const controller::config& get_config() const {
return cfg;
}
Expand Down Expand Up @@ -669,8 +673,6 @@ namespace eosio::testing {
return;
}
try {
if( num_blocks_to_producer_before_shutdown > 0 )
produce_blocks( num_blocks_to_producer_before_shutdown );
if (!skip_validate && std::uncaught_exceptions() == 0)
BOOST_CHECK_EQUAL( validate(), true );
} catch( const fc::exception& e ) {
Expand Down Expand Up @@ -784,7 +786,6 @@ namespace eosio::testing {
}

unique_ptr<controller> validating_node;
uint32_t num_blocks_to_producer_before_shutdown = 0;
bool skip_validate = false;
};

Expand Down
14 changes: 7 additions & 7 deletions libraries/testing/tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ namespace eosio::testing {
}

void base_tester::_wait_for_vote_if_needed(controller& c) {
if (c.head_block()->is_proper_svnn_block()) {
if (c.can_vote_on(c.head_block())) {
// wait for this node's vote to be processed
size_t retrys = 500;
while (!c.node_has_voted_if_finalizer(c.head_block_id()) && --retrys) {
Expand All @@ -528,12 +528,12 @@ namespace eosio::testing {

signed_block_ptr base_tester::produce_blocks( uint32_t n, bool empty ) {
signed_block_ptr res;
if( empty ) {
for( uint32_t i = 0; i < n; ++i )
res = produce_empty_block();
} else {
for( uint32_t i = 0; i < n; ++i )
res = produce_block();
for (uint32_t i = 0; i < n; ++i) {
// for performance, only vote on the last four to move finality
// This is 4 instead of 3 because the extra block has to be produced to log_irreversible
if (n > 4)
control->allow_voting(i >= n - 4);
res = empty ? produce_empty_block() : produce_block();
}
return res;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/chain_plugin_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ BOOST_FIXTURE_TEST_CASE( get_block_with_invalid_abi, validating_tester ) try {
set_transaction_headers(trx);
trx.sign( get_private_key( "asserter"_n, "active" ), control->get_chain_id() );
push_transaction( trx );
produce_blocks(1);
produce_block();

// retrieve block num
uint32_t headnum = this->control->head_block_num();
Expand All @@ -106,7 +106,7 @@ BOOST_FIXTURE_TEST_CASE( get_block_with_invalid_abi, validating_tester ) try {
BOOST_TEST(pos != std::string::npos);
abi2.replace(pos, 4, "xxxx");
set_abi("asserter"_n, abi2.c_str());
produce_blocks(1);
produce_block();

// resolving the invalid abi result in exception
BOOST_CHECK_THROW(resolver("asserter"_n), invalid_type_inside_abi);
Expand All @@ -132,7 +132,7 @@ BOOST_FIXTURE_TEST_CASE( get_block_with_invalid_abi, validating_tester ) try {

BOOST_AUTO_TEST_CASE( get_consensus_parameters ) try {
tester t{setup_policy::old_wasm_parser};
t.produce_blocks(1);
t.produce_block();

chain_apis::read_only plugin(*(t.control), {}, fc::microseconds::maximum(), fc::microseconds::maximum(), nullptr);

Expand Down
4 changes: 1 addition & 3 deletions unittests/currency_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,11 @@ BOOST_AUTO_TEST_CASE_TEMPLATE( test_symbol, T, validating_testers ) try {
} FC_LOG_AND_RETHROW() /// test_symbol

BOOST_FIXTURE_TEST_CASE( test_proxy_deferred, pre_disable_deferred_trx_currency_tester ) try {
produce_blocks(2);

create_accounts( {"alice"_n, "proxy"_n} );
produce_block();

set_code("proxy"_n, test_contracts::proxy_wasm());
produce_blocks(1);
produce_block();

abi_serializer proxy_abi_ser(json::from_string(test_contracts::proxy_abi()).as<abi_def>(), abi_serializer::create_yield_function( abi_serializer_max_time ));

Expand Down
Loading

0 comments on commit 391be54

Please sign in to comment.