This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 816
Expand file tree
/
Copy pathchain.js
More file actions
206 lines (169 loc) · 5.4 KB
/
Copy pathchain.js
File metadata and controls
206 lines (169 loc) · 5.4 KB
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
#!/usr/bin/env node
//var ganacheLib = require("../../../../ganache-core"); // for easy testing
const ganacheLib = require("ganache-core");
const logging = require("./logging");
if (!process.send) {
console.log("Not running as child process. Throwing.");
throw new Error("Must be run as a child process!");
}
// remove the uncaughtException listener added by ganache-cli
process.removeAllListeners("uncaughtException");
process.on("unhandledRejection", err => {
//console.log('unhandled rejection:', err.stack || err)
process.send({ type: "error", data: copyErrorFields(err) });
});
process.on("uncaughtException", err => {
//console.log('uncaught exception:', err.stack || err)
process.send({ type: "error", data: copyErrorFields(err) });
});
let server;
let blockInterval;
let dbLocation;
async function stopServer() {
clearInterval(blockInterval);
if (server) {
return new Promise((resolve, reject) => {
server.close((err) => {
if (err) {
if (err.code === "ERR_SERVER_NOT_RUNNING"){
process.send({ type: "server-stopped" });
resolve();
} else {
reject(err);
}
}
else resolve();
});
})
} else {
process.send({ type: "server-stopped" });
}
}
async function startServer(options) {
await stopServer();
if (options.connectToServer) {
process.send({ type: "server-started", data: {
} });
return
}
let sanitizedOptions = Object.assign({}, options);
delete sanitizedOptions.mnemonic;
const logToFile =
options.logDirectory !== null && typeof options.logDirectory === "string";
if (typeof options.logger === "undefined") {
if (logToFile) {
logging.generateLogFilePath(options.logDirectory);
options.logger = {
log: message => {
if (typeof message === "string") {
logging.logToFile(message);
}
},
};
} else {
// The TestRPC's logging system is archaic. We'd like more control
// over what's logged. For now, the really important stuff all has
// a space on the front of it. So let's only log the stuff with a
// space on the front. ¯\_(ツ)_/¯
options.logger = {
log: message => {
if (
typeof message === "string" &&
(options.verbose || message.indexOf(" ") == 0)
) {
console.log(message);
}
},
};
}
}
// log startup options without logging user's mnemonic
const startingMessage = `Starting server with initial configuration: ${JSON.stringify(sanitizedOptions)}`;
console.log(startingMessage);
if (logToFile) {
logging.logToFile(startingMessage);
}
server = ganacheLib.server(options);
// We'll also log all methods that aren't marked internal by Ganache
var oldSend = server.provider.send.bind(server.provider);
server.provider.send = (payload, callback) => {
if (payload.internal !== true) {
if (Array.isArray(payload)) {
payload.forEach(function(item) {
console.log(item.method);
});
} else {
console.log(payload.method);
}
}
oldSend(payload, callback);
};
server.listen(options.port, options.hostname, (err, result) => {
if (err) {
process.send({ type: "start-error", data: {code: err.code, stack: err.stack, message: err.message} });
return;
}
const state = result ? result : server.provider.manager.state;
dbLocation = state.blockchain.data.directory;
if (!state) {
process.send({
type: "start-error",
data: "Couldn't get a reference to TestRPC's StateManager.",
});
return;
}
const privateKeys = {};
const accounts = state.accounts;
const addresses = Object.keys(accounts);
addresses.forEach((address) => {
privateKeys[address] = accounts[address].secretKey.toString("hex");
});
const data = Object.assign({}, server.provider.options);
// delete anything which might've been in the ganache-core options object
// that we don't want to pass on to the main process
delete data.logger;
delete data.vm;
delete data.state;
delete data.trie;
// ensure certain fields are present for backward compatibility with old
// versions of ganache-core
data.hdPath = data.hdPath || state.wallet_hdpath;
data.mnemonic = data.mnemonic || state.mnemonic;
data.privateKeys = privateKeys;
process.send({ type: "server-started", data: data });
console.log("Ganache started successfully!");
console.log("Waiting for requests...");
});
server.on("close", () => {
server = null;
process.send({ type: "server-stopped" });
});
}
function getDbLocation() {
process.send({ type: "db-location", data: dbLocation || null });
}
process.on("message", (message) => {
//console.log("CHILD RECEIVED", message)
switch (message.type) {
case "start-server":
startServer(message.data);
break;
case "stop-server":
stopServer();
break;
case "get-db-location":
getDbLocation();
break;
}
});
function copyErrorFields(e) {
let err = Object.assign({}, e);
// I think these properties aren't enumerable on Error objects, so we copy
// them manually if we don't do this, they aren't passed via IPC back to the
// main process
err.message = e.message;
err.stack = e.stack;
err.name = e.name;
return err;
}
process.send({ type: "process-started" });