Skip to content

Commit

Permalink
0.6.0 patch (#198)
Browse files Browse the repository at this point in the history
* fix: generate challenge

* fix: challenge select miner.

* fix: temporary adjustment of reallocation time interval and frequency limit

* fix: temporary adjustment of reallocation time interval and frequency limit

* fix: receive reward to controller

* feat: increase the maximum number of reassignments

* feat: whitelist deduplication

* feat: benchmark fixing

* Revert "fix: receive reward to controller"

This reverts commit 2fc7db0.

* fix: challenge index bounded

* feat: reassign miner

* style: delete extra code

* fix: upload file reassign miner

* fix: upload reassign lock_space

* feat: principle of optimizing random allocation

* fix: failed count  >= 5

* fix: overflow

* fix: repair potential hazards

* fix: bug where file reassignment failed, resulting in inability to delete orders

* feat: add rust-toolchain.toml. lock toolchain

* testing (#199)

* refactor: edition “2018” to edition "2021"

---------

Co-authored-by: Jimmy Chu <jimmychu0807@gmail.com>
  • Loading branch information
ytqaljn and jimmychu0807 committed Aug 15, 2023
1 parent 79c2413 commit 2bad941
Show file tree
Hide file tree
Showing 15 changed files with 243 additions and 86 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ members = [
'primitives/*'
]

resolver = "2"

# The list of dependencies below (which can be both direct and indirect dependencies) are crates
# that are suspected to be CPU-intensive, and that are unlikely to require debugging (as some of
# their debug info might be missing) or to require to be frequently recompiled. We compile these
Expand Down
2 changes: 1 addition & 1 deletion c-pallets/audit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "pallet-audit"
authors = ["CESS LAB"]
version = "0.6.0"
edition = "2018"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/CESSProject/cess"
description = "FRAME pallet for segment management"
Expand Down
70 changes: 47 additions & 23 deletions c-pallets/audit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,21 @@ pub mod pallet {

fn offchain_worker(now: T::BlockNumber) {
let deadline = Self::verify_duration();
let lock = <Lock<T>>::get();
if sp_io::offchain::is_validator() {
if now > deadline {
//Determine whether to trigger a challenge
if Self::trigger_challenge(now) {
log::info!("offchain worker random challenge start");
if let Err(e) = Self::offchain_work_start(now) {
match e {
OffchainErr::Working => log::info!("offchain working, Unable to perform a new round of work."),
_ => log::info!("offchain worker generation challenge failed:{:?}", e),
};
if lock {
if now > deadline {
//Determine whether to trigger a challenge
if Self::trigger_challenge(now) {
log::info!("offchain worker random challenge start");
if let Err(e) = Self::offchain_work_start(now) {
match e {
OffchainErr::Working => log::info!("offchain working, Unable to perform a new round of work."),
_ => log::info!("offchain worker generation challenge failed:{:?}", e),
};
}
log::info!("offchain worker random challenge end");
}
log::info!("offchain worker random challenge end");
}
}
}
Expand Down Expand Up @@ -395,6 +398,7 @@ pub mod pallet {
.checked_add(&proposal.1.net_snap_shot.life).ok_or(Error::<T>::Overflow)?
.checked_add(&one_hour).ok_or(Error::<T>::Overflow)?;
<VerifyDuration<T>>::put(v_duration);
<ChallengeSnapShot<T>>::put(proposal.1);
let _ = ChallengeProposal::<T>::clear(ChallengeProposal::<T>::count(), None);
}

Expand Down Expand Up @@ -533,6 +537,20 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(3)]
#[transactional]
#[pallet::weight(100_000_000)]
pub fn lock_(origin: OriginFor<T>) -> DispatchResult {
let _ = ensure_root(origin)?;

let lock = <Lock<T>>::get();

<Lock<T>>::put(!lock);

Ok(())
}

}

#[pallet::validate_unsigned]
Expand Down Expand Up @@ -844,13 +862,16 @@ pub mod pallet {
fn generation_challenge(now: BlockNumberOf<T>)
-> Result<ChallengeInfo<T>, OffchainErr>
{
let miner_count = T::MinerControl::get_miner_count();

// let miner_count = T::MinerControl::get_miner_count();
let allminer = T::MinerControl::get_all_miner().map_err(|_| OffchainErr::GenerateInfoError)?;
let miner_count = allminer.len() as u32;
if miner_count == 0 {
Err(OffchainErr::GenerateInfoError)?;
}

let need_miner_count = miner_count / 10 + 1;
// TEMP
let need_miner_count = miner_count;
// let need_miner_count = miner_count / 10 + 1;

let mut miner_list: BoundedVec<MinerSnapShot<AccountOf<T>>, T::ChallengeMinerMax> = Default::default();

Expand All @@ -859,22 +880,21 @@ pub mod pallet {
let mut total_idle_space: u128 = u128::MIN;
let mut total_service_space: u128 = u128::MIN;
let mut max_space: u128 = 0;

// TODO: need to set a maximum number of cycles
let mut seed: u32 = 20230601;
while (miner_list.len() as u32 != need_miner_count) && (valid_index_list.len() as u32 != miner_count) {
while ((miner_list.len() as u32) < need_miner_count) && (valid_index_list.len() as u32 != miner_count) {
seed = seed.saturating_add(1);
let index_list = Self::random_select_miner(need_miner_count, miner_count, &valid_index_list, seed);

let allminer = T::MinerControl::get_all_miner().map_err(|_| OffchainErr::GenerateInfoError)?;

for index in index_list {
valid_index_list.push(index);
let miner = allminer[index as usize].clone();
let state = T::MinerControl::get_miner_state(&miner).map_err(|_| OffchainErr::GenerateInfoError)?;
if state == "lock".as_bytes().to_vec() {
if state == "lock".as_bytes().to_vec() || state == "offline".as_bytes().to_vec() || state == "exit".as_bytes().to_vec() {
continue;
}

let (idle_space, service_space) = T::MinerControl::get_power(&miner).map_err(|_| OffchainErr::GenerateInfoError)?;

if (idle_space == 0) && (service_space == 0) {
Expand All @@ -885,18 +905,22 @@ pub mod pallet {
if miner_total_space > max_space {
max_space = miner_total_space;
}

total_idle_space = total_idle_space.checked_add(idle_space).ok_or(OffchainErr::Overflow)?;
total_service_space = total_service_space.checked_add(service_space).ok_or(OffchainErr::Overflow)?;
let miner_snapshot = MinerSnapShot::<AccountOf<T>> {
miner,
idle_space,
service_space,
};
let result = miner_list.try_push(miner_snapshot);
if let Err(_e) = result {

if let Err(_e) = miner_list.try_push(miner_snapshot) {
return Err(OffchainErr::GenerateInfoError)?;
};

if (miner_list.len() as u32) >= need_miner_count {
break;
}
}
}

Expand All @@ -923,7 +947,7 @@ pub mod pallet {

let life: BlockNumberOf<T> = ((max_space / 8_947_849 + 12) as u32).saturated_into();

let total_reward: u128 = T::MinerControl::get_reward();
let total_reward: u128 = T::MinerControl::get_reward() / 6;
let snap_shot = NetSnapShot::<BlockNumberOf<T>>{
start: now,
life: life,
Expand All @@ -940,7 +964,7 @@ pub mod pallet {
// Ensure that the length is not 0
fn random_select_miner(need: u32, length: u32, valid_index_list: &Vec<u32>, seed: u32) -> Vec<u32> {
let mut miner_index_list: Vec<u32> = Default::default();
let mut seed: u32 = seed.saturating_mul(1000);
let mut seed: u32 = seed.saturating_mul(5000);
while (miner_index_list.len() as u32) < need && ((valid_index_list.len() + miner_index_list.len()) as u32 != length) {
seed += 1;
let index = Self::random_number(seed);
Expand Down
2 changes: 1 addition & 1 deletion c-pallets/file-bank/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "pallet-file-bank"
authors = ["CESS LAB"]
version = "0.6.0"
edition = "2018"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/CESSProject/cess"
description = "FRAME pallet for file-bank management"
Expand Down
2 changes: 1 addition & 1 deletion c-pallets/file-bank/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// The average number of bytes that a storage node can transmit within each block
pub(super) const TRANSFER_RATE: u128 = 8_947_849;
pub(super) const TRANSFER_RATE: u128 = 4_089_446;

pub(super) const CALCULATE_RATE: u128 = 67_108_864;
105 changes: 103 additions & 2 deletions c-pallets/file-bank/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ impl<T: Config> Pallet<T> {
pub(super) fn start_first_task(task_id: Vec<u8>, deal_hash: Hash, count: u8, life: u32) -> DispatchResult {
let start: u32 = <frame_system::Pallet<T>>::block_number().saturated_into();
let survival_block = start
.checked_add(50 * (count as u32)).ok_or(Error::<T>::Overflow)?
// temp
// .checked_add(50 * (count as u32)).ok_or(Error::<T>::Overflow)?
.checked_add(50).ok_or(Error::<T>::Overflow)?
.checked_add(life).ok_or(Error::<T>::Overflow)?;

T::FScheduler::schedule_named(
Expand Down Expand Up @@ -198,6 +200,80 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub(super) fn remove_deal(deal_hash: &Hash) -> DispatchResult {
let deal_info = <DealMap<T>>::try_get(deal_hash).map_err(|_| Error::<T>::NonExistent)?;
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.assigned_miner {
let count = miner_task.fragment_list.len() as u128;
T::MinerControl::unlock_space(&miner_task.miner, FRAGMENT_SIZE * count)?;
}

<DealMap<T>>::remove(deal_hash);

Ok(())
}

pub(super) fn reassign_miner(
task_list: BoundedVec<MinerTaskList<T>, T::StringLimit>,
selected_miner: BoundedVec<AccountOf<T>, T::StringLimit>,
) -> Result<BoundedVec<MinerTaskList<T>, T::StringLimit>, DispatchError> {
let mut all_miner = T::MinerControl::get_all_miner()?;
let mut total = all_miner.len() as u32;
let mut seed = <frame_system::Pallet<T>>::block_number().saturated_into();
// Used to calculate the upper limit of the number of random selections
let random_count_limit = (task_list.len() - selected_miner.len()) as u32 * 3 + 10;
let mut new_task_list: BoundedVec<MinerTaskList<T>, T::StringLimit> = Default::default();
for miner_task in &task_list {
// Used to count the number of random selections.
let mut cur_count = 0;
let miner = loop {
if random_count_limit == cur_count {
Err(Error::<T>::NotQualified)?;
}

if total == 0 {
Err(Error::<T>::NotQualified)?;
}

let index = Self::generate_random_number(seed)? as u32 % total;
seed = seed.checked_add(1).ok_or(Error::<T>::Overflow)?;

let miner = all_miner[index as usize].clone();
all_miner.remove(index as usize);
cur_count += 1;
total = total - 1;

if selected_miner.contains(&miner) {
continue;
}

if Self::miner_failed_exceeds_limit(&miner) {
continue;
}

let result = T::MinerControl::is_positive(&miner)?;
if !result {
continue;
}

let (idle, _) = T::MinerControl::get_power(&miner)?;
let needed_size = miner_task.fragment_list.len() as u128 * FRAGMENT_SIZE;
if idle < needed_size {
continue;
}
T::MinerControl::lock_space(&miner, miner_task.fragment_list.len() as u128 * FRAGMENT_SIZE)?;

break miner;
};

new_task_list.try_push(MinerTaskList::<T>{miner: miner, fragment_list: miner_task.fragment_list.clone()}).map_err(|_| Error::<T>::BugInvalid)?;
}

return Ok(new_task_list)
}

pub(super) fn random_assign_miner(
needed_list: &BoundedVec<SegmentList<T>, T::SegmentCount>
) -> Result<BoundedVec<MinerTaskList<T>, T::StringLimit>, DispatchError> {
Expand Down Expand Up @@ -239,11 +315,16 @@ impl<T: Config> Pallet<T> {
let miner = all_miner[index as usize].clone();
all_miner.remove(index as usize);
total = total - 1;

if Self::miner_failed_exceeds_limit(&miner) {
continue;
}

let result = T::MinerControl::is_positive(&miner)?;
if !result {
continue;
}

let cur_space: u128 = T::MinerControl::get_miner_idle_space(&miner)?;
// If sufficient, the miner is selected.
if cur_space > needed_list.len() as u128 * FRAGMENT_SIZE {
Expand Down Expand Up @@ -608,4 +689,24 @@ impl<T: Config> Pallet<T> {

true
}

pub(super) fn add_task_failed_count(miner: &AccountOf<T>) -> DispatchResult {
TaskFailedCount::<T>::try_mutate(miner, |count| -> DispatchResult {
*count = count.checked_add(1).unwrap_or(255);
Ok(())
})
}

pub(super) fn zero_task_failed_count(miner: &AccountOf<T>) -> DispatchResult {
TaskFailedCount::<T>::try_mutate(miner, |count| -> DispatchResult {
*count = 0;
Ok(())
})
}

pub(super) fn miner_failed_exceeds_limit(miner: &AccountOf<T>) -> bool {
let count = TaskFailedCount::<T>::get(miner);

count >= 5
}
}
Loading

0 comments on commit 2bad941

Please sign in to comment.