Skip to content

Commit

Permalink
0.7.7 territory (#365)
Browse files Browse the repository at this point in the history
* temp save

* feat: territory

* feat: file for territory version

* fix: replenish event and fix lock block

* feat: territry file delivery

* fix: fix benchmark error

* feat: territory benchmark
  • Loading branch information
ytqaljn authored Jul 1, 2024
1 parent f9e8e33 commit 19d9c07
Show file tree
Hide file tree
Showing 19 changed files with 1,716 additions and 843 deletions.
2 changes: 2 additions & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ pub type ReportSign = BoundedVec<u8, ConstU32<344>>;
pub type Report = BoundedVec<u8, ConstU32<5000>>;
pub type Cert = BoundedVec<u8, ConstU32<1588>>;

pub type TerrName = BoundedVec<u8, ConstU32<63>>;

#[derive(PartialEq, Eq, Encode, Decode, Clone, RuntimeDebug, MaxEncodedLen, TypeInfo)]
pub enum DataType {
File,
Expand Down
10 changes: 5 additions & 5 deletions pallets/audit/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::{Pallet as Audit, *};
use crate::{Pallet as Audit};
// // use cp_cess_common::{IpAddress, Hash, DataType};
// // use codec::{alloc::string::ToString, Decode};
use frame_benchmarking::{
Expand Down Expand Up @@ -137,7 +137,7 @@ pub fn bench_generate_challenge<T: Config>(miner: AccountOf<T>) -> Result<(), &'
benchmarks! {
submit_idle_proof {
log::info!("start submit_idle_proof");
pallet_tee_worker::benchmarking::generate_workers::<T>();
pallet_tee_worker::benchmarking::generate_workers::<T>()?;
let miner: AccountOf<T> = account("miner1", 100, SEED);
pallet_sminer::benchmarking::register_positive_miner::<T>(miner.clone())?;
let _ = pallet_file_bank::benchmarking::cert_idle_for_miner::<T>(miner.clone())?;
Expand All @@ -151,7 +151,7 @@ benchmarks! {

submit_service_proof {
log::info!("start submit_service_proof");
pallet_tee_worker::benchmarking::generate_workers::<T>();
pallet_tee_worker::benchmarking::generate_workers::<T>()?;
let miner: AccountOf<T> = account("miner1", 100, SEED);
pallet_sminer::benchmarking::register_positive_miner::<T>(miner.clone())?;
let _ = pallet_file_bank::benchmarking::cert_idle_for_miner::<T>(miner.clone())?;
Expand All @@ -165,7 +165,7 @@ benchmarks! {

submit_verify_idle_result {
log::info!("start submit_verify_idle_result");
pallet_tee_worker::benchmarking::generate_workers::<T>();
pallet_tee_worker::benchmarking::generate_workers::<T>()?;
let miner: AccountOf<T> = account("miner1", 100, SEED);
pallet_sminer::benchmarking::register_positive_miner::<T>(miner.clone())?;
let _ = pallet_file_bank::benchmarking::cert_idle_for_miner::<T>(miner.clone())?;
Expand Down Expand Up @@ -203,7 +203,7 @@ benchmarks! {

submit_verify_service_result {
log::info!("start submit_verify_service_result");
pallet_tee_worker::benchmarking::generate_workers::<T>();
pallet_tee_worker::benchmarking::generate_workers::<T>()?;
let miner: AccountOf<T> = account("miner1", 100, SEED);
pallet_sminer::benchmarking::register_positive_miner::<T>(miner.clone())?;
let _ = pallet_file_bank::benchmarking::cert_idle_for_miner::<T>(miner.clone())?;
Expand Down
4 changes: 2 additions & 2 deletions pallets/cess-treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use frame_support::{
// use sp_std::prelude::*;
use sp_runtime::{
SaturatedConversion, Perbill,
traits::{CheckedAdd, CheckedSub, CheckedDiv, AccountIdConversion},
traits::{CheckedAdd, CheckedSub, AccountIdConversion},
};
use frame_system::{
pallet_prelude::OriginFor,
Expand Down Expand Up @@ -83,7 +83,7 @@ pub mod pallet {
pub enum Error<T> {
/// Data operation overflow
Overflow,

/// Logically speaking, errors that should not occur
Unexpected,
}

Expand Down
37 changes: 14 additions & 23 deletions pallets/file-bank/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ pub fn cert_idle_for_miner<T: Config>(miner: T::AccountId) -> Result<(), &'stati
}

pub fn buy_space<T: Config>(user: T::AccountId) -> Result<(), &'static str> {
let territory_name: TerrName = "t1".as_bytes().to_vec().try_into().map_err(|_| "boundedvec error")?;
<T as pallet_sminer::Config>::Currency::make_free_balance_be(
&user,
365_000_000_000_000_000_000_000u128.try_into().map_err(|_| "tryinto error!").expect("tryinto error!"),
);

pallet_storage_handler::Pallet::<T>::buy_space(RawOrigin::Signed(user).into(), 10)?;
pallet_storage_handler::Pallet::<T>::mint_territory(RawOrigin::Signed(user).into(), 10, territory_name)?;

Ok(())
}
Expand All @@ -107,10 +108,12 @@ pub fn initialize_file_from_scratch<T: Config>() -> Result<(), &'static str> {
let bucket_name = "test-bucket1".as_bytes().to_vec();
let file_hash: Hash = Hash([80u8; 64]);
let file_size: u128 = SEGMENT_SIZE * 3;
let territory_name: TerrName = "t1".as_bytes().to_vec().try_into().map_err(|_| "boundedvec error")?;
let user_brief = UserBrief::<T> {
user: user.clone(),
file_name: file_name.try_into().map_err(|_e| "file name convert err")?,
bucket_name: bucket_name.try_into().map_err(|_e| "bucket name convert err")?,
territory_name,
};

let mut deal_info: BoundedVec<SegmentList<T>, T::SegmentCount> = Default::default();
Expand Down Expand Up @@ -229,10 +232,12 @@ benchmarks! {
let bucket_name = "test-bucket1".as_bytes().to_vec();
let file_hash: Hash = Hash([80u8; 64]);
let file_size: u128 = SEGMENT_SIZE * 3;
let territory_name: TerrName = "t1".as_bytes().to_vec().try_into().map_err(|_| "boundedvec error")?;
let user_brief = UserBrief::<T> {
user: user.clone(),
file_name: file_name.try_into().map_err(|_e| "file name convert err")?,
bucket_name: bucket_name.try_into().map_err(|_e| "bucket name convert err")?,
territory_name,
};

let mut deal_info: BoundedVec<SegmentList<T>, T::SegmentCount> = Default::default();
Expand Down Expand Up @@ -280,10 +285,12 @@ benchmarks! {
let bucket_name = "test-bucket1".as_bytes().to_vec();
let file_hash: Hash = Hash([80u8; 64]);
let file_size: u128 = SEGMENT_SIZE * 3;
let territory_name: TerrName = "t1".as_bytes().to_vec().try_into().map_err(|_| "boundedvec error")?;
let user_brief = UserBrief::<T> {
user: user.clone(),
file_name: file_name.try_into().map_err(|_e| "file name convert err")?,
bucket_name: bucket_name.try_into().map_err(|_e| "bucket name convert err")?,
territory_name,
};

let mut deal_info: BoundedVec<SegmentList<T>, T::SegmentCount> = Default::default();
Expand Down Expand Up @@ -336,10 +343,12 @@ benchmarks! {
let bucket_name = "test-bucket1".as_bytes().to_vec();
let file_hash: Hash = Hash([80u8; 64]);
let file_size: u128 = SEGMENT_SIZE * 3;
let territory_name: TerrName = "t1".as_bytes().to_vec().try_into().map_err(|_| "boundedvec error")?;
let user_brief = UserBrief::<T> {
user: user.clone(),
file_name: file_name.try_into().map_err(|_e| "file name convert err")?,
bucket_name: bucket_name.try_into().map_err(|_e| "bucket name convert err")?,
territory_name,
};

let mut deal_info: BoundedVec<SegmentList<T>, T::SegmentCount> = Default::default();
Expand Down Expand Up @@ -414,10 +423,12 @@ benchmarks! {
let bucket_name = "test-bucket1".as_bytes().to_vec();
let file_hash: Hash = Hash([80u8; 64]);
let file_size: u128 = SEGMENT_SIZE * 3;
let territory_name: TerrName = "t1".as_bytes().to_vec().try_into().map_err(|_| "boundedvec error")?;
let user_brief = UserBrief::<T> {
user: user.clone(),
file_name: file_name.try_into().map_err(|_e| "file name convert err")?,
bucket_name: bucket_name.try_into().map_err(|_e| "bucket name convert err")?,
territory_name,
};

let mut deal_info: BoundedVec<SegmentList<T>, T::SegmentCount> = Default::default();
Expand Down Expand Up @@ -516,10 +527,12 @@ benchmarks! {
let bucket_name = "test-bucket1".as_bytes().to_vec();
let file_hash: Hash = Hash([80u8; 64]);
let file_size: u128 = SEGMENT_SIZE * 3;
let territory_name: TerrName = "t1".as_bytes().to_vec().try_into().map_err(|_| "boundedvec error")?;
let user_brief = UserBrief::<T> {
user: user.clone(),
file_name: file_name.try_into().map_err(|_e| "file name convert err")?,
bucket_name: bucket_name.try_into().map_err(|_e| "bucket name convert err")?,
territory_name,
};

let mut deal_info: BoundedVec<SegmentList<T>, T::SegmentCount> = Default::default();
Expand Down Expand Up @@ -650,26 +663,4 @@ benchmarks! {
let (_, space) = T::MinerControl::get_power(&miner2)?;
assert_eq!(space, FRAGMENT_SIZE);
}

ownership_transfer {
log::info!("start ownership_transfer");
initialize_file_from_scratch::<T>()?;
let user: AccountOf<T> = account("user1", 100, SEED);
let user2: AccountOf<T> = account("user2", 100, SEED);
buy_space::<T>(user2.clone())?;
let file_name = "test-file".as_bytes().to_vec();
let bucket_name = "test-bucket1".as_bytes().to_vec();
let bucket_name: BoundedVec<u8, T::NameStrLimit> = bucket_name.try_into().unwrap();
FileBank::<T>::create_bucket(RawOrigin::Signed(user2.clone()).into(), user2.clone(), bucket_name.clone())?;
let user_brief = UserBrief::<T> {
user: user2.clone(),
file_name: file_name.try_into().map_err(|_e| "file name convert err")?,
bucket_name: bucket_name.clone(),
};
}: _(RawOrigin::Signed(user.clone()), user_brief, Hash([80u8; 64]))
verify {
assert!(<Bucket<T>>::contains_key(&user2, &bucket_name));
let info = <Bucket<T>>::try_get(&user2, &bucket_name).unwrap();
assert!(info.object_list.contains(&Hash([80u8; 64])));
}
}
21 changes: 15 additions & 6 deletions pallets/file-bank/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl<T: Config> Pallet<T> {
let deal_info = <DealMap<T>>::try_get(deal_hash).map_err(|_| Error::<T>::NonExistent)?;
let segment_len = deal_info.segment_list.len() as u128;
let needed_space = Self::cal_file_size(segment_len);
T::StorageHandle::unlock_user_space(&deal_info.user.user, needed_space)?;
T::StorageHandle::unlock_user_space(&deal_info.user.user, &deal_info.user.territory_name, needed_space)?;
// unlock mienr space
for complete_info in deal_info.complete_list {
T::MinerControl::unlock_space(&complete_info.miner, FRAGMENT_SIZE * segment_len)?;
Expand Down Expand Up @@ -194,7 +194,7 @@ impl<T: Config> Pallet<T> {
if acc == &user_brief.user {
let file_size = Self::cal_file_size(file.segment_list.len() as u128);
if user_clear {
T::StorageHandle::update_user_space(acc, 2, file_size)?;
T::StorageHandle::sub_territory_used_space(acc, &user_brief.territory_name, file_size)?;
}
file.owner.remove(index);
break;
Expand Down Expand Up @@ -264,8 +264,13 @@ impl<T: Config> Pallet<T> {
}

if user_clear {
T::StorageHandle::update_user_space(acc, 2, total_fragment_dec as u128 * FRAGMENT_SIZE)?;
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
for user_brief in file.owner {
if &user_brief.user == acc {
T::StorageHandle::sub_territory_used_space(acc, &user_brief.territory_name, total_fragment_dec as u128 * FRAGMENT_SIZE)?;
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
break;
}
}
}
T::StorageHandle::sub_total_service_space(total_fragment_dec as u128 * FRAGMENT_SIZE)?;
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
Expand Down Expand Up @@ -319,9 +324,13 @@ impl<T: Config> Pallet<T> {
user: &AccountOf<T>,
file_hash: Hash,
file_size: u128,
territory_name: TerrName,
) -> DispatchResult {
let file_info =
UserFileSliceInfo { file_hash: file_hash, file_size };
let file_info = UserFileSliceInfo {
territory_name,
file_hash: file_hash,
file_size,
};
<UserHoldFileList<T>>::try_mutate(user, |v| -> DispatchResult {
ensure!(!v.contains(&file_info), Error::<T>::Existed);
v.try_push(file_info).map_err(|_| Error::<T>::StorageLimitReached)?;
Expand Down
3 changes: 0 additions & 3 deletions pallets/file-bank/src/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
pub mod receptionist;
pub use receptionist::*;

pub mod dealimpl;
pub use dealimpl::*;
12 changes: 6 additions & 6 deletions pallets/file-bank/src/impls/receptionist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ impl<T: Config> Receptionist<T> {
.checked_mul(15).ok_or(Error::<T>::Overflow)?
.checked_div(10).ok_or(Error::<T>::Overflow)?
.checked_mul(file.segment_list.len() as u128).ok_or(Error::<T>::Overflow)?;
ensure!(T::StorageHandle::get_user_avail_space(&user_brief.user)? > needed_space, Error::<T>::InsufficientAvailableSpace);
T::StorageHandle::update_user_space(&user_brief.user, 1, needed_space)?;
ensure!(T::StorageHandle::get_user_avail_space(&user_brief.user, &user_brief.territory_name)? > needed_space, Error::<T>::InsufficientAvailableSpace);
T::StorageHandle::add_territory_used_space(&user_brief.user, &user_brief.territory_name, needed_space)?;

if <Bucket<T>>::contains_key(&user_brief.user, &user_brief.bucket_name) {
Pallet::<T>::add_file_to_bucket(&user_brief.user, &user_brief.bucket_name, &file_hash)?;
} else {
Pallet::<T>::create_bucket_helper(&user_brief.user, &user_brief.bucket_name, Some(file_hash))?;
}

Pallet::<T>::add_user_hold_fileslice(&user_brief.user, file_hash, needed_space)?;
Pallet::<T>::add_user_hold_fileslice(&user_brief.user, file_hash, needed_space, user_brief.territory_name.clone())?;
file.owner.try_push(user_brief.clone()).map_err(|_e| Error::<T>::BoundedVecError)?;

Ok(())
Expand All @@ -35,7 +35,7 @@ impl<T: Config> Receptionist<T> {
needed_space: u128,
file_size: u128,
) -> DispatchResult {
T::StorageHandle::lock_user_space(&user_brief.user, needed_space)?;
T::StorageHandle::lock_user_space(&user_brief.user, &user_brief.territory_name, needed_space)?;
// TODO! Replace the file_hash param
Pallet::<T>::generate_deal(file_hash.clone(), deal_info, user_brief.clone(), file_size)?;

Expand All @@ -59,7 +59,7 @@ impl<T: Config> Receptionist<T> {

let segment_count = deal_info.segment_list.len();
let needed_space = Pallet::<T>::cal_file_size(segment_count as u128);
T::StorageHandle::unlock_and_used_user_space(&deal_info.user.user, needed_space)?;
T::StorageHandle::unlock_and_used_user_space(&deal_info.user.user, &deal_info.user.territory_name, needed_space)?;
T::StorageHandle::sub_total_idle_space(needed_space)?;
T::StorageHandle::add_total_service_space(needed_space)?;

Expand All @@ -68,7 +68,7 @@ impl<T: Config> Receptionist<T> {
} else {
Pallet::<T>::create_bucket_helper(&deal_info.user.user, &deal_info.user.bucket_name, Some(deal_hash))?;
}
Pallet::<T>::add_user_hold_fileslice(&deal_info.user.user, deal_hash.clone(), needed_space)?;
Pallet::<T>::add_user_hold_fileslice(&deal_info.user.user, deal_hash.clone(), needed_space, deal_info.user.territory_name.clone())?;
<DealMap<T>>::remove(deal_hash);
Pallet::<T>::deposit_event(Event::<T>::StorageCompleted{ file_hash: deal_hash });
}
Expand Down
Loading

0 comments on commit 19d9c07

Please sign in to comment.