This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
dapp.js
90 lines (78 loc) · 2.99 KB
/
dapp.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
// Copyright 2022 Cartesi Pte. Ltd.
//
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
const { ethers } = require("ethers");
const rollup_server = process.env.ROLLUP_HTTP_SERVER_URL;
console.log("HTTP rollup_server url is " + rollup_server);
async function handle_advance(data) {
console.log("Received advance request data " + JSON.stringify(data));
const payload = data["payload"];
try {
const payloadStr = ethers.utils.toUtf8String(payload);
console.log(`Adding notice "${payloadStr}"`);
} catch (e) {
console.log(`Adding notice with binary value "${payload}"`);
}
const advance_req = await fetch(rollup_server + '/notice', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ payload })
});
const json = await advance_req.json();
console.log("Received notice status " + advance_req.status + " with body " + JSON.stringify(json));
return "accept";
}
async function handle_inspect(data) {
console.log("Received inspect request data " + JSON.stringify(data));
const payload = data["payload"];
try {
const payloadStr = ethers.utils.toUtf8String(payload);
console.log(`Adding report "${payloadStr}"`);
} catch (e) {
console.log(`Adding report with binary value "${payload}"`);
}
const inspect_req = await fetch(rollup_server + '/report', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ payload })
});
console.log("Received report status " + inspect_req.status);
return "accept";
}
var handlers = {
advance_state: handle_advance,
inspect_state: handle_inspect,
}
var finish = { status: "accept" };
(async () => {
while (true) {
console.log("Sending finish")
const finish_req = await fetch(rollup_server + '/finish', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ status: 'accept' })
});
console.log("Received finish status " + finish_req.status);
if (finish_req.status == 202) {
console.log("No pending rollup request, trying again");
} else {
const rollup_req = await finish_req.json();
var handler = handlers[rollup_req["request_type"]];
finish["status"] = await handler(rollup_req["data"]);
}
}
})();