1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use crate::counters;
use anyhow::anyhow;
use channel::message_queues::QueueStyle;
use consensus_types::{
block_retrieval::{BlockRetrievalRequest, BlockRetrievalResponse},
epoch_retrieval::EpochRetrievalRequest,
experimental::{commit_decision::CommitDecision, commit_vote::CommitVote},
proposal_msg::ProposalMsg,
sync_info::SyncInfo,
vote_msg::VoteMsg,
};
use diem_infallible::RwLock;
use diem_logger::prelude::*;
use diem_metrics::IntCounterVec;
use diem_types::{epoch_change::EpochChangeProof, PeerId};
use network::{
constants::NETWORK_CHANNEL_SIZE,
error::NetworkError,
peer_manager::{ConnectionRequestSender, PeerManagerRequestSender},
protocols::{
network::{NetworkEvents, NetworkSender, NewNetworkSender},
rpc::error::RpcError,
wire::handshake::v1::SupportedProtocols,
},
ProtocolId,
};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, sync::Arc, time::Duration};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum ConsensusMsg {
BlockRetrievalRequest(Box<BlockRetrievalRequest>),
BlockRetrievalResponse(Box<BlockRetrievalResponse>),
EpochRetrievalRequest(Box<EpochRetrievalRequest>),
ProposalMsg(Box<ProposalMsg>),
SyncInfo(Box<SyncInfo>),
EpochChangeProof(Box<EpochChangeProof>),
VoteMsg(Box<VoteMsg>),
CommitVoteMsg(Box<CommitVote>),
CommitDecisionMsg(Box<CommitDecision>),
}
pub type ConsensusNetworkEvents = NetworkEvents<ConsensusMsg>;
#[derive(Clone)]
pub struct ConsensusNetworkSender {
network_sender: NetworkSender<ConsensusMsg>,
peers_protocols: Option<Arc<RwLock<HashMap<PeerId, SupportedProtocols>>>>,
}
pub const RPC: &[ProtocolId] = &[ProtocolId::ConsensusRpc];
pub const DIRECT_SEND: &[ProtocolId] = &[
ProtocolId::ConsensusDirectSendJSON,
ProtocolId::ConsensusDirectSend,
];
pub fn network_endpoint_config() -> (
Vec<ProtocolId>,
Vec<ProtocolId>,
QueueStyle,
usize,
Option<&'static IntCounterVec>,
) {
(
RPC.to_vec(),
DIRECT_SEND.to_vec(),
QueueStyle::LIFO,
NETWORK_CHANNEL_SIZE,
Some(&counters::PENDING_CONSENSUS_NETWORK_EVENTS),
)
}
impl NewNetworkSender for ConsensusNetworkSender {
fn new(
peer_mgr_reqs_tx: PeerManagerRequestSender,
connection_reqs_tx: ConnectionRequestSender,
) -> Self {
Self {
network_sender: NetworkSender::new(peer_mgr_reqs_tx, connection_reqs_tx),
peers_protocols: None,
}
}
}
impl ConsensusNetworkSender {
pub fn send_to(
&mut self,
recipient: PeerId,
message: ConsensusMsg,
) -> Result<(), NetworkError> {
let protocol = self.preferred_protocol_for_peer(recipient, DIRECT_SEND)?;
self.network_sender.send_to(recipient, protocol, message)
}
pub fn send_to_many(
&mut self,
recipients: impl Iterator<Item = PeerId>,
message: ConsensusMsg,
) -> Result<(), NetworkError> {
let mut peers_per_protocol = HashMap::new();
for peer in recipients {
match self.preferred_protocol_for_peer(peer, DIRECT_SEND) {
Ok(protocol) => peers_per_protocol
.entry(protocol)
.or_insert_with(Vec::new)
.push(peer),
Err(e) => error!("{}", e),
}
}
for (protocol, peers) in peers_per_protocol {
self.network_sender
.send_to_many(peers.into_iter(), protocol, message.clone())?;
}
Ok(())
}
pub async fn send_rpc(
&mut self,
recipient: PeerId,
message: ConsensusMsg,
timeout: Duration,
) -> Result<ConsensusMsg, RpcError> {
let protocol = self.preferred_protocol_for_peer(recipient, RPC)?;
self.network_sender
.send_rpc(recipient, protocol, message, timeout)
.await
}
pub fn initialize(&mut self, connections: Arc<RwLock<HashMap<PeerId, SupportedProtocols>>>) {
self.peers_protocols = Some(connections);
}
fn supported_protocols(&self, peer: PeerId) -> anyhow::Result<SupportedProtocols> {
if let Some(map) = &self.peers_protocols {
map.read()
.get(&peer)
.cloned()
.ok_or_else(|| anyhow!("Peer not connected"))
} else {
Err(anyhow!("ConsensusNetworkSender not initialized"))
}
}
fn preferred_protocol_for_peer(
&self,
peer: PeerId,
local_protocols: &[ProtocolId],
) -> anyhow::Result<ProtocolId> {
let remote_protocols = self.supported_protocols(peer)?;
for protocol in local_protocols {
if remote_protocols.contains(*protocol) {
return Ok(*protocol);
}
}
Err(anyhow!("No available protocols for peer {}", peer))
}
}