Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update miner register #254

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions c-pallets/sminer/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;

pub(super) const STATE_NOT_READY: &str = "not ready";

pub(super) const STATE_POSITIVE: &str = "positive";

pub(super) const STATE_FROZEN: &str = "frozen";
Expand Down
3 changes: 2 additions & 1 deletion c-pallets/sminer/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ impl<T: Config> Pallet<T> {
pub(super) fn withdraw(acc: &AccountOf<T>) -> DispatchResult {
let miner_info = <MinerItems<T>>::try_get(acc).map_err(|_| Error::<T>::NotMiner)?;
T::Currency::unreserve(acc, miner_info.collaterals);
let encoding = miner_info.space_proof_info.pois_key.encode();
let space_proof_info = miner_info.space_proof_info.ok_or(Error::<T>::NotpositiveState)?;
let encoding = space_proof_info.pois_key.encode();
let hashing = sp_io::hashing::sha2_256(&encoding);
MinerPublicKey::<T>::remove(hashing);
<MinerItems<T>>::remove(acc);
Expand Down
27 changes: 18 additions & 9 deletions c-pallets/sminer/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ impl<T: Config> Pallet<T> {
// check state
ensure!(miner_info.state.to_vec() == STATE_POSITIVE.as_bytes().to_vec(), Error::<T>::NotpositiveState);

ensure!(miner_info.space_proof_info.rear < rear, Error::<T>::CountError);
let mut space_proof_info = miner_info.space_proof_info.clone().ok_or(Error::<T>::NotpositiveState)?;

let count = rear.checked_sub(miner_info.space_proof_info.rear).ok_or(Error::<T>::Overflow)?;
ensure!(space_proof_info.rear < rear, Error::<T>::CountError);

let count = rear.checked_sub(space_proof_info.rear).ok_or(Error::<T>::Overflow)?;
let idle_space = IDLE_SEG_SIZE.checked_mul(count as u128).ok_or(Error::<T>::Overflow)?;

miner_info.space_proof_info.rear = rear;
space_proof_info.rear = rear;

miner_info.space_proof_info.accumulator = accumulator;
space_proof_info.accumulator = accumulator;

miner_info.idle_space =
miner_info.idle_space.checked_add(idle_space).ok_or(Error::<T>::Overflow)?;

miner_info.tee_signature = tee_sig;

miner_info.space_proof_info = Some(space_proof_info);

Ok(idle_space)
})
}
Expand All @@ -40,16 +44,20 @@ impl<T: Config> Pallet<T> {
MinerItems::<T>::try_mutate(acc, |miner_info_opt| -> Result<u64, DispatchError> {
let miner_info = miner_info_opt.as_mut().ok_or(Error::<T>::NotMiner)?;

ensure!(miner_info.space_proof_info.front < front, Error::<T>::CountError);
let mut space_proof_info = miner_info.space_proof_info.clone().ok_or(Error::<T>::NotpositiveState)?;

let count = front - miner_info.space_proof_info.front;
ensure!(space_proof_info.front < front, Error::<T>::CountError);

miner_info.space_proof_info.front = front;
let count = front - space_proof_info.front;

miner_info.space_proof_info.accumulator = accumulator;
space_proof_info.front = front;

space_proof_info.accumulator = accumulator;

miner_info.tee_signature = tee_sig;

miner_info.space_proof_info = Some(space_proof_info);

Ok(count)
})
}
Expand Down Expand Up @@ -249,7 +257,8 @@ impl<T: Config> Pallet<T> {
T::StorageHandle::sub_total_idle_space(miner.idle_space)?;
Self::create_restoral_target(acc, miner.service_space)?;
miner.state = Self::str_to_bound(STATE_OFFLINE)?;
let encoding = miner.space_proof_info.pois_key.encode();
let space_proof_info = miner.space_proof_info.clone().ok_or(Error::<T>::NotpositiveState)?;
let encoding = space_proof_info.pois_key.encode();
let hashing = sp_io::hashing::sha2_256(&encoding);
MinerPublicKey::<T>::remove(hashing);
Ok(())
Expand Down
80 changes: 52 additions & 28 deletions c-pallets/sminer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,53 +313,29 @@ pub mod pallet {
beneficiary: AccountOf<T>,
peer_id: PeerId,
staking_val: BalanceOf<T>,
pois_key: PoISKey,
tee_sig: TeeRsaSignature,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(!(<MinerItems<T>>::contains_key(&sender)), Error::<T>::AlreadyRegistered);
ensure!(staking_val >= BASE_LIMIT.try_into().map_err(|_| Error::<T>::Overflow)?, Error::<T>::CollateralNotUp);
T::Currency::reserve(&sender, staking_val)?;

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 original_text = sp_io::hashing::sha2_256(&encoding);
let tee_puk = T::TeeWorkerHandler::get_tee_publickey()?;
ensure!(verify_rsa(&tee_puk, &original_text, &tee_sig), Error::<T>::VerifyTeeSigFailed);

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

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

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

RewardMap::<T>::insert(
&sender,
Reward::<T>{
Expand All @@ -378,6 +354,53 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(16)]
#[transactional]
#[pallet::weight(Weight::zero())]
pub fn register_pois_key(
origin: OriginFor<T>,
pois_key: PoISKey<AccountOf<T>>,
tee_sig: TeeRsaSignature
) -> 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 original_text = sp_io::hashing::sha2_256(&encoding);
let tee_puk = T::TeeWorkerHandler::get_tee_publickey()?;
ensure!(verify_rsa(&tee_puk, &original_text, &tee_sig), 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);
miner_info.state = Self::str_to_bound(STATE_POSITIVE)?;
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(())
})?;

Ok(())
}

/// Increase Collateral and Update Miner's State
///
Expand Down Expand Up @@ -1026,7 +1049,8 @@ impl<T: Config> MinerControl<<T as frame_system::Config>::AccountId, BlockNumber
}
//There is a judgment on whether the primary key exists above
let miner_info = <MinerItems<T>>::try_get(miner).map_err(|_| Error::<T>::NotMiner)?;
Ok((miner_info.idle_space, miner_info.service_space, miner_info.service_bloom_filter, miner_info.space_proof_info, miner_info.tee_signature))
let space_proof_info = miner_info.space_proof_info.ok_or(Error::<T>::Unexpected)?;
Ok((miner_info.idle_space, miner_info.service_space, miner_info.service_bloom_filter, space_proof_info, miner_info.tee_signature))
}

fn update_restoral_target(miner: &AccountOf<T>, service_space: u128) -> DispatchResult {
Expand Down
2 changes: 1 addition & 1 deletion c-pallets/sminer/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct MinerInfo<T: Config> {
pub(super) idle_space: u128,
pub(super) service_space: u128,
pub(super) lock_space: u128,
pub(super) space_proof_info: SpaceProofInfo<AccountOf<T>>,
pub(super) space_proof_info: Option<SpaceProofInfo<AccountOf<T>>>,
pub(super) service_bloom_filter: BloomFilter,
pub(super) tee_signature: TeeRsaSignature,
}
Expand Down
5 changes: 3 additions & 2 deletions primitives/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub struct Hash(pub [u8; 64]);
pub struct TryFromSliceError(());

#[derive(PartialEq, Eq, Encode, Decode, Clone, RuntimeDebug, MaxEncodedLen, TypeInfo)]
pub struct PoISKey {
pub struct PoISKey<AccountId> {
pub acc: AccountId,
pub g: [u8; 256],
pub n: [u8; 256],
}
Expand All @@ -28,7 +29,7 @@ pub struct SpaceProofInfo<AccountId> {
pub miner: AccountId,
pub front: u64,
pub rear: u64,
pub pois_key: PoISKey,
pub pois_key: PoISKey<AccountId>,
pub accumulator: Accumulator,
}

Expand Down
Loading