Skip to content

Commit

Permalink
feat(compile): add dir option
Browse files Browse the repository at this point in the history
  • Loading branch information
murongg committed Feb 22, 2024
1 parent e308673 commit 7d6fe45
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 27 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ cle compile [root]

#### Options

| Options | Description |
| ----------------------- | ----------------------- |
| `--yaml-path <path>` | Path to yaml file |
| `--mapping-path <path>` | Path to mapping file |
| Options | Description |
| -------------------- | ------------------------------------- |
| `--yaml-path <path>` | Path to yaml file |
| `--dir <path>` | Path to directory containing cle.yaml |

### Execute

Expand Down Expand Up @@ -141,7 +141,7 @@ cle prove <blockId> <offchainData> <expectedStateStr> [root]
| Arguments | Description |
| ------------------ | ----------------------------------------------- |
| `<block id>` | Block number (or block hash) as runtime context |
| `<expected state>` | State output of the CLE execution |
| `<expected state>` | State output of the CLE execution |
| `<offchainData>` | offchain data |


Expand Down Expand Up @@ -194,10 +194,10 @@ cle publish <ipfs_hash> [bounty_reward_per_trigger]

#### Arguments

| Arguments | Description |
| ----------------------------- | ---------------------------------------------------------- |
| `<ipfs hash>` | IPFS hash of uploaded CLE |
| `[bounty reward per trigger]` | Bounty reward per trigger in ETH |
| Arguments | Description |
| ----------------------------- | -------------------------------- |
| `<ipfs hash>` | IPFS hash of uploaded CLE |
| `[bounty reward per trigger]` | Bounty reward per trigger in ETH |

### Deposit

Expand Down
18 changes: 9 additions & 9 deletions packages/cle-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ cle compile [root]

#### Options

| Options | Description |
| ----------------------- | ----------------------- |
| `--yaml-path <path>` | Path to yaml file |
| `--mapping-path <path>` | Path to mapping file |
| Options | Description |
| -------------------- | ------------------------------------- |
| `--yaml-path <path>` | Path to yaml file |
| `--dir <path>` | Path to directory containing cle.yaml |

### Execute

Expand Down Expand Up @@ -88,7 +88,7 @@ cle prove <blockId> <offchainData> <expectedStateStr> [root]
| Arguments | Description |
| ------------------ | ----------------------------------------------- |
| `<block id>` | Block number (or block hash) as runtime context |
| `<expected state>` | State output of the CLE execution |
| `<expected state>` | State output of the CLE execution |
| `<offchainData>` | offchain data |


Expand Down Expand Up @@ -141,10 +141,10 @@ cle publish <ipfs_hash> [bounty_reward_per_trigger]

#### Arguments

| Arguments | Description |
| ----------------------------- | ---------------------------------------------------------- |
| `<ipfs hash>` | IPFS hash of uploaded CLE |
| `[bounty reward per trigger]` | Bounty reward per trigger in ETH |
| Arguments | Description |
| ----------------------------- | -------------------------------- |
| `<ipfs hash>` | IPFS hash of uploaded CLE |
| `[bounty reward per trigger]` | Bounty reward per trigger in ETH |

### Deposit

Expand Down
4 changes: 3 additions & 1 deletion packages/cle-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export async function run() {
.command('compile', 'Compile for Full Image (Link Compiled with Compiler Server)')
// .option('--local', 'Compile for Local Image')
.option('--yaml-path <path>', 'Path to yaml file')
.option('--dir <path>', 'Path to directory containing cle.yaml')
.example('cle compile')
.action((options) => {
const { yamlPath = '' } = options
const { yamlPath = '', dir } = options
const wasmPath = config.WasmBinPath

compile({
Expand All @@ -36,6 +37,7 @@ export async function run() {
compilerServerEndpoint: config.CompilerServerEndpoint,
wasmPath,
watPath: wasmPath.replace(/\.wasm/, '.wat'),
dirPath: dir,
})
})

Expand Down
31 changes: 24 additions & 7 deletions packages/cle-cli/src/commands/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { createOnNonexist, isTsFile, loadMappingPathFromYaml } from '../utils'
import { logger } from '../logger'

export interface CompileOptions {
// local: boolean
yamlPath?: string
dirPath?: string
compilerServerEndpoint: string
wasmPath: string
watPath: string
Expand All @@ -29,11 +29,26 @@ export async function compile(options: CompileOptions) {

async function compileRun(options: CompileOptions) {
const {
yamlPath,
wasmPath,
watPath,
// local,
} = options

let {
yamlPath,
dirPath,
} = options

if (dirPath) {
if (!fs.existsSync(dirPath)) {
// if dirPath is relative
if (fs.existsSync(path.join(process.cwd(), dirPath)))
dirPath = path.join(process.cwd(), dirPath)
}
const yamlPathInDir = path.join(dirPath, 'cle.yaml')
if (fs.existsSync(yamlPathInDir))
yamlPath = yamlPathInDir
}

if (!yamlPath) {
logger.error('no yaml path provided')
return false
Expand All @@ -52,12 +67,14 @@ async function compileRun(options: CompileOptions) {

createOnNonexist(wasmPath)
createOnNonexist(watPath)
if (!dirPath)
dirPath = path.dirname(mappingPath)

const paths = getFileTreeByDir(path.dirname(mappingPath))
const relativePaths = getRelativePaths(path.dirname(mappingPath), paths)
const fileMap = getFileContentsByFilePaths(relativePaths, path.dirname(mappingPath))
const paths = getFileTreeByDir(dirPath)
const relativePaths = getRelativePaths(dirPath, paths)
const fileMap = getFileContentsByFilePaths(relativePaths, dirPath)

const relativeYamlPath = path.relative(path.dirname(mappingPath), yamlPath)
const relativeYamlPath = path.relative(dirPath, yamlPath)

const [err, res] = await to(zkgapi.compile({
...webjson,
Expand Down
25 changes: 24 additions & 1 deletion test/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,30 @@ describe('compile', () => {

await compile({
yamlPath,
// local: false,
compilerServerEndpoint: 'http://compiler.dev.hyperoracle.io/compile',
wasmPath,
watPath,
})
const hasWasm = fs.existsSync(wasmPath)
const hasWat = fs.existsSync(watPath)
expect(hasWasm).toBeTruthy()
expect(hasWat).toBeTruthy()

const wasm = fs.readFileSync(wasmPath)
const wasmUint8Array = new Uint8Array(wasm)

const inst = await instantiateWasm(wasmUint8Array)

expect(inst.asmain).not.toBeUndefined()
expect(inst.zkmain).not.toBeUndefined()
}, 200000)

it.runIf(process.platform !== 'win32')('dir', async () => {
const wasmPath = path.join(projectRoot, 'temp/cle.wasm')
const watPath = wasmPath.replace(/\.wasm/, '.wat')

await compile({
dirPath: commandsFixturesRoot,
compilerServerEndpoint: 'http://compiler.dev.hyperoracle.io/compile',
wasmPath,
watPath,
Expand Down

0 comments on commit 7d6fe45

Please sign in to comment.