cfxcore/pos/protocol/message/
mod.rs

1// Copyright 2019-2020 Conflux Foundation. All rights reserved.
2// TreeGraph is free software and distributed under Apache License 2.0.
3// See https://www.apache.org/licenses/LICENSE-2.0
4
5pub mod block_retrieval;
6pub mod block_retrieval_response;
7pub mod consensus_msg;
8pub mod epoch_change;
9pub mod epoch_retrieval;
10pub mod mempool_sync_msg;
11pub mod proposal;
12pub mod sync_info;
13pub mod vote;
14
15use super::HSB_PROTOCOL_VERSION;
16
17use crate::{
18    message::{
19        GetMaybeRequestId, Message, MessageProtocolVersionBound, MsgId,
20        RequestId, SetRequestId,
21    },
22    pos::{consensus::network::ConsensusMsg, mempool::network::MempoolSyncMsg},
23};
24
25use block_retrieval::BlockRetrievalRpcRequest;
26use block_retrieval_response::BlockRetrievalRpcResponse;
27use consensus_types::{
28    epoch_retrieval::EpochRetrievalRequest, proposal_msg::ProposalMsg,
29    sync_info::SyncInfo, vote_msg::VoteMsg,
30};
31use diem_types::epoch_change::EpochChangeProof;
32use network::service::ProtocolVersion;
33
34// FIXME: A temporary workaround by avoiding msg_id overlapping
35// with SynchronizationProtocolHandler msg_id.
36build_msgid! {
37    PROPOSAL = 0x50
38    VOTE = 0x51
39    SYNC_INFO = 0x52
40    BLOCK_RETRIEVAL = 0x53
41    BLOCK_RETRIEVAL_RESPONSE = 0x54
42    EPOCH_CHANGE = 0x55
43    EPOCH_RETRIEVAL = 0x56
44    CONSENSUS_MSG = 0x57
45    MEMPOOL_SYNC_MSG = 0x58
46    INVALID = 0xff
47}
48
49macro_rules! build_msg_impl_with_serde_serialization {
50    ($name:ident, $msg:expr, $name_str:literal) => {
51        impl GetMaybeRequestId for $name {}
52
53        impl Message for $name {
54            fn msg_id(&self) -> MsgId { $msg }
55
56            fn msg_name(&self) -> &'static str { $name_str }
57
58            fn encode(&self) -> Vec<u8> {
59                let mut encoded =
60                    bcs::to_bytes(self).expect("Failed to serialize.");
61                encoded.push(self.msg_id() as u8);
62                encoded
63            }
64        }
65    };
66}
67
68macro_rules! build_msg_impl_with_serde_serialization_generic {
69    ($name:ident, $msg:expr, $name_str:literal) => {
70        impl GetMaybeRequestId for $name {}
71
72        impl Message for $name {
73            fn msg_id(&self) -> MsgId { $msg }
74
75            fn msg_name(&self) -> &'static str { $name_str }
76
77            fn encode(&self) -> Vec<u8> {
78                let mut encoded =
79                    bcs::to_bytes(self).expect("Failed to serialize.");
80                encoded.push(self.msg_id() as u8);
81                encoded
82            }
83        }
84    };
85}
86
87macro_rules! build_msg_impl_with_request_id_and_serde_serialization {
88    ($name:ident, $msg:expr, $name_str:literal) => {
89        impl Message for $name {
90            fn msg_id(&self) -> MsgId { $msg }
91
92            fn msg_name(&self) -> &'static str { $name_str }
93
94            fn encode(&self) -> Vec<u8> {
95                let mut encoded =
96                    bcs::to_bytes(self).expect("Failed to serialize.");
97                encoded.push(self.msg_id() as u8);
98                encoded
99            }
100        }
101
102        impl_request_id_methods!($name);
103    };
104}
105
106build_msg_impl_with_serde_serialization_generic! {ProposalMsg, msgid::PROPOSAL, "ProposalMessage"}
107mark_msg_version_bound!(
108    ProposalMsg,
109    HSB_PROTOCOL_VERSION,
110    HSB_PROTOCOL_VERSION
111);
112build_msg_impl_with_serde_serialization_generic! {BlockRetrievalRpcResponse, msgid::BLOCK_RETRIEVAL_RESPONSE, "BlockRetrievalResponseMessage"}
113mark_msg_version_bound!(
114    BlockRetrievalRpcResponse,
115    HSB_PROTOCOL_VERSION,
116    HSB_PROTOCOL_VERSION
117);
118build_msg_impl_with_serde_serialization! {VoteMsg, msgid::VOTE, "VoteMessage"}
119mark_msg_version_bound!(VoteMsg, HSB_PROTOCOL_VERSION, HSB_PROTOCOL_VERSION);
120build_msg_impl_with_serde_serialization! {SyncInfo, msgid::SYNC_INFO, "SyncInfoMessage"}
121mark_msg_version_bound!(SyncInfo, HSB_PROTOCOL_VERSION, HSB_PROTOCOL_VERSION);
122build_msg_impl_with_serde_serialization! {EpochChangeProof, msgid::EPOCH_CHANGE, "EpochChangeMessage"}
123mark_msg_version_bound!(
124    EpochChangeProof,
125    HSB_PROTOCOL_VERSION,
126    HSB_PROTOCOL_VERSION
127);
128build_msg_impl_with_serde_serialization! {ConsensusMsg, msgid::CONSENSUS_MSG, "ConsensusMsg"}
129mark_msg_version_bound!(
130    ConsensusMsg,
131    HSB_PROTOCOL_VERSION,
132    HSB_PROTOCOL_VERSION
133);
134build_msg_impl_with_serde_serialization! {EpochRetrievalRequest, msgid::EPOCH_RETRIEVAL, "EpochRetrievalMessage"}
135mark_msg_version_bound!(
136    EpochRetrievalRequest,
137    HSB_PROTOCOL_VERSION,
138    HSB_PROTOCOL_VERSION
139);
140build_msg_impl_with_request_id_and_serde_serialization! {BlockRetrievalRpcRequest, msgid::BLOCK_RETRIEVAL, "BlockRetrievalMessage"}
141mark_msg_version_bound!(
142    BlockRetrievalRpcRequest,
143    HSB_PROTOCOL_VERSION,
144    HSB_PROTOCOL_VERSION
145);
146build_msg_impl_with_serde_serialization! {MempoolSyncMsg, msgid::MEMPOOL_SYNC_MSG, "MempoolSyncMsg"}
147mark_msg_version_bound!(
148    MempoolSyncMsg,
149    HSB_PROTOCOL_VERSION,
150    HSB_PROTOCOL_VERSION
151);