Skip to content

Commit

Permalink
Support optionally providing piece getter for DSN sync during node in…
Browse files Browse the repository at this point in the history
…stantiation
  • Loading branch information
nazar-pc committed Feb 4, 2024
1 parent 111e6df commit 519aabb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ fn main() -> Result<(), Error> {
// Domain node needs slots notifications for bundle production.
force_new_slot_notifications: true,
subspace_networking: SubspaceNetworking::Create { config: dsn_config },
dsn_piece_getter: None,
sync_from_dsn: true,
is_timekeeper: false,
timekeeper_cpu_cores: Default::default(),
Expand Down
7 changes: 7 additions & 0 deletions crates/subspace-networking/src/utils/piece_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use futures::StreamExt;
use libp2p::PeerId;
use std::collections::HashSet;
use std::error::Error;
use std::fmt;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use subspace_core_primitives::{Piece, PieceIndex};
Expand Down Expand Up @@ -63,6 +64,12 @@ pub struct PieceProvider<PV> {
piece_validator: Option<PV>,
}

impl<PV> fmt::Debug for PieceProvider<PV> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PieceProvider").finish_non_exhaustive()
}
}

impl<PV> PieceProvider<PV>
where
PV: PieceValidator,
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-node/src/commands/run/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ pub(super) fn create_consensus_chain_configuration(
// Domain node needs slots notifications for bundle production.
force_new_slot_notifications: domains_enabled,
subspace_networking: SubspaceNetworking::Create { config: dsn_config },
dsn_piece_getter: None,
sync_from_dsn,
is_timekeeper: timekeeper_options.timekeeper,
timekeeper_cpu_cores: timekeeper_options.timekeeper_cpu_cores,
Expand Down
3 changes: 3 additions & 0 deletions crates/subspace-service/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::dsn::DsnConfig;
use crate::sync_from_dsn::DsnSyncPieceGetter;
use prometheus_client::registry::Registry;
use sc_chain_spec::ChainSpec;
use sc_network::config::{
Expand Down Expand Up @@ -242,6 +243,8 @@ pub struct SubspaceConfiguration {
pub force_new_slot_notifications: bool,
/// Subspace networking (DSN).
pub subspace_networking: SubspaceNetworking,
/// DSN piece getter
pub dsn_piece_getter: Option<Arc<dyn DsnSyncPieceGetter + Send + Sync + 'static>>,
/// Enables DSN-sync on startup.
pub sync_from_dsn: bool,
/// Is this node a Timekeeper
Expand Down
18 changes: 10 additions & 8 deletions crates/subspace-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,14 +846,16 @@ where

network_wrapper.set(network_service.clone());
if config.sync_from_dsn {
let piece_provider = PieceProvider::new(
node.clone(),
Some(SegmentCommitmentPieceValidator::new(
let dsn_sync_piece_getter = config.dsn_piece_getter.unwrap_or_else(|| {
Arc::new(PieceProvider::new(
node.clone(),
subspace_link.kzg().clone(),
segment_headers_store.clone(),
)),
);
Some(SegmentCommitmentPieceValidator::new(
node.clone(),
subspace_link.kzg().clone(),
segment_headers_store.clone(),
)),
))
});

let (observer, worker) = sync_from_dsn::create_observer_and_worker(
segment_headers_store.clone(),
Expand All @@ -863,7 +865,7 @@ where
import_queue_service,
sync_target_block_number,
pause_sync,
piece_provider,
dsn_sync_piece_getter,
);
task_manager
.spawn_handle()
Expand Down
5 changes: 3 additions & 2 deletions crates/subspace-service/src/sync_from_dsn/import_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use sp_runtime::generic::SignedBlock;
use sp_runtime::traits::{Block as BlockT, Header, NumberFor, One};
use sp_runtime::Saturating;
use std::error::Error;
use std::fmt;
use std::num::NonZeroU16;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -41,7 +42,7 @@ use tracing::warn;

/// Trait representing a way to get pieces for DSN sync purposes
#[async_trait]
pub trait DsnSyncPieceGetter {
pub trait DsnSyncPieceGetter: fmt::Debug {
async fn get_piece(
&self,
piece_index: PieceIndex,
Expand All @@ -51,7 +52,7 @@ pub trait DsnSyncPieceGetter {
#[async_trait]
impl<T> DsnSyncPieceGetter for Arc<T>
where
T: DsnSyncPieceGetter + Send + Sync,
T: DsnSyncPieceGetter + Send + Sync + ?Sized,
{
async fn get_piece(
&self,
Expand Down

0 comments on commit 519aabb

Please sign in to comment.