From 1587ba47649097e66850f3a06c300ca2f79c5dc9 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Mon, 25 Sep 2023 08:19:56 -0500 Subject: [PATCH] refactored guard_functions --- .github/workflows/test.yml | 2 +- examples/guard_functions/dfx.json | 3 +- examples/guard_functions/src/index.ts | 157 ++++++++++-------- .../candid/reference/service.ts | 13 +- src/lib_functional/canister_methods/index.ts | 1 + src/lib_functional/canister_methods/query.ts | 3 +- src/lib_functional/canister_methods/update.ts | 3 +- src/lib_new/method_decorators.ts | 2 +- 8 files changed, 108 insertions(+), 76 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 50a4ca851c..c6d6fcfd80 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/guard_functions", # "examples/heartbeat", # "examples/ic_api", # "examples/imports", @@ -114,6 +113,7 @@ jobs: "examples/cycles", "examples/date", "examples/ethereum_json_rpc", + "examples/guard_functions", "examples/primitive_types", "examples/principal", "examples/query", diff --git a/examples/guard_functions/dfx.json b/examples/guard_functions/dfx.json index f32f3a1ddf..4e8af29a65 100644 --- a/examples/guard_functions/dfx.json +++ b/examples/guard_functions/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/guard_functions/guard_functions.wasm.gz", + "wasm": ".azle/guard_functions/guard_functions.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/guard_functions", "node_compatibility": true diff --git a/examples/guard_functions/src/index.ts b/examples/guard_functions/src/index.ts index 4a40dd658c..17971544c9 100644 --- a/examples/guard_functions/src/index.ts +++ b/examples/guard_functions/src/index.ts @@ -13,76 +13,97 @@ export let state = { heartbeatTick: 0 }; -export default class extends Service { - @query([], nat32) - getCounter(): nat32 { +export default Service({ + getCounter: query([], nat32, () => { return state.counter; - } - - @query([], bool) - identifierAnnotation(): bool { + }), + identifierAnnotation: query([], bool, () => { console.log('identifierAnnotation called'); return true; - } - - @query([], bool) - callExpressionWithoutOptionsObject(): bool { + }), + callExpressionWithoutOptionsObject: query([], bool, () => { console.log('callExpressionWithoutOptionsObject called'); return true; - } - - @query([], bool, {}) - callExpressionWithEmptyOptionsObject(): bool { - console.log('callExpressionWithEmptyOptionsObject called'); - return true; - } - - @query([], bool, { guard: allowAll }) - looselyGuarded(): bool { - console.log('looselyGuarded called'); - return true; - } - - @query([], bool, { manual: true, guard: allowAll }) - looselyGuardedManual(): Manual { - console.log('looselyGuardedManual called'); - ic.reply(true, bool); - } - - // prettier-ignore - @query([], bool, { "guard": allowAll }) - looselyGuardedWithGuardOptionKeyAsString(): bool { - console.log('looselyGuardedWithGuardOptionKeyAsString called'); - return true; - } - - @update([], bool, { guard: incrementCounterAndAllowAll }) - modifyStateGuarded(): bool { - console.log('modifyStateGuarded called'); - return true; - } - - @query([], bool, { guard: unpassable }) - tightlyGuarded(): bool { - console.log('tightlyGuarded called'); - return true; - } - - @query([], bool, { guard: throwString }) - errorStringGuarded(): bool { - console.log('errorStringGuarded called'); - return true; - } - - @query([], bool, { guard: throwCustomError }) - customErrorGuarded(): bool { - console.log('customErrorGuarded called'); - return true; - } - - @query([], bool, { guard: returnNonStringErrValue }) - nonStringErrValueGuarded(): bool { - console.log('nonStringErrValueGuarded called'); - return true; - } -} + }), + callExpressionWithEmptyOptionsObject: query( + [], + bool, + () => { + console.log('callExpressionWithEmptyOptionsObject called'); + return true; + }, + {} + ), + looselyGuarded: query( + [], + bool, + () => { + console.log('looselyGuarded called'); + return true; + }, + { guard: allowAll } + ), + looselyGuardedManual: query( + [], + Manual(bool), + () => { + console.log('looselyGuardedManual called'); + ic.reply(true, bool); + }, + { manual: true, guard: allowAll } + ), + looselyGuardedWithGuardOptionKeyAsString: query( + [], + bool, + () => { + console.log('looselyGuardedWithGuardOptionKeyAsString called'); + return true; + }, + { guard: allowAll } + ), + modifyStateGuarded: update( + [], + bool, + () => { + console.log('modifyStateGuarded called'); + return true; + }, + { guard: incrementCounterAndAllowAll } + ), + tightlyGuarded: query( + [], + bool, + () => { + console.log('tightlyGuarded called'); + return true; + }, + { guard: unpassable } + ), + errorStringGuarded: query( + [], + bool, + () => { + console.log('errorStringGuarded called'); + return true; + }, + { guard: throwString } + ), + customErrorGuarded: query( + [], + bool, + () => { + console.log('customErrorGuarded called'); + return true; + }, + { guard: throwCustomError } + ), + nonStringErrValueGuarded: query( + [], + bool, + () => { + console.log('nonStringErrValueGuarded called'); + return true; + }, + { guard: returnNonStringErrValue } + ) +}); diff --git a/src/lib_functional/candid/reference/service.ts b/src/lib_functional/candid/reference/service.ts index 7b675cd7c8..bfb7cfc96e 100644 --- a/src/lib_functional/candid/reference/service.ts +++ b/src/lib_functional/candid/reference/service.ts @@ -1,5 +1,10 @@ import { Principal, TypeMapping } from '../../'; -import { IDL, ServiceFunctionInfo, serviceCall } from '../../../lib_new'; +import { + IDL, + ServiceFunctionInfo, + createGlobalGuard, + serviceCall +} from '../../../lib_new'; import { Parent, toParamIDLTypes, @@ -93,7 +98,8 @@ export function Service( return { name: key, - composite: value.async + composite: value.async, + guard_name: createGlobalGuard(value.guard, key) }; }); @@ -109,7 +115,8 @@ export function Service( const value = entry[1]; return { - name: key + name: key, + guard_name: createGlobalGuard(value.guard, key) }; }); diff --git a/src/lib_functional/canister_methods/index.ts b/src/lib_functional/canister_methods/index.ts index d685dfce98..10f13fff17 100644 --- a/src/lib_functional/canister_methods/index.ts +++ b/src/lib_functional/canister_methods/index.ts @@ -18,6 +18,7 @@ export type CanisterMethodInfo, K> = { candidTypes: string[]; paramsIdls: any[]; returnIdl: any; + guard: (() => any) | undefined; }; export type Callback, Return> = ( diff --git a/src/lib_functional/canister_methods/query.ts b/src/lib_functional/canister_methods/query.ts index 2e9fe2b2a5..99ca0338d5 100644 --- a/src/lib_functional/canister_methods/query.ts +++ b/src/lib_functional/canister_methods/query.ts @@ -50,6 +50,7 @@ export function query< candidTypes: newTypesToStingArr(returnCandid[2]), paramsIdls: paramsIdls as any, returnIdl, - async: callback === undefined ? false : isAsync(callback) + async: callback === undefined ? false : isAsync(callback), + guard: methodArgs?.guard }; } diff --git a/src/lib_functional/canister_methods/update.ts b/src/lib_functional/canister_methods/update.ts index 1e167f7e6d..3f6f9e3c90 100644 --- a/src/lib_functional/canister_methods/update.ts +++ b/src/lib_functional/canister_methods/update.ts @@ -49,6 +49,7 @@ export function update< candidTypes: newTypesToStingArr(returnCandid[2]), paramsIdls: paramsIdls as any, returnIdl, - async: callback === undefined ? false : isAsync(callback) + async: callback === undefined ? false : isAsync(callback), + guard: methodArgs?.guard }; } diff --git a/src/lib_new/method_decorators.ts b/src/lib_new/method_decorators.ts index 90584095e6..78da74a66c 100644 --- a/src/lib_new/method_decorators.ts +++ b/src/lib_new/method_decorators.ts @@ -388,7 +388,7 @@ export function isAsync(originalFunction: any) { } } -function createGlobalGuard( +export function createGlobalGuard( guard: (() => GuardResult) | undefined, functionName: string ): string | undefined {