From 7fdd0384b046bc8755083def36e9eed825edd1cf Mon Sep 17 00:00:00 2001 From: "matej.vukosav" Date: Fri, 8 Nov 2024 23:13:36 +0700 Subject: [PATCH 1/3] fix: update app id in contract --- crates/context/src/lib.rs | 42 ++++++++++++++++++- crates/meroctl/src/cli/context/create.rs | 4 +- crates/server-primitives/src/admin.rs | 8 +++- .../context/update_context_application.rs | 7 +++- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/crates/context/src/lib.rs b/crates/context/src/lib.rs index f8eb49938..e57bf8b7e 100644 --- a/crates/context/src/lib.rs +++ b/crates/context/src/lib.rs @@ -39,7 +39,7 @@ use calimero_store::types::{ use calimero_store::Store; use camino::Utf8PathBuf; use eyre::{bail, OptionExt, Result as EyreResult}; -use futures_util::{AsyncRead, TryFutureExt, TryStreamExt}; +use futures_util::{AsyncRead, TryStreamExt}; use rand::rngs::StdRng; use rand::seq::IteratorRandom; use rand::SeedableRng; @@ -834,10 +834,11 @@ impl ContextManager { Ok(contexts) } - pub fn update_application_id( + pub async fn update_application_id( &self, context_id: ContextId, application_id: ApplicationId, + signer_id: PublicKey, ) -> EyreResult<()> { // todo! use context config @@ -849,6 +850,43 @@ impl ContextManager { bail!("Context not found") }; + let this = self.clone(); + + let Some(application) = this.get_application(&application_id)? else { + bail!("Application with id {:?} not found", application_id) + }; + + let Some(ContextIdentityValue { + private_key: Some(requester_secret), + }) = handle.get(&ContextIdentityKey::new(context_id, signer_id))? + else { + bail!( + "Identity {:?} not found in context {:?}", + signer_id, + context_id + ) + }; + + let _ = this + .config_client + .mutate::( + this.client_config.new.protocol.as_str().into(), + this.client_config.new.network.as_str().into(), + this.client_config.new.contract_id.as_str().into(), + ) + .update_application( + context_id.rt().expect("bla"), + ApplicationConfig::new( + application.id.rt().expect("infallible conversion"), + application.blob.rt().expect("infallible conversion"), + application.size, + ApplicationSourceConfig(application.source.to_string().into()), + ApplicationMetadataConfig(Repr::new(application.metadata.into())), + ), + ) + .send(requester_secret) + .await?; + value.application = ApplicationMetaKey::new(application_id); handle.put(&key, &value)?; diff --git a/crates/meroctl/src/cli/context/create.rs b/crates/meroctl/src/cli/context/create.rs index 31403818c..e4e835a1c 100644 --- a/crates/meroctl/src/cli/context/create.rs +++ b/crates/meroctl/src/cli/context/create.rs @@ -260,7 +260,9 @@ async fn update_context_application( &format!("admin-api/dev/contexts/{context_id}/application"), )?; - let request = UpdateContextApplicationRequest::new(application_id); + //How to make conversion from libp2p to public key + let public_key = keypair.public(); + let request = UpdateContextApplicationRequest::new(application_id, public_key); let response: UpdateContextApplicationResponse = do_request(client, url, Some(request), keypair, RequestType::Post).await?; diff --git a/crates/server-primitives/src/admin.rs b/crates/server-primitives/src/admin.rs index 3fd690846..cd06ca8fe 100644 --- a/crates/server-primitives/src/admin.rs +++ b/crates/server-primitives/src/admin.rs @@ -395,11 +395,15 @@ impl JoinContextResponse { #[serde(rename_all = "camelCase")] pub struct UpdateContextApplicationRequest { pub application_id: ApplicationId, + pub executor_public_key: PublicKey, } impl UpdateContextApplicationRequest { - pub const fn new(application_id: ApplicationId) -> Self { - Self { application_id } + pub const fn new(application_id: ApplicationId, executor_public_key: PublicKey) -> Self { + Self { + application_id, + executor_public_key, + } } } diff --git a/crates/server/src/admin/handlers/context/update_context_application.rs b/crates/server/src/admin/handlers/context/update_context_application.rs index d21acf916..368243622 100644 --- a/crates/server/src/admin/handlers/context/update_context_application.rs +++ b/crates/server/src/admin/handlers/context/update_context_application.rs @@ -28,7 +28,12 @@ pub async fn handler( let result = state .ctx_manager - .update_application_id(context_id_result, req.application_id) + .update_application_id( + context_id_result, + req.application_id, + req.executor_public_key, + ) + .await .map_err(parse_api_error); match result { From c70862d4738de26518dc87c6f773b6a2cb0e09a2 Mon Sep 17 00:00:00 2001 From: petarjuki7 Date: Fri, 8 Nov 2024 19:29:21 +0100 Subject: [PATCH 2/3] Fixed keypair --- crates/context/src/lib.rs | 20 +++++++------------- crates/meroctl/src/cli/context/create.rs | 15 +++++++++------ 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/crates/context/src/lib.rs b/crates/context/src/lib.rs index e57bf8b7e..48785c3a6 100644 --- a/crates/context/src/lib.rs +++ b/crates/context/src/lib.rs @@ -850,9 +850,7 @@ impl ContextManager { bail!("Context not found") }; - let this = self.clone(); - - let Some(application) = this.get_application(&application_id)? else { + let Some(application) = self.get_application(&application_id)? else { bail!("Application with id {:?} not found", application_id) }; @@ -860,22 +858,18 @@ impl ContextManager { private_key: Some(requester_secret), }) = handle.get(&ContextIdentityKey::new(context_id, signer_id))? else { - bail!( - "Identity {:?} not found in context {:?}", - signer_id, - context_id - ) + bail!("'{}' is not a member of '{}'", signer_id, context_id) }; - let _ = this + let _ = self .config_client .mutate::( - this.client_config.new.protocol.as_str().into(), - this.client_config.new.network.as_str().into(), - this.client_config.new.contract_id.as_str().into(), + self.client_config.new.protocol.as_str().into(), + self.client_config.new.network.as_str().into(), + self.client_config.new.contract_id.as_str().into(), ) .update_application( - context_id.rt().expect("bla"), + context_id.rt().expect("infallible conversion"), ApplicationConfig::new( application.id.rt().expect("infallible conversion"), application.blob.rt().expect("infallible conversion"), diff --git a/crates/meroctl/src/cli/context/create.rs b/crates/meroctl/src/cli/context/create.rs index e4e835a1c..ecca61dcf 100644 --- a/crates/meroctl/src/cli/context/create.rs +++ b/crates/meroctl/src/cli/context/create.rs @@ -1,6 +1,7 @@ use calimero_primitives::application::ApplicationId; use calimero_primitives::context::ContextId; use calimero_primitives::hash::Hash; +use calimero_primitives::identity::PublicKey; use calimero_server_primitives::admin::{ CreateContextRequest, CreateContextResponse, GetApplicationResponse, InstallApplicationResponse, InstallDevApplicationRequest, UpdateContextApplicationRequest, @@ -118,7 +119,7 @@ impl CreateCommand { ) .await?; - let context_id = create_context( + let (context_id, member_public_key) = create_context( environment, &client, multiaddr, @@ -137,6 +138,7 @@ impl CreateCommand { path, metadata, &config.identity, + member_public_key, ) .await?; } @@ -155,7 +157,7 @@ async fn create_context( application_id: ApplicationId, params: Option, keypair: &Keypair, -) -> EyreResult { +) -> EyreResult<(ContextId, PublicKey)> { if !app_installed(base_multiaddr, &application_id, client, keypair).await? { bail!("Application is not installed on node.") } @@ -172,7 +174,7 @@ async fn create_context( environment.output.write(&response); - Ok(response.data.context_id) + Ok((response.data.context_id, response.data.member_public_key)) } async fn watch_app_and_update_context( @@ -183,6 +185,7 @@ async fn watch_app_and_update_context( path: Utf8PathBuf, metadata: Option>, keypair: &Keypair, + member_public_key: PublicKey, ) -> EyreResult<()> { let (tx, mut rx) = mpsc::channel(1); @@ -240,6 +243,7 @@ async fn watch_app_and_update_context( context_id, application_id, keypair, + member_public_key, ) .await?; } @@ -254,15 +258,14 @@ async fn update_context_application( context_id: ContextId, application_id: ApplicationId, keypair: &Keypair, + member_public_key: PublicKey, ) -> EyreResult<()> { let url = multiaddr_to_url( base_multiaddr, &format!("admin-api/dev/contexts/{context_id}/application"), )?; - //How to make conversion from libp2p to public key - let public_key = keypair.public(); - let request = UpdateContextApplicationRequest::new(application_id, public_key); + let request = UpdateContextApplicationRequest::new(application_id, member_public_key); let response: UpdateContextApplicationResponse = do_request(client, url, Some(request), keypair, RequestType::Post).await?; From de1991e0be618f3c58840e19eeae5dfa1c156e67 Mon Sep 17 00:00:00 2001 From: "matej.vukosav" Date: Sat, 9 Nov 2024 17:06:38 +0700 Subject: [PATCH 3/3] fix: use config from the store --- crates/context/src/lib.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/crates/context/src/lib.rs b/crates/context/src/lib.rs index 48785c3a6..8243b869a 100644 --- a/crates/context/src/lib.rs +++ b/crates/context/src/lib.rs @@ -840,13 +840,11 @@ impl ContextManager { application_id: ApplicationId, signer_id: PublicKey, ) -> EyreResult<()> { - // todo! use context config - let mut handle = self.store.handle(); let key = ContextMetaKey::new(context_id); - let Some(mut value) = handle.get(&key)? else { + let Some(mut context_meta) = handle.get(&key)? else { bail!("Context not found") }; @@ -861,12 +859,18 @@ impl ContextManager { bail!("'{}' is not a member of '{}'", signer_id, context_id) }; + let Some(context_config) = handle.get(&ContextConfigKey::new(context_id))? else { + bail!( + "Failed to retrieve ContextConfig for context ID: {}", + context_id + ); + }; let _ = self .config_client .mutate::( - self.client_config.new.protocol.as_str().into(), - self.client_config.new.network.as_str().into(), - self.client_config.new.contract_id.as_str().into(), + context_config.protocol.as_ref().into(), + context_config.network.as_ref().into(), + context_config.contract.as_ref().into(), ) .update_application( context_id.rt().expect("infallible conversion"), @@ -881,9 +885,9 @@ impl ContextManager { .send(requester_secret) .await?; - value.application = ApplicationMetaKey::new(application_id); + context_meta.application = ApplicationMetaKey::new(application_id); - handle.put(&key, &value)?; + handle.put(&key, &context_meta)?; Ok(()) }