2018-01-26 18:04:13 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2018-02-04 19:54:51 +00:00
|
|
|
"context"
|
2018-02-02 10:02:25 +00:00
|
|
|
"errors"
|
2018-01-26 18:04:13 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2018-02-01 20:28:45 +00:00
|
|
|
"sync"
|
2018-01-28 10:12:05 +00:00
|
|
|
"time"
|
2018-01-27 15:00:28 +00:00
|
|
|
|
2018-02-01 20:28:45 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
2018-02-01 18:54:23 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/network/payload"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2018-01-26 18:04:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-01-26 20:39:34 +00:00
|
|
|
// node version
|
|
|
|
version = "2.6.0"
|
|
|
|
// official ports according to the protocol.
|
2018-01-26 18:04:13 +00:00
|
|
|
portMainNet = 10333
|
|
|
|
portTestNet = 20333
|
2018-01-31 19:11:08 +00:00
|
|
|
maxPeers = 50
|
2018-01-26 18:04:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type messageTuple struct {
|
2018-01-31 08:27:08 +00:00
|
|
|
peer Peer
|
2018-01-26 18:04:13 +00:00
|
|
|
msg *Message
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server is the representation of a full working NEO TCP node.
|
|
|
|
type Server struct {
|
|
|
|
logger *log.Logger
|
2018-01-28 07:03:18 +00:00
|
|
|
// id of the server
|
|
|
|
id uint32
|
2018-01-26 20:39:34 +00:00
|
|
|
// the port the TCP listener is listening on.
|
|
|
|
port uint16
|
2018-01-26 18:04:13 +00:00
|
|
|
// userAgent of the server.
|
|
|
|
userAgent string
|
|
|
|
// The "magic" mode the server is currently running on.
|
|
|
|
// This can either be 0x00746e41 or 0x74746e41 for main or test net.
|
2018-01-26 20:39:34 +00:00
|
|
|
// Or 56753 to work with the docker privnet.
|
|
|
|
net NetMode
|
2018-01-26 18:04:13 +00:00
|
|
|
// map that holds all connected peers to this server.
|
2018-01-31 08:27:08 +00:00
|
|
|
peers map[Peer]bool
|
2018-01-31 19:11:08 +00:00
|
|
|
// channel for handling new registerd peers.
|
|
|
|
register chan Peer
|
|
|
|
// channel for safely removing and disconnecting peers.
|
2018-01-31 08:27:08 +00:00
|
|
|
unregister chan Peer
|
2018-01-26 18:04:13 +00:00
|
|
|
// channel for coordinating messages.
|
|
|
|
message chan messageTuple
|
|
|
|
// channel used to gracefull shutdown the server.
|
|
|
|
quit chan struct{}
|
|
|
|
// Whether this server will receive and forward messages.
|
|
|
|
relay bool
|
|
|
|
// TCP listener of the server
|
|
|
|
listener net.Listener
|
2018-02-01 08:00:42 +00:00
|
|
|
// channel for safely responding the number of current connected peers.
|
|
|
|
peerCountCh chan peerCount
|
2018-02-01 20:28:45 +00:00
|
|
|
// a list of hashes that
|
|
|
|
knownHashes protectedHashmap
|
|
|
|
// The blockchain.
|
|
|
|
bc *core.Blockchain
|
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// TODO: Maybe util is a better place for such data types.
|
2018-02-01 20:28:45 +00:00
|
|
|
type protectedHashmap struct {
|
|
|
|
*sync.RWMutex
|
|
|
|
hashes map[util.Uint256]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m protectedHashmap) add(h util.Uint256) bool {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
if _, ok := m.hashes[h]; !ok {
|
|
|
|
m.hashes[h] = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m protectedHashmap) remove(h util.Uint256) bool {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
if _, ok := m.hashes[h]; ok {
|
|
|
|
delete(m.hashes, h)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m protectedHashmap) has(h util.Uint256) bool {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
_, ok := m.hashes[h]
|
|
|
|
|
|
|
|
return ok
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns a pointer to a new server.
|
2018-01-26 20:39:34 +00:00
|
|
|
func NewServer(net NetMode) *Server {
|
2018-01-31 19:11:08 +00:00
|
|
|
logger := log.New(os.Stdout, "[NEO SERVER] :: ", 0)
|
2018-01-26 18:04:13 +00:00
|
|
|
|
2018-01-26 20:39:34 +00:00
|
|
|
if net != ModeTestNet && net != ModeMainNet && net != ModeDevNet {
|
|
|
|
logger.Fatalf("invalid network mode %d", net)
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 06:43:32 +00:00
|
|
|
// For now I will hard code a genesis block of the docker privnet container.
|
|
|
|
startHash, _ := util.Uint256DecodeFromString("996e37358dc369912041f966f8c5d8d3a8255ba5dcbd3447f8a82b55db869099")
|
|
|
|
|
2018-01-26 18:04:13 +00:00
|
|
|
s := &Server{
|
2018-02-01 08:00:42 +00:00
|
|
|
id: util.RandUint32(1111111, 9999999),
|
|
|
|
userAgent: fmt.Sprintf("/NEO:%s/", version),
|
|
|
|
logger: logger,
|
|
|
|
peers: make(map[Peer]bool),
|
|
|
|
register: make(chan Peer),
|
|
|
|
unregister: make(chan Peer),
|
|
|
|
message: make(chan messageTuple),
|
|
|
|
relay: true, // currently relay is not handled.
|
|
|
|
net: net,
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
peerCountCh: make(chan peerCount),
|
2018-02-06 06:43:32 +00:00
|
|
|
bc: core.NewBlockchain(core.NewMemoryStore(), logger, startHash),
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start run's the server.
|
2018-02-01 08:00:42 +00:00
|
|
|
// TODO: server should be initialized with a config.
|
|
|
|
func (s *Server) Start(opts StartOpts) {
|
|
|
|
s.port = uint16(opts.TCP)
|
2018-01-26 20:39:34 +00:00
|
|
|
|
2018-01-26 18:04:13 +00:00
|
|
|
fmt.Println(logo())
|
2018-01-28 07:03:18 +00:00
|
|
|
fmt.Println(string(s.userAgent))
|
|
|
|
fmt.Println("")
|
|
|
|
s.logger.Printf("NET: %s - TCP: %d - RELAY: %v - ID: %d",
|
|
|
|
s.net, int(s.port), s.relay, s.id)
|
2018-01-26 18:04:13 +00:00
|
|
|
|
2018-02-01 08:00:42 +00:00
|
|
|
go listenTCP(s, opts.TCP)
|
2018-01-26 18:04:13 +00:00
|
|
|
|
2018-02-01 08:00:42 +00:00
|
|
|
if opts.RPC > 0 {
|
|
|
|
go listenHTTP(s, opts.RPC)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(opts.Seeds) > 0 {
|
|
|
|
connectToSeeds(s, opts.Seeds)
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.loop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the server, attemping a gracefull shutdown.
|
|
|
|
func (s *Server) Stop() { s.quit <- struct{}{} }
|
|
|
|
|
|
|
|
// shutdown the server, disconnecting all peers.
|
|
|
|
func (s *Server) shutdown() {
|
|
|
|
s.logger.Println("attemping a quitefull shutdown.")
|
|
|
|
s.listener.Close()
|
|
|
|
|
|
|
|
// disconnect and remove all connected peers.
|
|
|
|
for peer := range s.peers {
|
2018-02-02 10:02:25 +00:00
|
|
|
peer.disconnect()
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) loop() {
|
|
|
|
for {
|
|
|
|
select {
|
2018-01-31 19:11:08 +00:00
|
|
|
// When a new connection is been established, (by this server or remote node)
|
|
|
|
// its peer will be received on this channel.
|
|
|
|
// Any peer registration must happen via this channel.
|
2018-01-26 18:04:13 +00:00
|
|
|
case peer := <-s.register:
|
2018-01-31 19:11:08 +00:00
|
|
|
if len(s.peers) < maxPeers {
|
|
|
|
s.logger.Printf("peer registered from address %s", peer.addr())
|
|
|
|
s.peers[peer] = true
|
2018-02-02 10:02:25 +00:00
|
|
|
|
|
|
|
if err := s.handlePeerConnected(peer); err != nil {
|
|
|
|
s.logger.Printf("failed handling peer connection: %s", err)
|
|
|
|
peer.disconnect()
|
|
|
|
}
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
2018-01-27 12:39:07 +00:00
|
|
|
|
2018-02-02 10:02:25 +00:00
|
|
|
// unregister safely deletes a peer. For disconnecting peers use the
|
|
|
|
// disconnect() method on the peer, it will call unregister and terminates its routines.
|
2018-01-26 18:04:13 +00:00
|
|
|
case peer := <-s.unregister:
|
2018-01-27 07:37:07 +00:00
|
|
|
if _, ok := s.peers[peer]; ok {
|
|
|
|
delete(s.peers, peer)
|
2018-01-31 19:11:08 +00:00
|
|
|
s.logger.Printf("peer %s disconnected", peer.addr())
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:00:42 +00:00
|
|
|
case t := <-s.peerCountCh:
|
|
|
|
t.count <- len(s.peers)
|
2018-01-31 19:11:08 +00:00
|
|
|
|
2018-01-26 18:04:13 +00:00
|
|
|
case <-s.quit:
|
|
|
|
s.shutdown()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 18:17:49 +00:00
|
|
|
// When a new peer is connected we send our version.
|
|
|
|
// No further communication should be made before both sides has received
|
|
|
|
// the versions of eachother.
|
2018-02-02 10:02:25 +00:00
|
|
|
func (s *Server) handlePeerConnected(p Peer) error {
|
2018-01-31 19:11:08 +00:00
|
|
|
// TODO: get the blockheight of this server once core implemented this.
|
2018-02-06 06:43:32 +00:00
|
|
|
payload := payload.NewVersion(s.id, s.port, s.userAgent, s.bc.HeaderHeight(), s.relay)
|
2018-01-27 15:00:28 +00:00
|
|
|
msg := newMessage(s.net, cmdVersion, payload)
|
2018-02-02 10:02:25 +00:00
|
|
|
return p.callVersion(msg)
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 10:02:25 +00:00
|
|
|
func (s *Server) handleVersionCmd(msg *Message, p Peer) error {
|
2018-02-01 08:00:42 +00:00
|
|
|
version := msg.Payload.(*payload.Version)
|
|
|
|
if s.id == version.Nonce {
|
2018-02-02 10:02:25 +00:00
|
|
|
return errors.New("identical nonce")
|
2018-01-29 18:17:49 +00:00
|
|
|
}
|
2018-02-01 08:00:42 +00:00
|
|
|
if p.addr().Port != version.Port {
|
2018-02-04 19:54:51 +00:00
|
|
|
return fmt.Errorf("port mismatch: %d and %d", version.Port, p.addr().Port)
|
2018-01-28 10:20:42 +00:00
|
|
|
}
|
2018-02-02 10:02:25 +00:00
|
|
|
|
|
|
|
return p.callVerack(newMessage(s.net, cmdVerack, nil))
|
2018-01-30 10:56:36 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 10:02:25 +00:00
|
|
|
func (s *Server) handleGetaddrCmd(msg *Message, p Peer) error {
|
2018-02-01 08:00:42 +00:00
|
|
|
return nil
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
2018-01-30 10:56:36 +00:00
|
|
|
|
2018-02-01 20:28:45 +00:00
|
|
|
// The node can broadcast the object information it owns by this message.
|
|
|
|
// The message can be sent automatically or can be used to answer getbloks messages.
|
2018-02-02 10:02:25 +00:00
|
|
|
func (s *Server) handleInvCmd(msg *Message, p Peer) error {
|
2018-02-01 08:00:42 +00:00
|
|
|
inv := msg.Payload.(*payload.Inventory)
|
|
|
|
if !inv.Type.Valid() {
|
2018-02-02 10:02:25 +00:00
|
|
|
return fmt.Errorf("invalid inventory type %s", inv.Type)
|
2018-02-01 08:00:42 +00:00
|
|
|
}
|
|
|
|
if len(inv.Hashes) == 0 {
|
2018-02-02 10:02:25 +00:00
|
|
|
return errors.New("inventory should have at least 1 hash got 0")
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
2018-01-30 10:56:36 +00:00
|
|
|
|
2018-02-01 20:28:45 +00:00
|
|
|
// todo: only grab the hashes that we dont know.
|
|
|
|
|
2018-02-01 08:00:42 +00:00
|
|
|
payload := payload.NewInventory(inv.Type, inv.Hashes)
|
|
|
|
resp := newMessage(s.net, cmdGetData, payload)
|
2018-02-02 10:02:25 +00:00
|
|
|
|
|
|
|
return p.callGetdata(resp)
|
2018-01-27 12:39:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 20:28:45 +00:00
|
|
|
// handleBlockCmd processes the received block.
|
2018-02-02 10:02:25 +00:00
|
|
|
func (s *Server) handleBlockCmd(msg *Message, p Peer) error {
|
2018-02-01 20:28:45 +00:00
|
|
|
block := msg.Payload.(*core.Block)
|
|
|
|
hash, err := block.Hash()
|
|
|
|
if err != nil {
|
2018-02-02 10:02:25 +00:00
|
|
|
return err
|
2018-02-01 20:28:45 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
s.logger.Printf("new block: index %d hash %s", block.Index, hash)
|
2018-02-01 20:28:45 +00:00
|
|
|
|
2018-02-06 06:43:32 +00:00
|
|
|
return nil
|
2018-02-01 20:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// After receiving the getaddr message, the node returns an addr message as response
|
|
|
|
// and provides information about the known nodes on the network.
|
2018-02-02 10:02:25 +00:00
|
|
|
func (s *Server) handleAddrCmd(msg *Message, p Peer) error {
|
2018-02-01 08:00:42 +00:00
|
|
|
addrList := msg.Payload.(*payload.AddressList)
|
|
|
|
for _, addr := range addrList.Addrs {
|
|
|
|
if !s.peerAlreadyConnected(addr.Addr) {
|
|
|
|
// TODO: this is not transport abstracted.
|
|
|
|
go connectToRemoteNode(s, addr.Addr.String())
|
|
|
|
}
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
2018-02-02 10:02:25 +00:00
|
|
|
return nil
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 06:43:32 +00:00
|
|
|
// Handle the headers received from the remote after we asked for headers with the
|
|
|
|
// "getheaders" message.
|
|
|
|
func (s *Server) handleHeadersCmd(headers *payload.Headers, p Peer) error {
|
2018-02-04 19:54:51 +00:00
|
|
|
// Set a deadline for adding headers?
|
|
|
|
go func(ctx context.Context, headers []*core.Header) {
|
2018-02-06 06:43:32 +00:00
|
|
|
if err := s.bc.AddHeaders(headers...); err != nil {
|
|
|
|
s.logger.Printf("failed to add headers: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask more headers if we are not in sync with the peer.
|
|
|
|
if s.bc.HeaderHeight() < p.version().StartHeight {
|
|
|
|
s.logger.Printf("header height %d peer height %d", s.bc.HeaderHeight(), p.version().StartHeight)
|
|
|
|
if err := s.askMoreHeaders(p); err != nil {
|
|
|
|
s.logger.Printf("getheaders RPC failed: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
}(context.TODO(), headers.Hdrs)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the peer for more headers We use the current block hash as start.
|
|
|
|
func (s *Server) askMoreHeaders(p Peer) error {
|
|
|
|
start := []util.Uint256{s.bc.CurrentBlockHash()}
|
|
|
|
payload := payload.NewGetBlocks(start, util.Uint256{})
|
|
|
|
msg := newMessage(s.net, cmdGetHeaders, payload)
|
|
|
|
|
|
|
|
return p.callGetheaders(msg)
|
2018-02-01 20:28:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 19:11:08 +00:00
|
|
|
// check if the addr is already connected to the server.
|
|
|
|
func (s *Server) peerAlreadyConnected(addr net.Addr) bool {
|
2018-02-04 19:54:51 +00:00
|
|
|
// TODO: Dont try to connect with ourselfs.
|
2018-01-28 13:59:32 +00:00
|
|
|
for peer := range s.peers {
|
2018-01-31 19:11:08 +00:00
|
|
|
if peer.addr().String() == addr.String() {
|
2018-01-28 13:59:32 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-06 06:43:32 +00:00
|
|
|
// TODO: Quit this routine if the peer is disconnected.
|
2018-02-04 19:54:51 +00:00
|
|
|
func (s *Server) startProtocol(p Peer) {
|
2018-02-06 06:43:32 +00:00
|
|
|
if s.bc.HeaderHeight() < p.version().StartHeight {
|
|
|
|
s.askMoreHeaders(p)
|
|
|
|
}
|
2018-01-28 10:12:05 +00:00
|
|
|
for {
|
|
|
|
getaddrMsg := newMessage(s.net, cmdGetAddr, nil)
|
2018-02-04 19:54:51 +00:00
|
|
|
p.callGetaddr(getaddrMsg)
|
2018-01-28 10:12:05 +00:00
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
time.Sleep(30 * time.Second)
|
2018-01-28 10:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 08:00:42 +00:00
|
|
|
type peerCount struct {
|
|
|
|
count chan int
|
|
|
|
}
|
|
|
|
|
|
|
|
// peerCount returns the number of connected peers to this server.
|
|
|
|
func (s *Server) peerCount() int {
|
|
|
|
ch := peerCount{
|
|
|
|
count: make(chan int),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.peerCountCh <- ch
|
|
|
|
|
|
|
|
return <-ch.count
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartOpts holds the server configuration.
|
|
|
|
type StartOpts struct {
|
|
|
|
// tcp port
|
|
|
|
TCP int
|
|
|
|
// slice of peer addresses the server will connect to
|
|
|
|
Seeds []string
|
|
|
|
// JSON-RPC port. If 0 no RPC handler will be attached.
|
|
|
|
RPC int
|
|
|
|
}
|
|
|
|
|
2018-01-26 18:04:13 +00:00
|
|
|
func logo() string {
|
|
|
|
return `
|
|
|
|
_ ____________ __________
|
|
|
|
/ | / / ____/ __ \ / ____/ __ \
|
|
|
|
/ |/ / __/ / / / /_____/ / __/ / / /
|
|
|
|
/ /| / /___/ /_/ /_____/ /_/ / /_/ /
|
|
|
|
/_/ |_/_____/\____/ \____/\____/
|
|
|
|
`
|
|
|
|
}
|