[syncmgr]

- refactored OnBlockBlockMode to use blockPool
- syncmgr now checks for future blocks, instead of looking for a
FutureBlockErr from the Chain. This makes it so that syncmgr does not
depend on the Chain package.
- removed GetBestBlockHash function from config(unused)
- refactored all tests in the syncmgr package
This commit is contained in:
BlockChainDev 2019-03-30 21:36:50 +00:00
parent c401247af9
commit 751d2711d4
6 changed files with 34 additions and 31 deletions

View file

@ -9,22 +9,29 @@ import (
// and receives a block. // and receives a block.
func (s *Syncmgr) blockModeOnBlock(peer SyncPeer, block payload.Block) error { func (s *Syncmgr) blockModeOnBlock(peer SyncPeer, block payload.Block) error {
// Process Block // Check if it is a future block
err := s.cfg.ProcessBlock(block) // XXX: since we are storing blocks in memory, we do not want to store blocks
// from the tip
if err == chain.ErrFutureBlock { if block.Index > s.nextBlockIndex+2000 {
// XXX(Optimisation): We can cache future blocks in blockmode, if we have the corresponding header return nil
// We can have the server cache them and sort out the semantics for when to send them to the chain }
// Server can listen on chain for when a new block is saved if block.Index != s.nextBlockIndex {
// or we could embed a struct in this syncmgr called blockCache, syncmgr can just tell it when it has processed s.addToBlockPool(block)
//a block and we can call ProcessBlock return nil
return err
} }
// Process Block
err := s.processBlock(block)
if err != nil && err != chain.ErrBlockAlreadyExists { if err != nil && err != chain.ErrBlockAlreadyExists {
return s.cfg.FetchBlockAgain(block.Hash) return s.cfg.FetchBlockAgain(block.Hash)
} }
// Check the block pool
err = s.checkPool()
if err != nil {
return err
}
// Check if blockhashReceived == the header hash from last get headers this node performed // Check if blockhashReceived == the header hash from last get headers this node performed
// if not then increment and request next block // if not then increment and request next block
if s.headerHash != block.Hash { if s.headerHash != block.Hash {
@ -32,8 +39,7 @@ func (s *Syncmgr) blockModeOnBlock(peer SyncPeer, block payload.Block) error {
if err != nil { if err != nil {
return err return err
} }
err = s.cfg.RequestBlock(nextHash, block.Index) return s.cfg.RequestBlock(nextHash, block.Index)
return err
} }
// If we are caught up then go into normal mode // If we are caught up then go into normal mode

View file

@ -24,9 +24,6 @@ type Config struct {
// at the tip of this nodes chain. This assumes that the node is not in sync // at the tip of this nodes chain. This assumes that the node is not in sync
GetNextBlockHash func() (util.Uint256, error) GetNextBlockHash func() (util.Uint256, error)
// GetBestBlockHash gets the block hash of the last saved block.
GetBestBlockHash func() (util.Uint256, error)
// AskForNewBlocks will send out a message to the network // AskForNewBlocks will send out a message to the network
// asking for new blocks // asking for new blocks
AskForNewBlocks func() AskForNewBlocks func()

View file

@ -89,7 +89,7 @@ func randomUint256(t *testing.T) util.Uint256 {
return u return u
} }
func setupSyncMgr(mode mode) (*Syncmgr, *syncTestHelper) { func setupSyncMgr(mode mode, nextBlockIndex uint32) (*Syncmgr, *syncTestHelper) {
helper := &syncTestHelper{} helper := &syncTestHelper{}
cfg := &Config{ cfg := &Config{
@ -106,7 +106,7 @@ func setupSyncMgr(mode mode) (*Syncmgr, *syncTestHelper) {
RequestHeaders: helper.RequestHeaders, RequestHeaders: helper.RequestHeaders,
} }
syncmgr := New(cfg) syncmgr := New(cfg, nextBlockIndex)
syncmgr.syncmode = mode syncmgr.syncmode = mode
return syncmgr, helper return syncmgr, helper

View file

@ -43,7 +43,7 @@ func (s *Syncmgr) normalModeOnBlock(peer SyncPeer, block payload.Block) error {
s.timer.Stop() s.timer.Stop()
// process block // process block
err := s.cfg.ProcessBlock(block) err := s.processBlock(block)
if err != nil { if err != nil {
s.timer.Reset(blockTimer) s.timer.Reset(blockTimer)
return s.cfg.FetchBlockAgain(block.Hash) return s.cfg.FetchBlockAgain(block.Hash)

View file

@ -11,7 +11,7 @@ import (
func TestHeadersModeOnBlock(t *testing.T) { func TestHeadersModeOnBlock(t *testing.T) {
syncmgr, helper := setupSyncMgr(headersMode) syncmgr, helper := setupSyncMgr(headersMode, 0)
syncmgr.OnBlock(&mockPeer{}, randomBlockMessage(t, 0)) syncmgr.OnBlock(&mockPeer{}, randomBlockMessage(t, 0))
@ -21,7 +21,7 @@ func TestHeadersModeOnBlock(t *testing.T) {
func TestBlockModeOnBlock(t *testing.T) { func TestBlockModeOnBlock(t *testing.T) {
syncmgr, helper := setupSyncMgr(blockMode) syncmgr, helper := setupSyncMgr(blockMode, 0)
syncmgr.OnBlock(&mockPeer{}, randomBlockMessage(t, 0)) syncmgr.OnBlock(&mockPeer{}, randomBlockMessage(t, 0))
@ -30,7 +30,7 @@ func TestBlockModeOnBlock(t *testing.T) {
} }
func TestNormalModeOnBlock(t *testing.T) { func TestNormalModeOnBlock(t *testing.T) {
syncmgr, helper := setupSyncMgr(normalMode) syncmgr, helper := setupSyncMgr(normalMode, 0)
syncmgr.OnBlock(&mockPeer{}, randomBlockMessage(t, 0)) syncmgr.OnBlock(&mockPeer{}, randomBlockMessage(t, 0))
@ -40,7 +40,7 @@ func TestNormalModeOnBlock(t *testing.T) {
func TestBlockModeToNormalMode(t *testing.T) { func TestBlockModeToNormalMode(t *testing.T) {
syncmgr, _ := setupSyncMgr(blockMode) syncmgr, _ := setupSyncMgr(blockMode, 100)
peer := &mockPeer{ peer := &mockPeer{
height: 100, height: 100,
@ -57,7 +57,7 @@ func TestBlockModeToNormalMode(t *testing.T) {
} }
func TestBlockModeStayInBlockMode(t *testing.T) { func TestBlockModeStayInBlockMode(t *testing.T) {
syncmgr, _ := setupSyncMgr(blockMode) syncmgr, _ := setupSyncMgr(blockMode, 0)
// We need our latest know hash to not be equal to the hash // We need our latest know hash to not be equal to the hash
// of the block we received, to stay in blockmode // of the block we received, to stay in blockmode
@ -77,7 +77,7 @@ func TestBlockModeStayInBlockMode(t *testing.T) {
} }
func TestBlockModeAlreadyExistsErr(t *testing.T) { func TestBlockModeAlreadyExistsErr(t *testing.T) {
syncmgr, helper := setupSyncMgr(blockMode) syncmgr, helper := setupSyncMgr(blockMode, 100)
helper.err = chain.ErrBlockAlreadyExists helper.err = chain.ErrBlockAlreadyExists
syncmgr.OnBlock(&mockPeer{}, randomBlockMessage(t, 100)) syncmgr.OnBlock(&mockPeer{}, randomBlockMessage(t, 100))

View file

@ -12,7 +12,7 @@ import (
func TestHeadersModeOnHeaders(t *testing.T) { func TestHeadersModeOnHeaders(t *testing.T) {
syncmgr, helper := setupSyncMgr(headersMode) syncmgr, helper := setupSyncMgr(headersMode, 0)
syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 0)) syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 0))
@ -29,14 +29,14 @@ func TestHeadersModeOnHeaders(t *testing.T) {
} }
func TestBlockModeOnHeaders(t *testing.T) { func TestBlockModeOnHeaders(t *testing.T) {
syncmgr, helper := setupSyncMgr(blockMode) syncmgr, helper := setupSyncMgr(blockMode, 0)
// If we receive a header in blockmode, no headers will be processed // If we receive a header in blockmode, no headers will be processed
syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 100)) syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 100))
assert.Equal(t, 0, helper.headersProcessed) assert.Equal(t, 0, helper.headersProcessed)
} }
func TestNormalModeOnHeadersMaxHeaders(t *testing.T) { func TestNormalModeOnHeadersMaxHeaders(t *testing.T) {
syncmgr, helper := setupSyncMgr(normalMode) syncmgr, helper := setupSyncMgr(normalMode, 0)
// If we receive a header in normalmode, headers will be processed // If we receive a header in normalmode, headers will be processed
syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 2000)) syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 2000))
@ -49,7 +49,7 @@ func TestNormalModeOnHeadersMaxHeaders(t *testing.T) {
// This differs from the previous function in that // This differs from the previous function in that
//we did not receive the max amount of headers //we did not receive the max amount of headers
func TestNormalModeOnHeaders(t *testing.T) { func TestNormalModeOnHeaders(t *testing.T) {
syncmgr, helper := setupSyncMgr(normalMode) syncmgr, helper := setupSyncMgr(normalMode, 0)
// If we receive a header in normalmode, headers will be processed // If we receive a header in normalmode, headers will be processed
syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 200)) syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 200))
@ -60,7 +60,7 @@ func TestNormalModeOnHeaders(t *testing.T) {
} }
func TestLastHeaderUpdates(t *testing.T) { func TestLastHeaderUpdates(t *testing.T) {
syncmgr, _ := setupSyncMgr(headersMode) syncmgr, _ := setupSyncMgr(headersMode, 0)
hdrsMessage := randomHeadersMessage(t, 200) hdrsMessage := randomHeadersMessage(t, 200)
hdrs := hdrsMessage.Headers hdrs := hdrsMessage.Headers
@ -95,7 +95,7 @@ func TestLastHeaderUpdates(t *testing.T) {
func TestHeadersModeOnHeadersErr(t *testing.T) { func TestHeadersModeOnHeadersErr(t *testing.T) {
syncmgr, helper := setupSyncMgr(headersMode) syncmgr, helper := setupSyncMgr(headersMode, 0)
helper.err = &chain.ValidationError{} helper.err = &chain.ValidationError{}
syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 200)) syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 200))
@ -106,7 +106,7 @@ func TestHeadersModeOnHeadersErr(t *testing.T) {
} }
func TestNormalModeOnHeadersErr(t *testing.T) { func TestNormalModeOnHeadersErr(t *testing.T) {
syncmgr, helper := setupSyncMgr(normalMode) syncmgr, helper := setupSyncMgr(normalMode, 0)
helper.err = &chain.ValidationError{} helper.err = &chain.ValidationError{}
syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 200)) syncmgr.OnHeader(&mockPeer{}, randomHeadersMessage(t, 200))