Skip to content

Commit

Permalink
feat(xtask): migrate xtask local command (#4)
Browse files Browse the repository at this point in the history
Creates the initial xtask command support.

Closes #CHAIN-16

---------

Co-authored-by: Luiz Carvalho <[email protected]>
  • Loading branch information
aripiprazole and saiintbrisson authored Dec 11, 2024
1 parent 4cd4490 commit 1d87da7
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"
32 changes: 28 additions & 4 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["node", "runtime", "pallets/*"]
members = ["node", "runtime", "pallets/*", "xtask"]
resolver = "2"

[workspace.package]
Expand Down
9 changes: 9 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "xtask"
version = "0.1.0"
edition = "2021"

[dependencies]
xflags = "0.3.2"
polkadot-sdk = { workspace = true, features = ["std", "sp-keyring"] }
tempfile = "3.14.0"
105 changes: 105 additions & 0 deletions xtask/src/flags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use std::path::PathBuf;

xflags::xflags! {
src "src/flags.rs"

cmd xtask {
/// Runs a substrate node.
cmd run {
/// Overrides the base path of the node. If not set,
/// a temporary directory will be used.
optional -p, --path path: PathBuf

/// Initiates the node with the Alice account.
/// The Alice node listens on TCP 30341 and RPC 9951.
optional --alice
/// Initiates the node with the Bob account.
/// The Bob node listens on TCP 30342 and RPC 9952.
optional --bob

/// If set, the node will be set to 0 Out/In peers.
optional --isolated

/// Overrides the default node name.
optional --node-name node_name: String
/// Overrides the default node key.
optional --node-key node_key: String
/// Overrides whether this node should run as a validator.
optional --node-validator node_validator: bool

/// Overrides the default TCP port.
optional --tcp-port tcp_port: u16
/// Overrides the default RPC port.
optional --rpc-port rpc_port: u16
/// Overrides the default bootnode list.
repeated --bootnodes bootnodes: String

/// Starts a local node based on the given chain spec.
cmd local {
/// The chain spec file. If missing, it will search
/// for a `spec.json` file on the current directory.
optional -c, --chain-spec chain_spec: PathBuf

/// The account SURI. The pattern is: `<mnemonic>//<seed>`.
optional --account-suri account_suri: String
}
}
}
}

// generated start
// The following code is generated by `xflags` macro.
// Run `env UPDATE_XFLAGS=1 cargo build` to regenerate.
#[derive(Debug)]
pub struct Xtask {
pub subcommand: XtaskCmd,
}

#[derive(Debug)]
pub enum XtaskCmd {
Run(Run),
}

#[derive(Debug)]
pub struct Run {
pub path: Option<PathBuf>,
pub alice: bool,
pub bob: bool,
pub isolated: bool,
pub node_name: Option<String>,
pub node_key: Option<String>,
pub node_validator: Option<bool>,
pub tcp_port: Option<u16>,
pub rpc_port: Option<u16>,
pub bootnodes: Vec<String>,
pub subcommand: RunCmd,
}

#[derive(Debug)]
pub enum RunCmd {
Local(Local),
}

#[derive(Debug)]
pub struct Local {
pub chain_spec: Option<PathBuf>,
pub account_suri: Option<String>,
}

impl Xtask {
#[allow(dead_code)]
pub fn from_env_or_exit() -> Self {
Self::from_env_or_exit_()
}

#[allow(dead_code)]
pub fn from_env() -> xflags::Result<Self> {
Self::from_env_()
}

#[allow(dead_code)]
pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
Self::from_vec_(args)
}
}
// generated end
110 changes: 110 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use std::{borrow::Cow, net::IpAddr};

use polkadot_sdk::sp_keyring;

mod flags;
mod run;

fn main() {
let cmd = flags::Xtask::from_env_or_exit();
match cmd.subcommand {
flags::XtaskCmd::Run(run) => run::run(run),
}
}

#[derive(Clone)]
pub(crate) struct Node<'a> {
pub(crate) name: Option<Cow<'a, str>>,
pub(crate) id: Option<Cow<'a, str>>,
pub(crate) key: Option<Cow<'a, str>>,
pub(crate) tcp_port: u16,
pub(crate) rpc_port: u16,
pub(crate) validator: bool,
}

impl Node<'_> {
fn bootnode_uri(&self, addr: IpAddr) -> String {
format!(
"/{}/{addr}/tcp/{}/p2p/{}",
match addr {
IpAddr::V4(_) => "ip4",
IpAddr::V6(_) => "ip6",
},
self.tcp_port,
self.id.as_ref().unwrap()
)
}
}

impl Default for Node<'_> {
fn default() -> Self {
Self {
name: Default::default(),
id: Default::default(),
key: Default::default(),
tcp_port: 30333,
rpc_port: 9944,
validator: false,
}
}
}

#[allow(dead_code)]
#[derive(Clone)]
struct Account<'a> {
pub(crate) suri: Cow<'a, str>,
pub(crate) aura_address: sp_keyring::Sr25519Keyring,
pub(crate) grandpa_address: sp_keyring::Ed25519Keyring,
}

impl<'a> Default for Account<'a> {
fn default() -> Self {
Self {
suri: "".into(),
aura_address: sp_keyring::Sr25519Keyring::One,
grandpa_address: sp_keyring::Ed25519Keyring::One,
}
}
}

static ALICE_ACCOUNT: Account<'static> = Account {
suri: Cow::Borrowed(
"bottom drive obey lake curtain smoke basket hold race lonely fit walk//Alice",
),
aura_address: sp_keyring::Sr25519Keyring::Alice,
grandpa_address: sp_keyring::Ed25519Keyring::Alice,
};

static BOB_ACCOUNT: Account<'static> = Account {
suri: Cow::Borrowed(
"bottom drive obey lake curtain smoke basket hold race lonely fit walk//Bob",
),
aura_address: sp_keyring::Sr25519Keyring::Bob,
grandpa_address: sp_keyring::Ed25519Keyring::Bob,
};

static ALICE_NODE: Node<'static> = Node {
name: Some(Cow::Borrowed("Alice")),
id: Some(Cow::Borrowed(
"12D3KooWBorpca6RKiebVjeFJA5o9iVWnZpg98yQbYqRC6f8CnLw",
)),
key: Some(Cow::Borrowed(
"2756181a3b9bca683a35b51a0a5d75ee536738680bcb9066c68be1db305a1ac5",
)),
tcp_port: 30341,
rpc_port: 9951,
validator: true,
};

static BOB_NODE: Node<'static> = Node {
name: Some(Cow::Borrowed("Bob")),
id: Some(Cow::Borrowed(
"12D3KooWQh3CeSp2rpUVvPb6jqvmHVCUieoZmKbkUhZ8rPR77vmA",
)),
key: Some(Cow::Borrowed(
"e83fa0787cb280d95c666ead866a2a4bc1ee1e36faa1ed06623595eb3f474681",
)),
tcp_port: 30342,
rpc_port: 9952,
validator: true,
};
Loading

0 comments on commit 1d87da7

Please sign in to comment.