-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync userIds to Identity canister (#6027)
- Loading branch information
Showing
21 changed files
with
233 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...pdates/c2c_sync_legacy_user_principals.rs → ...ntity/api/src/updates/c2c_set_user_ids.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
pub mod c2c_sync_legacy_user_principals; | ||
pub mod c2c_set_user_ids; | ||
pub mod create_identity; | ||
pub mod generate_challenge; | ||
pub mod prepare_delegation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "identity_canister_c2c_client" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
canister_client = { path = "../../../libraries/canister_client" } | ||
identity_canister = { path = "../api" } | ||
ic-cdk = { workspace = true } | ||
msgpack = { path = "../../../libraries/msgpack" } | ||
tracing = { workspace = true } | ||
types = { path = "../../../libraries/types" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use canister_client::generate_c2c_call; | ||
use identity_canister::*; | ||
|
||
// Queries | ||
|
||
// Updates | ||
generate_c2c_call!(c2c_set_user_ids); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use crate::read_state; | ||
|
||
pub fn caller_is_user_index_canister() -> Result<(), String> { | ||
if read_state(|state| state.is_caller_user_index_canister()) { | ||
Ok(()) | ||
} else { | ||
Err("Caller is not the user_index canister".to_string()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
backend/canisters/identity/impl/src/updates/c2c_set_user_ids.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::guards::caller_is_user_index_canister; | ||
use crate::{mutate_state, RuntimeState}; | ||
use canister_api_macros::update_msgpack; | ||
use canister_tracing_macros::trace; | ||
use identity_canister::c2c_set_user_ids::{Response::*, *}; | ||
|
||
#[update_msgpack(guard = "caller_is_user_index_canister")] | ||
#[trace] | ||
fn c2c_set_user_ids(args: Args) -> Response { | ||
// This function runs in O(number of users registered x batch size), | ||
// so we need to ensure each batch is fairly small | ||
assert!(args.users.len() <= 100); | ||
|
||
mutate_state(|state| c2c_set_user_ids_impl(args, state)) | ||
} | ||
|
||
fn c2c_set_user_ids_impl(args: Args, state: &mut RuntimeState) -> Response { | ||
for (principal, user_id) in args.users { | ||
state.data.user_principals.set_user_id(principal, user_id); | ||
} | ||
|
||
Success | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod c2c_set_user_ids; | ||
pub mod create_identity; | ||
pub mod generate_challenge; | ||
pub mod prepare_delegation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
backend/canisters/user_index/impl/src/jobs/sync_users_to_identity_canister.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use crate::{mutate_state, RuntimeState}; | ||
use candid::Principal; | ||
use ic_cdk_timers::TimerId; | ||
use std::cell::Cell; | ||
use std::cmp::min; | ||
use std::time::Duration; | ||
use tracing::trace; | ||
use types::{CanisterId, UserId}; | ||
|
||
const BATCH_SIZE: usize = 100; | ||
|
||
thread_local! { | ||
static TIMER_ID: Cell<Option<TimerId>> = Cell::default(); | ||
} | ||
|
||
pub(crate) fn start_job_if_required(state: &RuntimeState) -> bool { | ||
if TIMER_ID.get().is_none() && !state.data.identity_canister_user_sync_queue.is_empty() { | ||
let timer_id = ic_cdk_timers::set_timer(Duration::ZERO, run); | ||
TIMER_ID.set(Some(timer_id)); | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
pub(crate) fn try_run_now(state: &mut RuntimeState) -> bool { | ||
if let Some((canister_id, users)) = next_batch(state) { | ||
if let Some(timer_id) = TIMER_ID.take() { | ||
ic_cdk_timers::clear_timer(timer_id); | ||
} | ||
ic_cdk::spawn(sync_users(canister_id, users)); | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
fn run() { | ||
trace!("'sync_users_to_identity_canister' job running"); | ||
TIMER_ID.set(None); | ||
|
||
if let Some((canister_id, users)) = mutate_state(next_batch) { | ||
ic_cdk::spawn(sync_users(canister_id, users)); | ||
} | ||
} | ||
|
||
#[allow(clippy::type_complexity)] | ||
fn next_batch(state: &mut RuntimeState) -> Option<(CanisterId, Vec<(Principal, Option<UserId>)>)> { | ||
let count = min(state.data.identity_canister_user_sync_queue.len(), BATCH_SIZE); | ||
if count == 0 { | ||
return None; | ||
} | ||
|
||
let batch: Vec<_> = state.data.identity_canister_user_sync_queue.drain(..count).collect(); | ||
|
||
Some((state.data.identity_canister_id, batch)) | ||
} | ||
|
||
async fn sync_users(identity_canister_id: CanisterId, users: Vec<(Principal, Option<UserId>)>) { | ||
let args = identity_canister::c2c_set_user_ids::Args { users: users.clone() }; | ||
let success = identity_canister_c2c_client::c2c_set_user_ids(identity_canister_id, &args) | ||
.await | ||
.is_ok(); | ||
|
||
mutate_state(|state| { | ||
if !success { | ||
state.data.identity_canister_user_sync_queue.extend(users); | ||
} | ||
start_job_if_required(state); | ||
}); | ||
} |
Oops, something went wrong.