From e6d7e7d829fbfdb87899fc4dd05f5bddc344bdb6 Mon Sep 17 00:00:00 2001 From: Determinant Date: Sun, 24 Mar 2019 21:32:23 -0400 Subject: clean up --- include/hotstuff/consensus.h | 6 ++--- include/hotstuff/crypto.h | 62 ++++++++++++++++++++++---------------------- include/hotstuff/entity.h | 2 +- include/hotstuff/type.h | 1 + salticidae | 2 +- src/consensus.cpp | 49 +++++++++++++++------------------- src/crypto.cpp | 16 ++++++------ src/hotstuff.cpp | 13 ++++------ 8 files changed, 71 insertions(+), 80 deletions(-) diff --git a/include/hotstuff/consensus.h b/include/hotstuff/consensus.h index b273f97..5f781c5 100644 --- a/include/hotstuff/consensus.h +++ b/include/hotstuff/consensus.h @@ -79,7 +79,7 @@ class HotStuffCore { /** Call to initialize the protocol, should be called once before all other * functions. */ - void on_init(uint32_t nfaulty) { config.nmajority = 2 * nfaulty + 1; } + void on_init(uint32_t nfaulty); /* TODO: better name for "delivery" ? */ /** Call to inform the state machine that a block is ready to be handled. @@ -267,13 +267,13 @@ struct Vote: public Serializable { bool verify() const { assert(hsc != nullptr); return cert->verify(hsc->get_config().get_pubkey(voter)) && - cert->get_blk_hash() == blk_hash; + cert->get_obj_hash() == blk_hash; } promise_t verify(VeriPool &vpool) const { assert(hsc != nullptr); return cert->verify(hsc->get_config().get_pubkey(voter), vpool).then([this](bool result) { - return result && cert->get_blk_hash() == blk_hash; + return result && cert->get_obj_hash() == blk_hash; }); } diff --git a/include/hotstuff/crypto.h b/include/hotstuff/crypto.h index 7af9d8c..60e7dfc 100644 --- a/include/hotstuff/crypto.h +++ b/include/hotstuff/crypto.h @@ -50,7 +50,7 @@ class PartCert: public Serializable, public Cloneable { virtual ~PartCert() = default; virtual promise_t verify(const PubKey &pubkey, VeriPool &vpool) = 0; virtual bool verify(const PubKey &pubkey) = 0; - virtual const uint256_t &get_blk_hash() const = 0; + virtual const uint256_t &get_obj_hash() const = 0; virtual PartCert *clone() override = 0; }; @@ -63,7 +63,7 @@ class QuorumCert: public Serializable, public Cloneable { virtual void compute() = 0; virtual promise_t verify(const ReplicaConfig &config, VeriPool &vpool) = 0; virtual bool verify(const ReplicaConfig &config) = 0; - virtual const uint256_t &get_blk_hash() const = 0; + virtual const uint256_t &get_obj_hash() const = 0; virtual QuorumCert *clone() override = 0; }; @@ -84,23 +84,23 @@ class PrivKeyDummy: public PrivKey { }; class PartCertDummy: public PartCert { - uint256_t blk_hash; + uint256_t obj_hash; public: PartCertDummy() {} - PartCertDummy(const uint256_t &blk_hash): - blk_hash(blk_hash) {} + PartCertDummy(const uint256_t &obj_hash): + obj_hash(obj_hash) {} void serialize(DataStream &s) const override { - s << (uint32_t)0 << blk_hash; + s << (uint32_t)0 << obj_hash; } void unserialize(DataStream &s) override { uint32_t tmp; - s >> tmp >> blk_hash; + s >> tmp >> obj_hash; } PartCert *clone() override { - return new PartCertDummy(blk_hash); + return new PartCertDummy(obj_hash); } bool verify(const PubKey &) override { return true; } @@ -108,23 +108,23 @@ class PartCertDummy: public PartCert { return promise_t([](promise_t &pm){ pm.resolve(true); }); } - const uint256_t &get_blk_hash() const override { return blk_hash; } + const uint256_t &get_obj_hash() const override { return obj_hash; } }; class QuorumCertDummy: public QuorumCert { - uint256_t blk_hash; + uint256_t obj_hash; public: QuorumCertDummy() {} - QuorumCertDummy(const ReplicaConfig &, const uint256_t &blk_hash): - blk_hash(blk_hash) {} + QuorumCertDummy(const ReplicaConfig &, const uint256_t &obj_hash): + obj_hash(obj_hash) {} void serialize(DataStream &s) const override { - s << (uint32_t)1 << blk_hash; + s << (uint32_t)1 << obj_hash; } void unserialize(DataStream &s) override { uint32_t tmp; - s >> tmp >> blk_hash; + s >> tmp >> obj_hash; } QuorumCert *clone() override { @@ -138,7 +138,7 @@ class QuorumCertDummy: public QuorumCert { return promise_t([](promise_t &pm) { pm.resolve(true); }); } - const uint256_t &get_blk_hash() const override { return blk_hash; } + const uint256_t &get_obj_hash() const override { return obj_hash; } }; @@ -163,7 +163,7 @@ class Secp256k1Context { } }; -using secp256k1_context_t = RcObj; +using secp256k1_context_t = ArcObj; extern secp256k1_context_t secp256k1_default_sign_ctx; extern secp256k1_context_t secp256k1_default_verify_ctx; @@ -346,55 +346,55 @@ class Secp256k1VeriTask: public VeriTask { }; class PartCertSecp256k1: public SigSecp256k1, public PartCert { - uint256_t blk_hash; + uint256_t obj_hash; public: PartCertSecp256k1() = default; - PartCertSecp256k1(const PrivKeySecp256k1 &priv_key, const uint256_t &blk_hash): - SigSecp256k1(blk_hash, priv_key), + PartCertSecp256k1(const PrivKeySecp256k1 &priv_key, const uint256_t &obj_hash): + SigSecp256k1(obj_hash, priv_key), PartCert(), - blk_hash(blk_hash) {} + obj_hash(obj_hash) {} bool verify(const PubKey &pub_key) override { - return SigSecp256k1::verify(blk_hash, + return SigSecp256k1::verify(obj_hash, static_cast(pub_key), secp256k1_default_verify_ctx); } promise_t verify(const PubKey &pub_key, VeriPool &vpool) override { - return vpool.verify(new Secp256k1VeriTask(blk_hash, + return vpool.verify(new Secp256k1VeriTask(obj_hash, static_cast(pub_key), static_cast(*this))); } - const uint256_t &get_blk_hash() const override { return blk_hash; } + const uint256_t &get_obj_hash() const override { return obj_hash; } PartCertSecp256k1 *clone() override { return new PartCertSecp256k1(*this); } void serialize(DataStream &s) const override { - s << blk_hash; + s << obj_hash; this->SigSecp256k1::serialize(s); } void unserialize(DataStream &s) override { - s >> blk_hash; + s >> obj_hash; this->SigSecp256k1::unserialize(s); } }; class QuorumCertSecp256k1: public QuorumCert { - uint256_t blk_hash; + uint256_t obj_hash; salticidae::Bits rids; std::unordered_map sigs; public: QuorumCertSecp256k1() = default; - QuorumCertSecp256k1(const ReplicaConfig &config, const uint256_t &blk_hash); + QuorumCertSecp256k1(const ReplicaConfig &config, const uint256_t &obj_hash); void add_part(ReplicaID rid, const PartCert &pc) override { - if (pc.get_blk_hash() != blk_hash) + if (pc.get_obj_hash() != obj_hash) throw std::invalid_argument("PartCert does match the block hash"); sigs.insert(std::make_pair( rid, static_cast(pc))); @@ -406,20 +406,20 @@ class QuorumCertSecp256k1: public QuorumCert { bool verify(const ReplicaConfig &config) override; promise_t verify(const ReplicaConfig &config, VeriPool &vpool) override; - const uint256_t &get_blk_hash() const override { return blk_hash; } + const uint256_t &get_obj_hash() const override { return obj_hash; } QuorumCertSecp256k1 *clone() override { return new QuorumCertSecp256k1(*this); } void serialize(DataStream &s) const override { - s << blk_hash << rids; + s << obj_hash << rids; for (size_t i = 0; i < rids.size(); i++) if (rids.get(i)) s << sigs.at(i); } void unserialize(DataStream &s) override { - s >> blk_hash >> rids; + s >> obj_hash >> rids; for (size_t i = 0; i < rids.size(); i++) if (rids.get(i)) s >> sigs[i]; } diff --git a/include/hotstuff/entity.h b/include/hotstuff/entity.h index c112dd7..12d92cc 100644 --- a/include/hotstuff/entity.h +++ b/include/hotstuff/entity.h @@ -105,7 +105,7 @@ class Command: public Serializable { } }; -using command_t = RcObj; +using command_t = ArcObj; template inline static std::vector diff --git a/include/hotstuff/type.h b/include/hotstuff/type.h index e4c87d6..1d5ac55 100644 --- a/include/hotstuff/type.h +++ b/include/hotstuff/type.h @@ -28,6 +28,7 @@ namespace hotstuff { using salticidae::RcObj; +using salticidae::ArcObj; using salticidae::BoxObj; using salticidae::uint256_t; diff --git a/salticidae b/salticidae index 1d89070..b84b00c 160000 --- a/salticidae +++ b/salticidae @@ -1 +1 @@ -Subproject commit 1d89070e5280985ce3212c6ae1f8befb0910e32a +Subproject commit b84b00cb9d17a51b2667716229dd3ecde756b709 diff --git a/src/consensus.cpp b/src/consensus.cpp index 8b21064..8b6e977 100644 --- a/src/consensus.cpp +++ b/src/consensus.cpp @@ -70,7 +70,7 @@ bool HotStuffCore::on_deliver_blk(const block_t &blk) { if (blk->qc) { - block_t _blk = storage->find_blk(blk->qc->get_blk_hash()); + block_t _blk = storage->find_blk(blk->qc->get_obj_hash()); if (_blk == nullptr) throw std::runtime_error("block referred by qc not fetched"); blk->qc_ref = std::move(_blk); @@ -142,7 +142,6 @@ void HotStuffCore::on_propose(const std::vector &cmds, if (p != b0 && p->voted.size() >= config.nmajority) { qc = p->self_qc->clone(); - qc->compute(); qc_ref = p; } /* create the new block */ @@ -194,14 +193,12 @@ void HotStuffCore::on_receive_proposal(const Proposal &prop) { if (bnew->qc_ref) on_qc_finish(bnew->qc_ref); on_receive_proposal_(prop); - do_vote(prop.proposer, - Vote(id, - bqc->get_hash(), - bnew->get_hash(), - ((opinion && !neg_vote) ? - create_part_cert(*priv_key, bnew->get_hash()) : - nullptr), - this)); + if (opinion && !neg_vote) + do_vote(prop.proposer, + Vote(id, + bqc->get_hash(), + bnew->get_hash(), + create_part_cert(*priv_key, bnew->get_hash()), this)); } void HotStuffCore::on_receive_vote(const Vote &vote) { @@ -209,33 +206,29 @@ void HotStuffCore::on_receive_vote(const Vote &vote) { LOG_PROTO("got %s", std::string(vote).c_str()); LOG_PROTO("now state: %s", std::string(*this).c_str()); block_t blk = get_delivered_blk(vote.blk_hash); - if (vote.cert == nullptr) return; - /* otherwise the vote is positive */ - //if (!vote.verify()) - //{ - // LOG_WARN("invalid vote from %d", vote.voter); - // return; - //} + assert(vote.cert); + size_t qsize = blk->voted.size(); + if (qsize >= config.nmajority) return; if (!blk->voted.insert(vote.voter).second) { LOG_WARN("duplicate vote from %d", vote.voter); return; } - size_t qsize = blk->voted.size(); - if (qsize <= config.nmajority) + auto &qc = blk->self_qc; + if (qc == nullptr) { - auto &qc = blk->self_qc; - if (qc == nullptr) - { - LOG_WARN("vote for block not proposed by itself"); - qc = create_quorum_cert(blk->get_hash()); - } - qc->add_part(vote.voter, *vote.cert); - if (qsize == config.nmajority) - on_qc_finish(blk); + LOG_WARN("vote for block not proposed by itself"); + qc = create_quorum_cert(blk->get_hash()); + } + qc->add_part(vote.voter, *vote.cert); + if (qsize + 1 == config.nmajority) + { + qc->compute(); + on_qc_finish(blk); } } /*** end HotStuff protocol logic ***/ +void HotStuffCore::on_init(uint32_t nfaulty) { config.nmajority = 2 * nfaulty + 1; } void HotStuffCore::prune(uint32_t staleness) { block_t start; diff --git a/src/crypto.cpp b/src/crypto.cpp index c51cc02..7e839ef 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -23,8 +23,8 @@ secp256k1_context_t secp256k1_default_sign_ctx = new Secp256k1Context(true); secp256k1_context_t secp256k1_default_verify_ctx = new Secp256k1Context(false); QuorumCertSecp256k1::QuorumCertSecp256k1( - const ReplicaConfig &config, const uint256_t &blk_hash): - QuorumCert(), blk_hash(blk_hash), rids(config.nreplicas) { + const ReplicaConfig &config, const uint256_t &obj_hash): + QuorumCert(), obj_hash(obj_hash), rids(config.nreplicas) { rids.clear(); } @@ -33,9 +33,9 @@ bool QuorumCertSecp256k1::verify(const ReplicaConfig &config) { for (size_t i = 0; i < rids.size(); i++) if (rids.get(i)) { - HOTSTUFF_LOG_DEBUG("checking cert(%d), blk_hash=%s", - i, get_hex10(blk_hash).c_str()); - if (!sigs[i].verify(blk_hash, + HOTSTUFF_LOG_DEBUG("checking cert(%d), obj_hash=%s", + i, get_hex10(obj_hash).c_str()); + if (!sigs[i].verify(obj_hash, static_cast(config.get_pubkey(i)), secp256k1_default_verify_ctx)) return false; @@ -50,9 +50,9 @@ promise_t QuorumCertSecp256k1::verify(const ReplicaConfig &config, VeriPool &vpo for (size_t i = 0; i < rids.size(); i++) if (rids.get(i)) { - HOTSTUFF_LOG_DEBUG("checking cert(%d), blk_hash=%s", - i, get_hex10(blk_hash).c_str()); - vpm.push_back(vpool.verify(new Secp256k1VeriTask(blk_hash, + HOTSTUFF_LOG_DEBUG("checking cert(%d), obj_hash=%s", + i, get_hex10(obj_hash).c_str()); + vpm.push_back(vpool.verify(new Secp256k1VeriTask(obj_hash, static_cast(config.get_pubkey(i)), sigs[i]))); } diff --git a/src/hotstuff.cpp b/src/hotstuff.cpp index 957d7b4..557a81e 100644 --- a/src/hotstuff.cpp +++ b/src/hotstuff.cpp @@ -229,7 +229,7 @@ promise_t HotStuffBase::async_deliver_blk(const uint256_t &blk_hash, std::vector pms; const auto &qc = blk->get_qc(); if (qc) - pms.push_back(async_fetch_blk(qc->get_blk_hash(), &replica_id)); + pms.push_back(async_fetch_blk(qc->get_obj_hash(), &replica_id)); /* the parents should be delivered */ for (const auto &phash: blk->get_parent_hashes()) pms.push_back(async_deliver_blk(phash, replica_id)); @@ -262,16 +262,13 @@ void HotStuffBase::vote_handler(MsgVote &&msg, const Net::conn_t &conn) { RcObj v(new Vote(std::move(msg.vote))); promise::all(std::vector{ async_deliver_blk(v->bqc_hash, peer), - async_deliver_blk(v->blk_hash, peer) - }).then([this, v=std::move(v)]() { - //bool result = vote->verify(); - auto pm = v->verify(vpool); - pm.then([this, v=std::move(v)](bool result) { - if (!result) + async_deliver_blk(v->blk_hash, peer), + v->verify(vpool), + }).then([this, v=std::move(v)](const promise::values_t values) { + if (!promise::any_cast(values[2])) LOG_WARN("invalid vote from %d", v->voter); else on_receive_vote(*v); - }); }); } -- cgit v1.2.3