Skip to content

Commit

Permalink
Get nervous system updates from the Registry (#4557)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Oct 12, 2023
1 parent aa88dae commit ec3ab40
Show file tree
Hide file tree
Showing 23 changed files with 326 additions and 234 deletions.
17 changes: 15 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ members = [
"backend/canisters/proposals_bot/api",
"backend/canisters/proposals_bot/impl",
"backend/canisters/registry/api",
"backend/canisters/registry/c2c_client",
"backend/canisters/registry/client",
"backend/canisters/registry/impl",
"backend/canisters/storage_bucket/api",
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/proposals_bot/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- 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))
- Get nervous system updates from the Registry ([#4557](https://github.com/open-chat-labs/open-chat/pull/4557))

## [[2.0.881](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.879-proposals_bot)] - 2023-10-10

Expand Down
4 changes: 2 additions & 2 deletions backend/canisters/proposals_bot/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ local_user_index_canister_c2c_client = { path = "../../local_user_index/c2c_clie
msgpack = { path = "../../../libraries/msgpack" }
proposals_bot_canister = { path = "../api" }
rand = { workspace = true }
registry_canister = { path = "../../../canisters/registry/api" }
registry_canister_c2c_client = { path = "../../../canisters/registry/c2c_client" }
serde = { workspace = true }
serializer = { path = "../../../libraries/serializer" }
sha2 = { workspace = true }
sns_governance_canister = { path = "../../../external_canisters/sns_governance/api" }
sns_governance_canister_c2c_client = { path = "../../../external_canisters/sns_governance/c2c_client" }
sns_swap_canister_c2c_client = { path = "../../../external_canisters/sns_swap/c2c_client" }
sns_wasm_canister_c2c_client = { path = "../../../external_canisters/sns_wasm/c2c_client" }
tracing = { workspace = true }
types = { path = "../../../libraries/types" }
user_index_canister_c2c_client = { path = "../../user_index/c2c_client" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use crate::{mutate_state, read_state};
use registry_canister::NervousSystemDetails;
use std::fmt::Write;
use std::time::Duration;
use tracing::{error, info, trace};
use types::{
CanisterId, GovernanceProposalsSubtype, GroupPermissionRole, GroupPermissions, GroupSubtype, MultiUserChat, Rules,
};
use utils::canister_timers::run_now_then_interval;
use utils::time::HOUR_IN_MS;

pub fn start_job() {
run_now_then_interval(Duration::from_millis(HOUR_IN_MS), run);
}

fn run() {
ic_cdk::spawn(run_async());
}

async fn run_async() {
let (registry_canister_id, registry_synced_up_to) =
read_state(|state| (state.data.registry_canister_id, state.data.registry_synced_up_to));

match registry_canister_c2c_client::c2c_nervous_systems(
registry_canister_id,
&registry_canister::c2c_nervous_systems::Args {
updates_since: Some(registry_synced_up_to),
},
)
.await
{
Ok(registry_canister::c2c_nervous_systems::Response::Success(result)) => {
mutate_state(|state| {
for ns in result.nervous_systems {
let governance_canister_id = ns.governance_canister_id;
if state.data.nervous_systems.exists(&governance_canister_id) {
info!(%governance_canister_id, "Updating nervous system");
state.data.nervous_systems.update_from_registry(ns);
} else {
info!(%governance_canister_id, "Creating group for nervous system");
ic_cdk::spawn(create_group(ns, state.data.group_index_canister_id));
}
}
state.data.registry_synced_up_to = result.last_updated;
info!(synced_up_to = result.last_updated, "Registry sync complete");
});
}
Ok(registry_canister::c2c_nervous_systems::Response::SuccessNoUpdates) => {
trace!("No registry updates");
}
_ => {}
}
}

async fn create_group(ns: NervousSystemDetails, group_index_canister_id: CanisterId) {
let governance_canister_id = ns.governance_canister_id;
let name = ns.name.clone();

let create_group_args = group_index_canister::c2c_create_group::Args {
is_public: true,
name: format!("{} Proposals", name),
description: default_description(&name),
rules: Rules::default(),
subtype: Some(GroupSubtype::GovernanceProposals(GovernanceProposalsSubtype {
governance_canister_id,
is_nns: ns.is_nns,
})),
avatar: None,
history_visible_to_new_joiners: true,
permissions: Some(GroupPermissions {
create_polls: GroupPermissionRole::Admins,
send_messages: GroupPermissionRole::Admins,
..Default::default()
}),
events_ttl: None,
gate: None,
};

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(ns, MultiUserChat::Group(result.chat_id));
});
info!(%governance_canister_id, name = name.as_str(), "Proposals group created");
}
response => error!(?response, %governance_canister_id, name = name.as_str(), "Failed to create proposals group"),
}
}

fn default_description(name: &str) -> String {
let mut description = String::new();
writeln!(&mut description, "Join this group to view and vote on {name} proposals.").unwrap();
writeln!(&mut description).unwrap();
writeln!(
&mut description,
"To vote on proposals you must add your user id as a hotkey to any {name} neurons you wish to vote with."
)
.unwrap();
writeln!(&mut description).unwrap();
writeln!(&mut description, "Your OpenChat user id is {{userId}}.").unwrap();
description
}
125 changes: 0 additions & 125 deletions backend/canisters/proposals_bot/impl/src/jobs/check_for_new_snses.rs

This file was deleted.

4 changes: 2 additions & 2 deletions backend/canisters/proposals_bot/impl/src/jobs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::RuntimeState;

mod check_for_new_snses;
mod check_for_nervous_system_updates;
mod push_proposals;
mod retrieve_proposals;
mod update_finished_proposals;
mod update_proposals;

pub(crate) fn start(state: &RuntimeState) {
check_for_new_snses::start_job();
check_for_nervous_system_updates::start_job();
push_proposals::start_job_if_required(state);
retrieve_proposals::start_job();
update_finished_proposals::start_job_if_required(state);
Expand Down
Loading

0 comments on commit ec3ab40

Please sign in to comment.