2019-03-28 22:49:34 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
|
2019-03-30 18:10:27 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/peermgr"
|
|
|
|
|
2019-03-28 22:49:34 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/peer"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/syncmgr"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/wire/payload"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/wire/util"
|
|
|
|
)
|
|
|
|
|
2019-03-30 21:39:39 +00:00
|
|
|
func setupSyncManager(s *Server) (*syncmgr.Syncmgr, error) {
|
2019-03-28 22:49:34 +00:00
|
|
|
|
|
|
|
cfg := &syncmgr.Config{
|
|
|
|
ProcessBlock: s.processBlock,
|
|
|
|
ProcessHeaders: s.processHeaders,
|
|
|
|
|
|
|
|
RequestBlock: s.requestBlock,
|
|
|
|
RequestHeaders: s.requestHeaders,
|
|
|
|
|
|
|
|
GetNextBlockHash: s.getNextBlockHash,
|
|
|
|
AskForNewBlocks: s.askForNewBlocks,
|
|
|
|
|
|
|
|
FetchHeadersAgain: s.fetchHeadersAgain,
|
|
|
|
FetchBlockAgain: s.fetchBlockAgain,
|
|
|
|
}
|
|
|
|
|
2019-03-30 21:39:39 +00:00
|
|
|
// Add nextBlockIndex in syncmgr
|
|
|
|
lastBlock, err := s.chain.Db.GetLastBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nextBlockIndex := lastBlock.Index + 1
|
|
|
|
|
|
|
|
return syncmgr.New(cfg, nextBlockIndex), nil
|
2019-03-28 22:49:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) onHeader(peer *peer.Peer, hdrsMessage *payload.HeadersMessage) {
|
|
|
|
s.pmg.MsgReceived(peer, hdrsMessage.Command())
|
|
|
|
s.smg.OnHeader(peer, hdrsMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) onBlock(peer *peer.Peer, blockMsg *payload.BlockMessage) {
|
2019-03-30 18:10:27 +00:00
|
|
|
s.pmg.BlockMsgReceived(peer, peermgr.BlockInfo{
|
|
|
|
BlockHash: blockMsg.Hash,
|
|
|
|
BlockIndex: blockMsg.Index,
|
|
|
|
})
|
2019-03-28 22:49:34 +00:00
|
|
|
s.smg.OnBlock(peer, blockMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) processBlock(block payload.Block) error {
|
|
|
|
return s.chain.ProcessBlock(block)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) processHeaders(hdrs []*payload.BlockBase) error {
|
|
|
|
return s.chain.ProcessHeaders(hdrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) requestHeaders(hash util.Uint256) error {
|
|
|
|
return s.pmg.RequestHeaders(hash)
|
|
|
|
}
|
|
|
|
|
2019-03-30 18:10:27 +00:00
|
|
|
func (s *Server) requestBlock(hash util.Uint256, index uint32) error {
|
|
|
|
return s.pmg.RequestBlock(peermgr.BlockInfo{
|
|
|
|
BlockHash: hash,
|
|
|
|
BlockIndex: index,
|
|
|
|
})
|
2019-03-28 22:49:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getNextBlockHash searches the database for the blockHash
|
|
|
|
// that is the height above our best block. The hash will be taken from a header.
|
|
|
|
func (s *Server) getNextBlockHash() (util.Uint256, error) {
|
|
|
|
bestBlock, err := s.chain.Db.GetLastBlock()
|
|
|
|
if err != nil {
|
|
|
|
// Panic!
|
|
|
|
// XXX: One alternative, is to get the network, erase the database and then start again from scratch.
|
|
|
|
// This should never happen. The latest block will always be atleast the genesis block
|
|
|
|
panic("could not get best block from database" + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
index := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(index, bestBlock.Index+1)
|
|
|
|
|
|
|
|
hdr, err := s.chain.Db.GetHeaderFromHeight(index)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, err
|
|
|
|
}
|
|
|
|
return hdr.Hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getBestBlockHash() (util.Uint256, error) {
|
|
|
|
return util.Uint256{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) askForNewBlocks() {
|
|
|
|
// send a getblocks message with the latest block saved
|
|
|
|
|
|
|
|
// when we receive something then send get data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) fetchHeadersAgain(util.Uint256) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) fetchBlockAgain(util.Uint256) error {
|
|
|
|
return nil
|
|
|
|
}
|