@@ -71,7 +71,7 @@ class Client {
71
71
this . _consensusOn ( consensus , 'transaction-mined' , ( tx , block , blockNow ) => this . _onMinedTransaction ( block , tx , blockNow ) ) ;
72
72
this . _consensusOn ( consensus , 'consensus-failed' , ( ) => this . _onConsensusFailed ( ) ) ;
73
73
74
- await this . _onConsensusChanged ( consensus . established ? Client . ConsensusState . ESTABLISHED : Client . ConsensusState . CONNECTING ) ;
74
+ this . _onConsensusChanged ( consensus . established ? Client . ConsensusState . ESTABLISHED : Client . ConsensusState . CONNECTING ) ;
75
75
76
76
if ( this . _config . hasFeature ( Client . Feature . PASSIVE ) ) {
77
77
consensus . network . allowInboundConnections = true ;
@@ -107,7 +107,7 @@ class Client {
107
107
this . _consensus = newConsensus ;
108
108
const consensus = await this . _consensus ;
109
109
oldConsensus . handoverTo ( consensus ) ;
110
- await this . _setupConsensus ( ) ;
110
+ return this . _setupConsensus ( ) ;
111
111
}
112
112
113
113
/**
@@ -181,7 +181,7 @@ class Client {
181
181
* @private
182
182
*/
183
183
_onBlock ( blockHash ) {
184
- for ( const listener of this . _blockListeners . valueIterator ( ) ) {
184
+ for ( const listener of this . _blockListeners . values ( ) ) {
185
185
listener ( blockHash ) ;
186
186
}
187
187
}
@@ -191,9 +191,10 @@ class Client {
191
191
* @private
192
192
*/
193
193
_onConsensusChanged ( state ) {
194
- if ( state === this . _consensusState ) return ;
195
194
this . _consensusSynchronizer . push ( async ( ) => {
196
195
const consensus = await this . _consensus ;
196
+ if ( state === this . _consensusState ) return ;
197
+
197
198
if ( consensus . established ) {
198
199
const oldSubscription = consensus . getSubscription ( ) ;
199
200
if ( oldSubscription . type === Subscription . Type . ADDRESSES ) {
@@ -202,17 +203,17 @@ class Client {
202
203
}
203
204
204
205
this . _consensusState = state ;
205
- for ( const listener of this . _consensusChangedListeners . valueIterator ( ) ) {
206
+ for ( const listener of this . _consensusChangedListeners . values ( ) ) {
206
207
try {
207
208
await listener ( state ) ;
208
209
} catch ( e ) {
209
210
Log . e ( Client , `Error in listener: ${ e } ` ) ;
210
211
}
211
212
}
212
213
213
- if ( state === Client . ConsensusState . ESTABLISHED ) {
214
+ if ( consensus . established ) {
214
215
const headHash = await consensus . getHeadHash ( ) ;
215
- for ( const listener of this . _headChangedListeners . valueIterator ( ) ) {
216
+ for ( const listener of this . _headChangedListeners . values ( ) ) {
216
217
try {
217
218
await listener ( headHash , 'established' , [ ] , [ headHash ] ) ;
218
219
} catch ( e ) {
@@ -224,15 +225,15 @@ class Client {
224
225
}
225
226
226
227
_onConsensusFailed ( ) {
227
- return this . _consensusSynchronizer . push ( async ( ) => {
228
+ this . _consensusSynchronizer . push ( async ( ) => {
228
229
const consensus = await this . _consensus ;
229
230
if ( consensus instanceof PicoConsensus ) {
230
231
// Upgrade pico consensus to nano consensus
231
232
Log . w ( Client , 'Pico consensus failed, automatically upgrading to nano consensus' ) ;
232
233
const newConsensus = new NanoConsensus ( await new NanoChain ( consensus . network . time ) , consensus . mempool , consensus . network ) ;
233
234
await this . _replaceConsensus ( Promise . resolve ( newConsensus ) ) ;
234
235
}
235
- } ) ;
236
+ } ) . catch ( Log . e . tag ( Client ) ) ;
236
237
}
237
238
238
239
/**
@@ -246,7 +247,7 @@ class Client {
246
247
this . _consensusSynchronizer . push ( async ( ) => {
247
248
// Process head-changed listeners.
248
249
if ( this . _consensusState === Client . ConsensusState . ESTABLISHED ) {
249
- for ( const listener of this . _headChangedListeners . valueIterator ( ) ) {
250
+ for ( const listener of this . _headChangedListeners . values ( ) ) {
250
251
try {
251
252
await listener ( blockHash , reason , revertedBlocks . map ( b => b . hash ( ) ) , adoptedBlocks . map ( b => b . hash ( ) ) ) ;
252
253
} catch ( e ) {
@@ -265,7 +266,7 @@ class Client {
265
266
if ( block . isFull ( ) ) {
266
267
revertedTxs . addAll ( block . transactions ) ;
267
268
}
268
- // FIXME
269
+ // FIXME
269
270
for ( const tx of this . _transactionConfirmWaiting . get ( this . _txConfirmsAt ( block . height ) ) . valueIterator ( ) ) {
270
271
revertedTxs . add ( tx ) ;
271
272
}
@@ -328,7 +329,7 @@ class Client {
328
329
_onPendingTransaction ( tx , blockNow ) {
329
330
let details ;
330
331
let fs = [ ] ;
331
- for ( const { listener, addresses} of this . _transactionListeners . valueIterator ( ) ) {
332
+ for ( const { listener, addresses} of this . _transactionListeners . values ( ) ) {
332
333
if ( addresses . contains ( tx . sender ) || addresses . contains ( tx . recipient ) ) {
333
334
if ( blockNow && blockNow . height >= this . _txExpiresAt ( tx ) ) {
334
335
details = details || new Client . TransactionDetails ( tx , Client . TransactionState . EXPIRED ) ;
@@ -362,7 +363,7 @@ class Client {
362
363
_onMinedTransaction ( block , tx , blockNow ) {
363
364
let details ;
364
365
let fs = [ ] ;
365
- for ( const { listener, addresses} of this . _transactionListeners . valueIterator ( ) ) {
366
+ for ( const { listener, addresses} of this . _transactionListeners . values ( ) ) {
366
367
if ( addresses . contains ( tx . sender ) || addresses . contains ( tx . recipient ) ) {
367
368
let state = Client . TransactionState . MINED , confirmations = 1 ;
368
369
if ( blockNow ) {
@@ -397,7 +398,7 @@ class Client {
397
398
_onConfirmedTransaction ( block , tx , blockNow ) {
398
399
let details ;
399
400
let fs = [ ] ;
400
- for ( const { listener, addresses} of this . _transactionListeners . valueIterator ( ) ) {
401
+ for ( const { listener, addresses} of this . _transactionListeners . values ( ) ) {
401
402
if ( addresses . contains ( tx . sender ) || addresses . contains ( tx . recipient ) ) {
402
403
details = details || new Client . TransactionDetails ( tx , Client . TransactionState . CONFIRMED , block . hash ( ) , block . height , ( blockNow . height - block . height ) + 1 , block . timestamp ) ;
403
404
fs . push ( async ( ) => {
@@ -422,7 +423,7 @@ class Client {
422
423
_onExpiredTransaction ( block , tx ) {
423
424
let details ;
424
425
let fs = [ ] ;
425
- for ( const { listener, addresses} of this . _transactionListeners . valueIterator ( ) ) {
426
+ for ( const { listener, addresses} of this . _transactionListeners . values ( ) ) {
426
427
if ( addresses . contains ( tx . sender ) || addresses . contains ( tx . recipient ) ) {
427
428
details = details || new Client . TransactionDetails ( tx , Client . TransactionState . EXPIRED ) ;
428
429
fs . push ( async ( ) => {
@@ -910,8 +911,8 @@ class Client {
910
911
* @returns {Promise }
911
912
*/
912
913
waitForConsensusEstablished ( ) {
913
- return new Promise ( async resolve => {
914
- if ( ( await this . _consensus ) . established ) {
914
+ return new Promise ( resolve => {
915
+ if ( this . _consensusState === Client . ConsensusState . ESTABLISHED ) {
915
916
resolve ( ) ;
916
917
} else {
917
918
let handle ;
0 commit comments