Skip to content

Commit

Permalink
Reuse existing uninstalled user canisters (#6047)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Jul 17, 2024
1 parent 3e8951f commit 9fba60f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/canisters/local_user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

- Store `unique_person_proof` in User canisters ([#6029](https://github.com/open-chat-labs/open-chat/pull/6029))
- Reuse existing uninstalled user canisters ([#6047](https://github.com/open-chat-labs/open-chat/pull/6047))

### Removed

Expand Down
1 change: 1 addition & 0 deletions backend/canisters/local_user_index/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub enum Event {
DeleteUser(DeleteUser),
SecretKeySet(Vec<u8>),
NotifyUniquePersonProof(UserId, UniquePersonProof),
AddCanisterToPool(CanisterId),
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,10 @@ fn handle_event(event: Event, state: &mut RuntimeState) {
Event::NotifyUniquePersonProof(user_id, proof) => {
state.data.global_users.insert_unique_person_proof(user_id, proof);
}
Event::AddCanisterToPool(canister_id) => {
if !state.data.canister_pool.contains(&canister_id) {
state.data.canister_pool.push(canister_id);
}
}
}
}
4 changes: 4 additions & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Add `is_unique_person` field to user responses ([#6040](https://github.com/open-chat-labs/open-chat/pull/6040))
- Auto delete users who get flagged as being empty and dormant ([#6046](https://github.com/open-chat-labs/open-chat/pull/6046))

### Changed

- Reuse existing uninstalled user canisters ([#6047](https://github.com/open-chat-labs/open-chat/pull/6047))

## [[2.0.1235](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1235-user_index)] - 2024-07-11

### Added
Expand Down
26 changes: 26 additions & 0 deletions backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use canister_tracing_macros::trace;
use ic_cdk::post_upgrade;
use stable_memory::get_reader;
use tracing::info;
use types::CanisterId;
use user_index_canister::post_upgrade::Args;
use utils::cycles::init_cycles_dispenser_client;

Expand Down Expand Up @@ -33,5 +34,30 @@ fn post_upgrade(args: Args) {
.push_back((user.principal, Some(user.user_id)));
}
crate::jobs::sync_users_to_identity_canister::start_job_if_required(state);

for canister_id in state.data.deleted_users.iter().map(|u| CanisterId::from(u.user_id)) {
let local_user_index = local_user_index_canister(canister_id, state.data.test_mode);
let event = local_user_index_canister::Event::AddCanisterToPool(canister_id);
state.data.user_index_event_sync_queue.push(local_user_index, event);
}
crate::jobs::sync_events_to_local_user_index_canisters::start_job_if_required(state);
})
}

fn local_user_index_canister(canister_id: CanisterId, test_mode: bool) -> CanisterId {
let bytes = canister_id.as_slice();
if bytes > [0, 0, 0, 0, 2, 32, 0, 0, 1, 1].as_slice() && bytes < [0, 0, 0, 0, 2, 48, 0, 0, 1, 1].as_slice() {
return if test_mode {
CanisterId::from_text("pecvb-tqaaa-aaaaf-bhdiq-cai").unwrap()
} else {
CanisterId::from_text("nq4qv-wqaaa-aaaaf-bhdgq-cai").unwrap()
};
}
if bytes > [0, 0, 0, 0, 0, 160, 0, 0, 1, 1].as_slice() && bytes < [0, 0, 0, 0, 0, 176, 0, 0, 1, 1].as_slice() {
return CanisterId::from_text("aboy3-giaaa-aaaar-aaaaq-cai").unwrap();
}

assert!(test_mode);
// This will only be reached during tests + local development
CanisterId::from_text("be2us-64aaa-aaaaa-qaabq-cai").unwrap()
}

0 comments on commit 9fba60f

Please sign in to comment.