2019-03-28 21:22:17 +00:00
|
|
|
package syncmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/chain"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/wire/payload"
|
|
|
|
)
|
|
|
|
|
|
|
|
// blockModeOnBlock is called when the sync manager is block mode
|
|
|
|
// and receives a block.
|
|
|
|
func (s *Syncmgr) blockModeOnBlock(peer SyncPeer, block payload.Block) error {
|
|
|
|
|
2019-03-30 21:36:50 +00:00
|
|
|
// Check if it is a future block
|
|
|
|
// XXX: since we are storing blocks in memory, we do not want to store blocks
|
|
|
|
// from the tip
|
|
|
|
if block.Index > s.nextBlockIndex+2000 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-30 22:42:07 +00:00
|
|
|
if block.Index > s.nextBlockIndex {
|
2019-03-30 21:36:50 +00:00
|
|
|
s.addToBlockPool(block)
|
|
|
|
return nil
|
2019-03-28 21:22:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 21:36:50 +00:00
|
|
|
// Process Block
|
|
|
|
err := s.processBlock(block)
|
2019-03-28 21:22:17 +00:00
|
|
|
if err != nil && err != chain.ErrBlockAlreadyExists {
|
|
|
|
return s.cfg.FetchBlockAgain(block.Hash)
|
|
|
|
}
|
|
|
|
|
2019-03-30 21:36:50 +00:00
|
|
|
// Check the block pool
|
|
|
|
err = s.checkPool()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-28 21:22:17 +00:00
|
|
|
// Check if blockhashReceived == the header hash from last get headers this node performed
|
|
|
|
// if not then increment and request next block
|
|
|
|
if s.headerHash != block.Hash {
|
|
|
|
nextHash, err := s.cfg.GetNextBlockHash()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-30 21:36:50 +00:00
|
|
|
return s.cfg.RequestBlock(nextHash, block.Index)
|
2019-03-28 21:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are caught up then go into normal mode
|
|
|
|
diff := peer.Height() - block.Index
|
|
|
|
if diff <= cruiseHeight {
|
|
|
|
s.syncmode = normalMode
|
|
|
|
s.timer.Reset(blockTimer)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not then we go back into headersMode and request more headers.
|
|
|
|
s.syncmode = headersMode
|
|
|
|
return s.cfg.RequestHeaders(block.Hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Syncmgr) blockModeOnHeaders(peer SyncPeer, hdrs []*payload.BlockBase) error {
|
|
|
|
// We ignore headers when in this mode
|
|
|
|
return nil
|
|
|
|
}
|