Skip to content

Commit

Permalink
fix: resolved PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Dec 20, 2024
1 parent 39e5c26 commit 2fa86ac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
5 changes: 2 additions & 3 deletions contracts/icp/context-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::collections::BTreeMap;

use calimero_context_config::icp::repr::ICRepr;
use calimero_context_config::icp::types::{ICApplication, ICCapability, ICRequest, ICSigned};
Expand All @@ -21,9 +21,8 @@ thread_local! {
#[derive(CandidType, Deserialize, Debug)]
pub struct Context {
pub application: Guard<ICApplication>,
pub members: Guard<BTreeSet<ICRepr<ContextIdentity>>>,
pub members: Guard<BTreeMap<ICRepr<ContextIdentity>, u64>>,
pub proxy: Guard<Principal>,
pub member_nonces: BTreeMap<ICRepr<ContextIdentity>, u64>,
}

#[derive(CandidType, Deserialize, Debug)]
Expand Down
30 changes: 14 additions & 16 deletions contracts/icp/context-config/src/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,18 @@ async fn add_context(

with_state_mut(|configs| {
// Create context with guards
let mut context = Context {
let context = Context {
application: Guard::new(author_id.rt().expect("infallible conversion"), application),
members: Guard::new(
author_id.rt().expect("infallible conversion"),
[author_id.rt().expect("infallible conversion")].into(),
[(author_id.rt().expect("infallible conversion"), 0)].into_iter().collect(),
),
proxy: Guard::new(
author_id.rt().expect("infallible conversion"),
proxy_canister_id,
),
member_nonces: BTreeMap::new(),
};

// Initialize the author's nonce
context
.member_nonces
.insert(author_id.rt().expect("infallible conversion"), 0);

// Store context
if configs.contexts.insert(context_id, context).is_some() {
return Err("context already exists".into());
Expand Down Expand Up @@ -113,7 +107,7 @@ async fn deploy_proxy_contract(context_id: ICRepr<ContextId>) -> Result<Principa
}),
};

let (canister_record,) = create_canister(create_args, 850_000_000_000u128)
let (canister_record,) = create_canister(create_args, 1_500_000_000_000u128)
.await
.map_err(|e| format!("Failed to create canister: {:?}", e))?;

Expand Down Expand Up @@ -184,8 +178,9 @@ fn add_members(
let mut ctx_members = guard_ref.get_mut();

for member in members {
ctx_members.insert(member);
context.member_nonces.insert(member, 0);
if !ctx_members.contains_key(&member) {
ctx_members.insert(member, 0); // Only insert if member doesn't exist
}
}

Ok(())
Expand Down Expand Up @@ -247,7 +242,7 @@ fn grant(
check_and_increment_nonce(context, nonce, signer_id)?;

for (identity, capability) in capabilities {
let is_member = context.members.deref().contains(&identity);
let is_member = context.members.deref().contains_key(&identity);

if !is_member {
return Err("unable to grant privileges to non-member".to_string());
Expand Down Expand Up @@ -379,12 +374,15 @@ fn check_and_increment_nonce(
signer_id: &SignerId,
) -> Result<(), String> {
let context_identity = signer_id.rt().expect("infallible conversion");
let current_nonce = *context.member_nonces.get(&context_identity).unwrap_or(&0);

let guard_ref = context.members.get(signer_id).map_err(|e| e.to_string())?;
let mut members = guard_ref.get_mut();

let current_nonce = members.get(&context_identity).copied().unwrap_or(0);

if current_nonce != nonce {
return Err("invalid nonce".into());
}

context.member_nonces.insert(context_identity, nonce + 1);
members.insert(context_identity, nonce + 1);
Ok(())
}
9 changes: 5 additions & 4 deletions contracts/icp/context-config/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::BTreeMap;
use std::ops::Deref;

use calimero_context_config::icp::repr::ICRepr;
use calimero_context_config::icp::types::{ICApplication, ICCapability};
Expand Down Expand Up @@ -56,8 +57,8 @@ fn members(
.get(&context_id)
.expect("context does not exist");

let members = &*context.members;
members.iter().skip(offset).take(length).cloned().collect()
let members = context.members.deref();
members.keys().skip(offset).take(length).cloned().collect()
})
}

Expand All @@ -69,7 +70,7 @@ fn has_member(context_id: ICRepr<ContextId>, identity: ICRepr<ContextIdentity>)
.get(&context_id)
.expect("context does not exist");

context.members.contains(&identity)
context.members.deref().contains_key(&identity)
})
}

Expand Down Expand Up @@ -141,7 +142,7 @@ fn fetch_nonce(context_id: ICRepr<ContextId>, member_id: ICRepr<ContextIdentity>
configs
.contexts
.get(&context_id)
.and_then(|context| context.member_nonces.get(&member_id))
.and_then(|context| context.members.deref().get(&member_id))
.copied()
})
}

0 comments on commit 2fa86ac

Please sign in to comment.