From 2694ebc430e685fb0b42aac4ec619b0463be6be9 Mon Sep 17 00:00:00 2001 From: muxator Date: Fri, 29 Sep 2023 15:12:03 +0200 Subject: [PATCH] format: selectively enable InsertBraces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .clang-format | 1 + src/blockchain/grind.cpp | 3 +- src/fbft/messages/Block.cpp | 12 ++++-- src/fbft/messages/Commit.cpp | 9 +++-- src/fbft/messages/Message.cpp | 12 ++++-- src/fbft/messages/NewView.cpp | 27 ++++++++----- src/fbft/messages/PrePrepare.cpp | 15 ++++--- src/fbft/messages/Prepare.cpp | 12 ++++-- src/fbft/messages/Request.cpp | 6 ++- src/fbft/messages/RoastPreSignature.cpp | 9 +++-- src/fbft/messages/RoastSignatureShare.cpp | 9 +++-- src/fbft/messages/ViewChange.cpp | 39 ++++++++++++------- src/fbft/state/ReplicaState.cpp | 8 ++-- src/test/stubs/DummyNetwork.cpp | 6 ++- .../test_blockchain_frost_wallet_bitcoin.cpp | 12 ++++-- src/test/test_fbft_replica2.cpp | 3 +- 16 files changed, 121 insertions(+), 62 deletions(-) diff --git a/.clang-format b/.clang-format index 6ec32b3..cd602b2 100644 --- a/.clang-format +++ b/.clang-format @@ -10,6 +10,7 @@ BasedOnStyle: LLVM # # The following are the project-specific rules 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 diff --git a/src/blockchain/grind.cpp b/src/blockchain/grind.cpp index 6e01517..498e347 100644 --- a/src/blockchain/grind.cpp +++ b/src/blockchain/grind.cpp @@ -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; diff --git a/src/fbft/messages/Block.cpp b/src/fbft/messages/Block.cpp index 970107e..c094826 100644 --- a/src/fbft/messages/Block.cpp +++ b/src/fbft/messages/Block.cpp @@ -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(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); } diff --git a/src/fbft/messages/Commit.cpp b/src/fbft/messages/Commit.cpp index 611180d..b76b1fa 100644 --- a/src/fbft/messages/Commit.cpp +++ b/src/fbft/messages/Commit.cpp @@ -61,14 +61,17 @@ std::vector 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(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); } diff --git a/src/fbft/messages/Message.cpp b/src/fbft/messages/Message.cpp index 52fad1d..4a95fa0 100644 --- a/src/fbft/messages/Message.cpp +++ b/src/fbft/messages/Message.cpp @@ -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; } diff --git a/src/fbft/messages/NewView.cpp b/src/fbft/messages/NewView.cpp index f821e17..31e62af 100644 --- a/src/fbft/messages/NewView.cpp +++ b/src/fbft/messages/NewView.cpp @@ -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); @@ -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); @@ -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(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); } diff --git a/src/fbft/messages/PrePrepare.cpp b/src/fbft/messages/PrePrepare.cpp index 3e612c0..e4fcda6 100644 --- a/src/fbft/messages/PrePrepare.cpp +++ b/src/fbft/messages/PrePrepare.cpp @@ -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(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); } diff --git a/src/fbft/messages/Prepare.cpp b/src/fbft/messages/Prepare.cpp index 319d387..7e335df 100644 --- a/src/fbft/messages/Prepare.cpp +++ b/src/fbft/messages/Prepare.cpp @@ -44,16 +44,20 @@ std::vector> 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(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); } diff --git a/src/fbft/messages/Request.cpp b/src/fbft/messages/Request.cpp index ef249eb..e02b27e 100644 --- a/src/fbft/messages/Request.cpp +++ b/src/fbft/messages/Request.cpp @@ -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(other); - if (m_timestamp != typed_other.m_timestamp) + if (m_timestamp != typed_other.m_timestamp) { return false; + } return Message::equals(other); } diff --git a/src/fbft/messages/RoastPreSignature.cpp b/src/fbft/messages/RoastPreSignature.cpp index 2fa0a6e..26855c7 100644 --- a/src/fbft/messages/RoastPreSignature.cpp +++ b/src/fbft/messages/RoastPreSignature.cpp @@ -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(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); } diff --git a/src/fbft/messages/RoastSignatureShare.cpp b/src/fbft/messages/RoastSignatureShare.cpp index 8bc8552..725bf53 100644 --- a/src/fbft/messages/RoastSignatureShare.cpp +++ b/src/fbft/messages/RoastSignatureShare.cpp @@ -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(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); } diff --git a/src/fbft/messages/ViewChange.cpp b/src/fbft/messages/ViewChange.cpp index ac3daff..ebcf969 100644 --- a/src/fbft/messages/ViewChange.cpp +++ b/src/fbft/messages/ViewChange.cpp @@ -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 [] @@ -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); @@ -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(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); } diff --git a/src/fbft/state/ReplicaState.cpp b/src/fbft/state/ReplicaState.cpp index 4a7f984..05a3654 100644 --- a/src/fbft/state/ReplicaState.cpp +++ b/src/fbft/state/ReplicaState.cpp @@ -336,9 +336,9 @@ 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(); } } @@ -346,9 +346,9 @@ double ReplicaState::latest_request_time() const { 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(); } } diff --git a/src/test/stubs/DummyNetwork.cpp b/src/test/stubs/DummyNetwork.cpp index 14f512c..b072e94 100644 --- a/src/test/stubs/DummyNetwork.cpp +++ b/src/test/stubs/DummyNetwork.cpp @@ -21,8 +21,9 @@ NetworkStub::NetworkStub() : active(true) {} DummyNetwork::DummyNetwork(const itcoin::FbftConfig& conf) : NetworkTransport(conf) {} void DummyNetwork::BroadcastMessage(std::unique_ptr p_msg) { - if (!active) + if (!active) { return; + } BOOST_LOG_TRIVIAL(debug) << str(boost::format("R%1% Transport, broadcasting %2% to other replicas.") % p_msg->sender_id() % p_msg->identify()); @@ -30,8 +31,9 @@ void DummyNetwork::BroadcastMessage(std::unique_ptr p_msg) { } void DummyNetwork::SimulateReceiveMessages() { - if (!active) + if (!active) { return; + } for (auto& p_msg : m_buffer) { for (shared_ptr p_listener : listeners) { diff --git a/src/test/test_blockchain_frost_wallet_bitcoin.cpp b/src/test/test_blockchain_frost_wallet_bitcoin.cpp index 8682e4d..dabd1be 100644 --- a/src/test/test_blockchain_frost_wallet_bitcoin.cpp +++ b/src/test/test_blockchain_frost_wallet_bitcoin.cpp @@ -98,8 +98,9 @@ BOOST_FIXTURE_TEST_CASE(test_blockchain_frost_wallet_bitcoin_00, Replica4Fixture // Concatenate presignatures as done in prolog for (int i = 0; i < CLUSTER_SIZE; i++) { - if (i > 0) + if (i > 0) { pre_signatures.append("+"); + } pre_signatures.append(frost_wallets[i]->GetPreSignatureShare()); } // GetSignatureShare @@ -122,8 +123,9 @@ BOOST_FIXTURE_TEST_CASE(test_blockchain_frost_wallet_bitcoin_00, Replica4Fixture // Concatenate PreSignatures as done in prolog for (int i = 0; i < CLUSTER_SIZE; i++) { - if (i > 0) + if (i > 0) { pre_signatures.append("+"); + } pre_signatures.append(frost_wallets[i]->GetPreSignatureShare()); } // GetSignatureShare @@ -158,8 +160,9 @@ BOOST_FIXTURE_TEST_CASE(test_blockchain_frost_wallet_bitcoin_00, Replica4Fixture // Concatenate PreSignatures as done in prolog for (int i = 0; i < CLUSTER_SIZE; i++) { auto presignature = frost_wallets[i]->GetPreSignatureShare(); - if (i == 1) + if (i == 1) { pre_signatures.append(presignature); + } if (i == 2) { pre_signatures.append("+"); pre_signatures.append(presignature); @@ -235,8 +238,9 @@ BOOST_FIXTURE_TEST_CASE(test_blockchain_frost_wallet_bitcoin_00, Replica4Fixture // Concatenate PreSignatures as done in prolog for (int i = 0; i < CLUSTER_SIZE; i++) { auto presignature = frost_wallets[i]->GetPreSignatureShare(); - if (i == 1) + if (i == 1) { pre_signatures.append(presignature); + } } // GetSignatureShare: only 1 node signs std::vector signature_shares; diff --git a/src/test/test_fbft_replica2.cpp b/src/test/test_fbft_replica2.cpp index 2600f08..0430514 100644 --- a/src/test/test_fbft_replica2.cpp +++ b/src/test/test_fbft_replica2.cpp @@ -62,8 +62,9 @@ BOOST_FIXTURE_TEST_CASE(test_fbft_replica2_00, Replica2Fixture) { } else { int new_dead_replica = (std::rand() % CLUSTER_SIZE); if (new_dead_replica != dead_replica) { - if (dead_replica != -1) + if (dead_replica != -1) { wake(dead_replica); + } kill(new_dead_replica); dead_replica = new_dead_replica; dead_replica_time = test_time;