From 3c355d7f9e7b491b9fc5af4516286ab4100238c6 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 19 Jul 2018 17:51:15 -0400 Subject: ... --- include/hotstuff/client.h | 24 --------------------- include/hotstuff/consensus.h | 50 ++++++++++++++++++++++++++++++++++++++++++-- include/hotstuff/entity.h | 12 ++++------- include/hotstuff/hotstuff.h | 11 ++++++---- 4 files changed, 59 insertions(+), 38 deletions(-) (limited to 'include/hotstuff') diff --git a/include/hotstuff/client.h b/include/hotstuff/client.h index 87e60b7..95a003d 100644 --- a/include/hotstuff/client.h +++ b/include/hotstuff/client.h @@ -14,30 +14,6 @@ enum { CHK_CMD = 0x6 }; -struct Finality: public Serializable { - ReplicaID rid; - int8_t decision; - uint256_t cmd_hash; - uint256_t blk_hash; - - public: - Finality() = default; - Finality(ReplicaID rid, int8_t decision, - uint256_t cmd_hash, uint256_t blk_hash): - rid(rid), decision(decision), - cmd_hash(cmd_hash), blk_hash(blk_hash) {} - - void serialize(DataStream &s) const override { - s << rid << decision << cmd_hash; - if (decision == 1) s << blk_hash; - } - - void unserialize(DataStream &s) override { - s >> rid >> decision >> cmd_hash; - if (decision == 1) s >> blk_hash; - } -}; - struct MsgClient: public salticidae::MsgBase<> { using MsgBase::MsgBase; void gen_reqcmd(const Command &cmd); diff --git a/include/hotstuff/consensus.h b/include/hotstuff/consensus.h index a3a1f25..475f1f2 100644 --- a/include/hotstuff/consensus.h +++ b/include/hotstuff/consensus.h @@ -14,6 +14,7 @@ namespace hotstuff { struct Proposal; struct Vote; +struct Finality; /** Abstraction for HotStuff protocol state machine (without network implementation). */ class HotStuffCore { @@ -86,7 +87,7 @@ class HotStuffCore { * the events. */ protected: /** Called by HotStuffCore upon the decision being made for cmd. */ - virtual void do_decide(const command_t &cmd) = 0; + virtual void do_decide(Finality &&fin) = 0; /** Called by HotStuffCore upon broadcasting a new proposal. * The user should send the proposal message to all replicas except for * itself. */ @@ -127,7 +128,6 @@ class HotStuffCore { /* Other useful functions */ block_t get_genesis() { return b0; } const ReplicaConfig &get_config() { return config; } - int8_t get_cmd_decision(const uint256_t &cmd_hash); ReplicaID get_id() const { return id; } const std::set get_tails() const { return tails; } operator std::string () const; @@ -249,6 +249,52 @@ struct Vote: public Serializable { } }; +struct Finality: public Serializable { + ReplicaID rid; + int8_t decision; + uint32_t cmd_idx; + uint32_t cmd_height; + uint256_t cmd_hash; + uint256_t blk_hash; + + public: + Finality() = default; + Finality(ReplicaID rid, + int8_t decision, + uint32_t cmd_idx, + uint32_t cmd_height, + uint256_t cmd_hash, + uint256_t blk_hash): + rid(rid), decision(decision), + cmd_idx(cmd_idx), cmd_height(cmd_height), + cmd_hash(cmd_hash), blk_hash(blk_hash) {} + + void serialize(DataStream &s) const override { + s << rid << decision + << cmd_idx << cmd_height + << cmd_hash; + if (decision == 1) s << blk_hash; + } + + void unserialize(DataStream &s) override { + s >> rid >> decision + >> cmd_idx >> cmd_height + >> cmd_hash; + if (decision == 1) s >> blk_hash; + } + + operator std::string () const { + DataStream s; + s << ""; + return std::move(s); + } +}; + } #endif diff --git a/include/hotstuff/entity.h b/include/hotstuff/entity.h index 28b5148..1fca18c 100644 --- a/include/hotstuff/entity.h +++ b/include/hotstuff/entity.h @@ -80,9 +80,10 @@ class Command: public Serializable { virtual ~Command() = default; virtual const uint256_t &get_hash() const = 0; virtual bool verify() const = 0; - inline int8_t get_decision() const; - block_t get_container() const { - return container; + virtual operator std::string () const { + DataStream s; + s << ""; + return std::move(s); } }; @@ -194,11 +195,6 @@ struct BlockHeightCmp { } }; -int8_t Command::get_decision() const { - block_t cptr = container; - return cptr ? cptr->get_decision() : 0; -} - class EntityStorage { std::unordered_map blk_cache; std::unordered_map cmd_cache; diff --git a/include/hotstuff/hotstuff.h b/include/hotstuff/hotstuff.h index c8d8b5d..45992f1 100644 --- a/include/hotstuff/hotstuff.h +++ b/include/hotstuff/hotstuff.h @@ -152,8 +152,13 @@ class HotStuffBase: public HotStuffCore { void do_broadcast_proposal(const Proposal &) override; void do_vote(ReplicaID, const Vote &) override; - void do_decide(const command_t &) override; - void do_forward(const uint256_t &cmd_hash, ReplicaID rid); + void do_decide(Finality &&) override; + + protected: + + /** Called to replicate the execution of a command, the application should + * implement this to make transition for the application state. */ + virtual void state_machine_execute(const Finality &) = 0; public: HotStuffBase(uint32_t blk_size, @@ -182,8 +187,6 @@ class HotStuffBase: public HotStuffCore { promise_t async_fetch_blk(const uint256_t &blk_hash, const NetAddr *replica_id, bool fetch_now = true); /** Returns a promise resolved (with block_t blk) when Block is delivered (i.e. prefix is fetched). */ promise_t async_deliver_blk(const uint256_t &blk_hash, const NetAddr &replica_id); - /** Returns a promise resolved (with command_t cmd) when Command is decided. */ - promise_t async_decide(const uint256_t &cmd_hash); }; /** HotStuff protocol (templated by cryptographic implementation). */ -- cgit v1.2.3