Skip to content

Commit

Permalink
fix: fix calculate report bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ytqaljn committed Jan 3, 2024
1 parent ebf8a4b commit 3ac4aee
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 153 deletions.
26 changes: 21 additions & 5 deletions pallets/file-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,11 +576,13 @@ pub mod pallet {
for segment in file_info.segment_list.iter_mut() {
for fragment in segment.fragment_list.iter_mut() {
if fragment.miner == sender {
fragment.tag = Some(now);
count = count + 1;
let hash_temp = fragment.hash.binary().map_err(|_| Error::<T>::BugInvalid)?;
hash_list.push(hash_temp);
flag = true;
if fragment.tag.is_none() {
fragment.tag = Some(now);
count = count + 1;
let hash_temp = fragment.hash.binary().map_err(|_| Error::<T>::BugInvalid)?;
hash_list.push(hash_temp);
flag = true;
}
}
}
}
Expand Down Expand Up @@ -1086,6 +1088,20 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(22)]
#[transactional]
#[pallet::weight(Weight::zero())]
pub fn root_clear_file(origin: OriginFor<T>, owner: AccountOf<T>, file_hash: Hash) -> DispatchResult {
let sender = ensure_root(origin)?;

let file = <File<T>>::try_get(&file_hash).map_err(|_| Error::<T>::NonExistent)?;
Self::bucket_remove_file(&file_hash, &owner, &file)?;
Self::remove_user_hold_file_list(&file_hash, &owner)?;
<File<T>>::remove(file_hash);

Ok(())
}
}
}

Expand Down
312 changes: 165 additions & 147 deletions pallets/sminer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,153 +392,6 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(16)]
#[transactional]
#[pallet::weight(Weight::zero())]
pub fn register_pois_key(
origin: OriginFor<T>,
pois_key: PoISKey,
tee_sig_need_verify: TeeRsaSignature,
tee_sig: TeeRsaSignature,
tee_acc: AccountOf<T>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
// Because the next operation consumes system resources, make a judgment in advance.
ensure!(<MinerItems<T>>::contains_key(&sender), Error::<T>::NotMiner);

let space_proof_info = SpaceProofInfo::<AccountOf<T>> {
miner: sender.clone(),
front: u64::MIN,
rear: u64::MIN,
pois_key: pois_key.clone(),
accumulator: pois_key.g,
};

let encoding = space_proof_info.encode();
let tee_acc_encode = tee_acc.encode();
let mut original = Vec::new();
original.extend_from_slice(&encoding);
original.extend_from_slice(&tee_acc_encode);
let original_text = sp_io::hashing::sha2_256(&original);
let tee_puk = T::TeeWorkerHandler::get_tee_publickey()?;
ensure!(verify_rsa(&tee_puk, &original_text, &tee_sig_need_verify), Error::<T>::VerifyTeeSigFailed);

MinerPublicKey::<T>::insert(&original_text, sender.clone());

<MinerItems<T>>::try_mutate(&sender, |info_opt| -> DispatchResult {
let miner_info = info_opt.as_mut().ok_or(Error::<T>::NotMiner)?;
ensure!(STATE_NOT_READY.as_bytes().to_vec() == miner_info.state.to_vec(), Error::<T>::StateError);

miner_info.space_proof_info = Some(space_proof_info);
let base_limit: BalanceOf<T> = Self::calculate_limit_by_space(miner_info.declaration_space)?
.try_into().map_err(|_| Error::<T>::Overflow)?;
if miner_info.collaterals >= base_limit {
miner_info.state = Self::str_to_bound(STATE_POSITIVE)?;
} else {
miner_info.state = Self::str_to_bound(STATE_FROZEN)?;
}
miner_info.tee_signature = tee_sig;

Ok(())
})?;

AllMiner::<T>::try_mutate(|all_miner| -> DispatchResult {
all_miner
.try_push(sender.clone())
.map_err(|_e| Error::<T>::StorageLimitReached)?;
Ok(())
})?;

Self::deposit_event(Event::<T>::RegisterPoisKey {
miner: sender,
});

Ok(())
}

#[pallet::call_index(17)]
#[transactional]
#[pallet::weight(<T as pallet::Config>::WeightInfo::regnstk())]
pub fn regnstk_assign_staking(
origin: OriginFor<T>,
beneficiary: AccountOf<T>,
peer_id: PeerId,
staking_account: AccountOf<T>,
tib_count: u32,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(!(<MinerItems<T>>::contains_key(&sender)), Error::<T>::AlreadyRegistered);
let declaration_space = T_BYTE.checked_mul(tib_count as u128).ok_or(Error::<T>::Overflow)?;

<MinerItems<T>>::insert(
&sender,
MinerInfo::<T> {
beneficiary: beneficiary.clone(),
staking_account: staking_account.clone(),
peer_id: peer_id,
collaterals: BalanceOf::<T>::zero(),
debt: BalanceOf::<T>::zero(),
state: Self::str_to_bound(STATE_NOT_READY)?,
declaration_space: declaration_space,
idle_space: u128::MIN,
service_space: u128::MIN,
lock_space: u128::MIN,
space_proof_info: Option::None,
service_bloom_filter: Default::default(),
tee_signature: [0u8; 256],
},
);

RewardMap::<T>::insert(
&sender,
Reward::<T>{
total_reward: 0u32.saturated_into(),
reward_issued: 0u32.saturated_into(),
currently_available_reward: 0u32.saturated_into(),
order_list: Default::default()
},
);

Self::deposit_event(Event::<T>::Registered {
acc: sender,
});

Ok(())
}

#[pallet::call_index(18)]
#[transactional]
#[pallet::weight(<T as pallet::Config>::WeightInfo::increase_collateral())]
pub fn increase_declaration_space(origin: OriginFor<T>, tib_count: u32) -> DispatchResult {
let sender = ensure_signed(origin)?;

ensure!(MinerItems::<T>::contains_key(&sender), Error::<T>::NotMiner);
let increase_space = T_BYTE.checked_mul(tib_count as u128).ok_or(Error::<T>::Overflow)?;

<MinerItems<T>>::try_mutate(&sender, |miner_info_opt| -> DispatchResult {
let miner_info = miner_info_opt.as_mut().ok_or(Error::<T>::ConversionError)?;

ensure!(miner_info.state.to_vec() == STATE_POSITIVE.as_bytes().to_vec(), Error::<T>::StateError);
miner_info.declaration_space = miner_info.declaration_space
.checked_add(increase_space).ok_or(Error::<T>::Overflow)?;
let base_limit: BalanceOf<T> = Self::calculate_limit_by_space(miner_info.declaration_space)?
.try_into().map_err(|_| Error::<T>::Overflow)?;
if base_limit > miner_info.collaterals {
miner_info.state = Self::str_to_bound(STATE_FROZEN)?;
}

Ok(())
})?;

Self::deposit_event(Event::<T>::IncreaseDeclarationSpace {
miner: sender,
space: increase_space,
});

Ok(())
}

/// Increase Collateral and Update Miner's State
///
/// This function allows a registered Miner to increase their staked collateral.
Expand Down Expand Up @@ -955,6 +808,171 @@ pub mod pallet {

Ok(())
}


#[pallet::call_index(16)]
#[transactional]
#[pallet::weight(Weight::zero())]
pub fn register_pois_key(
origin: OriginFor<T>,
pois_key: PoISKey,
tee_sig_need_verify: TeeRsaSignature,
tee_sig: TeeRsaSignature,
tee_acc: AccountOf<T>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
// Because the next operation consumes system resources, make a judgment in advance.
ensure!(<MinerItems<T>>::contains_key(&sender), Error::<T>::NotMiner);

let space_proof_info = SpaceProofInfo::<AccountOf<T>> {
miner: sender.clone(),
front: u64::MIN,
rear: u64::MIN,
pois_key: pois_key.clone(),
accumulator: pois_key.g,
};

let encoding = space_proof_info.encode();
let tee_acc_encode = tee_acc.encode();
let mut original = Vec::new();
original.extend_from_slice(&encoding);
original.extend_from_slice(&tee_acc_encode);
let original_text = sp_io::hashing::sha2_256(&original);
let tee_puk = T::TeeWorkerHandler::get_tee_publickey()?;
ensure!(verify_rsa(&tee_puk, &original_text, &tee_sig_need_verify), Error::<T>::VerifyTeeSigFailed);

MinerPublicKey::<T>::insert(&original_text, sender.clone());

<MinerItems<T>>::try_mutate(&sender, |info_opt| -> DispatchResult {
let miner_info = info_opt.as_mut().ok_or(Error::<T>::NotMiner)?;
ensure!(STATE_NOT_READY.as_bytes().to_vec() == miner_info.state.to_vec(), Error::<T>::StateError);

miner_info.space_proof_info = Some(space_proof_info);
let base_limit: BalanceOf<T> = Self::calculate_limit_by_space(miner_info.declaration_space)?
.try_into().map_err(|_| Error::<T>::Overflow)?;
if miner_info.collaterals >= base_limit {
miner_info.state = Self::str_to_bound(STATE_POSITIVE)?;
} else {
miner_info.state = Self::str_to_bound(STATE_FROZEN)?;
}
miner_info.tee_signature = tee_sig;

Ok(())
})?;

AllMiner::<T>::try_mutate(|all_miner| -> DispatchResult {
all_miner
.try_push(sender.clone())
.map_err(|_e| Error::<T>::StorageLimitReached)?;
Ok(())
})?;

Self::deposit_event(Event::<T>::RegisterPoisKey {
miner: sender,
});

Ok(())
}

#[pallet::call_index(17)]
#[transactional]
#[pallet::weight(<T as pallet::Config>::WeightInfo::regnstk())]
pub fn regnstk_assign_staking(
origin: OriginFor<T>,
beneficiary: AccountOf<T>,
peer_id: PeerId,
staking_account: AccountOf<T>,
tib_count: u32,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(!(<MinerItems<T>>::contains_key(&sender)), Error::<T>::AlreadyRegistered);
let declaration_space = T_BYTE.checked_mul(tib_count as u128).ok_or(Error::<T>::Overflow)?;

<MinerItems<T>>::insert(
&sender,
MinerInfo::<T> {
beneficiary: beneficiary.clone(),
staking_account: staking_account.clone(),
peer_id: peer_id,
collaterals: BalanceOf::<T>::zero(),
debt: BalanceOf::<T>::zero(),
state: Self::str_to_bound(STATE_NOT_READY)?,
declaration_space: declaration_space,
idle_space: u128::MIN,
service_space: u128::MIN,
lock_space: u128::MIN,
space_proof_info: Option::None,
service_bloom_filter: Default::default(),
tee_signature: [0u8; 256],
},
);

RewardMap::<T>::insert(
&sender,
Reward::<T>{
total_reward: 0u32.saturated_into(),
reward_issued: 0u32.saturated_into(),
currently_available_reward: 0u32.saturated_into(),
order_list: Default::default()
},
);

Self::deposit_event(Event::<T>::Registered {
acc: sender,
});

Ok(())
}

#[pallet::call_index(18)]
#[transactional]
#[pallet::weight(<T as pallet::Config>::WeightInfo::increase_collateral())]
pub fn increase_declaration_space(origin: OriginFor<T>, tib_count: u32) -> DispatchResult {
let sender = ensure_signed(origin)?;

ensure!(MinerItems::<T>::contains_key(&sender), Error::<T>::NotMiner);
let increase_space = T_BYTE.checked_mul(tib_count as u128).ok_or(Error::<T>::Overflow)?;

<MinerItems<T>>::try_mutate(&sender, |miner_info_opt| -> DispatchResult {
let miner_info = miner_info_opt.as_mut().ok_or(Error::<T>::ConversionError)?;

ensure!(miner_info.state.to_vec() == STATE_POSITIVE.as_bytes().to_vec(), Error::<T>::StateError);
miner_info.declaration_space = miner_info.declaration_space
.checked_add(increase_space).ok_or(Error::<T>::Overflow)?;
let base_limit: BalanceOf<T> = Self::calculate_limit_by_space(miner_info.declaration_space)?
.try_into().map_err(|_| Error::<T>::Overflow)?;
if base_limit > miner_info.collaterals {
miner_info.state = Self::str_to_bound(STATE_FROZEN)?;
}

Ok(())
})?;

Self::deposit_event(Event::<T>::IncreaseDeclarationSpace {
miner: sender,
space: increase_space,
});

Ok(())
}

// FOR TESTING
#[pallet::call_index(19)]
#[transactional]
#[pallet::weight(0)]
pub fn clear_miner_service(origin: OriginFor<T>, miner: AccountOf<T>) -> DispatchResult {
let _ = ensure_root(origin)?;

<MinerItems<T>>::try_mutate(&miner, |miner_info_opt| -> DispatchResult {
let miner_info = miner_info_opt.as_mut().ok_or(Error::<T>::ConversionError)?;
miner_info.service_space = 0;
miner_info.service_bloom_filter = Default::default();

Ok(())
})?;

Ok(())
}
}
}

Expand Down
Loading

0 comments on commit 3ac4aee

Please sign in to comment.