Skip to content

Commit

Permalink
format: selectively enable InsertBraces
Browse files Browse the repository at this point in the history
This change has been done individually in order to manually inspect it, as
advised by https://clang.llvm.org/docs/ClangFormatStyleOptions.html#insertbraces:
    Setting this option to true could lead to incorrect code formatting due to
    clang-format’s lack of complete semantic information. As such, extra care
    should be taken to review code changes made by this option.
  • Loading branch information
muxator committed Sep 29, 2023
1 parent e81ea40 commit d186846
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 62 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Language: Cpp
BasedOnStyle: LLVM
ColumnLimit: 110
InsertBraces: true
# LineEnding: LF # requires clang-format >= 16, and ubuntu 22.04 has clang-format 15 at best
# InsertNewlineAtEOF: true # requires clang-format >= 16, and ubuntu 22.04 has clang-format 15 at best
PointerAlignment: Left
Expand Down
3 changes: 2 additions & 1 deletion src/blockchain/grind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ void grind_task(uint32_t nBits, CBlockHeader& header_orig, uint32_t offset, uint
arith_uint256 target;
bool neg, over;
target.SetCompact(nBits, &neg, &over);
if (target == 0 || neg || over)
if (target == 0 || neg || over) {
return;
}
CBlockHeader header = header_orig; // working copy
header.nNonce = offset;

Expand Down
12 changes: 8 additions & 4 deletions src/fbft/messages/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ Block::Block(uint32_t block_height, uint32_t block_time, std::string block_hash)
Block::~Block(){};

bool Block::equals(const Message& other) const {
if (typeid(*this) != typeid(other))
if (typeid(*this) != typeid(other)) {
return false;
}
auto typed_other = static_cast<const Block&>(other);

if (m_block_height != typed_other.m_block_height)
if (m_block_height != typed_other.m_block_height) {
return false;
if (m_block_time != typed_other.m_block_time)
}
if (m_block_time != typed_other.m_block_time) {
return false;
if (m_block_hash != typed_other.m_block_hash)
}
if (m_block_hash != typed_other.m_block_hash) {
return false;
}
return Message::equals(other);
}

Expand Down
9 changes: 6 additions & 3 deletions src/fbft/messages/Commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ std::vector<messages::Commit> Commit::FindByV_N(uint32_t replica_id, uint32_t v,
}

bool Commit::equals(const Message& other) const {
if (typeid(*this) != typeid(other))
if (typeid(*this) != typeid(other)) {
return false;
}
auto typed_other = static_cast<const Commit&>(other);

if (m_view != typed_other.m_view)
if (m_view != typed_other.m_view) {
return false;
if (m_seq_number != typed_other.m_seq_number)
}
if (m_seq_number != typed_other.m_seq_number) {
return false;
}
return Message::equals(other);
}

Expand Down
12 changes: 8 additions & 4 deletions src/fbft/messages/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ const std::string Message::digest() const {
}

bool Message::equals(const Message& other) const {
if (typeid(*this) != typeid(other))
if (typeid(*this) != typeid(other)) {
return false;
if (m_sender_role != other.m_sender_role)
}
if (m_sender_role != other.m_sender_role) {
return false;
if (m_sender_id != other.m_sender_id)
}
if (m_sender_id != other.m_sender_id) {
return false;
if (m_signature != other.m_signature)
}
if (m_signature != other.m_signature) {
return false;
}
return true;
}

Expand Down
27 changes: 18 additions & 9 deletions src/fbft/messages/NewView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ new_view_nu_t NewView::nu_from_plterm(PlTerm Nu) {
PlTail Nu_tail{Nu};
PlTerm Nu_elem;
while (Nu_tail.next(Nu_elem)) {
if (string("[|]").compare(Nu_elem.name()) != 0 || Nu_elem.arity() != 2)
if (string("[|]").compare(Nu_elem.name()) != 0 || Nu_elem.arity() != 2) {
throw std::runtime_error("NEW_VIEW Nu element term must have arity = 2");
}
uint32_t nu_n = (long)Nu_elem[1];
PlTerm Nu_elem_2 = Nu_elem[2];
if (string("[|]").compare(Nu_elem_2.name()) != 0 || Nu_elem_2.arity() != 2)
if (string("[|]").compare(Nu_elem_2.name()) != 0 || Nu_elem_2.arity() != 2) {
throw std::runtime_error("NEW_VIEW Nu_elem_2 term must have arity = 2");
}
std::string nu_digest{(const char*)Nu_elem_2[1]};
PlTerm Nu_elem_3 = Nu_elem_2[2]; // Prolog lists end with empty list, the constant []
assert(Nu_elem_3.type() == PL_NIL);
Expand All @@ -95,18 +97,21 @@ new_view_chi_t NewView::chi_from_plterm(PlTerm Chi) {
PlTail Chi_tail{Chi};
PlTerm Chi_elem;
while (Chi_tail.next(Chi_elem)) {
if (string("[|]").compare(Chi_elem.name()) != 0 || Chi_elem.arity() != 2)
if (string("[|]").compare(Chi_elem.name()) != 0 || Chi_elem.arity() != 2) {
throw std::runtime_error("SEND_NEW_VIEW Chi_elem term must have arity = 2");
}
uint32_t chi_n = (long)Chi_elem[1];
PlTerm Chi_elem_2 = Chi_elem[2];

if (string("[|]").compare(Chi_elem_2.name()) != 0 || Chi_elem_2.arity() != 2)
if (string("[|]").compare(Chi_elem_2.name()) != 0 || Chi_elem_2.arity() != 2) {
throw std::runtime_error("SEND_NEW_VIEW Chi_elem_2 term must have arity = 2");
}
std::string chi_digest{(const char*)Chi_elem_2[1]};
PlTerm Chi_elem_3 = Chi_elem_2[2];

if (string("[|]").compare(Chi_elem_3.name()) != 0 || Chi_elem_3.arity() != 2)
if (string("[|]").compare(Chi_elem_3.name()) != 0 || Chi_elem_3.arity() != 2) {
throw std::runtime_error("SEND_NEW_VIEW Chi_elem_3 term must have arity = 2");
}
std::string chi_block{(const char*)Chi_elem_3[1]};
PlTerm Chi_elem_4 = Chi_elem_3[2]; // Prolog lists end with empty list, the constant []
assert(Chi_elem_4.type() == PL_NIL);
Expand Down Expand Up @@ -196,16 +201,20 @@ bool NewView::VerifySignatures(const RoastWallet& wallet) {
}

bool NewView::equals(const Message& other) const {
if (typeid(*this) != typeid(other))
if (typeid(*this) != typeid(other)) {
return false;
}
auto typed_other = static_cast<const NewView&>(other);

if (m_view != typed_other.m_view)
if (m_view != typed_other.m_view) {
return false;
if (m_vc_messages != typed_other.m_vc_messages)
}
if (m_vc_messages != typed_other.m_vc_messages) {
return false;
if (m_ppp_messages != typed_other.m_ppp_messages)
}
if (m_ppp_messages != typed_other.m_ppp_messages) {
return false;
}
return Message::equals(other);
}

Expand Down
15 changes: 10 additions & 5 deletions src/fbft/messages/PrePrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,23 @@ messages::PrePrepare PrePrepare::FindByV_N_Req(uint32_t replica_id, uint32_t v,
}

bool PrePrepare::equals(const Message& other) const {
if (typeid(*this) != typeid(other))
if (typeid(*this) != typeid(other)) {
return false;
}
auto typed_other = static_cast<const PrePrepare&>(other);

if (m_view != typed_other.m_view)
if (m_view != typed_other.m_view) {
return false;
if (m_seq_number != typed_other.m_seq_number)
}
if (m_seq_number != typed_other.m_seq_number) {
return false;
if (m_req_digest != typed_other.m_req_digest)
}
if (m_req_digest != typed_other.m_req_digest) {
return false;
if (m_proposed_block.GetHash() != typed_other.m_proposed_block.GetHash())
}
if (m_proposed_block.GetHash() != typed_other.m_proposed_block.GetHash()) {
return false;
}
return Message::equals(other);
}

Expand Down
12 changes: 8 additions & 4 deletions src/fbft/messages/Prepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,20 @@ std::vector<std::unique_ptr<messages::Prepare>> Prepare::BuildToBeSent(uint32_t
}

bool Prepare::equals(const Message& other) const {
if (typeid(*this) != typeid(other))
if (typeid(*this) != typeid(other)) {
return false;
}
auto typed_other = static_cast<const Prepare&>(other);

if (m_view != typed_other.m_view)
if (m_view != typed_other.m_view) {
return false;
if (m_seq_number != typed_other.m_seq_number)
}
if (m_seq_number != typed_other.m_seq_number) {
return false;
if (m_req_digest != typed_other.m_req_digest)
}
if (m_req_digest != typed_other.m_req_digest) {
return false;
}
return Message::equals(other);
}

Expand Down
6 changes: 4 additions & 2 deletions src/fbft/messages/Request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ bool Request::TryFindByDigest(uint32_t replica_id, const std::string req_digest,
}

bool Request::equals(const Message& other) const {
if (typeid(*this) != typeid(other))
if (typeid(*this) != typeid(other)) {
return false;
}
auto typed_other = static_cast<const Request&>(other);

if (m_timestamp != typed_other.m_timestamp)
if (m_timestamp != typed_other.m_timestamp) {
return false;
}
return Message::equals(other);
}

Expand Down
9 changes: 6 additions & 3 deletions src/fbft/messages/RoastPreSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ const std::string RoastPreSignature::digest() const {
}

bool RoastPreSignature::equals(const Message& other) const {
if (typeid(*this) != typeid(other))
if (typeid(*this) != typeid(other)) {
return false;
}
auto typed_other = static_cast<const RoastPreSignature&>(other);

if (m_signers != typed_other.m_signers)
if (m_signers != typed_other.m_signers) {
return false;
if (m_pre_signature != typed_other.m_pre_signature)
}
if (m_pre_signature != typed_other.m_pre_signature) {
return false;
}
return Message::equals(other);
}

Expand Down
9 changes: 6 additions & 3 deletions src/fbft/messages/RoastSignatureShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ const std::string RoastSignatureShare::digest() const {
}

bool RoastSignatureShare::equals(const Message& other) const {
if (typeid(*this) != typeid(other))
if (typeid(*this) != typeid(other)) {
return false;
}
auto typed_other = static_cast<const RoastSignatureShare&>(other);

if (m_signature_share != typed_other.m_signature_share)
if (m_signature_share != typed_other.m_signature_share) {
return false;
if (m_next_pre_signature_share != typed_other.m_next_pre_signature_share)
}
if (m_next_pre_signature_share != typed_other.m_next_pre_signature_share) {
return false;
}
return Message::equals(other);
}

Expand Down
39 changes: 26 additions & 13 deletions src/fbft/messages/ViewChange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ ViewChange::ViewChange(PlTerm Sender_id, PlTerm V, PlTerm Hi, PlTerm C, PlTerm P
PlTail P_tail{Pi};
PlTerm P_elem;
while (P_tail.next(P_elem)) {
if (string("[|]").compare(P_elem.name()) != 0 || P_elem.arity() != 2)
if (string("[|]").compare(P_elem.name()) != 0 || P_elem.arity() != 2) {
throw std::runtime_error("VIEW_CHANGE message P_elem term must have arity = 2");
}
uint32_t p_n = (long)P_elem[1];
PlTerm P_elem_2 = P_elem[2];

if (string("[|]").compare(P_elem_2.name()) != 0 || P_elem_2.arity() != 2)
if (string("[|]").compare(P_elem_2.name()) != 0 || P_elem_2.arity() != 2) {
throw std::runtime_error("VIEW_CHANGE message P_elem_2 term must have arity = 2");
}
std::string p_req_digest{(char*)P_elem_2[1]};
PlTerm P_elem_3 = P_elem_2[2];

if (string("[|]").compare(P_elem_3.name()) != 0 || P_elem_3.arity() != 2)
if (string("[|]").compare(P_elem_3.name()) != 0 || P_elem_3.arity() != 2) {
throw std::runtime_error("VIEW_CHANGE message P_elem_3 term must have arity = 2");
}
uint32_t p_v = (long)P_elem_3[1];
PlTerm P_elem_4 = P_elem_3[2];
assert(P_elem_4.type() == PL_NIL); // Prolog lists end with empty list, the constant []
Expand All @@ -60,23 +63,27 @@ ViewChange::ViewChange(PlTerm Sender_id, PlTerm V, PlTerm Hi, PlTerm C, PlTerm P
PlTail Q_tail{Qi};
PlTerm Q_elem;
while (Q_tail.next(Q_elem)) {
if (string("[|]").compare(Q_elem.name()) != 0 || Q_elem.arity() != 2)
if (string("[|]").compare(Q_elem.name()) != 0 || Q_elem.arity() != 2) {
throw std::runtime_error("VIEW_CHANGE message Q_elem term must have arity = 2");
}
uint32_t q_n = (long)Q_elem[1];
PlTerm Q_elem_2 = Q_elem[2];

if (string("[|]").compare(Q_elem_2.name()) != 0 || Q_elem_2.arity() != 2)
if (string("[|]").compare(Q_elem_2.name()) != 0 || Q_elem_2.arity() != 2) {
throw std::runtime_error("VIEW_CHANGE message Q_elem_2 term must have arity = 2");
}
std::string q_req_digest{(char*)Q_elem_2[1]};
PlTerm Q_elem_3 = Q_elem_2[2];

if (string("[|]").compare(Q_elem_3.name()) != 0 || Q_elem_3.arity() != 2)
if (string("[|]").compare(Q_elem_3.name()) != 0 || Q_elem_3.arity() != 2) {
throw std::runtime_error("VIEW_CHANGE message Q_elem_3 term must have arity = 2");
}
std::string q_prep_block{(char*)Q_elem_3[1]};
PlTerm Q_elem_4 = Q_elem_3[2];

if (string("[|]").compare(Q_elem_4.name()) != 0 || Q_elem_4.arity() != 2)
if (string("[|]").compare(Q_elem_4.name()) != 0 || Q_elem_4.arity() != 2) {
throw std::runtime_error("VIEW_CHANGE message Q_elem_4 term must have arity = 2");
}
uint32_t q_v = (long)Q_elem_4[1];
PlTerm Q_elem_5 = Q_elem_4[2]; // Prolog lists end with empty list, the constant []
assert(Q_elem_5.type() == PL_NIL);
Expand Down Expand Up @@ -118,20 +125,26 @@ messages::ViewChange ViewChange::FindByDigest(uint32_t replica_id, uint32_t send
}

bool ViewChange::equals(const Message& other) const {
if (typeid(*this) != typeid(other))
if (typeid(*this) != typeid(other)) {
return false;
}
auto typed_other = static_cast<const ViewChange&>(other);

if (m_view != typed_other.m_view)
if (m_view != typed_other.m_view) {
return false;
if (m_hi != typed_other.m_hi)
}
if (m_hi != typed_other.m_hi) {
return false;
if (m_c != typed_other.m_c)
}
if (m_c != typed_other.m_c) {
return false;
if (m_pi != typed_other.m_pi)
}
if (m_pi != typed_other.m_pi) {
return false;
if (m_qi != typed_other.m_qi)
}
if (m_qi != typed_other.m_qi) {
return false;
}
return Message::equals(other);
}

Expand Down
8 changes: 4 additions & 4 deletions src/fbft/state/ReplicaState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,19 +336,19 @@ double ReplicaState::latest_request_time() const {
PlTerm Max_t;
int result =
prolog_engine_one_shot_call("get_latest_request_time", PlTermv(PlTerm{(long)m_conf.id()}, Max_t));
if (result)
if (result) {
return (double)Max_t;
else {
} else {
return m_conf.genesis_block_timestamp();
}
}

double ReplicaState::latest_reply_time() const {
PlTerm Last_rep_t;
int result = prolog_engine_one_shot_call("last_rep", PlTermv(PlTerm{(long)m_conf.id()}, Last_rep_t));
if (result)
if (result) {
return (double)Last_rep_t;
else {
} else {
return m_conf.genesis_block_timestamp();
}
}
Expand Down
Loading

0 comments on commit d186846

Please sign in to comment.