diff --git a/contracts/icp/context-config/src/lib.rs b/contracts/icp/context-config/src/lib.rs index 40cb300c1..c1795e755 100644 --- a/contracts/icp/context-config/src/lib.rs +++ b/contracts/icp/context-config/src/lib.rs @@ -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}; @@ -21,9 +21,8 @@ thread_local! { #[derive(CandidType, Deserialize, Debug)] pub struct Context { pub application: Guard, - pub members: Guard>>, + pub members: Guard, u64>>, pub proxy: Guard, - pub member_nonces: BTreeMap, u64>, } #[derive(CandidType, Deserialize, Debug)] diff --git a/contracts/icp/context-config/src/mutate.rs b/contracts/icp/context-config/src/mutate.rs index b00f43102..ce4dd339c 100644 --- a/contracts/icp/context-config/src/mutate.rs +++ b/contracts/icp/context-config/src/mutate.rs @@ -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()); @@ -113,7 +107,7 @@ async fn deploy_proxy_contract(context_id: ICRepr) -> Result 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(()) } diff --git a/contracts/icp/context-config/src/query.rs b/contracts/icp/context-config/src/query.rs index fc2b84140..6ba5d7c24 100644 --- a/contracts/icp/context-config/src/query.rs +++ b/contracts/icp/context-config/src/query.rs @@ -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}; @@ -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() }) } @@ -69,7 +70,7 @@ fn has_member(context_id: ICRepr, identity: ICRepr) .get(&context_id) .expect("context does not exist"); - context.members.contains(&identity) + context.members.deref().contains_key(&identity) }) } @@ -141,7 +142,7 @@ fn fetch_nonce(context_id: ICRepr, member_id: ICRepr configs .contexts .get(&context_id) - .and_then(|context| context.member_nonces.get(&member_id)) + .and_then(|context| context.members.deref().get(&member_id)) .copied() }) }