Skip to content

Commit

Permalink
Merge pull request #2218 from AntelopeIO/GH2175-enable-short-fork-test
Browse files Browse the repository at this point in the history
IF: Test: Enable nodeos_short_fork_take_over_if_test
  • Loading branch information
heifner authored Feb 7, 2024
2 parents 4e92a59 + 70511a3 commit 55606cb
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 82 deletions.
3 changes: 3 additions & 0 deletions libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ block_state::block_state(const block_state_legacy& bsp) {
const auto& if_extension = std::get<instant_finality_extension>(*ext);
assert(if_extension.new_finalizer_policy); // required by current transition mechanism
active_finalizer_policy = std::make_shared<finalizer_policy>(*if_extension.new_finalizer_policy);
// TODO: https://github.com/AntelopeIO/leap/issues/2057
// TODO: Do not aggregate votes on blocks created from block_state_legacy. This can be removed when #2057 complete.
pending_qc = pending_quorum_certificate{active_finalizer_policy->finalizers.size(), active_finalizer_policy->threshold, active_finalizer_policy->max_weak_sum_before_weak_final()};
active_proposer_policy = std::make_shared<proposer_policy>();
active_proposer_policy->active_time = bsp.timestamp();
active_proposer_policy->proposer_schedule = bsp.active_schedule;
Expand Down
7 changes: 6 additions & 1 deletion libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3006,7 +3006,12 @@ struct controller_impl {
return bsp->aggregate_vote(vote);
return {vote_status::unknown_block, {}};
};
auto [status, new_lib] = fork_db.apply_if<std::pair<vote_status, std::optional<uint32_t>>>(do_vote);
// TODO: https://github.com/AntelopeIO/leap/issues/2057
// TODO: Do not aggregate votes on block_state if in legacy block fork_db
auto do_vote_legacy = [](auto&) -> std::pair<vote_status, std::optional<uint32_t>> {
return {vote_status::unknown_block, {}};
};
auto [status, new_lib] = fork_db.apply<std::pair<vote_status, std::optional<uint32_t>>>(do_vote_legacy, do_vote);
if (new_lib) {
set_if_irreversible_block_num(*new_lib);
}
Expand Down
30 changes: 1 addition & 29 deletions libraries/chain/hotstuff/hotstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ valid_quorum_certificate pending_quorum_certificate::to_valid_quorum_certificate

valid_quorum_certificate valid_qc;

valid_qc._proposal_id = _proposal_id;
valid_qc._proposal_digest = _proposal_digest;
if( _state == state_t::strong ) {
valid_qc._strong_votes = _strong_votes._bitset;
valid_qc._sig = _strong_votes._sig;
Expand All @@ -155,41 +153,15 @@ valid_quorum_certificate pending_quorum_certificate::to_valid_quorum_certificate
return valid_qc;
}

// ================== begin compatibility functions =======================
// these are present just to make the tests still work. will be removed.
// these assume *only* strong votes.
quorum_certificate_message pending_quorum_certificate::to_msg() const {
return {.proposal_id = _proposal_id,
.strong_votes = bitset_to_vector(_strong_votes._bitset),
.active_agg_sig = _strong_votes._sig};
}

std::string pending_quorum_certificate::get_votes_string() const {
return std::string("strong(\"") + bitset_to_string(_strong_votes._bitset) + "\", weak(\"" +
bitset_to_string(_weak_votes._bitset) + "\"";
}
// ================== end compatibility functions =======================

valid_quorum_certificate::valid_quorum_certificate(
const fc::sha256& proposal_id, const std::vector<uint8_t>& proposal_digest,
const std::vector<uint32_t>& strong_votes, // bitset encoding, following canonical order
const std::vector<uint32_t>& weak_votes, // bitset encoding, following canonical order
const bls_signature& sig)
: _proposal_id(proposal_id)
, _proposal_digest(proposal_digest)
, _sig(sig) {
: _sig(sig) {
if (!strong_votes.empty())
_strong_votes = vector_to_bitset(strong_votes);
if (!weak_votes.empty())
_weak_votes = vector_to_bitset(weak_votes);
}

quorum_certificate_message valid_quorum_certificate::to_msg() const {
return {
.proposal_id = _proposal_id,
.strong_votes = _strong_votes ? bitset_to_vector(*_strong_votes) : std::vector<uint32_t>{1, 0},
.active_agg_sig = _sig
};
}

} // namespace eosio::chain
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/block_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ using block_state_ptr = std::shared_ptr<block_state>;
} // namespace eosio::chain

// not exporting pending_qc or valid_qc
FC_REFLECT_DERIVED( eosio::chain::block_state, (eosio::chain::block_header_state), (block)(validated)(strong_digest)(weak_digest) )
FC_REFLECT_DERIVED( eosio::chain::block_state, (eosio::chain::block_header_state), (block)(validated)(strong_digest)(weak_digest)(pending_qc)(valid_qc) )
33 changes: 7 additions & 26 deletions libraries/chain/include/eosio/chain/hotstuff/hotstuff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ namespace eosio::chain {
// -------------------- valid_quorum_certificate -------------------------------------------------
class valid_quorum_certificate {
public:
valid_quorum_certificate(const fc::sha256& proposal_id,
const std::vector<uint8_t>& proposal_digest,
const std::vector<uint32_t>& strong_votes, //bitset encoding, following canonical order
valid_quorum_certificate(const std::vector<uint32_t>& strong_votes, //bitset encoding, following canonical order
const std::vector<uint32_t>& weak_votes, //bitset encoding, following canonical order
const bls_signature& sig);

Expand All @@ -119,16 +117,7 @@ namespace eosio::chain {
bool is_weak() const { return !!_weak_votes; }
bool is_strong() const { return !_weak_votes; }

// ================== begin compatibility functions =======================
// these are present just to make the tests still work. will be removed.
// these assume *only* strong votes.
quorum_certificate_message to_msg() const;
const fc::sha256& get_proposal_id() const { return _proposal_id; }
// ================== end compatibility functions =======================

friend struct fc::reflector<valid_quorum_certificate>;
fc::sha256 _proposal_id; // [todo] remove
std::vector<uint8_t> _proposal_digest; // [todo] remove
std::optional<hs_bitset> _strong_votes;
std::optional<hs_bitset> _weak_votes;
bls_signature _sig;
Expand Down Expand Up @@ -174,28 +163,18 @@ namespace eosio::chain {

// thread safe
std::pair<vote_status, bool> add_vote(bool strong,
const std::vector<uint8_t>&proposal_digest,
const std::vector<uint8_t>& proposal_digest,
size_t index,
const bls_public_key&pubkey,
const bls_signature&sig,
const bls_public_key& pubkey,
const bls_signature& sig,
uint64_t weight);

state_t state() const { std::lock_guard g(*_mtx); return _state; };
valid_quorum_certificate to_valid_quorum_certificate() const;

// ================== begin compatibility functions =======================
// these are present just to make the tests still work. will be removed.
// these assume *only* strong votes.
quorum_certificate_message to_msg() const;
const fc::sha256& get_proposal_id() const { return _proposal_id; }
std::string get_votes_string() const;
// ================== end compatibility functions =======================

private:
friend struct fc::reflector<pending_quorum_certificate>;
friend class qc_chain;
fc::sha256 _proposal_id; // only used in to_msg(). Remove eventually
std::vector<uint8_t> _proposal_digest;
std::unique_ptr<std::mutex> _mtx;
uint64_t _quorum {0};
uint64_t _max_weak_sum_before_weak_final {0}; // max weak sum before becoming weak_final
Expand Down Expand Up @@ -228,5 +207,7 @@ FC_REFLECT(eosio::chain::vote_message, (proposal_id)(strong)(finalizer_key)(sig)
FC_REFLECT(eosio::chain::hs_proposal_message, (proposal_id)(block_id)(parent_id)(final_on_qc)(justify)(phase_counter));
FC_REFLECT(eosio::chain::hs_new_view_message, (high_qc));
FC_REFLECT(eosio::chain::hs_message, (msg));
FC_REFLECT(eosio::chain::valid_quorum_certificate, (_proposal_id)(_proposal_digest)(_strong_votes)(_weak_votes)(_sig));
FC_REFLECT(eosio::chain::valid_quorum_certificate, (_strong_votes)(_weak_votes)(_sig));
FC_REFLECT(eosio::chain::pending_quorum_certificate, (_quorum)(_max_weak_sum_before_weak_final)(_state)(_strong_sum)(_weak_sum)(_weak_votes)(_strong_votes));
FC_REFLECT(eosio::chain::pending_quorum_certificate::votes_t, (_bitset)(_sig));
FC_REFLECT(eosio::chain::quorum_certificate, (block_num)(qc));
30 changes: 18 additions & 12 deletions plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2142,8 +2142,8 @@ namespace eosio {
void sync_manager::request_next_chunk( const connection_ptr& conn ) REQUIRES(sync_mtx) {
auto chain_info = my_impl->get_chain_info();

fc_dlog( logger, "sync_last_requested_num: ${r}, sync_next_expected_num: ${e}, sync_known_lib_num: ${k}, sync_req_span: ${s}, head: ${h}",
("r", sync_last_requested_num)("e", sync_next_expected_num)("k", sync_known_lib_num)("s", sync_req_span)("h", chain_info.head_num) );
fc_dlog( logger, "sync_last_requested_num: ${r}, sync_next_expected_num: ${e}, sync_known_lib_num: ${k}, sync_req_span: ${s}, head: ${h}, lib: ${lib}",
("r", sync_last_requested_num)("e", sync_next_expected_num)("k", sync_known_lib_num)("s", sync_req_span)("h", chain_info.head_num)("lib", chain_info.lib_num) );

if( chain_info.head_num + sync_req_span < sync_last_requested_num && sync_source && sync_source->current() ) {
fc_dlog( logger, "ignoring request, head is ${h} last req = ${r}, sync_next_expected_num: ${e}, sync_known_lib_num: ${k}, sync_req_span: ${s}, source connection ${c}",
Expand Down Expand Up @@ -2187,8 +2187,8 @@ namespace eosio {
sync_last_requested_num = end;
sync_source = new_sync_source;
request_sent = true;
new_sync_source->strand.post( [new_sync_source, start, end, head_num=chain_info.head_num]() {
peer_ilog( new_sync_source, "requesting range ${s} to ${e}, head ${h}", ("s", start)("e", end)("h", head_num) );
new_sync_source->strand.post( [new_sync_source, start, end, head_num=chain_info.head_num, lib=chain_info.lib_num]() {
peer_ilog( new_sync_source, "requesting range ${s} to ${e}, head ${h}, lib ${lib}", ("s", start)("e", end)("h", head_num)("lib", lib) );
new_sync_source->request_sync_blocks( start, end );
} );
}
Expand Down Expand Up @@ -2234,8 +2234,10 @@ namespace eosio {

if( sync_state != lib_catchup ) {
set_state( lib_catchup );
sync_next_expected_num = chain_info.lib_num + 1;
} else {
sync_next_expected_num = std::max( chain_info.lib_num + 1, sync_next_expected_num );
}
sync_next_expected_num = std::max( chain_info.lib_num + 1, sync_next_expected_num );

request_next_chunk( c );
}
Expand Down Expand Up @@ -2443,11 +2445,10 @@ namespace eosio {
// called from connection strand
void sync_manager::rejected_block( const connection_ptr& c, uint32_t blk_num, closing_mode mode ) {
c->block_status_monitor_.rejected();
// reset sync on rejected block
fc::unique_lock g( sync_mtx );
sync_last_requested_num = 0;
if (blk_num < sync_next_expected_num) {
sync_next_expected_num = my_impl->get_chain_lib_num();
}
sync_next_expected_num = my_impl->get_chain_lib_num() + 1;
if( mode == closing_mode::immediately || c->block_status_monitor_.max_events_violated()) {
peer_wlog( c, "block ${bn} not accepted, closing connection", ("bn", blk_num) );
sync_source.reset();
Expand Down Expand Up @@ -2526,7 +2527,11 @@ namespace eosio {
c->sync_wait();
}

sync_next_expected_num = blk_num + 1;
if (sync_last_requested_num == 0) { // block was rejected
sync_next_expected_num = my_impl->get_chain_lib_num() + 1;
} else {
sync_next_expected_num = blk_num + 1;
}
}

uint32_t head = my_impl->get_chain_head_num();
Expand Down Expand Up @@ -3824,8 +3829,9 @@ namespace eosio {
// use c in this method instead of this to highlight that all methods called on c-> must be thread safe
connection_ptr c = shared_from_this();

uint32_t lib = cc.last_irreversible_block_num();
try {
if( blk_num <= cc.last_irreversible_block_num() || cc.block_exists(blk_id) ) {
if( blk_num <= lib || cc.block_exists(blk_id) ) {
c->strand.post( [sync_master = my_impl->sync_master.get(),
dispatcher = my_impl->dispatcher.get(), c, blk_id, blk_num]() {
dispatcher->add_peer_block( blk_id, c->connection_id );
Expand All @@ -3841,8 +3847,8 @@ namespace eosio {
}

fc::microseconds age( fc::time_point::now() - block->timestamp);
fc_dlog( logger, "received signed_block: #${n} block age in secs = ${age}, connection ${cid}, ${v}",
("n", blk_num)("age", age.to_seconds())("cid", c->connection_id)("v", obt ? "pre-validated" : "validation pending") );
fc_dlog( logger, "received signed_block: #${n} block age in secs = ${age}, connection ${cid}, ${v}, lib #${lib}",
("n", blk_num)("age", age.to_seconds())("cid", c->connection_id)("v", obt ? "pre-validated" : "validation pending")("lib", lib) );

go_away_reason reason = no_reason;
bool accepted = false;
Expand Down
11 changes: 8 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ship_test.py ${CMAKE_CURRENT_BINARY_D
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ship_streamer_test.py ${CMAKE_CURRENT_BINARY_DIR}/ship_streamer_test.py COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/bridge_for_fork_test_shape.json ${CMAKE_CURRENT_BINARY_DIR}/bridge_for_fork_test_shape.json COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/large-lib-test.py ${CMAKE_CURRENT_BINARY_DIR}/large-lib-test.py COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/lib_advance_test.py ${CMAKE_CURRENT_BINARY_DIR}/lib_advance_test.py COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/http_plugin_test.py ${CMAKE_CURRENT_BINARY_DIR}/http_plugin_test.py COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/p2p_high_latency_test.py ${CMAKE_CURRENT_BINARY_DIR}/p2p_high_latency_test.py COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/p2p_multiple_listen_test.py ${CMAKE_CURRENT_BINARY_DIR}/p2p_multiple_listen_test.py COPYONLY)
Expand Down Expand Up @@ -298,9 +299,8 @@ set_property(TEST nodeos_startup_catchup_if_lr_test PROPERTY LABELS long_running

add_test(NAME nodeos_short_fork_take_over_test COMMAND tests/nodeos_short_fork_take_over_test.py -v --wallet-port 9905 ${UNSHARE} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_property(TEST nodeos_short_fork_take_over_test PROPERTY LABELS nonparallelizable_tests)
# requires https://github.com/AntelopeIO/leap/issues/2175
#add_test(NAME nodeos_short_fork_take_over_if_test COMMAND tests/nodeos_short_fork_take_over_test.py -v --activate-if --wallet-port 9905 ${UNSHARE} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
#set_property(TEST nodeos_short_fork_take_over_if_test PROPERTY LABELS nonparallelizable_tests)
add_test(NAME nodeos_short_fork_take_over_if_test COMMAND tests/nodeos_short_fork_take_over_test.py -v --activate-if --wallet-port 9905 ${UNSHARE} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_property(TEST nodeos_short_fork_take_over_if_test PROPERTY LABELS nonparallelizable_tests)

add_test(NAME nodeos_extra_packed_data_test COMMAND tests/nodeos_extra_packed_data_test.py -v -p 2 ${UNSHARE} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_property(TEST nodeos_extra_packed_data_test PROPERTY LABELS nonparallelizable_tests)
Expand Down Expand Up @@ -328,6 +328,11 @@ set_property(TEST larger_lib_test PROPERTY LABELS nonparallelizable_tests)
add_test(NAME larger_lib_if_test COMMAND tests/large-lib-test.py --activate-if ${UNSHARE} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_property(TEST larger_lib_if_test PROPERTY LABELS nonparallelizable_tests)

add_test(NAME lib_advance_test COMMAND tests/lib_advance_test.py -v ${UNSHARE} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_property(TEST lib_advance_test PROPERTY LABELS nonparallelizable_tests)
add_test(NAME lib_advance_if_test COMMAND tests/lib_advance_test.py --activate-if -v ${UNSHARE} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_property(TEST lib_advance_if_test PROPERTY LABELS nonparallelizable_tests)

add_test(NAME leap_util_bls_test COMMAND tests/leap_util_bls_test.py WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

add_test(NAME http_plugin_test COMMAND tests/http_plugin_test.py ${UNSHARE} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
Expand Down
Loading

0 comments on commit 55606cb

Please sign in to comment.