Skip to content

Commit

Permalink
Add config endpoint (#2602)
Browse files Browse the repository at this point in the history
Adds an endpoint to query the currently active config
(i.e. `InternetIdentityInit`).
  • Loading branch information
frederikrothenberger authored Sep 11, 2024
1 parent f4a3980 commit edd4b57
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/canister_tests/src/api/internet_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,5 +355,12 @@ pub fn acknowledge_entries(
)
}

pub fn config(
env: &PocketIc,
canister_id: CanisterId,
) -> Result<types::InternetIdentityInit, CallError> {
call_candid(env, canister_id, RawEffectivePrincipal::None, "config", ()).map(|(x,)| x)
}

/// A "compatibility" module for the previous version of II to handle API changes.
pub mod compat {}
1 change: 1 addition & 0 deletions src/frontend/generated/internet_identity_idl.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ export const idlFactory = ({ IDL }) => {
[IDL.Variant({ 'Ok' : Challenge, 'Err' : IDL.Null })],
[],
),
'config' : IDL.Func([], [InternetIdentityInit], ['query']),
'create_challenge' : IDL.Func([], [Challenge], []),
'deploy_archive' : IDL.Func([IDL.Vec(IDL.Nat8)], [DeployArchiveResult], []),
'enter_device_registration_mode' : IDL.Func([UserNumber], [Timestamp], []),
Expand Down
1 change: 1 addition & 0 deletions src/frontend/generated/internet_identity_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ export interface _SERVICE {
{ 'Err' : AuthnMethodSecuritySettingsReplaceError }
>,
'captcha_create' : ActorMethod<[], { 'Ok' : Challenge } | { 'Err' : null }>,
'config' : ActorMethod<[], InternetIdentityInit>,
'create_challenge' : ActorMethod<[], Challenge>,
'deploy_archive' : ActorMethod<[Uint8Array | number[]], DeployArchiveResult>,
'enter_device_registration_mode' : ActorMethod<[UserNumber], Timestamp>,
Expand Down
1 change: 1 addition & 0 deletions src/internet_identity/internet_identity.did
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ service : (opt InternetIdentityInit) -> {
// ================
init_salt: () -> ();
stats : () -> (InternetIdentityStats) query;
config : () -> (InternetIdentityInit) query;

deploy_archive: (wasm: blob) -> (DeployArchiveResult);
// Returns a batch of entries _sorted by sequence number_ to be archived.
Expand Down
20 changes: 20 additions & 0 deletions src/internet_identity/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::anchor_management::tentative_device_registration::{
};
use crate::archive::ArchiveState;
use crate::assets::init_assets;
use crate::state::persistent_state;
use crate::stats::event_stats::all_aggregations_top_n;
use authz_utils::{
anchor_operation_with_authz_check, check_authorization, check_authz_and_record_activity,
Expand Down Expand Up @@ -328,6 +329,25 @@ fn stats() -> InternetIdentityStats {
})
}

#[query]
fn config() -> InternetIdentityInit {
let archive_config = match state::archive_state() {
ArchiveState::NotConfigured => None,
ArchiveState::Configured { config } | ArchiveState::CreationInProgress { config, .. } => {
Some(config)
}
ArchiveState::Created { config, .. } => Some(config),
};
let user_range = state::storage_borrow(|s| s.assigned_anchor_number_range());
persistent_state(|persistent_state| InternetIdentityInit {
assigned_user_number_range: Some(user_range),
archive_config,
canister_creation_cycles_cost: Some(persistent_state.canister_creation_cycles_cost),
register_rate_limit: Some(persistent_state.registration_rate_limit.clone()),
max_inflight_captchas: Some(persistent_state.max_inflight_captchas),
})
}

#[update]
async fn deploy_archive(wasm: ByteBuf) -> DeployArchiveResult {
archive::deploy_archive(wasm).await
Expand Down
31 changes: 31 additions & 0 deletions src/internet_identity/tests/integration/conifg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use canister_tests::api::internet_identity as api;
use canister_tests::framework::{env, install_ii_canister_with_arg, II_WASM};
use internet_identity_interface::internet_identity::types::{
ArchiveConfig, InternetIdentityInit, RateLimitConfig,
};
use pocket_ic::CallError;

#[test]
fn should_retain_anchor_on_user_range_change() -> Result<(), CallError> {
let env = env();
let config = InternetIdentityInit {
assigned_user_number_range: Some((3456, 798977)),
archive_config: Some(ArchiveConfig {
module_hash: [17; 32],
entries_buffer_limit: 123789,
polling_interval_ns: 659871258,
entries_fetch_limit: 33,
}),
canister_creation_cycles_cost: Some(123),
register_rate_limit: Some(RateLimitConfig {
time_per_token_ns: 99,
max_tokens: 874,
}),
max_inflight_captchas: Some(456),
};

let canister_id = install_ii_canister_with_arg(&env, II_WASM.clone(), Some(config.clone()));

assert_eq!(api::config(&env, canister_id)?, config);
Ok(())
}
1 change: 1 addition & 0 deletions src/internet_identity/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod activity_stats;
mod aggregation_stats;
mod anchor_management;
mod archive_integration;
mod conifg;
mod delegation;
mod http;
mod rollback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub struct AnchorCredentials {
pub recovery_phrases: Vec<PublicKey>,
}

#[derive(Clone, Debug, CandidType, Deserialize, Default)]
#[derive(Clone, Debug, CandidType, Deserialize, Default, Eq, PartialEq)]
pub struct InternetIdentityInit {
pub assigned_user_number_range: Option<(AnchorNumber, AnchorNumber)>,
pub archive_config: Option<ArchiveConfig>,
Expand Down
2 changes: 1 addition & 1 deletion src/vc-api/src/generated/vc_issuer_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ export interface _SERVICE {
{ 'Ok' : PreparedCredentialData } |
{ 'Err' : IssueCredentialError }
>,
'set_derivation_origin' : ActorMethod<[string, string], undefined>,
'set_alternative_origins' : ActorMethod<[string], undefined>,
'set_derivation_origin' : ActorMethod<[string, string], undefined>,
'vc_consent_message' : ActorMethod<
[Icrc21VcConsentMessageRequest],
{ 'Ok' : Icrc21ConsentInfo } |
Expand Down

0 comments on commit edd4b57

Please sign in to comment.