diff --git a/src/api/compile.ts b/src/api/compile.ts index a755bee..9f5eff7 100644 --- a/src/api/compile.ts +++ b/src/api/compile.ts @@ -99,7 +99,8 @@ export interface CompileOptions { yamlPath?: string outWatPath?: string outWasmPath?: string - outInnerWasmPath?: string + outInnerWasmPath?: string // optional + outInnerWatPath?: string // optional compilerServerEndpoint?: string isLocal?: boolean } @@ -120,18 +121,18 @@ export async function compile( // cache final out path const { isLocal = false, + outInnerWasmPath = DEFAULT_PATH.OUT_INNER_WASM, + outInnerWatPath = DEFAULT_PATH.OUT_INNER_WAT, } = options - let { - outWasmPath = DEFAULT_PATH.OUT_WASM, - outWatPath = DEFAULT_PATH.OUT_WAT, - } = options - const finalOutWasmPath = outWasmPath - const finalOutWatPath = outWatPath + options.outWasmPath = options.outWasmPath || DEFAULT_PATH.OUT_WASM + options.outWatPath = options.outWatPath || DEFAULT_PATH.OUT_WAT + const finalOutWasmPath = options.outWasmPath + const finalOutWatPath = options.outWatPath // compile locally with asc, use inner path if isLocal if (isLocal) { - outWasmPath = DEFAULT_PATH.OUT_INNER_WASM - outWatPath = DEFAULT_PATH.OUT_INNER_WAT + options.outWasmPath = outInnerWasmPath + options.outWatPath = outInnerWatPath } const result = await compileAsc(sources, options) if (result.error) @@ -139,9 +140,9 @@ export async function compile( // compile remotely on the compiler server if needed, using final out path if (isLocal === false) { - const outWasm = result.outputs[outWasmPath as string] as Uint8Array + const outWasm = result.outputs[options.outWasmPath as string] as Uint8Array const innerCLEExecutable = { wasmUint8Array: outWasm, cleYaml } - options.outInnerWasmPath = outWasmPath + // options.outInnerWasmPath = options.outWasmPath options.outWasmPath = finalOutWasmPath options.outWatPath = finalOutWatPath return await compileServer(innerCLEExecutable, cleYamlContent, options) @@ -214,14 +215,13 @@ export async function compileServer( compilerServerEndpoint = DEFAULT_URL.COMPILER_SERVER, outWasmPath = DEFAULT_PATH.OUT_WASM, outWatPath = DEFAULT_PATH.OUT_WAT, - outInnerWasmPath = DEFAULT_PATH.OUT_WASM, } = options const outputs: Record = {} // Set up form data const data = new FormData() - data.append('wasmFile', createFileStream(wasmUint8Array, { fileType: 'application/wasm', fileName: 'inner.wasm', tmpPath: outInnerWasmPath })) + data.append('wasmFile', createFileStream(wasmUint8Array, { fileType: 'application/wasm', fileName: 'inner.wasm' })) // , tmpPath: outInnerWasmPath rm since paths may be abstract data.append('yamlFile', createFileStream(cleYamlContent, { fileType: 'text/yaml', fileName: 'src/cle.yaml' })) // if (__BROWSER__) { diff --git a/src/api/deposit_bounty.ts b/src/api/deposit_bounty.ts index c4316bc..1588136 100644 --- a/src/api/deposit_bounty.ts +++ b/src/api/deposit_bounty.ts @@ -1,6 +1,6 @@ import { Contract, ethers } from 'ethers' import { - cle_abi, + cleContractABI, } from '../common/constants' /** @@ -23,7 +23,7 @@ export async function deposit( ) { const { depositAmount } = options - const cleContract = new Contract(cleContractAddress, cle_abi, signer) + const cleContract = new Contract(cleContractAddress, cleContractABI, signer) const tx = await cleContract .deposit( ethers.utils.parseEther(depositAmount), { value: ethers.utils.parseEther(depositAmount) }, diff --git a/src/api/publish.ts b/src/api/publish.ts index 095cb00..ce8c8e7 100644 --- a/src/api/publish.ts +++ b/src/api/publish.ts @@ -3,13 +3,14 @@ import { Contract, ethers, utils } from 'ethers' import { AddressZero, DEFAULT_URL, + EventSigNewCLE, abiFactory, addressFactory, } from '../common/constants' -import { DSPNotFound, GraphAlreadyExist } from '../common/error' +import { CLEAlreadyExist, DSPNotFound, TxFailed } from '../common/error' import { dspHub } from '../dsp/hub' import { zkwasm_imagedetails } from '../requests/zkwasm_imagedetails' -import type { CLEExecutable } from '../types' +import type { CLEExecutable, CLEYaml } from '../types' export interface PublishOptions { proverUrl?: string @@ -62,7 +63,27 @@ export async function publishByImgCmt( if (!dsp) throw new DSPNotFound('Can\'t find DSP for this data source kind.') - const dspID = utils.keccak256(utils.toUtf8Bytes(dsp.getLibDSPName())) + // for ora prover upgrade + const suffix = (cy: CLEYaml) => { + const allEthDS = cy.dataSources.filter( + ds => ds.kind === 'ethereum')// ds.filterByKeys(['event', 'storage', 'transaction']) // + const allEthDSState = allEthDS.filter( + ds => Object.keys(ds.filterByKeys(['storage'])).length !== 0) + const allEthDSStateOnly = allEthDSState.filter( + ds => Object.keys(ds.filterByKeys(['event', 'transaction'])).length === 0) + if (allEthDSStateOnly.length > 0) + return ':stateonly' + + const allNoTx = allEthDS.filter( + ds => Object.keys(ds.filterByKeys(['transaction'])).length === 0) + if (allNoTx.length > 0) + return ':notx' + + return '' + } + // logger.debug('[*] dsp name suffix for clecontract:', suffix(cleYaml)) + + const dspID = utils.keccak256(utils.toUtf8Bytes(dsp.getLibDSPName() + suffix(cleYaml))) const destinationContractAddress = (cleYaml?.dataDestinations && cleYaml?.dataDestinations.length) @@ -84,20 +105,26 @@ export async function publishByImgCmt( imageCommitment.pointY, ) .catch((_err: any) => { - throw new GraphAlreadyExist('Duplicate CLE detected. Only publishing distinct CLEs is allowed.') + throw new CLEAlreadyExist('Duplicate CLE detected. Only publishing distinct CLEs is allowed.') }) const txReceipt = await tx.wait(1).catch((err: any) => { throw err }) - // in the transaction receipt, get the event data with topic 0x3573344393f569107cbc8438d3f0a47ca210029fdc8226cc33804a7b35cd32d8 - // this is the event newZkG(address graph) - const logs = txReceipt.logs.filter((log: { topics: string[] }) => - log.topics.includes('0x3573344393f569107cbc8438d3f0a47ca210029fdc8226cc33804a7b35cd32d8'), + if (txReceipt.status !== 1) + throw new TxFailed(`Transaction failed (${txReceipt.transactionHash})`) + + // filter event with topic "NewCLE(address)" in transaction receipt + const logs = txReceipt.logs.filter((log: { address: string; topics: string[] }) => + log.address === factoryAddress + && log.topics[0] === EventSigNewCLE, ) - // Extract the graph address from the event + if (logs.length === 0) + throw new Error(`Can't identify NewCLE(address) event in tx receipt (${txReceipt.transactionHash}), please check the TX or factory contract (${factoryAddress})`) + + // Extract the cle address from the event const cleAddress = `0x${logs[0].data.slice(-40)}` return { diff --git a/src/api/trigger.ts b/src/api/trigger.ts index 4766a61..f87ffb4 100644 --- a/src/api/trigger.ts +++ b/src/api/trigger.ts @@ -12,6 +12,9 @@ export async function trigger( ): Promise { const { cleYaml } = cleExecutable const ddps = ddpHub.getDDPsByYaml(cleYaml) - for (let i = 0; i < ddpParamsList.length; i++) + if (ddpParamsList.length !== ddps.length) + throw new Error(`The length of DDP params list provided (${ddpParamsList.length}) doesn't match yaml specified DDP numbers ${ddps.length} `) + + for (let i = 0; i < ddps.length; i++) await ddps[i].go(cleId, proofParams, ddpParamsList[i]) } diff --git a/src/api/verify.ts b/src/api/verify.ts index 76b4281..72cb240 100644 --- a/src/api/verify.ts +++ b/src/api/verify.ts @@ -37,11 +37,11 @@ export async function verifyOnchain( if (isZKVerifier === false) throw new Error('isZKVerifier==false is reserved, not supported yet') const { provider } = options - const network = await provider.getNetwork() + const network = (await provider.getNetwork()).name const defaultVerifierAddress = batchStyle === BatchStyle.ORA - ? AggregatorVerifierAddress.Ora[network.name] - : AggregatorVerifierAddress.ZkWasmHub[network.name] + ? AggregatorVerifierAddress.ORA[network] + : AggregatorVerifierAddress.ZKWASMHUB[network] const { verifierAddress = defaultVerifierAddress } = options diff --git a/src/common/abi/clecontractabi.ts b/src/common/abi/clecontractabi.ts new file mode 100644 index 0000000..68c77c3 --- /dev/null +++ b/src/common/abi/clecontractabi.ts @@ -0,0 +1,346 @@ +export const cleContractABI = [ + { + type: 'constructor', + inputs: [], + stateMutability: 'nonpayable', + }, + { + type: 'function', + name: 'bountyReward', + inputs: [], + outputs: [ + { + name: '', + type: 'uint256', + internalType: 'uint256', + }, + ], + stateMutability: 'view', + }, + { + type: 'function', + name: 'bountyToken', + inputs: [], + outputs: [ + { + name: '', + type: 'address', + internalType: 'address', + }, + ], + stateMutability: 'view', + }, + { + type: 'function', + name: 'cleURI', + inputs: [], + outputs: [ + { + name: '', + type: 'string', + internalType: 'string', + }, + ], + stateMutability: 'view', + }, + { + type: 'function', + name: 'deposit', + inputs: [ + { + name: 'amount', + type: 'uint256', + internalType: 'uint256', + }, + ], + outputs: [], + stateMutability: 'payable', + }, + { + type: 'function', + name: 'destAddr', + inputs: [], + outputs: [ + { + name: '', + type: 'address', + internalType: 'address', + }, + ], + stateMutability: 'view', + }, + { + type: 'function', + name: 'dspID', + inputs: [], + outputs: [ + { + name: '', + type: 'bytes32', + internalType: 'bytes32', + }, + ], + stateMutability: 'view', + }, + { + type: 'function', + name: 'encoding_scalars_to_point', + inputs: [ + { + name: 'a', + type: 'uint256', + internalType: 'uint256', + }, + { + name: 'b', + type: 'uint256', + internalType: 'uint256', + }, + { + name: 'c', + type: 'uint256', + internalType: 'uint256', + }, + ], + outputs: [ + { + name: 'x', + type: 'uint256', + internalType: 'uint256', + }, + { + name: 'y', + type: 'uint256', + internalType: 'uint256', + }, + ], + stateMutability: 'pure', + }, + { + type: 'function', + name: 'factory', + inputs: [], + outputs: [ + { + name: '', + type: 'address', + internalType: 'address', + }, + ], + stateMutability: 'view', + }, + { + type: 'function', + name: 'owner', + inputs: [], + outputs: [ + { + name: '', + type: 'address', + internalType: 'address', + }, + ], + stateMutability: 'view', + }, + { + type: 'function', + name: 'pointX', + inputs: [], + outputs: [ + { + name: '', + type: 'uint256', + internalType: 'uint256', + }, + ], + stateMutability: 'view', + }, + { + type: 'function', + name: 'pointY', + inputs: [], + outputs: [ + { + name: '', + type: 'uint256', + internalType: 'uint256', + }, + ], + stateMutability: 'view', + }, + { + type: 'function', + name: 'renounceOwnership', + inputs: [], + outputs: [], + stateMutability: 'nonpayable', + }, + { + type: 'function', + name: 'transferOwnership', + inputs: [ + { + name: 'newOwner', + type: 'address', + internalType: 'address', + }, + ], + outputs: [], + stateMutability: 'nonpayable', + }, + { + type: 'function', + name: 'trigger', + inputs: [ + { + name: 'proof', + type: 'uint256[]', + internalType: 'uint256[]', + }, + { + name: 'verifyInstance', + type: 'uint256[]', + internalType: 'uint256[]', + }, + { + name: 'aux', + type: 'uint256[]', + internalType: 'uint256[]', + }, + { + name: 'targetInstance', + type: 'uint256[][]', + internalType: 'uint256[][]', + }, + { + name: 'extra', + type: 'uint256[]', + internalType: 'uint256[]', + }, + ], + outputs: [], + stateMutability: 'nonpayable', + }, + { + type: 'function', + name: 'updateReward', + inputs: [ + { + name: 'amount', + type: 'uint256', + internalType: 'uint256', + }, + ], + outputs: [], + stateMutability: 'nonpayable', + }, + { + type: 'function', + name: 'verifyToCalldata', + inputs: [ + { + name: 'proof', + type: 'uint256[]', + internalType: 'uint256[]', + }, + { + name: 'verifyInstance', + type: 'uint256[]', + internalType: 'uint256[]', + }, + { + name: 'aux', + type: 'uint256[]', + internalType: 'uint256[]', + }, + { + name: 'targetInstance', + type: 'uint256[][]', + internalType: 'uint256[][]', + }, + { + name: 'extra', + type: 'uint256[]', + internalType: 'uint256[]', + }, + ], + outputs: [ + { + name: '', + type: 'bytes', + internalType: 'bytes', + }, + ], + stateMutability: 'view', + }, + { + type: 'event', + name: 'OwnershipTransferred', + inputs: [ + { + name: 'previousOwner', + type: 'address', + indexed: true, + internalType: 'address', + }, + { + name: 'newOwner', + type: 'address', + indexed: true, + internalType: 'address', + }, + ], + anonymous: false, + }, + { + type: 'event', + name: 'Revert', + inputs: [ + { + name: 'reason', + type: 'string', + indexed: false, + internalType: 'string', + }, + ], + anonymous: false, + }, + { + type: 'event', + name: 'Trigger', + inputs: [ + { + name: 'sender', + type: 'address', + indexed: false, + internalType: 'address', + }, + { + name: 'data', + type: 'bytes', + indexed: false, + internalType: 'bytes', + }, + ], + anonymous: false, + }, + { + type: 'event', + name: 'UpdateReward', + inputs: [ + { + name: 'oldReward', + type: 'uint256', + indexed: false, + internalType: 'uint256', + }, + { + name: 'newReward', + type: 'uint256', + indexed: false, + internalType: 'uint256', + }, + ], + anonymous: false, + }, +] diff --git a/src/common/constants.ts b/src/common/constants.ts index cbefa1c..7ca5610 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -1,3 +1,5 @@ +export { cleContractABI } from './abi/clecontractabi' + export const networks = [ { name: 'Sepolia', @@ -66,359 +68,27 @@ export const AggregatorVerifierABI = { ], } -export const cle_abi = [ - { - inputs: [ - { - internalType: 'address', - name: '_bountyToken', - type: 'address', - }, - { - internalType: 'uint256', - name: '_bountyReward', - type: 'uint256', - }, - { - internalType: 'address', - name: '_verifier', - type: 'address', - }, - { - internalType: 'address', - name: '_destAddr', - type: 'address', - }, - { - internalType: 'string', - name: '_graphURI', - type: 'string', - }, - ], - stateMutability: 'nonpayable', - type: 'constructor', - }, - { - anonymous: false, - inputs: [ - { - indexed: true, - internalType: 'address', - name: 'previousOwner', - type: 'address', - }, - { - indexed: true, - internalType: 'address', - name: 'newOwner', - type: 'address', - }, - ], - name: 'OwnershipTransferred', - type: 'event', - }, - { - anonymous: false, - inputs: [ - { - indexed: false, - internalType: 'address', - name: 'sender', - type: 'address', - }, - { - indexed: false, - internalType: 'bytes', - name: 'zkgState', - type: 'bytes', - }, - ], - name: 'Trigger', - type: 'event', - }, - { - anonymous: false, - inputs: [ - { - indexed: false, - internalType: 'uint256', - name: 'oldReward', - type: 'uint256', - }, - { - indexed: false, - internalType: 'uint256', - name: 'newReward', - type: 'uint256', - }, - ], - name: 'UpdateReward', - type: 'event', - }, - { - inputs: [], - name: 'bountyReward', - outputs: [ - { - internalType: 'uint256', - name: '', - type: 'uint256', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [], - name: 'bountyToken', - outputs: [ - { - internalType: 'address', - name: '', - type: 'address', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [ - { - internalType: 'uint256', - name: 'amount', - type: 'uint256', - }, - ], - name: 'deposit', - outputs: [], - stateMutability: 'payable', - type: 'function', - }, - { - inputs: [], - name: 'destAddr', - outputs: [ - { - internalType: 'address', - name: '', - type: 'address', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [ - { - internalType: 'uint256', - name: 'blockNumber', - type: 'uint256', - }, - { - internalType: 'bytes32', - name: 'blockHash', - type: 'bytes32', - }, - { - internalType: 'bytes', - name: 'zkgState', - type: 'bytes', - }, - ], - name: 'encodePublicInput', - outputs: [ - { - internalType: 'uint256[]', - name: '', - type: 'uint256[]', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [], - name: 'factory', - outputs: [ - { - internalType: 'address', - name: '', - type: 'address', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [], - name: 'graphURI', - outputs: [ - { - internalType: 'string', - name: '', - type: 'string', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [], - name: 'owner', - outputs: [ - { - internalType: 'address', - name: '', - type: 'address', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [], - name: 'renounceOwnership', - outputs: [], - stateMutability: 'nonpayable', - type: 'function', - }, - { - inputs: [ - { - internalType: 'address', - name: 'newOwner', - type: 'address', - }, - ], - name: 'transferOwnership', - outputs: [], - stateMutability: 'nonpayable', - type: 'function', - }, - { - inputs: [ - { - internalType: 'uint256[]', - name: 'proof', - type: 'uint256[]', - }, - { - internalType: 'uint256[]', - name: 'verifyInstance', - type: 'uint256[]', - }, - { - internalType: 'uint256[]', - name: 'aux', - type: 'uint256[]', - }, - { - internalType: 'uint256[][]', - name: 'targetInstance', - type: 'uint256[][]', - }, - { - internalType: 'uint256[]', - name: 'extra', - type: 'uint256[]', - }, - ], - name: 'trigger', - outputs: [], - stateMutability: 'nonpayable', - type: 'function', - }, - { - inputs: [ - { - internalType: 'uint256', - name: 'amount', - type: 'uint256', - }, - ], - name: 'updateReward', - outputs: [], - stateMutability: 'nonpayable', - type: 'function', - }, - { - inputs: [], - name: 'verifier', - outputs: [ - { - internalType: 'address', - name: '', - type: 'address', - }, - ], - stateMutability: 'view', - type: 'function', - }, - { - inputs: [ - { - internalType: 'uint256', - name: 'blockNumber', - type: 'uint256', - }, - { - internalType: 'bytes32', - name: 'blockHash', - type: 'bytes32', - }, - { - internalType: 'bytes', - name: 'zkgState', - type: 'bytes', - }, - { - internalType: 'uint256[]', - name: 'proof', - type: 'uint256[]', - }, - { - internalType: 'uint256[]', - name: 'verify_instance', - type: 'uint256[]', - }, - { - internalType: 'uint256[]', - name: 'aux', - type: 'uint256[]', - }, - ], - name: 'verify', - outputs: [ - { - internalType: 'bool', - name: '', - type: 'bool', - }, - ], - stateMutability: 'view', - type: 'function', - }, -] - export const addressFactory = { mainnet: 'not support yet', - sepolia: '0xE484E5B8b71aA7955d1De4D52737BF436eBf9970', + sepolia: '0x638e019Cfd8A5f1c2fecd473062471c7f6978c9B', goerli: 'not support yet', } export const abiFactory = [ - 'function getAllZkg() external view returns (address[] memory)', - 'function registry(address destAddr, address bountyToken, uint256 bountyReward, bytes32 dspID, string memory graphURI, uint256 pointX, uint256 pointY) external returns (address graph)', - 'function getGraphBycreator(address creator) external view returns (address[] memory)', - 'function getGraphInfoByAddress(address graph) external view returns (address creator, uint256 bountyReward, address verifier, string memory graphURI)', + 'function getAllCLEs() external view returns (address[] memory)', + 'function registry(address destAddr,address bountyToken,uint256 bountyReward,bytes32 dspID,string memory cleURI,uint256 pointX,uint256 pointY) public returns (address cle)', + 'function getCLEBycreator(address creator) external view returns (address[] memory)', + 'function getCLEInfoByAddress(address cle) external view returns (address creator, uint256 bountyReward, address destAddr, string memory cleURI)', ] +export const EventSigNewCLE = '0x39f7254e91d2eee9fa8ffc88bc7b0dff5c67916b7a1cc84284b3192bde4ab1d2' + export const AggregatorVerifierAddress: { [key: string]: { [key: string]: string } } = { - ZkWasmHub: { + ZKWASMHUB: { mainnet: 'not support yet', sepolia: '0xfD74dce645Eb5EB65D818aeC544C72Ba325D93B0', goerli: '0xbEF9572648284CB63a0DA32a89D3b4F2BeD65a89', }, - Ora: { + ORA: { mainnet: 'not support yet', sepolia: '0xf48dC1e1AaA6bB8cA43b03Ca0695973a2F440090', goerli: 'not support yet', diff --git a/src/common/error.ts b/src/common/error.ts index 144314f..bbeaf0a 100644 --- a/src/common/error.ts +++ b/src/common/error.ts @@ -45,7 +45,20 @@ export class TDNoTaskFound extends Error { } } -export class GraphAlreadyExist extends Error { +// Publish Error +export class CLEAlreadyExist extends Error { + constructor(message: string | undefined) { + super(message) + } +} + +export class TxFailed extends Error { + constructor(message: string | undefined) { + super(message) + } +} + +export class CLEAddressMissing extends Error { constructor(message: string | undefined) { super(message) } diff --git a/src/common/logger.ts b/src/common/logger.ts index 9794cb9..6f712a6 100644 --- a/src/common/logger.ts +++ b/src/common/logger.ts @@ -12,3 +12,11 @@ export let logger = console as CLELogger export function setCLELogger(newLogger: CLELogger) { logger = newLogger } + +export class SilentLogger implements CLELogger { + debug(..._args: any[]): void {} + info(..._args: any[]): void {} + warn(..._args: any[]): void {} + error(..._args: any[]): void {} + log(..._args: any[]): void {} +} diff --git a/src/ddp/ethereum/index.ts b/src/ddp/ethereum/index.ts index 3313df4..b1c03c2 100644 --- a/src/ddp/ethereum/index.ts +++ b/src/ddp/ethereum/index.ts @@ -1,7 +1,7 @@ import { ethers } from 'ethers' import { ZkWasmUtil } from '@ora-io/zkwasm-service-helper' import type { KeyofToArray } from '@murongg/utils/index' -import { cle_abi } from '../../common/constants' +import { cleContractABI } from '../../common/constants' import type { ProofParams } from '../../types' import { DataDestinationPlugin } from '../interface' import { logger } from '../../common' @@ -9,10 +9,11 @@ import { logger } from '../../common' export interface EthereumDDPGoParams { signer: ethers.Wallet gasLimit: number + onlyMock: boolean } export class EthereumDataDestinationPlugin extends DataDestinationPlugin { - goParams: KeyofToArray = ['signer'] + goParams: KeyofToArray = ['signer', 'gasLimit'] async go(cleId: string, proofParams: ProofParams, goParams: EthereumDDPGoParams): Promise { const proof = ZkWasmUtil.bytesToBigIntArray(proofParams.aggregate_proof) @@ -25,10 +26,18 @@ export class EthereumDataDestinationPlugin extends DataDestinationPlugin dataSources.push(dataSourceClassMap.get(ds.kind)?.from_v_0_0_2(ds))) + yamlObj.dataSources.forEach((ds: any) => dataSources.push(dataSourceClassMap.get(ds.kind)?.from_v_0_0_2(ds))) const dataDestinations: DataDestination[] = [] - if (yaml.dataDestinations !== undefined && yaml.dataDestinations !== null && yaml.dataDestinations.length !== 0) - yaml.dataDestinations.forEach((dd: { kind: any }) => dataDestinations.push(dataDestinationClassMap.get(dd.kind).from_v_0_0_2(dd))) + if (yamlObj.dataDestinations !== undefined && yamlObj.dataDestinations !== null && yamlObj.dataDestinations.length !== 0) + yamlObj.dataDestinations.forEach((dd: { kind: any }) => dataDestinations.push(dataDestinationClassMap.get(dd.kind).from_v_0_0_2(dd))) return new CLEYaml( - yaml.specVersion, - yaml.apiVersion, - yaml.name, - yaml.description, - yaml.repository, + yamlObj, + yamlObj.specVersion, + yamlObj.apiVersion, + yamlObj.name, + yamlObj.description, + yamlObj.repository, dataSources, dataDestinations, - Mapping.from_v_0_0_2(yaml.mapping), + Mapping.from_v_0_0_2(yamlObj.mapping), ) } // TODO: type: should be the return class of yaml.load - static fromYaml(yaml: any) { + static fromYaml(yamlObj: any) { // health check before parse - CLEYaml.healthCheck(yaml) - if (yaml.specVersion === '0.0.1') - return CLEYaml.from_v_0_0_1(yaml) + CLEYaml.healthCheck(yamlObj) + if (yamlObj.specVersion === '0.0.1') + return CLEYaml.from_v_0_0_1(yamlObj) - else if (yaml.specVersion === '0.0.2') - return CLEYaml.from_v_0_0_2(yaml) + else if (yamlObj.specVersion === '0.0.2') + return CLEYaml.from_v_0_0_2(yamlObj) else - throw new Error(`Unsupported specVersion: ${yaml.specVersion}`) + throw new Error(`Unsupported specVersion: ${yamlObj.specVersion}`) } static fromYamlContent(yamlContent: string) { try { // Parse the YAML content - const config = yaml.load(yamlContent) - return CLEYaml.fromYaml(config) + const yamlObj = yaml.load(yamlContent) + return CLEYaml.fromYaml(yamlObj) } catch (error: any) { // TODO: is there other cases than "Invalid Yaml"? @@ -192,10 +195,6 @@ export class CLEYaml { // } } - toString() { - return yaml.dump(this) - } - decidePublishNetwork(): string | undefined { // 1. if there's ethereum destination use that destination network const ddkinds = this.dataDestinations.map((dataDest: { kind: any }) => { return dataDest.kind }) diff --git a/src/yaml/cleyaml_eth.ts b/src/yaml/cleyaml_eth.ts index 9e68c54..5fb5258 100644 --- a/src/yaml/cleyaml_eth.ts +++ b/src/yaml/cleyaml_eth.ts @@ -70,6 +70,7 @@ export class EthereumDataSource extends DataSource { storageSectionCache: StorageSectionCache | null = null constructor( + yamlObj: any, kind: DataSourceKind, unsafe: boolean, network: string, @@ -79,7 +80,7 @@ export class EthereumDataSource extends DataSource { account: any[] | null, block: any, ) { - super(kind) + super(yamlObj, kind) this.unsafe = unsafe this.network = network this.event = event @@ -91,6 +92,7 @@ export class EthereumDataSource extends DataSource { static from_v_0_0_2(yamlEthDS: { kind: DataSourceKind; unsafe?: boolean; network: string; event: EventItem[]; storage: StorageItem[]; transaction: TransactionItem[] }) { return new EthereumDataSource( + yamlEthDS, yamlEthDS.kind, yamlEthDS.unsafe == null ? false : yamlEthDS.unsafe, yamlEthDS.network, @@ -188,8 +190,8 @@ export class EthereumDataSource extends DataSource { export class EthereumDataDestination extends DataDestination { network: string address: string - constructor(kind: DataDestinationKind, network: string, address: string) { - super(kind) + constructor(yamlObj: any, kind: DataDestinationKind, network: string, address: string) { + super(yamlObj, kind) this.network = network this.address = address } @@ -201,6 +203,7 @@ export class EthereumDataDestination extends DataDestination { static from_v_0_0_2(yamlEthDD: { kind: DataDestinationKind; network: string; address: string }) { return new EthereumDataDestination( + yamlEthDD, yamlEthDD.kind, yamlEthDD.network, yamlEthDD.address, @@ -209,6 +212,7 @@ export class EthereumDataDestination extends DataDestination { static from_v_0_0_1(yamlEthDD: { kind: DataDestinationKind; network: string; destination: { address: string } }) { return new EthereumDataDestination( + yamlEthDD, yamlEthDD.kind, yamlEthDD.network, yamlEthDD.destination.address, diff --git a/src/yaml/cleyaml_off.ts b/src/yaml/cleyaml_off.ts index a5d0694..9c0fe03 100644 --- a/src/yaml/cleyaml_off.ts +++ b/src/yaml/cleyaml_off.ts @@ -22,8 +22,8 @@ import { DataSource } from './interface' export class OffchainDataSource extends DataSource { type: any - constructor(kind: any, type: any) { - super(kind) + constructor(yamlObj: any, kind: any, type: any) { + super(yamlObj, kind) this.type = type } @@ -34,6 +34,7 @@ export class OffchainDataSource extends DataSource { static from_v_0_0_2(yamlOffDS: { kind: any; type: any }) { return new OffchainDataSource( + yamlOffDS, yamlOffDS.kind, yamlOffDS.type, // CustomDataSection.from_v_0_0_2(yamlOffDS.data) diff --git a/src/yaml/interface.ts b/src/yaml/interface.ts index 79a0b5d..67be4c6 100644 --- a/src/yaml/interface.ts +++ b/src/yaml/interface.ts @@ -1,9 +1,32 @@ +import yaml from 'js-yaml' + export type DataSourceKind = 'ethereum' | 'offchain' export type DataDestinationKind = 'ethereum' | 'offchain' -export class DataSource { +export class WrappedYaml { + yamlObj: any + constructor(yamlObj: any) { + this.yamlObj = yamlObj + } + + toString() { + return yaml.dump(this.yamlObj) + } + + filterByKeys(keysToInclude: string[]) { + const filteredObject: any = {} + Object.keys(this.yamlObj).forEach((key) => { + if (keysToInclude.includes(key)) + filteredObject[key] = this.yamlObj[key] + }) + return filteredObject + } +} + +export class DataSource extends WrappedYaml { kind: DataSourceKind - constructor(kind: DataSourceKind) { + constructor(yamlObj: any, kind: DataSourceKind) { + super(yamlObj) this.kind = kind } @@ -16,9 +39,10 @@ export class DataSource { } } -export class DataDestination { +export class DataDestination extends WrappedYaml { kind: DataDestinationKind - constructor(kind: DataDestinationKind) { + constructor(yamlObj: any, kind: DataDestinationKind) { + super(yamlObj) this.kind = kind } diff --git a/tests/build/cle_full.wasm b/tests/build/cle_full.wasm deleted file mode 100644 index c11fb28..0000000 Binary files a/tests/build/cle_full.wasm and /dev/null differ diff --git a/tests/compile.test.ts b/tests/compile.test.ts index 7b3c3ae..62526c4 100644 --- a/tests/compile.test.ts +++ b/tests/compile.test.ts @@ -1,12 +1,12 @@ -import path from 'path' import fs from 'fs' -import { expect, it } from 'vitest' +import { describe, expect, it } from 'vitest' import { objectKeys } from '@murongg/utils' import webjson from '@ora-io/cle-lib/test/weblib/weblib.json' -import { compile } from '../src/api/compile' +import { DEFAULT_PATH } from '../src/common/constants' +import * as cleapi from '../src/index' import { loadYamlFromPath } from './utils/yaml' -// import { config } from './config' import { createOnNonexist } from './utils/file' +import { fixtures } from './fixureoptions' (global as any).__BROWSER__ = false @@ -14,42 +14,42 @@ function readFile(filepath: string) { return fs.readFileSync(filepath, 'utf-8') } -it('test compile', async () => { - const yaml = loadYamlFromPath(path.join(__dirname, 'fixtures/compile/cle.yaml')) - if (!yaml) +const pathfromfixtures = 'dsp/ethereum(storage)' +// const pathfromfixtures = 'dsp/ethereum.unsafe-ethereum' +const option = fixtures[pathfromfixtures] + +describe('test compile', async () => { + it('test compile', async () => { + await testCompile(option) + }, { timeout: 100000 }) +}) + +export async function testCompile(option: any) { + const { mappingPath, yamlPath, wasmPath, watPath } = option + const cleYaml = loadYamlFromPath(yamlPath) + if (!cleYaml) throw new Error('yaml is null') const sources = { ...webjson, - 'src/mapping.ts': readFile(path.join(__dirname, 'fixtures/compile/mapping.ts')), - 'src/cle.yaml': readFile(path.join(__dirname, 'fixtures/compile/cle.yaml')), + 'src/mapping.ts': readFile(mappingPath), + 'src/cle.yaml': readFile(yamlPath), } - const outWasmPath = path.join(__dirname, 'fixtures/build/cle-compiletest.wasm') - const outWatPath = path.join(__dirname, 'fixtures/build/cle-compiletest.wat') + const result = await cleapi.compile(sources) - const result = await compile(sources, { - // yamlPath: 'cle.yaml', - outWasmPath, - outWatPath, - // compilerServerEndpoint: config.CompilerServerEndpoint, - }) if ((result?.stderr as any)?.length > 0) throw new Error(result?.stderr?.toString()) + expect(result.error).toBeNull() expect(objectKeys(result.outputs).length).toBeGreaterThanOrEqual(1) - - const wasmContent = result.outputs[outWasmPath] - const watContent = result.outputs[outWatPath] + const wasmContent = result.outputs[DEFAULT_PATH.OUT_WASM] + const watContent = result.outputs[DEFAULT_PATH.OUT_WAT] expect(wasmContent).toBeDefined() expect(watContent).toBeDefined() - createOnNonexist(outWasmPath) - fs.writeFileSync(outWasmPath, wasmContent) - fs.writeFileSync(outWatPath, watContent) // optional: output compile result for further exec test - // let wasmPath = 'tests/build/cle-compiletest.wasm' - // let watPath = 'tests/build/cle-compiletest.wat' - // fs.writeFileSync(wasmPath, wasmContent) - // fs.writeFileSync(watPath, watContent) -}) + createOnNonexist(wasmPath) + fs.writeFileSync(wasmPath, wasmContent) + fs.writeFileSync(watPath, watContent) +} diff --git a/tests/deposit.test.ts b/tests/deposit.test.ts index 5997542..69c536a 100644 --- a/tests/deposit.test.ts +++ b/tests/deposit.test.ts @@ -1,6 +1,6 @@ import { it } from 'vitest' import { ethers } from 'ethers' -import * as zkgapi from '../src/index' +import * as cleapi from '../src/index' import { config } from './config' (global as any).__BROWSER__ = false @@ -12,7 +12,7 @@ it('test deposit', async () => { const depositAmount = '0.001' const userPrivateKey = config.UserPrivateKey const signer = new ethers.Wallet(userPrivateKey, provider) - const result = await zkgapi.deposit(deployedContractAddress, signer, { depositAmount }) + const result = await cleapi.deposit(deployedContractAddress, signer, { depositAmount }) console.log(result) }, { timeout: 100000 }) diff --git a/tests/dispatch.test.ts b/tests/dispatch.test.ts index ae2ca8c..6b25b3f 100644 --- a/tests/dispatch.test.ts +++ b/tests/dispatch.test.ts @@ -1,6 +1,6 @@ import { it } from 'vitest' import { ethers } from 'ethers' -import * as zkgapi from '../src/index' +import * as cleapi from '../src/index' import { config } from './config' (global as any).__BROWSER__ = false @@ -14,7 +14,7 @@ it('test dispatch', async () => { const privateKey = config.UserPrivateKey const signer = new ethers.Wallet(privateKey, provider) - const dispatcher = new zkgapi.TaskDispatch(queryAPI, contractAddress, feeInWei, provider, signer) + const dispatcher = new cleapi.TaskDispatch(queryAPI, contractAddress, feeInWei, provider, signer) const tx = await dispatcher.setup('cle', 22) await tx.wait() diff --git a/tests/dsp-hub.test.ts b/tests/dsp-hub.test.ts index 5bc95d5..53266bb 100644 --- a/tests/dsp-hub.test.ts +++ b/tests/dsp-hub.test.ts @@ -2,12 +2,17 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import type { CLEYaml } from '../src' import { EthereumDataSourcePlugin } from '../src/dsp/ethereum' import type { DSPHubForeignKeys } from '../src/dsp/hub' -import { dspHub } from '../src/dsp/hub' +import { DSPHub, dspHub } from '../src/dsp/hub' + +const dspHub4test = new DSPHub() describe('DSPHub', () => { + it.only('print all dsp key', () => { + console.log('dsp keys', dspHub.hub.keys()) + }) beforeEach(() => { // Clear the hub before each test - dspHub.hub.clear() + dspHub4test.hub.clear() }) it('should set and get DSPs correctly', () => { @@ -16,9 +21,9 @@ describe('DSPHub', () => { // const foreignKeys: DSPHubForeignKeys = { isLocal: false } const dsp = new EthereumDataSourcePlugin() - dspHub.setDSP(primaryKey, foreignKeys, dsp) + dspHub4test.setDSP(primaryKey, foreignKeys, dsp) - expect(dspHub.getDSP(primaryKey, foreignKeys)).toBe(dsp) + expect(dspHub4test.getDSP(primaryKey, foreignKeys)).toBe(dsp) }) it('should throw an error when getting a non-existent DSP', () => { @@ -27,7 +32,7 @@ describe('DSPHub', () => { // const foreignKeys: DSPHubForeignKeys = { isLocal: false } expect(() => { - dspHub.getDSP(primaryKey, foreignKeys) + dspHub4test.getDSP(primaryKey, foreignKeys) }).toThrowError(`Data Source Plugin Hub Key "${primaryKey}" doesn't exist.`) }) @@ -39,8 +44,8 @@ describe('DSPHub', () => { // const foreignKeys: DSPHubForeignKeys = { isLocal: false } const dsp = new EthereumDataSourcePlugin() - dspHub.setDSP('ethereum', foreignKeys, dsp) + dspHub4test.setDSP('ethereum', foreignKeys, dsp) - expect(dspHub.getDSPByYaml(cleYaml as unknown as CLEYaml, foreignKeys)).toBe(dsp) + expect(dspHub4test.getDSPByYaml(cleYaml as unknown as CLEYaml, foreignKeys)).toBe(dsp) }) }) diff --git a/tests/dsp.test.ts b/tests/dsp.test.ts index 56ac395..553113a 100644 --- a/tests/dsp.test.ts +++ b/tests/dsp.test.ts @@ -1,120 +1,20 @@ -import fs from 'fs' -import path from 'path' -import FormData from 'form-data' -import { describe, expect, it } from 'vitest' -import { objectKeys } from '@murongg/utils' -import webjson from '@ora-io/cle-lib/test/weblib/weblib.json' -import { providers } from 'ethers' -import { fromHexString, loadConfigByNetwork, toHexString } from '../src/common/utils' -import * as zkgapi from '../src/index' -import { loadYamlFromPath } from './utils/yaml' -import { config } from './config' +import { describe, it } from 'vitest' import { fixtures } from './fixureoptions' -import { createOnNonexist } from './utils/file' +import { testCompile } from './compile.test' +import { testExecute } from './exec.test' (global as any).__BROWSER__ = false -function readFile(filepath: string) { - return fs.readFileSync(filepath, 'utf-8') -} - -// const pathfromfixtures = 'dsp/ethereum(storage)' -const pathfromfixtures = 'dsp/ethereum.unsafe-ethereum' +const pathfromfixtures = 'dsp/ethereum(storage)' +// const pathfromfixtures = 'dsp/ethereum.unsafe-ethereum' const option = fixtures[pathfromfixtures] describe(`test dsp: ${pathfromfixtures}`, () => { it('test compile', async () => { - const { mappingPath, yamlPath, wasmPath, watPath } = option - const cleYaml = loadYamlFromPath(yamlPath) - if (!cleYaml) - throw new Error('yaml is null') - - const sources = { - ...webjson, - 'src/mapping.ts': readFile(mappingPath), - 'src/cle.yaml': readFile(yamlPath), - } - - const outWasmPath = path.join(__dirname, 'fixtures/build/cle-compiletest.wasm') - const outWatPath = path.join(__dirname, 'fixtures/build/cle-compiletest.wat') - - const result = await zkgapi.compile(sources, { - outWasmPath, - outWatPath, - }) - - if ((result?.stderr as any)?.length > 0) - throw new Error(result?.stderr?.toString()) - - expect(result.error).toBeNull() - expect(objectKeys(result.outputs).length).toBeGreaterThanOrEqual(1) - const wasmContent = result.outputs[outWasmPath] - const watContent = result.outputs[outWatPath] - expect(wasmContent).toBeDefined() - expect(watContent).toBeDefined() - - // TODO: only write once at the end of "compile test", unnecessary to write these tmp files if complete zkgapi.compile - // optional: output compile result for further exec test - createOnNonexist(wasmPath) - fs.writeFileSync(wasmPath, wasmContent) - fs.writeFileSync(watPath, watContent) - - /** - * TODO: move this into zkgapi.compile - */ - // Set up form data - const data = new FormData() - // data.append("asFile", createReadStream(mappingPath)); - data.append('wasmFile', fs.createReadStream(wasmPath)) - data.append('yamlFile', fs.createReadStream(yamlPath)) - - if (zkgapi.onlyAscCompile(cleYaml) === false) { - // Note: the style is random pick, can align with cli compile - // console.log('remote compile') - await zkgapi.compileRequest(config.CompilerServerEndpoint, data) - .then((response) => { - const wasmModuleHex = response.data.wasmModuleHex - const wasmWat = response.data.wasmWat - - createOnNonexist(wasmPath) - fs.writeFileSync(wasmPath, fromHexString(wasmModuleHex)) - - createOnNonexist(watPath) - fs.writeFileSync(watPath, wasmWat) - }) - } - /** - * end of TODO - */ + await testCompile(option) }, { timeout: 100000 }) it('test exec', async () => { - const { wasmPath, yamlPath, expectedState, blocknum } = option - - const wasm = fs.readFileSync(wasmPath) - const wasmUint8Array = new Uint8Array(wasm) - // const yamlContent = fs.readFileSync(yamlPath, 'utf-8') - const yaml = loadYamlFromPath(yamlPath) as zkgapi.CLEYaml - const dsp = zkgapi.dspHub.getDSPByYaml(yaml, { }) - // const dsp = zkgapi.dspHub.getDSPByYaml(yaml, { isLocal: false }) - - const jsonRpcUrl = loadConfigByNetwork(yaml, config.JsonRpcProviderUrl, true) - const provider = new providers.JsonRpcProvider(jsonRpcUrl) - const generalParams = { - provider, - blockId: loadConfigByNetwork(yaml, blocknum, true), // for storage - // blockId: loadConfigByNetwork(yaml, blocknumForEventTest, true), // for event - } - - const execParams = dsp?.toExecParams(generalParams) - - const state = await zkgapi.execute( - { wasmUint8Array, cleYaml: yaml }, - execParams as any, - ) - // console.log(toHexString(state)) - - expect(toHexString(state)).toEqual(expectedState) - return state + await testExecute(option) }, { timeout: 100000 }) }) diff --git a/tests/exec.test.ts b/tests/exec.test.ts index 938f648..d59eae7 100644 --- a/tests/exec.test.ts +++ b/tests/exec.test.ts @@ -1,97 +1,64 @@ import fs from 'node:fs' import { providers } from 'ethers' -import { describe, it } from 'vitest' -import { loadConfigByNetwork } from '../src/common/utils' -import * as zkgapi from '../src/index' +import { describe, expect, it } from 'vitest' +import { loadConfigByNetwork, toHexString } from '../src/common/utils' +import * as cleapi from '../src/index' import { DSPNotFound } from '../src/common/error' import { config } from './config' -import { getLatestBlocknumber } from './utils/ethers' import { loadYamlFromPath } from './utils/yaml' +import { fixtures } from './fixureoptions' (global as any).__BROWSER__ = false -const blocknumForEventTest = { - sepolia: 2279547, // to test event use 2279547, to test storage use latest blocknum - mainnet: 17633573, +const pathfromfixtures = 'dsp/ethereum(storage)' +// const pathfromfixtures = 'dsp/ethereum.unsafe-ethereum' +const option = fixtures[pathfromfixtures] + +// enable this to silence logs +// cleapi.setCLELogger(new cleapi.SilentLogger()) + +export async function testExecute(option: any) { + const { wasmPath, yamlPath, expectedState, blocknum } = option + + const wasm = fs.readFileSync(wasmPath) + const wasmUint8Array = new Uint8Array(wasm) + // const yamlContent = fs.readFileSync(yamlPath, 'utf-8') + const yaml = loadYamlFromPath(yamlPath) as cleapi.CLEYaml + const dsp = cleapi.dspHub.getDSPByYaml(yaml, { }) + // const dsp = cleapi.dspHub.getDSPByYaml(yaml, { isLocal: false }) + + const jsonRpcUrl = loadConfigByNetwork(yaml, config.JsonRpcProviderUrl, true) + const provider = new providers.JsonRpcProvider(jsonRpcUrl) + const generalParams = { + provider, + blockId: loadConfigByNetwork(yaml, blocknum, true), // for storage + // blockId: loadConfigByNetwork(yaml, blocknumForEventTest, true), // for event + } + + const execParams = dsp?.toExecParams(generalParams) + + const state = await cleapi.execute( + { wasmUint8Array, cleYaml: yaml }, + execParams as any, + ) + // console.log(toHexString(state)) + + expect(toHexString(state)).toEqual(expectedState) + return state } - -const blocknumForStorageTest = { - sepolia: await getLatestBlocknumber(config.JsonRpcProviderUrl.sepolia), - mainnet: await getLatestBlocknumber(config.JsonRpcProviderUrl.mainnet), -} - -const execOptionsForEvent = { - wasmPath: 'tests/build/cle-event.wasm', - yamlPath: 'tests/testsrc/cle-event.yaml', -} - -const execOptionsForStorage = { - wasmPath: 'tests/build/cle-storage.wasm', - yamlPath: 'tests/testsrc/cle-storage.yaml', -} - describe('test exec', () => { - it('test_exec', async () => { - const wasmPath = 'tests/build/cle-latest.wasm' - const yamlPath = 'tests/testsrc/cle-latest.yaml' - - const wasm = fs.readFileSync(wasmPath) - const wasmUint8Array = new Uint8Array(wasm) - // const yamlContent = fs.readFileSync(yamlPath, 'utf-8') - const yaml = loadYamlFromPath(yamlPath) as zkgapi.CLEYaml - const dsp = zkgapi.dspHub.getDSPByYaml(yaml) - - const jsonRpcUrl = loadConfigByNetwork(yaml, config.JsonRpcProviderUrl, true) - const provider = new providers.JsonRpcProvider(jsonRpcUrl) - const generalParams = { - provider, - // blockId: loadConfigByNetwork(yaml, blocknumForStorageTest, true), // for storage - blockId: loadConfigByNetwork(yaml, blocknumForEventTest, true), // for event - } - - const execParams = dsp?.toExecParams(generalParams) - const state = await zkgapi.execute( - { wasmUint8Array, cleYaml: yaml }, - execParams as any, - ) - - return state - }, { timeout: 100000 }) - - it('test_exec_with_latest', async () => { - const { wasmPath, yamlPath } = execOptionsForEvent - - const wasm = fs.readFileSync(wasmPath) - const wasmUint8Array = new Uint8Array(wasm) - // const yamlContent = fs.readFileSync(yamlPath, 'utf-8') - const yaml = loadYamlFromPath(yamlPath) as zkgapi.CLEYaml - const dsp = zkgapi.dspHub.getDSPByYaml(yaml) - - const jsonRpcUrl = loadConfigByNetwork(yaml, config.JsonRpcProviderUrl, true) - const provider = new providers.JsonRpcProvider(jsonRpcUrl) - const generalParams = { - provider, - // blockId: loadConfigByNetwork(yaml, blocknumForStorageTest, true), // for storage - blockId: loadConfigByNetwork(yaml, blocknumForEventTest, true), // for event - } - - const execParams = dsp?.toExecParams(generalParams) - const state = await zkgapi.execute( - { wasmUint8Array, cleYaml: yaml }, - execParams as any, - ) - - return state + it('test exec', async () => { + await testExecute(option) }, { timeout: 100000 }) it('test_exec_with_prepare_data', async () => { - const { wasmPath, yamlPath } = execOptionsForStorage + const { wasmPath, yamlPath, expectedState, blocknum } = option const wasm = fs.readFileSync(wasmPath) const wasmUint8Array = new Uint8Array(wasm) // const yamlContent = fs.readFileSync(yamlPath, 'utf-8') - const yaml = loadYamlFromPath(yamlPath) as zkgapi.CLEYaml - const dsp = zkgapi.dspHub.getDSPByYaml(yaml) + const yaml = loadYamlFromPath(yamlPath) as cleapi.CLEYaml + const dsp = cleapi.dspHub.getDSPByYaml(yaml) if (!dsp) throw new DSPNotFound('DSP not found') @@ -99,7 +66,7 @@ describe('test exec', () => { const provider = new providers.JsonRpcProvider(jsonRpcUrl) const generalParams = { provider, - blockId: loadConfigByNetwork(yaml, blocknumForStorageTest, true), // for storage + blockId: loadConfigByNetwork(yaml, blocknum, true), // for storage // blockId: loadConfigByNetwork(yaml, blocknumForEventTest, true), // for event } @@ -107,20 +74,21 @@ describe('test exec', () => { /** * Prepare Data, can construct your own dataPrep based on this. - * the actual dataPrep here is instance of zkgapi.ETHDSP.EthereumDataPrep + * the actual dataPrep here is instance of cleapi.ETHDSP.EthereumDataPrep */ const dataPrep = await dsp?.prepareData(yaml, await dsp.toPrepareParams(execParams, 'exec')) - const state = await zkgapi.executeOnDataPrep( + const state = await cleapi.executeOnDataPrep( { wasmUint8Array, cleYaml: yaml }, - dataPrep as zkgapi.DataPrep, + dataPrep as cleapi.DataPrep, ) + expect(toHexString(state)).toEqual(expectedState) return state }, { timeout: 100000 }) it('test_exec_then_prove', async () => { - const { wasmPath, yamlPath } = execOptionsForEvent + const { wasmPath, yamlPath, expectedState, blocknum } = option /** * assemble cleExecutable & get dsp @@ -130,10 +98,10 @@ describe('test exec', () => { const wasm = fs.readFileSync(wasmPath) const wasmUint8Array = new Uint8Array(wasm) // const yamlContent = fs.readFileSync(yamlPath, 'utf-8') - const yaml = loadYamlFromPath(yamlPath) as zkgapi.CLEYaml + const yaml = loadYamlFromPath(yamlPath) as cleapi.CLEYaml const cleExecutable = { wasmUint8Array, cleYaml: yaml } // get dsp - const dsp = zkgapi.dspHub.getDSPByYaml(yaml) + const dsp = cleapi.dspHub.getDSPByYaml(yaml) if (!dsp) throw new DSPNotFound('DSP not found') // get pre-defined test params @@ -142,7 +110,7 @@ describe('test exec', () => { const generalParams = { provider, // blockId: loadConfigByNetwork(yaml, blocknumForStorageTest, true), // for storage - blockId: loadConfigByNetwork(yaml, blocknumForEventTest, true), // for event + blockId: loadConfigByNetwork(yaml, blocknum, true), // for event } /** @@ -153,20 +121,22 @@ describe('test exec', () => { const execParams = dsp?.toExecParams(generalParams) // Prepare Data, can construct your own dataPrep based on this. - // the actual dataPrep here is instance of zkgapi.ETHDSP.EthereumDataPrep + // the actual dataPrep here is instance of cleapi.ETHDSP.EthereumDataPrep let dataPrep = await dsp?.prepareData(yaml, await dsp.toPrepareParams(execParams, 'exec')) - const stateu8a = await zkgapi.executeOnDataPrep( + const stateu8a = await cleapi.executeOnDataPrep( { wasmUint8Array, cleYaml: yaml }, - dataPrep as zkgapi.DataPrep, + dataPrep as cleapi.DataPrep, ) - const stateStr = zkgapi.utils.toHexString(stateu8a) + expect(toHexString(stateu8a)).toEqual(expectedState) + + const stateStr = cleapi.utils.toHexString(stateu8a) // /** // * the 2nd way to exec get state. // * gen private/public input (without expectedState) // */ - // let input = new zkgapi.Input(); + // let input = new cleapi.Input(); // input = dsp.fillExecInput(input, yaml, dataPrep) // let [privateInputStr, publicInputStr] = [input.getPrivateInputStr(), input.getPublicInputStr()]; @@ -174,18 +144,18 @@ describe('test exec', () => { // console.log(`(Execute) Public Input: ${publicInputStr}`) // // execute, get state - // const state = await zkgapi.executeOnInputs(cleExecutable, privateInputStr, publicInputStr) + // const state = await cleapi.executeOnInputs(cleExecutable, privateInputStr, publicInputStr) - console.log(`CLE STATE OUTPUT: ${stateStr}`) + // console.log(`CLE STATE OUTPUT: ${stateStr}`) /** * Prove Input Gen */ dataPrep = dsp?.toProveDataPrep(dataPrep, stateStr) - const input = zkgapi.proveInputGenOnDataPrep(cleExecutable, dataPrep as zkgapi.DataPrep) - - console.log(`(Prove) Private Input: ${input.getPrivateInputStr()}`) - console.log(`(Prove) Public Input: ${input.getPublicInputStr()}`) + const input = cleapi.proveInputGenOnDataPrep(cleExecutable, dataPrep as cleapi.DataPrep) + input + // console.log(`(Prove) Private Input: ${input.getPrivateInputStr()}`) + // console.log(`(Prove) Public Input: ${input.getPublicInputStr()}`) }, { timeout: 100000 }) }) diff --git a/tests/fixtures/compile/cle.yaml b/tests/fixtures/compile/cle.yaml index 5625c57..e118d77 100644 --- a/tests/fixtures/compile/cle.yaml +++ b/tests/fixtures/compile/cle.yaml @@ -2,7 +2,7 @@ specVersion: 0.0.2 apiVersion: 0.0.2 name: eg_hello description: "This demo CLE always set a ascii string as the output state. " -repository: https://github.com/ora-io/zkgraph +repository: https://github.com/ora-io/cle dataSources: - kind: ethereum network: sepolia diff --git a/tests/fixtures/dsp/ethereum(event)/cle-event.yaml b/tests/fixtures/dsp/ethereum(event)/cle-event.yaml index 0f574f2..3e7fcd8 100644 --- a/tests/fixtures/dsp/ethereum(event)/cle-event.yaml +++ b/tests/fixtures/dsp/ethereum(event)/cle-event.yaml @@ -2,7 +2,7 @@ specVersion: 0.0.2 apiVersion: 0.0.2 name: eg_addr description: 'Dev Test CLE. ' -repository: https://github.com/ora-io/zkgraph +repository: https://github.com/ora-io/cle dataSources: - kind: ethereum network: sepolia diff --git a/tests/fixtures/dsp/ethereum(storage)/cle-event.yaml b/tests/fixtures/dsp/ethereum(storage)/cle-event.yaml index 40aa5c2..b3ca676 100644 --- a/tests/fixtures/dsp/ethereum(storage)/cle-event.yaml +++ b/tests/fixtures/dsp/ethereum(storage)/cle-event.yaml @@ -2,7 +2,7 @@ specVersion: 0.0.2 apiVersion: 0.0.2 name: eg_addr description: 'Dev Test CLE. ' -repository: https://github.com/ora-io/zkgraph +repository: https://github.com/ora-io/cle dataSources: - kind: ethereum network: sepolia diff --git a/tests/fixtures/dsp/ethereum.unsafe-ethereum/cle-event.yaml b/tests/fixtures/dsp/ethereum.unsafe-ethereum/cle-event.yaml index 4213f7f..85b6fd9 100644 --- a/tests/fixtures/dsp/ethereum.unsafe-ethereum/cle-event.yaml +++ b/tests/fixtures/dsp/ethereum.unsafe-ethereum/cle-event.yaml @@ -2,7 +2,7 @@ specVersion: 0.0.2 apiVersion: 0.0.2 name: eg_addr description: 'Dev Test CLE. ' -repository: https://github.com/ora-io/zkgraph +repository: https://github.com/ora-io/cle dataSources: - kind: ethereum unsafe: true diff --git a/tests/fixtures/dsp/ethereum.unsafe/cle-event.yaml b/tests/fixtures/dsp/ethereum.unsafe/cle-event.yaml index 55dd8db..c2bfda7 100644 --- a/tests/fixtures/dsp/ethereum.unsafe/cle-event.yaml +++ b/tests/fixtures/dsp/ethereum.unsafe/cle-event.yaml @@ -2,7 +2,7 @@ specVersion: 0.0.2 apiVersion: 0.0.2 name: eg_addr description: 'Dev Test CLE. ' -repository: https://github.com/ora-io/zkgraph +repository: https://github.com/ora-io/cle dataSources: - kind: ethereum unsafe: true diff --git a/tests/prove.test.ts b/tests/prove.test.ts index c80e3c3..9739123 100644 --- a/tests/prove.test.ts +++ b/tests/prove.test.ts @@ -2,7 +2,7 @@ import fs from 'node:fs' import { ethers, providers } from 'ethers' import { describe, expect, it } from 'vitest' import { loadConfigByNetwork } from '../src/common/utils' -import * as zkgapi from '../src/index' +import * as cleapi from '../src/index' import { config } from './config' import { loadYamlFromPath } from './utils/yaml' import { fixtures } from './fixureoptions' @@ -31,8 +31,8 @@ describe(`test prove ${pathfromfixtures}`, () => { const wasm = fs.readFileSync(wasmPath) const wasmUint8Array = new Uint8Array(wasm) // const yamlContent = fs.readFileSync(yamlPath, 'utf-8') - const yaml = loadYamlFromPath(yamlPath) as zkgapi.CLEYaml - const dsp = zkgapi.dspHub.getDSPByYaml(yaml) + const yaml = loadYamlFromPath(yamlPath) as cleapi.CLEYaml + const dsp = cleapi.dspHub.getDSPByYaml(yaml) const jsonRpcUrl = loadConfigByNetwork(yaml, config.JsonRpcProviderUrl, true) const provider = new providers.JsonRpcProvider(jsonRpcUrl) const generalParams = { @@ -42,14 +42,14 @@ describe(`test prove ${pathfromfixtures}`, () => { } const proveParams = dsp?.toProveParams(generalParams) - const input = await zkgapi.proveInputGen( + const input = await cleapi.proveInputGen( { cleYaml: yaml }, // doesn't care about wasmUint8Array proveParams as any, ) console.log(input.auxParams) - const res = await zkgapi.proveMock( + const res = await cleapi.proveMock( { wasmUint8Array }, input, ) @@ -59,9 +59,9 @@ describe(`test prove ${pathfromfixtures}`, () => { const { wasmPath, yamlPath, zkwasmUrl, blocknum, expectedState } = option const wasm = fs.readFileSync(wasmPath) const wasmUint8Array = new Uint8Array(wasm) - const yaml = loadYamlFromPath(yamlPath) as zkgapi.CLEYaml + const yaml = loadYamlFromPath(yamlPath) as cleapi.CLEYaml - const dsp = zkgapi.dspHub.getDSPByYaml(yaml) + const dsp = cleapi.dspHub.getDSPByYaml(yaml) const jsonRpcUrl = loadConfigByNetwork(yaml, config.JsonRpcProviderUrl, true) const provider = new providers.JsonRpcProvider(jsonRpcUrl) @@ -73,7 +73,7 @@ describe(`test prove ${pathfromfixtures}`, () => { const proveParams = dsp?.toProveParams(generalParams) - const input = await zkgapi.proveInputGen( + const input = await cleapi.proveInputGen( { cleYaml: yaml }, // doesn't care about wasmUint8Array proveParams as any, ) @@ -84,10 +84,14 @@ describe(`test prove ${pathfromfixtures}`, () => { const userPrivateKey = config.UserPrivateKey const signer = new ethers.Wallet(userPrivateKey, provider) - const result = await zkgapi.requestProve( + const result = await cleapi.requestProve( { wasmUint8Array }, // doesn't care about cleYaml input, - { proverUrl: zkwasmUrl, signer }) + { + proverUrl: zkwasmUrl, + signer, + batchStyle: cleapi.BatchStyle.ORA, + }) console.log(result) expect(result.taskId).toBeTypeOf('string') @@ -96,7 +100,7 @@ describe(`test prove ${pathfromfixtures}`, () => { it.only('test waitProve', async () => { const { zkwasmUrl } = option const taskId = '65dae256429af08ed922479a' - const result = await zkgapi.waitProve(zkwasmUrl, taskId as string, { batchStyle: zkgapi.BatchStyle.ZKWASMHUB }) + const result = await cleapi.waitProve(zkwasmUrl, taskId as string, { batchStyle: cleapi.BatchStyle.ZKWASMHUB }) // console.log(result.proofParams?.instances) expect((result.proofParams?.instances as any[])[0]).toBeInstanceOf(Array) }, { timeout: 100000 }) diff --git a/tests/publish.test.ts b/tests/publish.test.ts index dcd0472..258dccd 100644 --- a/tests/publish.test.ts +++ b/tests/publish.test.ts @@ -1,16 +1,25 @@ import fs from 'node:fs' import { Contract, ethers } from 'ethers' import { expect, it } from 'vitest' -import { GraphAlreadyExist } from '../src/common/error' -import * as zkgapi from '../src/index' -import { DEFAULT_URL, abiFactory, addressFactory, cle_abi } from '../src/common/constants' +import { CLEAlreadyExist } from '../src/common/error' +import * as cleapi from '../src/index' +import { DEFAULT_URL, abiFactory, addressFactory, cleContractABI } from '../src/common/constants' import { config } from './config' import { loadYamlFromPath } from './utils/yaml' +import { fixtures } from './fixureoptions' (global as any).__BROWSER__ = false -it('test publish', async () => { - const cleYaml = loadYamlFromPath('tests/testsrc/cle-dirty.yaml') as zkgapi.CLEYaml +const pathfromfixtures = 'dsp/ethereum(storage)' +// const pathfromfixtures = 'dsp/ethereum.unsafe-ethereum' +const option = fixtures[pathfromfixtures] + +// enable this to silence logs +// cleapi.setCLELogger(new cleapi.SilentLogger()) + +it.skip('test publish', async () => { + const { wasmPath, yamlPath } = option + const cleYaml = loadYamlFromPath(yamlPath) as cleapi.CLEYaml const network = cleYaml.decidePublishNetwork() console.log('network', network) expect(network).toBeDefined() @@ -24,13 +33,13 @@ it('test publish', async () => { const signer = new ethers.Wallet(userPrivateKey, provider) const ipfsHash = Math.floor(Math.random() * (100000 - 0 + 1)).toString() const newBountyRewardPerTrigger = 0.01 - const wasm = fs.readFileSync('tests/build/cle_full.wasm') + const wasm = fs.readFileSync(wasmPath) const wasmUint8Array = new Uint8Array(wasm) try { // proverUrl?: string, // ipfsHash: string, // bountyRewardPerTrigger: number, - const publishTxHash = await zkgapi.publish( + const publishTxHash = await cleapi.publish( { wasmUint8Array, cleYaml }, signer, { proverUrl: DEFAULT_URL.ZKWASMHUB, ipfsHash, bountyRewardPerTrigger: newBountyRewardPerTrigger }, @@ -43,17 +52,17 @@ it('test publish', async () => { // console.log(address); const factoryContract = new Contract(addressFactory.sepolia, abiFactory, signer) - const deployedAddress = await factoryContract.getGraphBycreator(signer.address) + const deployedAddress = await factoryContract.getCLEBycreator(signer.address) - const graphContract = new Contract(deployedAddress[deployedAddress.length - 1], cle_abi, provider).connect(signer) + const cleContract = new Contract(deployedAddress[deployedAddress.length - 1], cleContractABI, provider).connect(signer) - const reward = await graphContract.bountyReward() + const reward = await cleContract.bountyReward() // expect reward is equal to newBountyRewardPerTrigger expect(reward).toEqual(ethers.utils.parseEther(newBountyRewardPerTrigger.toString())) } catch (error) { - if (error instanceof GraphAlreadyExist) - console.error('Graph already exist') + if (error instanceof CLEAlreadyExist) + console.error('CLE already exist') else throw error } diff --git a/tests/setup.test.ts b/tests/setup.test.ts index 9cb6e6a..207f9e9 100644 --- a/tests/setup.test.ts +++ b/tests/setup.test.ts @@ -1,7 +1,7 @@ import fs from 'node:fs' import { describe, it } from 'vitest' import { ethers, providers } from 'ethers' -import * as zkgapi from '../src/index' +import * as cleapi from '../src/index' import { DEFAULT_URL } from '../src/common/constants' import { config } from './config' import { fixtures } from './fixureoptions' @@ -16,14 +16,11 @@ describe('test setup', () => { const { wasmPath } = option const wasm = fs.readFileSync(wasmPath) const wasmUint8Array = new Uint8Array(wasm) - // const yaml = loadYamlFromPath(yamlPath) as zkgapi.CLEYaml - // const image = createFileFromUint8Array(wasm, 'poc.wasm') - // const jsonRpcUrl = loadConfigByNetwork(yaml, config.JsonRpcProviderUrl, true) - // const provider = new providers.JsonRpcProvider(jsonRpcUrl) const provider = new providers.JsonRpcProvider('http://localhost') // not important const signer = new ethers.Wallet(config.UserPrivateKey, provider) - const result = await zkgapi.setup( + const result = await cleapi.setup( { wasmUint8Array }, + // { circuitSize: 22, proverUrl: DEFAULT_URL.ZKWASMHUB, signer }, { circuitSize: 22, proverUrl: DEFAULT_URL.ZKWASMHUB, signer }, ) result diff --git a/tests/testsrc/cle-dirty.yaml b/tests/testsrc/cle-dirty.yaml deleted file mode 100644 index 8729873..0000000 --- a/tests/testsrc/cle-dirty.yaml +++ /dev/null @@ -1,29 +0,0 @@ -specVersion: 0.0.2 -apiVersion: 0.0.2 -name: eg_addr -description: 'Demo CLE. ' -repository: https://github.com/ora-io/zkgraph -dataSources: - - kind: ethereum - network: mainnet - # block: - # - offset: [0] - event: - - address: '0xa60ecf32309539dd84f27a9563754dca818b815e' - events: - - 'Sync(uint112,uint112)' - - '0xa818b815ea60ecf3230563754dc9539dd84f27a98b815ea60ecf32309539dd84' - - address: '0x9dd84f27a9563a60ecf3230953754dca818b815e' - events: - - 'Sync(uint112,uint112)' - - '0x63754dc9539dda818b815ea60ecf3230584f27a98b815ea60ecf32309539dd84' - -mapping: - language: wasm/assemblyscript - file: ./mapping.ts - handler: handleBlocks - -dataDestinations: - - kind: ethereum - network: sepolia - address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' diff --git a/tests/testsrc/cle-event-unsafe.yaml b/tests/testsrc/cle-event-unsafe.yaml deleted file mode 100644 index 9d49438..0000000 --- a/tests/testsrc/cle-event-unsafe.yaml +++ /dev/null @@ -1,30 +0,0 @@ -specVersion: 0.0.2 -apiVersion: 0.0.2 -name: eg_addr -description: 'Demo CLE. ' -repository: https://github.com/ora-io/zkgraph -dataSources: - - kind: ethereum - unsafe: true - network: sepolia - # block: - # - offset: [0] - event: - - address: '0xa60ecf32309539dd84f27a9563754dca818b815e' - events: - - 'Sync(uint112,uint112)' - - 'Sync1(uint112,uint112)' - - address: '0x5c7a6cf20cbd3eef32e19b9cad4eca17c432a794' - events: - - 'SubmissionReceived(int256,uint32,address)' - - '0x63754dc9539dda818b815ea60ecf3230584f27a98b815ea60ecf32309539dd84' - -mapping: - language: wasm/assemblyscript - file: ./mapping.ts - handler: handleBlocks - -dataDestinations: - - kind: ethereum - network: sepolia - address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' diff --git a/tests/testsrc/cle-event.yaml b/tests/testsrc/cle-event.yaml deleted file mode 100644 index 16e7e51..0000000 --- a/tests/testsrc/cle-event.yaml +++ /dev/null @@ -1,29 +0,0 @@ -specVersion: 0.0.2 -apiVersion: 0.0.2 -name: eg_addr -description: 'Demo CLE. ' -repository: https://github.com/ora-io/zkgraph -dataSources: - - kind: ethereum - network: sepolia - # block: - # - offset: [0] - event: - - address: '0xa60ecf32309539dd84f27a9563754dca818b815e' - events: - - 'Sync(uint112,uint112)' - - 'Sync1(uint112,uint112)' - - address: '0x5c7a6cf20cbd3eef32e19b9cad4eca17c432a794' - events: - - 'SubmissionReceived(int256,uint32,address)' - - '0x63754dc9539dda818b815ea60ecf3230584f27a98b815ea60ecf32309539dd84' - -mapping: - language: wasm/assemblyscript - file: ./mapping.ts - handler: handleBlocks - -dataDestinations: - - kind: ethereum - network: sepolia - address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' diff --git a/tests/testsrc/cle-latest.yaml b/tests/testsrc/cle-latest.yaml deleted file mode 100644 index c2aebe1..0000000 --- a/tests/testsrc/cle-latest.yaml +++ /dev/null @@ -1,22 +0,0 @@ -specVersion: 0.0.2 -apiVersion: 0.0.2 -name: zkAuto-DEMO -description: 'This demo zkGraph shows Uniswap price-based zkAutomation case. Calculate ETH/USDC price, set it as the trigger payload whenever price > theshold.' -repository: https://github.com/ora-io/zkgraph -dataSources: - - kind: ethereum - network: sepolia - event: - - address: '0x362b63b77448C2ffBC03511FE100fCd3d879795F' - events: - - 'eventTriggered(uint256,uint256)' - -mapping: - language: wasm/assemblyscript - file: ./mapping.ts - handler: handleBlocks - -dataDestinations: - - kind: ethereum - network: sepolia - address: '0x362b63b77448C2ffBC03511FE100fCd3d879795F' diff --git a/tests/testsrc/cle-storage.yaml b/tests/testsrc/cle-storage.yaml deleted file mode 100644 index df932eb..0000000 --- a/tests/testsrc/cle-storage.yaml +++ /dev/null @@ -1,29 +0,0 @@ -specVersion: 0.0.2 -apiVersion: 0.0.2 -name: eg_addr -description: 'Demo CLE. ' -repository: https://github.com/ora-io/zkgraph -dataSources: - - kind: ethereum - network: sepolia - # block: - # - offset: [0] - storage: - - address: '0xa60ecf32309539dd84f27a9563754dca818b815e' - slots: - - 8 - - 9 - # - address: "0x539dd84f27a9563754dcaa60ecf32309818b815e" - # slots: - # - 1 - # - 2 - -mapping: - language: wasm/assemblyscript - file: ./mapping.ts - handler: handleBlocks - -dataDestinations: - - kind: ethereum - network: sepolia - address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' diff --git a/tests/testsrc/cle-tx.yaml b/tests/testsrc/cle-tx.yaml deleted file mode 100644 index fc45cc9..0000000 --- a/tests/testsrc/cle-tx.yaml +++ /dev/null @@ -1,25 +0,0 @@ -specVersion: 0.0.2 -apiVersion: 0.0.2 -name: eg_addr -description: 'Demo CLE. ' -repository: https://github.com/ora-io/zkgraph -dataSources: - - kind: ethereum - network: sepolia - # block: - # - offset: [0] - transaction: - - from: '0x1E2cD78882b12d3954a049Fd82FFD691565dC0A5' - to: '*' - - from: '*' - to: '0x33B47b485d56FaCb821442f504eDD93efaFa6B03' - -mapping: - language: wasm/assemblyscript - file: ./mapping.ts - handler: handleBlocks - -dataDestinations: - - kind: ethereum - network: sepolia - address: '0x1B17C66e37CB33202Fd1C058fa1B97e36b7e517D' diff --git a/tests/trigger.test.ts b/tests/trigger.test.ts index 6327584..27394f7 100644 --- a/tests/trigger.test.ts +++ b/tests/trigger.test.ts @@ -1,32 +1,38 @@ import { describe } from 'node:test' import { it } from 'vitest' import { ethers } from 'ethers' -import * as zkgapi from '../src/index' +import * as cleapi from '../src/index' import { DEFAULT_URL } from '../src/common/constants' import { loadYamlFromPath } from './utils/yaml' import { config } from './config' +import { fixtures } from './fixureoptions' (global as any).__BROWSER__ = false // const rpcUrl = 'https://rpc.ankr.com/eth_sepolia' -const yamlPath = 'tests/testsrc/cle-event.yaml' +const pathfromfixtures = 'dsp/ethereum(storage)' +// const pathfromfixtures = 'dsp/ethereum.unsafe-ethereum' +const option = fixtures[pathfromfixtures] // let ZkwasmProviderUrl = "https://zkwasm-explorer.delphinuslab.com:8090" -// let proveTaskId = "6554584c82ab2c8b29dbc2c2" // true -const proveTaskId = '655568eaadb2c56ffd2f0ee0' // fasle +const proveTaskId = '65dd7dad235cd47b5193efce' // true +// const proveTaskId = '655568eaadb2c56ffd2f0ee0' // fasle + +// TODO: use a reward == 0 cle to pass trigger test describe('test trigger', () => { - const yaml = loadYamlFromPath(yamlPath) + it('eth ddp', async () => { + const { yamlPath } = option + const yaml = loadYamlFromPath(yamlPath) - it('test verify proof params', async () => { - const proofParams = await zkgapi.getVerifyProofParamsByTaskID(DEFAULT_URL.ZKWASMHUB, proveTaskId) - const CLEID = '0x870ef9B5DcBB6F71139a5f35D10b78b145853e69' + const proofParams = await cleapi.getVerifyProofParamsByTaskID(DEFAULT_URL.ZKWASMHUB, proveTaskId) + const CLEID = '0x8fd9e85b23d3777993ebf04ad3a3b0878f7fee77' const userPrivateKey = config.UserPrivateKey const rpcUrl = config.JsonRpcProviderUrl.sepolia const provider = new ethers.providers.JsonRpcProvider(rpcUrl) const signer = new ethers.Wallet(userPrivateKey, provider) - const ddpParams = { signer, gasLimit: 3000000 } - await zkgapi.trigger( + const ddpParams = { signer, gasLimit: 10000000, onlyMock: true } + await cleapi.trigger( { cleYaml: yaml }, CLEID, proofParams, diff --git a/tests/utils.test.ts b/tests/utils.test.ts index d945da6..e96a47d 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -1,41 +1,57 @@ -import { describe, it } from 'vitest' +import fs from 'node:fs' +import { describe, expect, it } from 'vitest' import { ethers } from 'ethers' import { ZkWasmUtil } from '@ora-io/zkwasm-service-helper' -import * as zkgapi from '../src/index' +import yaml from 'js-yaml' +import * as cleapi from '../src/index' import { u32ListToUint8Array } from '../src/common/utils' import { config } from './config' import { loadYamlFromPath } from './utils/yaml' +import { fixtures } from './fixureoptions' (global as any).__BROWSER__ = false describe('test utils', () => { it('test healthcheck', () => { - test_healthcheck('tests/testsrc/cle-event.yaml') - test_healthcheck('tests/testsrc/cle-storage.yaml') - test_healthcheck('tests/testsrc/cle-dirty.yaml') + test_healthcheck(fixtures['dsp/ethereum(storage)'].yamlPath) + test_healthcheck(fixtures['dsp/ethereum(event)'].yamlPath) }) it('getRawReceipts', async () => { const provider = new ethers.providers.JsonRpcProvider(config.JsonRpcProviderUrl.sepolia) - const res = await zkgapi.getRawReceipts(provider, 4818711, false) + const res = await cleapi.getRawReceipts(provider, 4818711, false) res // console.log(res) }, { timeout: 100000 }) - it('u32ListToUint8Array', async () => { + it('u32ListToUint8Array', () => { const blocknums = [5353087, 5353088] const result = u32ListToUint8Array(blocknums, 32) - console.log('test u32ListToUint8Array', result) + // console.log('test u32ListToUint8Array', result) const extra = ZkWasmUtil.bytesToBigIntArray(result) console.log('test u32ListToUint8Array extra', extra) // expect(result).equals(new Uint8Array([ 127, 174, 81, 0 ])) // imcomplete }) + + it('yaml filterSections', () => { + const yaml = loadYamlFromPath(fixtures['dsp/ethereum(event)'].yamlPath) as any + expect(yaml.dataSources[0].filterByKeys(['event', 'storage']).event).toBeInstanceOf(Array) + }) + it('yaml toString', () => { + const yamlpath = fixtures['dsp/ethereum(event)'].yamlPath + const yamlContents = fs.readFileSync(yamlpath, 'utf8') + const expectedYamlDump = yaml.dump(yaml.load(yamlContents)) + + const yamlObj = loadYamlFromPath(yamlpath) as any + + expect(yamlObj.toString()).equals(expectedYamlDump) + }) }) function test_healthcheck(yamlPath: string) { try { const yaml1 = loadYamlFromPath(yamlPath) as any - zkgapi.CLEYaml.healthCheck(yaml1) + cleapi.CLEYaml.healthCheck(yaml1) console.log('valid:', yamlPath) } catch (e) { diff --git a/tests/utils/yaml.ts b/tests/utils/yaml.ts index 338bc25..ba1544b 100644 --- a/tests/utils/yaml.ts +++ b/tests/utils/yaml.ts @@ -1,13 +1,6 @@ import fs from 'node:fs' import { CLEYaml } from '../../src' export function loadYamlFromPath(path: string) { - let fileContents = '' - try { - // Read the YAML file contents - fileContents = fs.readFileSync(path, 'utf8') - } - catch (error) { - console.error(error) - } + const fileContents = fs.readFileSync(path, 'utf8') return CLEYaml.fromYamlContent(fileContents) } diff --git a/tests/verify.test.ts b/tests/verify.test.ts index 0b02d27..5298d0a 100644 --- a/tests/verify.test.ts +++ b/tests/verify.test.ts @@ -1,50 +1,50 @@ import { describe } from 'node:test' import { expect, it } from 'vitest' import { ethers } from 'ethers' -import * as zkgapi from '../src/index' +import * as cleapi from '../src/index' import { AggregatorVerifierAddress, DEFAULT_URL } from '../src/common/constants' import { loadYamlFromPath } from './utils/yaml' +import { fixtures } from './fixureoptions' (global as any).__BROWSER__ = false const rpcUrl = 'https://rpc.ankr.com/eth_sepolia' -const yamlPath = 'tests/testsrc/cle-event.yaml' +const yamlPath = fixtures['dsp/ethereum(event)'].yamlPath // let ZkwasmProviderUrl = "https://zkwasm-explorer.delphinuslab.com:8090" -// let proveTaskId = "6554584c82ab2c8b29dbc2c2" // true -const proveTaskId = '65d1c1edc3e455a0eebd7bb6' // fasle +const proveTaskId = '65dd7dad235cd47b5193efce' // true +// const proveTaskId = '65d1c1edc3e455a0eebd7bb6' // fasle describe('test verify', () => { const cleYaml = loadYamlFromPath(yamlPath) it('test verify CLEExecutable', async () => { - const verifyParams = await zkgapi.getVerifyProofParamsByTaskID(DEFAULT_URL.ZKWASMHUB, proveTaskId) + const verifyParams = await cleapi.getVerifyProofParamsByTaskID(DEFAULT_URL.ZKWASMHUB, proveTaskId) const network = cleYaml.decidePublishNetwork() - console.log('network', network) expect(network).toBeDefined() if (network === undefined) throw new Error('network is undefined') // const verifierContractAddress = loadConfigByNetwork(cleYaml as CLEYaml, AggregatorVerifierAddress, false) const verifierAddress = (AggregatorVerifierAddress as any)[network] - expect(await zkgapi.verify( + expect(await cleapi.verify( verifyParams, { verifierAddress, provider: new ethers.providers.JsonRpcProvider(rpcUrl) }, )).toBeTruthy() }) // 2nd way to verify proof. it('test verify proof params', async () => { - const proofParams = await zkgapi.getVerifyProofParamsByTaskID(DEFAULT_URL.ZKWASMHUB, proveTaskId) + const proofParams = await cleapi.getVerifyProofParamsByTaskID(DEFAULT_URL.ZKWASMHUB, proveTaskId) const sepolia_verifier = '0xfD74dce645Eb5EB65D818aeC544C72Ba325D93B0' - expect(await zkgapi.verifyProof( + expect(await cleapi.verifyProof( proofParams, { verifierAddress: sepolia_verifier, provider: new ethers.providers.JsonRpcProvider(rpcUrl) }, )).toBeTruthy() // make a wrong proof proofParams.aggregate_proof[0] = 0x12 - expect(await zkgapi.verifyProof( + expect(await cleapi.verifyProof( proofParams, { verifierAddress: sepolia_verifier, provider: new ethers.providers.JsonRpcProvider(rpcUrl) }, )).toBeFalsy()