Skip to content

Commit

Permalink
Log error if any users have duplicate usernames or principals (#5645)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Apr 8, 2024
1 parent ca5a715 commit 0dd7051
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
4 changes: 4 additions & 0 deletions backend/canisters/user_index/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]

### Changed

- Log error if any users have duplicate usernames or principals ([#5645](https://github.com/open-chat-labs/open-chat/pull/5645))

## [[2.0.1129](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1129-user_index)] - 2024-04-05

### Added
Expand Down
30 changes: 28 additions & 2 deletions backend/canisters/user_index/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::lifecycle::{init_env, init_state};
use crate::memory::get_upgrades_memory;
use crate::Data;
use crate::{read_state, Data};
use canister_logger::LogEntry;
use canister_tracing_macros::trace;
use ic_cdk_macros::post_upgrade;
use stable_memory::get_reader;
use tracing::info;
use tracing::{error, info};
use user_index_canister::post_upgrade::Args;
use utils::cycles::init_cycles_dispenser_client;

Expand All @@ -24,4 +24,30 @@ fn post_upgrade(args: Args) {
init_state(env, data, args.wasm_version);

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

read_state(|state| {
let users_with_duplicate_usernames: Vec<_> = state
.data
.users
.users_with_duplicate_usernames
.iter()
.map(|(u1, u2)| (u1.to_string(), u2.to_string()))
.collect();

let users_with_duplicate_principals: Vec<_> = state
.data
.users
.users_with_duplicate_principals
.iter()
.map(|(u1, u2)| (u1.to_string(), u2.to_string()))
.collect();

if !users_with_duplicate_usernames.is_empty() {
error!(?users_with_duplicate_usernames);
}

if !users_with_duplicate_principals.is_empty() {
error!(?users_with_duplicate_principals);
}
})
}
13 changes: 11 additions & 2 deletions backend/canisters/user_index/impl/src/model/user_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub struct UserMap {
principal_to_user_id: HashMap<Principal, UserId>,
#[serde(skip)]
user_referrals: HashMap<UserId, Vec<UserId>>,
#[serde(skip)]
pub users_with_duplicate_usernames: Vec<(UserId, UserId)>,
#[serde(skip)]
pub users_with_duplicate_principals: Vec<(UserId, UserId)>,
suspected_bots: BTreeSet<UserId>,
suspended_or_unsuspended_users: BTreeSet<(TimestampMillis, UserId)>,
}
Expand Down Expand Up @@ -330,8 +334,13 @@ impl From<UserMapTrimmed> for UserMap {
user_map.user_referrals.entry(referred_by).or_default().push(*user_id);
}

user_map.username_to_user_id.insert(&user.username, *user_id);
user_map.principal_to_user_id.insert(user.principal, *user_id);
if let Some(other_user_id) = user_map.username_to_user_id.insert(&user.username, *user_id) {
user_map.users_with_duplicate_usernames.push((*user_id, other_user_id));
}

if let Some(other_user_id) = user_map.principal_to_user_id.insert(user.principal, *user_id) {
user_map.users_with_duplicate_principals.push((*user_id, other_user_id));
}
}

user_map
Expand Down

0 comments on commit 0dd7051

Please sign in to comment.