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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::{collections::HashSet, sync::Arc};
use async_trait::async_trait;
use futures::{prelude::*, stream::BoxStream};
use parity_scale_codec::Encode;
use sc_network::{
multiaddr::Multiaddr, Event as NetworkEvent, IfDisconnected, NetworkService, OutboundFailure,
RequestFailure,
};
use sc_network_common::{
config::parse_addr,
protocol::ProtocolName,
service::{NetworkEventStream, NetworkNotification, NetworkPeers, NetworkRequest},
};
use polkadot_node_network_protocol::{
peer_set::{PeerSet, PeerSetProtocolNames, ProtocolVersion},
request_response::{OutgoingRequest, Recipient, ReqProtocolNames, Requests},
PeerId, UnifiedReputationChange as Rep,
};
use polkadot_primitives::v2::{AuthorityDiscoveryId, Block, Hash};
use crate::validator_discovery::AuthorityDiscovery;
const LOG_TARGET: &'static str = "parachain::network-bridge-net";
pub(crate) fn send_message<M>(
net: &mut impl Network,
mut peers: Vec<PeerId>,
peer_set: PeerSet,
version: ProtocolVersion,
protocol_names: &PeerSetProtocolNames,
message: M,
metrics: &super::Metrics,
) where
M: Encode + Clone,
{
let message = {
let encoded = message.encode();
metrics.on_notification_sent(peer_set, version, encoded.len(), peers.len());
encoded
};
let last_peer = peers.pop();
let protocol_name = protocol_names.get_name(peer_set, version);
peers.into_iter().for_each(|peer| {
net.write_notification(peer, protocol_name.clone(), message.clone());
});
if let Some(peer) = last_peer {
net.write_notification(peer, protocol_name, message);
}
}
#[async_trait]
pub trait Network: Clone + Send + 'static {
fn event_stream(&mut self) -> BoxStream<'static, NetworkEvent>;
async fn set_reserved_peers(
&mut self,
protocol: ProtocolName,
multiaddresses: HashSet<Multiaddr>,
) -> Result<(), String>;
async fn remove_from_peers_set(&mut self, protocol: ProtocolName, peers: Vec<PeerId>);
async fn start_request<AD: AuthorityDiscovery>(
&self,
authority_discovery: &mut AD,
req: Requests,
req_protocol_names: &ReqProtocolNames,
if_disconnected: IfDisconnected,
);
fn report_peer(&self, who: PeerId, cost_benefit: Rep);
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName);
fn write_notification(&self, who: PeerId, protocol: ProtocolName, message: Vec<u8>);
}
#[async_trait]
impl Network for Arc<NetworkService<Block, Hash>> {
fn event_stream(&mut self) -> BoxStream<'static, NetworkEvent> {
NetworkService::event_stream(self, "polkadot-network-bridge").boxed()
}
async fn set_reserved_peers(
&mut self,
protocol: ProtocolName,
multiaddresses: HashSet<Multiaddr>,
) -> Result<(), String> {
NetworkService::set_reserved_peers(&**self, protocol, multiaddresses)
}
async fn remove_from_peers_set(&mut self, protocol: ProtocolName, peers: Vec<PeerId>) {
NetworkService::remove_peers_from_reserved_set(&**self, protocol, peers);
}
fn report_peer(&self, who: PeerId, cost_benefit: Rep) {
NetworkService::report_peer(&**self, who, cost_benefit.into_base_rep());
}
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName) {
NetworkService::disconnect_peer(&**self, who, protocol);
}
fn write_notification(&self, who: PeerId, protocol: ProtocolName, message: Vec<u8>) {
NetworkService::write_notification(&**self, who, protocol, message);
}
async fn start_request<AD: AuthorityDiscovery>(
&self,
authority_discovery: &mut AD,
req: Requests,
req_protocol_names: &ReqProtocolNames,
if_disconnected: IfDisconnected,
) {
let (protocol, OutgoingRequest { peer, payload, pending_response }) = req.encode_request();
let peer_id = match peer {
Recipient::Peer(peer_id) => Some(peer_id),
Recipient::Authority(authority) => {
let mut found_peer_id = None;
for addr in authority_discovery
.get_addresses_by_authority_id(authority)
.await
.into_iter()
.flat_map(|list| list.into_iter())
{
let (peer_id, addr) = match parse_addr(addr) {
Ok(v) => v,
Err(_) => continue,
};
NetworkService::add_known_address(&*self, peer_id.clone(), addr);
found_peer_id = Some(peer_id);
}
found_peer_id
},
};
let peer_id = match peer_id {
None => {
gum::debug!(target: LOG_TARGET, "Discovering authority failed");
match pending_response
.send(Err(RequestFailure::Network(OutboundFailure::DialFailure)))
{
Err(_) =>
gum::debug!(target: LOG_TARGET, "Sending failed request response failed."),
Ok(_) => {},
}
return
},
Some(peer_id) => peer_id,
};
NetworkService::start_request(
&*self,
peer_id,
req_protocol_names.get_name(protocol),
payload,
pending_response,
if_disconnected,
);
}
}
pub async fn get_peer_id_by_authority_id<AD: AuthorityDiscovery>(
authority_discovery: &mut AD,
authority: AuthorityDiscoveryId,
) -> Option<PeerId> {
authority_discovery
.get_addresses_by_authority_id(authority)
.await
.into_iter()
.flat_map(|list| list.into_iter())
.find_map(|addr| parse_addr(addr).ok().map(|(p, _)| p))
}