From 962a614e3b19a8d13537cbd627ccf0d7e8b422ce Mon Sep 17 00:00:00 2001 From: Norman Date: Sun, 24 Mar 2024 14:21:50 +0800 Subject: [PATCH] feat: add requireImageDetails in prove & PROVER_RPC_CONSTANTS & beautify tests --- src/api/prove.ts | 14 ++++++-- src/api/publish.ts | 12 +++---- src/api/setup.ts | 25 +++++++++---- src/api/verify.ts | 6 ++-- src/common/constants.ts | 14 +++++++- src/common/error.ts | 12 +++++++ src/requests/zkwasm_deploy.ts | 2 ++ src/requests/zkwasm_imagedetails.ts | 35 +++++++++++++++++-- src/requests/zkwasm_taskdetails.ts | 19 ---------- .../{cle-event.yaml => cle.yaml} | 8 ++--- .../{cle-event.yaml => cle.yaml} | 8 ++--- .../{cle-event.yaml => cle.yaml} | 8 ++--- .../{cle-event.yaml => cle.yaml} | 8 ++--- tests/fixureoptions.ts | 2 +- 14 files changed, 112 insertions(+), 61 deletions(-) rename tests/fixtures/dsp/ethereum(event)/{cle-event.yaml => cle.yaml} (80%) rename tests/fixtures/dsp/ethereum(storage)/{cle-event.yaml => cle.yaml} (84%) rename tests/fixtures/dsp/ethereum.unsafe-ethereum/{cle-event.yaml => cle.yaml} (88%) rename tests/fixtures/dsp/ethereum.unsafe/{cle-event.yaml => cle.yaml} (81%) diff --git a/src/api/prove.ts b/src/api/prove.ts index 6b36152..0a9fa7b 100644 --- a/src/api/prove.ts +++ b/src/api/prove.ts @@ -7,7 +7,8 @@ import { import type { BatchOption, CLEExecutable, ProofParams, SingableProver } from '../types' import { BatchStyle } from '../types' import { logger } from '../common' -import { FinishStatusList } from '../common/constants' +import { PROVER_RPC_CONSTANTS } from '../common/constants' +import { requireImageDetails } from './setup' export type ProveOptions = SingableProver & BatchOption @@ -46,6 +47,9 @@ export async function requestProve( const md5 = ZkWasmUtil.convertToMd5(wasmUint8Array).toUpperCase() logger.log(`[*] IMAGE MD5: ${md5}`, '\n') + // require image exist & valid + await requireImageDetails(options.proverUrl, md5) + let taskId = '' // taskId must be set to response.data.result.id later const response = await ora_prove(md5, input, options) taskId = response.data.result.id @@ -73,7 +77,11 @@ export async function waitProve( options: BatchOption = {}, ): Promise { const { batchStyle = BatchStyle.ZKWASMHUB } = options - const task = await waitTaskStatus(proverUrl, proveTaskId, FinishStatusList, 3000, 0) + const task = await waitTaskStatus( + proverUrl, + proveTaskId, + PROVER_RPC_CONSTANTS.TASK_STATUS_PROVE_FINISH_LIST, + 3000, 0) // .catch((err) => { // throw err // }) // TODO: timeout @@ -84,7 +92,7 @@ export async function waitProve( taskDetails: task, } - if (task.status === 'Done') { + if (task.status === PROVER_RPC_CONSTANTS.TASK_STATUS_DONE) { const proofParams: ProofParams = { aggregate_proof: task.proof, batch_instances: task.batch_instances, diff --git a/src/api/publish.ts b/src/api/publish.ts index ce8c8e7..938a1c7 100644 --- a/src/api/publish.ts +++ b/src/api/publish.ts @@ -9,8 +9,8 @@ import { } from '../common/constants' import { CLEAlreadyExist, DSPNotFound, TxFailed } from '../common/error' import { dspHub } from '../dsp/hub' -import { zkwasm_imagedetails } from '../requests/zkwasm_imagedetails' import type { CLEExecutable, CLEYaml } from '../types' +import { requireImageDetails } from './setup' export interface PublishOptions { proverUrl?: string @@ -153,12 +153,8 @@ export async function getImageCommitment( ) { const { wasmUint8Array } = cleExecutable const md5 = ZkWasmUtil.convertToMd5(wasmUint8Array).toLowerCase() - const details = await zkwasm_imagedetails(proverUrl, md5) - const result = details[0]?.data.result[0] - if (result === null) - throw new Error('Can\'t find zkWasm image details, please finish setup before publish.') - - const pointX = littleEndianToUint256(result.checksum.x) - const pointY = littleEndianToUint256(result.checksum.y) + const details = await requireImageDetails(proverUrl, md5) + const pointX = littleEndianToUint256(details.checksum.x) + const pointY = littleEndianToUint256(details.checksum.y) return { pointX, pointY } } diff --git a/src/api/setup.ts b/src/api/setup.ts index 344e570..0c02376 100644 --- a/src/api/setup.ts +++ b/src/api/setup.ts @@ -2,12 +2,12 @@ import { ZkWasmUtil } from '@ora-io/zkwasm-service-helper' import { waitTaskStatus, } from '../requests/zkwasm_taskdetails' -import { CircuitSizeOutOfRange, ImageAlreadyExists } from '../common/error' +import { CircuitSizeOutOfRange, ImageAlreadyExists, ImageInvalid, ImageNotExists } from '../common/error' import { zkwasm_imagetask } from '../requests/zkwasm_imagetask' import type { CLEExecutable, SingableProver } from '../types' -import { ora_setup } from '../requests' +import { ora_setup, zkwasm_imagedetails } from '../requests' import { createFileStream } from '../common/compatible' -import { DEFAULT_CIRCUIT_SIZE, DEFAULT_URL, MAX_CIRCUIT_SIZE, MIN_CIRCUIT_SIZE } from '../common/constants' +import { DEFAULT_CIRCUIT_SIZE, DEFAULT_URL, MAX_CIRCUIT_SIZE, MIN_CIRCUIT_SIZE, PROVER_RPC_CONSTANTS } from '../common/constants' import { logger } from '../common' export interface BasicSetupParams { @@ -79,10 +79,10 @@ export async function requestSetup( // return the last status if exists if (error instanceof ImageAlreadyExists) { // check if there's any "Reset" task before - let res = await zkwasm_imagetask(proverUrl, md5, 'Reset') + let res = await zkwasm_imagetask(proverUrl, md5, PROVER_RPC_CONSTANTS.TASK_TYPE_RESET) // if no "Reset", check "Setup" if (res.data.result.total === 0) - res = await zkwasm_imagetask(proverUrl, md5, 'Setup') + res = await zkwasm_imagetask(proverUrl, md5, PROVER_RPC_CONSTANTS.TASK_TYPE_SETUP) taskDetails = res.data.result.data[0] taskId = res.data.result.data[0]._id.$oid @@ -107,15 +107,26 @@ export async function waitSetup(proverUrl: string, taskId: string): Promise { const result = await waitProve(proverUrl, proveTaskId, options) - if (result.status !== 'Done' || !result.proofParams) - throw new ProveTaskNotReady('Prove task is not \'Done\', can\'t verify') + if (result.status !== PROVER_RPC_CONSTANTS.TASK_STATUS_DONE || !result.proofParams) + throw new ProveTaskNotReady(`Prove task is not \'${PROVER_RPC_CONSTANTS.TASK_STATUS_DONE}\', can\'t verify`) return result.proofParams } diff --git a/src/common/constants.ts b/src/common/constants.ts index c2908e0..c240e40 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -116,4 +116,16 @@ export const DEFAULT_CIRCUIT_SIZE = 22 export const MIN_CIRCUIT_SIZE = 18 export const MAX_CIRCUIT_SIZE = 24 -export const FinishStatusList = ['Done', 'Fail', 'DryRunFailed'] +export const PROVER_RPC_CONSTANTS = { + TASK_STATUS_DONE: 'Done', + TASK_STATUS_FAIL: 'Fail', + TASK_STATUS_DRYRUNFAILED: 'DryRunFailed', + TASK_STATUS_SETUP_FINISH_LIST: ['Done', 'Fail'], + TASK_STATUS_PROVE_FINISH_LIST: ['Done', 'Fail', 'DryRunFailed'], + IMAGE_STATUS_VALID: 'Verified', + TASK_TYPE_SETUP: 'Setup', + TASK_TYPE_RESET: 'Reset', +} + +// TODO: compatible only, deprecating +export const FinishStatusList = PROVER_RPC_CONSTANTS.TASK_STATUS_PROVE_FINISH_LIST diff --git a/src/common/error.ts b/src/common/error.ts index bbeaf0a..b695217 100644 --- a/src/common/error.ts +++ b/src/common/error.ts @@ -10,6 +10,18 @@ export class ImageAlreadyExists extends Error { } } +export class ImageNotExists extends Error { + constructor(message: string | undefined) { + super(message) + } +} + +export class ImageInvalid extends Error { + constructor(message: string | undefined) { + super(message) + } +} + export class ProveTaskNotReady extends Error { constructor(message: string | undefined) { super(message) diff --git a/src/requests/zkwasm_deploy.ts b/src/requests/zkwasm_deploy.ts index 69b489a..e772142 100644 --- a/src/requests/zkwasm_deploy.ts +++ b/src/requests/zkwasm_deploy.ts @@ -1,3 +1,5 @@ +// Deprecating, no need for deploy. + import type { AxiosResponse } from 'axios' import axios from 'axios' import { Wallet, utils } from 'ethers' diff --git a/src/requests/zkwasm_imagedetails.ts b/src/requests/zkwasm_imagedetails.ts index e360c28..2b895fc 100644 --- a/src/requests/zkwasm_imagedetails.ts +++ b/src/requests/zkwasm_imagedetails.ts @@ -1,5 +1,7 @@ import axios from 'axios' +import { logger } from 'zkwasm-toolchain' import url from './url.js' +import { handleAxiosError } from './error_handle.js' export async function zkwasm_imagedetails(zkwasmProverUrl: string, md5: string) { const requestConfig = { @@ -11,9 +13,36 @@ export async function zkwasm_imagedetails(zkwasmProverUrl: string, md5: string) }, } + // let errorMessage = null + // const response = await axios.request(requestConfig).catch((error) => { + // errorMessage = error + // }) + let errorMessage = null - const response = await axios.request(requestConfig).catch((error) => { - errorMessage = error - }) + + // TODO: should change to setTimeInterval. + const retry_time = 1 + let response + let isRetry + for (let i = 0; i < retry_time + 1; i++) { + response = await axios.request(requestConfig).catch((error) => { + [errorMessage, isRetry] = handleAxiosError(error) + if (isRetry) { + // pass + } + else { + // console.error("Error in ora_prove. Please retry."); + // throw error; + logger.error(error.message) + } + // errorMessage = error.response.data; + }) + if (!isRetry) + break + + // for debug purpose, can delete after stable. + logger.log(errorMessage, 'retrying..') + } + return [response, errorMessage] } diff --git a/src/requests/zkwasm_taskdetails.ts b/src/requests/zkwasm_taskdetails.ts index 72961aa..dff70c5 100644 --- a/src/requests/zkwasm_taskdetails.ts +++ b/src/requests/zkwasm_taskdetails.ts @@ -41,15 +41,6 @@ export async function waitTaskStatus( interval: number | undefined, _timeout = 0, ) { - // let done = false; - // setInterval(() => { - // var [response, isSetUpSuccess, errorMessage] = await zkwasm_taskdetails(taskId) - // if(response.data.result.data[0].status == 'Done'){ - // done = true - // return - // } - // }, interval) - // var [response, isSetUpSuccess, errorMessage] = await zkwasm_taskdetails(taskId) return new Promise((resolve, reject) => { const checkStatus = async () => { const [response, error] = await zkwasm_taskdetails(zkwasmProverUrl, taskId) @@ -109,13 +100,3 @@ export function taskPrettyPrint(resData: { submit_time: string | number | Date; )}`, ) } - -// try{ -// let a = await waitTaskStatus('64c0c2bbf0e3eee93f75c260', ['Done', 'Fail'], 100); -// // console.log(a) -// taskPrettyPrint(a, '[*] ') -// }catch(error) { -// console.log(error) -// } -// var [response, errorMessage] =await zkwasm_taskdetails('64c0c2bbf0e3eee93f75c260') -// console.log(response.data.result.data[0].status) diff --git a/tests/fixtures/dsp/ethereum(event)/cle-event.yaml b/tests/fixtures/dsp/ethereum(event)/cle.yaml similarity index 80% rename from tests/fixtures/dsp/ethereum(event)/cle-event.yaml rename to tests/fixtures/dsp/ethereum(event)/cle.yaml index 3e7fcd8..1f40e22 100644 --- a/tests/fixtures/dsp/ethereum(event)/cle-event.yaml +++ b/tests/fixtures/dsp/ethereum(event)/cle.yaml @@ -19,7 +19,7 @@ mapping: file: ./mapping.ts handler: handleBlocks -dataDestinations: - - kind: ethereum - network: sepolia - address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' +# dataDestinations: +# - kind: ethereum +# network: sepolia +# address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' diff --git a/tests/fixtures/dsp/ethereum(storage)/cle-event.yaml b/tests/fixtures/dsp/ethereum(storage)/cle.yaml similarity index 84% rename from tests/fixtures/dsp/ethereum(storage)/cle-event.yaml rename to tests/fixtures/dsp/ethereum(storage)/cle.yaml index b3ca676..3d76bbd 100644 --- a/tests/fixtures/dsp/ethereum(storage)/cle-event.yaml +++ b/tests/fixtures/dsp/ethereum(storage)/cle.yaml @@ -24,7 +24,7 @@ mapping: file: ./mapping.ts handler: handleBlocks -dataDestinations: - - kind: ethereum - network: sepolia - address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' +# dataDestinations: +# - kind: ethereum +# network: sepolia +# address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' diff --git a/tests/fixtures/dsp/ethereum.unsafe-ethereum/cle-event.yaml b/tests/fixtures/dsp/ethereum.unsafe-ethereum/cle.yaml similarity index 88% rename from tests/fixtures/dsp/ethereum.unsafe-ethereum/cle-event.yaml rename to tests/fixtures/dsp/ethereum.unsafe-ethereum/cle.yaml index 85b6fd9..081f2e4 100644 --- a/tests/fixtures/dsp/ethereum.unsafe-ethereum/cle-event.yaml +++ b/tests/fixtures/dsp/ethereum.unsafe-ethereum/cle.yaml @@ -34,7 +34,7 @@ mapping: file: ./mapping.ts handler: handleBlocks -dataDestinations: - - kind: ethereum - network: sepolia - address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' +# dataDestinations: +# - kind: ethereum +# network: sepolia +# address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' diff --git a/tests/fixtures/dsp/ethereum.unsafe/cle-event.yaml b/tests/fixtures/dsp/ethereum.unsafe/cle.yaml similarity index 81% rename from tests/fixtures/dsp/ethereum.unsafe/cle-event.yaml rename to tests/fixtures/dsp/ethereum.unsafe/cle.yaml index c2bfda7..dd959d1 100644 --- a/tests/fixtures/dsp/ethereum.unsafe/cle-event.yaml +++ b/tests/fixtures/dsp/ethereum.unsafe/cle.yaml @@ -20,7 +20,7 @@ mapping: file: ./mapping.ts handler: handleBlocks -dataDestinations: - - kind: ethereum - network: sepolia - address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' +# dataDestinations: +# - kind: ethereum +# network: sepolia +# address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' diff --git a/tests/fixureoptions.ts b/tests/fixureoptions.ts index b6f54f3..faf22ad 100644 --- a/tests/fixureoptions.ts +++ b/tests/fixureoptions.ts @@ -18,7 +18,7 @@ interface OptionType { const defaultPath = (pathFromFixtures: string) => { return { mappingPath: `tests/fixtures/${pathFromFixtures}/mapping.ts`, - yamlPath: `tests/fixtures/${pathFromFixtures}/cle-event.yaml`, + yamlPath: `tests/fixtures/${pathFromFixtures}/cle.yaml`, wasmPath: `tests/fixtures/build/${pathFromFixtures}.wasm`, watPath: `tests/fixtures/build/${pathFromFixtures}.wat`, }