Skip to content

Commit

Permalink
refactor: clean
Browse files Browse the repository at this point in the history
  • Loading branch information
MatejVukosav committed Dec 20, 2024
1 parent 49ee39b commit 123a6d8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 99 deletions.
21 changes: 6 additions & 15 deletions crates/meroctl/src/cli/app/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,26 @@ impl Report for InstallApplicationResponse {

impl InstallCommand {
pub async fn run(self, environment: &Environment) -> Result<()> {
drop(
InstallCommand::install_app(self.path, self.hash, self.metadata, self.url, environment)
.await,
);
let _ignored = self.install_app(environment).await;

Ok(())
}

pub async fn install_app(
path: Option<Utf8PathBuf>,
hash: Option<Hash>,
metadata: Option<String>,
url: Option<String>,
environment: &Environment,
) -> Result<ApplicationId> {
pub async fn install_app(self, environment: &Environment) -> Result<ApplicationId> {
let config = load_config(&environment.args.home, &environment.args.node_name)?;
let mut is_dev_installation = false;
let metadata = metadata.map(String::into_bytes).unwrap_or_default();
let metadata = self.metadata.map(String::into_bytes).unwrap_or_default();

let request = if let Some(app_path) = path {
let request = if let Some(app_path) = self.path {
is_dev_installation = true;
serde_json::to_value(InstallDevApplicationRequest::new(
app_path.canonicalize_utf8()?,
metadata,
))?
} else if let Some(app_url) = url {
} else if let Some(app_url) = self.url {
serde_json::to_value(InstallApplicationRequest::new(
Url::parse(&app_url)?,
hash,
self.hash,
metadata,
))?
} else {
Expand Down
89 changes: 36 additions & 53 deletions crates/meroctl/src/cli/bootstrap/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,21 @@ pub struct StartBootstrapCommand {
}

impl StartBootstrapCommand {
pub async fn run(self, environment: &Environment) -> EyreResult<()> {
pub async fn run(mut self, environment: &Environment) -> EyreResult<()> {
println!("Starting bootstrap process");
let nodes_dir: Utf8PathBuf = environment.args.home.clone();
let binary = self.merod_path.clone();
let mut processes: Vec<Child> = vec![];

let mut app_path = self.app_path.clone();

// TODO app default from releases

let mut demo_app = false;
if app_path.is_none() {
if self.app_path.is_none() {
println!("Downloading demo app...");
demo_app = true;

let wasm_url = "https://github.com/calimero-network/core-app-template/raw/refs/heads/master/logic/res/logic.wasm";
let output_path: Utf8PathBuf = "output/app.wasm".into();
app_path = Some(output_path.clone());
self.app_path = Some(output_path.clone());

if let Err(e) = self.download_wasm(wasm_url, output_path).await {
bail!("Failed to download the WASM file: {:?}", e);
Expand All @@ -70,7 +67,6 @@ impl StartBootstrapCommand {

let node1_process = self
.initialize_and_start_node(
binary.clone(),
nodes_dir.to_owned(),
node1_log_dir.to_owned(),
&node1_name,
Expand All @@ -82,11 +78,7 @@ impl StartBootstrapCommand {

println!("Creating context in {:?}", node1_name);
let (context_id, public_key, application_id) =
StartBootstrapCommand::create_context_in_bootstrap(
app_path.to_owned(),
node1_environment,
)
.await?;
self.create_context_in_bootstrap(node1_environment).await?;

let node2_name = "node2".to_owned();
let node2_log_dir: Utf8PathBuf = "output/node_2_output".into();
Expand All @@ -102,7 +94,6 @@ impl StartBootstrapCommand {

let node2_process = self
.initialize_and_start_node(
binary,
nodes_dir.to_owned(),
node2_log_dir.to_owned(),
&node2_name,
Expand All @@ -128,20 +119,19 @@ impl StartBootstrapCommand {

if demo_app {
println!(
"Application is available at https://calimero-network.github.io/core-app-template/"
"Connect to the node from https://calimero-network.github.io/core-app-template/"
);
println!("Open it in a separate window for each node.");
println!("Open application in two separate windows to use it with two different nodes.");
println!("Application setup screen requires application id and node url.");
println!("Application id is {:?} ", application_id.to_string(),);

println!(
"Application id for node {:?} is {:?} and url is http://localhost:{}",
node1_environment.args.node_name,
application_id.to_string(),
node1_server_port
"Node {:?} url is http://localhost:{}",
node1_environment.args.node_name, node1_server_port
);
println!(
"Application id for node {:?} is {:?} and url is http://localhost:{}",
node1_environment.args.node_name,
application_id.to_string(),
node2_server_port
"Node {:?} url is http://localhost:{}",
node1_environment.args.node_name, node2_server_port
);
}
println!("************************************************");
Expand All @@ -153,7 +143,6 @@ impl StartBootstrapCommand {

async fn initialize_and_start_node(
&self,
binary: Utf8PathBuf,
nodes_dir: Utf8PathBuf,
log_dir: Utf8PathBuf,
node_name: &str,
Expand All @@ -163,7 +152,6 @@ impl StartBootstrapCommand {
println!("Initializing node {:?}", node_name);

self.init(
binary.to_owned(),
nodes_dir.to_owned(),
log_dir.to_owned(),
node_name.to_owned(),
Expand All @@ -175,7 +163,7 @@ impl StartBootstrapCommand {
println!("Starting node {:?}.", node_name);

let process = self
.run_node(binary, nodes_dir, log_dir, node_name.to_owned())
.run_node(nodes_dir, log_dir, node_name.to_owned())
.await?;

sleep(Duration::from_secs(10)).await;
Expand All @@ -196,13 +184,12 @@ impl StartBootstrapCommand {
invitee_environment.args.node_name, context_id
);

let invitation_payload = InviteCommand::invite(
let invite_command = InviteCommand {
context_id,
inviter_public_key,
invitee_private_key.public_key(),
invitor_environment,
)
.await?;
inviter_id: inviter_public_key,
invitee_id: invitee_private_key.public_key(),
};
let invitation_payload = invite_command.invite(invitor_environment).await?;

println!(
"Node {:?} successfully invited.",
Expand All @@ -213,7 +200,12 @@ impl StartBootstrapCommand {
"Joining node {:?} to context.",
invitee_environment.args.node_name
);
JoinCommand::join(invitee_private_key, invitation_payload, invitee_environment).await?;

let join_command = JoinCommand {
private_key: invitee_private_key,
invitation_payload,
};
join_command.join(invitee_environment).await?;
println!(
"Node {:?} joined successfully.",
invitee_environment.args.node_name
Expand All @@ -224,7 +216,6 @@ impl StartBootstrapCommand {

pub async fn init(
&self,
binary: Utf8PathBuf,
nodes_dir: Utf8PathBuf,
log_dir: Utf8PathBuf,
node_name: String,
Expand All @@ -236,7 +227,6 @@ impl StartBootstrapCommand {

let mut child = self
.run_cmd(
binary.clone(),
nodes_dir.clone(),
log_dir.clone(),
node_name.clone(),
Expand All @@ -257,14 +247,7 @@ impl StartBootstrapCommand {
}

let mut child = self
.run_cmd(
binary,
nodes_dir,
log_dir,
node_name.clone(),
&["config"],
"config",
)
.run_cmd(nodes_dir, log_dir, node_name.clone(), &["config"], "config")
.await?;
let result = child.wait().await?;
if !result.success() {
Expand All @@ -275,30 +258,31 @@ impl StartBootstrapCommand {

pub async fn run_node(
&self,
binary: Utf8PathBuf,
nodes_dir: Utf8PathBuf,
log_dir: Utf8PathBuf,
node_name: String,
) -> EyreResult<Child> {
Ok(self
.run_cmd(binary, nodes_dir, log_dir, node_name, &["run"], "run")
.run_cmd(nodes_dir, log_dir, node_name, &["run"], "run")
.await?)
}

pub async fn create_context_in_bootstrap(
app_path: Option<Utf8PathBuf>,
&self,
environment: &Environment,
) -> EyreResult<(ContextId, PublicKey, ApplicationId)> {
let config = load_config(&environment.args.home, &environment.args.node_name)?;
let multiaddr = fetch_multiaddr(&config)?;
let client = Client::new();

let app_hash = Some(Hash::new("hash".as_bytes()));
let app_metadata = Some("".to_owned());
let url = Some("".to_owned());
let install_command = InstallCommand {
path: self.app_path.clone(),
url: Some("".to_owned()),
metadata: Some("".to_owned()),
hash: Some(Hash::new("hash".as_bytes())),
};

let application_id =
InstallCommand::install_app(app_path, app_hash, app_metadata, url, environment).await?;
let application_id = install_command.install_app(environment).await?;

let (context_id, public_key) = create_context(
environment,
Expand All @@ -318,7 +302,6 @@ impl StartBootstrapCommand {

async fn run_cmd(
&self,
binary: Utf8PathBuf,
nodes_dir: Utf8PathBuf,
log_dir: Utf8PathBuf,
node_name: String,
Expand All @@ -331,7 +314,7 @@ impl StartBootstrapCommand {
let log_file = log_dir.join(format!("{}.log", log_suffix));
let mut log_file = File::create(&log_file).await?;

let mut child = Command::new(&binary)
let mut child = Command::new(&self.merod_path)
.args(root_args)
.stdout(Stdio::piped())
.spawn()?;
Expand Down
23 changes: 5 additions & 18 deletions crates/meroctl/src/cli/context/invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,21 @@ impl Report for InviteToContextResponse {

impl InviteCommand {
pub async fn run(self, environment: &Environment) -> EyreResult<()> {
drop(
InviteCommand::invite(
self.context_id,
self.inviter_id,
self.invitee_id,
environment,
)
.await,
);
let _ignored = self.invite(environment).await?;

Ok(())
}

pub async fn invite(
context_id: ContextId,
inviter_id: PublicKey,
invitee_id: PublicKey,
environment: &Environment,
) -> EyreResult<ContextInvitationPayload> {
pub async fn invite(&self, environment: &Environment) -> EyreResult<ContextInvitationPayload> {
let config = load_config(&environment.args.home, &environment.args.node_name)?;

let response: InviteToContextResponse = do_request(
&Client::new(),
multiaddr_to_url(fetch_multiaddr(&config)?, "admin-api/dev/contexts/invite")?,
Some(InviteToContextRequest {
context_id,
inviter_id,
invitee_id,
context_id: self.context_id,
inviter_id: self.inviter_id,
invitee_id: self.invitee_id,
}),
&config.identity,
RequestType::Post,
Expand Down
21 changes: 8 additions & 13 deletions crates/meroctl/src/cli/context/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ pub struct JoinCommand {
value_name = "PRIVATE_KEY",
help = "The private key for signing the join context request"
)]
private_key: PrivateKey,
pub private_key: PrivateKey,
#[clap(
value_name = "INVITE",
help = "The invitation payload for joining the context"
)]
invitation_payload: ContextInvitationPayload,
pub invitation_payload: ContextInvitationPayload,
}

impl Report for JoinContextResponse {
Expand All @@ -38,25 +38,20 @@ impl Report for JoinContextResponse {

impl JoinCommand {
pub async fn run(self, environment: &Environment) -> EyreResult<()> {
drop(JoinCommand::join(
self.private_key,
self.invitation_payload,
environment,
));
let _ignored = self.join(environment);
Ok(())
}

pub async fn join(
private_key: PrivateKey,
invitation_payload: ContextInvitationPayload,
environment: &Environment,
) -> EyreResult<()> {
pub async fn join(self, environment: &Environment) -> EyreResult<()> {
let config = load_config(&environment.args.home, &environment.args.node_name)?;

let response: JoinContextResponse = do_request(
&Client::new(),
multiaddr_to_url(fetch_multiaddr(&config)?, "admin-api/dev/contexts/join")?,
Some(JoinContextRequest::new(private_key, invitation_payload)),
Some(JoinContextRequest::new(
self.private_key,
self.invitation_payload,
)),
&config.identity,
RequestType::Post,
)
Expand Down

0 comments on commit 123a6d8

Please sign in to comment.