-
Notifications
You must be signed in to change notification settings - Fork 0
/
archwayd.js
346 lines (304 loc) · 12.1 KB
/
archwayd.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
const child_process = require('node:child_process');
const fs = require('node:fs');
const fs_async = require('node:fs/promises');
const path = require('node:path');
const process = require('node:process');
const date_fns = require('date-fns');
const mkdirp = require('mkdirp');
class Archwayd {
constructor(options) {
this.logger = options.logger;
this.project_id = options.project_id;
this.project_dir = options.project_dir;
this.chain_id = options.chain_id;
this.accounts = [];
this.is_persistent = options.is_persistent;
this.reset_state = options.reset_state;
this.Docker = options.docker;
this.docker = this.Docker.docker;
this.image = options.docker.image;
this.volume = options.docker.volume;
this.docker_opts = {
NetworkDisabled: true,
HostConfig: {
AutoRemove: true,
Mounts: [{
source: this.volume,
target: '/root/.archway',
type: 'volume'
}]
}
};
this.bin_dir = path.resolve(__dirname, 'bin');
this.is_apple_silicon = process.platform === 'darwin' && process.arch === 'arm64';
this.pid = null;
if (this.is_persistent) {
this.docker_opts.HostConfig.Mounts = [{
source: this.project_dir,
target: '/root/.archway',
type: 'bind'
}];
}
}
async load_config() {
if (!this.is_persistent) return;
try {
const accounts = JSON.parse(await fs_async.readFile(path.resolve(this.project_dir, 'spinarch_accounts.json')));
if (accounts.length > 0) this.accounts = accounts;
} catch (err) {
this.logger.app('Creating new config...');
}
}
async init_genesis() {
try {
const genesis_path = path.resolve(this.project_dir, 'config', 'genesis.json');
await fs_async.access(genesis_path, fs.constants.F_OK);
} catch (err) {
this.logger.app(`Generating genesis file for ${this.chain_id}...`);
await this.docker.run(this.image, [
'init', this.project_id,
'--chain-id', this.chain_id
], undefined, this.docker_opts);
}
}
display_accounts(accounts) {
const logger = this.logger;
logger.account('Available Accounts');
logger.account('==================');
accounts.forEach(function(account, idx) {
const validator_label = idx === 0 ? ' (validator)' : '';
logger.account(`(${account.name}) ${account.address} ${validator_label}`);
});
logger.account('\nMnemonics');
logger.account('==================');
accounts.forEach(function(account) {
logger.account(`(${account.name}) ${account.mnemonic}`);
});
}
async generate_accounts(num_accounts, balance) {
if (this.accounts.length > 0) {
this.display_accounts(this.accounts);
return;
}
const logger = this.logger;
const docker = this.docker;
const image = this.image;
const accounts = this.accounts;
const docker_opts = this.docker_opts;
logger.app(`Generating ${num_accounts} accounts...`);
for (let i = 0; i < num_accounts; i++) {
await new Promise(function(resolve, reject) {
docker.run(image, [
'keys', 'add', `${i}`,
'--keyring-backend', 'test',
'--output', 'json'
], undefined, docker_opts, function(err) {
if (err) {
return reject(err);
}
return resolve();
})
.on('stream', function(stream) {
stream.on('data', function(data) {
const json = data.toString();
accounts.push(JSON.parse(json));
});
});
});
}
logger.app(`Adding ${num_accounts} accounts to the genesis file...`);
for (let i = 0; i < num_accounts; i++) {
await new Promise(function(resolve, reject) {
docker.run(image, [
'add-genesis-account', `${i}`, `${balance}stake`,
'--keyring-backend', 'test',
'--output', 'json'
], undefined, docker_opts, function(err) {
if (err) {
return reject(err);
}
return resolve();
})
.on('stream', function(stream) {
stream.on('data', function(data) {
logger.docker(data.toString());
});
});
});
}
logger.app(`Creating validator...`);
await docker.run(image, [
'gentx', '0', '100000000stake',
'--chain-id', this.chain_id,
'--keyring-backend', 'test',
'--output', 'json'
], undefined, {
...docker_opts,
NetworkDisabled: false
});
logger.app(`Collecting gentxs...`);
await docker.run(image, ['collect-gentxs'], undefined, docker_opts);
if (this.is_persistent) {
await fs_async.writeFile(path.resolve(this.project_dir, 'spinarch_accounts.json'), JSON.stringify(accounts));
}
this.display_accounts(accounts);
}
async start_node() {
const logger = this.logger;
const docker = this.docker;
const container_name = 'spinarch_archwayd';
if (this.is_persistent && this.reset_state) {
logger.app(`Resetting state to the genesis...`);
await docker.run(this.image, ['unsafe-reset-all'], undefined, this.docker_opts);
}
logger.app(`Starting node... (RPC on 127.0.0.1:26657)`);
if ((this.is_apple_silicon && process.env.USE_NATIVE) && this.is_persistent) {
const cp = child_process.spawn(`${this.bin_dir}/archwayd-darwin-arm64`, [
'start',
'--moniker', this.project_id,
'--minimum-gas-prices', '0stake',
'--rpc.laddr', 'tcp://127.0.0.1:26657',
'--home', this.project_dir
]);
this.pid = cp.pid;
cp.stdout.on('data', function(data) {
logger.docker(data.toString().trim());
});
cp.stderr.on('data', function(data) {
logger.docker(data.toString().trim());
});
cp.on('error', function(err) {
logger.app('Failed to spawn child process');
logger.docker(err);
});
cp.on('close', function(code) {
logger.app(`Child process exited with code ${code}`);
this.pid = null;
});
} else {
const docker_opts = {
...this.docker_opts,
name: container_name,
NetworkDisabled: false
};
docker_opts.HostConfig.PortBindings = {
'26657/tcp': [{
HostIp: '127.0.0.1',
HostPort: '26657'
}]
};
docker.run(this.image, [
'start',
'--moniker', this.project_id,
'--minimum-gas-prices', '0stake',
'--rpc.laddr', 'tcp://0.0.0.0:26657'
], undefined, docker_opts, function(err) {
if (err) throw err;
})
.on('stream', function(stream) {
stream.on('data', function(data) {
logger.docker(data.toString().trim());
});
});
}
if (this.is_persistent) {
logger.app('<Press Ctrl-S to take snapshot, Ctrl-C to stop node>');
} else {
logger.app('<Press Ctrl-C to stop node>');
}
}
async stop_node() {
if (this.pid) {
process.kill(this.pid, 'SIGINT');
return;
}
const container = this.docker.getContainer('spinarch_archwayd');
await container.stop();
if (!this.is_persistent) await this.Docker.remove_volume();
}
async snapshot() {
if (!this.is_persistent) return;
try {
this.logger.app('Taking snapshot...');
await this.stop_node();
const snapshot_path = path.resolve(this.project_dir, '..', '.snapshots');
const snapshot_name = `${this.project_id}_${date_fns.format(new Date(), 'yyyy-MM-dd_HHmmss')}`;
await mkdirp(snapshot_path);
const docker = this.docker;
const logger = this.logger;
const project_dir = this.project_dir;
await new Promise(function(resolve, reject) {
docker.run('alpine:latest', [
'tar', 'cvf', `/ss/${snapshot_name}.tar`, '/state'
], undefined, {
NetworkDisabled: true,
HostConfig: {
AutoRemove: true,
Mounts: [{
source: snapshot_path,
target: '/ss',
type: 'bind'
}, {
source: project_dir,
target: '/state',
type: 'bind'
}]
}
}, function(err) {
if (err) return reject(err);
resolve();
})
.on('stream', function(stream) {
stream.on('data', function(data) {
logger.docker(data.toString().trim());
});
});
});
this.logger.app(`\n> Snapshot saved to ${snapshot_path}/${snapshot_name}.tar\n`);
} catch (err) {
this.logger.app('Failed to take snapshot');
}
await this.start_node(); // Resume node
}
async restore(snapshot_name) {
if (!this.is_persistent) return;
try {
const snapshot_path = path.resolve(this.project_dir, '..', '.snapshots');
const docker = this.docker;
const logger = this.logger;
const project_dir = this.project_dir;
await new Promise(function(resolve, reject) {
docker.run('alpine:latest', [
'sh', '-c',
`rm -r /state/* && cd / && tar xvf /ss/${snapshot_name}`
], undefined, {
NetworkDisabled: true,
HostConfig: {
AutoRemove: true,
Mounts: [{
source: snapshot_path,
target: '/ss',
type: 'bind'
}, {
source: project_dir,
target: '/state',
type: 'bind'
}]
}
}, function(err) {
if (err) return reject(err);
resolve();
})
.on('stream', function(stream) {
stream.on('data', function(data) {
logger.docker(data.toString().trim());
});
});
});
this.logger.app(`\n> Snapshot restored to ${this.project_dir}\n`);
} catch (err) {
this.logger.app('Failed to restore snapshot');
}
}
}
module.exports = Archwayd;