1
+ import { EventEmitter } from 'events' ;
2
+ import { promises as fs } from 'fs' ;
1
3
import path from 'path' ;
2
4
import bootstrap from './bootstrap' ;
3
- import Logger from './Logger' ;
4
5
import Config from './Config' ;
5
6
import DB from './db/DB' ;
6
- import OrderBook from './orderbook/OrderBook' ;
7
7
import GrpcServer from './grpc/GrpcServer' ;
8
8
import GrpcWebProxyServer from './grpc/webproxy/GrpcWebProxyServer' ;
9
- import Pool from './p2p/Pool' ;
9
+ import HttpServer from './http/HttpServer' ;
10
+ import Logger from './Logger' ;
10
11
import NodeKey from './nodekey/NodeKey' ;
12
+ import OrderBook from './orderbook/OrderBook' ;
13
+ import Pool from './p2p/Pool' ;
14
+ import InitService from './service/InitService' ;
11
15
import Service from './service/Service' ;
12
- import { EventEmitter } from 'events' ;
13
- import Swaps from './swaps/Swaps' ;
14
- import HttpServer from './http/HttpServer' ;
15
16
import SwapClientManager from './swaps/SwapClientManager' ;
16
- import InitService from './service/InitService' ;
17
- import { promises as fs } from 'fs' ;
17
+ import Swaps from './swaps/Swaps' ;
18
18
import { UnitConverter } from './utils/UnitConverter' ;
19
19
20
20
const version : string = require ( '../package.json' ) . version ;
@@ -72,6 +72,28 @@ class Xud extends EventEmitter {
72
72
}
73
73
74
74
try {
75
+ if ( ! this . config . rpc . disable ) {
76
+ // start rpc server first, it will respond with UNAVAILABLE error
77
+ // indicating xud is starting until xud finishes initializing
78
+ this . rpcServer = new GrpcServer ( loggers . rpc ) ;
79
+ await this . rpcServer . listen (
80
+ this . config . rpc . port ,
81
+ this . config . rpc . host ,
82
+ path . join ( this . config . xudir , 'tls.cert' ) ,
83
+ path . join ( this . config . xudir , 'tls.key' ) ,
84
+ ) ;
85
+
86
+ if ( ! this . config . webproxy . disable ) {
87
+ this . grpcAPIProxy = new GrpcWebProxyServer ( loggers . rpc ) ;
88
+ await this . grpcAPIProxy . listen (
89
+ this . config . webproxy . port ,
90
+ this . config . rpc . port ,
91
+ this . config . rpc . host ,
92
+ path . join ( this . config . xudir , 'tls.cert' ) ,
93
+ ) ;
94
+ }
95
+ }
96
+
75
97
this . db = new DB ( loggers . db , this . config . dbpath ) ;
76
98
await this . db . init ( this . config . network , this . config . initdb ) ;
77
99
@@ -93,18 +115,11 @@ class Xud extends EventEmitter {
93
115
nodeKey = await NodeKey . generate ( ) ;
94
116
await nodeKey . toFile ( nodeKeyPath ) ;
95
117
}
96
- } else {
118
+ } else if ( this . rpcServer ) {
119
+ this . rpcServer . grpcService . locked = true ;
97
120
const initService = new InitService ( this . swapClientManager , nodeKeyPath , nodeKeyExists ) ;
98
121
99
- const initRpcServer = new GrpcServer ( loggers . rpc ) ;
100
- initRpcServer . addXudInitService ( initService ) ;
101
- await initRpcServer . listen (
102
- this . config . rpc . port ,
103
- this . config . rpc . host ,
104
- path . join ( this . config . xudir , 'tls.cert' ) ,
105
- path . join ( this . config . xudir , 'tls.key' ) ,
106
- ) ;
107
-
122
+ this . rpcServer . grpcInitService . setInitService ( initService ) ;
108
123
this . logger . info ( "Node key is encrypted, unlock using 'xucli unlock' or set password using" +
109
124
" 'xucli create' if this is the first time starting xud" ) ;
110
125
nodeKey = await new Promise < NodeKey | undefined > ( ( resolve ) => {
@@ -115,10 +130,15 @@ class Xud extends EventEmitter {
115
130
initService . removeListener ( 'nodekey' , resolve ) ;
116
131
} ) ;
117
132
} ) ;
118
- await initRpcServer . close ( ) ;
119
133
if ( ! nodeKey ) {
120
134
return ; // we interrupted before unlocking xud
121
135
}
136
+ this . rpcServer . grpcService . locked = false ;
137
+ } else {
138
+ throw new Error ( 'rpc server cannot be disabled when xud is locked' ) ;
139
+ }
140
+ if ( this . rpcServer ) {
141
+ this . rpcServer . grpcInitService . disabled = true ;
122
142
}
123
143
124
144
this . logger . info ( `Local nodePubKey is ${ nodeKey . pubKey } ` ) ;
@@ -172,26 +192,10 @@ class Xud extends EventEmitter {
172
192
) ;
173
193
}
174
194
175
- // start rpc server last
176
- if ( ! this . config . rpc . disable ) {
177
- this . rpcServer = new GrpcServer ( loggers . rpc ) ;
178
- this . rpcServer . addXudService ( this . service ) ;
179
- await this . rpcServer . listen (
180
- this . config . rpc . port ,
181
- this . config . rpc . host ,
182
- path . join ( this . config . xudir , 'tls.cert' ) ,
183
- path . join ( this . config . xudir , 'tls.key' ) ,
184
- ) ;
195
+ // initialize rpc server last
196
+ if ( this . rpcServer ) {
197
+ this . rpcServer . grpcService . setService ( this . service ) ;
185
198
186
- if ( ! this . config . webproxy . disable ) {
187
- this . grpcAPIProxy = new GrpcWebProxyServer ( loggers . rpc ) ;
188
- await this . grpcAPIProxy . listen (
189
- this . config . webproxy . port ,
190
- this . config . rpc . port ,
191
- this . config . rpc . host ,
192
- path . join ( this . config . xudir , 'tls.cert' ) ,
193
- ) ;
194
- }
195
199
} else {
196
200
this . logger . info ( 'RPC server is disabled.' ) ;
197
201
}
0 commit comments