Skip to content

Commit

Permalink
Merge pull request #966 from subspace/dsn-sync-for-node
Browse files Browse the repository at this point in the history
Fix DSN sync for node
  • Loading branch information
nazar-pc authored Nov 29, 2022
2 parents 6a700d2 + cccff3c commit 409f2bb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/subspace-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ sc-cli = { version = "0.10.0-dev", git = "https://github.com/subspace/substrate"
sc-client-api = { version = "4.0.0-dev", git = "https://github.com/subspace/substrate", rev = "6d57dbc639bb3d9460dabeccb063cc6556452535" }
sc-consensus = { version = "0.10.0-dev", git = "https://github.com/subspace/substrate", rev = "6d57dbc639bb3d9460dabeccb063cc6556452535" }
sc-consensus-slots = { version = "0.10.0-dev", git = "https://github.com/subspace/substrate", rev = "6d57dbc639bb3d9460dabeccb063cc6556452535" }
sc-consensus-subspace = { version = "0.1.0", path = "../sc-consensus-subspace" }
sc-subspace-chain-specs = { version = "0.1.0", path = "../sc-subspace-chain-specs" }
sc-executor = { version = "0.10.0-dev", git = "https://github.com/subspace/substrate", rev = "6d57dbc639bb3d9460dabeccb063cc6556452535", features = ["wasmtime"] }
sc-service = { version = "0.10.0-dev", git = "https://github.com/subspace/substrate", rev = "6d57dbc639bb3d9460dabeccb063cc6556452535", default-features = false, features = ["wasmtime"] }
Expand All @@ -58,7 +59,6 @@ subspace-runtime-primitives = { version = "0.1.0", path = "../subspace-runtime-p
subspace-service = { version = "0.1.0", path = "../subspace-service" }
system-domain-runtime = { version = "0.1.0", path = "../../domains/runtime/system" }
thiserror = "1.0.32"
tokio = "1.21.2"

[build-dependencies]
substrate-build-script-utils = { version = "3.0.0", git = "https://github.com/subspace/substrate", rev = "6d57dbc639bb3d9460dabeccb063cc6556452535" }
Expand Down
13 changes: 12 additions & 1 deletion crates/subspace-node/src/bin/subspace-node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,21 @@ fn main() -> Result<(), Error> {
client,
import_queue,
task_manager,
other: (_block_import, subspace_link, _telemetry),
..
} = subspace_service::new_partial::<RuntimeApi, ExecutorDispatch>(&config)?;

sc_consensus_subspace::start_subspace_archiver(
&subspace_link,
client.clone(),
None,
&task_manager.spawn_essential_handle(),
config.role.is_authority(),
);

Ok((
cmd.run(client, import_queue).map_err(Error::SubstrateCli),
cmd.run(client, import_queue, task_manager.spawn_essential_handle())
.map_err(Error::SubstrateCli),
task_manager,
))
})?;
Expand Down
55 changes: 45 additions & 10 deletions crates/subspace-node/src/import_blocks_from_dsn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use sc_consensus::{BlockImportError, BlockImportStatus, IncomingBlock, Link};
use sc_service::ImportQueue;
use sc_tracing::tracing::{debug, info, trace};
use sp_consensus::BlockOrigin;
use sp_core::traits::SpawnEssentialNamed;
use sp_runtime::generic::BlockId;
use sp_runtime::traits::{Block as BlockT, Header, NumberFor};
use std::sync::Arc;
Expand Down Expand Up @@ -58,15 +59,26 @@ pub struct ImportBlocksFromDsnCmd {

impl ImportBlocksFromDsnCmd {
/// Run the import-blocks command
pub async fn run<B, C, IQ>(&self, client: Arc<C>, import_queue: IQ) -> sc_cli::Result<()>
pub async fn run<B, C, IQ>(
&self,
client: Arc<C>,
import_queue: IQ,
spawner: impl SpawnEssentialNamed,
) -> sc_cli::Result<()>
where
C: HeaderBackend<B> + BlockBackend<B> + Send + Sync + 'static,
B: BlockT + for<'de> serde::Deserialize<'de>,
IQ: sc_service::ImportQueue<B> + 'static,
{
import_blocks(self.bootstrap_node.clone(), client, import_queue, false)
.await
.map_err(Into::into)
import_blocks(
self.bootstrap_node.clone(),
client,
import_queue,
&spawner,
false,
)
.await
.map_err(Into::into)
}
}

Expand Down Expand Up @@ -109,7 +121,7 @@ impl<B: BlockT> Link<B> for WaitLink<B> {
B::Hash,
)>,
) {
println!("Imported {imported} blocks");
debug!("Imported {imported} blocks");
self.imported_blocks += imported as u64;

for result in results {
Expand All @@ -126,6 +138,7 @@ async fn import_blocks<B, IQ, C>(
bootstrap_nodes: Vec<Multiaddr>,
client: Arc<C>,
mut import_queue: IQ,
spawner: &impl SpawnEssentialNamed,
force: bool,
) -> Result<(), sc_service::Error>
where
Expand All @@ -142,9 +155,13 @@ where
.await
.map_err(|error| sc_service::Error::Other(error.to_string()))?;

tokio::spawn(async move {
node_runner.run().await;
});
spawner.spawn_essential(
"node-runner",
Some("subspace-networking"),
Box::pin(async move {
node_runner.run().await;
}),
);

debug!("Waiting for connected peers...");
let _ = node.wait_for_connected_peers().await;
Expand Down Expand Up @@ -263,10 +280,28 @@ where
}
}

while link.imported_blocks < imported_blocks {
futures::future::poll_fn(|ctx| {
import_queue.poll_actions(ctx, &mut link);

Poll::Ready(())
})
.await;

if let Some(WaitLinkError { error, hash }) = &link.error {
return Err(sc_service::Error::Other(format!(
"Stopping block import after #{} blocks on {} because of an error: {}",
link.imported_blocks, hash, error
)));
}
}

info!(
"🎉 Imported {} blocks, best #{}, exiting...",
"🎉 Imported {} blocks, best #{}/#{}, check against reliable sources to make sure it is a \
block on canonical chain",
imported_blocks,
client.info().best_number
client.info().best_number,
client.info().best_hash
);

Ok(())
Expand Down

0 comments on commit 409f2bb

Please sign in to comment.