@@ -7,6 +7,7 @@ import { TransactionModel } from '../models/transaction';
7
7
import { Bitcoin } from '../types/namespaces/Bitcoin' ;
8
8
import { StateModel } from '../models/state' ;
9
9
import { SpentHeightIndicators } from '../types/Coin' ;
10
+ import os from 'os' ;
10
11
const Chain = require ( '../chain' ) ;
11
12
const LRU = require ( 'lru-cache' ) ;
12
13
@@ -22,6 +23,7 @@ export class P2pService {
22
23
private pool : any ;
23
24
private invCache : any ;
24
25
private initialSyncComplete : boolean ;
26
+ private isSyncingNode : boolean ;
25
27
constructor ( params ) {
26
28
const { chain, network, chainConfig } = params ;
27
29
this . chain = chain ;
@@ -32,6 +34,7 @@ export class P2pService {
32
34
this . events = new EventEmitter ( ) ;
33
35
this . syncing = false ;
34
36
this . initialSyncComplete = false ;
37
+ this . isSyncingNode = false ;
35
38
this . invCache = new LRU ( { max : 10000 } ) ;
36
39
this . messages = new this . bitcoreP2p . Messages ( {
37
40
network : this . bitcoreLib . Networks . get ( this . network )
@@ -76,7 +79,7 @@ export class P2pService {
76
79
network : this . network ,
77
80
hash
78
81
} ) ;
79
- if ( ! this . invCache . get ( hash ) ) {
82
+ if ( this . isSyncingNode && ! this . invCache . get ( hash ) ) {
80
83
this . processTransaction ( message . transaction ) ;
81
84
this . events . emit ( 'transaction' , message . transaction ) ;
82
85
}
@@ -93,7 +96,7 @@ export class P2pService {
93
96
hash
94
97
} ) ;
95
98
96
- if ( ! this . invCache . get ( hash ) ) {
99
+ if ( this . isSyncingNode && ! this . invCache . get ( hash ) ) {
97
100
this . invCache . set ( hash ) ;
98
101
this . events . emit ( hash , message . block ) ;
99
102
this . events . emit ( 'block' , message . block ) ;
@@ -112,7 +115,7 @@ export class P2pService {
112
115
} ) ;
113
116
114
117
this . pool . on ( 'peerinv' , ( peer , message ) => {
115
- if ( ! this . syncing ) {
118
+ if ( this . isSyncingNode && ! this . syncing ) {
116
119
const filtered = message . inventory . filter ( inv => {
117
120
const hash = this . bitcoreLib . encoding
118
121
. BufferReader ( inv . hash )
@@ -304,6 +307,22 @@ export class P2pService {
304
307
logger . debug ( `Started worker for chain ${ this . chain } ` ) ;
305
308
this . setupListeners ( ) ;
306
309
await this . connect ( ) ;
307
- this . sync ( ) ;
310
+ setInterval ( async ( ) => {
311
+ const syncingNode = await StateModel . getSyncingNode ( { chain : this . chain , network : this . network } ) ;
312
+ const [ hostname , pid ] = syncingNode . split ( ':' ) ;
313
+ const amSyncingNode = ( hostname === os . hostname ( ) && pid === process . pid . toString ( ) ) ;
314
+ if ( amSyncingNode ) {
315
+ StateModel . selfNominateSyncingNode ( { chain : this . chain , network : this . network , lastHeartBeat : syncingNode } ) ;
316
+ if ( ! this . isSyncingNode ) {
317
+ logger . info ( `This worker is now the syncing node for ${ this . chain } ${ this . network } ` ) ;
318
+ this . isSyncingNode = true ;
319
+ this . sync ( ) ;
320
+ }
321
+ } else {
322
+ setTimeout ( ( ) => {
323
+ StateModel . selfNominateSyncingNode ( { chain : this . chain , network : this . network , lastHeartBeat : syncingNode } ) ;
324
+ } , 5000 )
325
+ }
326
+ } , 1000 ) ;
308
327
}
309
328
}
0 commit comments