Skip to content

Commit

Permalink
refactor: deal info struct refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ytqaljn committed Nov 9, 2023
1 parent 86a11ec commit 4f52f10
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 93 deletions.
41 changes: 13 additions & 28 deletions c-pallets/file-bank/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,31 @@ impl<T: Config> Pallet<T> {
pub fn generate_file(
file_hash: &Hash,
deal_info: BoundedVec<SegmentList<T>, T::SegmentCount>,
mut miner_task_list: BoundedVec<MinerTaskList<T>, ConstU32<ASSIGN_MINER_IDEAL_QUANTITY>>,
complete_list: BoundedVec<CompleteInfo<T>, T::FragmentCount>,
user_brief: UserBrief<T>,
stat: FileState,
file_size: u128,
) -> DispatchResult {
let mut segment_info_list: BoundedVec<SegmentInfo<T>, T::SegmentCount> = Default::default();
ensure!(complete_list.len() == FRAGMENT_COUNT as usize, Error::<T>::Unexpected);
for segment in deal_info.iter() {
let mut segment_info = SegmentInfo::<T> {
hash: segment.hash,
fragment_list: Default::default(),
};

for miner_task in &mut miner_task_list {
miner_task.fragment_list.sort();
}

for frag_hash in segment.fragment_list.iter() {
for miner_task in &mut miner_task_list {
if let Ok(index) = miner_task.fragment_list.binary_search(&frag_hash) {
let miner = miner_task.miner.clone().ok_or(Error::<T>::Unexpected)?;
let frag_info = FragmentInfo::<T> {
hash: *frag_hash,
avail: true,
miner: miner.clone(),
};
segment_info.fragment_list.try_push(frag_info).map_err(|_e| Error::<T>::BoundedVecError)?;
miner_task.fragment_list.remove(index);
break;
}
}
for (index, fragment_hash) in segment.fragment_list.iter().enumerate() {
let frag_info = FragmentInfo::<T> {
hash: *fragment_hash,
avail: true,
miner: complete_list[index as usize].miner.clone(),
};

segment_info.fragment_list.try_push(frag_info).map_err(|_e| Error::<T>::BoundedVecError)?;
}

segment_info_list.try_push(segment_info).map_err(|_e| Error::<T>::BoundedVecError)?;
}

let cur_block = <frame_system::Pallet<T>>::block_number();

let file_info = FileInfo::<T> {
Expand Down Expand Up @@ -111,7 +101,6 @@ impl<T: Config> Pallet<T> {
pub(super) fn generate_deal(
file_hash: Hash,
file_info: BoundedVec<SegmentList<T>, T::SegmentCount>,
miner_task_list: BoundedVec<MinerTaskList<T>, ConstU32<ASSIGN_MINER_IDEAL_QUANTITY>>,
user_brief: UserBrief<T>,
file_size: u128,
) -> DispatchResult {
Expand All @@ -127,7 +116,6 @@ impl<T: Config> Pallet<T> {
file_size,
segment_list: file_info.clone(),
user: user_brief,
miner_task_list: miner_task_list,
complete_list: Default::default(),
};

Expand Down Expand Up @@ -179,11 +167,8 @@ impl<T: Config> Pallet<T> {
let needed_space = Self::cal_file_size(deal_info.segment_list.len() as u128);
T::StorageHandle::unlock_user_space(&deal_info.user.user, needed_space)?;
// unlock mienr space
for miner_task in deal_info.miner_task_list {
if let Some(miner) = miner_task.miner {
let count = miner_task.fragment_list.len() as u128;
T::MinerControl::unlock_space(&miner, FRAGMENT_SIZE * count)?;
}
for complete_info in deal_info.complete_list {
T::MinerControl::unlock_space(&complete_info.miner, FRAGMENT_SIZE * FRAGMENT_COUNT as u128)?;
}

<DealMap<T>>::remove(deal_hash);
Expand Down
50 changes: 20 additions & 30 deletions c-pallets/file-bank/src/impls/dealimpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,34 @@ use crate::*;

impl<T: Config> DealInfo<T> {
pub fn complete_part(&mut self, miner: AccountOf<T>, index: u8) -> DispatchResult {
for task_info in &mut self.miner_task_list {
if task_info.index == index {
match task_info.miner {
Some(_) => Err(Error::<T>::Existed)?,
None => {
task_info.miner = Some(miner.clone());
T::MinerControl::lock_space(&miner, task_info.fragment_list.len() as u128 * FRAGMENT_SIZE)?;
self.complete_list.try_push(index).map_err(|_| Error::<T>::BoundedVecError)?;
},
};
} else {
if let Some(other_miner) = &task_info.miner {
if other_miner == &miner {
Err(Error::<T>::Existed)?;
}
};
}
for complete_info in &self.complete_list {
ensure!(index != complete_info.index, Error::<T>::Existed);
ensure!(miner != complete_info.miner, Error::<T>::Existed);
}

let complete_info = CompleteInfo::<T> {
index,
miner,
};

self.complete_list.try_push(complete_info).map_err(|_| Error::<T>::BoundedVecError)?;

Ok(())
}

pub fn completed_all(&mut self) -> DispatchResult {
self.stage = 2;
for complete_info in self.complete_list.iter() {
<PendingReplacements<T>>::try_mutate(&complete_info.miner, |pending_space| -> DispatchResult {
let replace_space = FRAGMENT_SIZE
.checked_mul(self.segment_list.len() as u128)
.ok_or(Error::<T>::Overflow)?;

*pending_space = pending_space
.checked_add(replace_space).ok_or(Error::<T>::Overflow)?;

for miner_task in self.miner_task_list.iter() {
// Miners need to report the replaced documents themselves.
// If a challenge is triggered before the report is completed temporarily,
// these documents to be replaced also need to be verified
if let Some(miner) = &miner_task.miner {
<PendingReplacements<T>>::try_mutate(miner, |pending_space| -> DispatchResult {
let replace_space = FRAGMENT_SIZE
.checked_mul(miner_task.fragment_list.len() as u128).ok_or(Error::<T>::Overflow)?;
let pending_space_temp = pending_space.checked_add(replace_space).ok_or(Error::<T>::Overflow)?;
*pending_space = pending_space_temp;
Ok(())
})?;
}
Ok(())
})?;
}

Ok(())
Expand Down
23 changes: 3 additions & 20 deletions c-pallets/file-bank/src/impls/receptionist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,26 @@ impl<T: Config> Receptionist<T> {
file_hash: Hash,
deal_info: BoundedVec<SegmentList<T>, T::SegmentCount>,
user_brief: UserBrief<T>,
assigned_data: BoundedVec<
BoundedVec<Hash, T::MissionCount>,
ConstU32<ASSIGN_MINER_IDEAL_QUANTITY>,
>,
needed_space: u128,
file_size: u128,
) -> DispatchResult {
let mut miner_task_list: BoundedVec<MinerTaskList<T>, ConstU32<ASSIGN_MINER_IDEAL_QUANTITY>> = Default::default();
let mut index = 1;
for fragment_list in assigned_data {
ensure!(fragment_list.len() == deal_info.len(), Error::<T>::SpecError);
let miner_task = MinerTaskList::<T> {
index,
miner: None,
fragment_list,
};
miner_task_list.try_push(miner_task).map_err(|_| Error::<T>::BoundedVecError)?;
index += 1;
}
T::StorageHandle::lock_user_space(&user_brief.user, needed_space)?;
// TODO! Replace the file_hash param
Pallet::<T>::generate_deal(file_hash.clone(), deal_info, miner_task_list, user_brief.clone(), file_size)?;
Pallet::<T>::generate_deal(file_hash.clone(), deal_info, user_brief.clone(), file_size)?;

Ok(())
}

pub fn qualification_report_processing(sender: AccountOf<T>, deal_hash: Hash, deal_info: &mut DealInfo<T>, index: u8) -> DispatchResult {
ensure!(!deal_info.complete_list.contains(&index), Error::<T>::Existed);
deal_info.complete_part(sender.clone(), index)?;

// If it is the last submitter of the order.
if deal_info.complete_list.len() == deal_info.miner_task_list.len() {
if deal_info.complete_list.len() == FRAGMENT_COUNT as usize {
deal_info.completed_all()?;
Pallet::<T>::generate_file(
&deal_hash,
deal_info.segment_list.clone(),
deal_info.miner_task_list.clone(),
deal_info.complete_list.clone(),
deal_info.user.clone(),
FileState::Calculate,
deal_info.file_size,
Expand Down
21 changes: 8 additions & 13 deletions c-pallets/file-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,6 @@ pub mod pallet {
origin: OriginFor<T>,
file_hash: Hash,
deal_info: BoundedVec<SegmentList<T>, T::SegmentCount>,
assigned_data: BoundedVec<
BoundedVec<Hash, T::MissionCount>,
ConstU32<ASSIGN_MINER_IDEAL_QUANTITY>,
>,
user_brief: UserBrief<T>,
file_size: u128,
) -> DispatchResult {
Expand All @@ -453,8 +449,7 @@ pub mod pallet {
if <File<T>>::contains_key(&file_hash) {
Receptionist::<T>::fly_upload_file(file_hash, user_brief.clone(), needed_space)?;
} else {
ensure!(assigned_data.len() as u32 == ASSIGN_MINER_IDEAL_QUANTITY, Error::<T>::SpecError);
Receptionist::<T>::generate_deal(file_hash, deal_info, user_brief.clone(), assigned_data, needed_space, file_size)?;
Receptionist::<T>::generate_deal(file_hash, deal_info, user_brief.clone(), needed_space, file_size)?;
}

Self::deposit_event(Event::<T>::UploadDeclaration { operator: sender, owner: user_brief.user, deal_hash: file_hash });
Expand Down Expand Up @@ -620,18 +615,18 @@ pub mod pallet {
let _ = ensure_root(origin)?;

let deal_info = <DealMap<T>>::try_get(&deal_hash).map_err(|_| Error::<T>::NonExistent)?;
for miner_task in deal_info.miner_task_list {
let count = miner_task.fragment_list.len() as u32;
let mut hash_list: Vec<Box<[u8; 256]>> = Default::default();
for fragment_hash in miner_task.fragment_list {
for (index, complete_info) in deal_info.complete_list.iter().enumerate() {
let count = FRAGMENT_COUNT;
let mut hash_list: Vec<Box<[u8; 256]>> = Default::default();
for segment in &deal_info.segment_list {
let fragment_hash = segment.fragment_list[index as usize];
let hash_temp = fragment_hash.binary().map_err(|_| Error::<T>::BugInvalid)?;
hash_list.push(hash_temp);
}
// Accumulate the number of fragments stored by each miner
let unlock_space = FRAGMENT_SIZE.checked_mul(count as u128).ok_or(Error::<T>::Overflow)?;
let miner = miner_task.miner.ok_or(Error::<T>::Unexpected)?;
T::MinerControl::unlock_space_to_service(&miner, unlock_space)?;
T::MinerControl::insert_service_bloom(&miner, hash_list)?;
T::MinerControl::unlock_space_to_service(&complete_info.miner, unlock_space)?;
T::MinerControl::insert_service_bloom(&complete_info.miner, hash_list)?;
}

<File<T>>::try_mutate(&deal_hash, |file_opt| -> DispatchResult {
Expand Down
11 changes: 9 additions & 2 deletions c-pallets/file-bank/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ pub struct DealInfo<T: Config> {
pub(super) file_size: u128,
pub(super) segment_list: BoundedVec<SegmentList<T>, T::SegmentCount>,
pub(super) user: UserBrief<T>,
pub(super) miner_task_list: BoundedVec<MinerTaskList<T>, ConstU32<ASSIGN_MINER_IDEAL_QUANTITY>>,
pub(super) complete_list: BoundedVec<u8, T::FragmentCount>,
pub(super) complete_list: BoundedVec<CompleteInfo<T>, T::FragmentCount>,
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct CompleteInfo<T: Config> {
pub(super) index: u8,
pub(super) miner: AccountOf<T>,
}

//TODO! BoundedVec type -> BTreeMap
Expand Down

0 comments on commit 4f52f10

Please sign in to comment.