Skip to content

Commit

Permalink
feat: add requireImageDetails in prove & PROVER_RPC_CONSTANTS & beaut…
Browse files Browse the repository at this point in the history
…ify tests
  • Loading branch information
nom4dv3 committed Mar 24, 2024
1 parent aada17b commit 962a614
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 61 deletions.
14 changes: 11 additions & 3 deletions src/api/prove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -73,7 +77,11 @@ export async function waitProve(
options: BatchOption = {},
): Promise<ProveResult> {
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
Expand All @@ -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,
Expand Down
12 changes: 4 additions & 8 deletions src/api/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
}
25 changes: 18 additions & 7 deletions src/api/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -107,15 +107,26 @@ export async function waitSetup(proverUrl: string, taskId: string): Promise<Setu
const taskDetails = await waitTaskStatus(
proverUrl,
taskId,
['Done', 'Fail'],
PROVER_RPC_CONSTANTS.TASK_STATUS_SETUP_FINISH_LIST,
3000,
0,
) // TODO: timeout

const result: SetupResult = {
success: taskDetails.status === 'Done',
success: taskDetails.status === PROVER_RPC_CONSTANTS.TASK_STATUS_DONE,
status: taskDetails.status,
taskDetails,
}
return result
}

export async function requireImageDetails(proverUrl: string, md5: string) {
const response = await zkwasm_imagedetails(proverUrl, md5)
const details = response[0]?.data.result[0]
if (details === null)
throw new ImageNotExists('Can\'t find wasm image in prover, please finish setup first.')
if (details.status !== PROVER_RPC_CONSTANTS.IMAGE_STATUS_VALID)
throw new ImageInvalid('wasm image is invalid in prover, please setup a valid wasm. or contact admin if you believe it should be valid.')

return details
}
6 changes: 3 additions & 3 deletions src/api/verify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ZkWasmUtil } from '@ora-io/zkwasm-service-helper'
import type { providers } from 'ethers'
import { Contract } from 'ethers'
import { AggregatorVerifierABI, AggregatorVerifierAddress } from '../common/constants'
import { AggregatorVerifierABI, AggregatorVerifierAddress, PROVER_RPC_CONSTANTS } from '../common/constants'
import { ProveTaskNotReady } from '../common/error'
import type { BatchOption, ProofParams, ProofParams as VerifyProofParams } from '../types'
import { BatchStyle } from '../types'
Expand Down Expand Up @@ -88,7 +88,7 @@ export async function getVerifyProofParamsByTaskID(
options: BatchOption = {},
): Promise<ProofParams> {
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
}
14 changes: 13 additions & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions src/common/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/requests/zkwasm_deploy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Deprecating, no need for deploy.

import type { AxiosResponse } from 'axios'
import axios from 'axios'
import { Wallet, utils } from 'ethers'
Expand Down
35 changes: 32 additions & 3 deletions src/requests/zkwasm_imagedetails.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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]
}
19 changes: 0 additions & 19 deletions src/requests/zkwasm_taskdetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>((resolve, reject) => {
const checkStatus = async () => {
const [response, error] = await zkwasm_taskdetails(zkwasmProverUrl, taskId)
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mapping:
file: ./mapping.ts
handler: handleBlocks

dataDestinations:
- kind: ethereum
network: sepolia
address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D'
# dataDestinations:
# - kind: ethereum
# network: sepolia
# address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D'
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mapping:
file: ./mapping.ts
handler: handleBlocks

dataDestinations:
- kind: ethereum
network: sepolia
address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D'
# dataDestinations:
# - kind: ethereum
# network: sepolia
# address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D'
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mapping:
file: ./mapping.ts
handler: handleBlocks

dataDestinations:
- kind: ethereum
network: sepolia
address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D'
# dataDestinations:
# - kind: ethereum
# network: sepolia
# address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D'
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mapping:
file: ./mapping.ts
handler: handleBlocks

dataDestinations:
- kind: ethereum
network: sepolia
address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D'
# dataDestinations:
# - kind: ethereum
# network: sepolia
# address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D'
2 changes: 1 addition & 1 deletion tests/fixureoptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
}
Expand Down

0 comments on commit 962a614

Please sign in to comment.