2018-01-26 18:04:13 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2018-03-14 09:36:59 +00:00
|
|
|
"errors"
|
2018-01-26 18:04:13 +00:00
|
|
|
"fmt"
|
2018-03-14 09:36:59 +00:00
|
|
|
"sync"
|
2018-01-28 10:12:05 +00:00
|
|
|
"time"
|
2018-01-27 15:00:28 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/network/payload"
|
2018-02-01 18:54:23 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2018-03-14 09:36:59 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-01-26 18:04:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-03-14 09:36:59 +00:00
|
|
|
minPeers = 5
|
|
|
|
maxBlockBatch = 200
|
|
|
|
minPoolCount = 30
|
2018-01-26 18:04:13 +00:00
|
|
|
)
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
var (
|
|
|
|
errPortMismatch = errors.New("port mismatch")
|
|
|
|
errIdenticalID = errors.New("identical node id")
|
|
|
|
errInvalidHandshake = errors.New("invalid handshake")
|
|
|
|
errInvalidNetwork = errors.New("invalid network")
|
|
|
|
errServerShutdown = errors.New("server shutdown")
|
|
|
|
errInvalidInvType = errors.New("invalid inventory type")
|
|
|
|
)
|
2018-02-01 20:28:45 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
type (
|
|
|
|
// Server represents the local Node in the network. Its transport could
|
|
|
|
// be of any kind.
|
|
|
|
Server struct {
|
2018-03-15 20:45:37 +00:00
|
|
|
// ServerConfig holds the Server configuration.
|
|
|
|
ServerConfig
|
2018-02-06 06:43:32 +00:00
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// id also known as the nonce of the server.
|
2018-03-14 09:36:59 +00:00
|
|
|
id uint32
|
2018-01-26 18:04:13 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
transport Transporter
|
|
|
|
discovery Discoverer
|
|
|
|
chain core.Blockchainer
|
2018-01-26 18:04:13 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
lock sync.RWMutex
|
|
|
|
peers map[Peer]bool
|
2018-01-26 20:39:34 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
register chan Peer
|
|
|
|
unregister chan peerDrop
|
|
|
|
quit chan struct{}
|
2018-01-26 18:04:13 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
proto <-chan protoTuple
|
|
|
|
}
|
2018-01-26 18:04:13 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
protoTuple struct {
|
|
|
|
msg *Message
|
|
|
|
peer Peer
|
|
|
|
}
|
2018-03-10 12:04:06 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
peerDrop struct {
|
|
|
|
peer Peer
|
|
|
|
reason error
|
2018-02-01 08:00:42 +00:00
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewServer returns a new Server, initialized with the given configuration.
|
2018-03-15 20:45:37 +00:00
|
|
|
func NewServer(config ServerConfig, chain *core.Blockchain) *Server {
|
2018-03-09 15:55:25 +00:00
|
|
|
s := &Server{
|
2018-03-15 20:45:37 +00:00
|
|
|
ServerConfig: config,
|
|
|
|
chain: chain,
|
|
|
|
id: util.RandUint32(1000000, 9999999),
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
register: make(chan Peer),
|
|
|
|
unregister: make(chan peerDrop),
|
|
|
|
peers: make(map[Peer]bool),
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 20:45:37 +00:00
|
|
|
s.transport = NewTCPTransport(s, fmt.Sprintf(":%d", config.ListenTCP))
|
2018-03-14 09:36:59 +00:00
|
|
|
s.proto = s.transport.Consumer()
|
|
|
|
s.discovery = NewDefaultDiscovery(
|
|
|
|
s.DialTimeout,
|
|
|
|
s.transport,
|
|
|
|
)
|
2018-01-26 18:04:13 +00:00
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
return s
|
2018-01-30 10:56:36 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// ID returns the servers ID.
|
|
|
|
func (s *Server) ID() uint32 {
|
|
|
|
return s.id
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// Start will start the server and its underlying transport.
|
2018-03-23 20:36:59 +00:00
|
|
|
func (s *Server) Start(errChan chan error) {
|
2018-03-17 11:53:21 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"blockHeight": s.chain.BlockHeight(),
|
|
|
|
"headerHeight": s.chain.HeaderHeight(),
|
|
|
|
}).Info("node started")
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
go s.transport.Accept()
|
|
|
|
s.discovery.BackFill(s.Seeds...)
|
|
|
|
s.run()
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
2018-01-30 10:56:36 +00:00
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// Shutdown disconnects all peers and stops listening.
|
|
|
|
func (s *Server) Shutdown() {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"peers": s.PeerCount(),
|
|
|
|
}).Info("shutting down server")
|
|
|
|
close(s.quit)
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:58:09 +00:00
|
|
|
// UnconnectedPeers returns a list of peers that are in the discovery peer list
|
|
|
|
// but are not connected to the server.
|
2018-03-23 20:36:59 +00:00
|
|
|
func (s *Server) UnconnectedPeers() []string {
|
|
|
|
return s.discovery.UnconnectedPeers()
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:58:09 +00:00
|
|
|
// BadPeers returns a list of peers the are flagged as "bad" peers.
|
2018-03-23 20:36:59 +00:00
|
|
|
func (s *Server) BadPeers() []string {
|
|
|
|
return s.discovery.BadPeers()
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
func (s *Server) run() {
|
|
|
|
// Ask discovery to connect with remote nodes to fill up
|
|
|
|
// the server minimum peer slots.
|
|
|
|
s.discovery.RequestRemote(minPeers - s.PeerCount())
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
for {
|
2018-03-14 09:36:59 +00:00
|
|
|
select {
|
|
|
|
case proto := <-s.proto:
|
|
|
|
if err := s.processProto(proto); err != nil {
|
|
|
|
proto.peer.Disconnect(err)
|
|
|
|
// verack and version implies that the protocol is
|
|
|
|
// not started and the only way to disconnect them
|
|
|
|
// from the server is to manually call unregister.
|
|
|
|
switch proto.msg.CommandType() {
|
|
|
|
case CMDVerack, CMDVersion:
|
|
|
|
go func() {
|
|
|
|
s.unregister <- peerDrop{proto.peer, err}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-s.quit:
|
|
|
|
s.transport.Close()
|
2018-03-15 20:45:37 +00:00
|
|
|
for p := range s.peers {
|
2018-03-14 09:36:59 +00:00
|
|
|
p.Disconnect(errServerShutdown)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case p := <-s.register:
|
|
|
|
// When a new peer is connected we send out our version immediately.
|
|
|
|
s.sendVersion(p)
|
|
|
|
s.peers[p] = true
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"endpoint": p.Endpoint(),
|
|
|
|
}).Info("new peer connected")
|
|
|
|
case drop := <-s.unregister:
|
2018-03-23 20:36:59 +00:00
|
|
|
s.discovery.RegisterBadAddr(drop.peer.Endpoint().String())
|
2018-03-14 09:36:59 +00:00
|
|
|
delete(s.peers, drop.peer)
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"endpoint": drop.peer.Endpoint(),
|
|
|
|
"reason": drop.reason,
|
|
|
|
"peerCount": s.PeerCount(),
|
|
|
|
}).Warn("peer disconnected")
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
2018-01-27 12:39:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// Peers returns the current list of peers connected to
|
|
|
|
// the server.
|
|
|
|
func (s *Server) Peers() map[Peer]bool {
|
|
|
|
return s.peers
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// PeerCount returns the number of current connected peers.
|
|
|
|
func (s *Server) PeerCount() int {
|
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
|
|
|
return len(s.peers)
|
2018-02-01 20:28:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// startProtocol starts a long running background loop that interacts
|
|
|
|
// every ProtoTickInterval with the peer.
|
|
|
|
func (s *Server) startProtocol(p Peer) {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"endpoint": p.Endpoint(),
|
|
|
|
"userAgent": string(p.Version().UserAgent),
|
|
|
|
"startHeight": p.Version().StartHeight,
|
|
|
|
"id": p.Version().Nonce,
|
|
|
|
}).Info("started protocol")
|
|
|
|
|
|
|
|
s.requestHeaders(p)
|
|
|
|
s.requestPeerInfo(p)
|
2018-01-31 19:11:08 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
timer := time.NewTimer(s.ProtoTickInterval)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-p.Done():
|
|
|
|
s.unregister <- peerDrop{p, err}
|
|
|
|
return
|
|
|
|
case <-timer.C:
|
|
|
|
// Try to sync in headers and block with the peer if his block height is higher then ours.
|
|
|
|
if p.Version().StartHeight > s.chain.BlockHeight() {
|
|
|
|
s.requestBlocks(p)
|
|
|
|
}
|
|
|
|
// If the discovery does not have a healthy address pool
|
|
|
|
// we will ask for a new batch of addresses.
|
|
|
|
if s.discovery.PoolCount() < minPoolCount {
|
|
|
|
s.requestPeerInfo(p)
|
2018-02-06 06:43:32 +00:00
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
timer.Reset(s.ProtoTickInterval)
|
2018-02-06 06:43:32 +00:00
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// When a peer connects to the server, we will send our version immediately.
|
2018-03-10 12:04:06 +00:00
|
|
|
func (s *Server) sendVersion(p Peer) {
|
2018-03-14 09:36:59 +00:00
|
|
|
payload := payload.NewVersion(
|
|
|
|
s.id,
|
|
|
|
s.ListenTCP,
|
|
|
|
s.UserAgent,
|
|
|
|
s.chain.BlockHeight(),
|
|
|
|
s.Relay,
|
2018-03-09 15:55:25 +00:00
|
|
|
)
|
2018-03-14 09:36:59 +00:00
|
|
|
p.Send(NewMessage(s.Net, CMDVersion, payload))
|
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// When a peer sends out his version we reply with verack after validating
|
|
|
|
// the version.
|
|
|
|
func (s *Server) handleVersionCmd(p Peer, version *payload.Version) error {
|
|
|
|
if p.Endpoint().Port != version.Port {
|
|
|
|
return errPortMismatch
|
|
|
|
}
|
|
|
|
if s.id == version.Nonce {
|
|
|
|
return errIdenticalID
|
2018-01-28 13:59:32 +00:00
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
p.Send(NewMessage(s.Net, CMDVerack, nil))
|
|
|
|
return nil
|
2018-01-28 13:59:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// handleHeadersCmd will process the headers it received from its peer.
|
|
|
|
// if the headerHeight of the blockchain still smaller then the peer
|
|
|
|
// the server will request more headers.
|
|
|
|
// This method could best be called in a separate routine.
|
|
|
|
func (s *Server) handleHeadersCmd(p Peer, headers *payload.Headers) {
|
|
|
|
if err := s.chain.AddHeaders(headers.Hdrs...); err != nil {
|
|
|
|
log.Warnf("failed processing headers: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// The peer will respond with a maximum of 2000 headers in one batch.
|
|
|
|
// We will ask one more batch here if needed. Eventually we will get synced
|
|
|
|
// due to the startProtocol routine that will ask headers every protoTick.
|
|
|
|
if s.chain.HeaderHeight() < p.Version().StartHeight {
|
|
|
|
s.requestHeaders(p)
|
2018-01-28 10:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// handleBlockCmd processes the received block received from its peer.
|
|
|
|
func (s *Server) handleBlockCmd(p Peer, block *core.Block) error {
|
2018-03-17 11:53:21 +00:00
|
|
|
if !s.chain.HasBlock(block.Hash()) {
|
|
|
|
return s.chain.AddBlock(block)
|
|
|
|
}
|
|
|
|
return nil
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2018-02-01 08:00:42 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// handleInvCmd will process the received inventory.
|
|
|
|
func (s *Server) handleInvCmd(p Peer, inv *payload.Inventory) error {
|
|
|
|
if !inv.Type.Valid() || len(inv.Hashes) == 0 {
|
|
|
|
return errInvalidInvType
|
2018-02-01 08:00:42 +00:00
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
payload := payload.NewInventory(inv.Type, inv.Hashes)
|
|
|
|
p.Send(NewMessage(s.Net, CMDGetData, payload))
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-01 08:00:42 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
func (s *Server) handleGetHeadersCmd(p Peer, getHeaders *payload.GetBlocks) error {
|
|
|
|
log.Info(getHeaders)
|
|
|
|
return nil
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
2018-02-01 08:00:42 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// requestHeaders will send a getheaders message to the peer.
|
|
|
|
// The peer will respond with headers op to a count of 2000.
|
|
|
|
func (s *Server) requestHeaders(p Peer) {
|
|
|
|
start := []util.Uint256{s.chain.CurrentHeaderHash()}
|
|
|
|
payload := payload.NewGetBlocks(start, util.Uint256{})
|
|
|
|
p.Send(NewMessage(s.Net, CMDGetHeaders, payload))
|
2018-02-01 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// requestPeerInfo will send a getaddr message to the peer
|
|
|
|
// which will respond with his known addresses in the network.
|
|
|
|
func (s *Server) requestPeerInfo(p Peer) {
|
|
|
|
p.Send(NewMessage(s.Net, CMDGetAddr, nil))
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// requestBlocks will send a getdata message to the peer
|
|
|
|
// to sync up in blocks. A maximum of maxBlockBatch will
|
|
|
|
// send at once.
|
|
|
|
func (s *Server) requestBlocks(p Peer) {
|
|
|
|
var (
|
|
|
|
hashStart = s.chain.BlockHeight() + 1
|
|
|
|
headerHeight = s.chain.HeaderHeight()
|
|
|
|
hashes = []util.Uint256{}
|
|
|
|
)
|
|
|
|
for hashStart < headerHeight && len(hashes) < maxBlockBatch {
|
|
|
|
hash := s.chain.GetHeaderHash(int(hashStart))
|
|
|
|
hashes = append(hashes, hash)
|
|
|
|
hashStart++
|
|
|
|
}
|
|
|
|
if len(hashes) > 0 {
|
|
|
|
payload := payload.NewInventory(payload.BlockType, hashes)
|
|
|
|
p.Send(NewMessage(s.Net, CMDGetData, payload))
|
|
|
|
} else if s.chain.HeaderHeight() < p.Version().StartHeight {
|
|
|
|
s.requestHeaders(p)
|
|
|
|
}
|
2018-02-01 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// process the received protocol message.
|
|
|
|
func (s *Server) processProto(proto protoTuple) error {
|
|
|
|
var (
|
|
|
|
peer = proto.peer
|
|
|
|
msg = proto.msg
|
|
|
|
)
|
|
|
|
|
|
|
|
// Make sure both server and peer are operating on
|
|
|
|
// the same network.
|
|
|
|
if msg.Magic != s.Net {
|
|
|
|
return errInvalidNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msg.CommandType() {
|
|
|
|
case CMDVersion:
|
|
|
|
version := msg.Payload.(*payload.Version)
|
|
|
|
return s.handleVersionCmd(peer, version)
|
|
|
|
case CMDHeaders:
|
|
|
|
headers := msg.Payload.(*payload.Headers)
|
|
|
|
go s.handleHeadersCmd(peer, headers)
|
|
|
|
case CMDInv:
|
|
|
|
inventory := msg.Payload.(*payload.Inventory)
|
|
|
|
return s.handleInvCmd(peer, inventory)
|
|
|
|
case CMDBlock:
|
|
|
|
block := msg.Payload.(*core.Block)
|
|
|
|
return s.handleBlockCmd(peer, block)
|
|
|
|
case CMDGetHeaders:
|
|
|
|
getHeaders := msg.Payload.(*payload.GetBlocks)
|
|
|
|
s.handleGetHeadersCmd(peer, getHeaders)
|
|
|
|
case CMDVerack:
|
2018-04-09 16:58:09 +00:00
|
|
|
// Make sure this peer has send his version before we start the
|
|
|
|
// protocol with that peer.
|
2018-03-14 09:36:59 +00:00
|
|
|
if peer.Version() == nil {
|
|
|
|
return errInvalidHandshake
|
|
|
|
}
|
|
|
|
go s.startProtocol(peer)
|
|
|
|
case CMDAddr:
|
|
|
|
addressList := msg.Payload.(*payload.AddressList)
|
|
|
|
for _, addr := range addressList.Addrs {
|
|
|
|
s.discovery.BackFill(addr.Endpoint.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|