Skip to content
This repository was archived by the owner on Mar 3, 2024. It is now read-only.

Commit 0dc84e7

Browse files
authoredJul 12, 2023
Merge pull request #42 from hyperoracle/dev
Fix merging result of dev and suning
2 parents d8e5f8f + fbe5457 commit 0dc84e7

File tree

11 files changed

+164
-678
lines changed

11 files changed

+164
-678
lines changed
 

‎.github/workflows/main.yml

+19
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ name: Prettier Action
44
# https://github.com/marketplace/actions/prettier-action
55
on:
66
pull_request:
7+
branches:
8+
- master
9+
- main
710
push:
811
branches:
912
- master
13+
- main
1014

1115
jobs:
1216
prettier:
17+
# https://github.com/ad-m/github-push-action/issues/96
18+
# Allow write permission for github-actions
19+
permissions:
20+
contents: write
1321
runs-on: ubuntu-latest
1422

1523
steps:
@@ -18,8 +26,19 @@ jobs:
1826
with:
1927
# Make sure the actual branch is checked out when running on pull requests
2028
ref: ${{ github.head_ref }}
29+
# https://stackoverflow.com/questions/72375995/how-to-get-commit-count-of-the-repository-in-github-actions
30+
# 0 indicates all history for all branches and tags.
31+
fetch-depth: 0
32+
33+
- name: Get commit count
34+
id: commit-count
35+
run: |
36+
echo "COMMIT_COUNT=$(git rev-list --count HEAD)" >> $GITHUB_ENV
37+
echo "Commit count: ${{ env.COMMIT_COUNT }}"
2138
2239
- name: Prettify code
40+
# Step only run if the repo is not created by "using this template" (commit count is 1)
41+
if: ${{ env.COMMIT_COUNT != '1' }}
2342
uses: creyD/prettier_action@v4.3
2443
with:
2544
# This part is also where you can pass other options, for example:

‎.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,6 @@ build
107107
*.wasm
108108
*.wat
109109

110-
.DS_Store
110+
.DS_Store
111+
112+
config.js

‎README.md

+73-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
1-
# zkGraph SDK and Library
1+
# zkGraph Template
22

3-
## Develop zkGraph
3+
## Getting Started
44

5-
### Getting Started
5+
To create your zkGraph project based on this template, click `Use this template`, and `Creating a new repository`.
66

7-
First, fork this repo, and clone your forked repo. Then, run:
7+
### Configuration
8+
9+
After clone your project, you need to create `config.js` file at root folder based on `config-example.js`
10+
11+
```js
12+
// ./config.js
13+
export const config = {
14+
// Etherum JSON RPC provider URL:
15+
// (Please note the provider must support debug_getRawReceipts RPC method.)
16+
JsonRpcProviderUrl: "https://{URL}",
17+
};
18+
```
19+
20+
Then run:
821

922
```bash
10-
git update-index --skip-worktree constants.js
1123
npm install
1224
```
1325

14-
To test the whole flow of the library locally, update `constants.js` file with your data, then run:
26+
### Quick Start
27+
28+
To test the whole flow of the library, run this after you have done the configuration:
1529

1630
```bash
1731
sh test.sh
1832
```
1933

20-
## Usage Example
34+
## Commands
35+
36+
The workflow of local zkGraph development is: `Develop` (code in /src) -> `Compile` (to get compiled wasm image) -> `Execute` (to get expected output) -> `Prove` (to generate input and pre-test for actual proving) -> `Deploy`.
37+
38+
If you encounter any problem, please refer to the [test.sh](./test.sh) for the example usage of the commands.
2139

2240
### Compile Locally
2341

@@ -38,6 +56,45 @@ npm run prove-local -- --inputgen {block_id} {expected_state}
3856
npm run prove-local -- --pretest {block_id} {expected_state}
3957
```
4058

59+
## Develop
60+
61+
### `config.js`
62+
63+
The configuration (such as blockchain json rpc provider url) for the local development API.
64+
65+
### `src/zkgraph.yaml`
66+
67+
The configuration for the zkGraph.
68+
69+
It specifies information including:
70+
71+
- data source
72+
- target blockchain network
73+
- target smart contract address
74+
- target event
75+
- event handler
76+
77+
### `src/mapping.ts`
78+
79+
The logic of the event handler in AssemblyScript.
80+
81+
It specifies how to handle the event data and generate the output state.
82+
83+
```typescript
84+
export function handleEvents(events: Event[]): Bytes {
85+
let state = new Bytes(0);
86+
if (events.length > 0) {
87+
state = events[0].address;
88+
}
89+
require(state.length == 20 ? 1 : 0);
90+
return state;
91+
}
92+
```
93+
94+
## Resources
95+
96+
More info and API reference can be found in [Hyper Oracle zkGraph docs](https://docs.hyperoracle.io/zkgraph-standards/zkgraph).
97+
4198
## zkGraph Dev Tips
4299

43100
### Development
@@ -62,6 +119,15 @@ npm run prove-local -- --pretest {block_id} {expected_state}
62119

63120
References: [WebAssembly Opcodes](https://pengowray.github.io/wasm-ops/).
64121

122+
## Structure
123+
124+
This repo has the following folders relevant to zkGraph development:
125+
126+
- `bundle-js`: APIs (the scripts in `package.json`) for compile, execute, prove, and deploy zkGraph for testing locally.
127+
- `example`: Example zkGraphs.
128+
- `lib`: AssemblyScript library for zkGraph development, with data structure such as Bytes, ByteArray and BigInt.
129+
- `src`: Where your actual zkGraph should be in. Contains `mapping.ts` and `zkgraph.yaml`.
130+
65131
## Thanks
66132

67133
- zkWasm Project: [DelphinusLab/zkWasm](https://github.com/DelphinusLab/zkWasm)

‎bundle-js/api-local/compile.js

+36-16
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,46 @@ import { logDivider } from "../common/utils.js";
55
// Log script name
66
console.log(">> COMPILE", "\n");
77

8+
let isCompilationSuccess = true;
9+
810
const commands = [
911
"npx asc lib/main_local.ts -t build/zkgraph_local.wat -O --noAssert -o build/zkgraph_local.wasm --disable bulk-memory --use abort=lib/common/type/abort --target release --bindings esm --runtime stub",
1012
];
1113

1214
const combinedCommand = commands.join(" && ");
13-
execSync(combinedCommand, { encoding: "utf-8" });
15+
try {
16+
execSync(combinedCommand, { encoding: "utf-8" });
17+
} catch (error) {
18+
// Handle or log the error here if required
19+
isCompilationSuccess = false;
20+
}
21+
22+
if (isCompilationSuccess) {
23+
// Log compiled file size by line count
24+
const compiledFileContent = readFileSync("build/zkgraph_local.wat", "utf-8");
25+
const compiledFileLineCount = compiledFileContent.split("\n").length;
26+
console.log(
27+
"[*]",
28+
compiledFileLineCount,
29+
compiledFileLineCount > 1
30+
? "lines in build/zkgraph_local.wat"
31+
: "line in build/zkgraph_local.wat",
32+
);
33+
34+
// Log status
35+
console.log("[+] COMPILATION SUCCESS!", "\n");
36+
37+
logDivider();
38+
39+
process.exit(0);
40+
} else {
41+
// Extra new line for error
42+
console.log();
43+
44+
// Log status
45+
console.log("[-] ERROR WITH COMPILING.", "\n");
46+
47+
logDivider();
1448

15-
function getLineCount(filePath) {
16-
const content = readFileSync(filePath, "utf-8");
17-
const lines = content.split("\n");
18-
return lines.length;
49+
process.exit(1);
1950
}
20-
const compiledFileLineCount = getLineCount("build/zkgraph_local.wat");
21-
console.log(
22-
"[*]",
23-
compiledFileLineCount,
24-
compiledFileLineCount > 1
25-
? "lines in build/zkgraph_local.wat"
26-
: "lines in build/zkgraph_local.wat",
27-
);
28-
console.log("[+] COMPILATION COMPLETE!", "\n");
29-
30-
logDivider();

‎bundle-js/api-local/exec.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "../common/api_helper.js";
99
import { loadConfig } from "../common/config.js";
1010
import { program } from "commander";
11-
import { constants } from "../../constants.js";
11+
import { config } from "../../config.js";
1212
// usage: node exec.js -b <blocknum/blockhash>
1313
// TODO: update handler func name by yaml config
1414

@@ -31,7 +31,7 @@ const [source_address, source_esigs] = loadConfig("src/zkgraph.yaml");
3131
console.log("[*] Source contract address:", source_address);
3232
console.log("[*] Source events signatures:", source_esigs, "\n");
3333

34-
const provider = new providers.JsonRpcProvider(constants.JsonRpcProviderUrl);
34+
const provider = new providers.JsonRpcProvider(config.JsonRpcProviderUrl);
3535

3636
// Fetch raw receipts
3737
const rawreceiptList = await getRawReceipts(provider, blockid);
@@ -79,3 +79,5 @@ const state = asmain(rawReceipts, matchedEventOffsets);
7979
console.log("[+] ZKGRAPH STATE OUTPUT:", toHexString(state), "\n");
8080

8181
logDivider();
82+
83+
process.exit(0);

‎bundle-js/api-local/prove.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from "../common/utils.js";
1818
import { zkmain, setupZKWasmMock } from "../common/bundle_local.js";
1919
import { ZKWASMMock } from "../common/zkwasm_mock.js";
20-
import { constants } from "../../constants.js";
20+
import { config } from "../../config.js";
2121

2222
program.version("1.0.0");
2323

@@ -32,6 +32,21 @@ program.parse(process.argv);
3232
const args = program.args;
3333
const options = program.opts();
3434

35+
// Log mode name first
36+
switch (options.inputgen || options.pretest) {
37+
// Input generation mode
38+
case options.inputgen:
39+
// Log script name
40+
console.log(">> PROVE: INPUT GENERATION MODE", "\n");
41+
break;
42+
43+
// Pretest mode
44+
case options.pretest:
45+
// Log script name
46+
console.log(">> PROVE: PRETEST MODE", "\n");
47+
break;
48+
}
49+
3550
// Read block id
3651
const blockid = args[0].length >= 64 ? args[0] : parseInt(args[0]); //17633573
3752
let expectedStateStr = args[1];
@@ -40,7 +55,7 @@ expectedStateStr = trimPrefix(expectedStateStr, "0x");
4055
// Load config
4156
const [source_address, source_esigs] = loadConfig("src/zkgraph.yaml");
4257

43-
const provider = new providers.JsonRpcProvider(constants.JsonRpcProviderUrl);
58+
const provider = new providers.JsonRpcProvider(config.JsonRpcProviderUrl);
4459

4560
// Fetch raw receipts
4661
let rawreceiptList = await getRawReceipts(provider, blockid);
@@ -89,20 +104,17 @@ const privateInputStr = formatVarLenInput([
89104

90105
const publicInputStr = formatVarLenInput([expectedStateStr]);
91106

107+
// Log content based on mode
92108
switch (options.inputgen || options.pretest) {
93109
// Input generation mode
94110
case options.inputgen:
95-
// Log script name
96-
console.log(">> PROVE: INPUT GENERATION MODE", "\n");
97111
console.log("[+] ZKGRAPH STATE OUTPUT:", expectedStateStr, "\n");
98112
console.log("[+] PRIVATE INPUT FOR ZKWASM:", "\n" + privateInputStr, "\n");
99113
console.log("[+] PUBLIC INPUT FOR ZKWASM:", "\n" + publicInputStr, "\n");
100114
break;
101115

102116
// Pretest mode
103117
case options.pretest:
104-
// Log script name
105-
console.log(">> PROVE: PRETEST MODE", "\n");
106118
const mock = new ZKWASMMock();
107119
mock.set_private_input(privateInputStr);
108120
mock.set_public_input(publicInputStr);

‎bundle-js/common/api_helper.js

-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ export function rlpDecodeAndEventFilter(rawreceiptList, srcAddr, srcEsigs) {
4040
}
4141

4242
export function genStreamAndMatchedEventOffsets(rawreceiptList, eventList) {
43-
console.log("test ", rawreceiptList);
44-
console.log("test ", eventList);
4543
let matched_offset_list = [];
4644
let accumulateReceiptLength = 0;
4745
let rawreceipts = "";

‎config-example.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const config = {
2+
// Update your Etherum JSON RPC provider URL here.
3+
// Please note that the provider must support debug_getRawReceipts RPC method.
4+
JsonRpcProviderUrl: "https://{URL}",
5+
};

‎constants.js

-4
This file was deleted.

0 commit comments

Comments
 (0)