2018-01-26 18:04:13 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2018-01-31 19:11:08 +00:00
|
|
|
"bytes"
|
2018-02-02 10:02:25 +00:00
|
|
|
"errors"
|
2018-02-01 07:19:12 +00:00
|
|
|
"fmt"
|
2018-01-26 18:04:13 +00:00
|
|
|
"net"
|
2018-01-31 19:11:08 +00:00
|
|
|
|
2018-02-07 14:16:50 +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
|
|
|
)
|
|
|
|
|
2018-02-01 07:19:12 +00:00
|
|
|
func listenTCP(s *Server, port int) error {
|
|
|
|
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
2018-01-26 18:04:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-31 13:32:57 +00:00
|
|
|
s.listener = ln
|
|
|
|
|
2018-01-26 18:04:13 +00:00
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-01 13:53:49 +00:00
|
|
|
|
2018-01-29 18:17:49 +00:00
|
|
|
go handleConnection(s, conn)
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 13:59:32 +00:00
|
|
|
func connectToRemoteNode(s *Server, address string) {
|
|
|
|
conn, err := net.Dial("tcp", address)
|
|
|
|
if err != nil {
|
2018-01-28 17:42:22 +00:00
|
|
|
s.logger.Printf("failed to connect to remote node %s", address)
|
2018-01-28 13:59:32 +00:00
|
|
|
if conn != nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-01-29 18:17:49 +00:00
|
|
|
go handleConnection(s, conn)
|
2018-01-28 13:59:32 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 18:04:13 +00:00
|
|
|
func connectToSeeds(s *Server, addrs []string) {
|
|
|
|
for _, addr := range addrs {
|
2018-01-28 13:59:32 +00:00
|
|
|
go connectToRemoteNode(s, addr)
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 18:17:49 +00:00
|
|
|
func handleConnection(s *Server, conn net.Conn) {
|
2018-01-31 19:11:08 +00:00
|
|
|
peer := NewTCPPeer(conn, s)
|
2018-01-26 18:04:13 +00:00
|
|
|
s.register <- peer
|
|
|
|
|
|
|
|
// remove the peer from connected peers and cleanup the connection.
|
|
|
|
defer func() {
|
2018-02-02 10:02:25 +00:00
|
|
|
peer.disconnect()
|
2018-01-26 18:04:13 +00:00
|
|
|
}()
|
|
|
|
|
2018-02-01 13:53:49 +00:00
|
|
|
// Start a goroutine that will handle all outgoing messages.
|
2018-01-26 18:04:13 +00:00
|
|
|
go peer.writeLoop()
|
2018-02-01 13:53:49 +00:00
|
|
|
// Start a goroutine that will handle all incomming messages.
|
|
|
|
go handleMessage(s, peer)
|
2018-01-26 18:04:13 +00:00
|
|
|
|
2018-01-31 19:11:08 +00:00
|
|
|
// Read from the connection and decode it into a Message ready for processing.
|
2018-01-26 18:04:13 +00:00
|
|
|
for {
|
2018-01-28 10:20:42 +00:00
|
|
|
msg := &Message{}
|
2018-02-04 19:54:51 +00:00
|
|
|
if err := msg.decode(conn); err != nil {
|
|
|
|
s.logger.Printf("decode error: %s", err)
|
|
|
|
break
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
2018-02-01 13:53:49 +00:00
|
|
|
|
|
|
|
peer.receive <- msg
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 06:43:32 +00:00
|
|
|
// handleMessage multiplexes the message received from a TCP connection to a server command.
|
2018-02-01 13:53:49 +00:00
|
|
|
func handleMessage(s *Server, p *TCPPeer) {
|
2018-02-02 10:02:25 +00:00
|
|
|
var err error
|
|
|
|
|
2018-02-01 13:53:49 +00:00
|
|
|
for {
|
|
|
|
msg := <-p.receive
|
|
|
|
command := msg.commandType()
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// s.logger.Printf("IN :: %d :: %s :: %v", p.id(), command, msg)
|
2018-02-01 13:53:49 +00:00
|
|
|
|
|
|
|
switch command {
|
|
|
|
case cmdVersion:
|
2018-02-07 14:16:50 +00:00
|
|
|
version := msg.Payload.(*payload.Version)
|
|
|
|
if err = s.handleVersionCmd(version, p); err != nil {
|
2018-02-04 19:54:51 +00:00
|
|
|
break
|
2018-02-02 10:02:25 +00:00
|
|
|
}
|
2018-02-06 06:43:32 +00:00
|
|
|
p.nonce = version.Nonce
|
|
|
|
p.pVersion = version
|
2018-02-02 10:02:25 +00:00
|
|
|
|
|
|
|
// When a node receives a connection request, it declares its version immediately.
|
|
|
|
// There will be no other communication until both sides are getting versions of each other.
|
|
|
|
// When a node receives the version message, it replies to a verack as a response immediately.
|
|
|
|
// NOTE: The current official NEO nodes dont mimic this behaviour. There is small chance that the
|
|
|
|
// official nodes will not respond directly with a verack after we sended our version.
|
|
|
|
// is this a bug? - anthdm 02/02/2018
|
|
|
|
msgVerack := <-p.receive
|
|
|
|
if msgVerack.commandType() != cmdVerack {
|
2018-02-04 19:54:51 +00:00
|
|
|
err = errors.New("expected verack after sended out version")
|
|
|
|
break
|
2018-02-02 10:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// start the protocol
|
2018-02-04 19:54:51 +00:00
|
|
|
go s.startProtocol(p)
|
2018-02-01 13:53:49 +00:00
|
|
|
case cmdAddr:
|
2018-02-07 14:16:50 +00:00
|
|
|
addrList := msg.Payload.(*payload.AddressList)
|
|
|
|
err = s.handleAddrCmd(addrList, p)
|
2018-02-01 13:53:49 +00:00
|
|
|
case cmdGetAddr:
|
2018-02-02 10:02:25 +00:00
|
|
|
err = s.handleGetaddrCmd(msg, p)
|
2018-02-01 13:53:49 +00:00
|
|
|
case cmdInv:
|
2018-02-07 14:16:50 +00:00
|
|
|
inv := msg.Payload.(*payload.Inventory)
|
|
|
|
err = s.handleInvCmd(inv, p)
|
2018-02-01 13:53:49 +00:00
|
|
|
case cmdBlock:
|
2018-02-07 14:16:50 +00:00
|
|
|
block := msg.Payload.(*core.Block)
|
|
|
|
err = s.handleBlockCmd(block, p)
|
2018-02-01 13:53:49 +00:00
|
|
|
case cmdConsensus:
|
|
|
|
case cmdTX:
|
|
|
|
case cmdVerack:
|
2018-02-02 10:02:25 +00:00
|
|
|
// If we receive a verack here we disconnect. We already handled the verack
|
|
|
|
// when we sended our version.
|
2018-02-07 14:16:50 +00:00
|
|
|
err = errors.New("verack already received")
|
2018-02-01 13:53:49 +00:00
|
|
|
case cmdGetHeaders:
|
|
|
|
case cmdGetBlocks:
|
|
|
|
case cmdGetData:
|
|
|
|
case cmdHeaders:
|
2018-02-06 06:43:32 +00:00
|
|
|
headers := msg.Payload.(*payload.Headers)
|
|
|
|
err = s.handleHeadersCmd(headers, p)
|
2018-02-04 19:54:51 +00:00
|
|
|
default:
|
|
|
|
// This command is unknown by the server.
|
|
|
|
err = fmt.Errorf("unknown command received %v", msg.Command)
|
|
|
|
break
|
2018-02-01 13:53:49 +00:00
|
|
|
}
|
2018-02-02 10:02:25 +00:00
|
|
|
|
|
|
|
// catch all errors here and disconnect.
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Printf("processing message failed: %s", err)
|
2018-02-04 19:54:51 +00:00
|
|
|
break
|
2018-02-02 10:02:25 +00:00
|
|
|
}
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
|
|
|
|
// Disconnect the peer when breaked out of the loop.
|
|
|
|
p.disconnect()
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
2018-01-31 21:14:13 +00:00
|
|
|
|
2018-02-02 10:02:25 +00:00
|
|
|
type sendTuple struct {
|
|
|
|
msg *Message
|
|
|
|
err chan error
|
|
|
|
}
|
|
|
|
|
2018-01-31 21:14:13 +00:00
|
|
|
// TCPPeer represents a remote node, backed by TCP transport.
|
|
|
|
type TCPPeer struct {
|
|
|
|
s *Server
|
|
|
|
// nonce (id) of the peer.
|
|
|
|
nonce uint32
|
|
|
|
// underlying TCP connection
|
|
|
|
conn net.Conn
|
|
|
|
// host and port information about this peer.
|
|
|
|
endpoint util.Endpoint
|
|
|
|
// channel to coordinate messages writen back to the connection.
|
2018-02-02 10:02:25 +00:00
|
|
|
send chan sendTuple
|
2018-02-01 13:53:49 +00:00
|
|
|
// channel to receive from underlying connection.
|
|
|
|
receive chan *Message
|
2018-02-06 06:43:32 +00:00
|
|
|
// the version sended out by the peer when connected.
|
|
|
|
pVersion *payload.Version
|
2018-01-31 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTCPPeer returns a pointer to a TCP Peer.
|
|
|
|
func NewTCPPeer(conn net.Conn, s *Server) *TCPPeer {
|
|
|
|
e, _ := util.EndpointFromString(conn.RemoteAddr().String())
|
|
|
|
|
|
|
|
return &TCPPeer{
|
|
|
|
conn: conn,
|
2018-02-02 10:02:25 +00:00
|
|
|
send: make(chan sendTuple),
|
2018-02-01 13:53:49 +00:00
|
|
|
receive: make(chan *Message),
|
2018-01-31 21:14:13 +00:00
|
|
|
endpoint: e,
|
|
|
|
s: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 10:02:25 +00:00
|
|
|
func (p *TCPPeer) callVersion(msg *Message) error {
|
|
|
|
t := sendTuple{
|
|
|
|
msg: msg,
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
p.send <- t
|
|
|
|
|
|
|
|
return <-t.err
|
2018-01-31 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 06:43:32 +00:00
|
|
|
func (p *TCPPeer) version() *payload.Version {
|
|
|
|
return p.pVersion
|
|
|
|
}
|
|
|
|
|
2018-01-31 21:14:13 +00:00
|
|
|
// id implements the peer interface
|
|
|
|
func (p *TCPPeer) id() uint32 {
|
|
|
|
return p.nonce
|
|
|
|
}
|
|
|
|
|
|
|
|
// endpoint implements the peer interface
|
|
|
|
func (p *TCPPeer) addr() util.Endpoint {
|
|
|
|
return p.endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// callGetaddr will send the "getaddr" command to the remote.
|
2018-02-02 10:02:25 +00:00
|
|
|
func (p *TCPPeer) callGetaddr(msg *Message) error {
|
|
|
|
t := sendTuple{
|
|
|
|
msg: msg,
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
p.send <- t
|
|
|
|
|
|
|
|
return <-t.err
|
2018-01-31 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// callGetblocks will send the "getblocks" command to the remote.
|
|
|
|
func (p *TCPPeer) callGetblocks(msg *Message) error {
|
|
|
|
t := sendTuple{
|
|
|
|
msg: msg,
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
p.send <- t
|
|
|
|
|
|
|
|
return <-t.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// callGetheaders will send the "getheaders" command to the remote.
|
|
|
|
func (p *TCPPeer) callGetheaders(msg *Message) error {
|
|
|
|
t := sendTuple{
|
|
|
|
msg: msg,
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
p.send <- t
|
|
|
|
|
|
|
|
return <-t.err
|
|
|
|
}
|
|
|
|
|
2018-02-02 10:02:25 +00:00
|
|
|
func (p *TCPPeer) callVerack(msg *Message) error {
|
|
|
|
t := sendTuple{
|
|
|
|
msg: msg,
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
p.send <- t
|
|
|
|
|
|
|
|
return <-t.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TCPPeer) callGetdata(msg *Message) error {
|
|
|
|
t := sendTuple{
|
|
|
|
msg: msg,
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
p.send <- t
|
|
|
|
|
|
|
|
return <-t.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// disconnect disconnects the peer, cleaning up all its resources.
|
|
|
|
// 3 goroutines needs to be cleanup (writeLoop, handleConnection and handleMessage)
|
2018-01-31 21:14:13 +00:00
|
|
|
func (p *TCPPeer) disconnect() {
|
2018-02-02 10:02:25 +00:00
|
|
|
select {
|
|
|
|
case <-p.send:
|
|
|
|
case <-p.receive:
|
|
|
|
default:
|
|
|
|
close(p.send)
|
|
|
|
close(p.receive)
|
|
|
|
p.s.unregister <- p
|
|
|
|
p.conn.Close()
|
|
|
|
}
|
2018-01-31 21:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// writeLoop writes messages to the underlying TCP connection.
|
|
|
|
// A goroutine writeLoop is started for each connection.
|
|
|
|
// There should be at most one writer to a connection executing
|
|
|
|
// all writes from this goroutine.
|
|
|
|
func (p *TCPPeer) writeLoop() {
|
|
|
|
// clean up the connection.
|
|
|
|
defer func() {
|
2018-02-02 10:02:25 +00:00
|
|
|
p.disconnect()
|
2018-01-31 21:14:13 +00:00
|
|
|
}()
|
|
|
|
|
2018-02-07 14:16:50 +00:00
|
|
|
// resuse this buffer
|
2018-02-04 19:54:51 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2018-01-31 21:14:13 +00:00
|
|
|
for {
|
2018-02-02 10:02:25 +00:00
|
|
|
t := <-p.send
|
|
|
|
if t.msg == nil {
|
2018-02-04 19:54:51 +00:00
|
|
|
break // send probably closed.
|
2018-02-02 10:02:25 +00:00
|
|
|
}
|
2018-01-31 21:14:13 +00:00
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// p.s.logger.Printf("OUT :: %s :: %+v", t.msg.commandType(), t.msg.Payload)
|
|
|
|
|
|
|
|
if err := t.msg.encode(buf); err != nil {
|
|
|
|
t.err <- err
|
|
|
|
}
|
|
|
|
_, err := p.conn.Write(buf.Bytes())
|
|
|
|
t.err <- err
|
2018-01-31 21:14:13 +00:00
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
buf.Reset()
|
2018-01-31 21:14:13 +00:00
|
|
|
}
|
|
|
|
}
|