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 {
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()
for {
for {
- nextHeight := bp.chain.BestBlockHeight() + 1
- block, err := bp.storage.readBlock(nextHeight)
+ block, err := bp.storage.readBlock(syncHeight)
if err != nil {
break
}
return
}
- bp.storage.deleteBlock(nextHeight)
+ bp.storage.deleteBlock(syncHeight)
+ syncHeight++
}
if _, ok := <-downloadNotifyCh; !ok {
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)
}
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")