Skip to content

Commit ef2e4a8

Browse files
feat: migrate certificate
1 parent f6f51d5 commit ef2e4a8

File tree

6 files changed

+123
-2
lines changed

6 files changed

+123
-2
lines changed

src/console/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod rates;
1717
mod segments;
1818
mod store;
1919
mod types;
20+
mod upgrade;
2021

2122
use crate::types::interface::AuthenticationArgs;
2223
use crate::types::interface::AuthenticationResult;

src/console/src/memory/lifecycle.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::fees::init_factory_fees;
44
use crate::memory::manager::{get_memory_upgrades, init_stable_state, STATE};
55
use crate::rates::init::init_factory_rates;
66
use crate::types::state::{HeapState, ReleasesMetadata, State};
7+
use crate::upgrade::types::upgrade::UpgradeState;
78
use ciborium::{from_reader, into_writer};
89
use ic_cdk_macros::{init, post_upgrade, pre_upgrade};
910
use junobuild_shared::ic::api::caller;
@@ -50,9 +51,12 @@ fn post_upgrade() {
5051
let memory = get_memory_upgrades();
5152
let state_bytes = read_post_upgrade(&memory);
5253

53-
let state: State = from_reader(&*state_bytes)
54+
// TODO: remove once stable memory introduced on mainnet
55+
let upgrade_state: UpgradeState = from_reader(&*state_bytes)
5456
.expect("Failed to decode the state of the console in post_upgrade hook.");
5557

58+
let state: State = upgrade_state.into();
59+
5660
STATE.with(|s| *s.borrow_mut() = state);
5761

5862
defer_init_certified_assets();

src/console/src/upgrade/impls.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use crate::types::state::{HeapState, State};
2+
use crate::upgrade::types::upgrade::{
3+
UpgradeAuthenticationHeapState, UpgradeHeapState, UpgradeOpenIdProvider, UpgradeState,
4+
};
5+
use junobuild_auth::openid::types::provider::OpenIdProvider;
6+
use junobuild_auth::state::types::state::{AuthenticationHeapState, OpenIdState};
7+
8+
impl From<UpgradeState> for State {
9+
fn from(upgrade: UpgradeState) -> Self {
10+
State {
11+
stable: upgrade.stable,
12+
heap: upgrade.heap.into(),
13+
}
14+
}
15+
}
16+
17+
impl From<UpgradeHeapState> for HeapState {
18+
fn from(upgrade: UpgradeHeapState) -> Self {
19+
HeapState {
20+
authentication: upgrade.authentication.map(|auth| auth.into()),
21+
controllers: upgrade.controllers,
22+
mission_controls: upgrade.mission_controls,
23+
payments: upgrade.payments,
24+
invitation_codes: upgrade.invitation_codes,
25+
factory_fees: upgrade.factory_fees,
26+
factory_rates: upgrade.factory_rates,
27+
storage: upgrade.storage,
28+
releases_metadata: upgrade.releases_metadata,
29+
}
30+
}
31+
}
32+
33+
impl From<UpgradeAuthenticationHeapState> for AuthenticationHeapState {
34+
fn from(upgrade: UpgradeAuthenticationHeapState) -> Self {
35+
AuthenticationHeapState {
36+
config: upgrade.config,
37+
salt: upgrade.salt,
38+
openid: upgrade.openid.map(|openid_state| OpenIdState {
39+
certificates: openid_state
40+
.certificates
41+
.into_iter()
42+
.map(|(provider, cert)| (provider.into(), cert))
43+
.collect(),
44+
}),
45+
}
46+
}
47+
}
48+
49+
impl From<UpgradeOpenIdProvider> for OpenIdProvider {
50+
fn from(old: UpgradeOpenIdProvider) -> Self {
51+
match old {
52+
UpgradeOpenIdProvider::Google => OpenIdProvider::Google,
53+
UpgradeOpenIdProvider::GitHub => OpenIdProvider::GitHubAuth,
54+
}
55+
}
56+
}

src/console/src/upgrade/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod impls;
2+
pub mod types;

src/console/src/upgrade/types.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
pub mod upgrade {
2+
use crate::memory::manager::init_stable_state;
3+
use crate::types::state::{
4+
Accounts, FactoryFees, FactoryRates, IcpPayments, InvitationCodes, ReleasesMetadata,
5+
StableState,
6+
};
7+
use candid::{CandidType, Deserialize};
8+
use junobuild_auth::state::types::config::AuthenticationConfig;
9+
use junobuild_auth::state::types::state::{OpenIdCachedCertificate, Salt};
10+
use junobuild_shared::types::state::Controllers;
11+
use junobuild_storage::types::state::StorageHeapState;
12+
use serde::Serialize;
13+
use std::collections::HashMap;
14+
15+
#[derive(Serialize, Deserialize)]
16+
pub struct UpgradeState {
17+
// Direct stable state: State that is uses stable memory directly as its store. No need for pre/post upgrade hooks.
18+
#[serde(skip, default = "init_stable_state")]
19+
pub stable: StableState,
20+
21+
pub heap: UpgradeHeapState,
22+
}
23+
24+
#[derive(Default, CandidType, Serialize, Deserialize, Clone)]
25+
pub struct UpgradeHeapState {
26+
#[deprecated(note = "Deprecated. Use stable memory instead.")]
27+
pub mission_controls: Accounts,
28+
#[deprecated(note = "Deprecated. Use stable memory instead.")]
29+
pub payments: IcpPayments,
30+
pub invitation_codes: InvitationCodes,
31+
pub controllers: Controllers,
32+
pub factory_fees: Option<FactoryFees>,
33+
pub factory_rates: Option<FactoryRates>,
34+
pub storage: StorageHeapState,
35+
pub authentication: Option<UpgradeAuthenticationHeapState>,
36+
pub releases_metadata: ReleasesMetadata,
37+
}
38+
39+
#[derive(Default, CandidType, Serialize, Deserialize, Clone)]
40+
pub struct UpgradeAuthenticationHeapState {
41+
pub config: AuthenticationConfig,
42+
pub salt: Option<Salt>,
43+
pub openid: Option<UpgradeOpenIdState>,
44+
}
45+
46+
#[derive(Default, CandidType, Serialize, Deserialize, Clone)]
47+
pub struct UpgradeOpenIdState {
48+
pub certificates: HashMap<UpgradeOpenIdProvider, OpenIdCachedCertificate>,
49+
}
50+
51+
#[derive(
52+
CandidType, Serialize, Deserialize, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
53+
)]
54+
pub enum UpgradeOpenIdProvider {
55+
Google,
56+
GitHub,
57+
}
58+
}

src/tests/specs/console/upgrade/console.upgrade-v0-4-0.openid-provider.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,6 @@ describe('Console > Upgrade > OpenIdProvider > v0.3.3 -> v0.4.0', () => {
163163
});
164164

165165
it('should migrate OpenIdState.certificates to OpenIdProvider.GitHubAuth', async () => {
166-
await expect(upgradeCurrent()).not.toThrowError();
166+
await expect(upgradeCurrent()).resolves.not.toThrowError();
167167
});
168168
});

0 commit comments

Comments
 (0)