Skip to content

Commit

Permalink
add: avail support (#4)
Browse files Browse the repository at this point in the history
* add: avail support
  • Loading branch information
anshalshukla authored Jan 3, 2024
1 parent 6bb41e0 commit abe0cf8
Show file tree
Hide file tree
Showing 20 changed files with 2,323 additions and 189 deletions.
2,263 changes: 2,124 additions & 139 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ path = "src/main.rs"
[dependencies]
clap = { version = "4.4.11", features = ["derive"] }
dirs = "5.0.1"
env_logger = "0.10.1"
git2 = "0.18.1"
hex = { version = "0.4.3", features = [] }
inquire = "0.6.2"
log = "0.4.20"
remove_dir_all = "0.8.2"
reqwest = { version = "0.11.23", features = ["json", "blocking"] }
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.109"
sp-core = "27.0.0"
strum = { version = "0.25.0", features = ["derive"] }
strum_macros = { version = "0.25.3", features = [] }
thiserror = "1.0.52"
Expand Down
9 changes: 9 additions & 0 deletions keypair.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"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"
}
26 changes: 2 additions & 24 deletions src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use strum::EnumIter;
use strum_macros::Display;
use toml::ser::Error;

use crate::da::da_layers::DALayer;

#[derive(Serialize, Deserialize)]
pub struct AppChainConfig {
pub app_chain: String,
Expand All @@ -26,37 +28,13 @@ impl AppChainConfig {
}
}

impl Default for AppChainConfig {
fn default() -> Self {
AppChainConfig {
app_chain: "".to_string(),
base_path: "".to_string(),
chain_id: "".to_string(),
mode: RollupMode::Sovereign,
da_layer: DALayer::Avail,
block_time: 0,
disable_fees: false,
fee_token: "".to_string(),
madara_version: "".to_string(),
config_version: ConfigVersion::Version1,
}
}
}

#[derive(Debug, Serialize, Deserialize, EnumIter, Display)]
pub enum RollupMode {
Sovereign,
// Validity,
// Validium,
}

#[derive(Debug, Serialize, Deserialize, EnumIter, Display)]
pub enum DALayer {
Avail,
Celestia,
Ethereum,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum ConfigVersion {
Version1,
Expand Down
2 changes: 0 additions & 2 deletions src/cli/constants.rs

This file was deleted.

24 changes: 17 additions & 7 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use strum::IntoEnumIterator;
use thiserror::Error;

use super::prompt::{get_boolean_input, get_custom_input, get_option, get_text_input};
use crate::app::config::{AppChainConfig, ConfigVersion, DALayer, RollupMode};
use crate::cli::constants::{MADARA_REPO_NAME, MADARA_REPO_ORG};
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};
use crate::utils::errors::GithubError;
use crate::utils::github::get_latest_commit_hash;
use crate::utils::paths::{get_app_chains_home, get_app_home};
Expand All @@ -21,6 +22,8 @@ pub enum InitError {
FailedToGetLatestCommitHash(#[from] GithubError),
#[error("Failed to serialize to toml: {0}")]
FailedToSerializeToToml(#[from] toml::ser::Error),
#[error("Failed to generate keypair")]
FailedToGenerateKeypair,
}

pub fn init() {
Expand All @@ -36,6 +39,7 @@ pub fn init() {
panic!("Failed to write config: {}", err);
}
};
// fund_msg(&config.da_layer);
log::info!("✅ New app chain initialised.");
}

Expand All @@ -57,6 +61,14 @@ fn generate_config() -> Result<AppChainConfig, InitError> {
let madara_version = get_latest_commit_hash(MADARA_REPO_ORG, MADARA_REPO_NAME)?;
let config_version = ConfigVersion::Version1;

match DAFactory::new_da(&da_layer).setup_and_generate_keypair(&app_chain) {
Ok(_) => (),
Err(err) => {
log::error!("Failed to generate keypair: {}", err);
return Err(InitError::FailedToGenerateKeypair);
}
};

Ok(AppChainConfig {
app_chain,
base_path,
Expand All @@ -73,14 +85,12 @@ fn generate_config() -> Result<AppChainConfig, InitError> {

fn write_config(config: &AppChainConfig) -> Result<(), InitError> {
let toml = config.to_toml()?;
let config_file = format!("{}-config.toml", config.app_chain);
let app_home = get_app_home(&config.app_chain)?;
let full_file_path = app_home.join(config_file);
let file_path = get_app_home(&config.app_chain)?.join(APP_CONFIG_NAME);

if let Err(err) = fs::write(full_file_path, toml) {
if let Err(err) = fs::write(file_path, toml) {
panic!("Error writing to file: {}", err);
} else {
log::info!("Data written to file successfully!");
log::info!("Config file saved!");
}

Ok(())
Expand Down
2 changes: 0 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ pub mod init;

pub mod prompt;

pub mod constants;

pub mod list;

pub mod run;
55 changes: 55 additions & 0 deletions src/da/avail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::fs;

use hex::encode;
use serde_json::json;
use sp_core::{sr25519, Pair};

use crate::da::da_layers::DaConfig;
use crate::da::errors::KeyGenError;
use crate::utils::constants::{APP_DA_CONFIG_NAME, APP_SECRET_PHRASE};
use crate::utils::paths::get_app_home;

pub struct AvailConfig {}

impl DaConfig for AvailConfig {
fn setup_and_generate_keypair(&self, app_chain: &str) -> Result<(), KeyGenError> {
let file_path = get_app_home(app_chain)?.join(APP_SECRET_PHRASE);
let file_path_str = file_path.to_string_lossy().to_string();
let (pair, phrase, seed) = <sr25519::Pair as Pair>::generate_with_phrase(None);
let seed_str = format!("0x{}", encode(seed.as_ref()));

if let Err(err) = fs::write(file_path, phrase) {
panic!("Error writing to file: {}", err);
} else {
log::info!("Secret phrase stored in app home: {}", file_path_str);
log::info!(
"Please fund {} with atleast 1 AVL (https://docs.availproject.org/about/faucet/)",
pair.public()
);
}

generate_config(app_chain, &seed_str)?;

Ok(())
}
}

fn generate_config(app_chain: &str, seed: &str) -> Result<(), KeyGenError> {
let file_path = get_app_home(app_chain)?.join(APP_DA_CONFIG_NAME);

let avail_config = json! ({
"ws_provider": "wss://goldberg.avail.tools:443/ws",
"mode": "sovereign",
"seed": seed,
"app_id": 0,
})
.to_string();

if let Err(err) = fs::write(file_path, avail_config) {
panic!("Error writing to file: {}", err);
} else {
log::info!("Successfully generated Avail config!");
}

Ok(())
}
29 changes: 29 additions & 0 deletions src/da/da_layers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumIter};

use crate::da::avail::AvailConfig;
use crate::da::errors::KeyGenError;
use crate::da::no_da::NoDAConfig;

#[derive(Debug, Serialize, Deserialize, EnumIter, Display)]
pub enum DALayer {
Avail,
Celestia,
Ethereum,
NoDA,
}

pub trait DaConfig {
fn setup_and_generate_keypair(&self, app_chain: &str) -> Result<(), KeyGenError>;
}

pub struct DAFactory;

impl DAFactory {
pub fn new_da(da: &DALayer) -> Box<dyn DaConfig> {
match da {
DALayer::Avail => Box::new(AvailConfig {}),
_ => Box::new(NoDAConfig {}),
}
}
}
14 changes: 14 additions & 0 deletions src/da/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::io;
use std::string::FromUtf8Error;

use thiserror::Error;

#[derive(Debug, Error)]
pub enum KeyGenError {
#[error("Failed to read file: {0}")]
FailedToIoFilesystem(#[from] io::Error),
#[error("Failed to parse output: {0}")]
FailedToParseOutput(#[from] FromUtf8Error),
#[error("Failed to parse to json: {0}")]
FailedToParseToJson(#[from] serde_json::Error),
}
7 changes: 7 additions & 0 deletions src/da/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod da_layers;

pub mod avail;

pub mod errors;

pub mod no_da;
11 changes: 11 additions & 0 deletions src/da/no_da.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub struct NoDAConfig;

use crate::da::da_layers::DaConfig;
use crate::da::errors::KeyGenError;

impl DaConfig for NoDAConfig {
fn setup_and_generate_keypair(&self, app_chain: &str) -> Result<(), KeyGenError> {
log::info!("Launching {} without any DA mode", app_chain);
Ok(())
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pub mod cli;
pub mod app;

pub mod utils;

pub mod da;
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::{Parser, Subcommand};
use log::LevelFilter;
use madara_cli::cli;

#[derive(Parser)]
Expand All @@ -19,6 +20,8 @@ enum Commands {
}

fn main() {
env_logger::Builder::from_default_env().filter_level(LevelFilter::Info).init();

let cli = Cli::parse();

match &cli.command {
Expand Down
14 changes: 9 additions & 5 deletions src/utils/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use std::io::{Error, ErrorKind};
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::process::{Command, Output, Stdio};

pub fn execute_cmd(program: &str, args: &[&str], dir: &PathBuf) -> Result<(), Error> {
let result =
Command::new(program).current_dir(dir).args(args).stdout(Stdio::inherit()).stderr(Stdio::inherit()).output();
pub fn execute_cmd(program: &str, args: &[&str], dir: &PathBuf) -> Result<Output, Error> {
let output = execute_cmd_stdout(program, args, dir, Stdio::inherit())?;
Ok(output)
}

pub fn execute_cmd_stdout(program: &str, args: &[&str], dir: &PathBuf, out: Stdio) -> Result<Output, Error> {
let result = Command::new(program).current_dir(dir).args(args).stdout(out).stderr(Stdio::inherit()).output();

match result {
Ok(output) => {
if output.status.success() {
log::info!("Successfully executed {}", program);
Ok(())
Ok(output)
} else {
Err(Error::new(ErrorKind::Other, "Unable to execute command"))
}
Expand Down
6 changes: 6 additions & 0 deletions src/utils/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub const MADARA_REPO_ORG: &str = "keep-starknet-strange";
pub const MADARA_REPO_NAME: &str = "madara";

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";
2 changes: 2 additions & 0 deletions src/utils/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub enum MadaraError {
FailedToCloneRepo,
#[error("Failed to regenerate config")]
FailedToRegenerateConfig,
#[error("Failed to get DA config")]
FailedToGetDAConfig,
}

#[derive(Debug, Error)]
Expand Down
33 changes: 25 additions & 8 deletions src/utils/madara.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::cli::constants::{MADARA_REPO_NAME, MADARA_REPO_ORG};
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::errors::MadaraError;
use crate::utils::github::git_clone;
use crate::utils::paths::get_madara_home;
use crate::utils::paths::{get_app_home, get_madara_home};
use crate::utils::toml::regenerate_app_config;
pub const GITHUB_BASE_URL: &str = "https://github.com";

Expand Down Expand Up @@ -35,13 +36,29 @@ pub fn setup_and_run_madara(app_chain: &str) -> Result<(), MadaraError> {
}
};

let app_home = get_app_home(app_chain)?;
let binding = app_home.join(APP_DA_CONFIG_NAME);
let da_config_path = match binding.to_str() {
Some(path) => path,
None => {
return Err(MadaraError::FailedToGetDAConfig);
}
};

let da_conf = format!("--da-conf={}", da_config_path);
let base_path = format!("--base-path={}", config.base_path);
execute_cmd("cargo", &["run", "--release", "setup", "--chain=dev", "--from-remote", &base_path], &madara_path)?;
execute_cmd(
"./target/release/madara",
&["--rpc-cors=all", "--chain=dev", "--force-authoring", "--rpc-external", "--rpc-methods=unsafe", &base_path],
&madara_path,
)?;

let mut args =
vec!["--chain=dev", "--alice", "--force-authoring", "--rpc-cors=all", "--tx-ban-seconds=0", &base_path];

if let DALayer::Avail = &config.da_layer {
let avail_conf = vec!["--da-layer=avail", &da_conf];
args.extend(avail_conf);
};

execute_cmd("./target/release/madara", &["setup", "--chain=dev", "--from-remote", &base_path], &madara_path)?;

execute_cmd("./target/release/madara", args.as_slice(), &madara_path)?;

Ok(())
}
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ pub mod madara;
pub mod errors;

pub mod toml;

pub mod constants;
Loading

0 comments on commit abe0cf8

Please sign in to comment.