-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
174 lines (146 loc) · 4.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const express = require("express");
const hash = require("object-hash");
const fs = require("fs");
const { spawn } = require("child_process");
const cors = require("cors");
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.get("/", (req, res) => res.send("Im a teapot"));
app.get("/slow", (req, res) => {
// run somethign really slow here and see what happens
var sum = 0;
for (var i = 0; i < 10 ** 10; i++) {
sum += i * (i - 1) * (i * 2);
}
res.send(`${sum}`);
});
var currentProcessesRunning = new Set();
var queue = [];
var outputData = {};
app.use(cors({ origin: "*" }));
const inQueue = (hash) => {
// check if hash is in queue
return queue.includes(hash);
};
const startNewProcess = (hash) => {
if (!!outputData[hash]) return;
const inputFileName = `inputs/${hash}.json`;
const witnessFileName = `inputs/${hash}.wtns`;
const publicName = `public_${hash}.json`;
const proofName = `proof_${hash}.json`;
// spawn a child process to run the proof generation
const prover = spawn("sh", ["exec.sh", hash], {
timeout: 60 * 60 * 1000,
});
if (!prover.pid) {
res.status(500);
return false;
}
currentProcessesRunning.add(hash);
prover.stdout.on("data", (data) => {
var res = data.toString();
if (res.substring(0,8) === "{\"pi_a\":") {
outputData[hash] = res;
// delete the relevant files in inputs folder
fs.unlinkSync(inputFileName);
fs.unlinkSync(witnessFileName);
fs.unlinkSync(publicName);
fs.unlinkSync(proofName);
} else {
outputData[hash] = '{\"result\": \"BLS signature verification failed.\"}';
}
currentProcessesRunning.delete(hash);
processQueue();
prover.kill();
});
prover.stderr.on("data", (data) => {
console.error(`stderr: ${data}`);
outputData[hash] = '{\"result\": \"BLS signature verification failed.\"}';
currentProcessesRunning.delete(hash);
processQueue();
prover.kill();
});
prover.on("close", (code) => {
currentProcessesRunning.delete(hash);
processQueue();
console.log(`child process exited with code ${code}`);
});
return true;
};
const processQueue = () => {
if (currentProcessesRunning.size >= 1) return null;
// remove duplicates from queue
queue = queue.filter(
(item, index, self) => index === self.findIndex((t) => t === item)
);
// get top element from queue
const hash = queue.shift();
if (!hash) return;
// check if already in process
if (currentProcessesRunning.has(hash)) return;
if (!!outputData[hash]) return;
const status = startNewProcess(hash);
return status;
};
app.post("/generate_proof", function (req, res) {
const input = req.body;
const inputHash = hash(input);
const inputFileName = `inputs/${inputHash}.json`;
console.log("input", input);
console.log("inputFileName", inputFileName);
if (
currentProcessesRunning.has(inputHash) ||
!!outputData[inputHash] ||
inQueue(inputHash)
) {
res.json({ id: inputHash });
return;
}
// otherwise add to queue
// write to file
// and return id
fs.writeFileSync(inputFileName, JSON.stringify(input));
queue.push(inputHash);
const status = processQueue();
res.json({ id: inputHash });
});
app.post("/result", (req, res) => {
processQueue();
// console.log("outputData", outputData);
const id = req.body["id"];
const result = outputData[id];
if (result) {
try {
console.log(result);
JSON.parse(result);
res.send(result);
} catch (e) {
console.log("error", e);
res.status(404).send("{\"result\": \"ERROR\"}");
}
} else if (currentProcessesRunning.has(id) || inQueue(id)) {
console.log("waiting for result");
console.log("currentProcessesRunning", currentProcessesRunning);
res.status(400).send("{\"result\": \"Process still running\"}");
} else {
console.log("ERROR! result not found", id, queue);
// print outputdata keys
console.log("outputData", outputData);
res.status(404).send("{\"result\": \"ERROR\"}");
}
});
app.post("/generate_proof_slow", async function (req, res) {
const input = req.body["id"];
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
input,
"./circuit.wasm",
"./circuit.zkey"
);
const genCalldata = await genSolidityCalldata(publicSignals, proof);
res.json(genCalldata);
});
const port = process.env.PORT || 3000;
app.listen(port, () =>
console.log(`Server running on ${port}, http://localhost:${port}`)
);