From ec6a1f84324faf8e7c92f32137464db57410f58a Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 27 Jul 2018 17:33:23 -0400 Subject: fix signature verification bug --- include/hotstuff/consensus.h | 2 +- include/hotstuff/crypto.h | 17 ++++++++--------- include/hotstuff/entity.h | 11 ++++++++++- include/hotstuff/hotstuff.h | 2 ++ include/hotstuff/type.h | 3 ++- 5 files changed, 23 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/include/hotstuff/consensus.h b/include/hotstuff/consensus.h index 6de6bd3..cb86bf6 100644 --- a/include/hotstuff/consensus.h +++ b/include/hotstuff/consensus.h @@ -166,7 +166,7 @@ struct Proposal: public Serializable { >> bqc_hash; Block _blk; _blk.unserialize(s, hsc); - blk = hsc->storage->add_blk(std::move(_blk)); + blk = hsc->storage->add_blk(std::move(_blk), hsc->get_config()); } operator std::string () const { diff --git a/include/hotstuff/crypto.h b/include/hotstuff/crypto.h index 32997c8..40c9140 100644 --- a/include/hotstuff/crypto.h +++ b/include/hotstuff/crypto.h @@ -340,7 +340,7 @@ class PartCertSecp256k1: public SigSecp256k1, public PartCert { class QuorumCertSecp256k1: public QuorumCert { uint256_t blk_hash; salticidae::Bits rids; - std::vector sigs; + std::unordered_map sigs; public: QuorumCertSecp256k1() = default; @@ -349,11 +349,9 @@ class QuorumCertSecp256k1: public QuorumCert { void add_part(ReplicaID rid, const PartCert &pc) override { if (pc.get_blk_hash() != blk_hash) throw std::invalid_argument("PartCert does match the block hash"); - if (!rids.get(rid)) - { - rids.set(rid); - sigs.push_back(static_cast(pc)); - } + sigs.insert(std::make_pair( + rid, static_cast(pc))); + rids.set(rid); } void compute() override {} @@ -368,13 +366,14 @@ class QuorumCertSecp256k1: public QuorumCert { void serialize(DataStream &s) const override { s << blk_hash << rids; - for (const auto &sig: sigs) s << sig; + 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; - sigs.resize(rids.size()); - for (auto &sig: sigs) s >> sig; + 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 1fca18c..ba3906b 100644 --- a/include/hotstuff/entity.h +++ b/include/hotstuff/entity.h @@ -44,10 +44,14 @@ class ReplicaConfig { std::unordered_map replica_map; public: + size_t nreplicas; size_t nmajority; + ReplicaConfig(): nreplicas(0), nmajority(0) {} + void add_replica(ReplicaID rid, const ReplicaInfo &info) { replica_map.insert(std::make_pair(rid, info)); + nreplicas++; } const ReplicaInfo &get_info(ReplicaID rid) const { @@ -209,7 +213,12 @@ class EntityStorage { return blk_cache.count(blk_hash); } - const block_t &add_blk(Block &&_blk) { + block_t add_blk(Block &&_blk, const ReplicaConfig &config) { + if (!_blk.verify(config)) + { + HOTSTUFF_LOG_WARN("block is invalid"); + return nullptr; + } block_t blk = new Block(std::move(_blk)); return blk_cache.insert(std::make_pair(blk->get_hash(), blk)).first->second; } diff --git a/include/hotstuff/hotstuff.h b/include/hotstuff/hotstuff.h index b6bebb9..4e7332f 100644 --- a/include/hotstuff/hotstuff.h +++ b/include/hotstuff/hotstuff.h @@ -223,6 +223,8 @@ class HotStuff: public HotStuffBase { protected: part_cert_bt create_part_cert(const PrivKey &priv_key, const uint256_t &blk_hash) override { + HOTSTUFF_LOG_DEBUG("create part cert with priv=%s, blk_hash=%s", + get_hex10(priv_key).c_str(), get_hex10(blk_hash).c_str()); return new PartCertType( static_cast(priv_key), blk_hash); diff --git a/include/hotstuff/type.h b/include/hotstuff/type.h index 6014dab..0897956 100644 --- a/include/hotstuff/type.h +++ b/include/hotstuff/type.h @@ -28,7 +28,8 @@ using salticidae::Event; using salticidae::EventContext; using promise::promise_t; -inline std::string get_hex10(const uint256_t &x) { +template +inline std::string get_hex10(const SerialType &x) { return get_hex(x).substr(0, 10); } -- cgit v1.2.3-70-g09d2