Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,7 @@ serde = { version = "1.0.214", default-features = false }
serde-big-array = { version = "0.3.2" }
serde_derive = { version = "1.0.117" }
serde_json = { version = "1.0.132", default-features = false }
serde_with = { version = "3.12.0", default-features = false, features = ["hex", "macros"] }
serde_yaml = { version = "0.9" }
sha1 = { version = "0.10.6" }
sha2 = { version = "0.10.7", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions cumulus/client/relay-chain-minimal-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,22 @@ fn build_authority_discovery_service<Block: BlockT>(
_ => None,
}
});
let net_config_path = config.network.net_config_path.clone();
let (worker, service) = sc_authority_discovery::new_worker_and_service_with_config(
sc_authority_discovery::WorkerConfig {
publish_non_global_ips: auth_disc_publish_non_global_ips,
public_addresses: auth_disc_public_addresses,
// Require that authority discovery records are signed.
strict_record_validation: true,
persisted_cache_directory: net_config_path,
..Default::default()
},
client,
Arc::new(network.clone()),
Box::pin(dht_event_stream),
authority_discovery_role,
prometheus_registry,
task_manager.spawn_handle(),
);

task_manager.spawn_handle().spawn(
Expand Down
3 changes: 3 additions & 0 deletions polkadot/node/service/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ where
);
}

let network_config = config.network.clone();
let rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams {
config,
backend: backend.clone(),
Expand Down Expand Up @@ -573,13 +574,15 @@ where
public_addresses: auth_disc_public_addresses,
// Require that authority discovery records are signed.
strict_record_validation: true,
persisted_cache_directory: network_config.net_config_path,
..Default::default()
},
client.clone(),
Arc::new(network.clone()),
Box::pin(dht_event_stream),
authority_discovery_role,
prometheus_registry.clone(),
task_manager.spawn_handle(),
);

task_manager.spawn_handle().spawn(
Expand Down
23 changes: 23 additions & 0 deletions prdoc/pr_8839.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
title: "net/discovery: File persistence for AddrCache"
doc:
- audience: Node Dev
description: |-
Persisting the AddrCache periodically (every 10 minutes) and on worker
shutdown. Read AddrCache from file upon launch of worker.

AddrCache is saved as authority_discovery_addr_cache.json in the
folder configured by net_config_path of NetworkConfiguration.

This reduces the time it takes for a node to reconnect to peers after
restart.
crates:
- name: sc-authority-discovery
bump: major
- name: sc-network-types
bump: minor
- name: cumulus-relay-chain-minimal-node
bump: patch
- name: polkadot-service
bump: patch
- name: staging-node-cli
bump: patch
3 changes: 3 additions & 0 deletions substrate/bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ pub fn new_full_base<N: NetworkBackend<Block, <Block as BlockT>::Hash>>(
task_manager.spawn_handle().spawn("mixnet", None, mixnet);
}

let net_config_path = config.network.net_config_path.clone();
let rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams {
config,
backend: backend.clone(),
Expand Down Expand Up @@ -659,13 +660,15 @@ pub fn new_full_base<N: NetworkBackend<Block, <Block as BlockT>::Hash>>(
sc_authority_discovery::WorkerConfig {
publish_non_global_ips: auth_disc_publish_non_global_ips,
public_addresses: auth_disc_public_addresses,
persisted_cache_directory: net_config_path,
..Default::default()
},
client.clone(),
Arc::new(network.clone()),
Box::pin(dht_event_stream),
authority_discovery_role,
prometheus_registry.clone(),
task_manager.spawn_handle(),
);

task_manager.spawn_handle().spawn(
Expand Down
5 changes: 5 additions & 0 deletions substrate/client/authority-discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,23 @@ rand = { workspace = true, default-features = true }
sc-client-api = { workspace = true, default-features = true }
sc-network = { workspace = true, default-features = true }
sc-network-types = { workspace = true, default-features = true }
serde.workspace = true
serde_json.workspace = true
sp-api = { workspace = true, default-features = true }
sp-authority-discovery = { workspace = true, default-features = true }
sp-blockchain = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-keystore = { workspace = true, default-features = true }
sp-runtime = { workspace = true, default-features = true }
thiserror = { workspace = true }
tokio.workspace = true

[dev-dependencies]
hex.workspace = true
quickcheck = { workspace = true }
sp-tracing = { workspace = true, default-features = true }
substrate-test-runtime-client = { workspace = true }
tempfile.workspace = true

[build-dependencies]
prost-build = { workspace = true }
3 changes: 3 additions & 0 deletions substrate/client/authority-discovery/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ pub enum Error {
#[error("Failed to encode or decode scale payload.")]
EncodingDecodingScale(#[from] codec::Error),

#[error("Failed to encode or decode AddrCache.")]
EncodingDecodingAddrCache(String),

#[error("Failed to parse a libp2p multi address.")]
ParsingMultiaddress(#[from] sc_network::multiaddr::ParseError),

Expand Down
25 changes: 21 additions & 4 deletions substrate/client/authority-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub use crate::{
worker::{AuthorityDiscovery, NetworkProvider, Role, Worker},
};

use std::{collections::HashSet, sync::Arc, time::Duration};
use std::{collections::HashSet, path::PathBuf, sync::Arc, time::Duration};

use futures::{
channel::{mpsc, oneshot},
Expand All @@ -44,8 +44,8 @@ use sc_network::{event::DhtEvent, Multiaddr};
use sc_network_types::PeerId;
use sp_authority_discovery::AuthorityId;
use sp_blockchain::HeaderBackend;
use sp_core::traits::SpawnNamed;
use sp_runtime::traits::Block as BlockT;

mod error;
mod interval;
mod service;
Expand Down Expand Up @@ -88,6 +88,11 @@ pub struct WorkerConfig {
///
/// Defaults to `false` to provide compatibility with old versions
pub strict_record_validation: bool,

/// The directory of where the persisted AddrCache file is located,
/// optional since NetworkConfiguration's `net_config_path` field
/// is optional. If None, we won't persist the AddrCache at all.
pub persisted_cache_directory: Option<PathBuf>,
}

impl Default for WorkerConfig {
Expand All @@ -110,6 +115,7 @@ impl Default for WorkerConfig {
publish_non_global_ips: true,
public_addresses: Vec::new(),
strict_record_validation: false,
persisted_cache_directory: None,
}
}
}
Expand All @@ -123,6 +129,7 @@ pub fn new_worker_and_service<Client, Block, DhtEventStream>(
dht_event_rx: DhtEventStream,
role: Role,
prometheus_registry: Option<prometheus_endpoint::Registry>,
spawner: impl SpawnNamed + 'static,
) -> (Worker<Client, Block, DhtEventStream>, Service)
where
Block: BlockT + Unpin + 'static,
Expand All @@ -136,6 +143,7 @@ where
dht_event_rx,
role,
prometheus_registry,
spawner,
)
}

Expand All @@ -149,6 +157,7 @@ pub fn new_worker_and_service_with_config<Client, Block, DhtEventStream>(
dht_event_rx: DhtEventStream,
role: Role,
prometheus_registry: Option<prometheus_endpoint::Registry>,
spawner: impl SpawnNamed + 'static,
) -> (Worker<Client, Block, DhtEventStream>, Service)
where
Block: BlockT + Unpin + 'static,
Expand All @@ -157,8 +166,16 @@ where
{
let (to_worker, from_service) = mpsc::channel(0);

let worker =
Worker::new(from_service, client, network, dht_event_rx, role, prometheus_registry, config);
let worker = Worker::new(
from_service,
client,
network,
dht_event_rx,
role,
prometheus_registry,
config,
spawner,
);
let service = Service::new(to_worker);

(worker, service)
Expand Down
Loading
Loading