Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
bitdivine committed Aug 14, 2024
2 parents a7ed46c + 3cfd6ce commit 04c5ed6
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 37 deletions.
67 changes: 47 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ lazy_static = "1.4.0"
pocket-ic = "2.2.0"
pretty_assertions = "1.4.0"
bitcoin = "0.32.2"
strum = "0.26.3"
strum_macros = "0.26.4"
6 changes: 4 additions & 2 deletions src/backend/backend.did
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ type ListUsersResponse = record {
matches_max_length : nat64;
};
type MigrationProgress = variant {
MigratedUserTokensUpTo : principal;
MigratedUserTokensUpTo : opt principal;
MigratedUserTimestampsUpTo : opt principal;
TargetPreCheckOk;
MigratedCustomTokensUpTo : principal;
MigratedCustomTokensUpTo : opt principal;
Locked;
MigratedUserProfilesUpTo : opt record { nat64; principal };
CheckingTargetCanister;
TargetLocked;
Completed;
Expand Down
6 changes: 4 additions & 2 deletions src/declarations/backend/backend.did
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ type ListUsersResponse = record {
matches_max_length : nat64;
};
type MigrationProgress = variant {
MigratedUserTokensUpTo : principal;
MigratedUserTokensUpTo : opt principal;
MigratedUserTimestampsUpTo : opt principal;
TargetPreCheckOk;
MigratedCustomTokensUpTo : principal;
MigratedCustomTokensUpTo : opt principal;
Locked;
MigratedUserProfilesUpTo : opt record { nat64; principal };
CheckingTargetCanister;
TargetLocked;
Completed;
Expand Down
8 changes: 6 additions & 2 deletions src/declarations/backend/backend.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ export interface ListUsersResponse {
matches_max_length: bigint;
}
export type MigrationProgress =
| { MigratedUserTokensUpTo: Principal }
| {
MigratedUserTokensUpTo: [] | [Principal];
}
| { MigratedUserTimestampsUpTo: [] | [Principal] }
| { TargetPreCheckOk: null }
| { MigratedCustomTokensUpTo: Principal }
| { MigratedCustomTokensUpTo: [] | [Principal] }
| { Locked: null }
| { MigratedUserProfilesUpTo: [] | [[bigint, Principal]] }
| { CheckingTargetCanister: null }
| { TargetLocked: null }
| { Completed: null }
Expand Down
6 changes: 4 additions & 2 deletions src/declarations/backend/backend.factory.certified.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,12 @@ export const idlFactory = ({ IDL }) => {
matches_max_length: IDL.Nat64
});
const MigrationProgress = IDL.Variant({
MigratedUserTokensUpTo: IDL.Principal,
MigratedUserTokensUpTo: IDL.Opt(IDL.Principal),
MigratedUserTimestampsUpTo: IDL.Opt(IDL.Principal),
TargetPreCheckOk: IDL.Null,
MigratedCustomTokensUpTo: IDL.Principal,
MigratedCustomTokensUpTo: IDL.Opt(IDL.Principal),
Locked: IDL.Null,
MigratedUserProfilesUpTo: IDL.Opt(IDL.Tuple(IDL.Nat64, IDL.Principal)),
CheckingTargetCanister: IDL.Null,
TargetLocked: IDL.Null,
Completed: IDL.Null,
Expand Down
6 changes: 4 additions & 2 deletions src/declarations/backend/backend.factory.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,12 @@ export const idlFactory = ({ IDL }) => {
matches_max_length: IDL.Nat64
});
const MigrationProgress = IDL.Variant({
MigratedUserTokensUpTo: IDL.Principal,
MigratedUserTokensUpTo: IDL.Opt(IDL.Principal),
MigratedUserTimestampsUpTo: IDL.Opt(IDL.Principal),
TargetPreCheckOk: IDL.Null,
MigratedCustomTokensUpTo: IDL.Principal,
MigratedCustomTokensUpTo: IDL.Opt(IDL.Principal),
Locked: IDL.Null,
MigratedUserProfilesUpTo: IDL.Opt(IDL.Tuple(IDL.Nat64, IDL.Principal)),
CheckingTargetCanister: IDL.Null,
TargetLocked: IDL.Null,
Completed: IDL.Null,
Expand Down
12 changes: 12 additions & 0 deletions src/frontend/src/lib/api/backend.api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
AddUserCredentialError,
BitcoinNetwork,
CredentialSpec,
CustomToken,
GetUserProfileError,
Expand All @@ -14,6 +15,17 @@ import type { Identity } from '@dfinity/agent';
import type { Principal } from '@dfinity/principal';
import { toNullable, type QueryParams } from '@dfinity/utils';

export const getBtcAddress = async ({
identity,
network
}: {
identity: OptionIdentity;
network: BitcoinNetwork;
}): Promise<EthAddress> => {
const { caller_btc_address } = await getBackendActor({ identity });
return caller_btc_address(network);
};

export const getEthAddress = async (identity: OptionIdentity): Promise<EthAddress> => {
const { caller_eth_address } = await getBackendActor({ identity });
return caller_eth_address();
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/lib/types/address.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type BtcAddress = string;

export type EthAddress = string;

export type OptionEthAddress = EthAddress | undefined | null;
2 changes: 2 additions & 0 deletions src/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ ic-metrics-encoder = { workspace = true }
ic-verifiable-credentials = { workspace = true }
serde = { workspace = true }
serde_bytes = { workspace = true }
strum = { workspace = true }
strum_macros = { workspace = true }

65 changes: 63 additions & 2 deletions src/shared/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use crate::types::user_profile::{
AddUserCredentialError, OisyUser, StoredUserProfile, UserCredential, UserProfile,
};
use crate::types::{
ApiEnabled, Config, CredentialType, InitArg, Migration, MigrationReport, Timestamp,
TokenVersion, Version,
ApiEnabled, Config, CredentialType, InitArg, Migration, MigrationProgress, MigrationReport,
Timestamp, TokenVersion, Version,
};
use candid::Principal;
use ic_canister_sig_creation::{extract_raw_root_pk_from_der, IC_ROOT_PK_DER};
use std::collections::BTreeMap;
use std::fmt;
#[cfg(test)]
use strum::IntoEnumIterator;

impl From<&Token> for CustomTokenId {
fn from(token: &Token) -> Self {
Expand Down Expand Up @@ -213,3 +215,62 @@ fn test_api_enabled() {
assert_eq!(ApiEnabled::Disabled.readable(), false);
assert_eq!(ApiEnabled::Disabled.writable(), false);
}

impl MigrationProgress {
/// The next phase in the migration process.
///
/// Note: A given phase, such as migrating a `BTreeMap`, may need multiple steps.
/// The code for that phase will have to keep track of those steps by means of the data in the variant.
///
/// Prior art:
/// - There is an `enum_iterator` crate, however it deals only with simple enums
/// without variant fields. In this implementation, `next()` always uses the default value for
/// the new field, which is always None. `next()` does NOT step through the values of the
/// variant field.
/// - `strum` has the `EnumIter` derive macro, but that implements `.next()` on an iterator, not on the
/// enum itself, so stepping from one variant to the next is not straightforward.
///
/// Note: The next state after Completed is Completed, so the the iterator will run
/// indefinitely. In our case returning an option and ending with None would be fine but needs
/// additional code that we don't need.
#[must_use]
pub fn next(&self) -> Self {
match self {
MigrationProgress::Pending => MigrationProgress::Locked,
MigrationProgress::Locked => MigrationProgress::TargetLocked,
MigrationProgress::TargetLocked => MigrationProgress::TargetPreCheckOk,
MigrationProgress::TargetPreCheckOk => MigrationProgress::MigratedUserTokensUpTo(None),
MigrationProgress::MigratedUserTokensUpTo(_) => {
MigrationProgress::MigratedCustomTokensUpTo(None)
}
MigrationProgress::MigratedCustomTokensUpTo(_) => {
MigrationProgress::MigratedUserTimestampsUpTo(None)
}
MigrationProgress::MigratedUserTimestampsUpTo(_) => {
MigrationProgress::MigratedUserProfilesUpTo(None)
}
MigrationProgress::MigratedUserProfilesUpTo(_) => {
MigrationProgress::CheckingTargetCanister
}
MigrationProgress::CheckingTargetCanister | MigrationProgress::Completed => {
MigrationProgress::Completed
}
}
}
}

// `MigrationProgress::next(&self)` should list all the elements in the enum in order, but stop at Completed.
#[test]
fn next_matches_strum_iter() {
let mut iter = MigrationProgress::iter();
let mut next = MigrationProgress::Pending;
while next != MigrationProgress::Completed {
assert_eq!(iter.next(), Some(next), "iter.next() != Some(next)");
next = next.next();
}
assert_eq!(
next,
next.next(),
"Once completed, it should stay completed"
);
}
Loading

0 comments on commit 04c5ed6

Please sign in to comment.