[syncmgr]

- refactor syncmgr for blockpool
- add nextBlockIndex so that we can add blocks to the blockPool by
comparing their index
- add processBlock helper method,wraps around cfg.ProcessBlock and increments the nextBlockIndex
internally
This commit is contained in:
BlockChainDev 2019-03-30 21:34:27 +00:00
parent 5ed61ff389
commit c401247af9

View file

@ -2,6 +2,7 @@ package syncmgr
import (
"fmt"
"sync"
"time"
"github.com/CityOfZion/neo-go/pkg/wire/payload"
@ -50,10 +51,14 @@ type Syncmgr struct {
// When receiving blocks, we can use this to determine whether the node has downloaded
// all of the blocks for the last headers messages
headerHash util.Uint256
poolLock sync.Mutex
blockPool []payload.Block
nextBlockIndex uint32
}
// New creates a new sync manager
func New(cfg *Config) *Syncmgr {
func New(cfg *Config, nextBlockIndex uint32) *Syncmgr {
newBlockTimer := time.AfterFunc(blockTimer, func() {
cfg.AskForNewBlocks()
@ -61,9 +66,10 @@ func New(cfg *Config) *Syncmgr {
newBlockTimer.Stop()
return &Syncmgr{
syncmode: headersMode,
cfg: cfg,
timer: newBlockTimer,
syncmode: headersMode,
cfg: cfg,
timer: newBlockTimer,
nextBlockIndex: nextBlockIndex,
}
}
@ -133,3 +139,14 @@ func (s *Syncmgr) OnBlock(peer SyncPeer, msg *payload.BlockMessage) error {
func (s *Syncmgr) IsCurrent() bool {
return s.syncmode == normalMode
}
func (s *Syncmgr) processBlock(block payload.Block) error {
err := s.cfg.ProcessBlock(block)
if err != nil {
return err
}
s.nextBlockIndex++
return nil
}