Skip to content

Commit

Permalink
GH-14 Add connection id to vote log output
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Apr 13, 2024
1 parent 51728b5 commit b9a4ea6
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
7 changes: 4 additions & 3 deletions libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ void block_state::set_trxs_metas( deque<transaction_metadata_ptr>&& 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(),
Expand All @@ -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<const uint8_t>(weak_digest);
return pending_qc.add_vote(block_num(),
return pending_qc.add_vote(connection_id,
block_num(),
vote.strong,
digest,
index,
Expand Down
17 changes: 8 additions & 9 deletions libraries/chain/hotstuff/hotstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const uint8_t> 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<const uint8_t> 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;
}

Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/hotstuff/test/finality_misc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>& 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<uint8_t>& 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;
Expand Down
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 @@ -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

Expand Down
3 changes: 2 additions & 1 deletion libraries/chain/include/eosio/chain/hotstuff/hotstuff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const uint8_t> proposal_digest,
size_t index,
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/vote_processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 6 additions & 6 deletions unittests/block_state_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();

Expand Down Expand Up @@ -125,7 +125,7 @@ void do_quorum_test(const std::vector<uint64_t>& 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);
}
}

Expand Down

0 comments on commit b9a4ea6

Please sign in to comment.