Skip to content

Commit

Permalink
feat: upgrade observatory
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker committed Aug 24, 2024
1 parent 96de091 commit 1a95dba
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 57 deletions.
62 changes: 5 additions & 57 deletions scripts/console.upgrade.mjs
Original file line number Diff line number Diff line change
@@ -1,61 +1,9 @@
#!/usr/bin/env node

import { IDL } from '@dfinity/candid';
import { ICManagementCanister, InstallMode } from '@dfinity/ic-management';
import { fromNullable, uint8ArrayToHexString } from '@dfinity/utils';
import { fileExists } from '@junobuild/cli-tools';
import { createHash } from 'crypto';
import { readFile } from 'fs/promises';
import { join } from 'node:path';
import { localAgent } from './actor.mjs';
import { CONSOLE_ID } from './constants.mjs';
import { upgrade } from './module.upgrade.mjs';

const target = join(process.cwd(), 'target', 'deploy');

const loadGzippedWasm = async (destination) => {
const buffer = await readFile(destination);

return {
wasm: [...new Uint8Array(buffer)],
hash: createHash('sha256').update(buffer).digest('hex')
};
};

const agent = await localAgent();

const upgrade = async () => {
const sourceFilename = `console.wasm.gz`;
const source = join(target, sourceFilename);

if (!(await fileExists(source))) {
throw new Error(`${source} not found.`);
}

const EMPTY_ARG = IDL.encode([], []);

const { installCode, canisterStatus } = ICManagementCanister.create({
agent
});

const { wasm, hash } = await loadGzippedWasm(source);

const { module_hash } = await canisterStatus(CONSOLE_ID);

const currentHash = fromNullable(module_hash);

if (uint8ArrayToHexString(currentHash) === hash) {
console.log(`Module hash ${hash} already installed.`);
return;
}

await installCode({
mode: InstallMode.Upgrade,
canisterId: CONSOLE_ID,
wasmModule: wasm,
arg: new Uint8Array(EMPTY_ARG)
});

console.log(`Module upgraded to hash ${hash}.`);
};

await upgrade();
await upgrade({
sourceFilename: 'console.wasm.gz',
canisterId: CONSOLE_ID
});
61 changes: 61 additions & 0 deletions scripts/module.upgrade.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env node

import { IDL } from '@dfinity/candid';
import { ICManagementCanister, InstallMode } from '@dfinity/ic-management';
import { fromNullable, uint8ArrayToHexString } from '@dfinity/utils';
import { fileExists } from '@junobuild/cli-tools';
import { createHash } from 'crypto';
import { readFile } from 'fs/promises';
import { join } from 'node:path';
import { icAgent, localAgent } from './actor.mjs';
import { targetMainnet } from './utils.mjs';

const target = join(process.cwd(), 'target', 'deploy');

const loadGzippedWasm = async (destination) => {
const buffer = await readFile(destination);

return {
wasm: [...new Uint8Array(buffer)],
hash: createHash('sha256').update(buffer).digest('hex')
};
};

const fnAgent = targetMainnet() ? icAgent : localAgent;
const agent = await fnAgent();

export const upgrade = async ({ sourceFilename, canisterId }) => {
const source = join(target, sourceFilename);

console.log(`About to upgrade module ${canisterId} with source ${source}...`);

if (!(await fileExists(source))) {
throw new Error(`${source} not found.`);
}

const EMPTY_ARG = IDL.encode([], []);

const { installCode, canisterStatus } = ICManagementCanister.create({
agent
});

const { wasm, hash } = await loadGzippedWasm(source);

const { module_hash } = await canisterStatus(canisterId);

const currentHash = fromNullable(module_hash);

if (uint8ArrayToHexString(currentHash) === hash) {
console.log(`Module hash ${hash} already installed.`);
return;
}

await installCode({
mode: InstallMode.Upgrade,
canisterId,
wasmModule: wasm,
arg: new Uint8Array(EMPTY_ARG)
});

console.log(`Module upgraded to hash ${hash}.`);
};
9 changes: 9 additions & 0 deletions scripts/observatory.upgrade.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env node

import { OBSERVATORY_ID } from './constants.mjs';
import { upgrade } from './module.upgrade.mjs';

await upgrade({
sourceFilename: 'observatory.wasm.gz',
canisterId: OBSERVATORY_ID
});

0 comments on commit 1a95dba

Please sign in to comment.