From c401247af9ed72c38db53dabe8779222be1c2fb9 Mon Sep 17 00:00:00 2001 From: BlockChainDev Date: Sat, 30 Mar 2019 21:34:27 +0000 Subject: [PATCH] [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 --- pkg/syncmgr/syncmgr.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pkg/syncmgr/syncmgr.go b/pkg/syncmgr/syncmgr.go index bb06dfa7e..dc639a8d3 100644 --- a/pkg/syncmgr/syncmgr.go +++ b/pkg/syncmgr/syncmgr.go @@ -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 +}