Skip to content

Commit c8f6b5f

Browse files
authored
Merge pull request #2 from XinFinOrg/generator-v2.0.0
Generator v2.0.0
2 parents 5e92754 + 8aa550f commit c8f6b5f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+4685
-615
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ name: Build and update image
33
on:
44
push:
55
branches:
6-
- main
6+
- master
77
- generator*
88
paths:
9-
- deployment-generator/**
9+
- container-manager/**
1010
tags:
1111
- '*'
1212
workflow_dispatch: #allow manual trigger to workflow
@@ -18,7 +18,7 @@ jobs:
1818
environment: DockerHub
1919
defaults:
2020
run:
21-
working-directory: subnet/deployment-generator
21+
working-directory: container-manager/
2222
steps:
2323
- name: Check out code
2424
uses: actions/checkout@v3
@@ -41,8 +41,8 @@ jobs:
4141
4242
- name: Build and push image
4343
run: |
44-
docker build . --file docker/Dockerfile \
45-
--build-arg SUBNET_BRANCH=${{ steps.commit.outputs.commit}} \
44+
docker build . --file Dockerfile \
45+
--build-arg SUBNET_BRANCH=${{ steps.commit.outputs.commit}} \
4646
--build-arg IMAGE_NAME=${{ steps.image.outputs.name }} \
4747
--tag ${{ steps.image.outputs.name }}
4848
docker push ${{ steps.image.outputs.name }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ keys.json
55
node_modules
66
package-lock.json
77
generated
8+
mount
89
*.env*
910
**temp
1011
*key*

container-manager/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

container-manager/Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Use the official Node.js 18.15 image as the base
2+
FROM node:18.15
3+
4+
# Install Docker CLI (required to interact with the Docker API)
5+
RUN apt-get update && apt-get install -y \
6+
curl \
7+
&& curl -fsSL https://get.docker.com | sh
8+
9+
# # Optionally, switch to a non-root user (for testing permissions)
10+
# RUN useradd -m myuser
11+
# USER myuser
12+
13+
COPY /src /app
14+
15+
# Set the working directory
16+
WORKDIR /app
17+
18+
RUN npm install
19+
20+
# CMD ["sleep", "infinity"]
21+
CMD ["npm", "run", "start"]

container-manager/src/express.js

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
process.chdir(__dirname);
2+
const fs = require("fs");
3+
const state = require("./libs/state");
4+
const exec = require("./libs/exec");
5+
const path = require("path");
6+
const ethers = require("ethers");
7+
const consolidate = require("consolidate");
8+
const express = require("express");
9+
const app = express();
10+
const PORT = 5210;
11+
let lastCalled = Date.now();
12+
13+
app.engine("pug", consolidate.pug);
14+
app.engine("html", consolidate.swig);
15+
app.set("view engine", "html"); // Set default to HTML
16+
app.set("json spaces", 2);
17+
app.use(express.static(path.join(__dirname, "public")));
18+
app.use(
19+
express.urlencoded({
20+
extended: true,
21+
})
22+
);
23+
app.use(express.json());
24+
25+
app.get("/", (req, res) => {
26+
res.sendFile(path.join(__dirname, "views", "index.html"));
27+
});
28+
29+
app.get("/debug", (req, res) => {
30+
res.sendFile(path.join(__dirname, "views", "debug.html"));
31+
});
32+
33+
app.get("/state", async (req, res) => {
34+
console.log("/state called");
35+
const thisCall = Date.now();
36+
console.log("time form last call: ", thisCall - lastCalled);
37+
lastCalled = thisCall;
38+
const response = await state.getState();
39+
res.setHeader("Content-Type", "application/json");
40+
res.send(JSON.stringify(response, null, 2));
41+
});
42+
43+
app.get("/deploy_csc_lite", async (req, res) => {
44+
console.log("/deploy_csc_lite called");
45+
await exec.deployCSC("lite", setupRes(req, res));
46+
});
47+
app.get("/deploy_csc_full", async (req, res) => {
48+
console.log("/deploy_csc_full called");
49+
await exec.deployCSC("full", setupRes(req, res));
50+
});
51+
app.get("/deploy_csc_reversefull", async (req, res) => {
52+
console.log("/deploy_csc_reversefull called");
53+
await exec.deployCSC("reversefull", setupRes(req, res));
54+
});
55+
56+
app.get("/deploy_zero", async (req, res) => {
57+
console.log("/deploy_zero called");
58+
await exec.deployZero(setupRes(req, res));
59+
});
60+
61+
app.get("/deploy_subswap", async (req, res) => {
62+
console.log("/deploy_subswap called");
63+
await exec.deploySubswap(setupRes(req, res));
64+
});
65+
66+
app.get("/start_subnet", async (req, res) => {
67+
console.log("/start_subnet called");
68+
await exec.startComposeProfile("machine1", setupRes(req, res));
69+
});
70+
71+
app.get("/stop_subnet", async (req, res) => {
72+
console.log("/stop_subnet called");
73+
const callbacks = setupRes(req, res);
74+
await exec.stopComposeProfile("machine1", callbacks);
75+
});
76+
77+
app.get("/start_subnet_slow", async (req, res) => {
78+
console.log("/start_subnet_slow called");
79+
await exec.startSubnet("machine1", setupRes(req, res));
80+
});
81+
82+
app.get("/start_services", async (req, res) => {
83+
console.log("/start_services called");
84+
await exec.startComposeProfile("services", setupRes(req, res));
85+
});
86+
87+
app.get("/stop_services", async (req, res) => {
88+
console.log("/stop_services called");
89+
await exec.stopComposeProfile("services", setupRes(req, res));
90+
});
91+
92+
app.get("/start_subswap_frontend", async (req, res) => {
93+
console.log("/start_subswap_frontend called");
94+
await exec.startComposeProfile("subswap", setupRes(req, res));
95+
});
96+
97+
app.get("/stop_subswap_frontend", async (req, res) => {
98+
console.log("/stop_subswap_frontend called");
99+
await exec.stopComposeProfile("subswap", setupRes(req, res));
100+
});
101+
102+
app.get("/start_explorer", async (req, res) => {
103+
console.log("/start_explorer called");
104+
await exec.startComposeProfile("explorer", setupRes(req, res));
105+
});
106+
107+
app.get("/stop_explorer", async (req, res) => {
108+
console.log("/stop_explorer called");
109+
await exec.stopComposeProfile("explorer", setupRes(req, res));
110+
});
111+
112+
app.get("/remove_subnet", async (req, res) => {
113+
console.log("/remove_subnet called");
114+
await exec.stopComposeProfile("machine1", setupRes(req, res));
115+
});
116+
117+
// generator methods (also pug instead of html)
118+
app.get("/gen", (req, res) => {
119+
res.render("generator/index.pug", {});
120+
});
121+
122+
app.post("/submit", (req, res) => {
123+
console.log("/submit called");
124+
const [valid, genOut] = exec.generate(req.body);
125+
126+
if (!valid) {
127+
res.render("generator/submit.pug", {
128+
message: "failed, please try again",
129+
error: genOut,
130+
});
131+
} else {
132+
res.render("generator/submit.pug", {
133+
message:
134+
"Config generation success, please continue in the Deployment Wizard tab",
135+
});
136+
}
137+
});
138+
139+
app.post("/submit_preconfig", (req, res) => {
140+
console.log("/submit called");
141+
const [valid, genOut] = exec.generate(req.body);
142+
143+
if (!valid) {
144+
res.send("failed to generate");
145+
} else {
146+
res.send("success");
147+
}
148+
});
149+
150+
app.get("/address", (req, res) => {
151+
const randomWallet = ethers.Wallet.createRandom();
152+
res.json({
153+
publicKey: randomWallet.address,
154+
privateKey: randomWallet.privateKey,
155+
});
156+
});
157+
158+
// add method to create env with pk, then faucet wallet will create from that file
159+
app.get("/faucet", (req, res) => {
160+
res.render("faucet/index.pug", {});
161+
});
162+
163+
app.get("/faucet_subnet", async function (req, res) {
164+
console.log("/faucet_subnet called");
165+
console.log(req.query);
166+
try {
167+
const { subnetUrl, gmKey } = state.getFaucetParams();
168+
const provider = new ethers.JsonRpcProvider(subnetUrl);
169+
const fromWallet = new ethers.Wallet(gmKey, provider);
170+
const fromPK = "123";
171+
toAddress = req.query.dest;
172+
amount = req.query.amount;
173+
if (!ethers.isAddress(toAddress))
174+
throw Error("Invalid destination address");
175+
if (isNaN(Number(amount)) || parseFloat(amount) <= 0 || amount == "")
176+
throw Error("Invalid Amount");
177+
if (parseFloat(amount) > 1_000_000_000)
178+
throw Error("Faucet request over 1,000,000,000 is not allowed");
179+
let inputs = ["", "", fromPK, toAddress, amount];
180+
const { fromBalance, toBalance, txHash } = await exec.processTransfer(
181+
provider,
182+
fromWallet,
183+
toAddress,
184+
amount
185+
);
186+
res.json({
187+
success: true,
188+
sourceAddress: fromWallet.address,
189+
destAddress: toAddress,
190+
sourceBalance: fromBalance,
191+
destBalance: toBalance,
192+
txHash: txHash,
193+
});
194+
} catch (error) {
195+
console.log(error);
196+
console.log(error.message);
197+
res.json({
198+
success: false,
199+
message: error.message,
200+
});
201+
}
202+
});
203+
204+
app.listen(PORT, () => {
205+
console.log(`Server is running on http://localhost:${PORT}`);
206+
});
207+
208+
function sleepSync(ms) {
209+
const start = Date.now();
210+
while (Date.now() - start < ms) {
211+
// Busy-wait loop (blocks the event loop)
212+
}
213+
}
214+
215+
function setupRes(req, res) {
216+
res.setHeader("Content-Type", "text/event-stream");
217+
res.setHeader("Cache-Control", "no-cache");
218+
res.setHeader("Connection", "keep-alive");
219+
const dataCallback = (data) => {
220+
lines = data.split("\n");
221+
for (let l = 0; l < lines.length; l++) {
222+
res.write(`data:${lines[l]}\n\n`);
223+
}
224+
};
225+
const doneCallback = () => {
226+
res.write("event: close\ndata: Done\n\n");
227+
res.end();
228+
};
229+
req.on("close", () => {
230+
res.end();
231+
});
232+
return {
233+
dataCallback: dataCallback,
234+
doneCallback: doneCallback,
235+
};
236+
}

0 commit comments

Comments
 (0)