Skip to content

Commit

Permalink
fix: merged master and resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Nov 10, 2024
2 parents 66690f8 + 3837b0d commit fcadff2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 24 deletions.
14 changes: 8 additions & 6 deletions contracts/context-config/tests/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use calimero_context_config::types::{
Application, Capability, ContextIdentity, Revision, Signed, SignerId,
};
use calimero_context_config::{
ContextRequest, ContextRequestKind, Proposal, ProposalAction, ProxyMutateRequest, Request,
RequestKind, SystemRequest,
ContextRequest, ContextRequestKind, Proposal, ProposalAction, ProposalWithApprovals,
ProxyMutateRequest, Request, RequestKind, SystemRequest,
};
use ed25519_dalek::{Signer, SigningKey};
use eyre::Ok;
Expand Down Expand Up @@ -1049,7 +1049,10 @@ async fn test_deploy() -> eyre::Result<()> {
.await?;

// Assert proposal creation result
assert!(res.is_success(), "Transaction failed: {:#?}", res);
let success_value = res.raw_bytes()?;
let proposal_result: ProposalWithApprovals = serde_json::from_slice(&success_value)?;
assert_eq!(proposal_result.num_approvals, 1);
let created_proposal_id = proposal_result.proposal_id;
// Verify proposals list
let proposals: Vec<Proposal> = worker
.view(&proxy_address, "proposals")
Expand All @@ -1062,7 +1065,7 @@ async fn test_deploy() -> eyre::Result<()> {

assert_eq!(proposals.len(), 1, "Should have exactly one proposal");
let created_proposal = &proposals[0];
assert_eq!(created_proposal.id, proposal_id);
assert_eq!(created_proposal.id, created_proposal_id);
assert_eq!(created_proposal.author_id, alice_cx_id.rt()?);
assert_eq!(created_proposal.actions.len(), 1);

Expand Down Expand Up @@ -1096,8 +1099,7 @@ async fn test_deploy() -> eyre::Result<()> {
single_proposal.is_some(),
"Should be able to get single proposal"
);

assert_eq!(single_proposal.unwrap().id, proposal_id);
assert_eq!(single_proposal.unwrap().id, created_proposal_id);

Ok(())
}
62 changes: 51 additions & 11 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 Expand Up @@ -1083,9 +1119,13 @@ impl ContextManager {
.mutate::<ContextProxy>(
context_config.protocol.as_ref().into(),
context_config.network.as_ref().into(),
context_config.contract.as_ref().into(),
context_config.proxy_contract.as_ref().into(),
)
.propose(
proposal_id,
signer_id.rt().expect("infallible conversion"),
actions,
)
.propose(proposal_id, signer_id.rt().unwrap(), actions)
.send(signing_key)
.await?;

Expand Down Expand Up @@ -1116,9 +1156,9 @@ impl ContextManager {
.mutate::<ContextProxy>(
context_config.protocol.as_ref().into(),
context_config.network.as_ref().into(),
context_config.contract.as_ref().into(),
context_config.proxy_contract.as_ref().into(),
)
.approve(signer_id.rt().unwrap(), proposal_id)
.approve(signer_id.rt().expect("infallible conversion"), proposal_id)
.send(signing_key)
.await?;

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 fcadff2

Please sign in to comment.