From 761c95b1f1d35bc42684ad296526b069579a3658 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Wed, 18 Dec 2024 17:38:24 +0000 Subject: [PATCH] Have NeuronController refresh 8 year neuron rather than UserIndex (#7080) --- Cargo.lock | 2 - .../canisters/neuron_controller/CHANGELOG.md | 1 + .../neuron_controller/impl/src/jobs/mod.rs | 2 + .../impl/src/jobs/refresh_8_year_neuron.rs | 52 +++++++++++++++++++ backend/canisters/user_index/CHANGELOG.md | 1 + backend/canisters/user_index/impl/Cargo.toml | 2 - .../impl/src/jobs/make_pending_payments.rs | 10 ++-- backend/canisters/user_index/impl/src/lib.rs | 23 -------- 8 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 backend/canisters/neuron_controller/impl/src/jobs/refresh_8_year_neuron.rs diff --git a/Cargo.lock b/Cargo.lock index 8534604455..2cf97e7ac8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8384,8 +8384,6 @@ dependencies = [ "modclub_canister", "modclub_canister_c2c_client", "msgpack", - "nns_governance_canister", - "nns_governance_canister_c2c_client", "online_users_canister", "online_users_canister_c2c_client", "p256_key_pair", diff --git a/backend/canisters/neuron_controller/CHANGELOG.md b/backend/canisters/neuron_controller/CHANGELOG.md index 5b54ef402b..5166faa2d5 100644 --- a/backend/canisters/neuron_controller/CHANGELOG.md +++ b/backend/canisters/neuron_controller/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed - Expose size of each virtual stable memory in metrics ([#6981](https://github.com/open-chat-labs/open-chat/pull/6981)) +- Have NeuronController refresh 8 year neuron rather than UserIndex ([#7080](https://github.com/open-chat-labs/open-chat/pull/7080)) ## [[2.0.1484](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1484-neuron_controller)] - 2024-11-29 diff --git a/backend/canisters/neuron_controller/impl/src/jobs/mod.rs b/backend/canisters/neuron_controller/impl/src/jobs/mod.rs index 664b3f9c29..9bb7274f44 100644 --- a/backend/canisters/neuron_controller/impl/src/jobs/mod.rs +++ b/backend/canisters/neuron_controller/impl/src/jobs/mod.rs @@ -1,7 +1,9 @@ use crate::RuntimeState; pub mod process_neurons; +mod refresh_8_year_neuron; pub(crate) fn start(_state: &RuntimeState) { process_neurons::start_job(); + refresh_8_year_neuron::start_job(); } diff --git a/backend/canisters/neuron_controller/impl/src/jobs/refresh_8_year_neuron.rs b/backend/canisters/neuron_controller/impl/src/jobs/refresh_8_year_neuron.rs new file mode 100644 index 0000000000..be2a007ddb --- /dev/null +++ b/backend/canisters/neuron_controller/impl/src/jobs/refresh_8_year_neuron.rs @@ -0,0 +1,52 @@ +use crate::read_state; +use constants::DAY_IN_MS; +use nns_governance_canister::types::manage_neuron::claim_or_refresh::By; +use nns_governance_canister::types::manage_neuron::{ClaimOrRefresh, Command}; +use nns_governance_canister::types::neuron::DissolveState; +use nns_governance_canister::types::Empty; +use std::time::Duration; +use tracing::info; +use types::Milliseconds; +use utils::canister_timers::run_now_then_interval; + +const REFRESH_NEURON_INTERVAL: Milliseconds = DAY_IN_MS; + +pub fn start_job() { + run_now_then_interval(Duration::from_millis(REFRESH_NEURON_INTERVAL), run); +} + +fn run() { + ic_cdk::spawn(run_async()); +} + +async fn run_async() { + if let Some((nns_governance_canister_id, neuron_id)) = read_state(|state| { + state + .data + .neurons + .active_neurons + .iter() + .max_by_key(|n| { + n.dissolve_state + .as_ref() + .map(|ds| if let DissolveState::DissolveDelaySeconds(dd) = ds { *dd } else { 0 }) + }) + .map(|n| (state.data.nns_governance_canister_id, n.id.clone())) + }) { + if nns_governance_canister_c2c_client::manage_neuron( + nns_governance_canister_id, + &nns_governance_canister::manage_neuron::Args { + id: neuron_id.clone(), + neuron_id_or_subaccount: None, + command: Some(Command::ClaimOrRefresh(ClaimOrRefresh { + by: Some(By::NeuronIdOrSubaccount(Empty {})), + })), + }, + ) + .await + .is_ok() + { + info!(?neuron_id, "Refreshed neuron"); + } + } +} diff --git a/backend/canisters/user_index/CHANGELOG.md b/backend/canisters/user_index/CHANGELOG.md index 1778f09922..a505d13d8c 100644 --- a/backend/canisters/user_index/CHANGELOG.md +++ b/backend/canisters/user_index/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Allow Registry to add additional LocalUserIndexes ([#7072](https://github.com/open-chat-labs/open-chat/pull/7072)) - Add `update_bot` endpoint only callable by bot owner ([#7073](https://github.com/open-chat-labs/open-chat/pull/7073)) - Handle installing large wasms onto new subnets ([#7078](https://github.com/open-chat-labs/open-chat/pull/7078)) +- Have NeuronController refresh 8 year neuron rather than UserIndex ([#7080](https://github.com/open-chat-labs/open-chat/pull/7080)) ### Removed diff --git a/backend/canisters/user_index/impl/Cargo.toml b/backend/canisters/user_index/impl/Cargo.toml index ef25153676..7338f9f1ae 100644 --- a/backend/canisters/user_index/impl/Cargo.toml +++ b/backend/canisters/user_index/impl/Cargo.toml @@ -48,8 +48,6 @@ p256_key_pair = { path = "../../../libraries/p256_key_pair" } modclub_canister = { path = "../../../external_canisters/modclub/api" } modclub_canister_c2c_client = { path = "../../../external_canisters/modclub/c2c_client" } msgpack = { path = "../../../libraries/msgpack" } -nns_governance_canister = { path = "../../../external_canisters/nns_governance/api" } -nns_governance_canister_c2c_client = { path = "../../../external_canisters/nns_governance/c2c_client" } online_users_canister = { path = "../../online_users/api" } online_users_canister_c2c_client = { path = "../../online_users/c2c_client" } proof_of_unique_personhood = { path = "../../../libraries/proof_of_unique_personhood" } diff --git a/backend/canisters/user_index/impl/src/jobs/make_pending_payments.rs b/backend/canisters/user_index/impl/src/jobs/make_pending_payments.rs index 7b44e0a664..e3d1776509 100644 --- a/backend/canisters/user_index/impl/src/jobs/make_pending_payments.rs +++ b/backend/canisters/user_index/impl/src/jobs/make_pending_payments.rs @@ -51,15 +51,11 @@ async fn process_payment(pending_payment: PendingPayment) { mutate_state(|state| { match result { - Ok(Ok(block_index)) => match reason { - PendingPaymentReason::ReferralReward => { + Ok(Ok(block_index)) => { + if matches!(reason, PendingPaymentReason::ReferralReward) { inform_referrer(&pending_payment, block_index, state); } - PendingPaymentReason::TopUpNeuron => { - state.data.refresh_nns_neuron(); - } - _ => {} - }, + } Ok(Err(_)) => {} Err(_) => { state.data.pending_payments_queue.push(pending_payment); diff --git a/backend/canisters/user_index/impl/src/lib.rs b/backend/canisters/user_index/impl/src/lib.rs index 8e80a1ef12..bc5e6dfeca 100644 --- a/backend/canisters/user_index/impl/src/lib.rs +++ b/backend/canisters/user_index/impl/src/lib.rs @@ -19,9 +19,6 @@ use model::pending_modclub_submissions_queue::{PendingModclubSubmission, Pending use model::pending_payments_queue::{PendingPayment, PendingPaymentsQueue}; use model::reported_messages::{ReportedMessages, ReportingMetrics}; use model::user::SuspensionDetails; -use nns_governance_canister::types::manage_neuron::claim_or_refresh::By; -use nns_governance_canister::types::manage_neuron::{ClaimOrRefresh, Command}; -use nns_governance_canister::types::{Empty, ManageNeuron, NeuronId}; use p256_key_pair::P256KeyPair; use serde::{Deserialize, Serialize}; use std::cell::RefCell; @@ -503,26 +500,6 @@ impl Data { }) } - pub fn refresh_nns_neuron(&self) { - if let Some(neuron_id) = self.nns_8_year_neuron.as_ref().map(|n| n.neuron_id) { - ic_cdk::spawn(refresh_nns_neuron_inner(self.nns_governance_canister_id, neuron_id)); - } - - async fn refresh_nns_neuron_inner(nns_governance_canister_id: CanisterId, neuron_id: u64) { - let _ = nns_governance_canister_c2c_client::manage_neuron( - nns_governance_canister_id, - &ManageNeuron { - id: Some(NeuronId { id: neuron_id }), - neuron_id_or_subaccount: None, - command: Some(Command::ClaimOrRefresh(ClaimOrRefresh { - by: Some(By::NeuronIdOrSubaccount(Empty {})), - })), - }, - ) - .await; - } - } - pub fn chit_bands(&self, size: u32, year: u32, month: u8) -> BTreeMap { let mut bands = BTreeMap::new(); let month_key = MonthKey::new(year, month);