-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David Dal Busco <[email protected]>
- Loading branch information
1 parent
96de091
commit 1a95dba
Showing
3 changed files
with
75 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}.`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |