From 2dd469489581e4a1c9481f3a9d30f459f0855070 Mon Sep 17 00:00:00 2001 From: Anshal Shukla <53994948+anshalshukla@users.noreply.github.com> Date: Mon, 8 Jan 2024 00:58:46 +0530 Subject: [PATCH] add: env vars for explorer, branch based pull (#6) * add: env vars for explorer, branch based pull --- Cargo.lock | 1 + Cargo.toml | 1 + keypair.json | 9 --------- src/app/config.rs | 4 ---- src/cli/explorer.rs | 36 ++++++++++++++++++++++++++++++++++-- src/cli/init.rs | 20 ++------------------ src/utils/constants.rs | 4 ++++ src/utils/docker.rs | 13 +++++++------ src/utils/github.rs | 19 ++++++++++--------- src/utils/madara.rs | 6 +++--- 10 files changed, 62 insertions(+), 51 deletions(-) delete mode 100644 keypair.json diff --git a/Cargo.lock b/Cargo.lock index 2921d42..1b158b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1679,6 +1679,7 @@ dependencies = [ "hex", "inquire", "log", + "rand 0.8.5", "reqwest", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 58f8117..d3113ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ git2 = "0.18.1" hex = { version = "0.4.3", features = [] } inquire = "0.6.2" log = "0.4.20" +rand = "0.8.5" reqwest = { version = "0.11.23", features = ["json", "blocking"] } serde = { version = "1.0.193", features = ["derive"] } serde_json = "1.0.109" diff --git a/keypair.json b/keypair.json deleted file mode 100644 index 1f27b29..0000000 --- a/keypair.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "accountId": "0x00d2bf2d5f62e38d70739c45b56be6170ea26da6370c529cbf3b854b2c95f44f", - "networkId": "substrate", - "publicKey": "0x00d2bf2d5f62e38d70739c45b56be6170ea26da6370c529cbf3b854b2c95f44f", - "secretPhrase": "input scrap paddle tape fiction dentist differ sorry cargo cube mammal width", - "secretSeed": "0xfc9b279be387e666c3e8013c5abdf6780c8db8cb0e5374812e651cd1c331212f", - "ss58Address": "5C5nTkXmGFf56Ew7fXz1TmYzdGyhcTvAZ3KQeLrXeMfcFTFa", - "ss58PublicKey": "5C5nTkXmGFf56Ew7fXz1TmYzdGyhcTvAZ3KQeLrXeMfcFTFa" -} diff --git a/src/app/config.rs b/src/app/config.rs index cfc1e8c..c3b48ea 100644 --- a/src/app/config.rs +++ b/src/app/config.rs @@ -9,12 +9,8 @@ use crate::da::da_layers::DALayer; pub struct AppChainConfig { pub app_chain: String, pub base_path: String, - pub chain_id: String, pub mode: RollupMode, pub da_layer: DALayer, - pub block_time: u64, - pub disable_fees: bool, - pub fee_token: String, /// Stores commit hash of madara app chain build pub madara_version: String, /// Maintains version of config, will help in handling edge diff --git a/src/cli/explorer.rs b/src/cli/explorer.rs index 01fda4b..82c49c3 100644 --- a/src/cli/explorer.rs +++ b/src/cli/explorer.rs @@ -1,6 +1,38 @@ +use bollard::models::{HostConfig, PortBinding}; +use rand::distributions::Alphanumeric; +use rand::Rng; + +use std::collections::HashMap; + use crate::utils::docker::run_docker_image; pub fn explorer() { - let env_vars = vec!["RPC_API_HOST=\"http://127.0.0.1:9944\""]; - run_docker_image("ghcr.io/lambdaclass/stark_compass_explorer:v0.2.34.2", "madara-explorer", env_vars); + let random_string: String = (0..64).map(|_| rand::thread_rng().sample(Alphanumeric).to_string()).collect(); + let secret_key_base = format!("SECRET_KEY_BASE={}", random_string); + + let env = vec![ + "RPC_API_HOST=http://host.docker.internal:9944", + "DB_TYPE=sqlite", + "DISABLE_MAINNET_SYNC=false", + "DISABLE_TESTNET_SYNC=true", + "TESTNET_RPC_API_HOST=http://host.docker.internal:9944", + "DATABASE_PATH=/use/exp.db", + "PHX_HOST=localhost", + &secret_key_base, + ]; + + let mut port_bindings = HashMap::new(); + port_bindings.insert( + "4000/tcp".to_string(), + Some(vec![PortBinding { host_ip: Some("0.0.0.0".to_string()), host_port: Some("4000".to_string()) }]), + ); + + let host_config = HostConfig { port_bindings: Some(port_bindings), ..Default::default() }; + + run_docker_image( + "ghcr.io/lambdaclass/stark_compass_explorer:v0.2.34.2", + "madara-explorer", + Some(env), + Some(host_config), + ); } diff --git a/src/cli/init.rs b/src/cli/init.rs index 69b1363..f458511 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -4,7 +4,7 @@ use inquire::InquireError; use strum::IntoEnumIterator; use thiserror::Error; -use super::prompt::{get_boolean_input, get_custom_input, get_option, get_text_input}; +use super::prompt::{get_option, get_text_input}; use crate::app::config::{AppChainConfig, ConfigVersion, RollupMode}; use crate::da::da_layers::{DAFactory, DALayer}; use crate::utils::constants::{APP_CONFIG_NAME, MADARA_REPO_NAME, MADARA_REPO_ORG}; @@ -51,13 +51,8 @@ fn generate_config() -> Result { let default_base_path = binding.to_str().unwrap_or("madara-data"); let base_path = get_text_input("Enter base path for data directory of your app chain:", Some(default_base_path))?; - let chain_id = get_text_input("Enter chain id for your app chain:", Some("MADARA"))?; let mode = get_option("Select mode for your app chain:", RollupMode::iter().collect::>())?; let da_layer = get_option("Select DA layer for your app chain:", DALayer::iter().collect::>())?; - let block_time = - get_custom_input::("Enter block time of chain:", Some(1000), Some("Time in ms (e.g, 1000, 2000)."))?; - let disable_fees = get_boolean_input("Do you want to disable fees for your app chain:", Some(false))?; - let fee_token = get_text_input("Enter fee token:", Some("STRK"))?; let madara_version = get_latest_commit_hash(MADARA_REPO_ORG, MADARA_REPO_NAME)?; let config_version = ConfigVersion::Version1; @@ -69,18 +64,7 @@ fn generate_config() -> Result { } }; - Ok(AppChainConfig { - app_chain, - base_path, - chain_id, - mode, - da_layer, - block_time, - disable_fees, - fee_token, - madara_version, - config_version, - }) + Ok(AppChainConfig { app_chain, base_path, mode, da_layer, madara_version, config_version }) } fn write_config(config: &AppChainConfig) -> Result<(), InitError> { diff --git a/src/utils/constants.rs b/src/utils/constants.rs index 5a72f8f..f3277ce 100644 --- a/src/utils/constants.rs +++ b/src/utils/constants.rs @@ -1,6 +1,10 @@ pub const MADARA_REPO_ORG: &str = "keep-starknet-strange"; pub const MADARA_REPO_NAME: &str = "madara"; +pub const KARNOT_REPO_ORG: &str = "karnotxyz"; + +pub const BRANCH_NAME: &str = "custom_events_page"; + pub const APP_CONFIG_NAME: &str = "config.toml"; pub const APP_DA_CONFIG_NAME: &str = "da-config.json"; pub const APP_SECRET_PHRASE: &str = "secret-phrase.txt"; diff --git a/src/utils/docker.rs b/src/utils/docker.rs index ff4b096..e03090d 100644 --- a/src/utils/docker.rs +++ b/src/utils/docker.rs @@ -1,12 +1,12 @@ use bollard::container::{Config, CreateContainerOptions}; -use bollard::Docker; - use bollard::image::CreateImageOptions; +use bollard::models::HostConfig; +use bollard::Docker; use futures_util::TryStreamExt; -pub fn run_docker_image(image: &str, container_name: &str, env_vars: Vec<&str>) { +pub fn run_docker_image(image: &str, container_name: &str, env: Option>, host_config: Option) { is_docker_installed(); - match pull_and_start_docker_image(image, container_name, env_vars) { + match pull_and_start_docker_image(image, container_name, env, host_config) { Ok(..) => { log::info!("Successfully ran {}", container_name); } @@ -34,7 +34,8 @@ async fn is_docker_installed() -> bool { async fn pull_and_start_docker_image( image: &str, container_name: &str, - env_vars: Vec<&str>, + env: Option>, + host_config: Option, ) -> Result<(), Box> { let docker = Docker::connect_with_local_defaults().unwrap(); @@ -43,7 +44,7 @@ async fn pull_and_start_docker_image( .try_collect::>() .await?; - let config = Config { image: Some(image), tty: Some(true), env: Some(env_vars), ..Default::default() }; + let config = Config { image: Some(image), tty: Some(true), env, host_config, ..Default::default() }; let container_option = Some(CreateContainerOptions { name: container_name, ..Default::default() }); diff --git a/src/utils/github.rs b/src/utils/github.rs index 1d37566..b4bf704 100644 --- a/src/utils/github.rs +++ b/src/utils/github.rs @@ -32,7 +32,7 @@ pub fn get_latest_commit_hash(org: &str, repo: &str) -> Result Result<(), GithubError> { +pub fn git_clone(url: &str, path: &PathBuf, branch: Option<&str>) -> Result<(), GithubError> { if let Ok(repo) = Repository::open(path) { // Check if the repository is valid if repo.is_empty() == Ok(false) { @@ -51,14 +51,15 @@ pub fn git_clone(url: &str, path: &PathBuf) -> Result<(), GithubError> { fs::remove_dir_all(path)?; } - let output = Command::new("git") - .arg("clone") - .arg("--progress") - .arg(url) - .arg(path) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .output()?; + let mut cmd = Command::new("git"); + cmd.arg("clone").arg("--progress").arg(url).arg(path).stdout(Stdio::inherit()).stderr(Stdio::inherit()); + + if let Some(branch) = branch { + let clone_branch = format!("--branch={}", branch); + cmd.arg(clone_branch); + } + + let output = cmd.output()?; let status = output.status; diff --git a/src/utils/madara.rs b/src/utils/madara.rs index 3e800fd..e47a68f 100644 --- a/src/utils/madara.rs +++ b/src/utils/madara.rs @@ -1,6 +1,6 @@ use crate::da::da_layers::DALayer; use crate::utils::cmd::execute_cmd; -use crate::utils::constants::{APP_DA_CONFIG_NAME, MADARA_REPO_NAME, MADARA_REPO_ORG}; +use crate::utils::constants::{APP_DA_CONFIG_NAME, BRANCH_NAME, KARNOT_REPO_ORG, MADARA_REPO_NAME}; use crate::utils::errors::MadaraError; use crate::utils::github::git_clone; use crate::utils::paths::{get_app_home, get_madara_home}; @@ -8,10 +8,10 @@ use crate::utils::toml::regenerate_app_config; pub const GITHUB_BASE_URL: &str = "https://github.com"; pub fn clone_madara_and_build_repo() -> Result<(), MadaraError> { - let repo_url = format!("{}/{}/{}", GITHUB_BASE_URL, MADARA_REPO_ORG, MADARA_REPO_NAME); + let repo_url = format!("{}/{}/{}", GITHUB_BASE_URL, KARNOT_REPO_ORG, MADARA_REPO_NAME); let madara_path = get_madara_home()?.join("madara"); - match git_clone(&repo_url, &madara_path) { + match git_clone(&repo_url, &madara_path, Some(BRANCH_NAME)) { Ok(_) => { log::info!("Successfully cloned Madara repo"); }