/__w/smoldot/smoldot/repo/light-base/src/network_service.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Smoldot |
2 | | // Copyright (C) 2019-2022 Parity Technologies (UK) Ltd. |
3 | | // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 |
4 | | |
5 | | // This program is free software: you can redistribute it and/or modify |
6 | | // it under the terms of the GNU General Public License as published by |
7 | | // the Free Software Foundation, either version 3 of the License, or |
8 | | // (at your option) any later version. |
9 | | |
10 | | // This program is distributed in the hope that it will be useful, |
11 | | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | // GNU General Public License for more details. |
14 | | |
15 | | // You should have received a copy of the GNU General Public License |
16 | | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | | |
18 | | //! Background network service. |
19 | | //! |
20 | | //! The [`NetworkService`] manages background tasks dedicated to connecting to other nodes. |
21 | | //! Importantly, its design is oriented towards the particular use case of the light client. |
22 | | //! |
23 | | //! The [`NetworkService`] spawns one background task (using [`PlatformRef::spawn_task`]) for |
24 | | //! each active connection. |
25 | | //! |
26 | | //! The objective of the [`NetworkService`] in general is to try stay connected as much as |
27 | | //! possible to the nodes of the peer-to-peer network of the chain, and maintain open substreams |
28 | | //! with them in order to send out requests (e.g. block requests) and notifications (e.g. block |
29 | | //! announces). |
30 | | //! |
31 | | //! Connectivity to the network is performed in the background as an implementation detail of |
32 | | //! the service. The public API only allows emitting requests and notifications towards the |
33 | | //! already-connected nodes. |
34 | | //! |
35 | | //! After a [`NetworkService`] is created, one can add chains using [`NetworkService::add_chain`]. |
36 | | //! If all references to a [`NetworkServiceChain`] are destroyed, the chain is automatically |
37 | | //! purged. |
38 | | //! |
39 | | //! An important part of the API is the list of channel receivers of [`Event`] returned by |
40 | | //! [`NetworkServiceChain::subscribe`]. These channels inform the foreground about updates to the |
41 | | //! network connectivity. |
42 | | |
43 | | use crate::{ |
44 | | log, |
45 | | platform::{self, address_parse, PlatformRef}, |
46 | | }; |
47 | | |
48 | | use alloc::{ |
49 | | borrow::ToOwned as _, |
50 | | boxed::Box, |
51 | | collections::BTreeMap, |
52 | | format, |
53 | | string::{String, ToString as _}, |
54 | | sync::Arc, |
55 | | vec::{self, Vec}, |
56 | | }; |
57 | | use core::{cmp, mem, num::NonZeroUsize, pin::Pin, time::Duration}; |
58 | | use futures_channel::oneshot; |
59 | | use futures_lite::FutureExt as _; |
60 | | use futures_util::{future, stream, StreamExt as _}; |
61 | | use hashbrown::{HashMap, HashSet}; |
62 | | use rand::seq::IteratorRandom as _; |
63 | | use rand_chacha::rand_core::SeedableRng as _; |
64 | | use smoldot::{ |
65 | | header, |
66 | | informant::{BytesDisplay, HashDisplay}, |
67 | | libp2p::{ |
68 | | connection, |
69 | | multiaddr::{self, Multiaddr}, |
70 | | peer_id, |
71 | | }, |
72 | | network::{basic_peering_strategy, codec, service}, |
73 | | }; |
74 | | |
75 | | pub use codec::{CallProofRequestConfig, Role}; |
76 | | pub use service::{ChainId, EncodedMerkleProof, PeerId, QueueNotificationError}; |
77 | | |
78 | | mod tasks; |
79 | | |
80 | | /// Configuration for a [`NetworkService`]. |
81 | | pub struct Config<TPlat> { |
82 | | /// Access to the platform's capabilities. |
83 | | pub platform: TPlat, |
84 | | |
85 | | /// Value sent back for the agent version when receiving an identification request. |
86 | | pub identify_agent_version: String, |
87 | | |
88 | | /// Capacity to allocate for the list of chains. |
89 | | pub chains_capacity: usize, |
90 | | |
91 | | /// Maximum number of connections that the service can open simultaneously. After this value |
92 | | /// has been reached, a new connection can be opened after each |
93 | | /// [`Config::connections_open_pool_restore_delay`]. |
94 | | pub connections_open_pool_size: u32, |
95 | | |
96 | | /// Delay after which the service can open a new connection. |
97 | | /// The delay is cumulative. If no connection has been opened for example for twice this |
98 | | /// duration, then two connections can be opened at the same time, up to a maximum of |
99 | | /// [`Config::connections_open_pool_size`]. |
100 | | pub connections_open_pool_restore_delay: Duration, |
101 | | } |
102 | | |
103 | | /// See [`NetworkService::add_chain`]. |
104 | | /// |
105 | | /// Note that this configuration is intentionally missing a field containing the bootstrap |
106 | | /// nodes of the chain. Bootstrap nodes are supposed to be added afterwards by calling |
107 | | /// [`NetworkServiceChain::discover`]. |
108 | | pub struct ConfigChain { |
109 | | /// Name of the chain, for logging purposes. |
110 | | pub log_name: String, |
111 | | |
112 | | /// Number of "out slots" of this chain. We establish simultaneously gossip substreams up to |
113 | | /// this number of peers. |
114 | | pub num_out_slots: usize, |
115 | | |
116 | | /// Hash of the genesis block of the chain. Sent to other nodes in order to determine whether |
117 | | /// the chains match. |
118 | | /// |
119 | | /// > **Note**: Be aware that this *must* be the *genesis* block, not any block known to be |
120 | | /// > in the chain. |
121 | | pub genesis_block_hash: [u8; 32], |
122 | | |
123 | | /// Number and hash of the current best block. Can later be updated with |
124 | | /// [`NetworkServiceChain::set_local_best_block`]. |
125 | | pub best_block: (u64, [u8; 32]), |
126 | | |
127 | | /// Optional identifier to insert into the networking protocol names. Used to differentiate |
128 | | /// between chains with the same genesis hash. |
129 | | pub fork_id: Option<String>, |
130 | | |
131 | | /// Number of bytes of the block number in the networking protocol. |
132 | | pub block_number_bytes: usize, |
133 | | |
134 | | /// Must be `Some` if and only if the chain uses the GrandPa networking protocol. Contains the |
135 | | /// number of the finalized block at the time of the initialization. |
136 | | pub grandpa_protocol_finalized_block_height: Option<u64>, |
137 | | } |
138 | | |
139 | | pub struct NetworkService<TPlat: PlatformRef> { |
140 | | /// Channel connected to the background service. |
141 | | messages_tx: async_channel::Sender<ToBackground<TPlat>>, |
142 | | |
143 | | /// See [`Config::platform`]. |
144 | | platform: TPlat, |
145 | | } |
146 | | |
147 | | impl<TPlat: PlatformRef> NetworkService<TPlat> { |
148 | | /// Initializes the network service with the given configuration. |
149 | 0 | pub fn new(config: Config<TPlat>) -> Arc<Self> { |
150 | 0 | let (main_messages_tx, main_messages_rx) = async_channel::bounded(4); |
151 | 0 |
|
152 | 0 | let network = service::ChainNetwork::new(service::Config { |
153 | 0 | chains_capacity: config.chains_capacity, |
154 | 0 | connections_capacity: 32, |
155 | 0 | handshake_timeout: Duration::from_secs(8), |
156 | 0 | randomness_seed: { |
157 | 0 | let mut seed = [0; 32]; |
158 | 0 | config.platform.fill_random_bytes(&mut seed); |
159 | 0 | seed |
160 | 0 | }, |
161 | 0 | }); |
162 | 0 |
|
163 | 0 | // Spawn main task that processes the network service. |
164 | 0 | let (tasks_messages_tx, tasks_messages_rx) = async_channel::bounded(32); |
165 | 0 | let task = Box::pin(background_task(BackgroundTask { |
166 | 0 | randomness: rand_chacha::ChaCha20Rng::from_seed({ |
167 | 0 | let mut seed = [0; 32]; |
168 | 0 | config.platform.fill_random_bytes(&mut seed); |
169 | 0 | seed |
170 | 0 | }), |
171 | 0 | identify_agent_version: config.identify_agent_version, |
172 | 0 | tasks_messages_tx, |
173 | 0 | tasks_messages_rx: Box::pin(tasks_messages_rx), |
174 | 0 | peering_strategy: basic_peering_strategy::BasicPeeringStrategy::new( |
175 | 0 | basic_peering_strategy::Config { |
176 | 0 | randomness_seed: { |
177 | 0 | let mut seed = [0; 32]; |
178 | 0 | config.platform.fill_random_bytes(&mut seed); |
179 | 0 | seed |
180 | 0 | }, |
181 | 0 | peers_capacity: 50, // TODO: ? |
182 | 0 | chains_capacity: config.chains_capacity, |
183 | 0 | }, |
184 | 0 | ), |
185 | 0 | network, |
186 | 0 | connections_open_pool_size: config.connections_open_pool_size, |
187 | 0 | connections_open_pool_restore_delay: config.connections_open_pool_restore_delay, |
188 | 0 | num_recent_connection_opening: 0, |
189 | 0 | next_recent_connection_restore: None, |
190 | 0 | platform: config.platform.clone(), |
191 | 0 | open_gossip_links: BTreeMap::new(), |
192 | 0 | event_pending_send: None, |
193 | 0 | event_senders: either::Left(Vec::new()), |
194 | 0 | pending_new_subscriptions: Vec::new(), |
195 | 0 | important_nodes: HashSet::with_capacity_and_hasher(16, Default::default()), |
196 | 0 | main_messages_rx: Box::pin(main_messages_rx), |
197 | 0 | messages_rx: stream::SelectAll::new(), |
198 | 0 | blocks_requests: HashMap::with_capacity_and_hasher(8, Default::default()), |
199 | 0 | grandpa_warp_sync_requests: HashMap::with_capacity_and_hasher(8, Default::default()), |
200 | 0 | storage_proof_requests: HashMap::with_capacity_and_hasher(8, Default::default()), |
201 | 0 | call_proof_requests: HashMap::with_capacity_and_hasher(8, Default::default()), |
202 | 0 | chains_by_next_discovery: BTreeMap::new(), |
203 | 0 | })); |
204 | 0 |
|
205 | 0 | config.platform.spawn_task("network-service".into(), { |
206 | 0 | let platform = config.platform.clone(); |
207 | 0 | async move { |
208 | 0 | task.await; |
209 | 0 | log!(&platform, Debug, "network", "shutdown"); |
210 | 0 | } Unexecuted instantiation: _RNCNvMNtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_14NetworkServicepE3new0B6_ Unexecuted instantiation: _RNCNvMNtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_14NetworkServiceNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE3new0B1g_ Unexecuted instantiation: _RNCNvMNtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_14NetworkServicepE3new0B6_ |
211 | 0 | }); |
212 | 0 |
|
213 | 0 | Arc::new(NetworkService { |
214 | 0 | messages_tx: main_messages_tx, |
215 | 0 | platform: config.platform, |
216 | 0 | }) |
217 | 0 | } Unexecuted instantiation: _RNvMNtCsiGub1lfKphe_13smoldot_light15network_serviceINtB2_14NetworkServicepE3newB4_ Unexecuted instantiation: _RNvMNtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB2_14NetworkServiceNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE3newB1e_ Unexecuted instantiation: _RNvMNtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB2_14NetworkServicepE3newB4_ |
218 | | |
219 | | /// Adds a chain to the list of chains that the network service connects to. |
220 | | /// |
221 | | /// Returns an object representing the chain and that allows interacting with it. If all |
222 | | /// references to [`NetworkServiceChain`] are destroyed, the network service automatically |
223 | | /// purges that chain. |
224 | 0 | pub fn add_chain(&self, config: ConfigChain) -> Arc<NetworkServiceChain<TPlat>> { |
225 | 0 | let (messages_tx, messages_rx) = async_channel::bounded(32); |
226 | 0 |
|
227 | 0 | // TODO: this code is hacky because we don't want to make `add_chain` async at the moment, because it's not convenient for lib.rs |
228 | 0 | self.platform.spawn_task("add-chain-message-send".into(), { |
229 | 0 | let config = service::ChainConfig { |
230 | 0 | grandpa_protocol_config: config.grandpa_protocol_finalized_block_height.map( |
231 | 0 | |commit_finalized_height| service::GrandpaState { |
232 | 0 | commit_finalized_height, |
233 | 0 | round_number: 1, |
234 | 0 | set_id: 0, |
235 | 0 | }, Unexecuted instantiation: _RNCNvMNtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_14NetworkServicepE9add_chain0B6_ Unexecuted instantiation: _RNCNvMNtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_14NetworkServiceNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE9add_chain0B1g_ Unexecuted instantiation: _RNCNvMNtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_14NetworkServicepE9add_chain0B6_ |
236 | 0 | ), |
237 | 0 | fork_id: config.fork_id.clone(), |
238 | 0 | block_number_bytes: config.block_number_bytes, |
239 | 0 | best_hash: config.best_block.1, |
240 | 0 | best_number: config.best_block.0, |
241 | 0 | genesis_hash: config.genesis_block_hash, |
242 | 0 | role: Role::Light, |
243 | 0 | allow_inbound_block_requests: false, |
244 | 0 | user_data: Chain { |
245 | 0 | log_name: config.log_name, |
246 | 0 | block_number_bytes: config.block_number_bytes, |
247 | 0 | num_out_slots: config.num_out_slots, |
248 | 0 | num_references: NonZeroUsize::new(1).unwrap(), |
249 | 0 | next_discovery_period: Duration::from_secs(2), |
250 | 0 | next_discovery_when: self.platform.now(), |
251 | 0 | }, |
252 | 0 | }; |
253 | 0 |
|
254 | 0 | let messages_tx = self.messages_tx.clone(); |
255 | 0 | async move { |
256 | 0 | let _ = messages_tx |
257 | 0 | .send(ToBackground::AddChain { |
258 | 0 | messages_rx, |
259 | 0 | config, |
260 | 0 | }) |
261 | 0 | .await; |
262 | 0 | } Unexecuted instantiation: _RNCNvMNtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_14NetworkServicepE9add_chains_0B6_ Unexecuted instantiation: _RNCNvMNtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_14NetworkServiceNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE9add_chains_0B1g_ Unexecuted instantiation: _RNCNvMNtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_14NetworkServicepE9add_chains_0B6_ |
263 | 0 | }); |
264 | 0 |
|
265 | 0 | Arc::new(NetworkServiceChain { |
266 | 0 | _keep_alive_messages_tx: self.messages_tx.clone(), |
267 | 0 | messages_tx, |
268 | 0 | marker: core::marker::PhantomData, |
269 | 0 | }) |
270 | 0 | } Unexecuted instantiation: _RNvMNtCsiGub1lfKphe_13smoldot_light15network_serviceINtB2_14NetworkServicepE9add_chainB4_ Unexecuted instantiation: _RNvMNtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB2_14NetworkServiceNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE9add_chainB1e_ Unexecuted instantiation: _RNvMNtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB2_14NetworkServicepE9add_chainB4_ |
271 | | } |
272 | | |
273 | | pub struct NetworkServiceChain<TPlat: PlatformRef> { |
274 | | /// Copy of [`NetworkService::messages_tx`]. Used in order to maintain the network service |
275 | | /// background task alive. |
276 | | _keep_alive_messages_tx: async_channel::Sender<ToBackground<TPlat>>, |
277 | | |
278 | | /// Channel to send messages to the background task. |
279 | | messages_tx: async_channel::Sender<ToBackgroundChain>, |
280 | | |
281 | | /// Dummy to hold the `TPlat` type. |
282 | | marker: core::marker::PhantomData<TPlat>, |
283 | | } |
284 | | |
285 | | /// Severity of a ban. See [`NetworkServiceChain::ban_and_disconnect`]. |
286 | | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
287 | | pub enum BanSeverity { |
288 | | Low, |
289 | | High, |
290 | | } |
291 | | |
292 | | impl<TPlat: PlatformRef> NetworkServiceChain<TPlat> { |
293 | | /// Subscribes to the networking events that happen on the given chain. |
294 | | /// |
295 | | /// Calling this function returns a `Receiver` that receives events about the chain. |
296 | | /// The new channel will immediately receive events about all the existing connections, so |
297 | | /// that it is able to maintain a coherent view of the network. |
298 | | /// |
299 | | /// Note that this function is `async`, but it should return very quickly. |
300 | | /// |
301 | | /// The `Receiver` **must** be polled continuously. When the channel is full, the networking |
302 | | /// connections will be back-pressured until the channel isn't full anymore. |
303 | | /// |
304 | | /// The `Receiver` never yields `None` unless the [`NetworkService`] crashes or is destroyed. |
305 | | /// If `None` is yielded and the [`NetworkService`] is still alive, you should call |
306 | | /// [`NetworkServiceChain::subscribe`] again to obtain a new `Receiver`. |
307 | | /// |
308 | | /// # Panic |
309 | | /// |
310 | | /// Panics if the given [`ChainId`] is invalid. |
311 | | /// |
312 | | // TODO: consider not killing the background until the channel is destroyed, as that would be a more sensical behaviour |
313 | 0 | pub async fn subscribe(&self) -> async_channel::Receiver<Event> { Unexecuted instantiation: _RNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE9subscribeB6_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE9subscribeB1l_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE9subscribeB6_ |
314 | 0 | let (tx, rx) = async_channel::bounded(128); |
315 | 0 |
|
316 | 0 | let _ = self |
317 | 0 | .messages_tx |
318 | 0 | .send(ToBackgroundChain::Subscribe { sender: tx }) |
319 | 0 | .await |
320 | 0 | .unwrap(); |
321 | 0 |
|
322 | 0 | rx |
323 | 0 | } Unexecuted instantiation: _RNCNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE9subscribe0B8_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE9subscribe0B1n_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE9subscribe0B8_ |
324 | | |
325 | | /// Starts asynchronously disconnecting the given peer. A [`Event::Disconnected`] will later be |
326 | | /// generated. Prevents a new gossip link with the same peer from being reopened for a |
327 | | /// little while. |
328 | | /// |
329 | | /// `reason` is a human-readable string printed in the logs. |
330 | | /// |
331 | | /// Due to race conditions, it is possible to reconnect to the peer soon after, in case the |
332 | | /// reconnection was already happening as the call to this function is still being processed. |
333 | | /// If that happens another [`Event::Disconnected`] will be delivered afterwards. In other |
334 | | /// words, this function guarantees that we will be disconnected in the future rather than |
335 | | /// guarantees that we will disconnect. |
336 | 0 | pub async fn ban_and_disconnect( |
337 | 0 | &self, |
338 | 0 | peer_id: PeerId, |
339 | 0 | severity: BanSeverity, |
340 | 0 | reason: &'static str, |
341 | 0 | ) { Unexecuted instantiation: _RNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE18ban_and_disconnectB6_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE18ban_and_disconnectB1l_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE18ban_and_disconnectB6_ |
342 | 0 | let _ = self |
343 | 0 | .messages_tx |
344 | 0 | .send(ToBackgroundChain::DisconnectAndBan { |
345 | 0 | peer_id, |
346 | 0 | severity, |
347 | 0 | reason, |
348 | 0 | }) |
349 | 0 | .await; |
350 | 0 | } Unexecuted instantiation: _RNCNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE18ban_and_disconnect0B8_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE18ban_and_disconnect0B1n_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE18ban_and_disconnect0B8_ |
351 | | |
352 | | /// Sends a blocks request to the given peer. |
353 | | // TODO: more docs |
354 | 0 | pub async fn blocks_request( |
355 | 0 | self: Arc<Self>, |
356 | 0 | target: PeerId, |
357 | 0 | config: codec::BlocksRequestConfig, |
358 | 0 | timeout: Duration, |
359 | 0 | ) -> Result<Vec<codec::BlockData>, BlocksRequestError> { Unexecuted instantiation: _RNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE14blocks_requestB6_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE14blocks_requestB1l_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE14blocks_requestB6_ |
360 | 0 | let (tx, rx) = oneshot::channel(); |
361 | 0 |
|
362 | 0 | self.messages_tx |
363 | 0 | .send(ToBackgroundChain::StartBlocksRequest { |
364 | 0 | target: target.clone(), |
365 | 0 | config, |
366 | 0 | timeout, |
367 | 0 | result: tx, |
368 | 0 | }) |
369 | 0 | .await |
370 | 0 | .unwrap(); |
371 | 0 |
|
372 | 0 | rx.await.unwrap() |
373 | 0 | } Unexecuted instantiation: _RNCNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE14blocks_request0B8_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE14blocks_request0B1n_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE14blocks_request0B8_ |
374 | | |
375 | | /// Sends a grandpa warp sync request to the given peer. |
376 | | // TODO: more docs |
377 | 0 | pub async fn grandpa_warp_sync_request( |
378 | 0 | self: Arc<Self>, |
379 | 0 | target: PeerId, |
380 | 0 | begin_hash: [u8; 32], |
381 | 0 | timeout: Duration, |
382 | 0 | ) -> Result<service::EncodedGrandpaWarpSyncResponse, WarpSyncRequestError> { Unexecuted instantiation: _RNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE25grandpa_warp_sync_requestB6_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE25grandpa_warp_sync_requestB1l_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE25grandpa_warp_sync_requestB6_ |
383 | 0 | let (tx, rx) = oneshot::channel(); |
384 | 0 |
|
385 | 0 | self.messages_tx |
386 | 0 | .send(ToBackgroundChain::StartWarpSyncRequest { |
387 | 0 | target: target.clone(), |
388 | 0 | begin_hash, |
389 | 0 | timeout, |
390 | 0 | result: tx, |
391 | 0 | }) |
392 | 0 | .await |
393 | 0 | .unwrap(); |
394 | 0 |
|
395 | 0 | rx.await.unwrap() |
396 | 0 | } Unexecuted instantiation: _RNCNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE25grandpa_warp_sync_request0B8_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE25grandpa_warp_sync_request0B1n_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE25grandpa_warp_sync_request0B8_ |
397 | | |
398 | 0 | pub async fn set_local_best_block(&self, best_hash: [u8; 32], best_number: u64) { Unexecuted instantiation: _RNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE20set_local_best_blockB6_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE20set_local_best_blockB1l_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE20set_local_best_blockB6_ |
399 | 0 | self.messages_tx |
400 | 0 | .send(ToBackgroundChain::SetLocalBestBlock { |
401 | 0 | best_hash, |
402 | 0 | best_number, |
403 | 0 | }) |
404 | 0 | .await |
405 | 0 | .unwrap(); |
406 | 0 | } Unexecuted instantiation: _RNCNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE20set_local_best_block0B8_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE20set_local_best_block0B1n_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE20set_local_best_block0B8_ |
407 | | |
408 | 0 | pub async fn set_local_grandpa_state(&self, grandpa_state: service::GrandpaState) { Unexecuted instantiation: _RNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE23set_local_grandpa_stateB6_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE23set_local_grandpa_stateB1l_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE23set_local_grandpa_stateB6_ |
409 | 0 | self.messages_tx |
410 | 0 | .send(ToBackgroundChain::SetLocalGrandpaState { grandpa_state }) |
411 | 0 | .await |
412 | 0 | .unwrap(); |
413 | 0 | } Unexecuted instantiation: _RNCNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE23set_local_grandpa_state0B8_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE23set_local_grandpa_state0B1n_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE23set_local_grandpa_state0B8_ |
414 | | |
415 | | /// Sends a storage proof request to the given peer. |
416 | | // TODO: more docs |
417 | 0 | pub async fn storage_proof_request( |
418 | 0 | self: Arc<Self>, |
419 | 0 | target: PeerId, // TODO: takes by value because of futures longevity issue |
420 | 0 | config: codec::StorageProofRequestConfig<impl Iterator<Item = impl AsRef<[u8]> + Clone>>, |
421 | 0 | timeout: Duration, |
422 | 0 | ) -> Result<service::EncodedMerkleProof, StorageProofRequestError> { Unexecuted instantiation: _RINvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB5_19NetworkServiceChainpE21storage_proof_requestppEB7_ Unexecuted instantiation: _RINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB5_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE21storage_proof_requestINtNtCsdZExvAaxgia_5alloc3vec3VechEINtNtCsiw7FdGorGc8_9hashbrown3set8IntoIterB2D_EEB1m_ Unexecuted instantiation: _RINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB5_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE21storage_proof_requestINtNtCsdZExvAaxgia_5alloc3vec3VechEINtNtB2G_9into_iter8IntoIterB2D_EEB1m_ Unexecuted instantiation: _RINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB5_19NetworkServiceChainpE21storage_proof_requestppEB7_ |
423 | 0 | let (tx, rx) = oneshot::channel(); |
424 | 0 |
|
425 | 0 | self.messages_tx |
426 | 0 | .send(ToBackgroundChain::StartStorageProofRequest { |
427 | 0 | target: target.clone(), |
428 | 0 | config: codec::StorageProofRequestConfig { |
429 | 0 | block_hash: config.block_hash, |
430 | 0 | keys: config |
431 | 0 | .keys |
432 | 0 | .map(|key| key.as_ref().to_vec()) // TODO: to_vec() overhead Unexecuted instantiation: _RNCNCINvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB9_19NetworkServiceChainpE21storage_proof_requestppE00Bb_ Unexecuted instantiation: _RNCNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB9_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE21storage_proof_requestINtNtCsdZExvAaxgia_5alloc3vec3VechEINtNtCsiw7FdGorGc8_9hashbrown3set8IntoIterB2H_EE00B1q_ Unexecuted instantiation: _RNCNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB9_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE21storage_proof_requestINtNtCsdZExvAaxgia_5alloc3vec3VechEINtNtB2K_9into_iter8IntoIterB2H_EE00B1q_ Unexecuted instantiation: _RNCNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB9_19NetworkServiceChainpE21storage_proof_requestppE00Bb_ |
433 | 0 | .collect::<Vec<_>>() |
434 | 0 | .into_iter(), |
435 | 0 | }, |
436 | 0 | timeout, |
437 | 0 | result: tx, |
438 | 0 | }) |
439 | 0 | .await |
440 | 0 | .unwrap(); |
441 | 0 |
|
442 | 0 | rx.await.unwrap() |
443 | 0 | } Unexecuted instantiation: _RNCINvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB7_19NetworkServiceChainpE21storage_proof_requestppE0B9_ Unexecuted instantiation: _RNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB7_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE21storage_proof_requestINtNtCsdZExvAaxgia_5alloc3vec3VechEINtNtCsiw7FdGorGc8_9hashbrown3set8IntoIterB2F_EE0B1o_ Unexecuted instantiation: _RNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB7_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE21storage_proof_requestINtNtCsdZExvAaxgia_5alloc3vec3VechEINtNtB2I_9into_iter8IntoIterB2F_EE0B1o_ Unexecuted instantiation: _RNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB7_19NetworkServiceChainpE21storage_proof_requestppE0B9_ |
444 | | |
445 | | /// Sends a call proof request to the given peer. |
446 | | /// |
447 | | /// See also [`NetworkServiceChain::call_proof_request`]. |
448 | | // TODO: more docs |
449 | 0 | pub async fn call_proof_request( |
450 | 0 | self: Arc<Self>, |
451 | 0 | target: PeerId, // TODO: takes by value because of futures longevity issue |
452 | 0 | config: codec::CallProofRequestConfig<'_, impl Iterator<Item = impl AsRef<[u8]>>>, |
453 | 0 | timeout: Duration, |
454 | 0 | ) -> Result<EncodedMerkleProof, CallProofRequestError> { Unexecuted instantiation: _RINvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB5_19NetworkServiceChainpE18call_proof_requestppEB7_ Unexecuted instantiation: _RINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB5_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE18call_proof_requestINtNtCsdZExvAaxgia_5alloc3vec3VechEINtNtNtNtCsaYZPK01V26L_4core4iter7sources4once4OnceB2A_EEB1m_ Unexecuted instantiation: _RINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB5_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE18call_proof_requestRINtNtCsdZExvAaxgia_5alloc6borrow3CowShEINtNtNtNtCsaYZPK01V26L_4core4iter7sources4once4OnceB2A_EEB1m_ Unexecuted instantiation: _RINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB5_19NetworkServiceChainpE18call_proof_requestppEB7_ |
455 | 0 | let (tx, rx) = oneshot::channel(); |
456 | 0 |
|
457 | 0 | self.messages_tx |
458 | 0 | .send(ToBackgroundChain::StartCallProofRequest { |
459 | 0 | target: target.clone(), |
460 | 0 | config: codec::CallProofRequestConfig { |
461 | 0 | block_hash: config.block_hash, |
462 | 0 | method: config.method.into_owned().into(), |
463 | 0 | parameter_vectored: config |
464 | 0 | .parameter_vectored |
465 | 0 | .map(|v| v.as_ref().to_vec()) // TODO: to_vec() overhead Unexecuted instantiation: _RNCNCINvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB9_19NetworkServiceChainpE18call_proof_requestppE00Bb_ Unexecuted instantiation: _RNCNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB9_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE18call_proof_requestINtNtCsdZExvAaxgia_5alloc3vec3VechEINtNtNtNtCsaYZPK01V26L_4core4iter7sources4once4OnceB2E_EE00B1q_ Unexecuted instantiation: _RNCNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB9_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE18call_proof_requestRINtNtCsdZExvAaxgia_5alloc6borrow3CowShEINtNtNtNtCsaYZPK01V26L_4core4iter7sources4once4OnceB2E_EE00B1q_ Unexecuted instantiation: _RNCNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB9_19NetworkServiceChainpE18call_proof_requestppE00Bb_ |
466 | 0 | .collect::<Vec<_>>() |
467 | 0 | .into_iter(), |
468 | 0 | }, |
469 | 0 | timeout, |
470 | 0 | result: tx, |
471 | 0 | }) |
472 | 0 | .await |
473 | 0 | .unwrap(); |
474 | 0 |
|
475 | 0 | rx.await.unwrap() |
476 | 0 | } Unexecuted instantiation: _RNCINvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB7_19NetworkServiceChainpE18call_proof_requestppE0B9_ Unexecuted instantiation: _RNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB7_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE18call_proof_requestINtNtCsdZExvAaxgia_5alloc3vec3VechEINtNtNtNtCsaYZPK01V26L_4core4iter7sources4once4OnceB2C_EE0B1o_ Unexecuted instantiation: _RNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB7_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE18call_proof_requestRINtNtCsdZExvAaxgia_5alloc6borrow3CowShEINtNtNtNtCsaYZPK01V26L_4core4iter7sources4once4OnceB2C_EE0B1o_ Unexecuted instantiation: _RNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB7_19NetworkServiceChainpE18call_proof_requestppE0B9_ |
477 | | |
478 | | /// Announces transaction to the peers we are connected to. |
479 | | /// |
480 | | /// Returns a list of peers that we have sent the transaction to. Can return an empty `Vec` |
481 | | /// if we didn't send the transaction to any peer. |
482 | | /// |
483 | | /// Note that the remote doesn't confirm that it has received the transaction. Because |
484 | | /// networking is inherently unreliable, successfully sending a transaction to a peer doesn't |
485 | | /// necessarily mean that the remote has received it. In practice, however, the likelihood of |
486 | | /// a transaction not being received are extremely low. This can be considered as known flaw. |
487 | 0 | pub async fn announce_transaction(self: Arc<Self>, transaction: &[u8]) -> Vec<PeerId> { Unexecuted instantiation: _RNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE20announce_transactionB6_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE20announce_transactionB1l_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE20announce_transactionB6_ |
488 | 0 | let (tx, rx) = oneshot::channel(); |
489 | 0 |
|
490 | 0 | self.messages_tx |
491 | 0 | .send(ToBackgroundChain::AnnounceTransaction { |
492 | 0 | transaction: transaction.to_vec(), // TODO: ovheread |
493 | 0 | result: tx, |
494 | 0 | }) |
495 | 0 | .await |
496 | 0 | .unwrap(); |
497 | 0 |
|
498 | 0 | rx.await.unwrap() |
499 | 0 | } Unexecuted instantiation: _RNCNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE20announce_transaction0B8_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE20announce_transaction0B1n_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE20announce_transaction0B8_ |
500 | | |
501 | | /// See [`service::ChainNetwork::gossip_send_block_announce`]. |
502 | 0 | pub async fn send_block_announce( |
503 | 0 | self: Arc<Self>, |
504 | 0 | target: &PeerId, |
505 | 0 | scale_encoded_header: &[u8], |
506 | 0 | is_best: bool, |
507 | 0 | ) -> Result<(), QueueNotificationError> { Unexecuted instantiation: _RNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE19send_block_announceB6_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE19send_block_announceB6_ |
508 | 0 | let (tx, rx) = oneshot::channel(); |
509 | 0 |
|
510 | 0 | self.messages_tx |
511 | 0 | .send(ToBackgroundChain::SendBlockAnnounce { |
512 | 0 | target: target.clone(), // TODO: overhead |
513 | 0 | scale_encoded_header: scale_encoded_header.to_vec(), // TODO: overhead |
514 | 0 | is_best, |
515 | 0 | result: tx, |
516 | 0 | }) |
517 | 0 | .await |
518 | 0 | .unwrap(); |
519 | 0 |
|
520 | 0 | rx.await.unwrap() |
521 | 0 | } Unexecuted instantiation: _RNCNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE19send_block_announce0B8_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE19send_block_announce0B8_ |
522 | | |
523 | | /// Marks the given peers as belonging to the given chain, and adds some addresses to these |
524 | | /// peers to the address book. |
525 | | /// |
526 | | /// The `important_nodes` parameter indicates whether these nodes are considered note-worthy |
527 | | /// and should have additional logging. |
528 | 0 | pub async fn discover( |
529 | 0 | &self, |
530 | 0 | list: impl IntoIterator<Item = (PeerId, impl IntoIterator<Item = Multiaddr>)>, |
531 | 0 | important_nodes: bool, |
532 | 0 | ) { Unexecuted instantiation: _RINvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB5_19NetworkServiceChainpE8discoverppEB7_ Unexecuted instantiation: _RINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB5_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE8discoverINtNtCsdZExvAaxgia_5alloc3vec3VecNtNtNtCseuYC0Zibziv_7smoldot6libp2p9multiaddr9MultiaddrEIB2q_TNtNtB30_7peer_id6PeerIdB2p_EEEB1m_ Unexecuted instantiation: _RINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB5_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE8discoverINtNtNtNtCsaYZPK01V26L_4core4iter7sources4once4OnceNtNtNtCseuYC0Zibziv_7smoldot6libp2p9multiaddr9MultiaddrEIB2q_TNtNtB3i_7peer_id6PeerIdB2p_EEEB1m_ Unexecuted instantiation: _RINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB5_19NetworkServiceChainpE8discoverppEB7_ |
533 | 0 | self.messages_tx |
534 | 0 | .send(ToBackgroundChain::Discover { |
535 | 0 | // TODO: overhead |
536 | 0 | list: list |
537 | 0 | .into_iter() |
538 | 0 | .map(|(peer_id, addrs)| { |
539 | 0 | (peer_id, addrs.into_iter().collect::<Vec<_>>().into_iter()) |
540 | 0 | }) Unexecuted instantiation: _RNCNCINvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB9_19NetworkServiceChainpE8discoverppE00Bb_ Unexecuted instantiation: _RNCNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB9_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE8discoverINtNtCsdZExvAaxgia_5alloc3vec3VecNtNtNtCseuYC0Zibziv_7smoldot6libp2p9multiaddr9MultiaddrEIB2u_TNtNtB34_7peer_id6PeerIdB2t_EEE00B1q_ Unexecuted instantiation: _RNCNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB9_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE8discoverINtNtNtNtCsaYZPK01V26L_4core4iter7sources4once4OnceNtNtNtCseuYC0Zibziv_7smoldot6libp2p9multiaddr9MultiaddrEIB2u_TNtNtB3m_7peer_id6PeerIdB2t_EEE00B1q_ Unexecuted instantiation: _RNCNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB9_19NetworkServiceChainpE8discoverppE00Bb_ |
541 | 0 | .collect::<Vec<_>>() |
542 | 0 | .into_iter(), |
543 | 0 | important_nodes, |
544 | 0 | }) |
545 | 0 | .await |
546 | 0 | .unwrap(); |
547 | 0 | } Unexecuted instantiation: _RNCINvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB7_19NetworkServiceChainpE8discoverppE0B9_ Unexecuted instantiation: _RNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB7_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE8discoverINtNtCsdZExvAaxgia_5alloc3vec3VecNtNtNtCseuYC0Zibziv_7smoldot6libp2p9multiaddr9MultiaddrEIB2s_TNtNtB32_7peer_id6PeerIdB2r_EEE0B1o_ Unexecuted instantiation: _RNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB7_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE8discoverINtNtNtNtCsaYZPK01V26L_4core4iter7sources4once4OnceNtNtNtCseuYC0Zibziv_7smoldot6libp2p9multiaddr9MultiaddrEIB2s_TNtNtB3k_7peer_id6PeerIdB2r_EEE0B1o_ Unexecuted instantiation: _RNCINvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB7_19NetworkServiceChainpE8discoverppE0B9_ |
548 | | |
549 | | /// Returns a list of nodes (their [`PeerId`] and multiaddresses) that we know are part of |
550 | | /// the network. |
551 | | /// |
552 | | /// Nodes that are discovered might disappear over time. In other words, there is no guarantee |
553 | | /// that a node that has been added through [`NetworkServiceChain::discover`] will later be |
554 | | /// returned by [`NetworkServiceChain::discovered_nodes`]. |
555 | 0 | pub async fn discovered_nodes( |
556 | 0 | &self, |
557 | 0 | ) -> impl Iterator<Item = (PeerId, impl Iterator<Item = Multiaddr>)> { Unexecuted instantiation: _RNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE16discovered_nodesB6_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE16discovered_nodesB1l_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE16discovered_nodesB6_ |
558 | 0 | let (tx, rx) = oneshot::channel(); |
559 | 0 |
|
560 | 0 | self.messages_tx |
561 | 0 | .send(ToBackgroundChain::DiscoveredNodes { result: tx }) |
562 | 0 | .await |
563 | 0 | .unwrap(); |
564 | 0 |
|
565 | 0 | rx.await |
566 | 0 | .unwrap() |
567 | 0 | .into_iter() |
568 | 0 | .map(|(peer_id, addrs)| (peer_id, addrs.into_iter())) Unexecuted instantiation: _RNCNCNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB8_19NetworkServiceChainpE16discovered_nodes00Ba_ Unexecuted instantiation: _RNCNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB8_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE16discovered_nodes00B1p_ Unexecuted instantiation: _RNCNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB8_19NetworkServiceChainpE16discovered_nodes00Ba_ |
569 | 0 | } Unexecuted instantiation: _RNCNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE16discovered_nodes0B8_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE16discovered_nodes0B1n_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE16discovered_nodes0B8_ |
570 | | |
571 | | /// Returns an iterator to the list of [`PeerId`]s that we have an established connection |
572 | | /// with. |
573 | 0 | pub async fn peers_list(&self) -> impl Iterator<Item = PeerId> { Unexecuted instantiation: _RNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE10peers_listB6_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE10peers_listB1l_ Unexecuted instantiation: _RNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB4_19NetworkServiceChainpE10peers_listB6_ |
574 | 0 | let (tx, rx) = oneshot::channel(); |
575 | 0 | self.messages_tx |
576 | 0 | .send(ToBackgroundChain::PeersList { result: tx }) |
577 | 0 | .await |
578 | 0 | .unwrap(); |
579 | 0 | rx.await.unwrap().into_iter() |
580 | 0 | } Unexecuted instantiation: _RNCNvMs_NtCsiGub1lfKphe_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE10peers_list0B8_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE10peers_list0B1n_ Unexecuted instantiation: _RNCNvMs_NtCsih6EgvAwZF2_13smoldot_light15network_serviceINtB6_19NetworkServiceChainpE10peers_list0B8_ |
581 | | } |
582 | | |
583 | | /// Event that can happen on the network service. |
584 | | #[derive(Debug, Clone)] |
585 | | pub enum Event { |
586 | | Connected { |
587 | | peer_id: PeerId, |
588 | | role: Role, |
589 | | best_block_number: u64, |
590 | | best_block_hash: [u8; 32], |
591 | | }, |
592 | | Disconnected { |
593 | | peer_id: PeerId, |
594 | | }, |
595 | | BlockAnnounce { |
596 | | peer_id: PeerId, |
597 | | announce: service::EncodedBlockAnnounce, |
598 | | }, |
599 | | GrandpaNeighborPacket { |
600 | | peer_id: PeerId, |
601 | | finalized_block_height: u64, |
602 | | }, |
603 | | /// Received a GrandPa commit message from the network. |
604 | | GrandpaCommitMessage { |
605 | | peer_id: PeerId, |
606 | | message: service::EncodedGrandpaCommitMessage, |
607 | | }, |
608 | | } |
609 | | |
610 | | /// Error returned by [`NetworkServiceChain::blocks_request`]. |
611 | 0 | #[derive(Debug, derive_more::Display)] Unexecuted instantiation: _RNvXsa_NtCsiGub1lfKphe_13smoldot_light15network_serviceNtB5_18BlocksRequestErrorNtNtCsaYZPK01V26L_4core3fmt7Display3fmt Unexecuted instantiation: _RNvXsa_NtCsih6EgvAwZF2_13smoldot_light15network_serviceNtB5_18BlocksRequestErrorNtNtCsaYZPK01V26L_4core3fmt7Display3fmt |
612 | | pub enum BlocksRequestError { |
613 | | /// No established connection with the target. |
614 | | NoConnection, |
615 | | /// Error during the request. |
616 | | #[display(fmt = "{_0}")] |
617 | | Request(service::BlocksRequestError), |
618 | | } |
619 | | |
620 | | /// Error returned by [`NetworkServiceChain::grandpa_warp_sync_request`]. |
621 | 0 | #[derive(Debug, derive_more::Display)] Unexecuted instantiation: _RNvXsc_NtCsiGub1lfKphe_13smoldot_light15network_serviceNtB5_20WarpSyncRequestErrorNtNtCsaYZPK01V26L_4core3fmt7Display3fmt Unexecuted instantiation: _RNvXsc_NtCsih6EgvAwZF2_13smoldot_light15network_serviceNtB5_20WarpSyncRequestErrorNtNtCsaYZPK01V26L_4core3fmt7Display3fmt |
622 | | pub enum WarpSyncRequestError { |
623 | | /// No established connection with the target. |
624 | | NoConnection, |
625 | | /// Error during the request. |
626 | | #[display(fmt = "{_0}")] |
627 | | Request(service::GrandpaWarpSyncRequestError), |
628 | | } |
629 | | |
630 | | /// Error returned by [`NetworkServiceChain::storage_proof_request`]. |
631 | 0 | #[derive(Debug, derive_more::Display, Clone)] Unexecuted instantiation: _RNvXse_NtCsiGub1lfKphe_13smoldot_light15network_serviceNtB5_24StorageProofRequestErrorNtNtCsaYZPK01V26L_4core3fmt7Display3fmt Unexecuted instantiation: _RNvXse_NtCsih6EgvAwZF2_13smoldot_light15network_serviceNtB5_24StorageProofRequestErrorNtNtCsaYZPK01V26L_4core3fmt7Display3fmt |
632 | | pub enum StorageProofRequestError { |
633 | | /// No established connection with the target. |
634 | | NoConnection, |
635 | | /// Storage proof request is too large and can't be sent. |
636 | | RequestTooLarge, |
637 | | /// Error during the request. |
638 | | #[display(fmt = "{_0}")] |
639 | | Request(service::StorageProofRequestError), |
640 | | } |
641 | | |
642 | | /// Error returned by [`NetworkServiceChain::call_proof_request`]. |
643 | 0 | #[derive(Debug, derive_more::Display, Clone)] Unexecuted instantiation: _RNvXsh_NtCsiGub1lfKphe_13smoldot_light15network_serviceNtB5_21CallProofRequestErrorNtNtCsaYZPK01V26L_4core3fmt7Display3fmt Unexecuted instantiation: _RNvXsh_NtCsih6EgvAwZF2_13smoldot_light15network_serviceNtB5_21CallProofRequestErrorNtNtCsaYZPK01V26L_4core3fmt7Display3fmt |
644 | | pub enum CallProofRequestError { |
645 | | /// No established connection with the target. |
646 | | NoConnection, |
647 | | /// Call proof request is too large and can't be sent. |
648 | | RequestTooLarge, |
649 | | /// Error during the request. |
650 | | #[display(fmt = "{_0}")] |
651 | | Request(service::CallProofRequestError), |
652 | | } |
653 | | |
654 | | impl CallProofRequestError { |
655 | | /// Returns `true` if this is caused by networking issues, as opposed to a consensus-related |
656 | | /// issue. |
657 | 0 | pub fn is_network_problem(&self) -> bool { |
658 | 0 | match self { |
659 | 0 | CallProofRequestError::Request(err) => err.is_network_problem(), |
660 | 0 | CallProofRequestError::RequestTooLarge => false, |
661 | 0 | CallProofRequestError::NoConnection => true, |
662 | | } |
663 | 0 | } Unexecuted instantiation: _RNvMs0_NtCsiGub1lfKphe_13smoldot_light15network_serviceNtB5_21CallProofRequestError18is_network_problem Unexecuted instantiation: _RNvMs0_NtCsih6EgvAwZF2_13smoldot_light15network_serviceNtB5_21CallProofRequestError18is_network_problem |
664 | | } |
665 | | |
666 | | enum ToBackground<TPlat: PlatformRef> { |
667 | | AddChain { |
668 | | messages_rx: async_channel::Receiver<ToBackgroundChain>, |
669 | | config: service::ChainConfig<Chain<TPlat>>, |
670 | | }, |
671 | | } |
672 | | |
673 | | enum ToBackgroundChain { |
674 | | RemoveChain, |
675 | | Subscribe { |
676 | | sender: async_channel::Sender<Event>, |
677 | | }, |
678 | | DisconnectAndBan { |
679 | | peer_id: PeerId, |
680 | | severity: BanSeverity, |
681 | | reason: &'static str, |
682 | | }, |
683 | | // TODO: serialize the request before sending over channel |
684 | | StartBlocksRequest { |
685 | | target: PeerId, // TODO: takes by value because of future longevity issue |
686 | | config: codec::BlocksRequestConfig, |
687 | | timeout: Duration, |
688 | | result: oneshot::Sender<Result<Vec<codec::BlockData>, BlocksRequestError>>, |
689 | | }, |
690 | | // TODO: serialize the request before sending over channel |
691 | | StartWarpSyncRequest { |
692 | | target: PeerId, |
693 | | begin_hash: [u8; 32], |
694 | | timeout: Duration, |
695 | | result: |
696 | | oneshot::Sender<Result<service::EncodedGrandpaWarpSyncResponse, WarpSyncRequestError>>, |
697 | | }, |
698 | | // TODO: serialize the request before sending over channel |
699 | | StartStorageProofRequest { |
700 | | target: PeerId, |
701 | | config: codec::StorageProofRequestConfig<vec::IntoIter<Vec<u8>>>, |
702 | | timeout: Duration, |
703 | | result: oneshot::Sender<Result<service::EncodedMerkleProof, StorageProofRequestError>>, |
704 | | }, |
705 | | // TODO: serialize the request before sending over channel |
706 | | StartCallProofRequest { |
707 | | target: PeerId, // TODO: takes by value because of futures longevity issue |
708 | | config: codec::CallProofRequestConfig<'static, vec::IntoIter<Vec<u8>>>, |
709 | | timeout: Duration, |
710 | | result: oneshot::Sender<Result<service::EncodedMerkleProof, CallProofRequestError>>, |
711 | | }, |
712 | | SetLocalBestBlock { |
713 | | best_hash: [u8; 32], |
714 | | best_number: u64, |
715 | | }, |
716 | | SetLocalGrandpaState { |
717 | | grandpa_state: service::GrandpaState, |
718 | | }, |
719 | | AnnounceTransaction { |
720 | | transaction: Vec<u8>, |
721 | | result: oneshot::Sender<Vec<PeerId>>, |
722 | | }, |
723 | | SendBlockAnnounce { |
724 | | target: PeerId, |
725 | | scale_encoded_header: Vec<u8>, |
726 | | is_best: bool, |
727 | | result: oneshot::Sender<Result<(), QueueNotificationError>>, |
728 | | }, |
729 | | Discover { |
730 | | list: vec::IntoIter<(PeerId, vec::IntoIter<Multiaddr>)>, |
731 | | important_nodes: bool, |
732 | | }, |
733 | | DiscoveredNodes { |
734 | | result: oneshot::Sender<Vec<(PeerId, Vec<Multiaddr>)>>, |
735 | | }, |
736 | | PeersList { |
737 | | result: oneshot::Sender<Vec<PeerId>>, |
738 | | }, |
739 | | } |
740 | | |
741 | | struct BackgroundTask<TPlat: PlatformRef> { |
742 | | /// See [`Config::platform`]. |
743 | | platform: TPlat, |
744 | | |
745 | | /// Random number generator. |
746 | | randomness: rand_chacha::ChaCha20Rng, |
747 | | |
748 | | /// Value provided through [`Config::identify_agent_version`]. |
749 | | identify_agent_version: String, |
750 | | |
751 | | /// Channel to send messages to the background task. |
752 | | tasks_messages_tx: |
753 | | async_channel::Sender<(service::ConnectionId, service::ConnectionToCoordinator)>, |
754 | | |
755 | | /// Channel to receive messages destined to the background task. |
756 | | tasks_messages_rx: Pin< |
757 | | Box<async_channel::Receiver<(service::ConnectionId, service::ConnectionToCoordinator)>>, |
758 | | >, |
759 | | |
760 | | /// Data structure holding the entire state of the networking. |
761 | | network: service::ChainNetwork< |
762 | | Chain<TPlat>, |
763 | | async_channel::Sender<service::CoordinatorToConnection>, |
764 | | TPlat::Instant, |
765 | | >, |
766 | | |
767 | | /// All known peers and their addresses. |
768 | | peering_strategy: basic_peering_strategy::BasicPeeringStrategy<ChainId, TPlat::Instant>, |
769 | | |
770 | | /// See [`Config::connections_open_pool_size`]. |
771 | | connections_open_pool_size: u32, |
772 | | |
773 | | /// See [`Config::connections_open_pool_restore_delay`]. |
774 | | connections_open_pool_restore_delay: Duration, |
775 | | |
776 | | /// Every time a connection is opened, the value in this field is increased by one. After |
777 | | /// [`BackgroundTask::next_recent_connection_restore`] has yielded, the value is reduced by |
778 | | /// one. |
779 | | num_recent_connection_opening: u32, |
780 | | |
781 | | /// Delay after which [`BackgroundTask::num_recent_connection_opening`] is increased by one. |
782 | | next_recent_connection_restore: Option<Pin<Box<TPlat::Delay>>>, |
783 | | |
784 | | /// List of all open gossip links. |
785 | | // TODO: using this data structure unfortunately means that PeerIds are cloned a lot, maybe some user data in ChainNetwork is better? not sure |
786 | | open_gossip_links: BTreeMap<(ChainId, PeerId), OpenGossipLinkState>, |
787 | | |
788 | | /// List of nodes that are considered as important for logging purposes. |
789 | | // TODO: should also detect whenever we fail to open a block announces substream with any of these peers |
790 | | important_nodes: HashSet<PeerId, fnv::FnvBuildHasher>, |
791 | | |
792 | | /// Event about to be sent on the senders of [`BackgroundTask::event_senders`]. |
793 | | event_pending_send: Option<(ChainId, Event)>, |
794 | | |
795 | | /// Sending events through the public API. |
796 | | /// |
797 | | /// Contains either senders, or a `Future` that is currently sending an event and will yield |
798 | | /// the senders back once it is finished. |
799 | | // TODO: sort by ChainId instead of using a Vec? |
800 | | event_senders: either::Either< |
801 | | Vec<(ChainId, async_channel::Sender<Event>)>, |
802 | | Pin<Box<dyn future::Future<Output = Vec<(ChainId, async_channel::Sender<Event>)>> + Send>>, |
803 | | >, |
804 | | |
805 | | /// Whenever [`NetworkServiceChain::subscribe`] is called, the new sender is added to this list. |
806 | | /// Once [`BackgroundTask::event_senders`] is ready, we properly initialize these senders. |
807 | | pending_new_subscriptions: Vec<(ChainId, async_channel::Sender<Event>)>, |
808 | | |
809 | | main_messages_rx: Pin<Box<async_channel::Receiver<ToBackground<TPlat>>>>, |
810 | | |
811 | | messages_rx: |
812 | | stream::SelectAll<Pin<Box<dyn stream::Stream<Item = (ChainId, ToBackgroundChain)> + Send>>>, |
813 | | |
814 | | blocks_requests: HashMap< |
815 | | service::SubstreamId, |
816 | | oneshot::Sender<Result<Vec<codec::BlockData>, BlocksRequestError>>, |
817 | | fnv::FnvBuildHasher, |
818 | | >, |
819 | | |
820 | | grandpa_warp_sync_requests: HashMap< |
821 | | service::SubstreamId, |
822 | | oneshot::Sender<Result<service::EncodedGrandpaWarpSyncResponse, WarpSyncRequestError>>, |
823 | | fnv::FnvBuildHasher, |
824 | | >, |
825 | | |
826 | | storage_proof_requests: HashMap< |
827 | | service::SubstreamId, |
828 | | oneshot::Sender<Result<service::EncodedMerkleProof, StorageProofRequestError>>, |
829 | | fnv::FnvBuildHasher, |
830 | | >, |
831 | | |
832 | | call_proof_requests: HashMap< |
833 | | service::SubstreamId, |
834 | | oneshot::Sender<Result<service::EncodedMerkleProof, CallProofRequestError>>, |
835 | | fnv::FnvBuildHasher, |
836 | | >, |
837 | | |
838 | | /// All chains, indexed by the value of [`Chain::next_discovery_when`]. |
839 | | chains_by_next_discovery: BTreeMap<(TPlat::Instant, ChainId), Pin<Box<TPlat::Delay>>>, |
840 | | } |
841 | | |
842 | | struct Chain<TPlat: PlatformRef> { |
843 | | log_name: String, |
844 | | |
845 | | // TODO: this field is a hack due to the fact that `add_chain` can't be `async`; should eventually be fixed after a lib.rs refactor |
846 | | num_references: NonZeroUsize, |
847 | | |
848 | | /// See [`ConfigChain::block_number_bytes`]. |
849 | | // TODO: redundant with ChainNetwork? since we might not need to know this in the future i'm reluctant to add a getter to ChainNetwork |
850 | | block_number_bytes: usize, |
851 | | |
852 | | /// See [`ConfigChain::num_out_slots`]. |
853 | | num_out_slots: usize, |
854 | | |
855 | | /// When the next discovery should be started for this chain. |
856 | | next_discovery_when: TPlat::Instant, |
857 | | |
858 | | /// After [`Chain::next_discovery_when`] is reached, the following discovery happens after |
859 | | /// the given duration. |
860 | | next_discovery_period: Duration, |
861 | | } |
862 | | |
863 | | #[derive(Clone)] |
864 | | struct OpenGossipLinkState { |
865 | | role: Role, |
866 | | best_block_number: u64, |
867 | | best_block_hash: [u8; 32], |
868 | | /// `None` if unknown. |
869 | | finalized_block_height: Option<u64>, |
870 | | } |
871 | | |
872 | 0 | async fn background_task<TPlat: PlatformRef>(mut task: BackgroundTask<TPlat>) { Unexecuted instantiation: _RINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpEB4_ Unexecuted instantiation: _RINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefEB19_ Unexecuted instantiation: _RINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpEB4_ |
873 | | loop { |
874 | | // Yield at every loop in order to provide better tasks granularity. |
875 | 0 | futures_lite::future::yield_now().await; |
876 | | |
877 | | enum WakeUpReason<TPlat: PlatformRef> { |
878 | | ForegroundClosed, |
879 | | Message(ToBackground<TPlat>), |
880 | | MessageForChain(ChainId, ToBackgroundChain), |
881 | | NetworkEvent(service::Event<async_channel::Sender<service::CoordinatorToConnection>>), |
882 | | CanAssignSlot(PeerId, ChainId), |
883 | | NextRecentConnectionRestore, |
884 | | CanStartConnect(PeerId), |
885 | | CanOpenGossip(PeerId, ChainId), |
886 | | MessageFromConnection { |
887 | | connection_id: service::ConnectionId, |
888 | | message: service::ConnectionToCoordinator, |
889 | | }, |
890 | | MessageToConnection { |
891 | | connection_id: service::ConnectionId, |
892 | | message: service::CoordinatorToConnection, |
893 | | }, |
894 | | EventSendersReady, |
895 | | StartDiscovery(ChainId), |
896 | | } |
897 | | |
898 | 0 | let wake_up_reason = { |
899 | 0 | let message_received = async { |
900 | 0 | task.main_messages_rx |
901 | 0 | .next() |
902 | 0 | .await |
903 | 0 | .map_or(WakeUpReason::ForegroundClosed, WakeUpReason::Message) |
904 | 0 | }; Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE00B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE00B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE00B8_ |
905 | 0 | let message_for_chain_received = async { |
906 | | // Note that when the last entry of `messages_rx` yields `None`, `messages_rx` |
907 | | // itself will yield `None`. For this reason, we can't use |
908 | | // `task.messages_rx.is_empty()` to determine whether `messages_rx` will |
909 | | // yield `None`. |
910 | 0 | let Some((chain_id, message)) = task.messages_rx.next().await else { |
911 | 0 | future::pending().await |
912 | | }; |
913 | 0 | WakeUpReason::MessageForChain(chain_id, message) |
914 | 0 | }; Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s_0B8_ |
915 | 0 | let message_from_task_received = async { |
916 | 0 | let (connection_id, message) = task.tasks_messages_rx.next().await.unwrap(); |
917 | 0 | WakeUpReason::MessageFromConnection { |
918 | 0 | connection_id, |
919 | 0 | message, |
920 | 0 | } |
921 | 0 | }; Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s0_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s0_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s0_0B8_ |
922 | 0 | let service_event = async { |
923 | 0 | if let Some(event) = (task.event_pending_send.is_none() |
924 | 0 | && task.pending_new_subscriptions.is_empty()) |
925 | 0 | .then(|| task.network.next_event()) Unexecuted instantiation: _RNCNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s1_00Ba_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s1_00B1f_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s1_00Ba_ |
926 | 0 | .flatten() |
927 | | { |
928 | 0 | WakeUpReason::NetworkEvent(event) |
929 | 0 | } else if let Some(start_connect) = { |
930 | 0 | let x = (task.num_recent_connection_opening < task.connections_open_pool_size) |
931 | 0 | .then(|| { |
932 | 0 | task.network |
933 | 0 | .unconnected_desired() |
934 | 0 | .choose(&mut task.randomness) |
935 | 0 | .cloned() |
936 | 0 | }) Unexecuted instantiation: _RNCNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s1_0s_0Ba_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s1_0s_0B1f_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s1_0s_0Ba_ |
937 | 0 | .flatten(); |
938 | 0 | x |
939 | 0 | } { |
940 | 0 | WakeUpReason::CanStartConnect(start_connect) |
941 | 0 | } else if let Some((peer_id, chain_id)) = { |
942 | 0 | let x = task |
943 | 0 | .network |
944 | 0 | .connected_unopened_gossip_desired() |
945 | 0 | .choose(&mut task.randomness) |
946 | 0 | .map(|(peer_id, chain_id, _)| (peer_id.clone(), chain_id)); Unexecuted instantiation: _RNCNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s1_0s0_0Ba_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s1_0s0_0B1f_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s1_0s0_0Ba_ |
947 | 0 | x |
948 | 0 | } { |
949 | 0 | WakeUpReason::CanOpenGossip(peer_id, chain_id) |
950 | 0 | } else if let Some((connection_id, message)) = |
951 | 0 | task.network.pull_message_to_connection() |
952 | | { |
953 | 0 | WakeUpReason::MessageToConnection { |
954 | 0 | connection_id, |
955 | 0 | message, |
956 | 0 | } |
957 | | } else { |
958 | 0 | 'search: loop { |
959 | 0 | let mut earlier_unban = None; |
960 | | |
961 | 0 | for chain_id in task.network.chains().collect::<Vec<_>>() { |
962 | 0 | if task.network.gossip_desired_num( |
963 | 0 | chain_id, |
964 | 0 | service::GossipKind::ConsensusTransactions, |
965 | 0 | ) >= task.network[chain_id].num_out_slots |
966 | | { |
967 | 0 | continue; |
968 | 0 | } |
969 | 0 |
|
970 | 0 | match task |
971 | 0 | .peering_strategy |
972 | 0 | .pick_assignable_peer(&chain_id, &task.platform.now()) |
973 | | { |
974 | 0 | basic_peering_strategy::AssignablePeer::Assignable(peer_id) => { |
975 | 0 | break 'search WakeUpReason::CanAssignSlot( |
976 | 0 | peer_id.clone(), |
977 | 0 | chain_id, |
978 | 0 | ) |
979 | | } |
980 | | basic_peering_strategy::AssignablePeer::AllPeersBanned { |
981 | 0 | next_unban, |
982 | 0 | } => { |
983 | 0 | if earlier_unban.as_ref().map_or(true, |b| b > next_unban) { Unexecuted instantiation: _RNCNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s1_0s1_0Ba_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s1_0s1_0B1f_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s1_0s1_0Ba_ |
984 | 0 | earlier_unban = Some(next_unban.clone()); |
985 | 0 | } |
986 | | } |
987 | 0 | basic_peering_strategy::AssignablePeer::NoPeer => continue, |
988 | | } |
989 | | } |
990 | | |
991 | 0 | if let Some(earlier_unban) = earlier_unban { |
992 | 0 | task.platform.sleep_until(earlier_unban).await; |
993 | | } else { |
994 | 0 | future::pending::<()>().await; |
995 | | } |
996 | | } |
997 | | } |
998 | 0 | }; Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s1_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s1_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s1_0B8_ |
999 | 0 | let next_recent_connection_restore = async { |
1000 | 0 | if task.num_recent_connection_opening != 0 |
1001 | 0 | && task.next_recent_connection_restore.is_none() |
1002 | 0 | { |
1003 | 0 | task.next_recent_connection_restore = Some(Box::pin( |
1004 | 0 | task.platform |
1005 | 0 | .sleep(task.connections_open_pool_restore_delay), |
1006 | 0 | )); |
1007 | 0 | } |
1008 | 0 | if let Some(delay) = task.next_recent_connection_restore.as_mut() { |
1009 | 0 | delay.await; |
1010 | 0 | task.next_recent_connection_restore = None; |
1011 | 0 | WakeUpReason::NextRecentConnectionRestore |
1012 | | } else { |
1013 | 0 | future::pending().await |
1014 | | } |
1015 | 0 | }; Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s2_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s2_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s2_0B8_ |
1016 | 0 | let finished_sending_event = async { |
1017 | 0 | if let either::Right(event_sending_future) = &mut task.event_senders { |
1018 | 0 | let event_senders = event_sending_future.await; |
1019 | 0 | task.event_senders = either::Left(event_senders); |
1020 | 0 | WakeUpReason::EventSendersReady |
1021 | 0 | } else if task.event_pending_send.is_some() |
1022 | 0 | || !task.pending_new_subscriptions.is_empty() |
1023 | | { |
1024 | 0 | WakeUpReason::EventSendersReady |
1025 | | } else { |
1026 | 0 | future::pending().await |
1027 | | } |
1028 | 0 | }; Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s3_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s3_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s3_0B8_ |
1029 | 0 | let start_discovery = async { |
1030 | 0 | let Some(mut next_discovery) = task.chains_by_next_discovery.first_entry() else { |
1031 | 0 | future::pending().await |
1032 | | }; |
1033 | 0 | next_discovery.get_mut().await; |
1034 | 0 | let ((_, chain_id), _) = next_discovery.remove_entry(); |
1035 | 0 | WakeUpReason::StartDiscovery(chain_id) |
1036 | 0 | }; Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s4_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s4_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s4_0B8_ |
1037 | | |
1038 | 0 | message_for_chain_received |
1039 | 0 | .or(message_received) |
1040 | 0 | .or(message_from_task_received) |
1041 | 0 | .or(service_event) |
1042 | 0 | .or(next_recent_connection_restore) |
1043 | 0 | .or(finished_sending_event) |
1044 | 0 | .or(start_discovery) |
1045 | 0 | .await |
1046 | | }; |
1047 | | |
1048 | 0 | match wake_up_reason { |
1049 | | WakeUpReason::ForegroundClosed => { |
1050 | | // End the task. |
1051 | 0 | return; |
1052 | | } |
1053 | | WakeUpReason::Message(ToBackground::AddChain { |
1054 | 0 | messages_rx, |
1055 | 0 | config, |
1056 | 0 | }) => { |
1057 | 0 | // TODO: this is not a completely clean way of handling duplicate chains, because the existing chain might have a different best block and role and all ; also, multiple sync services will call set_best_block and set_finalized_block |
1058 | 0 | let chain_id = match task.network.add_chain(config) { |
1059 | 0 | Ok(id) => id, |
1060 | 0 | Err(service::AddChainError::Duplicate { existing_identical }) => { |
1061 | 0 | task.network[existing_identical].num_references = task.network |
1062 | 0 | [existing_identical] |
1063 | 0 | .num_references |
1064 | 0 | .checked_add(1) |
1065 | 0 | .unwrap(); |
1066 | 0 | existing_identical |
1067 | | } |
1068 | | }; |
1069 | | |
1070 | 0 | task.chains_by_next_discovery.insert( |
1071 | 0 | (task.network[chain_id].next_discovery_when.clone(), chain_id), |
1072 | 0 | Box::pin( |
1073 | 0 | task.platform |
1074 | 0 | .sleep_until(task.network[chain_id].next_discovery_when.clone()), |
1075 | 0 | ), |
1076 | 0 | ); |
1077 | 0 |
|
1078 | 0 | task.messages_rx |
1079 | 0 | .push(Box::pin( |
1080 | 0 | messages_rx |
1081 | 0 | .map(move |msg| (chain_id, msg)) Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s5_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s5_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s5_0B8_ |
1082 | 0 | .chain(stream::once(future::ready(( |
1083 | 0 | chain_id, |
1084 | 0 | ToBackgroundChain::RemoveChain, |
1085 | 0 | )))), |
1086 | 0 | ) as Pin<Box<_>>); |
1087 | 0 |
|
1088 | 0 | log!( |
1089 | 0 | &task.platform, |
1090 | 0 | Debug, |
1091 | 0 | "network", |
1092 | 0 | "chain-added", |
1093 | 0 | id = task.network[chain_id].log_name |
1094 | 0 | ); |
1095 | | } |
1096 | | WakeUpReason::EventSendersReady => { |
1097 | | // Dispatch the pending event, if any, to the various senders. |
1098 | | |
1099 | | // We made sure that the senders were ready before generating an event. |
1100 | 0 | let either::Left(event_senders) = &mut task.event_senders else { |
1101 | 0 | unreachable!() |
1102 | | }; |
1103 | | |
1104 | 0 | if let Some((event_to_dispatch_chain_id, event_to_dispatch)) = |
1105 | 0 | task.event_pending_send.take() |
1106 | 0 | { |
1107 | 0 | let mut event_senders = mem::take(event_senders); |
1108 | 0 | task.event_senders = either::Right(Box::pin(async move { |
1109 | | // Elements in `event_senders` are removed one by one and inserted |
1110 | | // back if the channel is still open. |
1111 | 0 | for index in (0..event_senders.len()).rev() { |
1112 | 0 | let (event_sender_chain_id, event_sender) = |
1113 | 0 | event_senders.swap_remove(index); |
1114 | 0 | if event_sender_chain_id == event_to_dispatch_chain_id { |
1115 | 0 | if event_sender.send(event_to_dispatch.clone()).await.is_err() { |
1116 | 0 | continue; |
1117 | 0 | } |
1118 | 0 | } |
1119 | 0 | event_senders.push((event_sender_chain_id, event_sender)); |
1120 | | } |
1121 | 0 | event_senders |
1122 | 0 | })); Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s6_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s6_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s6_0B8_ |
1123 | 0 | } else if !task.pending_new_subscriptions.is_empty() { |
1124 | 0 | let pending_new_subscriptions = mem::take(&mut task.pending_new_subscriptions); |
1125 | 0 | let mut event_senders = mem::take(event_senders); |
1126 | 0 | // TODO: cloning :-/ |
1127 | 0 | let open_gossip_links = task.open_gossip_links.clone(); |
1128 | 0 | task.event_senders = either::Right(Box::pin(async move { |
1129 | 0 | for (chain_id, new_subscription) in pending_new_subscriptions { |
1130 | 0 | for ((link_chain_id, peer_id), state) in &open_gossip_links { |
1131 | | // TODO: optimize? this is O(n) by chain |
1132 | 0 | if *link_chain_id != chain_id { |
1133 | 0 | continue; |
1134 | 0 | } |
1135 | 0 |
|
1136 | 0 | let _ = new_subscription |
1137 | 0 | .send(Event::Connected { |
1138 | 0 | peer_id: peer_id.clone(), |
1139 | 0 | role: state.role, |
1140 | 0 | best_block_number: state.best_block_number, |
1141 | 0 | best_block_hash: state.best_block_hash, |
1142 | 0 | }) |
1143 | 0 | .await; |
1144 | | |
1145 | 0 | if let Some(finalized_block_height) = state.finalized_block_height { |
1146 | 0 | let _ = new_subscription |
1147 | 0 | .send(Event::GrandpaNeighborPacket { |
1148 | 0 | peer_id: peer_id.clone(), |
1149 | 0 | finalized_block_height, |
1150 | 0 | }) |
1151 | 0 | .await; |
1152 | 0 | } |
1153 | | } |
1154 | | |
1155 | 0 | event_senders.push((chain_id, new_subscription)); |
1156 | | } |
1157 | | |
1158 | 0 | event_senders |
1159 | 0 | })); Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s7_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s7_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s7_0B8_ |
1160 | 0 | } |
1161 | | } |
1162 | | WakeUpReason::MessageFromConnection { |
1163 | 0 | connection_id, |
1164 | 0 | message, |
1165 | 0 | } => { |
1166 | 0 | task.network |
1167 | 0 | .inject_connection_message(connection_id, message); |
1168 | 0 | } |
1169 | 0 | WakeUpReason::MessageForChain(chain_id, ToBackgroundChain::RemoveChain) => { |
1170 | 0 | if let Some(new_ref) = |
1171 | 0 | NonZeroUsize::new(task.network[chain_id].num_references.get() - 1) |
1172 | | { |
1173 | 0 | task.network[chain_id].num_references = new_ref; |
1174 | 0 | continue; |
1175 | 0 | } |
1176 | | |
1177 | 0 | for peer_id in task |
1178 | 0 | .network |
1179 | 0 | .gossip_connected_peers(chain_id, service::GossipKind::ConsensusTransactions) |
1180 | 0 | .cloned() |
1181 | 0 | .collect::<Vec<_>>() |
1182 | | { |
1183 | 0 | task.network |
1184 | 0 | .gossip_close( |
1185 | 0 | chain_id, |
1186 | 0 | &peer_id, |
1187 | 0 | service::GossipKind::ConsensusTransactions, |
1188 | 0 | ) |
1189 | 0 | .unwrap(); |
1190 | 0 |
|
1191 | 0 | let _was_in = task.open_gossip_links.remove(&(chain_id, peer_id)); |
1192 | 0 | debug_assert!(_was_in.is_some()); |
1193 | | } |
1194 | | |
1195 | 0 | let _was_in = task |
1196 | 0 | .chains_by_next_discovery |
1197 | 0 | .remove(&(task.network[chain_id].next_discovery_when.clone(), chain_id)); |
1198 | 0 | debug_assert!(_was_in.is_some()); |
1199 | | |
1200 | 0 | log!( |
1201 | 0 | &task.platform, |
1202 | 0 | Debug, |
1203 | 0 | "network", |
1204 | 0 | "chain-removed", |
1205 | 0 | id = task.network[chain_id].log_name |
1206 | 0 | ); |
1207 | 0 | task.network.remove_chain(chain_id).unwrap(); |
1208 | 0 | task.peering_strategy.remove_chain_peers(&chain_id); |
1209 | | } |
1210 | 0 | WakeUpReason::MessageForChain(chain_id, ToBackgroundChain::Subscribe { sender }) => { |
1211 | 0 | task.pending_new_subscriptions.push((chain_id, sender)); |
1212 | 0 | } |
1213 | | WakeUpReason::MessageForChain( |
1214 | 0 | chain_id, |
1215 | 0 | ToBackgroundChain::DisconnectAndBan { |
1216 | 0 | peer_id, |
1217 | 0 | severity, |
1218 | 0 | reason, |
1219 | | }, |
1220 | | ) => { |
1221 | 0 | let ban_duration = Duration::from_secs(match severity { |
1222 | 0 | BanSeverity::Low => 10, |
1223 | 0 | BanSeverity::High => 40, |
1224 | | }); |
1225 | | |
1226 | 0 | let had_slot = matches!( |
1227 | 0 | task.peering_strategy.unassign_slot_and_ban( |
1228 | 0 | &chain_id, |
1229 | 0 | &peer_id, |
1230 | 0 | task.platform.now() + ban_duration, |
1231 | 0 | ), |
1232 | | basic_peering_strategy::UnassignSlotAndBan::Banned { had_slot: true } |
1233 | | ); |
1234 | | |
1235 | 0 | if had_slot { |
1236 | 0 | log!( |
1237 | 0 | &task.platform, |
1238 | 0 | Debug, |
1239 | 0 | "network", |
1240 | 0 | "slot-unassigned", |
1241 | 0 | chain = &task.network[chain_id].log_name, |
1242 | 0 | peer_id, |
1243 | 0 | ?ban_duration, |
1244 | 0 | reason = "user-ban", |
1245 | 0 | user_reason = reason |
1246 | 0 | ); |
1247 | 0 | task.network.gossip_remove_desired( |
1248 | 0 | chain_id, |
1249 | 0 | &peer_id, |
1250 | 0 | service::GossipKind::ConsensusTransactions, |
1251 | 0 | ); |
1252 | 0 | } |
1253 | | |
1254 | 0 | if task.network.gossip_is_connected( |
1255 | 0 | chain_id, |
1256 | 0 | &peer_id, |
1257 | 0 | service::GossipKind::ConsensusTransactions, |
1258 | 0 | ) { |
1259 | 0 | let _closed_result = task.network.gossip_close( |
1260 | 0 | chain_id, |
1261 | 0 | &peer_id, |
1262 | 0 | service::GossipKind::ConsensusTransactions, |
1263 | 0 | ); |
1264 | 0 | debug_assert!(_closed_result.is_ok()); |
1265 | | |
1266 | 0 | log!( |
1267 | 0 | &task.platform, |
1268 | 0 | Debug, |
1269 | 0 | "network", |
1270 | 0 | "gossip-closed", |
1271 | 0 | chain = &task.network[chain_id].log_name, |
1272 | 0 | peer_id, |
1273 | 0 | ); |
1274 | 0 |
|
1275 | 0 | let _was_in = task.open_gossip_links.remove(&(chain_id, peer_id.clone())); |
1276 | 0 | debug_assert!(_was_in.is_some()); |
1277 | | |
1278 | 0 | debug_assert!(task.event_pending_send.is_none()); |
1279 | 0 | task.event_pending_send = Some((chain_id, Event::Disconnected { peer_id })); |
1280 | 0 | } |
1281 | | } |
1282 | | WakeUpReason::MessageForChain( |
1283 | 0 | chain_id, |
1284 | 0 | ToBackgroundChain::StartBlocksRequest { |
1285 | 0 | target, |
1286 | 0 | config, |
1287 | 0 | timeout, |
1288 | 0 | result, |
1289 | 0 | }, |
1290 | 0 | ) => { |
1291 | 0 | match &config.start { |
1292 | 0 | codec::BlocksRequestConfigStart::Hash(hash) => { |
1293 | 0 | log!( |
1294 | 0 | &task.platform, |
1295 | 0 | Debug, |
1296 | 0 | "network", |
1297 | 0 | "blocks-request-started", |
1298 | 0 | chain = task.network[chain_id].log_name, target, |
1299 | 0 | start = HashDisplay(hash), |
1300 | 0 | num = config.desired_count.get(), |
1301 | 0 | descending = ?matches!(config.direction, codec::BlocksRequestDirection::Descending), |
1302 | | header = ?config.fields.header, body = ?config.fields.body, |
1303 | | justifications = ?config.fields.justifications |
1304 | | ); |
1305 | | } |
1306 | 0 | codec::BlocksRequestConfigStart::Number(number) => { |
1307 | 0 | log!( |
1308 | 0 | &task.platform, |
1309 | 0 | Debug, |
1310 | 0 | "network", |
1311 | 0 | "blocks-request-started", |
1312 | 0 | chain = task.network[chain_id].log_name, target, start = number, |
1313 | 0 | num = config.desired_count.get(), |
1314 | 0 | descending = ?matches!(config.direction, codec::BlocksRequestDirection::Descending), |
1315 | | header = ?config.fields.header, body = ?config.fields.body, justifications = ?config.fields.justifications |
1316 | | ); |
1317 | | } |
1318 | | } |
1319 | | |
1320 | 0 | match task |
1321 | 0 | .network |
1322 | 0 | .start_blocks_request(&target, chain_id, config.clone(), timeout) |
1323 | | { |
1324 | 0 | Ok(substream_id) => { |
1325 | 0 | task.blocks_requests.insert(substream_id, result); |
1326 | 0 | } |
1327 | 0 | Err(service::StartRequestError::NoConnection) => { |
1328 | 0 | log!( |
1329 | 0 | &task.platform, |
1330 | 0 | Debug, |
1331 | 0 | "network", |
1332 | 0 | "blocks-request-error", |
1333 | 0 | chain = task.network[chain_id].log_name, |
1334 | 0 | target, |
1335 | 0 | error = "NoConnection" |
1336 | 0 | ); |
1337 | 0 | let _ = result.send(Err(BlocksRequestError::NoConnection)); |
1338 | 0 | } |
1339 | | } |
1340 | | } |
1341 | | WakeUpReason::MessageForChain( |
1342 | 0 | chain_id, |
1343 | 0 | ToBackgroundChain::StartWarpSyncRequest { |
1344 | 0 | target, |
1345 | 0 | begin_hash, |
1346 | 0 | timeout, |
1347 | 0 | result, |
1348 | 0 | }, |
1349 | 0 | ) => { |
1350 | 0 | log!( |
1351 | 0 | &task.platform, |
1352 | 0 | Debug, |
1353 | 0 | "network", |
1354 | 0 | "warp-sync-request-started", |
1355 | 0 | chain = task.network[chain_id].log_name, |
1356 | 0 | target, |
1357 | 0 | start = HashDisplay(&begin_hash) |
1358 | 0 | ); |
1359 | 0 |
|
1360 | 0 | match task |
1361 | 0 | .network |
1362 | 0 | .start_grandpa_warp_sync_request(&target, chain_id, begin_hash, timeout) |
1363 | | { |
1364 | 0 | Ok(substream_id) => { |
1365 | 0 | task.grandpa_warp_sync_requests.insert(substream_id, result); |
1366 | 0 | } |
1367 | 0 | Err(service::StartRequestError::NoConnection) => { |
1368 | 0 | log!( |
1369 | 0 | &task.platform, |
1370 | 0 | Debug, |
1371 | 0 | "network", |
1372 | 0 | "warp-sync-request-error", |
1373 | 0 | chain = task.network[chain_id].log_name, |
1374 | 0 | target, |
1375 | 0 | error = "NoConnection" |
1376 | 0 | ); |
1377 | 0 | let _ = result.send(Err(WarpSyncRequestError::NoConnection)); |
1378 | 0 | } |
1379 | | } |
1380 | | } |
1381 | | WakeUpReason::MessageForChain( |
1382 | 0 | chain_id, |
1383 | 0 | ToBackgroundChain::StartStorageProofRequest { |
1384 | 0 | target, |
1385 | 0 | config, |
1386 | 0 | timeout, |
1387 | 0 | result, |
1388 | 0 | }, |
1389 | 0 | ) => { |
1390 | 0 | log!( |
1391 | 0 | &task.platform, |
1392 | 0 | Debug, |
1393 | 0 | "network", |
1394 | 0 | "storage-proof-request-started", |
1395 | 0 | chain = task.network[chain_id].log_name, |
1396 | 0 | target, |
1397 | 0 | block_hash = HashDisplay(&config.block_hash) |
1398 | 0 | ); |
1399 | 0 |
|
1400 | 0 | match task.network.start_storage_proof_request( |
1401 | 0 | &target, |
1402 | 0 | chain_id, |
1403 | 0 | config.clone(), |
1404 | 0 | timeout, |
1405 | 0 | ) { |
1406 | 0 | Ok(substream_id) => { |
1407 | 0 | task.storage_proof_requests.insert(substream_id, result); |
1408 | 0 | } |
1409 | 0 | Err(service::StartRequestMaybeTooLargeError::NoConnection) => { |
1410 | 0 | log!( |
1411 | 0 | &task.platform, |
1412 | 0 | Debug, |
1413 | 0 | "network", |
1414 | 0 | "storage-proof-request-error", |
1415 | 0 | chain = task.network[chain_id].log_name, |
1416 | 0 | target, |
1417 | 0 | error = "NoConnection" |
1418 | 0 | ); |
1419 | 0 | let _ = result.send(Err(StorageProofRequestError::NoConnection)); |
1420 | 0 | } |
1421 | 0 | Err(service::StartRequestMaybeTooLargeError::RequestTooLarge) => { |
1422 | 0 | log!( |
1423 | 0 | &task.platform, |
1424 | 0 | Debug, |
1425 | 0 | "network", |
1426 | 0 | "storage-proof-request-error", |
1427 | 0 | chain = task.network[chain_id].log_name, |
1428 | 0 | target, |
1429 | 0 | error = "RequestTooLarge" |
1430 | 0 | ); |
1431 | 0 | let _ = result.send(Err(StorageProofRequestError::RequestTooLarge)); |
1432 | 0 | } |
1433 | | }; |
1434 | | } |
1435 | | WakeUpReason::MessageForChain( |
1436 | 0 | chain_id, |
1437 | 0 | ToBackgroundChain::StartCallProofRequest { |
1438 | 0 | target, |
1439 | 0 | config, |
1440 | 0 | timeout, |
1441 | 0 | result, |
1442 | 0 | }, |
1443 | 0 | ) => { |
1444 | 0 | log!( |
1445 | 0 | &task.platform, |
1446 | 0 | Debug, |
1447 | 0 | "network", |
1448 | 0 | "call-proof-request-started", |
1449 | 0 | chain = task.network[chain_id].log_name, |
1450 | 0 | target, |
1451 | 0 | block_hash = HashDisplay(&config.block_hash), |
1452 | 0 | function = config.method |
1453 | 0 | ); |
1454 | 0 | // TODO: log parameter |
1455 | 0 |
|
1456 | 0 | match task.network.start_call_proof_request( |
1457 | 0 | &target, |
1458 | 0 | chain_id, |
1459 | 0 | config.clone(), |
1460 | 0 | timeout, |
1461 | 0 | ) { |
1462 | 0 | Ok(substream_id) => { |
1463 | 0 | task.call_proof_requests.insert(substream_id, result); |
1464 | 0 | } |
1465 | 0 | Err(service::StartRequestMaybeTooLargeError::NoConnection) => { |
1466 | 0 | log!( |
1467 | 0 | &task.platform, |
1468 | 0 | Debug, |
1469 | 0 | "network", |
1470 | 0 | "call-proof-request-error", |
1471 | 0 | chain = task.network[chain_id].log_name, |
1472 | 0 | target, |
1473 | 0 | error = "NoConnection" |
1474 | 0 | ); |
1475 | 0 | let _ = result.send(Err(CallProofRequestError::NoConnection)); |
1476 | 0 | } |
1477 | 0 | Err(service::StartRequestMaybeTooLargeError::RequestTooLarge) => { |
1478 | 0 | log!( |
1479 | 0 | &task.platform, |
1480 | 0 | Debug, |
1481 | 0 | "network", |
1482 | 0 | "call-proof-request-error", |
1483 | 0 | chain = task.network[chain_id].log_name, |
1484 | 0 | target, |
1485 | 0 | error = "RequestTooLarge" |
1486 | 0 | ); |
1487 | 0 | let _ = result.send(Err(CallProofRequestError::RequestTooLarge)); |
1488 | 0 | } |
1489 | | }; |
1490 | | } |
1491 | | WakeUpReason::MessageForChain( |
1492 | 0 | chain_id, |
1493 | 0 | ToBackgroundChain::SetLocalBestBlock { |
1494 | 0 | best_hash, |
1495 | 0 | best_number, |
1496 | 0 | }, |
1497 | 0 | ) => { |
1498 | 0 | task.network |
1499 | 0 | .set_chain_local_best_block(chain_id, best_hash, best_number); |
1500 | 0 | } |
1501 | | WakeUpReason::MessageForChain( |
1502 | 0 | chain_id, |
1503 | 0 | ToBackgroundChain::SetLocalGrandpaState { grandpa_state }, |
1504 | 0 | ) => { |
1505 | 0 | log!( |
1506 | 0 | &task.platform, |
1507 | 0 | Debug, |
1508 | 0 | "network", |
1509 | 0 | "local-grandpa-state-announced", |
1510 | 0 | chain = task.network[chain_id].log_name, |
1511 | 0 | set_id = grandpa_state.set_id, |
1512 | 0 | commit_finalized_height = grandpa_state.commit_finalized_height, |
1513 | 0 | ); |
1514 | 0 |
|
1515 | 0 | // TODO: log the list of peers we sent the packet to |
1516 | 0 |
|
1517 | 0 | task.network |
1518 | 0 | .gossip_broadcast_grandpa_state_and_update(chain_id, grandpa_state); |
1519 | 0 | } |
1520 | | WakeUpReason::MessageForChain( |
1521 | 0 | chain_id, |
1522 | 0 | ToBackgroundChain::AnnounceTransaction { |
1523 | 0 | transaction, |
1524 | 0 | result, |
1525 | 0 | }, |
1526 | 0 | ) => { |
1527 | 0 | // TODO: keep track of which peer knows about which transaction, and don't send it again |
1528 | 0 |
|
1529 | 0 | let peers_to_send = task |
1530 | 0 | .network |
1531 | 0 | .gossip_connected_peers(chain_id, service::GossipKind::ConsensusTransactions) |
1532 | 0 | .cloned() |
1533 | 0 | .collect::<Vec<_>>(); |
1534 | 0 |
|
1535 | 0 | let mut peers_sent = Vec::with_capacity(peers_to_send.len()); |
1536 | 0 | let mut peers_queue_full = Vec::with_capacity(peers_to_send.len()); |
1537 | 0 | for peer in &peers_to_send { |
1538 | 0 | match task |
1539 | 0 | .network |
1540 | 0 | .gossip_send_transaction(&peer, chain_id, &transaction) |
1541 | | { |
1542 | 0 | Ok(()) => peers_sent.push(peer.to_base58()), |
1543 | | Err(QueueNotificationError::QueueFull) => { |
1544 | 0 | peers_queue_full.push(peer.to_base58()) |
1545 | | } |
1546 | 0 | Err(QueueNotificationError::NoConnection) => unreachable!(), |
1547 | | } |
1548 | | } |
1549 | | |
1550 | 0 | log!( |
1551 | 0 | &task.platform, |
1552 | 0 | Debug, |
1553 | 0 | "network", |
1554 | 0 | "transaction-announced", |
1555 | 0 | chain = task.network[chain_id].log_name, |
1556 | 0 | transaction = |
1557 | 0 | hex::encode(blake2_rfc::blake2b::blake2b(32, &[], &transaction).as_bytes()), |
1558 | 0 | size = transaction.len(), |
1559 | 0 | peers_sent = peers_sent.join(", "), |
1560 | 0 | peers_queue_full = peers_queue_full.join(", "), |
1561 | 0 | ); |
1562 | 0 |
|
1563 | 0 | let _ = result.send(peers_to_send); |
1564 | | } |
1565 | | WakeUpReason::MessageForChain( |
1566 | 0 | chain_id, |
1567 | 0 | ToBackgroundChain::SendBlockAnnounce { |
1568 | 0 | target, |
1569 | 0 | scale_encoded_header, |
1570 | 0 | is_best, |
1571 | 0 | result, |
1572 | 0 | }, |
1573 | 0 | ) => { |
1574 | 0 | // TODO: log who the announce was sent to |
1575 | 0 | let _ = result.send(task.network.gossip_send_block_announce( |
1576 | 0 | &target, |
1577 | 0 | chain_id, |
1578 | 0 | &scale_encoded_header, |
1579 | 0 | is_best, |
1580 | 0 | )); |
1581 | 0 | } |
1582 | | WakeUpReason::MessageForChain( |
1583 | 0 | chain_id, |
1584 | 0 | ToBackgroundChain::Discover { |
1585 | 0 | list, |
1586 | 0 | important_nodes, |
1587 | | }, |
1588 | | ) => { |
1589 | 0 | for (peer_id, addrs) in list { |
1590 | 0 | if important_nodes { |
1591 | 0 | task.important_nodes.insert(peer_id.clone()); |
1592 | 0 | } |
1593 | | |
1594 | | // Note that we must call this function before `insert_address`, as documented |
1595 | | // in `basic_peering_strategy`. |
1596 | 0 | task.peering_strategy |
1597 | 0 | .insert_chain_peer(chain_id, peer_id.clone(), 30); // TODO: constant |
1598 | | |
1599 | 0 | for addr in addrs { |
1600 | 0 | let _ = |
1601 | 0 | task.peering_strategy |
1602 | 0 | .insert_address(&peer_id, addr.into_bytes(), 10); |
1603 | 0 | // TODO: constant |
1604 | 0 | } |
1605 | | } |
1606 | | } |
1607 | | WakeUpReason::MessageForChain( |
1608 | 0 | chain_id, |
1609 | 0 | ToBackgroundChain::DiscoveredNodes { result }, |
1610 | 0 | ) => { |
1611 | 0 | // TODO: consider returning Vec<u8>s for the addresses? |
1612 | 0 | let _ = result.send( |
1613 | 0 | task.peering_strategy |
1614 | 0 | .chain_peers_unordered(&chain_id) |
1615 | 0 | .map(|peer_id| { |
1616 | 0 | let addrs = task |
1617 | 0 | .peering_strategy |
1618 | 0 | .peer_addresses(peer_id) |
1619 | 0 | .map(|a| Multiaddr::from_bytes(a.to_owned()).unwrap()) Unexecuted instantiation: _RNCNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s8_00Ba_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s8_00B1f_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s8_00Ba_ |
1620 | 0 | .collect::<Vec<_>>(); |
1621 | 0 | (peer_id.clone(), addrs) |
1622 | 0 | }) Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s8_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s8_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s8_0B8_ |
1623 | 0 | .collect::<Vec<_>>(), |
1624 | 0 | ); |
1625 | 0 | } |
1626 | 0 | WakeUpReason::MessageForChain(chain_id, ToBackgroundChain::PeersList { result }) => { |
1627 | 0 | let _ = result.send( |
1628 | 0 | task.network |
1629 | 0 | .gossip_connected_peers( |
1630 | 0 | chain_id, |
1631 | 0 | service::GossipKind::ConsensusTransactions, |
1632 | 0 | ) |
1633 | 0 | .cloned() |
1634 | 0 | .collect(), |
1635 | 0 | ); |
1636 | 0 | } |
1637 | 0 | WakeUpReason::StartDiscovery(chain_id) => { |
1638 | 0 | // Re-insert the chain in `chains_by_next_discovery`. |
1639 | 0 | let chain = &mut task.network[chain_id]; |
1640 | 0 | chain.next_discovery_when = task.platform.now() + chain.next_discovery_period; |
1641 | 0 | chain.next_discovery_period = |
1642 | 0 | cmp::min(chain.next_discovery_period * 2, Duration::from_secs(120)); |
1643 | 0 | task.chains_by_next_discovery.insert( |
1644 | 0 | (chain.next_discovery_when.clone(), chain_id), |
1645 | 0 | Box::pin( |
1646 | 0 | task.platform |
1647 | 0 | .sleep(task.network[chain_id].next_discovery_period), |
1648 | 0 | ), |
1649 | 0 | ); |
1650 | 0 |
|
1651 | 0 | let random_peer_id = { |
1652 | 0 | let mut pub_key = [0; 32]; |
1653 | 0 | rand_chacha::rand_core::RngCore::fill_bytes(&mut task.randomness, &mut pub_key); |
1654 | 0 | PeerId::from_public_key(&peer_id::PublicKey::Ed25519(pub_key)) |
1655 | 0 | }; |
1656 | 0 |
|
1657 | 0 | // TODO: select target closest to the random peer instead |
1658 | 0 | let target = task |
1659 | 0 | .network |
1660 | 0 | .gossip_connected_peers(chain_id, service::GossipKind::ConsensusTransactions) |
1661 | 0 | .next() |
1662 | 0 | .cloned(); |
1663 | | |
1664 | 0 | if let Some(target) = target { |
1665 | 0 | match task.network.start_kademlia_find_node_request( |
1666 | 0 | &target, |
1667 | 0 | chain_id, |
1668 | 0 | &random_peer_id, |
1669 | 0 | Duration::from_secs(20), |
1670 | 0 | ) { |
1671 | 0 | Ok(_) => {} |
1672 | 0 | Err(service::StartRequestError::NoConnection) => unreachable!(), |
1673 | | }; |
1674 | | |
1675 | 0 | log!( |
1676 | 0 | &task.platform, |
1677 | 0 | Debug, |
1678 | 0 | "network", |
1679 | 0 | "discovery-find-node-started", |
1680 | 0 | chain = &task.network[chain_id].log_name, |
1681 | 0 | request_target = target, |
1682 | 0 | requested_peer_id = random_peer_id |
1683 | 0 | ); |
1684 | 0 | } else { |
1685 | 0 | log!( |
1686 | 0 | &task.platform, |
1687 | 0 | Debug, |
1688 | 0 | "network", |
1689 | 0 | "discovery-skipped-no-peer", |
1690 | 0 | chain = &task.network[chain_id].log_name |
1691 | 0 | ); |
1692 | 0 | } |
1693 | | } |
1694 | | WakeUpReason::NetworkEvent(service::Event::HandshakeFinished { |
1695 | 0 | peer_id, |
1696 | 0 | expected_peer_id, |
1697 | 0 | id, |
1698 | 0 | }) => { |
1699 | 0 | let remote_addr = |
1700 | 0 | Multiaddr::from_bytes(task.network.connection_remote_addr(id)).unwrap(); // TODO: review this unwrap |
1701 | 0 | if let Some(expected_peer_id) = expected_peer_id.as_ref().filter(|p| **p != peer_id) Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0s9_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0s9_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0s9_0B8_ |
1702 | | { |
1703 | 0 | log!( |
1704 | 0 | &task.platform, |
1705 | 0 | Debug, |
1706 | 0 | "network", |
1707 | 0 | "handshake-finished-peer-id-mismatch", |
1708 | 0 | remote_addr, |
1709 | 0 | expected_peer_id, |
1710 | 0 | actual_peer_id = peer_id |
1711 | 0 | ); |
1712 | 0 |
|
1713 | 0 | let _was_in = task |
1714 | 0 | .peering_strategy |
1715 | 0 | .decrease_address_connections_and_remove_if_zero( |
1716 | 0 | expected_peer_id, |
1717 | 0 | remote_addr.as_ref(), |
1718 | 0 | ); |
1719 | 0 | debug_assert!(_was_in.is_ok()); |
1720 | 0 | let _ = task.peering_strategy.increase_address_connections( |
1721 | 0 | &peer_id, |
1722 | 0 | remote_addr.into_bytes().to_vec(), |
1723 | 0 | 10, |
1724 | 0 | ); |
1725 | 0 | } else { |
1726 | 0 | log!( |
1727 | 0 | &task.platform, |
1728 | 0 | Debug, |
1729 | 0 | "network", |
1730 | 0 | "handshake-finished", |
1731 | 0 | remote_addr, |
1732 | 0 | peer_id |
1733 | 0 | ); |
1734 | 0 | } |
1735 | | } |
1736 | | WakeUpReason::NetworkEvent(service::Event::PreHandshakeDisconnected { |
1737 | | expected_peer_id: Some(_), |
1738 | | .. |
1739 | | }) |
1740 | | | WakeUpReason::NetworkEvent(service::Event::Disconnected { .. }) => { |
1741 | 0 | let (address, peer_id, handshake_finished) = match wake_up_reason { |
1742 | | WakeUpReason::NetworkEvent(service::Event::PreHandshakeDisconnected { |
1743 | 0 | address, |
1744 | 0 | expected_peer_id: Some(peer_id), |
1745 | 0 | .. |
1746 | 0 | }) => (address, peer_id, false), |
1747 | | WakeUpReason::NetworkEvent(service::Event::Disconnected { |
1748 | 0 | address, |
1749 | 0 | peer_id, |
1750 | 0 | .. |
1751 | 0 | }) => (address, peer_id, true), |
1752 | 0 | _ => unreachable!(), |
1753 | | }; |
1754 | | |
1755 | 0 | task.peering_strategy |
1756 | 0 | .decrease_address_connections(&peer_id, &address) |
1757 | 0 | .unwrap(); |
1758 | 0 | let address = Multiaddr::from_bytes(address).unwrap(); |
1759 | 0 | log!( |
1760 | 0 | &task.platform, |
1761 | 0 | Debug, |
1762 | 0 | "network", |
1763 | 0 | "connection-shutdown", |
1764 | 0 | peer_id, |
1765 | 0 | address, |
1766 | 0 | ?handshake_finished |
1767 | 0 | ); |
1768 | 0 |
|
1769 | 0 | // Ban the peer in order to avoid trying over and over again the same address(es). |
1770 | 0 | // Even if the handshake was finished, it is possible that the peer simply shuts |
1771 | 0 | // down connections immediately after it has been opened, hence the ban. |
1772 | 0 | // Due to race conditions and peerid mismatches, it is possible that there is |
1773 | 0 | // another existing connection or connection attempt with that same peer. However, |
1774 | 0 | // it is not possible to be sure that we will reach 0 connections or connection |
1775 | 0 | // attempts, and thus we ban the peer every time. |
1776 | 0 | let ban_duration = Duration::from_secs(5); |
1777 | 0 | task.network.gossip_remove_desired_all( |
1778 | 0 | &peer_id, |
1779 | 0 | service::GossipKind::ConsensusTransactions, |
1780 | 0 | ); |
1781 | 0 | for (&chain_id, what_happened) in task |
1782 | 0 | .peering_strategy |
1783 | 0 | .unassign_slots_and_ban(&peer_id, task.platform.now() + ban_duration) |
1784 | | { |
1785 | 0 | if matches!( |
1786 | 0 | what_happened, |
1787 | | basic_peering_strategy::UnassignSlotsAndBan::Banned { had_slot: true } |
1788 | 0 | ) { |
1789 | 0 | log!( |
1790 | 0 | &task.platform, |
1791 | 0 | Debug, |
1792 | 0 | "network", |
1793 | 0 | "slot-unassigned", |
1794 | 0 | chain = &task.network[chain_id].log_name, |
1795 | 0 | peer_id, |
1796 | 0 | ?ban_duration, |
1797 | 0 | reason = "pre-handshake-disconnect" |
1798 | 0 | ); |
1799 | 0 | } |
1800 | | } |
1801 | | } |
1802 | | WakeUpReason::NetworkEvent(service::Event::PreHandshakeDisconnected { |
1803 | | expected_peer_id: None, |
1804 | | .. |
1805 | | }) => { |
1806 | | // This path can't be reached as we always set an expected peer id when creating |
1807 | | // a connection. |
1808 | 0 | debug_assert!(false); |
1809 | | } |
1810 | | WakeUpReason::NetworkEvent(service::Event::PingOutSuccess { |
1811 | 0 | id, |
1812 | 0 | peer_id, |
1813 | 0 | ping_time, |
1814 | 0 | }) => { |
1815 | 0 | let remote_addr = |
1816 | 0 | Multiaddr::from_bytes(task.network.connection_remote_addr(id)).unwrap(); // TODO: review this unwrap |
1817 | 0 | log!( |
1818 | 0 | &task.platform, |
1819 | 0 | Debug, |
1820 | 0 | "network", |
1821 | 0 | "pong", |
1822 | 0 | peer_id, |
1823 | 0 | remote_addr, |
1824 | 0 | ?ping_time |
1825 | 0 | ); |
1826 | 0 | } |
1827 | | WakeUpReason::NetworkEvent(service::Event::BlockAnnounce { |
1828 | 0 | chain_id, |
1829 | 0 | peer_id, |
1830 | 0 | announce, |
1831 | 0 | }) => { |
1832 | 0 | log!( |
1833 | 0 | &task.platform, |
1834 | 0 | Debug, |
1835 | 0 | "network", |
1836 | 0 | "block-announce-received", |
1837 | 0 | chain = &task.network[chain_id].log_name, |
1838 | 0 | peer_id, |
1839 | 0 | block_hash = HashDisplay(&header::hash_from_scale_encoded_header( |
1840 | 0 | announce.decode().scale_encoded_header |
1841 | 0 | )), |
1842 | 0 | is_best = announce.decode().is_best |
1843 | 0 | ); |
1844 | 0 |
|
1845 | 0 | let decoded_announce = announce.decode(); |
1846 | 0 | if decoded_announce.is_best { |
1847 | 0 | let link = task |
1848 | 0 | .open_gossip_links |
1849 | 0 | .get_mut(&(chain_id, peer_id.clone())) |
1850 | 0 | .unwrap(); |
1851 | 0 | if let Ok(decoded) = header::decode( |
1852 | 0 | &decoded_announce.scale_encoded_header, |
1853 | 0 | task.network[chain_id].block_number_bytes, |
1854 | 0 | ) { |
1855 | 0 | link.best_block_hash = header::hash_from_scale_encoded_header( |
1856 | 0 | &decoded_announce.scale_encoded_header, |
1857 | 0 | ); |
1858 | 0 | link.best_block_number = decoded.number; |
1859 | 0 | } |
1860 | 0 | } |
1861 | | |
1862 | 0 | debug_assert!(task.event_pending_send.is_none()); |
1863 | 0 | task.event_pending_send = |
1864 | 0 | Some((chain_id, Event::BlockAnnounce { peer_id, announce })); |
1865 | | } |
1866 | | WakeUpReason::NetworkEvent(service::Event::GossipConnected { |
1867 | 0 | peer_id, |
1868 | 0 | chain_id, |
1869 | 0 | role, |
1870 | 0 | best_number, |
1871 | 0 | best_hash, |
1872 | 0 | kind: service::GossipKind::ConsensusTransactions, |
1873 | 0 | }) => { |
1874 | 0 | log!( |
1875 | 0 | &task.platform, |
1876 | 0 | Debug, |
1877 | 0 | "network", |
1878 | 0 | "gossip-open-success", |
1879 | 0 | chain = &task.network[chain_id].log_name, |
1880 | 0 | peer_id, |
1881 | 0 | best_number, |
1882 | 0 | best_hash = HashDisplay(&best_hash) |
1883 | 0 | ); |
1884 | 0 |
|
1885 | 0 | let _prev_value = task.open_gossip_links.insert( |
1886 | 0 | (chain_id, peer_id.clone()), |
1887 | 0 | OpenGossipLinkState { |
1888 | 0 | best_block_number: best_number, |
1889 | 0 | best_block_hash: best_hash, |
1890 | 0 | role, |
1891 | 0 | finalized_block_height: None, |
1892 | 0 | }, |
1893 | 0 | ); |
1894 | 0 | debug_assert!(_prev_value.is_none()); |
1895 | | |
1896 | 0 | debug_assert!(task.event_pending_send.is_none()); |
1897 | 0 | task.event_pending_send = Some(( |
1898 | 0 | chain_id, |
1899 | 0 | Event::Connected { |
1900 | 0 | peer_id, |
1901 | 0 | role, |
1902 | 0 | best_block_number: best_number, |
1903 | 0 | best_block_hash: best_hash, |
1904 | 0 | }, |
1905 | 0 | )); |
1906 | | } |
1907 | | WakeUpReason::NetworkEvent(service::Event::GossipOpenFailed { |
1908 | 0 | peer_id, |
1909 | 0 | chain_id, |
1910 | 0 | error, |
1911 | 0 | kind: service::GossipKind::ConsensusTransactions, |
1912 | 0 | }) => { |
1913 | 0 | log!( |
1914 | 0 | &task.platform, |
1915 | 0 | Debug, |
1916 | 0 | "network", |
1917 | 0 | "gossip-open-error", |
1918 | 0 | chain = &task.network[chain_id].log_name, |
1919 | 0 | peer_id, |
1920 | 0 | ?error, |
1921 | 0 | ); |
1922 | 0 | let ban_duration = Duration::from_secs(15); |
1923 | | |
1924 | | // Note that peer doesn't necessarily have an out slot, as this event might happen |
1925 | | // as a result of an inbound gossip connection. |
1926 | 0 | let had_slot = if let service::GossipConnectError::GenesisMismatch { .. } = error { |
1927 | 0 | matches!( |
1928 | 0 | task.peering_strategy |
1929 | 0 | .unassign_slot_and_remove_chain_peer(&chain_id, &peer_id), |
1930 | | basic_peering_strategy::UnassignSlotAndRemoveChainPeer::HadSlot |
1931 | | ) |
1932 | | } else { |
1933 | 0 | matches!( |
1934 | 0 | task.peering_strategy.unassign_slot_and_ban( |
1935 | 0 | &chain_id, |
1936 | 0 | &peer_id, |
1937 | 0 | task.platform.now() + ban_duration, |
1938 | 0 | ), |
1939 | | basic_peering_strategy::UnassignSlotAndBan::Banned { had_slot: true } |
1940 | | ) |
1941 | | }; |
1942 | | |
1943 | 0 | if had_slot { |
1944 | 0 | log!( |
1945 | 0 | &task.platform, |
1946 | 0 | Debug, |
1947 | 0 | "network", |
1948 | 0 | "slot-unassigned", |
1949 | 0 | chain = &task.network[chain_id].log_name, |
1950 | 0 | peer_id, |
1951 | 0 | ?ban_duration, |
1952 | 0 | reason = "gossip-open-failed" |
1953 | 0 | ); |
1954 | 0 | task.network.gossip_remove_desired( |
1955 | 0 | chain_id, |
1956 | 0 | &peer_id, |
1957 | 0 | service::GossipKind::ConsensusTransactions, |
1958 | 0 | ); |
1959 | 0 | } |
1960 | | } |
1961 | | WakeUpReason::NetworkEvent(service::Event::GossipDisconnected { |
1962 | 0 | peer_id, |
1963 | 0 | chain_id, |
1964 | 0 | kind: service::GossipKind::ConsensusTransactions, |
1965 | 0 | }) => { |
1966 | 0 | log!( |
1967 | 0 | &task.platform, |
1968 | 0 | Debug, |
1969 | 0 | "network", |
1970 | 0 | "gossip-closed", |
1971 | 0 | chain = &task.network[chain_id].log_name, |
1972 | 0 | peer_id, |
1973 | 0 | ); |
1974 | 0 | let ban_duration = Duration::from_secs(10); |
1975 | 0 |
|
1976 | 0 | let _was_in = task.open_gossip_links.remove(&(chain_id, peer_id.clone())); |
1977 | 0 | debug_assert!(_was_in.is_some()); |
1978 | | |
1979 | | // Note that peer doesn't necessarily have an out slot, as this event might happen |
1980 | | // as a result of an inbound gossip connection. |
1981 | 0 | if matches!( |
1982 | 0 | task.peering_strategy.unassign_slot_and_ban( |
1983 | 0 | &chain_id, |
1984 | 0 | &peer_id, |
1985 | 0 | task.platform.now() + ban_duration, |
1986 | 0 | ), |
1987 | | basic_peering_strategy::UnassignSlotAndBan::Banned { had_slot: true } |
1988 | 0 | ) { |
1989 | 0 | log!( |
1990 | 0 | &task.platform, |
1991 | 0 | Debug, |
1992 | 0 | "network", |
1993 | 0 | "slot-unassigned", |
1994 | 0 | chain = &task.network[chain_id].log_name, |
1995 | 0 | peer_id, |
1996 | 0 | ?ban_duration, |
1997 | 0 | reason = "gossip-closed" |
1998 | 0 | ); |
1999 | 0 | task.network.gossip_remove_desired( |
2000 | 0 | chain_id, |
2001 | 0 | &peer_id, |
2002 | 0 | service::GossipKind::ConsensusTransactions, |
2003 | 0 | ); |
2004 | 0 | } |
2005 | | |
2006 | 0 | debug_assert!(task.event_pending_send.is_none()); |
2007 | 0 | task.event_pending_send = Some((chain_id, Event::Disconnected { peer_id })); |
2008 | | } |
2009 | | WakeUpReason::NetworkEvent(service::Event::RequestResult { |
2010 | 0 | substream_id, |
2011 | 0 | peer_id, |
2012 | 0 | chain_id, |
2013 | 0 | response: service::RequestResult::Blocks(response), |
2014 | 0 | }) => { |
2015 | 0 | match &response { |
2016 | 0 | Ok(blocks) => { |
2017 | 0 | log!( |
2018 | 0 | &task.platform, |
2019 | 0 | Debug, |
2020 | 0 | "network", |
2021 | 0 | "blocks-request-success", |
2022 | 0 | chain = task.network[chain_id].log_name, |
2023 | 0 | target = peer_id, |
2024 | 0 | num_blocks = blocks.len(), |
2025 | 0 | block_data_total_size = |
2026 | 0 | BytesDisplay(blocks.iter().fold(0, |sum, block| { |
2027 | 0 | let block_size = block.header.as_ref().map_or(0, |h| h.len()) Unexecuted instantiation: _RNCNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0sc_00Ba_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0sc_00B1f_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0sc_00Ba_ |
2028 | 0 | + block |
2029 | 0 | .body |
2030 | 0 | .as_ref() |
2031 | 0 | .map_or(0, |b| b.iter().fold(0, |s, e| s + e.len())) Unexecuted instantiation: _RNCNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0sc_0s_0Ba_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0sc_0s_0B1f_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0sc_0s_0Ba_ Unexecuted instantiation: _RNCNCNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0sc_0s_00Bc_ Unexecuted instantiation: _RNCNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0sc_0s_00B1h_ Unexecuted instantiation: _RNCNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0sc_0s_00Bc_ |
2032 | 0 | + block |
2033 | 0 | .justifications |
2034 | 0 | .as_ref() |
2035 | 0 | .into_iter() |
2036 | 0 | .flat_map(|l| l.iter()) Unexecuted instantiation: _RNCNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0sc_0s0_0Ba_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0sc_0s0_0B1f_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0sc_0s0_0Ba_ |
2037 | 0 | .fold(0, |s, j| s + j.justification.len()); Unexecuted instantiation: _RNCNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0sc_0s1_0Ba_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0sc_0s1_0B1f_ Unexecuted instantiation: _RNCNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0sc_0s1_0Ba_ |
2038 | 0 | sum + u64::try_from(block_size).unwrap() |
2039 | 0 | })) Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0sc_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0sc_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0sc_0B8_ |
2040 | 0 | ); |
2041 | 0 | } |
2042 | 0 | Err(error) => { |
2043 | 0 | log!( |
2044 | 0 | &task.platform, |
2045 | 0 | Debug, |
2046 | 0 | "network", |
2047 | 0 | "blocks-request-error", |
2048 | 0 | chain = task.network[chain_id].log_name, |
2049 | 0 | target = peer_id, |
2050 | 0 | ?error |
2051 | 0 | ); |
2052 | 0 | } |
2053 | | } |
2054 | | |
2055 | 0 | match &response { |
2056 | 0 | Ok(_) => {} |
2057 | 0 | Err(service::BlocksRequestError::Request(err)) if !err.is_protocol_error() => {} |
2058 | 0 | Err(err) => { |
2059 | 0 | log!( |
2060 | 0 | &task.platform, |
2061 | 0 | Debug, |
2062 | 0 | "network", |
2063 | 0 | format!( |
2064 | 0 | "Error in block request with {}. This might indicate an \ |
2065 | 0 | incompatibility. Error: {}", |
2066 | 0 | peer_id, err |
2067 | 0 | ) |
2068 | 0 | ); |
2069 | 0 | } |
2070 | | } |
2071 | | |
2072 | 0 | let _ = task |
2073 | 0 | .blocks_requests |
2074 | 0 | .remove(&substream_id) |
2075 | 0 | .unwrap() |
2076 | 0 | .send(response.map_err(BlocksRequestError::Request)); |
2077 | | } |
2078 | | WakeUpReason::NetworkEvent(service::Event::RequestResult { |
2079 | 0 | substream_id, |
2080 | 0 | peer_id, |
2081 | 0 | chain_id, |
2082 | 0 | response: service::RequestResult::GrandpaWarpSync(response), |
2083 | 0 | }) => { |
2084 | 0 | match &response { |
2085 | 0 | Ok(response) => { |
2086 | 0 | // TODO: print total bytes size |
2087 | 0 | let decoded = response.decode(); |
2088 | 0 | log!( |
2089 | 0 | &task.platform, |
2090 | 0 | Debug, |
2091 | 0 | "network", |
2092 | 0 | "warp-sync-request-success", |
2093 | 0 | chain = task.network[chain_id].log_name, |
2094 | 0 | target = peer_id, |
2095 | 0 | num_fragments = decoded.fragments.len(), |
2096 | 0 | is_finished = ?decoded.is_finished, |
2097 | 0 | ); |
2098 | 0 | } |
2099 | 0 | Err(error) => { |
2100 | 0 | log!( |
2101 | 0 | &task.platform, |
2102 | 0 | Debug, |
2103 | 0 | "network", |
2104 | 0 | "warp-sync-request-error", |
2105 | 0 | chain = task.network[chain_id].log_name, |
2106 | 0 | target = peer_id, |
2107 | 0 | ?error, |
2108 | 0 | ); |
2109 | 0 | } |
2110 | | } |
2111 | | |
2112 | 0 | let _ = task |
2113 | 0 | .grandpa_warp_sync_requests |
2114 | 0 | .remove(&substream_id) |
2115 | 0 | .unwrap() |
2116 | 0 | .send(response.map_err(WarpSyncRequestError::Request)); |
2117 | | } |
2118 | | WakeUpReason::NetworkEvent(service::Event::RequestResult { |
2119 | 0 | substream_id, |
2120 | 0 | peer_id, |
2121 | 0 | chain_id, |
2122 | 0 | response: service::RequestResult::StorageProof(response), |
2123 | 0 | }) => { |
2124 | 0 | match &response { |
2125 | 0 | Ok(items) => { |
2126 | 0 | let decoded = items.decode(); |
2127 | 0 | log!( |
2128 | 0 | &task.platform, |
2129 | 0 | Debug, |
2130 | 0 | "network", |
2131 | 0 | "storage-proof-request-success", |
2132 | 0 | chain = task.network[chain_id].log_name, |
2133 | 0 | target = peer_id, |
2134 | 0 | total_size = BytesDisplay(u64::try_from(decoded.len()).unwrap()), |
2135 | 0 | ); |
2136 | 0 | } |
2137 | 0 | Err(error) => { |
2138 | 0 | log!( |
2139 | 0 | &task.platform, |
2140 | 0 | Debug, |
2141 | 0 | "network", |
2142 | 0 | "storage-proof-request-error", |
2143 | 0 | chain = task.network[chain_id].log_name, |
2144 | 0 | target = peer_id, |
2145 | 0 | ?error |
2146 | 0 | ); |
2147 | 0 | } |
2148 | | } |
2149 | | |
2150 | 0 | let _ = task |
2151 | 0 | .storage_proof_requests |
2152 | 0 | .remove(&substream_id) |
2153 | 0 | .unwrap() |
2154 | 0 | .send(response.map_err(StorageProofRequestError::Request)); |
2155 | | } |
2156 | | WakeUpReason::NetworkEvent(service::Event::RequestResult { |
2157 | 0 | substream_id, |
2158 | 0 | peer_id, |
2159 | 0 | chain_id, |
2160 | 0 | response: service::RequestResult::CallProof(response), |
2161 | 0 | }) => { |
2162 | 0 | match &response { |
2163 | 0 | Ok(items) => { |
2164 | 0 | let decoded = items.decode(); |
2165 | 0 | log!( |
2166 | 0 | &task.platform, |
2167 | 0 | Debug, |
2168 | 0 | "network", |
2169 | 0 | "call-proof-request-success", |
2170 | 0 | chain = task.network[chain_id].log_name, |
2171 | 0 | target = peer_id, |
2172 | 0 | total_size = BytesDisplay(u64::try_from(decoded.len()).unwrap()) |
2173 | 0 | ); |
2174 | 0 | } |
2175 | 0 | Err(error) => { |
2176 | 0 | log!( |
2177 | 0 | &task.platform, |
2178 | 0 | Debug, |
2179 | 0 | "network", |
2180 | 0 | "call-proof-request-error", |
2181 | 0 | chain = task.network[chain_id].log_name, |
2182 | 0 | target = peer_id, |
2183 | 0 | ?error |
2184 | 0 | ); |
2185 | 0 | } |
2186 | | } |
2187 | | |
2188 | 0 | let _ = task |
2189 | 0 | .call_proof_requests |
2190 | 0 | .remove(&substream_id) |
2191 | 0 | .unwrap() |
2192 | 0 | .send(response.map_err(CallProofRequestError::Request)); |
2193 | | } |
2194 | | WakeUpReason::NetworkEvent(service::Event::RequestResult { |
2195 | 0 | peer_id: requestee_peer_id, |
2196 | 0 | chain_id, |
2197 | 0 | response: service::RequestResult::KademliaFindNode(Ok(nodes)), |
2198 | | .. |
2199 | | }) => { |
2200 | 0 | for (peer_id, mut addrs) in nodes { |
2201 | | // Make sure to not insert too many address for a single peer. |
2202 | | // While the . |
2203 | 0 | if addrs.len() >= 10 { |
2204 | 0 | addrs.truncate(10); |
2205 | 0 | } |
2206 | | |
2207 | 0 | let mut valid_addrs = Vec::with_capacity(addrs.len()); |
2208 | 0 | for addr in addrs { |
2209 | 0 | match Multiaddr::from_bytes(addr) { |
2210 | 0 | Ok(a) => { |
2211 | 0 | if platform::address_parse::multiaddr_to_address(&a) |
2212 | 0 | .ok() |
2213 | 0 | .map_or(false, |addr| { |
2214 | 0 | task.platform.supports_connection_type((&addr).into()) |
2215 | 0 | }) Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0sa_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0sa_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0sa_0B8_ |
2216 | | { |
2217 | 0 | valid_addrs.push(a) |
2218 | 0 | } else { |
2219 | 0 | log!( |
2220 | 0 | &task.platform, |
2221 | 0 | Debug, |
2222 | 0 | "network", |
2223 | 0 | "discovered-address-not-supported", |
2224 | 0 | chain = &task.network[chain_id].log_name, |
2225 | 0 | peer_id, |
2226 | 0 | addr = &a, |
2227 | 0 | obtained_from = requestee_peer_id |
2228 | 0 | ); |
2229 | 0 | } |
2230 | | } |
2231 | 0 | Err((error, addr)) => { |
2232 | 0 | log!( |
2233 | 0 | &task.platform, |
2234 | 0 | Debug, |
2235 | 0 | "network", |
2236 | 0 | "discovered-address-invalid", |
2237 | 0 | chain = &task.network[chain_id].log_name, |
2238 | 0 | peer_id, |
2239 | 0 | error, |
2240 | 0 | addr = hex::encode(&addr), |
2241 | 0 | obtained_from = requestee_peer_id |
2242 | 0 | ); |
2243 | 0 | } |
2244 | | } |
2245 | | } |
2246 | | |
2247 | 0 | if !valid_addrs.is_empty() { |
2248 | | // Note that we must call this function before `insert_address`, |
2249 | | // as documented in `basic_peering_strategy`. |
2250 | 0 | let insert_outcome = |
2251 | 0 | task.peering_strategy |
2252 | 0 | .insert_chain_peer(chain_id, peer_id.clone(), 30); // TODO: constant |
2253 | | |
2254 | | if let basic_peering_strategy::InsertChainPeerResult::Inserted { |
2255 | 0 | peer_removed, |
2256 | 0 | } = insert_outcome |
2257 | | { |
2258 | 0 | if let Some(peer_removed) = peer_removed { |
2259 | 0 | log!( |
2260 | 0 | &task.platform, |
2261 | 0 | Debug, |
2262 | 0 | "network", |
2263 | 0 | "peer-purged-from-address-book", |
2264 | 0 | chain = &task.network[chain_id].log_name, |
2265 | 0 | peer_id = peer_removed, |
2266 | 0 | ); |
2267 | 0 | } |
2268 | | |
2269 | 0 | log!( |
2270 | 0 | &task.platform, |
2271 | 0 | Debug, |
2272 | 0 | "network", |
2273 | 0 | "peer-discovered", |
2274 | 0 | chain = &task.network[chain_id].log_name, |
2275 | 0 | peer_id, |
2276 | 0 | addrs = ?valid_addrs.iter().map(|a| a.to_string()).collect::<Vec<_>>(), // TODO: better formatting? Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0sd_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0sd_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0sd_0B8_ |
2277 | 0 | obtained_from = requestee_peer_id |
2278 | 0 | ); |
2279 | 0 | } |
2280 | 0 | } |
2281 | | |
2282 | 0 | for addr in valid_addrs { |
2283 | 0 | let _insert_result = |
2284 | 0 | task.peering_strategy |
2285 | 0 | .insert_address(&peer_id, addr.into_bytes(), 10); // TODO: constant |
2286 | 0 | debug_assert!(!matches!( |
2287 | 0 | _insert_result, |
2288 | | basic_peering_strategy::InsertAddressResult::UnknownPeer |
2289 | | )); |
2290 | | } |
2291 | | } |
2292 | | } |
2293 | | WakeUpReason::NetworkEvent(service::Event::RequestResult { |
2294 | 0 | peer_id, |
2295 | 0 | chain_id, |
2296 | 0 | response: service::RequestResult::KademliaFindNode(Err(error)), |
2297 | 0 | .. |
2298 | 0 | }) => { |
2299 | 0 | log!( |
2300 | 0 | &task.platform, |
2301 | 0 | Debug, |
2302 | 0 | "network", |
2303 | 0 | "discovery-find-node-error", |
2304 | 0 | chain = &task.network[chain_id].log_name, |
2305 | 0 | ?error, |
2306 | 0 | find_node_target = peer_id, |
2307 | 0 | ); |
2308 | | |
2309 | | // No error is printed if the request fails due to a benign networking error such |
2310 | | // as an unresponsive peer. |
2311 | 0 | match error { |
2312 | 0 | service::KademliaFindNodeError::RequestFailed(err) |
2313 | 0 | if !err.is_protocol_error() => {} |
2314 | | |
2315 | | service::KademliaFindNodeError::RequestFailed( |
2316 | | service::RequestError::Substream( |
2317 | | connection::established::RequestError::ProtocolNotAvailable, |
2318 | | ), |
2319 | 0 | ) => { |
2320 | 0 | // TODO: remove this warning in a long time |
2321 | 0 | log!( |
2322 | 0 | &task.platform, |
2323 | 0 | Warn, |
2324 | 0 | "network", |
2325 | 0 | format!( |
2326 | 0 | "Problem during discovery on {}: protocol not available. \ |
2327 | 0 | This might indicate that the version of Substrate used by \ |
2328 | 0 | the chain doesn't include \ |
2329 | 0 | <https://github.com/paritytech/substrate/pull/12545>.", |
2330 | 0 | &task.network[chain_id].log_name |
2331 | 0 | ) |
2332 | 0 | ); |
2333 | 0 | } |
2334 | 0 | _ => { |
2335 | 0 | log!( |
2336 | 0 | &task.platform, |
2337 | 0 | Debug, |
2338 | 0 | "network", |
2339 | 0 | format!( |
2340 | 0 | "Problem during discovery on {}: {}", |
2341 | 0 | &task.network[chain_id].log_name, error |
2342 | 0 | ) |
2343 | 0 | ); |
2344 | 0 | } |
2345 | | } |
2346 | | } |
2347 | | WakeUpReason::NetworkEvent(service::Event::RequestResult { .. }) => { |
2348 | | // We never start any other kind of requests. |
2349 | 0 | unreachable!() |
2350 | | } |
2351 | | WakeUpReason::NetworkEvent(service::Event::GossipInDesired { |
2352 | 0 | peer_id, |
2353 | 0 | chain_id, |
2354 | 0 | kind: service::GossipKind::ConsensusTransactions, |
2355 | 0 | }) => { |
2356 | 0 | // The networking state machine guarantees that `GossipInDesired` |
2357 | 0 | // can't happen if we are already opening an out slot, which we do |
2358 | 0 | // immediately. |
2359 | 0 | // TODO: add debug_assert! ^ |
2360 | 0 | if task |
2361 | 0 | .network |
2362 | 0 | .opened_gossip_undesired_by_chain(chain_id) |
2363 | 0 | .count() |
2364 | 0 | < 4 |
2365 | 0 | { |
2366 | 0 | log!( |
2367 | 0 | &task.platform, |
2368 | 0 | Debug, |
2369 | 0 | "network", |
2370 | 0 | "gossip-in-request", |
2371 | 0 | chain = &task.network[chain_id].log_name, |
2372 | 0 | peer_id, |
2373 | 0 | outcome = "accepted" |
2374 | 0 | ); |
2375 | 0 | task.network |
2376 | 0 | .gossip_open( |
2377 | 0 | chain_id, |
2378 | 0 | &peer_id, |
2379 | 0 | service::GossipKind::ConsensusTransactions, |
2380 | 0 | ) |
2381 | 0 | .unwrap(); |
2382 | 0 | } else { |
2383 | 0 | log!( |
2384 | 0 | &task.platform, |
2385 | 0 | Debug, |
2386 | 0 | "network", |
2387 | 0 | "gossip-in-request", |
2388 | 0 | chain = &task.network[chain_id].log_name, |
2389 | 0 | peer_id, |
2390 | 0 | outcome = "rejected", |
2391 | 0 | ); |
2392 | 0 | task.network |
2393 | 0 | .gossip_close( |
2394 | 0 | chain_id, |
2395 | 0 | &peer_id, |
2396 | 0 | service::GossipKind::ConsensusTransactions, |
2397 | 0 | ) |
2398 | 0 | .unwrap(); |
2399 | 0 | } |
2400 | | } |
2401 | | WakeUpReason::NetworkEvent(service::Event::GossipInDesiredCancel { .. }) => { |
2402 | | // Can't happen as we already instantaneously accept or reject gossip in requests. |
2403 | 0 | unreachable!() |
2404 | | } |
2405 | | WakeUpReason::NetworkEvent(service::Event::IdentifyRequestIn { |
2406 | 0 | peer_id, |
2407 | 0 | substream_id, |
2408 | 0 | }) => { |
2409 | 0 | log!( |
2410 | 0 | &task.platform, |
2411 | 0 | Debug, |
2412 | 0 | "network", |
2413 | 0 | "identify-request-received", |
2414 | 0 | peer_id, |
2415 | 0 | ); |
2416 | 0 | task.network |
2417 | 0 | .respond_identify(substream_id, &task.identify_agent_version); |
2418 | 0 | } |
2419 | 0 | WakeUpReason::NetworkEvent(service::Event::BlocksRequestIn { .. }) => unreachable!(), |
2420 | | WakeUpReason::NetworkEvent(service::Event::RequestInCancel { .. }) => { |
2421 | | // All incoming requests are immediately answered. |
2422 | 0 | unreachable!() |
2423 | | } |
2424 | | WakeUpReason::NetworkEvent(service::Event::GrandpaNeighborPacket { |
2425 | 0 | chain_id, |
2426 | 0 | peer_id, |
2427 | 0 | state, |
2428 | 0 | }) => { |
2429 | 0 | log!( |
2430 | 0 | &task.platform, |
2431 | 0 | Debug, |
2432 | 0 | "network", |
2433 | 0 | "grandpa-neighbor-packet-received", |
2434 | 0 | chain = &task.network[chain_id].log_name, |
2435 | 0 | peer_id, |
2436 | 0 | round_number = state.round_number, |
2437 | 0 | set_id = state.set_id, |
2438 | 0 | commit_finalized_height = state.commit_finalized_height, |
2439 | 0 | ); |
2440 | 0 |
|
2441 | 0 | task.open_gossip_links |
2442 | 0 | .get_mut(&(chain_id, peer_id.clone())) |
2443 | 0 | .unwrap() |
2444 | 0 | .finalized_block_height = Some(state.commit_finalized_height); |
2445 | 0 |
|
2446 | 0 | debug_assert!(task.event_pending_send.is_none()); |
2447 | 0 | task.event_pending_send = Some(( |
2448 | 0 | chain_id, |
2449 | 0 | Event::GrandpaNeighborPacket { |
2450 | 0 | peer_id, |
2451 | 0 | finalized_block_height: state.commit_finalized_height, |
2452 | 0 | }, |
2453 | 0 | )); |
2454 | | } |
2455 | | WakeUpReason::NetworkEvent(service::Event::GrandpaCommitMessage { |
2456 | 0 | chain_id, |
2457 | 0 | peer_id, |
2458 | 0 | message, |
2459 | 0 | }) => { |
2460 | 0 | log!( |
2461 | 0 | &task.platform, |
2462 | 0 | Debug, |
2463 | 0 | "network", |
2464 | 0 | "grandpa-commit-message-received", |
2465 | 0 | chain = &task.network[chain_id].log_name, |
2466 | 0 | peer_id, |
2467 | 0 | target_block_hash = HashDisplay(message.decode().target_hash), |
2468 | 0 | ); |
2469 | 0 |
|
2470 | 0 | debug_assert!(task.event_pending_send.is_none()); |
2471 | 0 | task.event_pending_send = |
2472 | 0 | Some((chain_id, Event::GrandpaCommitMessage { peer_id, message })); |
2473 | | } |
2474 | 0 | WakeUpReason::NetworkEvent(service::Event::ProtocolError { peer_id, error }) => { |
2475 | 0 | // TODO: handle properly? |
2476 | 0 | log!( |
2477 | 0 | &task.platform, |
2478 | 0 | Warn, |
2479 | 0 | "network", |
2480 | 0 | "protocol-error", |
2481 | 0 | peer_id, |
2482 | 0 | ?error |
2483 | 0 | ); |
2484 | 0 |
|
2485 | 0 | // TODO: disconnect peer |
2486 | 0 | } |
2487 | 0 | WakeUpReason::CanAssignSlot(peer_id, chain_id) => { |
2488 | 0 | task.peering_strategy.assign_slot(&chain_id, &peer_id); |
2489 | 0 |
|
2490 | 0 | log!( |
2491 | 0 | &task.platform, |
2492 | 0 | Debug, |
2493 | 0 | "network", |
2494 | 0 | "slot-assigned", |
2495 | 0 | chain = &task.network[chain_id].log_name, |
2496 | 0 | peer_id |
2497 | 0 | ); |
2498 | 0 |
|
2499 | 0 | task.network.gossip_insert_desired( |
2500 | 0 | chain_id, |
2501 | 0 | peer_id, |
2502 | 0 | service::GossipKind::ConsensusTransactions, |
2503 | 0 | ); |
2504 | 0 | } |
2505 | 0 | WakeUpReason::NextRecentConnectionRestore => { |
2506 | 0 | task.num_recent_connection_opening = |
2507 | 0 | task.num_recent_connection_opening.saturating_sub(1); |
2508 | 0 | } |
2509 | 0 | WakeUpReason::CanStartConnect(expected_peer_id) => { |
2510 | 0 | let Some(multiaddr) = task |
2511 | 0 | .peering_strategy |
2512 | 0 | .pick_address_and_add_connection(&expected_peer_id) |
2513 | | else { |
2514 | | // There is no address for that peer in the address book. |
2515 | 0 | task.network.gossip_remove_desired_all( |
2516 | 0 | &expected_peer_id, |
2517 | 0 | service::GossipKind::ConsensusTransactions, |
2518 | 0 | ); |
2519 | 0 | let ban_duration = Duration::from_secs(10); |
2520 | 0 | for (&chain_id, what_happened) in task.peering_strategy.unassign_slots_and_ban( |
2521 | 0 | &expected_peer_id, |
2522 | 0 | task.platform.now() + ban_duration, |
2523 | 0 | ) { |
2524 | 0 | if matches!( |
2525 | 0 | what_happened, |
2526 | | basic_peering_strategy::UnassignSlotsAndBan::Banned { had_slot: true } |
2527 | 0 | ) { |
2528 | 0 | log!( |
2529 | 0 | &task.platform, |
2530 | 0 | Debug, |
2531 | 0 | "network", |
2532 | 0 | "slot-unassigned", |
2533 | 0 | chain = &task.network[chain_id].log_name, |
2534 | 0 | peer_id = expected_peer_id, |
2535 | 0 | ?ban_duration, |
2536 | 0 | reason = "no-address" |
2537 | 0 | ); |
2538 | 0 | } |
2539 | | } |
2540 | 0 | continue; |
2541 | | }; |
2542 | | |
2543 | 0 | let multiaddr = match multiaddr::Multiaddr::from_bytes(multiaddr.to_owned()) { |
2544 | 0 | Ok(a) => a, |
2545 | 0 | Err((multiaddr::FromBytesError, addr)) => { |
2546 | 0 | // Address is in an invalid format. |
2547 | 0 | let _was_in = task |
2548 | 0 | .peering_strategy |
2549 | 0 | .decrease_address_connections_and_remove_if_zero( |
2550 | 0 | &expected_peer_id, |
2551 | 0 | &addr, |
2552 | 0 | ); |
2553 | 0 | debug_assert!(_was_in.is_ok()); |
2554 | 0 | continue; |
2555 | | } |
2556 | | }; |
2557 | | |
2558 | 0 | let address = address_parse::multiaddr_to_address(&multiaddr) |
2559 | 0 | .ok() |
2560 | 0 | .filter(|addr| { |
2561 | 0 | task.platform.supports_connection_type(match &addr { |
2562 | 0 | address_parse::AddressOrMultiStreamAddress::Address(addr) => { |
2563 | 0 | From::from(addr) |
2564 | | } |
2565 | | address_parse::AddressOrMultiStreamAddress::MultiStreamAddress( |
2566 | 0 | addr, |
2567 | 0 | ) => From::from(addr), |
2568 | | }) |
2569 | 0 | }); Unexecuted instantiation: _RNCNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0sb_0B8_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0sb_0B1d_ Unexecuted instantiation: _RNCNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0sb_0B8_ |
2570 | | |
2571 | 0 | let Some(address) = address else { |
2572 | | // Address is in an invalid format or isn't supported by the platform. |
2573 | 0 | let _was_in = task |
2574 | 0 | .peering_strategy |
2575 | 0 | .decrease_address_connections_and_remove_if_zero( |
2576 | 0 | &expected_peer_id, |
2577 | 0 | multiaddr.as_ref(), |
2578 | 0 | ); |
2579 | 0 | debug_assert!(_was_in.is_ok()); |
2580 | 0 | continue; |
2581 | | }; |
2582 | | |
2583 | | // Each connection has its own individual Noise key. |
2584 | 0 | let noise_key = { |
2585 | 0 | let mut noise_static_key = zeroize::Zeroizing::new([0u8; 32]); |
2586 | 0 | task.platform.fill_random_bytes(&mut *noise_static_key); |
2587 | 0 | let mut libp2p_key = zeroize::Zeroizing::new([0u8; 32]); |
2588 | 0 | task.platform.fill_random_bytes(&mut *libp2p_key); |
2589 | 0 | connection::NoiseKey::new(&libp2p_key, &noise_static_key) |
2590 | 0 | }; |
2591 | 0 |
|
2592 | 0 | log!( |
2593 | 0 | &task.platform, |
2594 | 0 | Debug, |
2595 | 0 | "network", |
2596 | 0 | "connection-started", |
2597 | 0 | expected_peer_id, |
2598 | 0 | remote_addr = multiaddr, |
2599 | 0 | local_peer_id = |
2600 | 0 | peer_id::PublicKey::Ed25519(*noise_key.libp2p_public_ed25519_key()) |
2601 | 0 | .into_peer_id(), |
2602 | 0 | ); |
2603 | 0 |
|
2604 | 0 | task.num_recent_connection_opening += 1; |
2605 | 0 |
|
2606 | 0 | let (coordinator_to_connection_tx, coordinator_to_connection_rx) = |
2607 | 0 | async_channel::bounded(8); |
2608 | 0 | let task_name = format!("connection-{}", multiaddr); |
2609 | 0 |
|
2610 | 0 | match address { |
2611 | 0 | address_parse::AddressOrMultiStreamAddress::Address(address) => { |
2612 | 0 | // As documented in the `PlatformRef` trait, `connect_stream` must |
2613 | 0 | // return as soon as possible. |
2614 | 0 | let connection = task.platform.connect_stream(address).await; |
2615 | | |
2616 | 0 | let (connection_id, connection_task) = |
2617 | 0 | task.network.add_single_stream_connection( |
2618 | 0 | task.platform.now(), |
2619 | 0 | service::SingleStreamHandshakeKind::MultistreamSelectNoiseYamux { |
2620 | 0 | is_initiator: true, |
2621 | 0 | noise_key: &noise_key, |
2622 | 0 | }, |
2623 | 0 | multiaddr.clone().into_bytes(), |
2624 | 0 | Some(expected_peer_id.clone()), |
2625 | 0 | coordinator_to_connection_tx, |
2626 | 0 | ); |
2627 | 0 |
|
2628 | 0 | task.platform.spawn_task( |
2629 | 0 | task_name.into(), |
2630 | 0 | tasks::single_stream_connection_task::<TPlat>( |
2631 | 0 | connection, |
2632 | 0 | multiaddr.to_string(), |
2633 | 0 | task.platform.clone(), |
2634 | 0 | connection_id, |
2635 | 0 | connection_task, |
2636 | 0 | coordinator_to_connection_rx, |
2637 | 0 | task.tasks_messages_tx.clone(), |
2638 | 0 | ), |
2639 | 0 | ); |
2640 | | } |
2641 | | address_parse::AddressOrMultiStreamAddress::MultiStreamAddress( |
2642 | | platform::MultiStreamAddress::WebRtc { |
2643 | 0 | ip, |
2644 | 0 | port, |
2645 | 0 | remote_certificate_sha256, |
2646 | | }, |
2647 | 0 | ) => { |
2648 | 0 | // We need to know the local TLS certificate in order to insert the |
2649 | 0 | // connection, and as such we need to call `connect_multistream` here. |
2650 | 0 | // As documented in the `PlatformRef` trait, `connect_multistream` must |
2651 | 0 | // return as soon as possible. |
2652 | 0 | let connection = task |
2653 | 0 | .platform |
2654 | 0 | .connect_multistream(platform::MultiStreamAddress::WebRtc { |
2655 | 0 | ip, |
2656 | 0 | port, |
2657 | 0 | remote_certificate_sha256, |
2658 | 0 | }) |
2659 | 0 | .await; |
2660 | | |
2661 | | // Convert the SHA256 hashes into multihashes. |
2662 | 0 | let local_tls_certificate_multihash = [18u8, 32] |
2663 | 0 | .into_iter() |
2664 | 0 | .chain(connection.local_tls_certificate_sha256.into_iter()) |
2665 | 0 | .collect(); |
2666 | 0 | let remote_tls_certificate_multihash = [18u8, 32] |
2667 | 0 | .into_iter() |
2668 | 0 | .chain(remote_certificate_sha256.into_iter().copied()) |
2669 | 0 | .collect(); |
2670 | 0 |
|
2671 | 0 | let (connection_id, connection_task) = |
2672 | 0 | task.network.add_multi_stream_connection( |
2673 | 0 | task.platform.now(), |
2674 | 0 | service::MultiStreamHandshakeKind::WebRtc { |
2675 | 0 | is_initiator: true, |
2676 | 0 | local_tls_certificate_multihash, |
2677 | 0 | remote_tls_certificate_multihash, |
2678 | 0 | noise_key: &noise_key, |
2679 | 0 | }, |
2680 | 0 | multiaddr.clone().into_bytes(), |
2681 | 0 | Some(expected_peer_id.clone()), |
2682 | 0 | coordinator_to_connection_tx, |
2683 | 0 | ); |
2684 | 0 |
|
2685 | 0 | task.platform.spawn_task( |
2686 | 0 | task_name.into(), |
2687 | 0 | tasks::webrtc_multi_stream_connection_task::<TPlat>( |
2688 | 0 | connection.connection, |
2689 | 0 | multiaddr.to_string(), |
2690 | 0 | task.platform.clone(), |
2691 | 0 | connection_id, |
2692 | 0 | connection_task, |
2693 | 0 | coordinator_to_connection_rx, |
2694 | 0 | task.tasks_messages_tx.clone(), |
2695 | 0 | ), |
2696 | 0 | ); |
2697 | | } |
2698 | | } |
2699 | | } |
2700 | 0 | WakeUpReason::CanOpenGossip(peer_id, chain_id) => { |
2701 | 0 | task.network |
2702 | 0 | .gossip_open( |
2703 | 0 | chain_id, |
2704 | 0 | &peer_id, |
2705 | 0 | service::GossipKind::ConsensusTransactions, |
2706 | 0 | ) |
2707 | 0 | .unwrap(); |
2708 | 0 |
|
2709 | 0 | log!( |
2710 | 0 | &task.platform, |
2711 | 0 | Debug, |
2712 | 0 | "network", |
2713 | 0 | "gossip-open-start", |
2714 | 0 | chain = &task.network[chain_id].log_name, |
2715 | 0 | peer_id, |
2716 | 0 | ); |
2717 | 0 | } |
2718 | | WakeUpReason::MessageToConnection { |
2719 | 0 | connection_id, |
2720 | 0 | message, |
2721 | | } => { |
2722 | | // Note that it is critical for the sending to not take too long here, in order to |
2723 | | // not block the process of the network service. |
2724 | | // In particular, if sending the message to the connection is blocked due to |
2725 | | // sending a message on the connection-to-coordinator channel, this will result |
2726 | | // in a deadlock. |
2727 | | // For this reason, the connection task is always ready to immediately accept a |
2728 | | // message on the coordinator-to-connection channel. |
2729 | 0 | let _send_result = task.network[connection_id].send(message).await; |
2730 | 0 | debug_assert!(_send_result.is_ok()); |
2731 | | } |
2732 | | } |
2733 | | } |
2734 | 0 | } Unexecuted instantiation: _RNCINvNtCsiGub1lfKphe_13smoldot_light15network_service15background_taskpE0B6_ Unexecuted instantiation: _RNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE0B1b_ Unexecuted instantiation: _RNCINvNtCsih6EgvAwZF2_13smoldot_light15network_service15background_taskpE0B6_ |