From 41efd33a3e165ac329f14b6e1cea935076a8b790 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 20 Jul 2018 20:09:24 -0400 Subject: improve msg & msg network interface --- include/hotstuff/client.h | 27 ++++++++++---------- include/hotstuff/consensus.h | 4 +-- include/hotstuff/hotstuff.h | 60 +++++++++++++++++++++++++++++++------------- include/hotstuff/type.h | 1 + 4 files changed, 58 insertions(+), 34 deletions(-) (limited to 'include') diff --git a/include/hotstuff/client.h b/include/hotstuff/client.h index 95a003d..92b4eec 100644 --- a/include/hotstuff/client.h +++ b/include/hotstuff/client.h @@ -8,22 +8,21 @@ namespace hotstuff { -enum { - REQ_CMD = 0x4, - RESP_CMD = 0x5, - CHK_CMD = 0x6 +struct MsgReqCmd { + static const opcode_t opcode = 0x4; + DataStream serialized; + command_t cmd; + MsgReqCmd(const Command &cmd); + MsgReqCmd(DataStream &&s): serialized(std::move(s)) {} + void postponed_parse(HotStuffCore *hsc); }; -struct MsgClient: public salticidae::MsgBase<> { - using MsgBase::MsgBase; - void gen_reqcmd(const Command &cmd); - void parse_reqcmd(command_t &cmd, HotStuffCore *hsc) const; - - void gen_respcmd(const Finality &fin); - void parse_respcmd(Finality &fin) const; - - void gen_chkcmd(const uint256_t &cmd_hash); - void parse_chkcmd(uint256_t &cmd_hash) const; +struct MsgRespCmd { + static const opcode_t opcode = 0x5; + DataStream serialized; + Finality fin; + MsgRespCmd(const Finality &fin); + MsgRespCmd(DataStream &&s); }; class CommandDummy: public Command { diff --git a/include/hotstuff/consensus.h b/include/hotstuff/consensus.h index 475f1f2..6de6bd3 100644 --- a/include/hotstuff/consensus.h +++ b/include/hotstuff/consensus.h @@ -145,7 +145,7 @@ struct Proposal: public Serializable { * a pointer to the object of the class derived from HotStuffCore */ HotStuffCore *hsc; - Proposal(HotStuffCore *hsc): blk(nullptr), hsc(hsc) {} + Proposal(): blk(nullptr), hsc(nullptr) {} Proposal(ReplicaID proposer, const uint256_t &bqc_hash, block_t &blk, @@ -192,7 +192,7 @@ struct Vote: public Serializable { /** handle of the core object to allow polymorphism */ HotStuffCore *hsc; - Vote(HotStuffCore *hsc): cert(nullptr), hsc(hsc) {} + Vote(): cert(nullptr), hsc(nullptr) {} Vote(ReplicaID voter, const uint256_t &bqc_hash, const uint256_t &blk_hash, diff --git a/include/hotstuff/hotstuff.h b/include/hotstuff/hotstuff.h index 45992f1..b6bebb9 100644 --- a/include/hotstuff/hotstuff.h +++ b/include/hotstuff/hotstuff.h @@ -31,19 +31,43 @@ enum { }; /** Network message format for HotStuff. */ -struct MsgHotStuff: public salticidae::MsgBase<> { - using MsgBase::MsgBase; - void gen_propose(const Proposal &); - void parse_propose(Proposal &) const; +struct MsgPropose { + static const opcode_t opcode = 0x0; + DataStream serialized; + Proposal proposal; + MsgPropose(const Proposal &); + /** Only move the data to serialized, do not parse immediately. */ + MsgPropose(DataStream &&s): serialized(std::move(s)) {} + /** Parse the serialized data to blks now, with `hsc->storage`. */ + void postponed_parse(HotStuffCore *hsc); +}; + +struct MsgVote { + static const opcode_t opcode = 0x1; + DataStream serialized; + Vote vote; + MsgVote(const Vote &); + MsgVote(DataStream &&s): serialized(std::move(s)) {} + void postponed_parse(HotStuffCore *hsc); +}; - void gen_vote(const Vote &); - void parse_vote(Vote &) const; +struct MsgReqBlock { + static const opcode_t opcode = 0x2; + DataStream serialized; + std::vector blk_hashes; + MsgReqBlock() = default; + MsgReqBlock(const std::vector &blk_hashes); + MsgReqBlock(DataStream &&s); +}; - void gen_qfetchblk(const std::vector &blk_hashes); - void parse_qfetchblk(std::vector &blk_hashes) const; - void gen_rfetchblk(const std::vector &blks); - void parse_rfetchblk(std::vector &blks, HotStuffCore *hsc) const; +struct MsgRespBlock { + static const opcode_t opcode = 0x3; + DataStream serialized; + std::vector blks; + MsgRespBlock(const std::vector &blks); + MsgRespBlock(DataStream &&s): serialized(std::move(s)) {} + void postponed_parse(HotStuffCore *hsc); }; using promise::promise_t; @@ -54,7 +78,7 @@ template class FetchContext: public promise_t { Event timeout; HotStuffBase *hs; - MsgHotStuff fetch_msg; + MsgReqBlock fetch_msg; const uint256_t ent_hash; std::unordered_set replica_ids; inline void timeout_cb(evutil_socket_t, short); @@ -92,7 +116,7 @@ class BlockDeliveryContext: public promise_t { class HotStuffBase: public HotStuffCore { using BlockFetchContext = FetchContext; using CmdFetchContext = FetchContext; - using conn_t = MsgNetwork::conn_t; + using conn_t = PeerNetwork::conn_t; friend BlockFetchContext; friend CmdFetchContext; @@ -110,7 +134,7 @@ class HotStuffBase: public HotStuffCore { /** whether libevent handle is owned by itself */ bool eb_loop; /** network stack */ - PeerNetwork pn; + PeerNetwork pn; #ifdef HOTSTUFF_ENABLE_BLK_PROFILE BlockProfiler blk_profiler; #endif @@ -142,13 +166,13 @@ class HotStuffBase: public HotStuffCore { void on_deliver_blk(const block_t &blk); /** deliver consensus message: */ - inline void propose_handler(const MsgHotStuff &, conn_t); + inline void propose_handler(MsgPropose &&, conn_t); /** deliver consensus message: */ - inline void vote_handler(const MsgHotStuff &, conn_t); + inline void vote_handler(MsgVote &&, conn_t); /** fetches full block data */ - inline void query_fetch_blk_handler(const MsgHotStuff &, conn_t); + inline void req_blk_handler(MsgReqBlock &&, conn_t); /** receives a block */ - inline void resp_fetch_blk_handler(const MsgHotStuff &, conn_t); + inline void resp_blk_handler(MsgRespBlock &&, conn_t); void do_broadcast_proposal(const Proposal &) override; void do_vote(ReplicaID, const Vote &) override; @@ -278,7 +302,7 @@ FetchContext::FetchContext( const uint256_t &ent_hash, HotStuffBase *hs): promise_t([](promise_t){}), hs(hs), ent_hash(ent_hash) { - fetch_msg.gen_qfetchblk(std::vector{ent_hash}); + fetch_msg = std::vector{ent_hash}; timeout = Event(hs->eb, -1, 0, std::bind(&FetchContext::timeout_cb, this, _1, _2)); diff --git a/include/hotstuff/type.h b/include/hotstuff/type.h index 670ee6c..6014dab 100644 --- a/include/hotstuff/type.h +++ b/include/hotstuff/type.h @@ -53,6 +53,7 @@ class Cloneable { }; using ReplicaID = uint16_t; +using opcode_t = uint8_t; } -- cgit v1.2.3-70-g09d2