Skip to content

Commit

Permalink
Store ledger_canister_id along with each NervousSystem (#4551)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Oct 11, 2023
1 parent c5a4cd1 commit 2edf2ee
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 18 deletions.
1 change: 1 addition & 0 deletions backend/canisters/proposals_bot/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -67,7 +67,7 @@ async fn is_successfully_launched(sns_swap_canister_id: CanisterId) -> Option<bo
}
}

async fn create_group(governance_canister_id: CanisterId, name: String) {
async fn create_group(governance_canister_id: CanisterId, ledger_canister_id: CanisterId, name: String) {
let (group_index_canister_id, is_nns) = read_state(|state| {
(
state.data.group_index_canister_id,
Expand Down Expand Up @@ -98,10 +98,11 @@ async fn create_group(governance_canister_id: CanisterId, name: String) {
match group_index_canister_c2c_client::c2c_create_group(group_index_canister_id, &create_group_args).await {
Ok(group_index_canister::c2c_create_group::Response::Success(result)) => {
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");
}
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/proposals_bot/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TimestampMillis>,
pub latest_failed_sync: Option<TimestampMillis>,
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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);
});
}
}
37 changes: 26 additions & 11 deletions backend/canisters/proposals_bot/impl/src/model/nervous_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MultiUserChat> {
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<SnsNeuronId> {
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,
Expand Down Expand Up @@ -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<TimestampMillis>,
latest_failed_sync: Option<TimestampMillis>,
Expand All @@ -242,16 +255,16 @@ pub struct NervousSystem {
proposals_to_be_pushed: ProposalsToBePushed,
proposals_to_be_updated: ProposalsToBeUpdated,
active_proposals: BTreeMap<ProposalId, (Proposal, MessageId)>,
#[serde(default)]
neuron_id_for_submitting_proposals: Option<SnsNeuronId>,
#[serde(default)]
sync_in_progress: bool,
#[serde(default)]
active_user_submitted_proposals: HashMap<ProposalId, UserId>,
#[serde(default)]
decided_user_submitted_proposals: Vec<UserSubmittedProposalResult>,
}

fn anonymous_principal() -> CanisterId {
CanisterId::anonymous()
}

#[derive(Serialize, Deserialize, Debug, Default)]
struct ProposalsToBePushed {
pub queue: BTreeMap<ProposalId, Proposal>,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2edf2ee

Please sign in to comment.