From b9a4ea6feb5e325eb7aa6994e3854a311b94341a Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Sat, 13 Apr 2024 14:22:53 -0500 Subject: [PATCH] GH-14 Add connection id to vote log output --- libraries/chain/block_state.cpp | 7 ++++--- libraries/chain/hotstuff/hotstuff.cpp | 17 ++++++++--------- .../chain/hotstuff/test/finality_misc_tests.cpp | 4 ++-- .../chain/include/eosio/chain/block_state.hpp | 2 +- .../include/eosio/chain/hotstuff/hotstuff.hpp | 3 ++- .../include/eosio/chain/vote_processor.hpp | 2 +- unittests/block_state_tests.cpp | 12 ++++++------ 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/libraries/chain/block_state.cpp b/libraries/chain/block_state.cpp index 4735e81de4..5c09ab633a 100644 --- a/libraries/chain/block_state.cpp +++ b/libraries/chain/block_state.cpp @@ -141,8 +141,8 @@ void block_state::set_trxs_metas( deque&& trxs_metas, cached_trxs = std::move( trxs_metas ); } -// Called from net threads -vote_status block_state::aggregate_vote(const vote_message& vote) { +// Called from vote threads +vote_status block_state::aggregate_vote(uint32_t connection_id, const vote_message& vote) { const auto& finalizers = active_finalizer_policy->finalizers; auto it = std::find_if(finalizers.begin(), finalizers.end(), @@ -151,7 +151,8 @@ vote_status block_state::aggregate_vote(const vote_message& vote) { if (it != finalizers.end()) { auto index = std::distance(finalizers.begin(), it); auto digest = vote.strong ? strong_digest.to_uint8_span() : std::span(weak_digest); - return pending_qc.add_vote(block_num(), + return pending_qc.add_vote(connection_id, + block_num(), vote.strong, digest, index, diff --git a/libraries/chain/hotstuff/hotstuff.cpp b/libraries/chain/hotstuff/hotstuff.cpp index ca39f7d93f..5cf54e7094 100644 --- a/libraries/chain/hotstuff/hotstuff.cpp +++ b/libraries/chain/hotstuff/hotstuff.cpp @@ -137,29 +137,28 @@ vote_status pending_quorum_certificate::add_weak_vote(size_t index, const bls_si } // thread safe -vote_status pending_quorum_certificate::add_vote(block_num_type block_num, bool strong, std::span proposal_digest, size_t index, +vote_status pending_quorum_certificate::add_vote(uint32_t connection_id, block_num_type block_num, + bool strong, std::span proposal_digest, size_t index, const bls_public_key& pubkey, const bls_signature& sig, uint64_t weight) { - vote_status s = vote_status::success; - if (has_voted_no_lock(strong, index)) { - dlog("block_num: ${bn}, vote strong: ${sv}, duplicate", ("bn", block_num)("sv", strong)); + dlog("connection - ${c} block_num: ${bn}, duplicate", ("c", connection_id)("bn", block_num)); return vote_status::duplicate; } if (!fc::crypto::blslib::verify(pubkey, proposal_digest, sig)) { - wlog( "signature from finalizer ${i} cannot be verified", ("i", index) ); + wlog("connection - ${c} signature from finalizer ${k}.. cannot be verified", ("k", pubkey.to_string().substr(8,16))); return vote_status::invalid_signature; } std::unique_lock g(*_mtx); state_t pre_state = _state; - s = strong ? add_strong_vote(index, sig, weight) - : add_weak_vote(index, sig, weight); + vote_status s = strong ? add_strong_vote(index, sig, weight) + : add_weak_vote(index, sig, weight); state_t post_state = _state; g.unlock(); - dlog("block_num: ${bn}, vote strong: ${sv}, status: ${s}, pre-state: ${pre}, post-state: ${state}, quorum_met: ${q}", - ("bn", block_num)("sv", strong)("s", s)("pre", pre_state)("state", post_state)("q", is_quorum_met(post_state))); + dlog("connection - ${c} block_num: ${bn}, vote strong: ${sv}, status: ${s}, pre-state: ${pre}, post-state: ${state}, quorum_met: ${q}", + ("c", connection_id)("bn", block_num)("sv", strong)("s", s)("pre", pre_state)("state", post_state)("q", is_quorum_met(post_state))); return s; } diff --git a/libraries/chain/hotstuff/test/finality_misc_tests.cpp b/libraries/chain/hotstuff/test/finality_misc_tests.cpp index ba58a06ed5..a86f9b230d 100644 --- a/libraries/chain/hotstuff/test/finality_misc_tests.cpp +++ b/libraries/chain/hotstuff/test/finality_misc_tests.cpp @@ -51,11 +51,11 @@ BOOST_AUTO_TEST_CASE(qc_state_transitions) try { pubkey.push_back(k.get_public_key()); auto weak_vote = [&](pending_quorum_certificate& qc, const std::vector& digest, size_t index, uint64_t weight) { - return qc.add_vote(0, false, digest, index, pubkey[index], sk[index].sign(digest), weight); + return qc.add_vote(0, 0, false, digest, index, pubkey[index], sk[index].sign(digest), weight); }; auto strong_vote = [&](pending_quorum_certificate& qc, const std::vector& digest, size_t index, uint64_t weight) { - return qc.add_vote(0, true, digest, index, pubkey[index], sk[index].sign(digest), weight); + return qc.add_vote(0, 0, true, digest, index, pubkey[index], sk[index].sign(digest), weight); }; constexpr uint64_t weight = 1; diff --git a/libraries/chain/include/eosio/chain/block_state.hpp b/libraries/chain/include/eosio/chain/block_state.hpp index a212f1655f..0326c758af 100644 --- a/libraries/chain/include/eosio/chain/block_state.hpp +++ b/libraries/chain/include/eosio/chain/block_state.hpp @@ -125,7 +125,7 @@ struct block_state : public block_header_state { // block_header_state provi finality_data_t get_finality_data(); // vote_status - vote_status aggregate_vote(const vote_message& vote); // aggregate vote into pending_qc + vote_status aggregate_vote(uint32_t connection_id, const vote_message& vote); // aggregate vote into pending_qc bool has_voted(const bls_public_key& key) const; void verify_qc(const valid_quorum_certificate& qc) const; // verify given qc is valid with respect block_state diff --git a/libraries/chain/include/eosio/chain/hotstuff/hotstuff.hpp b/libraries/chain/include/eosio/chain/hotstuff/hotstuff.hpp index 9e8682b905..4a2a148cd4 100644 --- a/libraries/chain/include/eosio/chain/hotstuff/hotstuff.hpp +++ b/libraries/chain/include/eosio/chain/hotstuff/hotstuff.hpp @@ -118,7 +118,8 @@ namespace eosio::chain { } // thread safe - vote_status add_vote(block_num_type block_num, + vote_status add_vote(uint32_t connection_id, + block_num_type block_num, bool strong, std::span proposal_digest, size_t index, diff --git a/libraries/chain/include/eosio/chain/vote_processor.hpp b/libraries/chain/include/eosio/chain/vote_processor.hpp index 793705acbc..af076b207e 100644 --- a/libraries/chain/include/eosio/chain/vote_processor.hpp +++ b/libraries/chain/include/eosio/chain/vote_processor.hpp @@ -181,7 +181,7 @@ class vote_processor_t { g.unlock(); // do not hold lock when posting for (auto& v : to_process) { boost::asio::post(thread_pool.get_executor(), [this, bsp, v=std::move(v)]() { - vote_status s = bsp->aggregate_vote(*v.msg); + vote_status s = bsp->aggregate_vote(v.connection_id, *v.msg); if (s != vote_status::duplicate) { // don't bother emitting duplicates emit(v.connection_id, s, v.msg); } diff --git a/unittests/block_state_tests.cpp b/unittests/block_state_tests.cpp index 434dace32b..51e073dd98 100644 --- a/unittests/block_state_tests.cpp +++ b/unittests/block_state_tests.cpp @@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(aggregate_vote_test) try { bool strong = (i % 2 == 0); // alternate strong and weak auto sig = strong ? private_key[i].sign(strong_digest.to_uint8_span()) : private_key[i].sign(weak_digest); vote_message vote{ block_id, strong, public_key[i], sig }; - BOOST_REQUIRE(bsp->aggregate_vote(vote) == vote_status::success); + BOOST_REQUIRE(bsp->aggregate_vote(0, vote) == vote_status::success); } } @@ -58,7 +58,7 @@ BOOST_AUTO_TEST_CASE(aggregate_vote_test) try { bsp->pending_qc = pending_quorum_certificate{ num_finalizers, 1, bsp->active_finalizer_policy->max_weak_sum_before_weak_final() }; vote_message vote {block_id, true, public_key[0], private_key[1].sign(strong_digest.to_uint8_span()) }; - BOOST_REQUIRE(bsp->aggregate_vote(vote) != vote_status::success); + BOOST_REQUIRE(bsp->aggregate_vote(0, vote) != vote_status::success); } { // duplicate votes @@ -68,8 +68,8 @@ BOOST_AUTO_TEST_CASE(aggregate_vote_test) try { bsp->pending_qc = pending_quorum_certificate{ num_finalizers, 1, bsp->active_finalizer_policy->max_weak_sum_before_weak_final() }; vote_message vote {block_id, true, public_key[0], private_key[0].sign(strong_digest.to_uint8_span()) }; - BOOST_REQUIRE(bsp->aggregate_vote(vote) == vote_status::success); - BOOST_REQUIRE(bsp->aggregate_vote(vote) != vote_status::success); + BOOST_REQUIRE(bsp->aggregate_vote(0, vote) == vote_status::success); + BOOST_REQUIRE(bsp->aggregate_vote(0, vote) != vote_status::success); } { // public key does not exit in finalizer set @@ -82,7 +82,7 @@ BOOST_AUTO_TEST_CASE(aggregate_vote_test) try { bls_public_key new_public_key{ new_private_key.get_public_key() }; vote_message vote {block_id, true, new_public_key, private_key[0].sign(strong_digest.to_uint8_span()) }; - BOOST_REQUIRE(bsp->aggregate_vote(vote) != vote_status::success); + BOOST_REQUIRE(bsp->aggregate_vote(0, vote) != vote_status::success); } } FC_LOG_AND_RETHROW(); @@ -125,7 +125,7 @@ void do_quorum_test(const std::vector& weights, if( to_vote[i] ) { auto sig = strong ? private_key[i].sign(strong_digest.to_uint8_span()) : private_key[i].sign(weak_digest); vote_message vote{ block_id, strong, public_key[i], sig }; - BOOST_REQUIRE(bsp->aggregate_vote(vote) == vote_status::success); + BOOST_REQUIRE(bsp->aggregate_vote(0, vote) == vote_status::success); } }