Skip to content

Commit

Permalink
Merge pull request #75 from ora-io/feat/contractupgrade
Browse files Browse the repository at this point in the history
Feat/contractupgrade
  • Loading branch information
nom4dv3 committed Feb 27, 2024
2 parents b5dae1e + 41f640a commit 1a56af5
Show file tree
Hide file tree
Showing 39 changed files with 726 additions and 886 deletions.
26 changes: 13 additions & 13 deletions src/api/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -120,28 +121,28 @@ 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)
return result

// 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)
Expand Down Expand Up @@ -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<string, string | Uint8Array> = {}
// 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__) {

Expand Down
4 changes: 2 additions & 2 deletions src/api/deposit_bounty.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Contract, ethers } from 'ethers'
import {
cle_abi,
cleContractABI,
} from '../common/constants'

/**
Expand All @@ -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) },
Expand Down
45 changes: 36 additions & 9 deletions src/api/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion src/api/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export async function trigger(
): Promise<void> {
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])
}
6 changes: 3 additions & 3 deletions src/api/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading

0 comments on commit 1a56af5

Please sign in to comment.