From fd7d7726c964d0f804486bc4db9b9525d27bb5bc Mon Sep 17 00:00:00 2001 From: Highstake Date: Tue, 21 Nov 2023 21:43:30 +0000 Subject: [PATCH 1/6] Merge upstream --- Cargo.lock | 18 +++++++++--------- standalone/prb/src/tx.rs | 18 ++++++++++++++++++ standalone/prb/src/worker.rs | 23 +++++++++++++++++++++++ 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99e815d127..8b720aaf52 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2831,12 +2831,12 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ - "darling_core 0.20.1", - "darling_macro 0.20.1", + "darling_core 0.20.3", + "darling_macro 0.20.3", ] [[package]] @@ -2868,9 +2868,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -2904,9 +2904,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core 0.20.1", "quote 1.0.33", @@ -15333,7 +15333,7 @@ dependencies = [ name = "subxt-macro" version = "0.29.0" dependencies = [ - "darling 0.20.1", + "darling 0.20.3", "proc-macro-error", "subxt-codegen", "syn 2.0.38", diff --git a/standalone/prb/src/tx.rs b/standalone/prb/src/tx.rs index 86251e5c2b..76df18b6c4 100644 --- a/standalone/prb/src/tx.rs +++ b/standalone/prb/src/tx.rs @@ -636,6 +636,24 @@ impl TxManager { ); self.clone().send_to_queue(pid, tx_payload, desc).await } + pub async fn restart_computing( + self: Arc, + pid: u64, + worker: Sr25519Public, + stake: String, + ) -> Result<()> { + let desc = format!( + "Restart computing for 0x{} with stake of {} in pool #{pid}.", + worker.encode_hex::(), + &stake + ); + let tx_payload = EncodedPayload::new( + "PhalaStakePoolv2", + "restart_computing", + (pid, Encoded(worker.encode()), stake.parse::()?).encode(), + ); + self.clone().send_to_queue(pid, tx_payload, desc).await + } pub async fn stop_computing(self: Arc, pid: u64, worker: Sr25519Public) -> Result<()> { let desc = format!( "Stop computing for 0x{} in pool #{pid}.", diff --git a/standalone/prb/src/worker.rs b/standalone/prb/src/worker.rs index 6b66dba474..7762e1e3a9 100644 --- a/standalone/prb/src/worker.rs +++ b/standalone/prb/src/worker.rs @@ -510,6 +510,7 @@ impl WorkerContext { txm.clone().add_worker(pid, pubkey).await?; } + let mut first_run = true; set_worker_message!(c, "Waiting for session info to update..."); loop { let cc = c.clone(); @@ -530,6 +531,28 @@ impl WorkerContext { set_worker_message!(c, "Worker is cooling down!"); } _ => { + if first_run { + // Check on-chain stake vs. configured stake and update if different + let stake_query = storage( + "PhalaComputation", + "Stakes", + vec![Value::from_bytes(worker_binding.as_ref().unwrap())], + ); + let stake_onchain: Option = fetch_storage_bytes(&api, &stake_query).await?; + if stake_onchain.is_some() { + info!("Stake: {:?}, {:?} on-chain", &worker.stake.parse::().unwrap(), &stake_onchain.unwrap()); + if &stake_onchain.unwrap() < &worker.stake.parse::().unwrap() { + set_worker_message!(c, "Adjusting on-chain stake..."); + txm.clone() + .restart_computing(pid, pubkey, worker.stake) + .await?; + Self::set_state(c.clone(), WorkerLifecycleState::Working).await; + } else if &stake_onchain.unwrap() > &worker.stake.parse::().unwrap() { + set_worker_message!(c, "Error on-chain stake higher, than configured..."); + } + } + first_run = false; + } Self::set_state(c.clone(), WorkerLifecycleState::Working).await; } } From 8703a80f67630cb0bc1277aaa96e90011b58ab4f Mon Sep 17 00:00:00 2001 From: Philipp D Date: Wed, 29 Nov 2023 13:32:59 +0100 Subject: [PATCH 2/6] Update worker.rs Make clippy happy --- standalone/prb/src/worker.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/standalone/prb/src/worker.rs b/standalone/prb/src/worker.rs index 7762e1e3a9..14d0d35daf 100644 --- a/standalone/prb/src/worker.rs +++ b/standalone/prb/src/worker.rs @@ -541,13 +541,13 @@ impl WorkerContext { let stake_onchain: Option = fetch_storage_bytes(&api, &stake_query).await?; if stake_onchain.is_some() { info!("Stake: {:?}, {:?} on-chain", &worker.stake.parse::().unwrap(), &stake_onchain.unwrap()); - if &stake_onchain.unwrap() < &worker.stake.parse::().unwrap() { + if stake_onchain.unwrap() < worker.stake.parse::().unwrap() { set_worker_message!(c, "Adjusting on-chain stake..."); txm.clone() .restart_computing(pid, pubkey, worker.stake) .await?; Self::set_state(c.clone(), WorkerLifecycleState::Working).await; - } else if &stake_onchain.unwrap() > &worker.stake.parse::().unwrap() { + } else if stake_onchain.unwrap() > worker.stake.parse::().unwrap() { set_worker_message!(c, "Error on-chain stake higher, than configured..."); } } From 5338eb38a26c83a3ef72bf4799955aeedc391a2b Mon Sep 17 00:00:00 2001 From: Philipp D Date: Wed, 29 Nov 2023 13:38:50 +0100 Subject: [PATCH 3/6] Rustify if condition --- standalone/prb/src/worker.rs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/standalone/prb/src/worker.rs b/standalone/prb/src/worker.rs index 14d0d35daf..19bb2926f8 100644 --- a/standalone/prb/src/worker.rs +++ b/standalone/prb/src/worker.rs @@ -539,17 +539,26 @@ impl WorkerContext { vec![Value::from_bytes(worker_binding.as_ref().unwrap())], ); let stake_onchain: Option = fetch_storage_bytes(&api, &stake_query).await?; - if stake_onchain.is_some() { - info!("Stake: {:?}, {:?} on-chain", &worker.stake.parse::().unwrap(), &stake_onchain.unwrap()); - if stake_onchain.unwrap() < worker.stake.parse::().unwrap() { - set_worker_message!(c, "Adjusting on-chain stake..."); - txm.clone() - .restart_computing(pid, pubkey, worker.stake) - .await?; - Self::set_state(c.clone(), WorkerLifecycleState::Working).await; - } else if stake_onchain.unwrap() > worker.stake.parse::().unwrap() { - set_worker_message!(c, "Error on-chain stake higher, than configured..."); + match stake_onchain { + Some(onchain_stake) => { + info!( + "Stake: {:?}, {:?} on-chain", + &worker.stake.parse::().unwrap(), + &onchain_stake + ); + match onchain_stake { + onchain_stake if onchain_stake < worker.stake.parse::().unwrap() => { + set_worker_message!(c, "Adjusting on-chain stake..."); + txm.clone().restart_computing(pid, pubkey, worker.stake).await?; + Self::set_state(c.clone(), WorkerLifecycleState::Working).await; + } + onchain_stake if onchain_stake > worker.stake.parse::().unwrap() => { + set_worker_message!(c, "Error on-chain stake higher, than configured..."); + } + _ => (), + } } + None => (), } first_run = false; } From b659cf045c204a01eafc6dec0832d39ae008cc6d Mon Sep 17 00:00:00 2001 From: Philipp D Date: Thu, 30 Nov 2023 19:55:08 +0100 Subject: [PATCH 4/6] Fix clippy errors --- standalone/prb/src/worker.rs | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/standalone/prb/src/worker.rs b/standalone/prb/src/worker.rs index 19bb2926f8..1ba344b965 100644 --- a/standalone/prb/src/worker.rs +++ b/standalone/prb/src/worker.rs @@ -539,26 +539,19 @@ impl WorkerContext { vec![Value::from_bytes(worker_binding.as_ref().unwrap())], ); let stake_onchain: Option = fetch_storage_bytes(&api, &stake_query).await?; - match stake_onchain { - Some(onchain_stake) => { - info!( - "Stake: {:?}, {:?} on-chain", - &worker.stake.parse::().unwrap(), - &onchain_stake - ); - match onchain_stake { - onchain_stake if onchain_stake < worker.stake.parse::().unwrap() => { - set_worker_message!(c, "Adjusting on-chain stake..."); - txm.clone().restart_computing(pid, pubkey, worker.stake).await?; - Self::set_state(c.clone(), WorkerLifecycleState::Working).await; - } - onchain_stake if onchain_stake > worker.stake.parse::().unwrap() => { - set_worker_message!(c, "Error on-chain stake higher, than configured..."); - } - _ => (), - } + if let Some(onchain_stake) = stake_onchain { + info!("Stake: {:?}, {:?} on-chain", &worker.stake.parse::().unwrap(), &onchain_stake); + match onchain_stake { + onchain_stake if onchain_stake < worker.stake.parse::().unwrap() => { + set_worker_message!(c, "Adjusting on-chain stake..."); + txm.clone().restart_computing(pid, pubkey, worker.stake).await?; + Self::set_state(c.clone(), WorkerLifecycleState::Working).await; + } + onchain_stake if onchain_stake > worker.stake.parse::().unwrap() => { + set_worker_message!(c, "Error on-chain stake higher, than configured..."); + } + _ => (), } - None => (), } first_run = false; } From bf30c4844a8eef0b0dc1e68d0fb7f2d4a93d6e32 Mon Sep 17 00:00:00 2001 From: Nexus2k Date: Sat, 2 Dec 2023 16:04:02 +0100 Subject: [PATCH 5/6] Revert Cargo.lock bumps --- Cargo.lock | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8b720aaf52..99e815d127 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2831,12 +2831,12 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.3" +version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" +checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" dependencies = [ - "darling_core 0.20.3", - "darling_macro 0.20.3", + "darling_core 0.20.1", + "darling_macro 0.20.1", ] [[package]] @@ -2868,9 +2868,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.3" +version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" +checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" dependencies = [ "fnv", "ident_case", @@ -2904,9 +2904,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.3" +version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" +checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" dependencies = [ "darling_core 0.20.1", "quote 1.0.33", @@ -15333,7 +15333,7 @@ dependencies = [ name = "subxt-macro" version = "0.29.0" dependencies = [ - "darling 0.20.3", + "darling 0.20.1", "proc-macro-error", "subxt-codegen", "syn 2.0.38", From 2e86abfad06b8aba58bdbb0bcde035b1fe8e9805 Mon Sep 17 00:00:00 2001 From: Nexus2k Date: Sat, 2 Dec 2023 16:44:52 +0100 Subject: [PATCH 6/6] Fix clippy bug for unread assignment --- standalone/prb/src/worker.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/standalone/prb/src/worker.rs b/standalone/prb/src/worker.rs index 1ba344b965..d9ea575b95 100644 --- a/standalone/prb/src/worker.rs +++ b/standalone/prb/src/worker.rs @@ -422,6 +422,7 @@ impl WorkerContext { Ok(()) } + #[allow(unused_assignments)] async fn handle_on_preparing(c: WrappedWorkerContext) -> Result<()> { set_worker_message!(c, "Reached latest finalized height, start preparing..."); let (lm, worker, pr) = extract_essential_values!(c);