-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
273 lines (246 loc) · 7.8 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env node
const fs_async = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');
const process = require('node:process');
const readline = require('node:readline');
const util = require('node:util');
const blessed = require('neo-blessed');
const {
program,
Option
} = require('commander');
const docker_names = require('docker-names');
const mkdirp = require('mkdirp');
const package = require('./package.json');
program
.name(package.name)
.description(package.description)
.version(package.version);
program
.addOption(new Option('--project-id <string>', 'Your project ID'))
.addOption(new Option('--chain-id <string>', 'Chain ID').default('spinarch-1'))
.addOption(new Option('--num-accounts <number>', 'Number of accounts to generate').default(10))
.addOption(new Option('--balance <number>', 'Default balance of each generated account').default(1000000000))
.addOption(new Option('--update-image', 'Update the Archway image to latest version'))
.addOption(new Option('--reset-state', 'Reset the blockchain to the genesis state'));
program.parse();
(async function() {
const options = program.opts();
const project_id = options.projectId ? options.projectId.replace(/[^0-9a-zA-Z-_]/g, '') : docker_names.getRandomName();
const project_dir = path.resolve(os.homedir(), '.spinarch', project_id);
const snapshot_dir = path.resolve(project_dir, '..', '.snapshots');
const is_persistent = !!options.projectId;
if (options.numAccounts < 1) {
console.error('Number of accounts to generate must be greater than 0');
process.exit(1);
}
//- Start: Choose snapshot
await mkdirp(snapshot_dir);
const snapshots = (await fs_async.readdir(snapshot_dir))
.filter(function(snapshot) {
return snapshot.startsWith(project_id + '_') && snapshot.endsWith('.tar');
})
.sort()
.reverse()
.slice(0, 10); // Return max 10 snapshots
let selected_snapshot = 'CURRENT'; // CURRENT is current state
if (snapshots.length > 0) {
const rl = readline.createInterface({
input: process.stdin,
output: null
});
console.log('Choose snapshot to restore from:');
console.log('[0] Last saved state');
snapshots.forEach(function(snapshot, idx) {
console.log(`[${idx+1}] ${snapshot}`.replace('.tar', ''));
});
try {
process.stdout.write(`\nChoose snapshot [0-${snapshots.length}] (default: 0): `);
const question = util.promisify(rl.question).bind(rl);
const answer = parseInt(await question(''));
if (!answer || (answer > 0 && answer <= snapshots.length)) {
if (!answer) {
selected_snapshot = 'CURRENT';
} else {
selected_snapshot = snapshots[answer - 1];
}
rl.close();
} else {
console.log('Invalid choice');
process.exit(1);
}
} catch (err) {
console.warn('Bye');
process.exit(1);
}
}
//- End: Choose snapshot
//- Start: Blessed TUI
const screen = blessed.screen({
smartCSR: true,
dockBorders: true,
fullUnicode: true,
title: `🥬 ${package.name} ${package.version} - ${project_id}`
});
// Application log
const box_top_left = blessed.log({
parent: screen,
top: 0,
left: 0,
width: '50%',
height: '50%',
border: {
type: 'line'
},
mouse: true,
scrollback: 100,
scrollbar: {
ch: ' ',
track: {
bg: 'green'
},
style: {
inverse: true
}
}
});
// Docker log
const box_top_right = blessed.log({
parent: screen,
top: 0,
left: '50%',
width: '50%',
height: '50%',
border: {
type: 'line'
},
mouse: true,
scrollback: 100,
scrollbar: {
ch: ' ',
track: {
bg: 'red'
},
style: {
inverse: true
}
}
});
// Accounts log
const box_bottom = blessed.log({
parent: screen,
top: '50%',
left: 0,
width: '100%',
height: '50%',
border: {
type: 'line'
},
mouse: true,
scrollback: 100,
scrollbar: {
ch: ' ',
track: {
bg: 'yellow'
},
style: {
inverse: true
}
}
});
// Log helper
const logger = {
app: function(string) {
box_top_left.log(string);
screen.render();
},
docker: function(string) {
box_top_right.log(string);
screen.render();
},
account: function(string) {
box_bottom.log(string);
screen.render();
}
};
box_bottom.focus();
//- End: Blessed TUI
const Docker = require('./docker');
const docker = new Docker({
logger: logger
});
const Archwayd = require('./archwayd');
const archwayd = new Archwayd({
logger: logger,
docker: docker,
project_id: project_id,
project_dir: project_dir,
chain_id: options.chainId,
is_persistent: is_persistent,
reset_state: options.resetState
});
// Hotkey - text selection
let is_selection_enabled = false;
screen.key(['C-t'], function() {
is_selection_enabled = !is_selection_enabled;
if (is_selection_enabled) {
screen.program.disableMouse();
logger.app('> Text selection is now enabled');
} else {
screen.program.enableMouse();
logger.app('> Text selection is now disabled');
}
});
// Hotkey - snapshot
let is_stopping = false;
let is_snapshotting = false;
screen.key(['C-s'], async function() {
if (is_snapshotting || is_stopping) return;
is_snapshotting = true;
await archwayd.snapshot();
is_snapshotting = false;
});
// Hotkey - terminate
screen.key(['C-c'], function() {
if (is_snapshotting || is_stopping) return;
is_stopping = true;
process.kill(process.pid, 'SIGINT');
});
// Handle termination
process.on('SIGINT', async function() {
try {
logger.app('Stopping...');
await archwayd.stop_node();
} catch (err) {
// Ignore
}
screen.destroy();
process.exit(0);
});
//- Start: Application flow
logger.app('<Press Ctrl-T to toggle text selection>');
logger.docker(await fs_async.readFile(path.resolve(__dirname, 'spinach.txt'), 'utf8')); // 🥬
if (!is_persistent) {
logger.app(`Starting with temporary state... (set --project-id to enable persistent state)`);
await docker.remove_volume();
} else {
logger.app(`Starting with persistent state... (${project_dir})`);
await mkdirp(project_dir);
if (selected_snapshot !== 'CURRENT') {
logger.app(`Restoring snapshot... (${selected_snapshot})`);
await archwayd.restore(selected_snapshot);
}
}
if (!(await docker.image_exists()) || options.updateImage) {
await docker.pull_image();
}
if (!(await docker.image_exists('alpine:latest'))) {
await docker.pull_image('alpine:latest');
}
await archwayd.load_config();
await archwayd.init_genesis();
await archwayd.generate_accounts(options.numAccounts, options.balance);
await archwayd.start_node();
//- End: Application flow
})();