[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 ( import (
"fmt" "fmt"
"sync"
"time" "time"
"github.com/CityOfZion/neo-go/pkg/wire/payload" "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 // When receiving blocks, we can use this to determine whether the node has downloaded
// all of the blocks for the last headers messages // all of the blocks for the last headers messages
headerHash util.Uint256 headerHash util.Uint256
poolLock sync.Mutex
blockPool []payload.Block
nextBlockIndex uint32
} }
// New creates a new sync manager // New creates a new sync manager
func New(cfg *Config) *Syncmgr { func New(cfg *Config, nextBlockIndex uint32) *Syncmgr {
newBlockTimer := time.AfterFunc(blockTimer, func() { newBlockTimer := time.AfterFunc(blockTimer, func() {
cfg.AskForNewBlocks() cfg.AskForNewBlocks()
@ -64,6 +69,7 @@ func New(cfg *Config) *Syncmgr {
syncmode: headersMode, syncmode: headersMode,
cfg: cfg, cfg: cfg,
timer: newBlockTimer, timer: newBlockTimer,
nextBlockIndex: nextBlockIndex,
} }
} }
@ -133,3 +139,14 @@ func (s *Syncmgr) OnBlock(peer SyncPeer, msg *payload.BlockMessage) error {
func (s *Syncmgr) IsCurrent() bool { func (s *Syncmgr) IsCurrent() bool {
return s.syncmode == normalMode 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
}