Skip to content

Commit

Permalink
updated qc_chain to use bls_public_key as finalizer id, completed int…
Browse files Browse the repository at this point in the history
…ernal wiring for signing and signature verification
  • Loading branch information
systemzax committed Sep 10, 2023
1 parent 5ede17a commit c8d9d38
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 152 deletions.
4 changes: 2 additions & 2 deletions libraries/chain/include/eosio/chain/hotstuff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace eosio::chain {

struct hs_vote_message {
fc::sha256 proposal_id; //vote on proposal
name finalizer;
fc::crypto::blslib::bls_public_key finalizer_key;
fc::crypto::blslib::bls_signature sig;
};

Expand Down Expand Up @@ -84,7 +84,7 @@ namespace eosio::chain {
// // @ignore quorum_met
FC_REFLECT(eosio::chain::quorum_certificate_message, (proposal_id)(active_finalizers)(active_agg_sig));
FC_REFLECT(eosio::chain::extended_schedule, (producer_schedule)(bls_pub_keys));
FC_REFLECT(eosio::chain::hs_vote_message, (proposal_id)(finalizer)(sig));
FC_REFLECT(eosio::chain::hs_vote_message, (proposal_id)(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_block_message, (block_id)(justify));
FC_REFLECT(eosio::chain::hs_new_view_message, (high_qc));
Expand Down
33 changes: 24 additions & 9 deletions libraries/hotstuff/chain_pacemaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace eosio { namespace hotstuff {
bls_key_map_t finalizer_keys,
fc::logger& logger)
: _chain(chain),
_qc_chain("default"_n, this, std::move(my_producers), std::move(finalizer_keys), logger),
_qc_chain(std::string("default"), this, std::move(my_producers), std::move(finalizer_keys), logger),
_logger(logger)
{
_accepted_block_connection = chain->accepted_block.connect( [this]( const block_state_ptr& blk ) {
Expand Down Expand Up @@ -260,23 +260,38 @@ namespace eosio { namespace hotstuff {
return n;
}

std::vector<name> chain_pacemaker::get_finalizers() {
std::vector<fc::crypto::blslib::bls_public_key> chain_pacemaker::get_finalizer_keys() {

#warning FIXME: Use _active_finalizer_set in pacemaker/qc_chain.
//#warning FIXME: Use _active_finalizer_set in pacemaker/qc_chain.
// _active_finalizer_set should be used

std::unique_lock g( _chain_state_mutex );
block_state_ptr hbs = _head_block_state;
g.unlock();

// Old code: get eosio::name from the producer schedule
std::vector<fc::crypto::blslib::bls_public_key> active_pub_keys;
active_pub_keys.reserve(_active_finalizer_set.finalizers.size());

std::transform(_active_finalizer_set.finalizers.begin(), _active_finalizer_set.finalizers.end(), active_pub_keys.begin(), [](finalizer_authority f_auth) {
return f_auth.public_key;
});

return active_pub_keys;

/* // Old code: get eosio::name from the producer schedule
const std::vector<producer_authority>& pa_list = hbs->active_schedule.producers;
std::vector<name> pn_list;
pn_list.reserve(pa_list.size());
std::transform(pa_list.begin(), pa_list.end(),
std::back_inserter(pn_list),
[](const producer_authority& p) { return p.producer_name; });
return pn_list;
return pn_list;*/

//_active_finalizer_set.finalizers




}

block_id_type chain_pacemaker::get_current_block_id() {
Expand All @@ -297,19 +312,19 @@ namespace eosio { namespace hotstuff {
prof.core_out();
}

void chain_pacemaker::send_hs_proposal_msg(const hs_proposal_message& msg, name id) {
void chain_pacemaker::send_hs_proposal_msg(const hs_proposal_message& msg, const std::string& id) {
bcast_hs_message(msg);
}

void chain_pacemaker::send_hs_vote_msg(const hs_vote_message& msg, name id) {
void chain_pacemaker::send_hs_vote_msg(const hs_vote_message& msg, const std::string& id) {
bcast_hs_message(msg);
}

void chain_pacemaker::send_hs_new_block_msg(const hs_new_block_message& msg, name id) {
void chain_pacemaker::send_hs_new_block_msg(const hs_new_block_message& msg, const std::string& id) {
bcast_hs_message(msg);
}

void chain_pacemaker::send_hs_new_view_msg(const hs_new_view_message& msg, name id) {
void chain_pacemaker::send_hs_new_view_msg(const hs_new_view_message& msg, const std::string& id) {
bcast_hs_message(msg);
}

Expand Down
14 changes: 9 additions & 5 deletions libraries/hotstuff/include/eosio/hotstuff/base_pacemaker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <eosio/chain/types.hpp>
#include <eosio/chain/name.hpp>

#include <fc/crypto/bls_utils.hpp>

#include <vector>

namespace eosio::chain {
Expand Down Expand Up @@ -33,13 +35,15 @@ namespace eosio::hotstuff {
virtual chain::name get_proposer() = 0;
virtual chain::name get_leader() = 0;
virtual chain::name get_next_leader() = 0;
virtual std::vector<chain::name> get_finalizers() = 0;
// virtual std::vector<chain::name> get_finalizers() = 0;
virtual std::vector<fc::crypto::blslib::bls_public_key> get_finalizer_keys() = 0;


//outbound communications; 'id' is the producer name (can be ignored if/when irrelevant to the implementer)
virtual void send_hs_proposal_msg(const chain::hs_proposal_message& msg, chain::name id) = 0;
virtual void send_hs_vote_msg(const chain::hs_vote_message& msg, chain::name id) = 0;
virtual void send_hs_new_view_msg(const chain::hs_new_view_message& msg, chain::name id) = 0;
virtual void send_hs_new_block_msg(const chain::hs_new_block_message& msg, chain::name id) = 0;
virtual void send_hs_proposal_msg(const chain::hs_proposal_message& msg, const std::string& id) = 0;
virtual void send_hs_vote_msg(const chain::hs_vote_message& msg, const std::string& id) = 0;
virtual void send_hs_new_view_msg(const chain::hs_new_view_message& msg, const std::string& id) = 0;
virtual void send_hs_new_block_msg(const chain::hs_new_block_message& msg, const std::string& id) = 0;
};

} // namespace eosio::hotstuff
11 changes: 6 additions & 5 deletions libraries/hotstuff/include/eosio/hotstuff/chain_pacemaker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ namespace eosio::hotstuff {
name get_proposer();
name get_leader() ;
name get_next_leader() ;
std::vector<name> get_finalizers();
//std::vector<name> get_finalizers();
std::vector<fc::crypto::blslib::bls_public_key> get_finalizer_keys();

block_id_type get_current_block_id();

uint32_t get_quorum_threshold();

void send_hs_proposal_msg(const hs_proposal_message& msg, name id);
void send_hs_vote_msg(const hs_vote_message& msg, name id);
void send_hs_new_view_msg(const hs_new_view_message& msg, name id);
void send_hs_new_block_msg(const hs_new_block_message& msg, name id);
void send_hs_proposal_msg(const hs_proposal_message& msg, const std::string& id);
void send_hs_vote_msg(const hs_vote_message& msg, const std::string& id);
void send_hs_new_view_msg(const hs_new_view_message& msg, const std::string& id);
void send_hs_new_block_msg(const hs_new_block_message& msg, const std::string& id);

private:
void on_accepted_block( const block_state_ptr& blk );
Expand Down
25 changes: 8 additions & 17 deletions libraries/hotstuff/include/eosio/hotstuff/qc_chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ namespace eosio::hotstuff {

qc_chain() = delete;

qc_chain(name id, base_pacemaker* pacemaker,
qc_chain(std::string id, base_pacemaker* pacemaker,
std::set<name> my_producers,
chain::bls_key_map_t finalizer_keys,
fc::logger& logger);

uint64_t get_state_version() const { return _state_version; } // calling this w/ thread sync is optional

name get_id_i() const { return _id; } // so far, only ever relevant in a test environment (no sync)
std::string get_id_i() const { return _id; } // so far, only ever relevant in a test environment (no sync)

// Calls to the following methods should be thread-synchronized externally:

Expand All @@ -124,16 +124,16 @@ namespace eosio::hotstuff {

uint32_t positive_bits_count(const hs_bitset& finalizers);

hs_bitset update_bitset(const hs_bitset& finalizer_set, name finalizer);
hs_bitset update_bitset(const hs_bitset& finalizer_set, fc::crypto::blslib::bls_public_key finalizer_key);

digest_type get_digest_to_sign(const block_id_type& block_id, uint8_t phase_counter, const fc::sha256& final_on_qc); //get digest to sign from proposal data

void reset_qc(const fc::sha256& proposal_id); //reset current internal qc

bool evaluate_quorum(const extended_schedule& es, const hs_bitset& finalizers, const fc::crypto::blslib::bls_signature& agg_sig, const hs_proposal_message& proposal); //evaluate quorum for a proposal
bool evaluate_quorum(const hs_bitset& finalizers, const fc::crypto::blslib::bls_signature& agg_sig, const hs_proposal_message& proposal); //evaluate quorum for a proposal

// qc.quorum_met has to be updated by the caller (if it wants to) based on the return value of this method
bool is_quorum_met(const quorum_certificate& qc, const extended_schedule& schedule, const hs_proposal_message& proposal); //check if quorum has been met over a proposal
bool is_quorum_met(const quorum_certificate& qc, const hs_proposal_message& proposal); //check if quorum has been met over a proposal

hs_proposal_message new_proposal_candidate(const block_id_type& block_id, uint8_t phase_counter); //create new proposal message
hs_new_block_message new_block_candidate(const block_id_type& block_id); //create new block message
Expand All @@ -147,7 +147,7 @@ namespace eosio::hotstuff {
void process_new_view(const hs_new_view_message& msg); //handles new view
void process_new_block(const hs_new_block_message& msg); //handles new block

hs_vote_message sign_proposal(const hs_proposal_message& proposal, name finalizer); //sign proposal
hs_vote_message sign_proposal(const hs_proposal_message& proposal, const fc::crypto::blslib::bls_private_key& finalizer_priv_key, const fc::crypto::blslib::bls_public_key& finalizer_pub_key);

bool extends(const fc::sha256& descendant, const fc::sha256& ancestor); //verify that a proposal descends from another

Expand All @@ -169,15 +169,6 @@ namespace eosio::hotstuff {

void gc_proposals(uint64_t cutoff); //garbage collection of old proposals

#warning remove. bls12-381 key used for testing purposes
//todo : remove. bls12-381 key used for testing purposes
std::vector<uint8_t> _seed =
{ 0, 50, 6, 244, 24, 199, 1, 25, 52, 88, 192,
19, 18, 12, 89, 6, 220, 18, 102, 58, 209, 82,
12, 62, 89, 110, 182, 9, 44, 20, 254, 22 };

fc::crypto::blslib::bls_private_key _private_key = fc::crypto::blslib::bls_private_key(_seed);

enum msg_type {
new_view = 1,
new_block = 2,
Expand All @@ -195,11 +186,11 @@ namespace eosio::hotstuff {
quorum_certificate _high_qc;
quorum_certificate _current_qc;
uint32_t _v_height = 0;
eosio::chain::extended_schedule _schedule;
//eosio::chain::extended_schedule _schedule;
base_pacemaker* _pacemaker = nullptr;
std::set<name> _my_producers;
chain::bls_key_map_t _my_finalizer_keys;
name _id;
std::string _id;

mutable std::atomic<uint64_t> _state_version = 1;

Expand Down
27 changes: 13 additions & 14 deletions libraries/hotstuff/include/eosio/hotstuff/test_pacemaker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ namespace eosio { namespace hotstuff {

//class-specific functions

bool is_qc_chain_active(const name & qcc_name) { return _qcc_deactivated.find(qcc_name) == _qcc_deactivated.end(); }
bool is_qc_chain_active(const name& qcc_name) { return _qcc_deactivated.find(qcc_name) == _qcc_deactivated.end(); }

using hotstuff_message = std::pair<name, std::variant<hs_proposal_message, hs_vote_message, hs_new_block_message, hs_new_view_message>>;
using hotstuff_message = std::pair<std::string, std::variant<hs_proposal_message, hs_vote_message, hs_new_block_message, hs_new_view_message>>;

void set_proposer(name proposer);

void set_leader(name leader);

void set_next_leader(name next_leader);

void set_finalizers(std::vector<name> finalizers);
void set_finalizer_keys(std::vector<fc::crypto::blslib::bls_public_key> finalizers);

void set_current_block_id(block_id_type id);

Expand All @@ -41,26 +41,26 @@ namespace eosio { namespace hotstuff {

void beat();

void on_hs_vote_msg(const hs_vote_message & msg, name id); //confirmation msg event handler
void on_hs_proposal_msg(const hs_proposal_message & msg, name id); //consensus msg event handler
void on_hs_new_view_msg(const hs_new_view_message & msg, name id); //new view msg event handler
void on_hs_new_block_msg(const hs_new_block_message & msg, name id); //new block msg event handler
void on_hs_vote_msg(const hs_vote_message & msg, const std::string& id); //confirmation msg event handler
void on_hs_proposal_msg(const hs_proposal_message & msg, const std::string& id); //consensus msg event handler
void on_hs_new_view_msg(const hs_new_view_message & msg, const std::string& id); //new view msg event handler
void on_hs_new_block_msg(const hs_new_block_message & msg, const std::string& id); //new block msg event handler

//base_pacemaker interface functions

name get_proposer();
name get_leader();
name get_next_leader();
std::vector<name> get_finalizers();
std::vector<fc::crypto::blslib::bls_public_key> get_finalizer_keys();

block_id_type get_current_block_id();

uint32_t get_quorum_threshold();

void send_hs_proposal_msg(const hs_proposal_message & msg, name id);
void send_hs_vote_msg(const hs_vote_message & msg, name id);
void send_hs_new_block_msg(const hs_new_block_message & msg, name id);
void send_hs_new_view_msg(const hs_new_view_message & msg, name id);
void send_hs_proposal_msg(const hs_proposal_message & msg, const std::string& id);
void send_hs_vote_msg(const hs_vote_message & msg, const std::string& id);
void send_hs_new_block_msg(const hs_new_block_message & msg, const std::string& id);
void send_hs_new_view_msg(const hs_new_view_message & msg, const std::string& id);

std::vector<hotstuff_message> _pending_message_queue;

Expand All @@ -78,11 +78,10 @@ namespace eosio { namespace hotstuff {
name _leader;
name _next_leader;

std::vector<name> _finalizers;
std::vector<fc::crypto::blslib::bls_public_key> _finalizer_keys;

block_id_type _current_block_id;

std::vector<name> _unique_replicas;
#warning calculate from schedule
uint32_t _quorum_threshold = 15; //todo : calculate from schedule
};
Expand Down
Loading

0 comments on commit c8d9d38

Please sign in to comment.