Skip to content

Commit

Permalink
fix: reverted back grouping of members and nonces
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Dec 20, 2024
1 parent d3d22a7 commit fcd5b3a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 36 deletions.
4 changes: 3 additions & 1 deletion contracts/icp/context-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::collections::BTreeSet;

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

#[derive(CandidType, Deserialize, Debug)]
Expand Down
45 changes: 15 additions & 30 deletions contracts/icp/context-config/src/mutate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::BTreeMap;
use std::ops::Deref;

use calimero_context_config::icp::repr::ICRepr;
Expand Down Expand Up @@ -70,14 +69,15 @@ async fn add_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"), 0)]
.into_iter()
.collect(),
[author_id.rt().expect("infallible conversion")].into_iter().collect(),
),
proxy: Guard::new(
author_id.rt().expect("infallible conversion"),
proxy_canister_id,
),
member_nonces: [(author_id.rt().expect("infallible conversion"), 0)]
.into_iter()
.collect(),
};

// Store context
Expand Down Expand Up @@ -175,13 +175,13 @@ fn add_members(
// Check nonce
check_and_increment_nonce(context, nonce, signer_id)?;

// Rest of the function...
let guard_ref = context.members.get(signer_id).map_err(|e| e.to_string())?;
let mut ctx_members = guard_ref.get_mut();

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

Expand All @@ -204,24 +204,12 @@ fn remove_members(
// Check nonce
check_and_increment_nonce(context, nonce, signer_id)?;

// Get mutable access to the members through the Guard
let mut ctx_members = context
.members
.get(signer_id)
.map_err(|e| e.to_string())?
.get_mut();
let guard_ref = context.members.get(signer_id).map_err(|e| e.to_string())?;
let mut ctx_members = guard_ref.get_mut();

for member in members {
ctx_members.remove(&member);

// Revoke privileges
ctx_members
.privileges()
.revoke(&member.rt().expect("infallible conversion"));
context
.application
.privileges()
.revoke(&member.rt().expect("infallible conversion"));
context.member_nonces.remove(&member);
}

Ok(())
Expand All @@ -244,7 +232,7 @@ fn grant(
check_and_increment_nonce(context, nonce, signer_id)?;

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

if !is_member {
return Err("unable to grant privileges to non-member".to_string());
Expand Down Expand Up @@ -376,15 +364,12 @@ fn check_and_increment_nonce(
signer_id: &SignerId,
) -> Result<(), String> {
let context_identity = signer_id.rt().expect("infallible conversion");
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);

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

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

members.insert(context_identity, nonce + 1);
context.member_nonces.insert(context_identity, nonce + 1);
Ok(())
}
9 changes: 4 additions & 5 deletions contracts/icp/context-config/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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 @@ -57,8 +56,8 @@ fn members(
.get(&context_id)
.expect("context does not exist");

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

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

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

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

0 comments on commit fcd5b3a

Please sign in to comment.