Skip to content

Commit

Permalink
Merge branch 'main' into feat/load-canister-snaptots
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Nov 21, 2024
2 parents ab98850 + 80badcf commit 051800d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
8 changes: 2 additions & 6 deletions packages/ic-management/src/ic-management.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type {
CanisterStatusResponse,
FetchCanisterLogsResponse,
} from "./types/ic-management.responses";
import { decodeSnapshotId } from "./utils/ic-management.utils";
import { mapSnapshotId } from "./utils/ic-management.utils";

export class ICManagementCanister {
private constructor(private readonly service: IcManagementService) {
Expand Down Expand Up @@ -384,11 +384,7 @@ export class ICManagementCanister {
return take_canister_snapshot({
canister_id: canisterId,
replace_snapshot: toNullable(
nonNullish(snapshotId)
? typeof snapshotId === "string"
? decodeSnapshotId(snapshotId)
: snapshotId
: undefined,
nonNullish(snapshotId) ? mapSnapshotId(snapshotId) : undefined,
),
});
};
Expand Down
4 changes: 4 additions & 0 deletions packages/ic-management/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export type {
canister_log_record,
canister_status_result,
chunk_hash,
definite_canister_settings,
fetch_canister_logs_result,
log_visibility,
snapshot_id,
} from "../candid/ic-management";
export { ICManagementCanister } from "./ic-management.canister";
Expand Down
19 changes: 18 additions & 1 deletion packages/ic-management/src/utils/ic-management.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { mockCanisterId } from "../ic-management.mock";
import { decodeSnapshotId, encodeSnapshotId } from "./ic-management.utils";
import {
decodeSnapshotId,
encodeSnapshotId,
mapSnapshotId,
} from "./ic-management.utils";

describe("ic-management.utils", () => {
const mockLocalSubnetId = [0, 0, 0, 0, 0, 0, 0, 1];
Expand Down Expand Up @@ -29,4 +33,17 @@ describe("ic-management.utils", () => {

expect(decoded).toEqual(mockSnapshotId);
});

describe("mapSnapshotId", () => {
it("should map a Uint8Array snapshot ID without changes", () => {
const result = mapSnapshotId(mockSnapshotId);

expect(result).toEqual(mockSnapshotId);
});

it("should map a string snapshot ID by decoding it", () => {
const result = mapSnapshotId(snapshotIdHex);
expect(result).toEqual(mockSnapshotId);
});
});
});
14 changes: 14 additions & 0 deletions packages/ic-management/src/utils/ic-management.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ export const encodeSnapshotId = (snapshotId: snapshot_id): SnapshotIdText =>
*/
export const decodeSnapshotId = (snapshotId: SnapshotIdText): snapshot_id =>
hexStringToUint8Array(snapshotId);

/**
* Maps a snapshot ID to the appropriate format for the IC interface.
*
* @param {SnapshotIdText | snapshot_id} snapshotId - The snapshot ID to map.
* It can either be a `string` (SnapshotIdText) or a `Uint8Array | number[]` (snapshot_id).
* If a `string` is provided, it is decoded into a `Uint8Array` using `decodeSnapshotId`.
*
* @returns {Uint8Array | number[]} The mapped snapshot ID.
*/
export const mapSnapshotId = (
snapshotId: SnapshotIdText | snapshot_id,
): snapshot_id =>
typeof snapshotId === "string" ? decodeSnapshotId(snapshotId) : snapshotId;

0 comments on commit 051800d

Please sign in to comment.