-
Notifications
You must be signed in to change notification settings - Fork 50
/
lib.rs
1344 lines (1223 loc) · 60 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Smoldot
// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//! Smoldot light client library.
//!
//! This library provides an easy way to create a light client.
//!
//! This light client is opinionated towards certain aspects: what it downloads, how much memory
//! and CPU it is willing to consume, etc.
//!
//! # Usage
//!
//! ## Initialization
//!
//! In order to use the light client, call [`Client::new`], passing an implementation of the
//! [`platform::PlatformRef`] trait. See the documentation of the [`platform::PlatformRef`] trait
//! for more information.
//!
//! The [`Client`] contains two generic parameters:
//!
//! - An implementation of the [`platform::PlatformRef`] trait.
//! - An opaque user data. If you do not use this, you can simply use `()`.
//!
//! ## Adding a chain
//!
//! After the client has been initialized, use [`Client::add_chain`] to ask the client to connect
//! to said chain. See the documentation of [`AddChainConfig`] for information about what to
//! provide.
//!
//! [`Client::add_chain`] returns a [`ChainId`], which identifies the chain within the [`Client`].
//! A [`Client`] can be thought of as a collection of chain connections, each identified by their
//! [`ChainId`], akin to a `HashMap<ChainId, ...>`.
//!
//! A chain can be removed at any time using [`Client::remove_chain`]. This will cause the client
//! to stop all connections and clean up its internal services. The [`ChainId`] is instantly
//! considered as invalid as soon as the method is called.
//!
//! ## JSON-RPC requests and responses
//!
//! Once a chain has been added, one can send JSON-RPC requests using [`Client::json_rpc_request`].
//!
//! The request parameter of this function must be a JSON-RPC request in its text form. For
//! example: `{"id":53,"jsonrpc":"2.0","method":"system_name","params":[]}`.
//!
//! Calling [`Client::json_rpc_request`] queues the request in the internals of the client. Later,
//! the client will process it.
//!
//! Responses can be pulled by calling the [`AddChainSuccess::json_rpc_responses`] that is returned
//! after a chain has been added.
//!
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![forbid(unsafe_code)]
#![deny(rustdoc::broken_intra_doc_links)]
// TODO: the `unused_crate_dependencies` lint is disabled because of dev-dependencies, see <https://github.com/rust-lang/rust/issues/95513>
// #![deny(unused_crate_dependencies)]
extern crate alloc;
use alloc::{borrow::ToOwned as _, boxed::Box, format, string::String, sync::Arc, vec, vec::Vec};
use core::{num::NonZeroU32, ops, pin};
use futures_util::{future, FutureExt as _};
use hashbrown::{hash_map::Entry, HashMap};
use itertools::Itertools as _;
use smoldot::{
chain, chain_spec, header,
informant::HashDisplay,
libp2p::{multiaddr, peer_id},
};
mod database;
mod json_rpc_service;
mod network_service;
mod runtime_service;
mod sync_service;
mod transactions_service;
mod util;
pub mod platform;
pub use json_rpc_service::HandleRpcError;
pub use peer_id::PeerId;
/// See [`Client::add_chain`].
#[derive(Debug, Clone)]
pub struct AddChainConfig<'a, TChain, TRelays> {
/// Opaque user data that the [`Client`] will hold for this chain. Can later be accessed using
/// the `Index` and `IndexMut` trait implementations on the [`Client`].
pub user_data: TChain,
/// JSON text containing the specification of the chain (the so-called "chain spec").
pub specification: &'a str,
/// Opaque data containing the database content that was retrieved by calling
/// the `chainHead_unstable_finalizedDatabase` JSON-RPC function in the past.
///
/// Pass an empty string if no database content exists or is known.
///
/// No error is generated if this data is invalid and/or can't be decoded. The implementation
/// reserves the right to break the format of this data at any point.
pub database_content: &'a str,
/// If [`AddChainConfig`] defines a parachain, contains the list of relay chains to choose
/// from. Ignored if not a parachain.
///
/// This field is necessary because multiple different chain can have the same identity. If
/// the client tried to find the corresponding relay chain in all the previously-spawned
/// chains, it means that a call to [`Client::add_chain`] could influence the outcome of a
/// subsequent call to [`Client::add_chain`].
///
/// For example: if user A adds a chain named "Kusama", then user B adds a different chain
/// also named "Kusama", then user B adds a parachain whose relay chain is "Kusama", it would
/// be wrong to connect to the "Kusama" created by user A.
pub potential_relay_chains: TRelays,
/// Configuration for the JSON-RPC endpoint.
pub json_rpc: AddChainConfigJsonRpc,
}
/// See [`AddChainConfig::json_rpc`].
#[derive(Debug, Clone)]
pub enum AddChainConfigJsonRpc {
/// No JSON-RPC endpoint is available for this chain. This saves up a lot of resources, but
/// will cause all JSON-RPC requests targeting this chain to fail.
Disabled,
/// The JSON-RPC endpoint is enabled. Normal operations.
Enabled {
/// Maximum number of JSON-RPC requests that can be added to a queue if it is not ready to
/// be processed immediately. Any additional request will be immediately rejected.
///
/// This parameter is necessary in order to prevent JSON-RPC clients from using up too
/// much memory within the client.
/// If the JSON-RPC client is entirely trusted, then passing `u32::max_value()` is
/// completely reasonable.
///
/// A typical value is 128.
max_pending_requests: NonZeroU32,
/// Maximum number of active subscriptions that can be started through JSON-RPC functions.
/// Any request that causes the JSON-RPC server to generate notifications counts as a
/// subscription.
/// Any additional subscription over this limit will be immediately rejected.
///
/// This parameter is necessary in order to prevent JSON-RPC clients from using up too
/// much memory within the client.
/// If the JSON-RPC client is entirely trusted, then passing `u32::max_value()` is
/// completely reasonable.
///
/// While a typical reasonable value would be for example 64, existing UIs tend to start
/// a lot of subscriptions, and a value such as 1024 is recommended.
max_subscriptions: u32,
},
}
/// Chain registered in a [`Client`].
///
/// This type is a simple wrapper around a `usize`. Use the `From<usize> for ChainId` and
/// `From<ChainId> for usize` trait implementations to convert back and forth if necessary.
//
// Implementation detail: corresponds to indices within [`Client::public_api_chains`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChainId(usize);
impl From<usize> for ChainId {
fn from(id: usize) -> ChainId {
ChainId(id)
}
}
impl From<ChainId> for usize {
fn from(chain_id: ChainId) -> usize {
chain_id.0
}
}
/// Holds a list of chains, connections, and JSON-RPC services.
pub struct Client<TPlat: platform::PlatformRef, TChain = ()> {
/// Access to the platform capabilities.
platform: TPlat,
/// List of chains currently running according to the public API. Indices in this container
/// are reported through the public API. The values are either an error if the chain has failed
/// to initialize, or key found in [`Client::chains_by_key`].
public_api_chains: slab::Slab<PublicApiChain<TChain>>,
/// De-duplicated list of chains that are *actually* running.
///
/// For each key, contains the services running for this chain plus the number of public API
/// chains that correspond to it.
///
/// Because we use a `SipHasher`, this hashmap isn't created in the `new` function (as this
/// function is `const`) but lazily the first time it is needed.
chains_by_key: Option<HashMap<ChainKey, RunningChain<TPlat>, util::SipHasherBuild>>,
}
struct PublicApiChain<TChain> {
/// Opaque user data passed to [`Client::add_chain`].
user_data: TChain,
/// Index of the underlying chain found in [`Client::chains_by_key`].
key: ChainKey,
/// Identifier of the chain found in its chain spec. Equal to the return value of
/// [`chain_spec::ChainSpec::id`]. Used in order to match parachains with relay chains.
chain_spec_chain_id: String,
/// Handle that sends requests to the JSON-RPC service that runs in the background.
/// Destroying this handle also shuts down the service. `None` iff
/// [`AddChainConfig::json_rpc`] was [`AddChainConfigJsonRpc::Disabled`] when adding the chain.
json_rpc_frontend: Option<json_rpc_service::Frontend>,
/// Notified when the [`PublicApiChain`] is destroyed, in order for the [`JsonRpcResponses`]
/// to detect when the chain has been removed.
public_api_chain_destroyed_event: event_listener::Event,
}
/// Identifies a chain, so that multiple identical chains are de-duplicated.
///
/// This struct serves as the key in a `HashMap<ChainKey, ChainServices>`. It must contain all the
/// values that are important to the logic of the fields that are contained in [`ChainServices`].
/// Failing to include a field in this struct could lead to two different chains using the same
/// [`ChainServices`], which has security consequences.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct ChainKey {
/// Hash of the genesis block of the chain.
genesis_block_hash: [u8; 32],
// TODO: what about light checkpoints?
// TODO: must also contain forkBlocks, and badBlocks fields
/// If the chain is a parachain, contains the relay chain and the "para ID" on this relay
/// chain.
relay_chain: Option<(Box<ChainKey>, u32)>,
/// Networking fork id, found in the chain specification.
fork_id: Option<String>,
}
struct RunningChain<TPlat: platform::PlatformRef> {
/// Services that are dedicated to this chain. Wrapped within a `MaybeDone` because the
/// initialization is performed asynchronously.
services: future::MaybeDone<future::Shared<future::RemoteHandle<ChainServices<TPlat>>>>,
/// Name of this chain in the logs. This is not necessarily the same as the identifier of the
/// chain in its chain specification.
log_name: String,
/// Number of elements in [`Client::public_api_chains`] that reference this chain. If this
/// number reaches `0`, the [`RunningChain`] should be destroyed.
num_references: NonZeroU32,
}
struct ChainServices<TPlat: platform::PlatformRef> {
network_service: Arc<network_service::NetworkService<TPlat>>,
network_service_chain_id: network_service::ChainId,
sync_service: Arc<sync_service::SyncService<TPlat>>,
runtime_service: Arc<runtime_service::RuntimeService<TPlat>>,
transactions_service: Arc<transactions_service::TransactionsService<TPlat>>,
}
impl<TPlat: platform::PlatformRef> Clone for ChainServices<TPlat> {
fn clone(&self) -> Self {
ChainServices {
network_service: self.network_service.clone(),
network_service_chain_id: self.network_service_chain_id.clone(),
sync_service: self.sync_service.clone(),
runtime_service: self.runtime_service.clone(),
transactions_service: self.transactions_service.clone(),
}
}
}
/// Returns by [`Client::add_chain`] on success.
pub struct AddChainSuccess {
/// Newly-allocated identifier for the chain.
pub chain_id: ChainId,
/// Stream of JSON-RPC responses or notifications.
///
/// Is always `Some` if [`AddChainConfig::json_rpc`] was [`AddChainConfigJsonRpc::Enabled`],
/// and `None` if it was [`AddChainConfigJsonRpc::Disabled`]. In other words, you can unwrap
/// this `Option` if you passed `Enabled`.
pub json_rpc_responses: Option<JsonRpcResponses>,
}
/// Stream of JSON-RPC responses or notifications.
///
/// See [`AddChainSuccess::json_rpc_responses`].
pub struct JsonRpcResponses {
/// Receiving side for responses.
///
/// As long as this object is alive, the JSON-RPC service will continue running. In order
/// to prevent that from happening, we destroy it as soon as the
/// [`JsonRpcResponses::public_api_chain_destroyed`] is notified of the destruction of
/// the sender.
inner: Option<json_rpc_service::Frontend>,
/// Notified when the [`PublicApiChain`] is destroyed.
public_api_chain_destroyed: pin::Pin<Box<event_listener::EventListener>>,
}
impl JsonRpcResponses {
/// Returns the next response or notification, or `None` if the chain has been removed.
pub async fn next(&mut self) -> Option<String> {
if let Some(frontend) = self.inner.as_mut() {
if let Some(response) = futures_lite::future::or(
async { Some(frontend.next_json_rpc_response().await) },
async {
(&mut self.public_api_chain_destroyed).await;
None
},
)
.await
{
return Some(response);
}
}
self.inner = None;
None
}
}
impl<TPlat: platform::PlatformRef, TChain> Client<TPlat, TChain> {
/// Initializes the smoldot client.
pub const fn new(platform: TPlat) -> Self {
Client {
platform,
public_api_chains: slab::Slab::new(),
chains_by_key: None,
}
}
/// Adds a new chain to the list of chains smoldot tries to synchronize.
///
/// Returns an error in case something is wrong with the configuration.
pub fn add_chain(
&mut self,
config: AddChainConfig<'_, TChain, impl Iterator<Item = ChainId>>,
) -> Result<AddChainSuccess, AddChainError> {
// `chains_by_key` is created lazily whenever needed.
let chains_by_key = self.chains_by_key.get_or_insert_with(|| {
HashMap::with_hasher(util::SipHasherBuild::new({
let mut seed = [0; 16];
self.platform.fill_random_bytes(&mut seed);
seed
}))
});
// Decode the chain specification.
let chain_spec = match chain_spec::ChainSpec::from_json_bytes(config.specification) {
Ok(cs) => cs,
Err(err) => {
return Err(AddChainError::ChainSpecParseError(err));
}
};
// Build the genesis block, its hash, and information about the chain.
let (
genesis_chain_information,
genesis_block_header,
print_warning_genesis_root_chainspec,
genesis_block_state_root,
) = {
// TODO: don't build the chain information if only the genesis hash is needed: https://github.com/smol-dot/smoldot/issues/1017
let genesis_chain_information = chain_spec.to_chain_information().map(|(ci, _)| ci); // TODO: don't just throw away the runtime;
match genesis_chain_information {
Ok(genesis_chain_information) => {
let header = genesis_chain_information.as_ref().finalized_block_header;
let state_root = *header.state_root;
let scale_encoded =
header.scale_encoding_vec(usize::from(chain_spec.block_number_bytes()));
(
Some(genesis_chain_information),
scale_encoded,
chain_spec.light_sync_state().is_some()
|| chain_spec.relay_chain().is_some(),
state_root,
)
}
Err(chain_spec::FromGenesisStorageError::UnknownStorageItems) => {
let state_root = *chain_spec.genesis_storage().into_trie_root_hash().unwrap();
let header = header::Header {
parent_hash: [0; 32],
number: 0,
state_root,
extrinsics_root: smoldot::trie::EMPTY_BLAKE2_TRIE_MERKLE_VALUE,
digest: header::DigestRef::empty().into(),
}
.scale_encoding_vec(usize::from(chain_spec.block_number_bytes()));
(None, header, false, state_root)
}
Err(err) => return Err(AddChainError::InvalidGenesisStorage(err)),
}
};
let genesis_block_hash = header::hash_from_scale_encoded_header(&genesis_block_header);
// Decode the database and make sure that it matches the chain by comparing the finalized
// block header in it with the actual one.
let (database, database_was_wrong_chain) = {
let mut maybe_database = database::decode_database(
config.database_content,
chain_spec.block_number_bytes().into(),
)
.ok();
let mut database_was_wrong = false;
if maybe_database
.as_ref()
.map_or(false, |db| db.genesis_block_hash != genesis_block_hash)
{
maybe_database = None;
database_was_wrong = true;
}
(maybe_database, database_was_wrong)
};
// Load the information about the chain. If a light sync state (also known as a checkpoint)
// is present in the chain spec, it is possible to start syncing at the finalized block
// it describes.
// At the same time, we deconstruct the database into `known_nodes`
// and `runtime_code_hint`.
let (chain_information, used_database_chain_information, known_nodes, runtime_code_hint) = {
let checkpoint = chain_spec
.light_sync_state()
.map(|s| s.to_chain_information());
match (genesis_chain_information, checkpoint, database) {
// Use the database if it contains a more recent block than the
// chain spec checkpoint.
(
_,
Some(Ok(checkpoint)),
Some(database::DatabaseContent {
chain_information: Some(db_ci),
known_nodes,
runtime_code_hint,
..
}),
) if db_ci.as_ref().finalized_block_header.number
>= checkpoint.as_ref().finalized_block_header.number =>
{
(Some(db_ci), true, known_nodes, runtime_code_hint)
}
// Otherwise, use the chain spec checkpoint.
(
_,
Some(Ok(checkpoint)),
Some(database::DatabaseContent {
known_nodes,
runtime_code_hint,
..
}),
) => (Some(checkpoint), false, known_nodes, runtime_code_hint),
(_, Some(Ok(checkpoint)), None) => (Some(checkpoint), false, Vec::new(), None),
// If neither the genesis chain information nor the checkpoint chain information
// is available, we could in principle use the database, but for API reasons we
// don't want users to be able to rely on just a database (as we reserve the right
// to break the database at any point) and thus return an error.
(
None,
None,
Some(database::DatabaseContent {
known_nodes,
runtime_code_hint,
..
}),
) => (None, false, known_nodes, runtime_code_hint),
(None, None, None) => (None, false, Vec::new(), None),
// Use the genesis block if no checkpoint is available.
(
Some(genesis_ci),
None
| Some(Err(
chain_spec::CheckpointToChainInformationError::GenesisBlockCheckpoint,
)),
Some(database::DatabaseContent {
known_nodes,
runtime_code_hint,
..
}),
) => (Some(genesis_ci), false, known_nodes, runtime_code_hint),
(
Some(genesis_ci),
None
| Some(Err(
chain_spec::CheckpointToChainInformationError::GenesisBlockCheckpoint,
)),
None,
) => (Some(genesis_ci), false, Vec::new(), None),
// If the checkpoint format is invalid, we return an error no matter whether the
// genesis chain information could be used.
(_, Some(Err(err)), _) => {
return Err(AddChainError::InvalidCheckpoint(err));
}
}
};
// If the chain specification specifies a parachain, find the corresponding relay chain
// in the list of potential relay chains passed by the user.
// If no relay chain can be found, the chain creation fails. Exactly one matching relay
// chain must be found. If there are multiple ones, the creation fails as well.
let relay_chain_id = if let Some((relay_chain_id, para_id)) = chain_spec.relay_chain() {
let chain = config
.potential_relay_chains
.filter(|c| {
self.public_api_chains
.get(c.0)
.map_or(false, |chain| chain.chain_spec_chain_id == relay_chain_id)
})
.exactly_one();
match chain {
Ok(c) => Some((c, para_id)),
Err(mut iter) => {
// `iter` here is identical to the iterator above before `exactly_one` is
// called. This lets us know what failed.
return Err(if iter.next().is_none() {
AddChainError::NoRelayChainFound
} else {
debug_assert!(iter.next().is_some());
AddChainError::MultipleRelayChains
});
}
}
} else {
None
};
// Build the list of bootstrap nodes ahead of time.
// Because the specification of the format of a multiaddress is a bit flexible, it is
// not possible to firmly affirm that a multiaddress is invalid. For this reason, we
// simply ignore unparsable bootnode addresses rather than returning an error.
// A list of invalid bootstrap node addresses is kept in order to print a warning later
// in case it is non-empty. This list is sanitized in order to be safely printable as part
// of the logs.
let (bootstrap_nodes, invalid_bootstrap_nodes_sanitized) = {
let mut valid_list = Vec::with_capacity(chain_spec.boot_nodes().len());
let mut invalid_list = Vec::with_capacity(0);
for node in chain_spec.boot_nodes() {
match node {
chain_spec::Bootnode::Parsed { multiaddr, peer_id } => {
if let Ok(multiaddr) = multiaddr.parse::<multiaddr::Multiaddr>() {
let peer_id = peer_id::PeerId::from_bytes(peer_id).unwrap();
valid_list.push((peer_id, vec![multiaddr]));
} else {
invalid_list.push(multiaddr)
}
}
chain_spec::Bootnode::UnrecognizedFormat(unparsed) => invalid_list.push(
unparsed
.chars()
.filter(|c| c.is_ascii())
.collect::<String>(),
),
}
}
(valid_list, invalid_list)
};
// All the checks are performed above. Adding the chain can't fail anymore at this point.
// Grab this field from the chain specification for later, as the chain specification is
// consumed below.
let chain_spec_chain_id = chain_spec.id().to_owned();
// The key generated here uniquely identifies this chain within smoldot. Mutiple chains
// having the same key will use the same services.
//
// This struct is extremely important from a security perspective. We want multiple
// identical chains to be de-duplicated, but security issues would arise if two chains
// were considered identical while they're in reality not identical.
let new_chain_key = ChainKey {
genesis_block_hash,
relay_chain: relay_chain_id.map(|(ck, _)| {
(
Box::new(self.public_api_chains.get(ck.0).unwrap().key.clone()),
chain_spec.relay_chain().unwrap().1,
)
}),
fork_id: chain_spec.fork_id().map(|f| f.to_owned()),
};
// If the chain we are adding is a parachain, grab the services of the relay chain.
//
// Since the initialization process of a chain is done asynchronously, it is possible that
// the relay chain is still initializing. For this reason, we don't don't simply grab
// the relay chain services, but instead a `future::MaybeDone` of a future that yelds the
// relay chain services.
//
// This could in principle be done later on, but doing so raises borrow checker errors.
let relay_chain_ready_future: Option<(future::MaybeDone<future::Shared<_>>, u32, String)> =
relay_chain_id.map(|(relay_chain, para_id)| {
let relay_chain = &chains_by_key
.get(&self.public_api_chains.get(relay_chain.0).unwrap().key)
.unwrap();
let future = match &relay_chain.services {
future::MaybeDone::Done(d) => future::MaybeDone::Done(d.clone()),
future::MaybeDone::Future(d) => future::MaybeDone::Future(d.clone()),
future::MaybeDone::Gone => unreachable!(),
};
(future, para_id, relay_chain.log_name.clone())
});
// Determinate the name under which the chain will be identified in the logs.
// Because the chain spec is untrusted input, we must transform the `id` to remove all
// weird characters.
//
// By default, this log name will be equal to chain's `id`. Since it is possible for
// multiple different chains to have the same `id`, we need to look into the list of
// existing chains and make sure that there's no conflict, in which case the log name
// will have the suffix `-1`, or `-2`, or `-3`, and so on.
//
// This value is ignored if we enter the `Entry::Occupied` block below. Because the
// calculation requires accessing the list of existing chains, this block can't be put in
// the `Entry::Vacant` block below, even though it would make sense for it to be there.
let log_name = {
let base = chain_spec
.id()
.chars()
.filter(|c| c.is_ascii_graphic())
.collect::<String>();
let mut suffix = None;
loop {
let attempt = if let Some(suffix) = suffix {
format!("{base}-{suffix}")
} else {
base.clone()
};
if !chains_by_key.values().any(|c| *c.log_name == attempt) {
break attempt;
}
match &mut suffix {
Some(v) => *v += 1,
v @ None => *v = Some(1),
}
}
};
// Start the services of the chain to add, or grab the services if they already exist.
let (services_init, log_name) = match chains_by_key.entry(new_chain_key.clone()) {
Entry::Occupied(mut entry) => {
// The chain to add always has a corresponding chain running. Simply grab the
// existing services and existing log name.
// The `log_name` created above is discarded in favour of the existing log name.
entry.get_mut().num_references = entry.get().num_references.checked_add(1).unwrap();
let entry = entry.into_mut();
(&mut entry.services, &entry.log_name)
}
Entry::Vacant(entry) => {
// Version of the client when requested through the networking.
let network_identify_agent_version = format!(
"{} {}",
self.platform.client_name(),
self.platform.client_version()
);
// Spawn a background task that initializes the services of the new chain and
// yields a `ChainServices`.
let running_chain_init_future: future::RemoteHandle<ChainServices<TPlat>> = {
let platform = self.platform.clone();
let fork_id = chain_spec.fork_id().map(|f| f.to_owned());
let chain_name = chain_spec.name().to_owned();
let has_bad_blocks = chain_spec.bad_blocks_hashes().count() != 0;
let has_protocol_id = chain_spec.protocol_id().is_some();
let has_telemetry_endpoints = chain_spec.telemetry_endpoints().count() != 0;
let log_name = log_name.clone();
let block_number_bytes = usize::from(chain_spec.block_number_bytes());
let starting_block_number = chain_information
.as_ref()
.map(|ci| ci.as_ref().finalized_block_header.number)
.unwrap_or(0);
let starting_block_hash = chain_information
.as_ref()
.map(|ci| ci.as_ref().finalized_block_header.hash(block_number_bytes))
.unwrap_or(genesis_block_hash);
if let (None, None) = (&relay_chain_ready_future, &chain_information) {
return Err(AddChainError::ChainSpecNeitherGenesisStorageNorCheckpoint);
}
let future = async move {
// Wait until the relay chain has finished initializing, if necessary.
let relay_chain = if let Some((
mut relay_chain_ready_future,
para_id,
relay_chain_log_name,
)) = relay_chain_ready_future
{
(&mut relay_chain_ready_future).await;
let running_relay_chain = pin::Pin::new(&mut relay_chain_ready_future)
.take_output()
.unwrap();
Some((running_relay_chain, para_id, relay_chain_log_name))
} else {
None
};
let running_chain = {
let config = match (&relay_chain, chain_information) {
(Some((relay_chain, para_id, _)), Some(chain_information)) => {
StartServicesChainTy::Parachain {
relay_chain,
finalized_block_header: chain_information
.as_ref()
.finalized_block_header
.scale_encoding_vec(block_number_bytes),
para_id: *para_id,
}
}
(Some((relay_chain, para_id, _)), None) => {
StartServicesChainTy::Parachain {
relay_chain,
finalized_block_header: genesis_block_header.clone(),
para_id: *para_id,
}
}
(None, Some(chain_information)) => {
StartServicesChainTy::RelayChain { chain_information }
}
(None, None) => {
// Checked above.
unreachable!()
}
};
start_services(
log_name.clone(),
&platform,
runtime_code_hint,
genesis_block_header,
block_number_bytes,
fork_id,
config,
network_identify_agent_version,
)
.await
};
// Note that the chain name is printed through the `Debug` trait (rather
// than `Display`) because it is an untrusted user input.
if let Some((_, para_id, relay_chain_log_name)) = relay_chain.as_ref() {
log::info!(
target: "smoldot",
"Parachain initialization complete for {}. Name: {:?}. Genesis \
hash: {}. Relay chain: {} (id: {})",
log_name,
chain_name,
HashDisplay(&genesis_block_hash),
relay_chain_log_name,
para_id,
);
} else {
log::info!(
target: "smoldot",
"Chain initialization complete for {}. Name: {:?}. Genesis \
hash: {}. {} starting at: {} (#{})",
log_name,
chain_name,
HashDisplay(&genesis_block_hash),
if used_database_chain_information { "Database" } else { "Chain specification" },
HashDisplay(&starting_block_hash),
starting_block_number
);
}
if print_warning_genesis_root_chainspec {
log::info!(
target: "smoldot",
"Chain specification of {} contains a `genesis.raw` item. It is \
possible to significantly improve the initialization time by \
replacing the `\"raw\": ...` field with \
`\"stateRootHash\": \"0x{}\"`",
log_name, hex::encode(genesis_block_state_root)
)
}
if has_protocol_id {
log::warn!(
target: "smoldot",
"Chain specification of {} contains a `protocolId` field. This \
field is deprecated and its value is no longer used. It can be \
safely removed from the JSON document.", log_name
);
}
if has_telemetry_endpoints {
log::warn!(
target: "smoldot",
"Chain specification of {} contains a non-empty \
`telemetryEndpoints` field. Smoldot doesn't support telemetry \
endpoints and as such this field is unused.", log_name
);
}
// TODO: remove after https://github.com/paritytech/smoldot/issues/2584
if has_bad_blocks {
log::warn!(
target: "smoldot",
"Chain specification of {} contains a list of bad blocks. Bad \
blocks are not implemented in the light client. An appropriate \
way to silence this warning is to remove the bad blocks from the \
chain specification, which can safely be done:\n\
- For relay chains: if the chain specification contains a \
checkpoint and that the bad blocks have a block number inferior \
to this checkpoint.\n\
- For parachains: if the bad blocks have a block number inferior \
to the current parachain finalized block.", log_name
);
}
if database_was_wrong_chain {
log::warn!(
target: "smoldot",
"Ignore database of {} because its genesis hash didn't match the \
genesis hash of the chain.", log_name
)
}
running_chain
};
let (background_future, output_future) = future.remote_handle();
self.platform
.spawn_task("services-initialization".into(), background_future.boxed());
output_future
};
let entry = entry.insert(RunningChain {
services: future::maybe_done(running_chain_init_future.shared()),
log_name,
num_references: NonZeroU32::new(1).unwrap(),
});
(&mut entry.services, &entry.log_name)
}
};
if !invalid_bootstrap_nodes_sanitized.is_empty() {
log::warn!(
target: "smoldot",
"Failed to parse some of the bootnodes of {}. \
These bootnodes have been ignored. List: {}",
log_name, invalid_bootstrap_nodes_sanitized.join(", ")
);
}
// Print a warning if the list of bootnodes is empty, as this is a common mistake.
if bootstrap_nodes.is_empty() {
// Note the usage of the word "likely", because another chain with the same key might
// have been added earlier and contains bootnodes, or we might receive an incoming
// substream on a connection normally used for a different chain.
log::warn!(
target: "smoldot",
"Newly-added chain {} has an empty list of bootnodes. Smoldot will likely fail \
to connect to its peer-to-peer network.",
log_name
);
}
// Apart from its services, each chain also has an entry in `public_api_chains`.
let public_api_chains_entry = self.public_api_chains.vacant_entry();
let new_chain_id = ChainId(public_api_chains_entry.key());
// Multiple chains can share the same network service, but each specify different
// bootstrap nodes and database nodes. In order to resolve this, each chain adds their own
// bootnodes and database nodes to the network service after it has been initialized. This
// is done by adding a short-lived task that waits for the chain initialization to finish
// then adds the nodes.
self.platform
.spawn_task("network-service-add-initial-topology".into(), {
// Clone `running_chain_init`.
let mut running_chain_init = match services_init {
future::MaybeDone::Done(d) => future::MaybeDone::Done(d.clone()),
future::MaybeDone::Future(d) => future::MaybeDone::Future(d.clone()),
future::MaybeDone::Gone => unreachable!(),
};
async move {
// Wait for the chain to finish initializing to proceed.
(&mut running_chain_init).await;
let running_chain = pin::Pin::new(&mut running_chain_init)
.take_output()
.unwrap();
running_chain
.network_service
.discover(running_chain.network_service_chain_id, known_nodes, false)
.await;
running_chain
.network_service
.discover(
running_chain.network_service_chain_id,
bootstrap_nodes,
true,
)
.await;
}
.boxed()
});
// JSON-RPC service initialization. This is done every time `add_chain` is called, even
// if a similar chain already existed.
let json_rpc_frontend = if let AddChainConfigJsonRpc::Enabled {
max_pending_requests,
max_subscriptions,
} = config.json_rpc
{
// Clone `running_chain_init`.
let mut running_chain_init = match services_init {
future::MaybeDone::Done(d) => future::MaybeDone::Done(d.clone()),
future::MaybeDone::Future(d) => future::MaybeDone::Future(d.clone()),
future::MaybeDone::Gone => unreachable!(),
};
let (frontend, service_starter) = json_rpc_service::service(json_rpc_service::Config {
log_name: log_name.clone(), // TODO: add a way to differentiate multiple different json-rpc services under the same chain
max_pending_requests,
max_subscriptions,
// Note that the settings below are intentionally not exposed in the publicly
// available configuration, as "good" values depend on the global number of tasks.
// In other words, these constants are relative to the number of other things that
// happen within the client rather than absolute values. Since the user isn't
// supposed to know what happens within the client, they can't rationally decide
// what value is appropriate.
max_parallel_requests: NonZeroU32::new(24).unwrap(),
});
let system_name = self.platform.client_name().into_owned();
let system_version = self.platform.client_version().into_owned();
let platform = self.platform.clone();
let init_future = async move {
// Wait for the chain to finish initializing before starting the JSON-RPC service.
(&mut running_chain_init).await;
let running_chain = pin::Pin::new(&mut running_chain_init)
.take_output()
.unwrap();
service_starter.start(json_rpc_service::StartConfig {
platform,
sync_service: running_chain.sync_service,
network_service: (
running_chain.network_service,
running_chain.network_service_chain_id,
),
transactions_service: running_chain.transactions_service,
runtime_service: running_chain.runtime_service,
chain_spec: &chain_spec,
system_name,
system_version,
genesis_block_hash,
genesis_block_state_root,
})
};
self.platform
.spawn_task("json-rpc-service-init".into(), init_future.boxed());
Some(frontend)
} else {
None
};
// Success!
let public_api_chain_destroyed_event = event_listener::Event::new();
let public_api_chain_destroyed = public_api_chain_destroyed_event.listen();
public_api_chains_entry.insert(PublicApiChain {
user_data: config.user_data,
key: new_chain_key,
chain_spec_chain_id,
json_rpc_frontend: json_rpc_frontend.clone(),
public_api_chain_destroyed_event,
});
Ok(AddChainSuccess {
chain_id: new_chain_id,
json_rpc_responses: json_rpc_frontend.map(|f| JsonRpcResponses {
inner: Some(f),
public_api_chain_destroyed,