From 2edf2eec32bd58c8296a3c8164230a2b11ae8f49 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Wed, 11 Oct 2023 08:28:29 +0100 Subject: [PATCH] Store `ledger_canister_id` along with each `NervousSystem` (#4551) --- backend/canisters/proposals_bot/CHANGELOG.md | 1 + .../impl/src/jobs/check_for_new_snses.rs | 13 ++++--- .../canisters/proposals_bot/impl/src/lib.rs | 1 + .../impl/src/lifecycle/post_upgrade.rs | 28 +++++++++++++- .../impl/src/model/nervous_systems.rs | 37 +++++++++++++------ 5 files changed, 62 insertions(+), 18 deletions(-) diff --git a/backend/canisters/proposals_bot/CHANGELOG.md b/backend/canisters/proposals_bot/CHANGELOG.md index 60659dc381..b5240f31d2 100644 --- a/backend/canisters/proposals_bot/CHANGELOG.md +++ b/backend/canisters/proposals_bot/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed - Retry submitting proposal if looking up user fails ([#4543](https://github.com/open-chat-labs/open-chat/pull/4543)) +- Store `ledger_canister_id` along with each `NervousSystem` ([#4551](https://github.com/open-chat-labs/open-chat/pull/4551)) ## [[2.0.881](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.879-proposals_bot)] - 2023-10-10 diff --git a/backend/canisters/proposals_bot/impl/src/jobs/check_for_new_snses.rs b/backend/canisters/proposals_bot/impl/src/jobs/check_for_new_snses.rs index c833c35e21..11fe6e0f89 100644 --- a/backend/canisters/proposals_bot/impl/src/jobs/check_for_new_snses.rs +++ b/backend/canisters/proposals_bot/impl/src/jobs/check_for_new_snses.rs @@ -44,7 +44,7 @@ async fn run_async() { sns_governance_canister_c2c_client::get_metadata(governance_canister_id, &Empty {}).await { let name = metadata.name.unwrap(); - ic_cdk::spawn(create_group(governance_canister_id, name)); + ic_cdk::spawn(create_group(governance_canister_id, sns.ledger_canister_id.unwrap(), name)); } } else { info!(%root_canister_id, "Recording failed SNS launch"); @@ -67,7 +67,7 @@ async fn is_successfully_launched(sns_swap_canister_id: CanisterId) -> Option { mutate_state(|state| { - state - .data - .nervous_systems - .add(governance_canister_id, MultiUserChat::Group(result.chat_id)); + state.data.nervous_systems.add( + governance_canister_id, + ledger_canister_id, + MultiUserChat::Group(result.chat_id), + ); }); info!(%governance_canister_id, name = name.as_str(), "Proposals group created"); } diff --git a/backend/canisters/proposals_bot/impl/src/lib.rs b/backend/canisters/proposals_bot/impl/src/lib.rs index e0aca060ee..698fc5c008 100644 --- a/backend/canisters/proposals_bot/impl/src/lib.rs +++ b/backend/canisters/proposals_bot/impl/src/lib.rs @@ -119,6 +119,7 @@ pub struct Metrics { #[derive(CandidType, Serialize, Debug)] pub struct NervousSystemMetrics { pub governance_canister_id: CanisterId, + pub ledger_canister_id: CanisterId, pub chat_id: MultiUserChat, pub latest_successful_sync: Option, pub latest_failed_sync: Option, diff --git a/backend/canisters/proposals_bot/impl/src/lifecycle/post_upgrade.rs b/backend/canisters/proposals_bot/impl/src/lifecycle/post_upgrade.rs index db208a44c7..d5a1398125 100644 --- a/backend/canisters/proposals_bot/impl/src/lifecycle/post_upgrade.rs +++ b/backend/canisters/proposals_bot/impl/src/lifecycle/post_upgrade.rs @@ -1,12 +1,14 @@ use crate::lifecycle::{init_env, init_state, UPGRADE_BUFFER_SIZE}; use crate::memory::get_upgrades_memory; -use crate::Data; +use crate::{mutate_state, read_state, Data}; use canister_logger::LogEntry; use canister_tracing_macros::trace; use ic_cdk_macros::post_upgrade; use ic_stable_structures::reader::{BufferedReader, Reader}; use proposals_bot_canister::post_upgrade::Args; +use std::time::Duration; use tracing::info; +use types::{CanisterId, Empty}; use utils::cycles::init_cycles_dispenser_client; #[post_upgrade] @@ -25,4 +27,28 @@ fn post_upgrade(args: Args) { init_state(env, data, args.wasm_version); info!(version = %args.wasm_version, "Post-upgrade complete"); + + ic_cdk_timers::set_timer(Duration::ZERO, || ic_cdk::spawn(fetch_ledger_canister_ids())); +} + +async fn fetch_ledger_canister_ids() { + let sns_wasm_canister_id = read_state(|state| state.data.sns_wasm_canister_id); + + if let Ok(response) = sns_wasm_canister_c2c_client::list_deployed_snses(sns_wasm_canister_id, &Empty {}).await { + mutate_state(|state| { + for sns in response.instances { + state + .data + .nervous_systems + .set_ledger_canister_id(sns.governance_canister_id.unwrap(), sns.ledger_canister_id.unwrap()); + } + + let nns_governance_canister_id = CanisterId::from_text("rrkah-fqaaa-aaaaa-aaaaq-cai").unwrap(); + let nns_ledger_canister_id = CanisterId::from_text("ryjl3-tyaaa-aaaaa-aaaba-cai").unwrap(); + state + .data + .nervous_systems + .set_ledger_canister_id(nns_governance_canister_id, nns_ledger_canister_id); + }); + } } diff --git a/backend/canisters/proposals_bot/impl/src/model/nervous_systems.rs b/backend/canisters/proposals_bot/impl/src/model/nervous_systems.rs index a5443158da..4a297280f2 100644 --- a/backend/canisters/proposals_bot/impl/src/model/nervous_systems.rs +++ b/backend/canisters/proposals_bot/impl/src/model/nervous_systems.rs @@ -16,21 +16,32 @@ pub struct NervousSystems { } impl NervousSystems { - pub fn add(&mut self, governance_canister_id: CanisterId, chat_id: MultiUserChat) { - self.nervous_systems - .insert(governance_canister_id, NervousSystem::new(governance_canister_id, chat_id)); + pub fn add(&mut self, governance_canister_id: CanisterId, ledger_canister_id: CanisterId, chat_id: MultiUserChat) { + self.nervous_systems.insert( + governance_canister_id, + NervousSystem::new(governance_canister_id, ledger_canister_id, chat_id), + ); + } + + pub fn get(&self, governance_canister_id: &CanisterId) -> Option<&NervousSystem> { + self.nervous_systems.get(governance_canister_id) } pub fn get_chat_id(&self, governance_canister_id: &CanisterId) -> Option { - self.nervous_systems.get(governance_canister_id).map(|ns| ns.chat_id) + self.get(governance_canister_id).map(|ns| ns.chat_id) } pub fn get_neuron_id_for_submitting_proposals(&self, governance_canister_id: &CanisterId) -> Option { - self.nervous_systems - .get(governance_canister_id) + self.get(governance_canister_id) .and_then(|ns| ns.neuron_id_for_submitting_proposals) } + pub fn set_ledger_canister_id(&mut self, governance_canister_id: CanisterId, ledger_canister_id: CanisterId) { + if let Some(ns) = self.nervous_systems.get_mut(&governance_canister_id) { + ns.ledger_canister_id = ledger_canister_id; + } + } + pub fn set_neuron_id_for_submitting_proposals( &mut self, governance_canister_id: &CanisterId, @@ -234,6 +245,8 @@ impl NervousSystems { #[derive(Serialize, Deserialize, Debug)] pub struct NervousSystem { governance_canister_id: CanisterId, + #[serde(default = "anonymous_principal")] + ledger_canister_id: CanisterId, chat_id: MultiUserChat, latest_successful_sync: Option, latest_failed_sync: Option, @@ -242,16 +255,16 @@ pub struct NervousSystem { proposals_to_be_pushed: ProposalsToBePushed, proposals_to_be_updated: ProposalsToBeUpdated, active_proposals: BTreeMap, - #[serde(default)] neuron_id_for_submitting_proposals: Option, - #[serde(default)] sync_in_progress: bool, - #[serde(default)] active_user_submitted_proposals: HashMap, - #[serde(default)] decided_user_submitted_proposals: Vec, } +fn anonymous_principal() -> CanisterId { + CanisterId::anonymous() +} + #[derive(Serialize, Deserialize, Debug, Default)] struct ProposalsToBePushed { pub queue: BTreeMap, @@ -265,9 +278,10 @@ struct ProposalsToBeUpdated { } impl NervousSystem { - pub fn new(governance_canister_id: CanisterId, chat_id: MultiUserChat) -> NervousSystem { + pub fn new(governance_canister_id: CanisterId, ledger_canister_id: CanisterId, chat_id: MultiUserChat) -> NervousSystem { NervousSystem { governance_canister_id, + ledger_canister_id, chat_id, latest_successful_sync: None, latest_failed_sync: None, @@ -364,6 +378,7 @@ impl From<&NervousSystem> for NervousSystemMetrics { fn from(ns: &NervousSystem) -> Self { NervousSystemMetrics { governance_canister_id: ns.governance_canister_id, + ledger_canister_id: ns.ledger_canister_id, chat_id: ns.chat_id, latest_successful_sync: ns.latest_successful_sync, latest_failed_sync: ns.latest_failed_sync,