Skip to content

Commit

Permalink
GH-1545 Remove NULL_BLOCK_ID and NULL_PROPOSAL_ID and use empty() ins…
Browse files Browse the repository at this point in the history
…tead
  • Loading branch information
heifner committed Aug 26, 2023
1 parent dd9802a commit 33ac700
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 60 deletions.
29 changes: 13 additions & 16 deletions libraries/chain/include/eosio/chain/hotstuff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

namespace eosio::chain {

const block_id_type NULL_BLOCK_ID = block_id_type("00");
const fc::sha256 NULL_PROPOSAL_ID = fc::sha256("00");

inline uint64_t compute_height(uint32_t block_height, uint32_t phase_counter) {
return (uint64_t{block_height} << 32) | phase_counter;
}
Expand All @@ -23,22 +20,22 @@ namespace eosio::chain {
};

struct quorum_certificate_message {
fc::sha256 proposal_id = NULL_PROPOSAL_ID;
fc::sha256 proposal_id;
std::string active_finalizers; //bitset encoding, following canonical order
fc::crypto::blslib::bls_signature active_agg_sig;
};

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

struct hs_proposal_message {
fc::sha256 proposal_id = NULL_PROPOSAL_ID; //vote on proposal
block_id_type block_id = NULL_BLOCK_ID;
fc::sha256 parent_id = NULL_PROPOSAL_ID; //new proposal
fc::sha256 final_on_qc = NULL_PROPOSAL_ID;
fc::sha256 proposal_id; //vote on proposal
block_id_type block_id;
fc::sha256 parent_id; //new proposal
fc::sha256 final_on_qc;
quorum_certificate_message justify; //justification
uint8_t phase_counter = 0;

Expand All @@ -47,7 +44,7 @@ namespace eosio::chain {
};

struct hs_new_block_message {
block_id_type block_id = NULL_BLOCK_ID; //new proposal
block_id_type block_id; //new proposal
quorum_certificate_message justify; //justification
};

Expand All @@ -57,12 +54,12 @@ namespace eosio::chain {

struct finalizer_state {
bool chained_mode = false;
fc::sha256 b_leaf = NULL_PROPOSAL_ID;
fc::sha256 b_lock = NULL_PROPOSAL_ID;
fc::sha256 b_exec = NULL_PROPOSAL_ID;
fc::sha256 b_finality_violation = NULL_PROPOSAL_ID;
block_id_type block_exec = NULL_BLOCK_ID;
block_id_type pending_proposal_block = NULL_BLOCK_ID;
fc::sha256 b_leaf;
fc::sha256 b_lock;
fc::sha256 b_exec;
fc::sha256 b_finality_violation;
block_id_type block_exec;
block_id_type pending_proposal_block;
uint32_t v_height = 0;
eosio::chain::quorum_certificate_message high_qc;
eosio::chain::quorum_certificate_message current_qc;
Expand Down
14 changes: 7 additions & 7 deletions libraries/hotstuff/include/eosio/hotstuff/qc_chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace eosio::hotstuff {

private:
friend struct fc::reflector<quorum_certificate>;
fc::sha256 proposal_id = NULL_PROPOSAL_ID;
fc::sha256 proposal_id;
boost::dynamic_bitset<> active_finalizers; //bitset encoding, following canonical order
fc::crypto::blslib::bls_signature active_agg_sig;
bool quorum_met = false; // not serialized across network
Expand Down Expand Up @@ -179,12 +179,12 @@ namespace eosio::hotstuff {
};

bool _chained_mode = false;
block_id_type _block_exec = NULL_BLOCK_ID;
block_id_type _pending_proposal_block = NULL_BLOCK_ID;
fc::sha256 _b_leaf = NULL_PROPOSAL_ID;
fc::sha256 _b_lock = NULL_PROPOSAL_ID;
fc::sha256 _b_exec = NULL_PROPOSAL_ID;
fc::sha256 _b_finality_violation = NULL_PROPOSAL_ID;
block_id_type _block_exec;
block_id_type _pending_proposal_block;
fc::sha256 _b_leaf;
fc::sha256 _b_lock;
fc::sha256 _b_exec;
fc::sha256 _b_finality_violation;
quorum_certificate _high_qc;
quorum_certificate _current_qc;
uint32_t _v_height = 0;
Expand Down
52 changes: 25 additions & 27 deletions libraries/hotstuff/qc_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ namespace eosio { namespace hotstuff {
b_new.parent_id = _b_leaf;
b_new.phase_counter = phase_counter;
b_new.justify = _high_qc.to_msg(); //or null if no _high_qc upon activation or chain launch
if (b_new.justify.proposal_id != NULL_PROPOSAL_ID){
if (!b_new.justify.proposal_id.empty()) {
std::vector<hs_proposal_message> current_qc_chain = get_qc_chain(b_new.justify.proposal_id);
size_t chain_length = std::distance(current_qc_chain.begin(), current_qc_chain.end());
if (chain_length>=2){
Expand Down Expand Up @@ -278,12 +278,12 @@ namespace eosio { namespace hotstuff {

qc_chain::qc_chain(name id, base_pacemaker* pacemaker, std::set<name> my_producers, fc::logger& logger)
: _pacemaker(pacemaker),
_id(id),
_my_producers(std::move(my_producers)),
_id(id),
_logger(logger)
{
_high_qc.reset(NULL_PROPOSAL_ID, 21); // TODO: use active schedule size
_current_qc.reset(NULL_PROPOSAL_ID, 21); // TODO: use active schedule size
_high_qc.reset({}, 21); // TODO: use active schedule size
_current_qc.reset({}, 21); // TODO: use active schedule size

fc_dlog(_logger, " === ${id} qc chain initialized ${my_producers}", ("my_producers", my_producers)("id", _id));
}
Expand Down Expand Up @@ -331,7 +331,7 @@ namespace eosio { namespace hotstuff {

//auto start = fc::time_point::now();

if (proposal.justify.proposal_id != NULL_PROPOSAL_ID){
if (!proposal.justify.proposal_id.empty()) {

const hs_proposal_message *jp = get_proposal( proposal.justify.proposal_id );
if (jp == nullptr) {
Expand Down Expand Up @@ -525,14 +525,14 @@ namespace eosio { namespace hotstuff {
fc_tlog(_logger, " === ${id} phase increment on proposal ${proposal_id}", ("proposal_id", vote.proposal_id)("id", _id));
hs_proposal_message proposal_candidate;

if (_pending_proposal_block == NULL_BLOCK_ID)
if (_pending_proposal_block.empty())
proposal_candidate = new_proposal_candidate( p->block_id, p->phase_counter + 1 );
else
proposal_candidate = new_proposal_candidate( _pending_proposal_block, 0 );

reset_qc(proposal_candidate.proposal_id);
fc_tlog(_logger, " === ${id} setting _pending_proposal_block to null (process_vote)", ("id", _id));
_pending_proposal_block = NULL_BLOCK_ID;
_pending_proposal_block = block_id_type{};
_b_leaf = proposal_candidate.proposal_id;

send_hs_proposal_msg(proposal_candidate);
Expand Down Expand Up @@ -578,7 +578,7 @@ namespace eosio { namespace hotstuff {

auto increment_version = fc::make_scoped_exit([this]() { ++_state_version; });

if (_current_qc.get_proposal_id() != NULL_PROPOSAL_ID && _current_qc.is_quorum_met() == false) {
if (!_current_qc.get_proposal_id().empty() && !_current_qc.is_quorum_met()) {

fc_tlog(_logger, " === ${id} pending proposal found ${proposal_id} : quorum met ${quorum_met}",
("id", _id)
Expand All @@ -600,7 +600,7 @@ namespace eosio { namespace hotstuff {

fc_tlog(_logger, " === ${id} setting _pending_proposal_block to null (process_new_block)", ("id", _id));

_pending_proposal_block = NULL_BLOCK_ID;
_pending_proposal_block = block_id_type{};
_b_leaf = proposal_candidate.proposal_id;

send_hs_proposal_msg(proposal_candidate);
Expand Down Expand Up @@ -709,8 +709,7 @@ namespace eosio { namespace hotstuff {

// if new high QC is higher than current, update to new

if (_high_qc.get_proposal_id() == NULL_PROPOSAL_ID){

if (_high_qc.get_proposal_id().empty()) {
_high_qc = high_qc;
_b_leaf = _high_qc.get_proposal_id();

Expand Down Expand Up @@ -757,11 +756,11 @@ namespace eosio { namespace hotstuff {

//leader changed, we send our new_view message

reset_qc(NULL_PROPOSAL_ID);
reset_qc({});

fc_tlog(_logger, " === ${id} setting _pending_proposal_block to null (leader_rotation_check)", ("id", _id));

_pending_proposal_block = NULL_BLOCK_ID;
_pending_proposal_block = block_id_type{};

hs_new_view_message new_view;

Expand All @@ -783,9 +782,9 @@ namespace eosio { namespace hotstuff {

fc::sha256 upcoming_commit;

if (proposal.justify.proposal_id == NULL_PROPOSAL_ID && _b_lock == NULL_PROPOSAL_ID)
if (proposal.justify.proposal_id.empty() && _b_lock.empty()) {
final_on_qc_check = true; //if chain just launched or feature just activated
else {
} else {

std::vector<hs_proposal_message> current_qc_chain = get_qc_chain(proposal.justify.proposal_id);

Expand Down Expand Up @@ -822,15 +821,15 @@ namespace eosio { namespace hotstuff {
monotony_check = true;
}

if (_b_lock != NULL_PROPOSAL_ID){
if (!_b_lock.empty()) {

//Safety check : check if this proposal extends the chain I'm locked on
if (extends(proposal.proposal_id, _b_lock)) {
safety_check = true;
}

//Liveness check : check if the height of this proposal's justification is higher than the height of the proposal I'm locked on. This allows restoration of liveness if a replica is locked on a stale block.
if (proposal.justify.proposal_id == NULL_PROPOSAL_ID && _b_lock == NULL_PROPOSAL_ID) {
if (proposal.justify.proposal_id.empty() && _b_lock.empty()) {
liveness_check = true; //if there is no justification on the proposal and I am not locked on anything, means the chain just launched or feature just activated
} else {
const hs_proposal_message *b_lock = get_proposal( _b_lock );
Expand Down Expand Up @@ -893,7 +892,7 @@ namespace eosio { namespace hotstuff {
void qc_chain::update(const hs_proposal_message& proposal) {
//fc_tlog(_logger, " === update internal state ===");
//if proposal has no justification, means we either just activated the feature or launched the chain, or the proposal is invalid
if (proposal.justify.proposal_id == NULL_PROPOSAL_ID){
if (proposal.justify.proposal_id.empty()) {
fc_dlog(_logger, " === ${id} proposal has no justification ${proposal_id}", ("proposal_id", proposal.proposal_id)("id", _id));
return;
}
Expand All @@ -903,7 +902,7 @@ namespace eosio { namespace hotstuff {
size_t chain_length = std::distance(current_qc_chain.begin(), current_qc_chain.end());

const hs_proposal_message *b_lock = get_proposal( _b_lock );
EOS_ASSERT( b_lock != nullptr || _b_lock == NULL_PROPOSAL_ID , chain_exception, "expected hs_proposal ${id} not found", ("id", _b_lock) );
EOS_ASSERT( b_lock != nullptr || _b_lock.empty(), chain_exception, "expected hs_proposal ${id} not found", ("id", _b_lock) );

//fc_tlog(_logger, " === update_high_qc : proposal.justify ===");
update_high_qc(quorum_certificate{proposal.justify});
Expand Down Expand Up @@ -939,8 +938,7 @@ namespace eosio { namespace hotstuff {
("b_lock_phase", b_lock->phase_counter));
}

if (_b_lock == NULL_PROPOSAL_ID || b_1.get_height() > b_lock->get_height()){

if (_b_lock.empty() || b_1.get_height() > b_lock->get_height()) {
fc_tlog(_logger, "setting _b_lock to ${proposal_id}", ("proposal_id",b_1.proposal_id ));
_b_lock = b_1.proposal_id; //commit phase on b1

Expand All @@ -965,7 +963,7 @@ namespace eosio { namespace hotstuff {
//direct parent relationship verification
if (b_2.parent_id == b_1.proposal_id && b_1.parent_id == b.proposal_id){

if (_b_exec!= NULL_PROPOSAL_ID){
if (!_b_exec.empty()) {

const hs_proposal_message *b_exec = get_proposal( _b_exec );
EOS_ASSERT( b_exec != nullptr , chain_exception, "expected hs_proposal ${id} not found", ("id", _b_exec) );
Expand Down Expand Up @@ -1056,7 +1054,7 @@ namespace eosio { namespace hotstuff {
bool exec_height_check = false;

const hs_proposal_message *last_exec_prop = get_proposal( _b_exec );
EOS_ASSERT( last_exec_prop != nullptr || _b_exec == NULL_PROPOSAL_ID, chain_exception, "expected hs_proposal ${id} not found", ("id", _b_exec) );
EOS_ASSERT( last_exec_prop != nullptr || _b_exec.empty(), chain_exception, "expected hs_proposal ${id} not found", ("id", _b_exec) );

if (last_exec_prop != nullptr) {
fc_tlog(_logger, " === _b_exec proposal #${block_num} ${proposal_id} block_id : ${block_id} phase : ${phase_counter} parent_id : ${parent_id}",
Expand All @@ -1077,13 +1075,13 @@ namespace eosio { namespace hotstuff {
("phase_counter_2", proposal.phase_counter));
}

if (_b_exec == NULL_PROPOSAL_ID)
if (_b_exec.empty()) {
exec_height_check = true;
else
} else {
exec_height_check = last_exec_prop->get_height() < proposal.get_height();
}

if (exec_height_check){

if (exec_height_check) {
const hs_proposal_message *p = get_proposal( proposal.parent_id );
if (p != nullptr) {
//fc_tlog(_logger, " === recursively committing" );
Expand Down
20 changes: 10 additions & 10 deletions plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,10 @@ class read_only : public api_base {
get_consensus_parameters_results get_consensus_parameters(const get_consensus_parameters_params&, const fc::time_point& deadline) const;

struct hs_complete_proposal_message {
fc::sha256 proposal_id = chain::NULL_PROPOSAL_ID;
chain::block_id_type block_id = chain::NULL_BLOCK_ID;
fc::sha256 parent_id = chain::NULL_PROPOSAL_ID;
fc::sha256 final_on_qc = chain::NULL_PROPOSAL_ID;
fc::sha256 proposal_id;
chain::block_id_type block_id;
fc::sha256 parent_id;
fc::sha256 final_on_qc;
chain::quorum_certificate_message justify;
uint8_t phase_counter = 0;
uint32_t block_height = 0;
Expand All @@ -859,12 +859,12 @@ class read_only : public api_base {
using get_finalizer_state_params = empty;
struct get_finalizer_state_results {
bool chained_mode = false;
fc::sha256 b_leaf = chain::NULL_PROPOSAL_ID;
fc::sha256 b_lock = chain::NULL_PROPOSAL_ID;
fc::sha256 b_exec = chain::NULL_PROPOSAL_ID;
fc::sha256 b_finality_violation = chain::NULL_PROPOSAL_ID;
chain::block_id_type block_exec = chain::NULL_BLOCK_ID;
chain::block_id_type pending_proposal_block = chain::NULL_BLOCK_ID;
fc::sha256 b_leaf;
fc::sha256 b_lock;
fc::sha256 b_exec;
fc::sha256 b_finality_violation;
chain::block_id_type block_exec;
chain::block_id_type pending_proposal_block;
uint32_t v_height = 0;
chain::quorum_certificate_message high_qc;
chain::quorum_certificate_message current_qc;
Expand Down

0 comments on commit 33ac700

Please sign in to comment.