@@ -30,6 +30,7 @@ const pwPath = path.join(gethDataDir, 'wallets.pw')
30
30
const gethProcessPath = path . join ( getExtensionsPath ( 'bin' ) , gethProcessKey )
31
31
32
32
const configurePeers = async ( dataDir ) => {
33
+ const staticNodePath = path . join ( dataDir , 'geth' , 'static-nodes.json' )
33
34
try {
34
35
const discoveryDomain = `_enode._tcp.${ envNet } .${ envSubDomain } .brave.com`
35
36
let newNodes = await dns . resolveSrv ( discoveryDomain )
@@ -47,9 +48,10 @@ const configurePeers = async (dataDir) => {
47
48
48
49
const enodes = newNodes . map ( ( { name, port} , i ) => `enode://${ newNodesPublicKeys [ i ] } @${ newNodesIps [ i ] } :${ port } ` )
49
50
50
- await fs . writeFile ( path . join ( dataDir , 'geth' , 'static-nodes.json' ) , JSON . stringify ( enodes ) )
51
- } catch ( e ) {
52
- console . error ( 'Failed to configure static nodes peers ' + e . message )
51
+ await fs . writeFile ( staticNodePath , JSON . stringify ( enodes ) )
52
+ } catch ( ex ) {
53
+ console . error ( 'unable to configure ' + staticNodePath + ': ' + ex . message )
54
+ throw ex
53
55
}
54
56
}
55
57
@@ -106,29 +108,34 @@ const spawnGeth = async () => {
106
108
}
107
109
108
110
const gethOptions = {
109
- stdio : process . env . GETH_LOG ? 'inherit' : 'ignore'
111
+ stdio : ( ( envNet === 'ropsten' ) || process . env . GETH_LOG ) ? 'inherit' : 'ignore'
110
112
}
111
113
112
- ensureGethDataDir ( )
113
-
114
114
// If the process from the previous browswer session still lingers, it should be killed
115
115
if ( await fs . pathExists ( pidPath ) ) {
116
116
try {
117
117
const pid = await fs . readFile ( pidPath )
118
118
cleanupGeth ( pid )
119
119
} catch ( ex ) {
120
- console . error ( 'Could not read from geth.pid' )
120
+ console . error ( 'unable to read ' + pidPath + ': ' + ex . message )
121
121
}
122
122
}
123
123
124
- geth = spawn ( gethProcessPath , gethArgs , gethOptions )
124
+ ensureGethDataDir ( )
125
+
126
+ try {
127
+ geth = spawn ( gethProcessPath , gethArgs , gethOptions )
125
128
126
- geth . on ( 'exit' , handleGethStop . bind ( null , 'exit' ) )
127
- geth . on ( 'close' , handleGethStop . bind ( null , 'close' ) )
129
+ geth . on ( 'exit' , handleGethStop . bind ( null , 'exit' ) )
130
+ geth . on ( 'close' , handleGethStop . bind ( null , 'close' ) )
128
131
129
- await writeGethPid ( geth . pid )
132
+ await writeGethPid ( geth . pid )
130
133
131
- console . warn ( 'GETH: spawned' )
134
+ console . warn ( 'GETH: spawned' )
135
+ } catch ( ex ) {
136
+ console . error ( 'unable to spawn ' + gethProcessPath + ': ' + ex . message )
137
+ cleanupGeth ( geth && geth . pid )
138
+ }
132
139
}
133
140
134
141
const ensureGethDataDir = ( ) => {
@@ -159,44 +166,64 @@ const handleGethStop = (event, code, signal) => {
159
166
160
167
const writeGethPid = async ( pid ) => {
161
168
if ( ! pid ) {
162
- return
169
+ throw new Error ( 'no pid returned by spawn' )
163
170
}
164
171
165
172
gethProcessId = pid
166
173
167
174
try {
168
175
await fs . writeFile ( pidPath , gethProcessId )
169
176
} catch ( ex ) {
170
- console . error ( 'Could not write geth.pid' )
177
+ console . error ( 'unable to write ' + pidPath + ': ' + ex . message )
178
+ throw ex
171
179
}
172
180
}
173
181
182
+ const cleanupGethAndExit = ( exitCode ) => {
183
+ cleanupGeth ( )
184
+ process . exit ( exitCode || 2 )
185
+ }
186
+
174
187
const cleanupGeth = ( processId ) => {
175
188
processId = processId || gethProcessId
176
189
177
- if ( processId ) {
178
- // Set geth to null to remove bound listeners
179
- // Otherwise, geth will attempt to restart itself
180
- // when killed.
181
- geth = null
190
+ if ( ! processId ) return console . warn ( 'GET: nothing to cleanup' )
191
+
192
+ // Set geth to null to remove bound listeners
193
+ // Otherwise, geth will attempt to restart itself
194
+ // when killed.
195
+ geth = null
182
196
183
- // Kill process
197
+ // Kill process
198
+ try {
184
199
process . kill ( processId )
200
+ } catch ( ex ) {
201
+ console . error ( 'unable to kill ' + processId + ': ' + ex . message )
202
+ }
203
+ try {
204
+ process . kill ( processId , 0 )
205
+ console . log ( 'GETH: unable to kill ' + processId )
206
+ } catch ( ex ) {
207
+ if ( ex . code === 'ESRCH' ) {
208
+ console . log ( 'GETH: process killed' )
209
+ } else {
210
+ console . error ( 'unable to kill ' + processId + ': ' + ex . message )
211
+ }
212
+ }
185
213
186
- // Remove in memory process id
187
- gethProcessId = null
214
+ // Remove in memory process id
215
+ gethProcessId = null
188
216
189
- // Named pipes on Windows will get deleted
190
- // automatically once no processes are using them.
191
- if ( ! isWindows ) {
192
- try {
193
- fs . unlinkSync ( pidPath )
194
- } catch ( ex ) {
195
- console . error ( 'Could not delete geth.pid' )
196
- }
217
+ // Named pipes on Windows will get deleted
218
+ // automatically once no processes are using them.
219
+ if ( ! isWindows ) {
220
+ try {
221
+ fs . unlinkSync ( pidPath )
222
+ } catch ( ex ) {
223
+ console . error ( 'unable to delete ' + pidPath + ': ' + ex . message )
197
224
}
198
- console . warn ( 'GETH: cleanup done' )
199
225
}
226
+ console . warn ( 'GETH: cleanup done' )
200
227
}
201
228
202
229
// Attempts to restart geth up to 3 times
@@ -218,16 +245,19 @@ const restartGeth = async (tries = 3) => {
218
245
219
246
// Geth should be killed on normal process, exit, SIGINT,
220
247
// and application crashing exceptions.
221
- process . on ( 'exit' , ( ) => {
222
- cleanupGeth ( gethProcessId )
223
- } )
224
- process . on ( 'SIGINT' , ( ) => {
225
- cleanupGeth ( gethProcessId )
226
- process . exit ( 2 )
227
- } )
248
+ process . on ( 'exit' , cleanupGeth )
249
+ process . on ( 'SIGHUP' , cleanupGethAndExit )
250
+ process . on ( 'SIGTERM' , cleanupGethAndExit )
251
+ process . on ( 'SIGINT' , cleanupGethAndExit )
228
252
229
253
ipcMain . on ( 'eth-wallet-create-wallet' , ( e , pwd ) => {
230
- const client = net . createConnection ( ipcPath )
254
+ let client
255
+
256
+ try {
257
+ client = net . createConnection ( ipcPath )
258
+ } catch ( ex ) {
259
+ return console . error ( 'unable to connect to ' + ipcPath + ' (1): ' + ex . message )
260
+ }
231
261
232
262
client . on ( 'connect' , ( ) => {
233
263
client . write ( JSON . stringify ( { 'method' : 'personal_newAccount' , 'params' : [ pwd ] , 'id' : 1 , 'jsonrpc' : '2.0' } ) )
@@ -243,7 +273,13 @@ ipcMain.on('eth-wallet-create-wallet', (e, pwd) => {
243
273
} )
244
274
245
275
ipcMain . on ( 'eth-wallet-wallets' , ( e , data ) => {
246
- const client = net . createConnection ( ipcPath )
276
+ let client
277
+
278
+ try {
279
+ client = net . createConnection ( ipcPath )
280
+ } catch ( ex ) {
281
+ console . error ( 'unable to connect to ' + ipcPath + ' (2): ' + ex . message )
282
+ }
247
283
248
284
client . on ( 'connect' , ( ) => {
249
285
client . write ( JSON . stringify ( { 'method' : 'db_putString' , 'params' : [ 'braveEthWallet' , 'wallets' , data ] , 'id' : 1 , 'jsonrpc' : '2.0' } ) )
@@ -255,7 +291,13 @@ ipcMain.on('eth-wallet-wallets', (e, data) => {
255
291
} )
256
292
257
293
ipcMain . on ( 'eth-wallet-unlock-account' , ( e , address , pw ) => {
258
- const client = net . createConnection ( ipcPath )
294
+ let client
295
+
296
+ try {
297
+ client = net . createConnection ( ipcPath )
298
+ } catch ( ex ) {
299
+ console . error ( 'unable to connect to ' + ipcPath + ' (3): ' + ex . message )
300
+ }
259
301
260
302
client . on ( 'connect' , ( ) => {
261
303
client . write ( JSON . stringify ( { 'method' : 'personal_unlockAccount' , 'params' : [ address , pw ] , 'id' : 1 , 'jsonrpc' : '2.0' } ) )
0 commit comments