From: yahtoo Date: Mon, 15 Jul 2019 05:05:01 +0000 (+0800) Subject: Fix fast sync bug when the chain has fork (#282) X-Git-Tag: v1.0.5~140 X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=commitdiff_plain;h=8643695a1ff4d574d231ef7ada1cde3bbc76404b;hp=a1d6ba85cdcf3b2f1877a2bd252fa613ae6f7944 Fix fast sync bug when the chain has fork (#282) * Fix fast sync bug when the chain has fork * Opz code format * Fix test file err --- diff --git a/netsync/chainmgr/block_process.go b/netsync/chainmgr/block_process.go index 2e6b41ea..fac5cb51 100644 --- a/netsync/chainmgr/block_process.go +++ b/netsync/chainmgr/block_process.go @@ -13,7 +13,7 @@ import ( var errOrphanBlock = errors.New("fast sync inserting orphan block") type BlockProcessor interface { - process(chan struct{}, chan struct{}, *sync.WaitGroup) + process(chan struct{}, chan struct{}, uint64, *sync.WaitGroup) } type blockProcessor struct { @@ -43,7 +43,7 @@ func (bp *blockProcessor) insert(blockStorage *blockStorage) error { return err } -func (bp *blockProcessor) process(downloadNotifyCh chan struct{}, ProcessStop chan struct{}, wg *sync.WaitGroup) { +func (bp *blockProcessor) process(downloadNotifyCh chan struct{}, ProcessStop chan struct{}, syncHeight uint64, wg *sync.WaitGroup) { defer func() { close(ProcessStop) wg.Done() @@ -51,8 +51,7 @@ func (bp *blockProcessor) process(downloadNotifyCh chan struct{}, ProcessStop ch for { for { - nextHeight := bp.chain.BestBlockHeight() + 1 - block, err := bp.storage.readBlock(nextHeight) + block, err := bp.storage.readBlock(syncHeight) if err != nil { break } @@ -62,7 +61,8 @@ func (bp *blockProcessor) process(downloadNotifyCh chan struct{}, ProcessStop ch return } - bp.storage.deleteBlock(nextHeight) + bp.storage.deleteBlock(syncHeight) + syncHeight++ } if _, ok := <-downloadNotifyCh; !ok { diff --git a/netsync/chainmgr/block_process_test.go b/netsync/chainmgr/block_process_test.go index 4f1e024b..5bc2075f 100644 --- a/netsync/chainmgr/block_process_test.go +++ b/netsync/chainmgr/block_process_test.go @@ -43,7 +43,7 @@ func TestBlockProcess(t *testing.T) { close(downloadNotifyCh) }() wg.Add(1) - bp.process(downloadNotifyCh, ProcessStopCh, &wg) + bp.process(downloadNotifyCh, ProcessStopCh, uint64(blockNum/2), &wg) if bp.chain.BestBlockHeight() != uint64(blockNum) { t.Fatalf("TestBlockProcess fail: got %d want %d", bp.chain.BestBlockHeight(), blockNum) } diff --git a/netsync/chainmgr/fast_sync.go b/netsync/chainmgr/fast_sync.go index 06c67c80..ed426b7c 100644 --- a/netsync/chainmgr/fast_sync.go +++ b/netsync/chainmgr/fast_sync.go @@ -131,7 +131,7 @@ func (fs *fastSync) process() error { var wg sync.WaitGroup wg.Add(2) go fs.msgFetcher.parallelFetchBlocks(tasks, downloadNotifyCh, processStopCh, &wg) - go fs.blockProcessor.process(downloadNotifyCh, processStopCh, &wg) + go fs.blockProcessor.process(downloadNotifyCh, processStopCh, tasks[0].startHeader.Height, &wg) wg.Wait() fs.msgFetcher.resetParameter() log.WithFields(log.Fields{"module": logModule, "height": fs.chain.BestBlockHeight()}).Info("fast sync complete")