Skip to content

Commit

Permalink
feat: use compile api
Browse files Browse the repository at this point in the history
  • Loading branch information
murongg committed Jan 3, 2024
1 parent 9d2eabe commit 23fd80e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 134 deletions.
97 changes: 15 additions & 82 deletions packages/zkgraph-cli/src/commands/compile.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { execSync } from 'node:child_process'
import fs from 'node:fs'
import path from 'node:path'
import to from 'await-to-js'
import FormData from 'form-data'
import type { AxiosRequestConfig } from 'axios'
import axios from 'axios'
import * as zkgapi from '@hyperoracle/zkgraph-api'
import { codegen, createOnNonexist, fromHexString, loadYamlFromPath, randomUniqueKey } from '../utils'
import webjson from '@hyperoracle/zkgraph-lib/test/weblib/weblib.json'
import { createOnNonexist, fromHexString, loadYamlFromPath } from '../utils'
import { logger } from '../logger'
import { parseTemplateTag } from '../tag'
import { COMPILE_CODEGEN, COMPILE_TEMP_ENTRY_FILE_NAME_TEMPLATE } from '../constants'
import { checkExecExist } from '../utils/system'

export interface CompileOptions {
Expand All @@ -21,8 +19,6 @@ export interface CompileOptions {
mappingPath: string
}

const wasmStartName = '__as_start'

export async function compile(options: CompileOptions) {
const {
wasmPath,
Expand Down Expand Up @@ -59,39 +55,24 @@ async function compileLocal(options: CompileOptions) {
return false
}

// general compile based on dsp. so local should be a boolean var rather than 'true'
const dsp = zkgapi.dspHub.getDSPByYaml(yaml, { isLocal: local })
if (!dsp) {
logger.error('[-] ERROR: Failed to get DSP')
const res = await zkgapi.compile({ zkgraphYaml: yaml }, {
...webjson,
'mapping.ts': getMappingContent(mappingPath),
}, { isLocal: local })

if (res.error) {
logger.error(`[-] COMPILATION ERROR. ${res.error.message}`)
return false
}
const wasmContent = res.outputs['inner_pre_pre.wasm']
const watContent = res.outputs['inner_pre_pre.wat']

// for CODE_GEN code, define imported lib function name
const libDSPName = dsp.getLibDSPName()

const mappingFileName = yaml.mapping.file
const handleFuncName = yaml.mapping.handler

// for entry file name only, not important.
const dspKey = zkgapi.dspHub.toHubKeyByYaml(yaml, { isLocal: local })

const srcDirPath = path.join(mappingPath, '..')
const entryFilename = getEntryFilename(dspKey)
const entryFilePath = await codegen(srcDirPath, entryFilename, COMPILE_CODEGEN(libDSPName, mappingFileName, handleFuncName))

// const innerPrePrePath = path.join(path.dirname(wasmPath), '/temp/inner_pre_pre.wasm')
createOnNonexist(wasmPath)

// const [compileErr] = await to(ascCompile(path.join(srcDirPath, entryFilePath), innerPrePrePath, `${innerPrePrePath}.wat`))
const [compileErr] = await to(ascCompile(path.join(srcDirPath, entryFilePath), wasmPath, watPath))
fs.writeFileSync(wasmPath, wasmContent)
fs.writeFileSync(watPath, watContent)

if (compileErr) {
logger.error(`[-] COMPILATION ERROR. ${compileErr.message}`)
return false
}
return true

// logCompileResult(wasmPath, watPath)
}

async function compileServer(options: CompileOptions) {
Expand Down Expand Up @@ -156,33 +137,6 @@ async function compileServer(options: CompileOptions) {
fs.writeFileSync(watPath, wasmWat)

return true
// logCompileResult(wasmPath, watPath)
}

async function ascCompile(entryFilePath: string, outputWasmPath: string, outputWatPath: string) {
const abortPath = getAbortTsFilepath(entryFilePath)

let commands: string[] = [
'npx asc',
]
const common = [
`-o ${outputWasmPath}`,
`-t ${outputWatPath}`,
'-O', '--noAssert',
'--disable', 'bulk-memory',
'--disable', 'mutable-globals',
'--exportRuntime',
'--exportStart', wasmStartName,
'--memoryBase', '70000',
'--runtime stub',
]
commands = commands.concat([
entryFilePath,
'--use', `abort=${abortPath}`,
])
commands = commands.concat(common)

return await execAndRmSync(commands.join(' '), entryFilePath)
}

function logCompileResult(wasmPath: string, watPath: string): void {
Expand All @@ -195,27 +149,6 @@ function logCompileResult(wasmPath: string, watPath: string): void {
logger.info('[+] COMPILATION SUCCESS!' + '\n')
}

async function execAndRmSync(command: string, filepath: string) {
return new Promise<void>((resolve, reject) => {
try {
execSync(command)
// norman: rmSync should seperate from execSync
fs.rmSync(filepath)
resolve()
}
catch (error) {
reject(error)
}
})
}

function getEntryFilename(env: string) {
return parseTemplateTag(COMPILE_TEMP_ENTRY_FILE_NAME_TEMPLATE, {
env,
salt: randomUniqueKey(10),
})
}

function getAbortTsFilepath(innerTsFilePath: string) {
return `${innerTsFilePath.replace(process.cwd(), '').substring(1).replace('.ts', '')}/abort`.replaceAll('\\', '/')
function getMappingContent(filepath: string) {
return fs.readFileSync(filepath, 'utf-8')
}
22 changes: 0 additions & 22 deletions packages/zkgraph-cli/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,6 @@ export const TAGS = {
root: process.cwd(),
}

export const COMPILE_TEMP_ENTRY_FILE_NAME_TEMPLATE = 'entry_[salt].[env].ts'

export const COMPILE_CODEGEN = (libDSPName: string, mappingFileName: string, handleFuncName: string) => `
import { zkmain_lib, asmain_lib, registerHandle } from "@hyperoracle/zkgraph-lib/dsp/${libDSPName}"
import { ${handleFuncName} } from "./${mappingFileName}"
declare function __call_as_start(): void;
export function zkmain(): void {
__call_as_start();
registerHandle(${handleFuncName})
return zkmain_lib()
}
export function asmain(): Uint8Array {
__call_as_start();
registerHandle(${handleFuncName})
return asmain_lib()
}
function abort(a: usize, b: usize, c: u32, d: u32): void {}
`

export const NETWORKS = [
{
name: 'Sepolia',
Expand Down
15 changes: 0 additions & 15 deletions packages/zkgraph-cli/src/utils/code.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
import path from 'node:path'
import fs from 'node:fs'
export async function codegen(mappingRoot: string, filename: string, content: string) {
return new Promise<string>((resolve, reject) => {
try {
const filepath = path.join(mappingRoot, filename)
fs.writeFileSync(filepath, content, 'utf-8')
resolve(filename)
}
catch (error) {
reject(error)
}
})
}

export function codegenImportReplace(code: string, targetLibFilepath: string) {
// match import {} from '@hyperoracle/zkgraph-lib'
// or import xxx from '@hyperoracle/zkgraph-lib' RegExp
Expand Down
15 changes: 0 additions & 15 deletions packages/zkgraph-cli/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,6 @@ export function toHexString(uint8array: Uint8Array) {
return Buffer.from(uint8array).toString('hex')
}

/**
* Generate a random key
* @param length
* @returns
*/
export const randomUniqueKey = (length = 6) => {
const chars = 'abcdefghijklmnopqrstuvwxyz1234567890'
const maxPos = chars.length
let key = ''
for (let i = 0; i < length; i++)
key += chars.charAt(Math.floor(Math.random() * maxPos))

return key
}

export function convertToMd5(value: Uint8Array): string {
const md5 = new Md5()
md5.appendByteArray(value)
Expand Down

0 comments on commit 23fd80e

Please sign in to comment.