Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ pub use token_state_manager::{
builtin_policy_commit_for_token, builtin_token_id_for_policy_commit,
canonical_balance_key_for_commit, register_policy_commit_ticker, TOKEN_CREATION_FEE_ERA,
resolve_ticker_for_policy_commit, derive_canonical_balance_key, resolve_policy_commit,
PolicyCommitResolver,
};
418 changes: 201 additions & 217 deletions dsm_client/deterministic_state_machine/dsm/src/core/token/policy/mod.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ impl Entries {
#[derive(Debug)]
pub struct PolicyCache {
entries: RwLock<Entries>,
token_index: RwLock<HashMap<String, PolicyAnchor>>,
config: PolicyCacheConfig,
}

impl PolicyCache {
pub fn new(config: PolicyCacheConfig) -> Self {
Self {
entries: RwLock::new(Entries::default()),
token_index: RwLock::new(HashMap::new()),
config,
}
}
Expand Down Expand Up @@ -95,15 +93,6 @@ impl PolicyCache {
);
}

pub fn index_token_policy(&self, token_id: String, anchor: PolicyAnchor) {
let mut index = self.token_index.write();
index.insert(token_id, anchor);
}

pub fn get_anchor_for_token(&self, token_id: &str) -> Option<PolicyAnchor> {
self.token_index.read().get(token_id).cloned()
}

pub fn len(&self) -> usize {
self.entries.read().by_anchor.len()
}
Expand All @@ -116,25 +105,21 @@ impl PolicyCache {
#[cfg(test)]
mod tests {
use super::*;
use crate::types::policy_types::{PolicyCondition, PolicyFile, PolicyRole};
use crate::types::policy_types::{PolicyCondition, PolicyFile};

fn make_policy(author: &str) -> TokenPolicy {
let mut pf = PolicyFile::new("TestPolicy", "1.0", author);
/// A policy at the commitment `[tag; 32]`.
fn make_policy(tag: u8) -> TokenPolicy {
let mut pf = PolicyFile::new("TestPolicy", "1.0", "author");
pf.add_condition(PolicyCondition::OperationRestriction {
allowed_operations: vec!["Transfer".to_string()],
});
pf.add_role(PolicyRole {
id: "owner".into(),
name: "Owner".into(),
permissions: vec!["Transfer".into()],
});
TokenPolicy::new(pf).unwrap()
TokenPolicy::new_with_anchor(pf, PolicyAnchor::from_bytes([tag; 32]))
}

#[tokio::test]
async fn test_store_and_get_policy() {
let cache = PolicyCache::new(PolicyCacheConfig::default());
let policy = make_policy("author-stored");
let policy = make_policy(0x01);
let anchor = policy.anchor.clone();

cache.store_policy(anchor.clone(), policy.clone());
Expand All @@ -156,9 +141,9 @@ mod tests {
let config = PolicyCacheConfig { max_entries: 2 };
let cache = PolicyCache::new(config);

let p1 = make_policy("author-p1");
let p2 = make_policy("author-p2");
let p3 = make_policy("author-p3");
let p1 = make_policy(0x11);
let p2 = make_policy(0x12);
let p3 = make_policy(0x13);

let a1 = p1.anchor.clone();
let a2 = p2.anchor.clone();
Expand All @@ -173,25 +158,6 @@ mod tests {
assert!(cache.entries.read().by_anchor.contains_key(&a3));
}

#[test]
fn test_index_token_policy_and_lookup() {
let cache = PolicyCache::new(PolicyCacheConfig::default());
let policy = make_policy("author-indexed");
let anchor = policy.anchor.clone();

cache.store_policy(anchor.clone(), policy);
cache.index_token_policy("tok-123".to_string(), anchor.clone());

let looked_up = cache.get_anchor_for_token("tok-123");
assert_eq!(looked_up, Some(anchor));
}

#[test]
fn test_index_token_policy_missing_returns_none() {
let cache = PolicyCache::new(PolicyCacheConfig::default());
assert!(cache.get_anchor_for_token("nonexistent").is_none());
}

#[test]
fn test_default_config_values() {
let config = PolicyCacheConfig::default();
Expand All @@ -204,7 +170,7 @@ mod tests {
assert!(cache.is_empty());
assert_eq!(cache.len(), 0);

let policy = make_policy("author-len");
let policy = make_policy(0x21);
let anchor = policy.anchor.clone();
cache.store_policy(anchor, policy);

Expand All @@ -217,7 +183,7 @@ mod tests {
let config = PolicyCacheConfig { max_entries: 2 };
let cache = PolicyCache::new(config);

let policy = make_policy("author-same");
let policy = make_policy(0x22);
let anchor = policy.anchor.clone();

cache.store_policy(anchor.clone(), policy.clone());
Expand Down
Loading
Loading