Skip to content

Commit

Permalink
refactored pre_and_post_upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Sep 25, 2023
1 parent 4d300d1 commit ca71925
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
# "examples/null_example",
# "examples/optional_types",
# "examples/outgoing_http_requests",
# "examples/pre_and_post_upgrade",
# "examples/rejections",
# "examples/robust_imports",
# "examples/run_time_errors",
Expand Down Expand Up @@ -113,6 +112,7 @@ jobs:
"examples/list_of_lists",
"examples/management_canister",
"examples/manual_reply",
"examples/pre_and_post_upgrade",
"examples/primitive_types",
"examples/principal",
"examples/query",
Expand Down
7 changes: 4 additions & 3 deletions examples/pre_and_post_upgrade/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"type": "custom",
"build": "npx azle pre_and_post_upgrade",
"root": "src",
"ts": "src/pre_and_post_upgrade.ts",
"candid": "src/pre_and_post_upgrade.did",
"wasm": ".azle/pre_and_post_upgrade/pre_and_post_upgrade.wasm.gz",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/pre_and_post_upgrade/pre_and_post_upgrade.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/pre_and_post_upgrade",
"node_compatibility": true
Expand Down
6 changes: 6 additions & 0 deletions examples/pre_and_post_upgrade/src/index.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type rec_1 = record {key:text; value:nat64};
type rec_2 = record {key:text; value:nat64};
service: () -> {
setEntry: (rec_1) -> ();
getEntries: () -> (vec rec_2) query;
}
74 changes: 74 additions & 0 deletions examples/pre_and_post_upgrade/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
init,
nat64,
postUpgrade,
preUpgrade,
query,
Record,
Service,
StableBTreeMap,
text,
update,
Vec,
Void
} from 'azle';

const Entry = Record({
key: text,
value: nat64
});

let stableStorage = StableBTreeMap(text, Vec(Entry), 0);

let entries: {
[key: string]: nat64;
} = {};

export default Service({
init: init([], () => {
console.log('init');

stableStorage.insert('entries', []);
}),
postUpgrade: postUpgrade([], () => {
console.log('postUpgrade');

const stableEntriesOpt = stableStorage.get('entries');

const stableEntries =
stableEntriesOpt.length === 0
? stableEntriesOpt
: stableEntriesOpt[0];

entries = stableEntries.reduce((result, entry) => {
return {
...result,
[entry.key]: entry.value
};
}, {});
}),
preUpgrade: preUpgrade(() => {
console.log('preUpgrade');

stableStorage.insert(
'entries',
Object.entries(entries).map((entry) => {
return {
key: entry[0],
value: entry[1]
};
})
);
}),
setEntry: update([Entry], Void, (entry) => {
entries[entry.key] = entry.value;
}),
getEntries: query([], Vec(Entry), () => {
return Object.entries(entries).map((entry) => {
return {
key: entry[0],
value: entry[1]
};
});
})
});
6 changes: 0 additions & 6 deletions examples/pre_and_post_upgrade/src/pre_and_post_upgrade.did

This file was deleted.

90 changes: 0 additions & 90 deletions examples/pre_and_post_upgrade/src/pre_and_post_upgrade.ts

This file was deleted.

23 changes: 23 additions & 0 deletions src/lib_functional/candid/reference/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function Service<T extends ServiceOptions>(
// }
}, {});

// TODO Once types have names we should deduplicate the init and post_upgrade param types
const candidTypes = Object.values(serviceOptions).reduce(
(acc: string[], canisterMethodInfo) => {
return [...acc, ...canisterMethodInfo.candidTypes];
Expand All @@ -85,6 +86,26 @@ export function Service<T extends ServiceOptions>(
name: initOption[0]
};

const postUpgradeOption = Object.entries(serviceOptions).find(
([key, value]) => value.mode === 'postUpgrade'
);
const postUpgrade =
postUpgradeOption === undefined
? undefined
: {
name: postUpgradeOption[0]
};

const preUpgradeOption = Object.entries(serviceOptions).find(
([key, value]) => value.mode === 'preUpgrade'
);
const preUpgrade =
preUpgradeOption === undefined
? undefined
: {
name: preUpgradeOption[0]
};

const heartbeatOption = Object.entries(serviceOptions).find(
([key, value]) => value.mode === 'heartbeat'
);
Expand Down Expand Up @@ -199,6 +220,8 @@ export function Service<T extends ServiceOptions>(
`;

returnFunction.init = init;
returnFunction.postUpgrade = postUpgrade;
returnFunction.preUpgrade = preUpgrade;
returnFunction.heartbeat = heartbeat;
returnFunction.inspect_message = inspectMessage;
returnFunction.queries = queries;
Expand Down
16 changes: 15 additions & 1 deletion src/lib_functional/canister_methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ import {
export * from './heartbeat';
export * from './init';
export * from './inspect_message';
export * from './post_upgrade';
export * from './pre_upgrade';
export * from './query';
export * from './update';

export type CanisterMethodInfo<T extends ReadonlyArray<any>, K> = {
mode: 'query' | 'update' | 'init' | 'heartbeat' | 'inspectMessage';
mode:
| 'query'
| 'update'
| 'init'
| 'heartbeat'
| 'inspectMessage'
| 'postUpgrade'
| 'preUpgrade';
async: boolean;
callback?: (...args: any) => any;
candid: string;
Expand Down Expand Up @@ -53,6 +62,11 @@ export function executeMethod(
return;
}

if (mode === 'preUpgrade') {
callback();
return;
}

const decoded = IDL.decode(paramCandid[0] as any, args[0]);

const myDecodedObject = paramCandid[0].map((idl: any, index: any) => {
Expand Down
48 changes: 48 additions & 0 deletions src/lib_functional/canister_methods/post_upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
handleRecursiveParams,
handleRecursiveReturn,
newTypesToStingArr
} from '../../lib_new/method_decorators';
import { Callback, CanisterMethodInfo, executeMethod } from '.';
import { TypeMapping } from '../candid';
import { Void } from '../../lib_new';

export function postUpgrade<
const Params extends ReadonlyArray<any>,
GenericCallback extends Callback<Params, Void>
>(
paramsIdls: Params,
callback?: Awaited<ReturnType<GenericCallback>> extends TypeMapping<Void>
? GenericCallback
: never
): CanisterMethodInfo<Params, Void> {
const paramCandid = handleRecursiveParams(paramsIdls as any);
const returnCandid = handleRecursiveReturn(Void as any, paramCandid[2]);

const finalCallback =
callback === undefined
? undefined
: (...args: any[]) => {
executeMethod(
'postUpgrade',
paramCandid,
returnCandid,
args,
callback,
paramsIdls as any,
Void,
false
);
};

return {
mode: 'postUpgrade',
callback: finalCallback,
candid: paramCandid[1].join(', '),
candidTypes: newTypesToStingArr(returnCandid[2]),
paramsIdls: paramsIdls as any,
returnIdl: Void,
async: false,
guard: undefined
};
}
31 changes: 31 additions & 0 deletions src/lib_functional/canister_methods/pre_upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { isAsync } from '../../lib_new/method_decorators';
import { CanisterMethodInfo, executeMethod } from '.';
import { Void } from '../../lib_new';

export function preUpgrade(
callback: () => void | Promise<void>
): CanisterMethodInfo<[], Void> {
const finalCallback = (...args: any[]) => {
executeMethod(
'preUpgrade',
undefined,
undefined,
args,
callback,
[],
Void,
false
);
};

return {
mode: 'preUpgrade',
callback: finalCallback,
candid: '',
candidTypes: [],
paramsIdls: [],
returnIdl: Void,
async: isAsync(callback),
guard: undefined
};
}

0 comments on commit ca71925

Please sign in to comment.