Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collect specs if rpc is unavailable #687

Merged
merged 2 commits into from
Nov 15, 2023
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
22 changes: 19 additions & 3 deletions cli/src/collector/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,39 @@ use std::path::PathBuf;

use anyhow::{Context, Result};
use indexmap::IndexMap;
use log::info;
use log::{info, warn};

use crate::common::path::{ContentType, QrPath};
use crate::common::types::MetaVersion;
use crate::export::{ExportChainSpec, ExportData, MetadataQr, QrCode, ReactAssetPath};
use crate::fetch::Fetcher;
use crate::fetch::{fetch_deployed_data, Fetcher};
use crate::qrs::{collect_metadata_qrs, metadata_files, spec_files};
use crate::AppConfig;

pub(crate) fn export_specs(config: &AppConfig, fetcher: impl Fetcher) -> Result<ExportData> {
let all_specs = spec_files(&config.qr_dir)?;
let all_metadata = metadata_files(&config.qr_dir)?;
let online = fetch_deployed_data(config).ok();

let mut export_specs = IndexMap::new();
for chain in &config.chains {
info!("Collecting {} info...", chain.name);
let specs = fetcher.fetch_specs(chain)?;
let specs = match fetcher.fetch_specs(chain) {
Ok(specs) => specs,
Err(e) => {
if let Some(online_specs) = online.as_ref() {
if let Some(online_chain_specs) = online_specs.get(&chain.portal_id()) {
warn!(
"Unable to fetch specs for {}. Keep current online specs. Err: {}.",
chain.name, e
);
export_specs.insert(chain.portal_id(), online_chain_specs.clone());
continue;
}
}
return Err(e);
}
};
let meta = fetcher.fetch_metadata(chain)?;
let live_meta_version = meta.meta_values.version;

Expand Down
21 changes: 2 additions & 19 deletions cli/src/deployment_checker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
use std::fs;
use std::path::Path;
use std::process::exit;

use anyhow::Result;
use log::{info, warn};
use reqwest::Url;
use serde::{Deserialize, Serialize};

use crate::collector::export::export_specs;
use crate::export::{ExportData, ReactAssetPath};
use crate::fetch::RpcFetcher;
use crate::fetch::{fetch_deployed_data, RpcFetcher};
use crate::AppConfig;

#[derive(Serialize, Deserialize)]
struct PkgJson {
homepage: String,
}

// Check whether the deployment is up to date.
// Exit code 12 if re-deploy is required
pub(crate) fn check_deployment(config: AppConfig) -> Result<()> {
let pkg_json = fs::read_to_string(Path::new("package.json"))?;
let pkg_json: PkgJson = serde_json::from_str(&pkg_json)?;

let data_file = ReactAssetPath::from_fs_path(&config.data_file, &config.public_dir)?;
let url = Url::parse(&pkg_json.homepage)?;
let url = url.join(&data_file.to_string())?;

let online = reqwest::blocking::get(url)?.json::<ExportData>()?;
let online = fetch_deployed_data(&config)?;
let local = export_specs(&config, RpcFetcher);

if let Err(e) = local {
Expand Down
4 changes: 2 additions & 2 deletions cli/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl fmt::Display for ReactAssetPath {
}
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ExportChainSpec {
pub(crate) title: String,
Expand All @@ -51,7 +51,7 @@ pub(crate) struct ExportChainSpec {
pub(crate) relay_chain: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub(crate) struct MetadataQr {
pub(crate) version: u32,
Expand Down
23 changes: 22 additions & 1 deletion cli/src/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use std::fs;
use std::path::Path;

use anyhow::{anyhow, bail, Result};
use definitions::crypto::Encryption;
use definitions::network_specs::NetworkSpecs;
use generate_message::helpers::{meta_fetch, specs_agnostic, MetaFetched};
use generate_message::parser::Token;
use log::warn;
use reqwest::Url;
use serde::{Deserialize, Serialize};

use crate::config::Chain;
use crate::config::{AppConfig, Chain};
use crate::ethereum::is_ethereum;
use crate::export::{ExportData, ReactAssetPath};

pub(crate) trait Fetcher {
fn fetch_specs(&self, chain: &Chain) -> Result<NetworkSpecs>;
Expand Down Expand Up @@ -68,3 +74,18 @@ impl Fetcher for RpcFetcher {
Ok(meta)
}
}

#[derive(Serialize, Deserialize)]
struct PkgJson {
homepage: String,
}
pub(crate) fn fetch_deployed_data(config: &AppConfig) -> Result<ExportData> {
let pkg_json = fs::read_to_string(Path::new("package.json"))?;
let pkg_json: PkgJson = serde_json::from_str(&pkg_json)?;

let data_file = ReactAssetPath::from_fs_path(&config.data_file, &config.public_dir)?;
let url = Url::parse(&pkg_json.homepage)?;
let url = url.join(&data_file.to_string())?;

Ok(reqwest::blocking::get(url)?.json::<ExportData>()?)
}