Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sketch of submit mode auto #37

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ts/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export const get_contract_addr = () => {
}
}

export const get_chain_id = () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using env?

return 16;
}

export const get_user_addr = () => {
if (process.env.USER_ADDRESS) {
return process.env.USER_ADDRESS;
Expand Down
29 changes: 9 additions & 20 deletions ts/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,18 @@ export class U8ArrayUtil {
}
}

export class NumberUtil {
bn: BN;
constructor(num: number) {
this.bn = new BN(num);
}
toBN(){
let bns = new Array<BN>();
let bnStr = this.bn.toString("hex", 64)
for (let i = 0; i < bnStr.length; i += 16) {
const chunk = bnStr.slice(i, i + 16);
let a = new BN(chunk, 'hex', 'be');
bns.push(a);
}
return bns;
}
toU64StringArray() {
return this.toBN().map((x) => x.toString(10));
}
}

export function merkleRootToBeHexString(root: BigUint64Array) {
let bigint = root[3] + (root[2]<<64n) + (root[1]<<128n) + (root[0]<<192n);
let bnStr = bigint.toString(10);
let bn = new BN(bnStr, 10);
return '0x' + bn.toString("hex", 64);
}

export function hexStringToMerkleRoot(hexStr: string) {
let merkleRootBN = BigInt(hexStr);
let root = [0n, 0n, 0n, 0n];
for (let i=0; i<4; i++) {
root[i] = (merkleRootBN >> BigInt(64 * (3-i))) % (1n << 64n);
}
return root;
}
2 changes: 1 addition & 1 deletion ts/src/reproduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import BN from "bn.js";
import { ServiceHelper, get_contract_addr, modelBundle, get_user_private_account } from "./config.js";
import abiData from './Proxy.json' assert { type: 'json' };
import {ZkWasmUtil, PaginationResult, QueryParams, Task, VerifyProofParams} from "zkwasm-service-helper";
import { U8ArrayUtil, NumberUtil } from './lib.js';
import { U8ArrayUtil } from './lib.js';

import * as fs from 'fs';

Expand Down
22 changes: 19 additions & 3 deletions ts/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getMerkleArray } from "./settle.js";
import { ZkWasmUtil } from "zkwasm-service-helper";
import dotenv from 'dotenv';
import mongoose from 'mongoose';
import {merkleRootToBeHexString} from "./lib.js";
import {hexStringToMerkleRoot, merkleRootToBeHexString} from "./lib.js";
import {sha256} from "ethers";

// Load environment variables from .env file
Expand Down Expand Up @@ -378,9 +378,7 @@ async function main() {
});

app.post('/query', async (req, res) => {
console.log("receive query command");
const value = req.body;
console.log("value is", value);
if (!value) {
return res.status(400).send('Value is required');
}
Expand Down Expand Up @@ -417,6 +415,24 @@ async function main() {
}
});

app.get('/prooftask/:root', async (req, res) => {
try {
let merkleRootString = req.params.root;
let merkleRoot = new BigUint64Array(hexStringToMerkleRoot(merkleRootString));
let record = await modelBundle.findOne({ merkleRoot: merkleRoot});
if (record) {
return res.status(201).json(record);
} else {
throw Error("TaskNotFound");
}
} catch (err) {
// job not tracked
console.log(err);
res.status(500).json({ message: (err as Error).toString()});
}
});


app.post('/config', async (req, res) => {
try {
let jstr = application.get_config();
Expand Down
Loading