OSDN Git Service

Fix fast sync bug when the chain has fork (#282)
authoryahtoo <yahtoo.ma@gmail.com>
Mon, 15 Jul 2019 05:05:01 +0000 (13:05 +0800)
committerPaladz <yzhu101@uottawa.ca>
Mon, 15 Jul 2019 05:05:01 +0000 (13:05 +0800)
* Fix fast sync bug when the chain has fork

* Opz code format

* Fix test file err

netsync/chainmgr/block_process.go
netsync/chainmgr/block_process_test.go
netsync/chainmgr/fast_sync.go

index 2e6b41e..fac5cb5 100644 (file)
@@ -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 {
index 4f1e024..5bc2075 100644 (file)
@@ -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)
        }
index 06c67c8..ed426b7 100644 (file)
@@ -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")