Skip to content

Commit 372b273

Browse files
fix(sync): handle sync node going awol
1 parent 9082d3d commit 372b273

1 file changed

Lines changed: 26 additions & 18 deletions

File tree

  • packages/bitcore-node/src/services

packages/bitcore-node/src/services/p2p.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class P2pWorker {
7070
private invCacheLimits: any;
7171
private initialSyncComplete: boolean;
7272
private isSyncingNode: boolean;
73-
private syncingNodeHeartBeat?: NodeJS.Timer;
73+
private stopping?: boolean;
7474
private blockModel: BlockModel;
7575
constructor({ chain, network, chainConfig, blockModel = BlockStorage }) {
7676
this.blockModel = blockModel;
@@ -389,43 +389,51 @@ export class P2pWorker {
389389
this.syncing = false;
390390
}
391391

392-
registerSyncingNode() {
393-
this.syncingNodeHeartBeat = setInterval(async () => {
392+
async registerSyncingNode() {
393+
while(!this.stopping) {
394394
const syncingNode = await StateStorage.getSyncingNode({ chain: this.chain, network: this.network });
395395
if (!syncingNode) {
396-
return StateStorage.selfNominateSyncingNode({
396+
StateStorage.selfNominateSyncingNode({
397397
chain: this.chain,
398398
network: this.network,
399399
lastHeartBeat: syncingNode
400400
});
401+
continue;
401402
}
402-
const [hostname, pid] = syncingNode.split(':');
403-
const amSyncingNode = hostname === os.hostname() && pid === process.pid.toString();
403+
const [hostname, pid, timestamp] = syncingNode.split(':');
404+
const amSyncingNode = hostname === os.hostname() && pid === process.pid.toString() && Date.now() - parseInt(timestamp) < 1000;
404405
if (amSyncingNode) {
405-
StateStorage.selfNominateSyncingNode({ chain: this.chain, network: this.network, lastHeartBeat: syncingNode });
406+
StateStorage.selfNominateSyncingNode({
407+
chain: this.chain,
408+
network: this.network,
409+
lastHeartBeat: syncingNode
410+
});
406411
if (!this.isSyncingNode) {
407412
logger.info(`This worker is now the syncing node for ${this.chain} ${this.network}`);
408413
this.isSyncingNode = true;
409414
this.sync();
410415
}
411416
} else {
412-
setTimeout(() => {
413-
StateStorage.selfNominateSyncingNode({
414-
chain: this.chain,
415-
network: this.network,
416-
lastHeartBeat: syncingNode
417-
});
418-
}, 10000);
417+
if (this.isSyncingNode) {
418+
logger.info(`This worker is no longer syncing node for ${this.chain} ${this.network}`);
419+
this.isSyncingNode = false;
420+
await new Promise(resolve => setTimeout(resolve, 100000));
421+
}
422+
await new Promise(resolve => setTimeout(resolve, 10000));
423+
StateStorage.selfNominateSyncingNode({
424+
chain: this.chain,
425+
network: this.network,
426+
lastHeartBeat: syncingNode
427+
});
419428
}
420-
}, 500);
429+
await new Promise(resolve => setTimeout(resolve, 500));
430+
}
421431
}
422432

423433
async stop() {
434+
this.stopping = true;
424435
logger.debug(`Stopping worker for chain ${this.chain}`);
425436
await this.disconnect();
426-
if (this.syncingNodeHeartBeat) {
427-
clearInterval(this.syncingNodeHeartBeat);
428-
}
429437
}
430438

431439
async start() {

0 commit comments

Comments
 (0)