diff --git a/app/modules/crawler/lib/crawler.ts b/app/modules/crawler/lib/crawler.ts index bd4a1381242abdef30254ea4439433608892dc0b..93a33b858c9ea27dcb01c339850617fc38b3471d 100644 --- a/app/modules/crawler/lib/crawler.ts +++ b/app/modules/crawler/lib/crawler.ts @@ -403,17 +403,23 @@ export class BlockCrawler { return block; } async applyMainBranch(block: BlockDTO): Promise<boolean> { - let addedBlock = await server.writeBlock(block, false) - if (!this.lastDownloaded) { - this.lastDownloaded = await dao.remoteCurrent(node); - } - this.crawler.pullingEvent(server, 'applying', {number: block.number, last: this.lastDownloaded && this.lastDownloaded.number}); - if (addedBlock) { - current = addedBlock; - // Emit block events (for sharing with the network) only in forkWindowSize - if (nodeCurrent && nodeCurrent.number - addedBlock.number < server.conf.forksize) { - server.streamPush(addedBlock); + const existing = await server.dal.getAbsoluteBlockByNumberAndHash(block.number, block.hash) + if (!existing) { + let addedBlock = await server.writeBlock(block, false) + if (!this.lastDownloaded) { + this.lastDownloaded = await dao.remoteCurrent(node); + } + this.crawler.pullingEvent(server, 'applying', {number: block.number, last: this.lastDownloaded && this.lastDownloaded.number}); + if (addedBlock) { + current = addedBlock; + // Emit block events (for sharing with the network) only in forkWindowSize + if (nodeCurrent && nodeCurrent.number - addedBlock.number < server.conf.forksize) { + server.streamPush(addedBlock); + } } + } else { + this.crawler.logger && this.crawler.logger.info('Downloaded already known block#%s-%s, try to fork...', block.number, block.hash) + await server.BlockchainService.tryToFork() } return true } diff --git a/app/service/BlockchainService.ts b/app/service/BlockchainService.ts index 5dd935434a35ea18e9a0b8e14a788fb9462163d3..a8adb3d1a42acb74288465daf0eaaea609c71c63 100644 --- a/app/service/BlockchainService.ts +++ b/app/service/BlockchainService.ts @@ -169,6 +169,13 @@ export class BlockchainService extends FIFOService { } } + async tryToFork() { + return this.pushFIFO("tryToFork", async () => { + const current = await this.mainContext.current() + await this.eventuallySwitchOnSideChain(current) + }) + } + private async eventuallySwitchOnSideChain(current:DBBlock) { const branches = await this.branches() const blocksAdvanceInBlocks = this.conf.switchOnHeadAdvance