Skip to content

Commit

Permalink
chore: merge main
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <david.dalbusco@dfinity.org>
  • Loading branch information
peterpeterparker committed Nov 21, 2024
2 parents 4279b53 + e662b54 commit dea284f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 10 deletions.
21 changes: 11 additions & 10 deletions packages/ic-management/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const { status, memory_size, ...rest } = await canisterStatus(YOUR_CANISTER_ID);
- [fetchCanisterLogs](#gear-fetchcanisterlogs)
- [takeCanisterSnapshot](#gear-takecanistersnapshot)
- [listCanisterSnapshots](#gear-listcanistersnapshots)
- [deleteCanisterSnapshot](#gear-deletecanistersnapshot)
- [loadCanisterSnapshot](#gear-loadcanistersnapshot)

##### :gear: create

Expand Down Expand Up @@ -281,21 +281,22 @@ Parameters:

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L405)

##### :gear: deleteCanisterSnapshot
##### :gear: loadCanisterSnapshot

Deletes a specific snapshot of a canister.
Loads a snapshot of a canister's state.

| Method | Type |
| ------------------------ | --------------------------------------------------------------------------------------------------------------- |
| `deleteCanisterSnapshot` | `({ canisterId, snapshotId, }: { canisterId: Principal; snapshotId: string or snapshot_id; }) => Promise<void>` |
| Method | Type |
| ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `loadCanisterSnapshot` | `({ canisterId, snapshotId, senderCanisterVersion, }: { canisterId: Principal; snapshotId: string or snapshot_id; senderCanisterVersion?: bigint or undefined; }) => Promise<void>` |

Parameters:

- `params`: - Parameters for the deletion operation.
- `params.canisterId`: - The ID of the canister for which the snapshot will be deleted.
- `params.snapshotId`: - The ID of the snapshot to delete.
- `params`: - Parameters for the snapshot loading operation.
- `params.canisterId`: - The ID of the canister for which the snapshot will be loaded.
- `params.snapshotId`: - The ID of the snapshot to load.
- `params.senderCanisterVersion`: - The optional sender canister version. If provided, its value must be equal to ic0.canister_version.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L430)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L431)

<!-- TSDOC_END -->

Expand Down
79 changes: 79 additions & 0 deletions packages/ic-management/src/ic-management.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,85 @@ describe("ICManagementCanister", () => {
});
});

describe("loadCanisterSnapshot", () => {
it("should call load_canister_snapshot with Uint8Array snapshotId", async () => {
const service = mock<IcManagementService>();
service.load_canister_snapshot.mockResolvedValue(undefined);

const icManagement = await createICManagement(service);

const params = {
canisterId: mockCanisterId,
snapshotId: Uint8Array.from([1, 2, 3, 4]),
};

await icManagement.loadCanisterSnapshot(params);

expect(service.load_canister_snapshot).toHaveBeenCalledWith({
canister_id: params.canisterId,
snapshot_id: params.snapshotId,
sender_canister_version: [],
});
});

it("should call load_canister_snapshot with string snapshotId", async () => {
const service = mock<IcManagementService>();
service.load_canister_snapshot.mockResolvedValue(undefined);

const icManagement = await createICManagement(service);

const params = {
canisterId: mockCanisterId,
snapshotId: "000000000000000201010000000000000001",
};

await icManagement.loadCanisterSnapshot(params);

expect(service.load_canister_snapshot).toHaveBeenCalledWith({
canister_id: params.canisterId,
snapshot_id: decodeSnapshotId(params.snapshotId),
sender_canister_version: [],
});
});

it("should call load_canister_snapshot with senderCanisterVersion", async () => {
const service = mock<IcManagementService>();
service.load_canister_snapshot.mockResolvedValue(undefined);

const icManagement = await createICManagement(service);

const params = {
canisterId: mockCanisterId,
snapshotId: Uint8Array.from([1, 2, 3, 4]),
senderCanisterVersion: 5n,
};

await icManagement.loadCanisterSnapshot(params);

expect(service.load_canister_snapshot).toHaveBeenCalledWith({
canister_id: params.canisterId,
snapshot_id: params.snapshotId,
sender_canister_version: [params.senderCanisterVersion],
});
});

it("should throw an Error if load_canister_snapshot fails", async () => {
const error = new Error("Test");
const service = mock<IcManagementService>();
service.load_canister_snapshot.mockRejectedValue(error);

const icManagement = await createICManagement(service);

const call = () =>
icManagement.loadCanisterSnapshot({
canisterId: mockCanisterId,
snapshotId: Uint8Array.from([1, 2, 3, 4]),
});

await expect(call).rejects.toThrow(error);
});
});

describe("deleteCanisterSnapshot", () => {
it("should call delete_canister_snapshot with Uint8Array snapshotId", async () => {
const service = mock<IcManagementService>();
Expand Down
32 changes: 32 additions & 0 deletions packages/ic-management/src/ic-management.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,38 @@ export class ICManagementCanister {
});
};

/**
* Loads a snapshot of a canister's state.
*
* @link https://internetcomputer.org/docs/current/references/ic-interface-spec#ic-load_canister_snapshot
*
* @param {Object} params - Parameters for the snapshot loading operation.
* @param {Principal} params.canisterId - The ID of the canister for which the snapshot will be loaded.
* @param {snapshot_id} params.snapshotId - The ID of the snapshot to load.
* @param {BigInt} [params.senderCanisterVersion] - The optional sender canister version. If provided, its value must be equal to ic0.canister_version.
*
* @returns {Promise<void>} A promise that resolves when the snapshot is successfully loaded.
*
* @throws {Error} If the snapshot loading operation fails.
*/
loadCanisterSnapshot = async ({
canisterId,
snapshotId,
senderCanisterVersion,
}: {
canisterId: Principal;
snapshotId: SnapshotIdText | snapshot_id;
senderCanisterVersion?: bigint;
}): Promise<void> => {
const { load_canister_snapshot } = this.service;

await load_canister_snapshot({
canister_id: canisterId,
snapshot_id: mapSnapshotId(snapshotId),
sender_canister_version: toNullable(senderCanisterVersion),
});
};

/**
* Deletes a specific snapshot of a canister.
*
Expand Down

0 comments on commit dea284f

Please sign in to comment.