Skip to content

Commit

Permalink
chore: dedupe icp types (#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx authored Dec 5, 2024
1 parent eeb9c33 commit 9c895cf
Show file tree
Hide file tree
Showing 27 changed files with 1,861 additions and 2,302 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/icp/context-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
bs58.workspace = true
calimero-context-config.workspace = true
calimero-context-config = { workspace = true, features = ["icp"] }
candid = "0.10"
ed25519-dalek.workspace = true
ic-cdk = "0.16"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ type ICApplication = record {
size : nat64;
};
type ICCapability = variant { Proxy; ManageMembers; ManageApplication };
type ICPSigned = record { signature : blob; _phantom : null; payload : blob };
type ICSigned = record { signature : blob; _phantom : null; payload : blob };
type Result = variant { Ok; Err : text };
service : () -> {
application : (blob) -> (ICApplication) query;
application_revision : (blob) -> (nat64) query;
has_member : (blob, blob) -> (bool) query;
members : (blob, nat64, nat64) -> (vec blob) query;
members_revision : (blob) -> (nat64) query;
mutate : (ICPSigned) -> (Result);
mutate : (ICSigned) -> (Result);
privileges : (blob, vec blob) -> (
vec record { blob; vec ICCapability },
) query;
Expand Down
30 changes: 13 additions & 17 deletions contracts/icp/context-config/src/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ use std::collections::BTreeSet;
use std::fmt;
use std::ops::{Deref, DerefMut};

use calimero_context_config::types::Revision;
use calimero_context_config::icp::repr::ICRepr;
use calimero_context_config::types::{Revision, SignerId};
use candid::CandidType;
use serde::{Deserialize, Serialize};
use serde::Deserialize;

use crate::types::ICSignerId;

#[derive(CandidType, Serialize, Deserialize, Clone, Debug)]
#[derive(CandidType, Deserialize, Debug)]
pub struct Guard<T> {
inner: T,
revision: Revision,
privileged: BTreeSet<ICSignerId>,
privileged: BTreeSet<ICRepr<SignerId>>,
}

#[derive(Debug)]
Expand All @@ -27,18 +26,15 @@ impl fmt::Display for UnauthorizedAccess {
}

impl<T> Guard<T> {
pub fn new(creator: ICSignerId, inner: T) -> Self {
pub fn new(creator: SignerId, inner: T) -> Self {
Self {
inner,
revision: 0,
privileged: BTreeSet::from([creator]),
privileged: BTreeSet::from([ICRepr::new(creator)]),
}
}

pub fn get(
&mut self,
signer_id: &ICSignerId,
) -> Result<GuardHandle<'_, T>, UnauthorizedAccess> {
pub fn get(&mut self, signer_id: &SignerId) -> Result<GuardHandle<'_, T>, UnauthorizedAccess> {
if !self.privileged.contains(signer_id) {
return Err(UnauthorizedAccess { _priv: () });
}
Expand All @@ -49,7 +45,7 @@ impl<T> Guard<T> {
self.inner
}

pub fn privileged(&self) -> &BTreeSet<ICSignerId> {
pub fn privileged(&self) -> &BTreeSet<ICRepr<SignerId>> {
&self.privileged
}

Expand Down Expand Up @@ -120,15 +116,15 @@ impl<T> Drop for GuardMut<'_, T> {

#[derive(Debug)]
pub struct Privileges<'a> {
inner: &'a mut BTreeSet<ICSignerId>,
inner: &'a mut BTreeSet<ICRepr<SignerId>>,
}

impl Privileges<'_> {
pub fn grant(&mut self, signer_id: ICSignerId) {
self.inner.insert(signer_id);
pub fn grant(&mut self, signer_id: SignerId) {
self.inner.insert(ICRepr::new(signer_id));
}

pub fn revoke(&mut self, signer_id: &ICSignerId) {
pub fn revoke(&mut self, signer_id: &SignerId) {
self.inner.remove(signer_id);
}
}
69 changes: 41 additions & 28 deletions contracts/icp/context-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,68 @@
use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap};
use std::collections::{BTreeMap, BTreeSet};

use calimero_context_config::icp::repr::ICRepr;
use calimero_context_config::icp::types::{ICApplication, ICCapability, ICRequest, ICSigned};
use calimero_context_config::types::{ContextId, ContextIdentity, SignerId};
use candid::{CandidType, Principal};
use guard::Guard;
use serde::{Deserialize, Serialize};
use serde::Deserialize;

use crate::types::{
ICApplication, ICCapability, ICContextId, ICContextIdentity, ICPSigned, ICSignerId, Request,
};
mod guard;
mod mutate;
mod query;
mod sys;

pub mod guard;
pub mod mutate;
pub mod query;
pub mod sys;
pub mod types;
use guard::Guard;

#[derive(CandidType, Serialize, Deserialize, Clone, Debug)]
thread_local! {
pub static CONTEXT_CONFIGS: RefCell<Option<ContextConfigs>> = RefCell::new(None);
}

#[derive(CandidType, Deserialize, Debug)]
pub struct Context {
pub application: Guard<ICApplication>,
pub members: Guard<Vec<ICContextIdentity>>,
pub members: Guard<BTreeSet<ICRepr<ContextIdentity>>>,
pub proxy: Guard<Principal>,
}

#[derive(CandidType, Deserialize, Clone, Debug)]
#[derive(CandidType, Deserialize, Debug)]
pub struct ContextConfigs {
pub contexts: HashMap<ICContextId, Context>,
pub contexts: BTreeMap<ICRepr<ContextId>, Context>,
pub proxy_code: Option<Vec<u8>>,
pub owner: Principal,
pub ledger_id: Principal,
}

impl Default for ContextConfigs {
fn default() -> Self {
Self {
contexts: HashMap::new(),
#[ic_cdk::init]
fn init() {
CONTEXT_CONFIGS.with(|state| {
*state.borrow_mut() = Some(ContextConfigs {
contexts: BTreeMap::new(),
proxy_code: None,
owner: ic_cdk::api::caller(),
ledger_id: Principal::anonymous(),
}
}
});
});
}

thread_local! {
pub static CONTEXT_CONFIGS: RefCell<ContextConfigs> = RefCell::new(ContextConfigs::default());
fn with_state<F, R>(f: F) -> R
where
F: FnOnce(&ContextConfigs) -> R,
{
CONTEXT_CONFIGS.with(|configs| {
let configs = configs.borrow();
f(configs.as_ref().expect("cannister is being upgraded"))
})
}

#[ic_cdk::init]
fn init() {
CONTEXT_CONFIGS.with(|state| {
*state.borrow_mut() = ContextConfigs::default();
});
fn with_state_mut<F, R>(f: F) -> R
where
F: FnOnce(&mut ContextConfigs) -> R,
{
CONTEXT_CONFIGS.with(|configs| {
let mut configs = configs.borrow_mut();
f(configs.as_mut().expect("cannister is being upgraded"))
})
}

ic_cdk::export_candid!();
Loading

0 comments on commit 9c895cf

Please sign in to comment.