Skip to content

Commit 8efb852

Browse files
committed
feat(torus0): dont change root namespace fee
1 parent 0645ef0 commit 8efb852

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

pallets/torus0/src/namespace.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ impl<T: Config> NamespacePricingConfig<T> {
6060
&self,
6161
account_namespace_count: u32,
6262
) -> Result<BalanceOf<T>, polkadot_sdk::sp_runtime::DispatchError> {
63+
if account_namespace_count == 0 {
64+
return Ok(Zero::zero());
65+
}
66+
6367
let Self {
6468
base_fee,
6569
count_midpoint,
@@ -95,6 +99,10 @@ impl<T: Config> NamespacePricingConfig<T> {
9599
/// is provided and contains more segments, the prefix (agent literal and agent name)
96100
/// will be dropped.
97101
pub fn namespace_deposit(&self, path: &NamespacePath) -> BalanceOf<T> {
102+
if path.is_agent_root() {
103+
return Zero::zero();
104+
}
105+
98106
self.deposit_per_byte
99107
.saturating_mul((path.as_bytes().len() as u32).into())
100108
}
@@ -243,14 +251,17 @@ pub(crate) fn create_namespace0<T: Config>(
243251
};
244252

245253
Namespaces::<T>::insert(&owner, path, metadata);
254+
255+
Pallet::<T>::deposit_event(Event::NamespaceCreated {
256+
owner: owner.clone(),
257+
path: path.clone(),
258+
});
246259
}
247260

248261
NamespaceCount::<T>::mutate(&owner, |count| {
249262
*count = count.saturating_add(missing_paths.len() as u32)
250263
});
251264

252-
Pallet::<T>::deposit_event(Event::NamespaceCreated { owner, path });
253-
254265
Ok(())
255266
}
256267

@@ -286,6 +297,10 @@ pub fn delete_namespace<T: Config>(
286297
for (path_to_delete, deposit) in namespaces_to_delete {
287298
total_deposit = total_deposit.saturating_add(deposit);
288299
Namespaces::<T>::remove(&owner, &path_to_delete);
300+
Pallet::<T>::deposit_event(Event::NamespaceDeleted {
301+
owner: owner.clone(),
302+
path: path_to_delete.clone(),
303+
});
289304
}
290305

291306
NamespaceCount::<T>::mutate(&owner, |count| *count = count.saturating_sub(deleted_count));
@@ -294,7 +309,5 @@ pub fn delete_namespace<T: Config>(
294309
T::Currency::unreserve(owner, total_deposit);
295310
}
296311

297-
Pallet::<T>::deposit_event(Event::NamespaceDeleted { owner, path });
298-
299312
Ok(())
300313
}

pallets/torus0/tests/namespace.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,21 @@ use polkadot_sdk::{
99
};
1010
use test_utils::{
1111
as_tors, get_origin, new_test_ext, pallet_governance, pallet_permission0::Permissions,
12-
Balances, Test,
12+
Balances, Test, Torus0,
1313
};
1414

1515
/// Helper function to register an agent with a specific name and balance
1616
fn register_agent_with(account_id: u32, name: &str, balance: u128) {
17-
// Register the agent with the specified name
18-
pallet_torus0::Agents::<Test>::set(
19-
account_id,
20-
Some(pallet_torus0::agent::Agent {
21-
key: account_id,
22-
name: name.as_bytes().to_vec().try_into().unwrap(),
23-
url: Default::default(),
24-
metadata: Default::default(),
25-
weight_penalty_factor: Default::default(),
26-
registration_block: <polkadot_sdk::frame_system::Pallet<Test>>::block_number(),
27-
fees: Default::default(),
28-
last_update_block: Default::default(),
29-
}),
30-
);
31-
3217
// Set the balance for the account
3318
let _ = Balances::deposit_creating(&account_id, balance);
19+
let name = name.as_bytes().to_vec();
20+
Torus0::register_agent(
21+
get_origin(account_id),
22+
name.clone(),
23+
name.clone(),
24+
name.clone(),
25+
)
26+
.unwrap();
3427
}
3528

3629
fn set_namespace_config() {
@@ -155,7 +148,7 @@ fn namespace_fee_zero_steepness() {
155148

156149
// steepness is 0, so always expect midpoint
157150
let expected = 1000 + 1000;
158-
assert_eq!(fee_0, expected);
151+
assert_eq!(fee_0, 0);
159152
assert_eq!(fee_10, expected);
160153
assert_eq!(fee_20, expected);
161154
});
@@ -239,9 +232,9 @@ fn namespace_deposit_basic() {
239232
max_fee_multiplier: 100,
240233
};
241234

242-
// The simple agent path
235+
// The root agent path has no deposit requirement
243236
let path_agent = "agent.alice".parse().unwrap();
244-
assert_eq!(config.namespace_deposit(&path_agent), 1100);
237+
assert_eq!(config.namespace_deposit(&path_agent), 0);
245238

246239
// Single character
247240
let path_a = "agent.alice.a".parse().unwrap();
@@ -340,7 +333,7 @@ fn namespace_fee_precision_handling() {
340333
let fee_1 = config.namespace_fee(1).unwrap();
341334
let fee_2 = config.namespace_fee(2).unwrap();
342335

343-
assert!(fee_0 > as_tors(1));
336+
assert_eq!(fee_0, 0);
344337
assert!(fee_1 > fee_0);
345338
assert!(fee_2 > fee_1);
346339
});
@@ -534,6 +527,7 @@ fn calculate_cost_no_existing_namespaces() {
534527
test_utils::register_empty_agent(owner);
535528

536529
let paths: &[NamespacePath] = &[
530+
"agent.alice".parse().unwrap(),
537531
"agent.alice.v1".parse().unwrap(),
538532
"agent.alice.v1.compute".parse().unwrap(),
539533
"agent.alice.v1.compute.gpu".parse().unwrap(),
@@ -763,7 +757,7 @@ fn namespace_must_start_with_agent_prefix() {
763757
// Verify that a valid namespace starting with "agent." works
764758
assert_ok!(pallet_torus0::Pallet::<Test>::create_namespace(
765759
get_origin(0),
766-
BoundedVec::truncate_from(b"agent.alice".to_vec())
760+
BoundedVec::truncate_from(b"agent.alice.bla".to_vec())
767761
));
768762
});
769763
}
@@ -776,13 +770,13 @@ fn create_namespace_simple() {
776770

777771
assert_ok!(pallet_torus0::Pallet::<Test>::create_namespace(
778772
get_origin(0),
779-
BoundedVec::truncate_from(b"agent.alice".to_vec())
773+
BoundedVec::truncate_from(b"agent.alice.a".to_vec())
780774
));
781775

782776
// Verify namespace was created
783777
assert!(pallet_torus0::Namespaces::<Test>::contains_key(
784778
pallet_torus0::namespace::NamespaceOwnership::Account(0),
785-
pallet_torus0_api::NamespacePath::new_agent(b"agent.alice").unwrap()
779+
pallet_torus0_api::NamespacePath::new_agent(b"agent.alice.a").unwrap()
786780
));
787781
});
788782
}
@@ -830,11 +824,11 @@ fn create_namespace_already_exists() {
830824
};
831825

832826
// Create namespace first time
833-
assert_ok!(create_namespace(b"agent.alice"));
827+
assert_ok!(create_namespace(b"agent.alice.a"));
834828

835829
// Try to create same namespace again
836830
assert_err!(
837-
create_namespace(b"agent.alice"),
831+
create_namespace(b"agent.alice.a"),
838832
pallet_torus0::Error::<Test>::NamespaceAlreadyExists
839833
);
840834
});
@@ -882,7 +876,7 @@ fn create_namespace_insufficient_balance() {
882876
assert_err!(
883877
pallet_torus0::Pallet::<Test>::create_namespace(
884878
get_origin(0),
885-
BoundedVec::truncate_from(b"agent.alice".to_vec())
879+
BoundedVec::truncate_from(b"agent.alice.b".to_vec())
886880
),
887881
pallet_balances::Error::<Test>::InsufficientBalance
888882
);

0 commit comments

Comments
 (0)