@@ -7,6 +7,7 @@ import { TransactionModel } from '../models/transaction';
77import { Bitcoin } from '../types/namespaces/Bitcoin' ;
88import { StateModel } from '../models/state' ;
99import { SpentHeightIndicators } from '../types/Coin' ;
10+ import os from 'os' ;
1011const Chain = require ( '../chain' ) ;
1112const LRU = require ( 'lru-cache' ) ;
1213
@@ -22,6 +23,7 @@ export class P2pService {
2223 private pool : any ;
2324 private invCache : any ;
2425 private initialSyncComplete : boolean ;
26+ private isSyncingNode : boolean ;
2527 constructor ( params ) {
2628 const { chain, network, chainConfig } = params ;
2729 this . chain = chain ;
@@ -32,6 +34,7 @@ export class P2pService {
3234 this . events = new EventEmitter ( ) ;
3335 this . syncing = false ;
3436 this . initialSyncComplete = false ;
37+ this . isSyncingNode = false ;
3538 this . invCache = new LRU ( { max : 10000 } ) ;
3639 this . messages = new this . bitcoreP2p . Messages ( {
3740 network : this . bitcoreLib . Networks . get ( this . network )
@@ -76,7 +79,7 @@ export class P2pService {
7679 network : this . network ,
7780 hash
7881 } ) ;
79- if ( ! this . invCache . get ( hash ) ) {
82+ if ( this . isSyncingNode && ! this . invCache . get ( hash ) ) {
8083 this . processTransaction ( message . transaction ) ;
8184 this . events . emit ( 'transaction' , message . transaction ) ;
8285 }
@@ -93,7 +96,7 @@ export class P2pService {
9396 hash
9497 } ) ;
9598
96- if ( ! this . invCache . get ( hash ) ) {
99+ if ( this . isSyncingNode && ! this . invCache . get ( hash ) ) {
97100 this . invCache . set ( hash ) ;
98101 this . events . emit ( hash , message . block ) ;
99102 this . events . emit ( 'block' , message . block ) ;
@@ -112,7 +115,7 @@ export class P2pService {
112115 } ) ;
113116
114117 this . pool . on ( 'peerinv' , ( peer , message ) => {
115- if ( ! this . syncing ) {
118+ if ( this . isSyncingNode && ! this . syncing ) {
116119 const filtered = message . inventory . filter ( inv => {
117120 const hash = this . bitcoreLib . encoding
118121 . BufferReader ( inv . hash )
@@ -304,6 +307,22 @@ export class P2pService {
304307 logger . debug ( `Started worker for chain ${ this . chain } ` ) ;
305308 this . setupListeners ( ) ;
306309 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 ) ;
308327 }
309328}
0 commit comments