Skip to content

Commit

Permalink
Store whether submitting proposals is enabled or not in the Registry (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Oct 12, 2023
1 parent 47de336 commit 307303a
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions backend/canister_installer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ async fn install_service_canisters_impl(
service_owner_principals: vec![principal],
user_index_canister_id: canister_ids.user_index,
group_index_canister_id: canister_ids.group_index,
registry_canister_id: canister_ids.registry,
nns_governance_canister_id: canister_ids.nns_governance,
sns_wasm_canister_id: canister_ids.nns_sns_wasm,
cycles_dispenser_canister_id: canister_ids.cycles_dispenser,
Expand Down Expand Up @@ -153,6 +154,7 @@ async fn install_service_canisters_impl(
let registry_canister_wasm = get_canister_wasm(CanisterName::Registry, version);
let registry_init_args = registry_canister::init::Args {
governance_principals: vec![principal],
proposals_bot_canister_id: canister_ids.proposals_bot,
nns_ledger_canister_id: canister_ids.nns_ledger,
nns_governance_canister_id: canister_ids.nns_governance,
nns_root_canister_id: canister_ids.nns_root,
Expand Down
4 changes: 4 additions & 0 deletions backend/canisters/proposals_bot/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Added

- Store whether submitting proposals is enabled or not in the Registry ([#4564](https://github.com/open-chat-labs/open-chat/pull/4564))

### Changed

- Retry submitting proposal if looking up user fails ([#4543](https://github.com/open-chat-labs/open-chat/pull/4543))
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/proposals_bot/api/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Args {
pub service_owner_principals: Vec<Principal>,
pub user_index_canister_id: CanisterId,
pub group_index_canister_id: CanisterId,
pub registry_canister_id: CanisterId,
pub nns_governance_canister_id: CanisterId,
pub sns_wasm_canister_id: CanisterId,
pub cycles_dispenser_canister_id: CanisterId,
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/proposals_bot/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ canister_timer_jobs = { path = "../../../libraries/canister_timer_jobs" }
canister_tracing_macros = { path = "../../../libraries/canister_tracing_macros" }
community_canister = { path = "../../community/api" }
community_canister_c2c_client = { path = "../../community/c2c_client" }
fire_and_forget_handler = { path = "../../../libraries/fire_and_forget_handler" }
group_canister = { path = "../../group/api" }
group_canister_c2c_client = { path = "../../group/c2c_client" }
group_index_canister = { path = "../../group_index/api" }
Expand Down
8 changes: 6 additions & 2 deletions backend/canisters/proposals_bot/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::timer_job_types::TimerJob;
use candid::{CandidType, Principal};
use canister_state_macros::canister_state;
use canister_timer_jobs::TimerJobs;
use fire_and_forget_handler::FireAndForgetHandler;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::cell::RefCell;
Expand Down Expand Up @@ -79,6 +80,8 @@ struct Data {
pub timer_jobs: TimerJobs<TimerJob>,
#[serde(default)]
pub registry_synced_up_to: TimestampMillis,
#[serde(default)]
pub fire_and_forget_handler: FireAndForgetHandler,
pub test_mode: bool,
}

Expand All @@ -91,22 +94,23 @@ impl Data {
governance_principals: HashSet<Principal>,
user_index_canister_id: CanisterId,
group_index_canister_id: CanisterId,
registry_canister_id: CanisterId,
cycles_dispenser_canister_id: CanisterId,
nns_governance_canister_id: CanisterId,
registry_canister_id: CanisterId,
test_mode: bool,
) -> Data {
Data {
nervous_systems: NervousSystems::default(),
governance_principals,
user_index_canister_id,
group_index_canister_id,
registry_canister_id,
cycles_dispenser_canister_id,
nns_governance_canister_id,
registry_canister_id,
finished_proposals_to_process: VecDeque::new(),
timer_jobs: TimerJobs::default(),
registry_synced_up_to: 0,
fire_and_forget_handler: FireAndForgetHandler::default(),
test_mode,
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/canisters/proposals_bot/impl/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ fn init(args: Args) {
args.service_owner_principals.into_iter().collect(),
args.user_index_canister_id,
args.group_index_canister_id,
args.registry_canister_id,
args.cycles_dispenser_canister_id,
args.nns_governance_canister_id,
args.sns_wasm_canister_id,
args.test_mode,
);

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, 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;
use utils::cycles::init_cycles_dispenser_client;

#[post_upgrade]
Expand All @@ -25,4 +27,20 @@ fn post_upgrade(args: Args) {
init_state(env, data, args.wasm_version);

info!(version = %args.wasm_version, "Post-upgrade complete");

// TODO Remove this after next upgrade
ic_cdk_timers::set_timer(Duration::ZERO, enable_submitting_oc_proposals);
}

fn enable_submitting_oc_proposals() {
mutate_state(|state| {
state.data.fire_and_forget_handler.send(
state.data.registry_canister_id,
"c2c_set_submitting_proposals_enabled_msgpack".to_string(),
msgpack::serialize_then_unwrap(registry_canister::c2c_set_submitting_proposals_enabled::Args {
governance_canister_id: CanisterId::from_text("2jvtu-yqaaa-aaaaq-aaama-cai").unwrap(),
enabled: true,
}),
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ async fn stake_neuron_for_submitting_proposals(args: Args) -> Response {
.data
.nervous_systems
.set_neuron_id_for_submitting_proposals(&args.governance_canister_id, neuron_id);

state.data.fire_and_forget_handler.send(
state.data.registry_canister_id,
"c2c_set_submitting_proposals_enabled_msgpack".to_string(),
msgpack::serialize_then_unwrap(registry_canister::c2c_set_submitting_proposals_enabled::Args {
governance_canister_id: args.governance_canister_id,
enabled: true,
}),
);
});
Success(neuron_id)
}
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/registry/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/).
### Added

- Added `c2c_nervous_systems` ([#4557](https://github.com/open-chat-labs/open-chat/pull/4557))
- Store whether submitting proposals is enabled or not in the Registry ([#4564](https://github.com/open-chat-labs/open-chat/pull/4564))

### Changed

Expand Down
2 changes: 2 additions & 0 deletions backend/canisters/registry/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub struct NervousSystemDetails {
pub min_dissolve_delay_to_vote: Milliseconds,
pub proposal_rejection_fee: u64,
pub is_nns: bool,
#[serde(default)]
pub submitting_proposals_enabled: bool,
pub added: TimestampMillis,
pub last_updated: TimestampMillis,
}
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/registry/api/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use types::{BuildVersion, CanisterId};
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub governance_principals: Vec<Principal>,
pub proposals_bot_canister_id: CanisterId,
pub nns_ledger_canister_id: CanisterId,
pub nns_governance_canister_id: CanisterId,
pub nns_root_canister_id: CanisterId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::CanisterId;

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub governance_canister_id: CanisterId,
pub enabled: bool,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
}
1 change: 1 addition & 0 deletions backend/canisters/registry/api/src/updates/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod add_token;
pub mod c2c_set_submitting_proposals_enabled;
pub mod update_token;
1 change: 1 addition & 0 deletions backend/canisters/registry/c2c_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ use registry_canister::*;
generate_c2c_call!(c2c_nervous_systems);

// Updates
generate_c2c_call!(c2c_set_submitting_proposals_enabled);
8 changes: 8 additions & 0 deletions backend/canisters/registry/impl/src/guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ pub fn caller_is_governance_principal() -> Result<(), String> {
Err("Caller is not a governance principal".to_string())
}
}

pub fn caller_is_proposals_bot() -> Result<(), String> {
if read_state(|state| state.is_caller_proposals_bot()) {
Ok(())
} else {
Err("Caller is not the proposals bot".to_string())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ fn build_nervous_system_details(
min_neuron_stake: parameters.neuron_minimum_stake_e8s?,
proposal_rejection_fee: parameters.reject_cost_e8s?,
is_nns: false,
submitting_proposals_enabled: false,
added: now,
last_updated: now,
})
Expand Down
15 changes: 15 additions & 0 deletions backend/canisters/registry/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl RuntimeState {
self.data.governance_principals.contains(&caller)
}

pub fn is_caller_proposals_bot(&self) -> bool {
self.env.caller() == self.data.proposals_bot_canister_id
}

pub fn metrics(&self) -> Metrics {
Metrics {
memory_used: utils::memory::used(),
Expand All @@ -52,6 +56,7 @@ impl RuntimeState {
nervous_systems: self.data.nervous_systems.get_all().iter().map(|ns| ns.into()).collect(),
failed_sns_launches: self.data.failed_sns_launches.iter().copied().collect(),
canister_ids: CanisterIds {
proposals_bot: self.data.proposals_bot_canister_id,
sns_wasm: self.data.sns_wasm_canister_id,
cycles_dispenser: self.data.cycles_dispenser_canister_id,
},
Expand All @@ -62,6 +67,8 @@ impl RuntimeState {
#[derive(Serialize, Deserialize)]
struct Data {
governance_principals: HashSet<Principal>,
#[serde(default = "proposals_bot_canister_id")]
proposals_bot_canister_id: CanisterId,
sns_wasm_canister_id: CanisterId,
cycles_dispenser_canister_id: CanisterId,
tokens: Tokens,
Expand All @@ -71,15 +78,21 @@ struct Data {
test_mode: bool,
}

fn proposals_bot_canister_id() -> CanisterId {
CanisterId::from_text("iywa7-ayaaa-aaaaf-aemga-cai").unwrap()
}

impl Data {
pub fn new(
governance_principals: HashSet<Principal>,
proposals_bot_canister_id: CanisterId,
sns_wasm_canister_id: CanisterId,
cycles_dispenser_canister_id: CanisterId,
test_mode: bool,
) -> Data {
Data {
governance_principals,
proposals_bot_canister_id,
sns_wasm_canister_id,
cycles_dispenser_canister_id,
tokens: Tokens::default(),
Expand Down Expand Up @@ -113,6 +126,7 @@ impl Data {
min_neuron_stake: 100_000_000,
proposal_rejection_fee: 1_000_000_000,
is_nns: true,
submitting_proposals_enabled: false,
added: now,
last_updated: now,
},
Expand Down Expand Up @@ -154,6 +168,7 @@ pub struct Metrics {

#[derive(Serialize)]
pub struct CanisterIds {
pub proposals_bot: CanisterId,
pub sns_wasm: CanisterId,
pub cycles_dispenser: CanisterId,
}
1 change: 1 addition & 0 deletions backend/canisters/registry/impl/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn init(args: Args) {
let env = init_env();
let mut data = Data::new(
args.governance_principals.into_iter().collect(),
args.proposals_bot_canister_id,
args.sns_wasm_canister_id,
args.cycles_dispenser_canister_id,
args.test_mode,
Expand Down
19 changes: 19 additions & 0 deletions backend/canisters/registry/impl/src/model/nervous_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ impl NervousSystems {
pub fn last_updated(&self) -> TimestampMillis {
self.last_updated
}

pub fn set_submitting_proposals_enabled(
&mut self,
governance_canister_id: CanisterId,
enabled: bool,
now: TimestampMillis,
) {
if let Some(ns) = self
.nervous_systems
.iter_mut()
.find(|ns| ns.governance_canister_id == governance_canister_id)
{
ns.submitting_proposals_enabled = enabled;
ns.last_updated = now;
self.last_updated = now;
}
}
}

#[derive(Serialize)]
Expand All @@ -48,6 +65,7 @@ pub struct NervousSystemMetrics {
min_dissolve_delay_to_vote: Milliseconds,
proposal_rejection_fee: u64,
is_nns: bool,
submitting_proposals_enabled: bool,
added: TimestampMillis,
last_updated: TimestampMillis,
}
Expand All @@ -69,6 +87,7 @@ impl From<&NervousSystemDetails> for NervousSystemMetrics {
min_dissolve_delay_to_vote: value.min_dissolve_delay_to_vote,
proposal_rejection_fee: value.proposal_rejection_fee,
is_nns: value.is_nns,
submitting_proposals_enabled: value.submitting_proposals_enabled,
added: value.added,
last_updated: value.last_updated,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::guards::caller_is_proposals_bot;
use crate::{mutate_state, RuntimeState};
use canister_api_macros::update_msgpack;
use canister_tracing_macros::trace;
use registry_canister::c2c_set_submitting_proposals_enabled::{Response::*, *};

#[update_msgpack(guard = "caller_is_proposals_bot")]
#[trace]
fn c2c_set_submitting_proposals_enabled(args: Args) -> Response {
mutate_state(|state| c2c_set_submitting_proposals_enabled_impl(args, state))
}

fn c2c_set_submitting_proposals_enabled_impl(args: Args, state: &mut RuntimeState) -> Response {
let now = state.env.now();
state
.data
.nervous_systems
.set_submitting_proposals_enabled(args.governance_canister_id, args.enabled, now);

Success
}
1 change: 1 addition & 0 deletions backend/canisters/registry/impl/src/updates/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod add_token;
pub mod c2c_set_submitting_proposals_enabled;
pub mod update_token;
pub mod wallet_receive;
2 changes: 2 additions & 0 deletions backend/integration_tests/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ fn install_canisters(env: &mut StateMachine, controller: Principal) -> CanisterI
service_owner_principals: vec![controller],
user_index_canister_id,
group_index_canister_id,
registry_canister_id,
nns_governance_canister_id,
sns_wasm_canister_id,
cycles_dispenser_canister_id,
Expand Down Expand Up @@ -207,6 +208,7 @@ fn install_canisters(env: &mut StateMachine, controller: Principal) -> CanisterI

let registry_init_args = registry_canister::init::Args {
governance_principals: vec![controller],
proposals_bot_canister_id,
nns_ledger_canister_id,
nns_root_canister_id,
nns_governance_canister_id,
Expand Down

0 comments on commit 307303a

Please sign in to comment.