Skip to content

Commit

Permalink
fix: update app id in contract (#957)
Browse files Browse the repository at this point in the history
Co-authored-by: petarjuki7 <[email protected]>
  • Loading branch information
MatejVukosav and petarjuki7 authored Nov 9, 2024
1 parent f42ef1e commit 3837b0d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
50 changes: 43 additions & 7 deletions crates/context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -834,24 +834,60 @@ 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

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")
};

value.application = ApplicationMetaKey::new(application_id);
let Some(application) = self.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!("'{}' 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::<ContextConfigEnv>(
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"),
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?;

context_meta.application = ApplicationMetaKey::new(application_id);

handle.put(&key, &value)?;
handle.put(&key, &context_meta)?;

Ok(())
}
Expand Down
13 changes: 9 additions & 4 deletions crates/meroctl/src/cli/context/create.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -118,7 +119,7 @@ impl CreateCommand {
)
.await?;

let context_id = create_context(
let (context_id, member_public_key) = create_context(
environment,
&client,
multiaddr,
Expand All @@ -137,6 +138,7 @@ impl CreateCommand {
path,
metadata,
&config.identity,
member_public_key,
)
.await?;
}
Expand All @@ -155,7 +157,7 @@ async fn create_context(
application_id: ApplicationId,
params: Option<String>,
keypair: &Keypair,
) -> EyreResult<ContextId> {
) -> EyreResult<(ContextId, PublicKey)> {
if !app_installed(base_multiaddr, &application_id, client, keypair).await? {
bail!("Application is not installed on node.")
}
Expand All @@ -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(
Expand All @@ -183,6 +185,7 @@ async fn watch_app_and_update_context(
path: Utf8PathBuf,
metadata: Option<Vec<u8>>,
keypair: &Keypair,
member_public_key: PublicKey,
) -> EyreResult<()> {
let (tx, mut rx) = mpsc::channel(1);

Expand Down Expand Up @@ -240,6 +243,7 @@ async fn watch_app_and_update_context(
context_id,
application_id,
keypair,
member_public_key,
)
.await?;
}
Expand All @@ -254,13 +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"),
)?;

let request = UpdateContextApplicationRequest::new(application_id);
let request = UpdateContextApplicationRequest::new(application_id, member_public_key);

let response: UpdateContextApplicationResponse =
do_request(client, url, Some(request), keypair, RequestType::Post).await?;
Expand Down
8 changes: 6 additions & 2 deletions crates/server-primitives/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3837b0d

Please sign in to comment.