Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpolaczyk committed Sep 19, 2024
1 parent cd9e27e commit 38b528f
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 169 deletions.
17 changes: 9 additions & 8 deletions client/consensus/src/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>.

use {
crate::collators::lookahead::BuyCoreParams,
crate::{
collators::lookahead::Params as LookAheadParams, OrchestratorAuraWorkerAuxData,
SlotFrequency,
Expand All @@ -23,7 +24,10 @@ use {
cumulus_client_collator::service::CollatorService,
cumulus_client_consensus_common::{ParachainBlockImportMarker, ValidationCodeHashProvider},
cumulus_client_consensus_proposer::Proposer as ConsensusProposer,
cumulus_primitives_core::{relay_chain::BlockId, CollationInfo, CollectCollationInfo, ParaId},
cumulus_primitives_core::{
relay_chain::{BlockId, ValidationCodeHash},
CollationInfo, CollectCollationInfo, ParaId,
},
cumulus_relay_chain_interface::{
CommittedCandidateReceipt, OverseerHandle, RelayChainInterface, RelayChainResult,
StorageValue,
Expand All @@ -35,7 +39,10 @@ use {
pallet_xcm_core_buyer_runtime_api::BuyingError,
parity_scale_codec::Encode,
polkadot_core_primitives::{Header as PHeader, InboundDownwardMessage, InboundHrmpMessage},
polkadot_node_subsystem::messages::{RuntimeApiMessage, RuntimeApiRequest},
polkadot_node_subsystem::{
messages::{RuntimeApiMessage, RuntimeApiRequest},
overseer, OverseerSignal,
},
polkadot_overseer::dummy::dummy_overseer_builder,
polkadot_parachain_primitives::primitives::HeadData,
polkadot_primitives::{
Expand Down Expand Up @@ -512,12 +519,6 @@ impl<B: BlockT> sc_consensus::Verifier<B> for SealExtractorVerfier {
}
}

use crate::collators::lookahead::BuyCoreParams;
use {
cumulus_primitives_core::relay_chain::ValidationCodeHash,
polkadot_node_subsystem::{overseer, OverseerSignal},
};

pub struct DummyCodeHashProvider;
impl ValidationCodeHashProvider<PHash> for DummyCodeHashProvider {
fn code_hash_at(&self, _at: PHash) -> Option<ValidationCodeHash> {
Expand Down
4 changes: 2 additions & 2 deletions client/service-container-chain/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>

use sc_cli::CliConfiguration;
use url::Url;
use {
crate::chain_spec::RawGenesisConfig,
cumulus_client_cli::{CollatorOptions, RelayChainMode},
dc_orchestrator_chain_interface::ContainerChainGenesisData,
dp_container_chain_genesis_data::json::properties_to_map,
sc_chain_spec::ChainSpec,
sc_cli::CliConfiguration,
sc_network::config::MultiaddrWithPeerId,
sp_runtime::Storage,
std::{collections::BTreeMap, net::SocketAddr},
url::Url,
};

/// The `run` command used to run a container chain node.
Expand Down
3 changes: 1 addition & 2 deletions client/service-container-chain/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use {
substrate_prometheus_endpoint::Registry,
tc_consensus::{
collators::lookahead::{
self as lookahead_tanssi_aura, Params as LookaheadTanssiAuraParams,
self as lookahead_tanssi_aura, BuyCoreParams, Params as LookaheadTanssiAuraParams,
},
OrchestratorAuraWorkerAuxData,
},
Expand All @@ -61,7 +61,6 @@ use {

#[allow(deprecated)]
use sc_executor::NativeElseWasmExecutor;
use tc_consensus::collators::lookahead::BuyCoreParams;

type FullBackend = TFullBackend<Block>;

Expand Down
13 changes: 1 addition & 12 deletions node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>.

use tc_service_container_chain::cli::ContainerChainRunCmd;
use {
node_common::service::Sealing,
sc_cli::{CliConfiguration, NodeKeyParams, SharedParams},
std::path::PathBuf,
tc_service_container_chain::cli::ContainerChainRunCmd,
};

/// Sub-commands supported by the collator.
Expand Down Expand Up @@ -86,17 +86,6 @@ pub struct SoloChainCmd {
#[arg(long)]
pub no_hardware_benchmarks: bool,

/*
/// Enable the development service to run without a backing relay chain
#[arg(long)]
pub dev_service: bool,
/// When blocks should be sealed in the dev service.
///
/// Options are "instant", "manual", or timer interval in milliseconds
#[arg(long, default_value = "instant")]
pub sealing: Sealing,
*/
/// Relay chain arguments
#[arg(raw = true)]
pub relay_chain_args: Vec<String>,
Expand Down
42 changes: 5 additions & 37 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>.

use crate::command::solochain::relay_chain_cli_new;
use {
crate::{
chain_spec,
Expand Down Expand Up @@ -343,8 +344,8 @@ pub fn run() -> Result<()> {
Some(Subcommand::SoloChain(cmd)) => {
// Cannot use create_configuration function because that needs a chain spec.
// So write our own `create_runner` function that doesn't need chain spec.
let normalized_run = cmd.run.normalize();
let runner = solochain::create_runner(&cli, &normalized_run)?;
let container_chain_cli = cmd.run.normalize();
let runner = solochain::create_runner(&cli, &container_chain_cli)?;

// TODO: Assert that there are no flags between `tanssi-node` and `solo-chain`.
// These will be ignored anyway.
Expand All @@ -359,56 +360,23 @@ pub fn run() -> Result<()> {
let collator_options = cmd.run.collator_options();

runner.run_node_until_exit(|config| async move {
/*
let polkadot_cli = RelayChainCli::new(
let polkadot_cli = relay_chain_cli_new(
&config,
[RelayChainCli::executable_name()]
.iter()
.chain(cmd.relay_chain_args.iter()),
);
*/
// TODO: refactor this into function that returns `RelayChainCli`
let binding = [RelayChainCli::executable_name()];
let relay_chain_args = binding.iter().chain(cmd.relay_chain_args.iter());
let polkadot_cli = {
let base_path = config.base_path.path().join("polkadot");

RelayChainCli {
base_path,
chain_id: Some(config.relay_chain.clone()),
base: clap::Parser::parse_from(relay_chain_args),
}
};

// TODO: dev mode does not make sense for starlight collator?
// But better to detect --dev flag and panic than ignore it
/*
let dev_service = config.chain_spec.is_dev()
|| relay_chain_id == Some("dev-service".to_string())
|| cli_run_dev_service;
if dev_service {
return crate::service::start_dev_node(config, cli_run_sealing, hwbench, id)
.map_err(Into::into);
}
*/

let tokio_handle = config.tokio_handle.clone();
let polkadot_config =
SubstrateCli::create_configuration(&polkadot_cli, &polkadot_cli, tokio_handle)
.map_err(|err| format!("Relay chain argument error: {}", err))?;

// We need to bake in some container-chain args
let container_chain_cli = normalized_run;
let tokio_handle = config.tokio_handle.clone();
let container_chain_config = (container_chain_cli, tokio_handle);

// TODO: we can't enable hwbench because we don't have a db. Find a workaround
let hwbench = None;

crate::service::start_solochain_node(
polkadot_config,
container_chain_config,
container_chain_cli,
collator_options,
hwbench,
)
Expand Down
87 changes: 49 additions & 38 deletions node/src/command/solochain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Helper functions used to implement solochain collator

use crate::cli::Cli;
use crate::cli::{Cli, RelayChainCli};
use futures::FutureExt;
use jsonrpsee::server::BatchRequestConfig;
use log::{info, warn};
Expand Down Expand Up @@ -249,6 +249,20 @@ fn init_cmd<T: CliConfiguration<DVC>, DVC: DefaultConfigurationValues>(
Ok(())
}

/// Equivalent to [RelayChainCli::new]
pub fn relay_chain_cli_new<'a>(
config: &SolochainConfig,
relay_chain_args: impl Iterator<Item = &'a String>,
) -> RelayChainCli {
let base_path = config.base_path.path().join("polkadot");

RelayChainCli {
base_path,
chain_id: Some(config.relay_chain.clone()),
base: clap::Parser::parse_from(relay_chain_args),
}
}

/// Create a dummy [Configuration] that should only be used as input to polkadot-sdk functions that
/// take this struct as input but only use one field of it.
/// This is needed because [Configuration] does not implement [Default].
Expand Down Expand Up @@ -333,12 +347,11 @@ pub fn dummy_config(tokio_handle: tokio::runtime::Handle, base_path: BasePath) -

/// Returns the default path for configuration directory based on the chain_spec
pub(crate) fn build_solochain_config_dir(base_path: &PathBuf) -> PathBuf {
// Original: Collator1000-01/chains/dancebox/
//base_path.path().join("chains").join(chain_id)
// Starlight: Collator1000-01/config/
// base_path: Collator1000-01/data/containers
// config_dir: Collator1000-01/data/config
let mut base_path = base_path.clone();
// Remove "/containers"
base_path.pop();

base_path.join("config")
}

Expand All @@ -365,46 +378,44 @@ fn zombienet_keystore_path(keystore: &KeystoreConfig) -> PathBuf {
/// When running under zombienet, collator keys are injected in a different folder from what we
/// expect. This function will check if the zombienet folder exists, and if so, copy all the keys
/// from there into the expected folder.
pub fn copy_zombienet_keystore(keystore: &KeystoreConfig) {
// TODO: error handling? Or assume keystore_path always exists?
pub fn copy_zombienet_keystore(keystore: &KeystoreConfig) -> std::io::Result<()> {
let keystore_path = keystore.path().unwrap();
let zombienet_path = zombienet_keystore_path(keystore);

if zombienet_path.exists() {
// Copy to keystore folder

// https://stackoverflow.com/a/65192210
// TODO: use a crate instead
// TODO: never overwrite files, only copy those that don't exist
fn copy_dir_all(
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
files_copied: &mut u32,
) -> std::io::Result<()> {
use std::fs;
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(
entry.path(),
dst.as_ref().join(entry.file_name()),
files_copied,
)?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
*files_copied += 1;
}
}
Ok(())
}

let mut files_copied = 0;
copy_dir_all(zombienet_path, keystore_path, &mut files_copied).unwrap();
copy_dir_all(zombienet_path, keystore_path, &mut files_copied)?;
log::info!("Copied {} keys from zombienet keystore", files_copied);

Ok(())
} else {
// TODO: remove this log before merging
log::warn!("Copy nimbus keys to {:?}", keystore_path);
// Zombienet folder does not exist, assume we are not running under zombienet
Ok(())
}
}

// https://stackoverflow.com/a/65192210
fn copy_dir_all(
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
files_copied: &mut u32,
) -> std::io::Result<()> {
use std::fs;
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(
entry.path(),
dst.as_ref().join(entry.file_name()),
files_copied,
)?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
*files_copied += 1;
}
}
Ok(())
}
Loading

0 comments on commit 38b528f

Please sign in to comment.