Skip to content

Commit

Permalink
Sync Phala pallets to 2b6304fc80070bc48adc7993307f884fbefa8603
Browse files Browse the repository at this point in the history
  • Loading branch information
jasl committed Apr 25, 2023
1 parent 9362959 commit e680cea
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
8 changes: 7 additions & 1 deletion pallets/phala/src/compute/computation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ pub mod pallet {
NonceIndexInvalid,
BudgetUpdateBlockInvalid,
BudgetExceedMaxLimit,
/// The worker need to be re-registered to be added to a pool.
WorkerReregisterNeeded,
}

#[pallet::call]
Expand Down Expand Up @@ -862,13 +864,17 @@ pub mod pallet {
/// and the worker is unbound.
///
/// Requires:
/// 1. The worker is already registered
/// 1. The worker is already (re)registered after the gatekeeper launched
/// 2. The worker has an initial benchmark
/// 3. Both the worker and the worker are not bound
/// 4. There's no stake in CD associated with the worker
pub fn bind(session: T::AccountId, pubkey: WorkerPublicKey) -> DispatchResult {
let worker =
registry::Workers::<T>::get(&pubkey).ok_or(Error::<T>::WorkerNotRegistered)?;
ensure!(
registry::Pallet::<T>::is_worker_registered_after_gk_launched(&pubkey),
Error::<T>::WorkerReregisterNeeded
);
// Check the worker has finished the benchmark
ensure!(worker.initial_score != None, Error::<T>::BenchmarkMissing);
// Check worker and worker not bound
Expand Down
2 changes: 2 additions & 0 deletions pallets/phala/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ pub fn new_test_ext() -> sp_io::TestExternalities {

pub fn set_block_1() {
System::set_block_number(1);
Timestamp::set_timestamp(1);
PhalaRegistry::internal_set_gk_launched_at(0, 0);
}

pub fn take_events() -> Vec<RuntimeEvent> {
Expand Down
30 changes: 29 additions & 1 deletion pallets/phala/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub mod pallet {
#[pallet::storage]
pub type GatekeeperMasterPubkey<T: Config> = StorageValue<_, MasterPublicKey>;

/// The block number and unix timestamp when the gatekeeper is launched
#[pallet::storage]
pub type GatekeeperLaunchedAt<T: Config> = StorageValue<_, (T::BlockNumber, u64)>;

/// The rotation counter starting from 1, it always equals to the latest rotation id.
/// The totation id 0 is reserved for the first master key before we introduce the rotation.
#[pallet::storage]
Expand Down Expand Up @@ -234,6 +238,7 @@ pub mod pallet {
},
MinimumPRuntimeVersionChangedTo(u32, u32, u32),
PRuntimeConsensusVersionChangedTo(u32),
GatekeeperLaunched,
}

#[pallet::error]
Expand Down Expand Up @@ -318,7 +323,7 @@ pub mod pallet {
pubkey,
ecdh_pubkey,
runtime_version: 0,
last_updated: 0,
last_updated: 1,
operator,
attestation_provider: Some(AttestationProvider::Root),
confidence_level: 128u8,
Expand Down Expand Up @@ -959,6 +964,7 @@ pub mod pallet {
Self::push_message(GatekeeperLaunch::master_pubkey_on_chain(
master_pubkey,
));
Self::on_gatekeeper_launched();
}
}
}
Expand Down Expand Up @@ -999,6 +1005,23 @@ pub mod pallet {
Ok(())
}

fn on_gatekeeper_launched() {
let block_number = frame_system::Pallet::<T>::block_number();
let now = T::UnixTime::now().as_secs().saturated_into::<u64>();
GatekeeperLaunchedAt::<T>::put((block_number, now));
Self::deposit_event(Event::<T>::GatekeeperLaunched);
}

pub fn is_worker_registered_after_gk_launched(worker: &WorkerPublicKey) -> bool {
let Some(worker) = Workers::<T>::get(worker) else {
return false;
};
let Some((_, gk_launched_at)) = GatekeeperLaunchedAt::<T>::get() else {
return false;
};
worker.last_updated > gk_launched_at
}

#[cfg(test)]
pub(crate) fn internal_set_benchmark(worker: &WorkerPublicKey, score: Option<u32>) {
Workers::<T>::mutate(worker, |w| {
Expand All @@ -1007,6 +1030,11 @@ pub mod pallet {
}
});
}

#[cfg(test)]
pub(crate) fn internal_set_gk_launched_at(block: T::BlockNumber, ts: u64) {
GatekeeperLaunchedAt::<T>::put((block, ts));
}
}

// Genesis config build
Expand Down
1 change: 1 addition & 0 deletions pallets/phala/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,7 @@ fn restart_computing_should_work() {
RuntimeOrigin::signed(2),
500 * DOLLARS
));
set_block_1();
setup_workers(1);
setup_stake_pool_with_workers(1, &[1]); // pid=0
assert_ok!(PhalaStakePoolv2::contribute(
Expand Down

0 comments on commit e680cea

Please sign in to comment.