From 3c6b56a57105e20b48558363c56e2499cbf13b6f Mon Sep 17 00:00:00 2001 From: phn210 Date: Fri, 11 Oct 2024 18:26:09 +0200 Subject: [PATCH] update utils --- package-lock.json | 4 ++-- package.json | 2 +- src/utils/constants.ts | 8 ++++---- src/utils/index.ts | 1 + src/utils/network.ts | 4 ++-- src/utils/utils.test.ts | 37 +++++++++++++++++++++++++++++++++++++ src/utils/zkApp.ts | 14 ++++++++++++-- 7 files changed, 59 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 21789e5..9933618 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@auxo-dev/auxo-libs", - "version": "1.0.2", + "version": "1.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@auxo-dev/auxo-libs", - "version": "1.0.2", + "version": "1.0.3", "license": "Apache-2.0", "dependencies": { "o1js": "1.8.0" diff --git a/package.json b/package.json index c2428a3..143cd58 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@auxo-dev/auxo-libs", - "version": "1.0.2", + "version": "1.0.3", "description": "", "author": "", "license": "Apache-2.0", diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 68ae1fb..1b63a74 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -95,8 +95,8 @@ type ZkApp = { // eslint-disable-next-line @typescript-eslint/no-explicit-any initArgs?: Record; actionStates: Field[]; - actions: Field[][]; - events: Field[][]; + actionss: Field[][][]; + eventss: Field[][][]; }; type FeePayer = { @@ -134,8 +134,8 @@ type ZkAppOptions = { // eslint-disable-next-line @typescript-eslint/no-explicit-any initArgs?: Record; actionStates?: Field[]; - actions?: Field[][]; - events?: Field[][]; + actionss?: Field[][][]; + eventss?: Field[][][]; }; type UtilsOptions = { diff --git a/src/utils/index.ts b/src/utils/index.ts index cb7e268..c952942 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -36,6 +36,7 @@ export { packNumberArray, unpackNumberArray, buildAssertMessage, + checkInvalidAction, requireSignature, requireCaller, checkCondition, diff --git a/src/utils/network.ts b/src/utils/network.ts index 4b66ca5..6970888 100644 --- a/src/utils/network.ts +++ b/src/utils/network.ts @@ -79,8 +79,8 @@ function getZkApp( name: options.name || 'Unknown', initArgs: options.initArgs || {}, actionStates: options.actionStates || [Reducer.initialActionState], - actions: options.actions || [], - events: options.events || [], + actionss: options.actionss || [], + eventss: options.eventss || [], }; } diff --git a/src/utils/utils.test.ts b/src/utils/utils.test.ts index 1c81ba6..067245d 100644 --- a/src/utils/utils.test.ts +++ b/src/utils/utils.test.ts @@ -1,4 +1,5 @@ import { + Bool, Cache, Field, Mina, @@ -30,6 +31,7 @@ import { import { FeePayer, TX_FEE, ZkApp } from './constants.js'; import { getProfiler } from './benchmark.js'; import { fromUInt64ToScalar } from './math.js'; +import { checkInvalidAction } from './zkApp.js'; describe('Math', () => { it('Should convert UInt64 to Scalar', async () => { @@ -66,6 +68,17 @@ describe('Network', () => { this.reducer.dispatch(value1); this.emitEvent('test', value2); } + + @method + async checkAction(action: Field) { + let flag = Bool(false); + flag = checkInvalidAction( + flag, + action.equals(Field(1)), + 'Test error' + ); + Provable.log(flag); + } } const TestProgram = ZkProgram({ @@ -234,6 +247,30 @@ describe('Network', () => { ); }); + it('Should check and log invalid action', async () => { + await Mina.transaction( + { + sender: feePayer.sender.publicKey, + fee: feePayer.fee || TX_FEE, + memo: feePayer.memo, + nonce: feePayer.nonce, + }, + async () => + (testZkApp1.contract as TestContract).checkAction(Field(1)) + ); + + await Mina.transaction( + { + sender: feePayer.sender.publicKey, + fee: feePayer.fee || TX_FEE, + memo: feePayer.memo, + nonce: feePayer.nonce, + }, + async () => + (testZkApp1.contract as TestContract).checkAction(Field(0)) + ); + }); + it('Should fetch actions', async () => { let actions = await fetchActions( testZkApp1.key.publicKey, diff --git a/src/utils/zkApp.ts b/src/utils/zkApp.ts index b86a158..0b6556f 100644 --- a/src/utils/zkApp.ts +++ b/src/utils/zkApp.ts @@ -14,6 +14,7 @@ export { packNumberArray, unpackNumberArray, buildAssertMessage, + checkInvalidAction, requireSignature, requireCaller, checkCondition, @@ -45,9 +46,18 @@ function unpackNumberArray(packed: Field, maxSize: number): number[] { function buildAssertMessage( circuit: string, method: string, - errorEnum: string + errorMsg: string ): string { - return `${circuit}::${method}: ${errorEnum}`; + return `${circuit}::${method}: ${errorMsg}`; +} + +function checkInvalidAction(flag: Bool, check: Bool, message?: string) { + Provable.witness(Void, () => { + if (check.not().toBoolean()) { + console.info(message || 'Unknown error!'); + } + }); + return flag.or(check.not()); } function requireSignature(address: PublicKey) {