From a9f9f1d5be72e8e37546aae920dee147140debfa Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Fri, 11 Aug 2017 11:17:23 +0200 Subject: [PATCH] [fix] #1068 Branch rebasing might fail with already known fork blocks --- app/modules/crawler/lib/crawler.ts | 26 ++++++++++++++++---------- app/service/BlockchainService.ts | 7 +++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/app/modules/crawler/lib/crawler.ts b/app/modules/crawler/lib/crawler.ts index bd4a13812..93a33b858 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 5dd935434..a8adb3d1a 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 -- GitLab