From e680cea7b91252664033c9b30de0bdfd48be3e9a Mon Sep 17 00:00:00 2001 From: jasl Date: Tue, 25 Apr 2023 21:40:51 +0800 Subject: [PATCH] Sync Phala pallets to 2b6304fc80070bc48adc7993307f884fbefa8603 --- pallets/phala/src/compute/computation.rs | 8 ++++++- pallets/phala/src/mock.rs | 2 ++ pallets/phala/src/registry.rs | 30 +++++++++++++++++++++++- pallets/phala/src/test.rs | 1 + 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/pallets/phala/src/compute/computation.rs b/pallets/phala/src/compute/computation.rs index 70414348..746a2394 100644 --- a/pallets/phala/src/compute/computation.rs +++ b/pallets/phala/src/compute/computation.rs @@ -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] @@ -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::::get(&pubkey).ok_or(Error::::WorkerNotRegistered)?; + ensure!( + registry::Pallet::::is_worker_registered_after_gk_launched(&pubkey), + Error::::WorkerReregisterNeeded + ); // Check the worker has finished the benchmark ensure!(worker.initial_score != None, Error::::BenchmarkMissing); // Check worker and worker not bound diff --git a/pallets/phala/src/mock.rs b/pallets/phala/src/mock.rs index 4ff8514d..a9989e84 100644 --- a/pallets/phala/src/mock.rs +++ b/pallets/phala/src/mock.rs @@ -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 { diff --git a/pallets/phala/src/registry.rs b/pallets/phala/src/registry.rs index 24cc5e5a..9f68a440 100644 --- a/pallets/phala/src/registry.rs +++ b/pallets/phala/src/registry.rs @@ -117,6 +117,10 @@ pub mod pallet { #[pallet::storage] pub type GatekeeperMasterPubkey = StorageValue<_, MasterPublicKey>; + /// The block number and unix timestamp when the gatekeeper is launched + #[pallet::storage] + pub type GatekeeperLaunchedAt = 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] @@ -234,6 +238,7 @@ pub mod pallet { }, MinimumPRuntimeVersionChangedTo(u32, u32, u32), PRuntimeConsensusVersionChangedTo(u32), + GatekeeperLaunched, } #[pallet::error] @@ -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, @@ -959,6 +964,7 @@ pub mod pallet { Self::push_message(GatekeeperLaunch::master_pubkey_on_chain( master_pubkey, )); + Self::on_gatekeeper_launched(); } } } @@ -999,6 +1005,23 @@ pub mod pallet { Ok(()) } + fn on_gatekeeper_launched() { + let block_number = frame_system::Pallet::::block_number(); + let now = T::UnixTime::now().as_secs().saturated_into::(); + GatekeeperLaunchedAt::::put((block_number, now)); + Self::deposit_event(Event::::GatekeeperLaunched); + } + + pub fn is_worker_registered_after_gk_launched(worker: &WorkerPublicKey) -> bool { + let Some(worker) = Workers::::get(worker) else { + return false; + }; + let Some((_, gk_launched_at)) = GatekeeperLaunchedAt::::get() else { + return false; + }; + worker.last_updated > gk_launched_at + } + #[cfg(test)] pub(crate) fn internal_set_benchmark(worker: &WorkerPublicKey, score: Option) { Workers::::mutate(worker, |w| { @@ -1007,6 +1030,11 @@ pub mod pallet { } }); } + + #[cfg(test)] + pub(crate) fn internal_set_gk_launched_at(block: T::BlockNumber, ts: u64) { + GatekeeperLaunchedAt::::put((block, ts)); + } } // Genesis config build diff --git a/pallets/phala/src/test.rs b/pallets/phala/src/test.rs index b26895b6..a6c798a8 100644 --- a/pallets/phala/src/test.rs +++ b/pallets/phala/src/test.rs @@ -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(