p2p

package
v0.8.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2017 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

func Dial(addr string) net.Conn

Dial connects to a endpoint

func GetLocalIP

func GetLocalIP() string

GetLocalIP returns the non loopback local IP of the host

func SendMessage

func SendMessage(w io.Writer, msg *Msg) (int, error)

SendMessage sends message to other node

Types

type Config

type Config struct {
	Address             string
	PrivateKey          *crypto.PrivateKey
	BootstrapNodes      []string
	MaxPeers            int
	ReconnectTimes      int
	ConnectTimeInterval int
	KeepAliveInterval   int
	KeepAliveTimes      int
	MinPeers            int
	Protocols           []Protocol
	RouteAddress        []string
}

Config is the p2p network configuration

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig defines the default network configuration

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

Connection represents a tcp client

type EncHandshake

type EncHandshake struct {
	ID        []byte
	Signature *crypto.Signature
	Hash      *crypto.Hash
}

EncHandshake is encryption handshake. implement the interface of Protocol

func GetEncHandshake

func GetEncHandshake() *EncHandshake

GetEncHandshake returns enchandshake message

type Msg

type Msg struct {
	// Magic 	uint32
	Cmd      uint8
	Payload  []byte
	CheckSum [4]byte
}

Msg on network

func NewMsg

func NewMsg(msgType uint8, payload []byte) *Msg

NewMsg New Message used by msgType chainId and payload

func (*Msg) Deserialize

func (m *Msg) Deserialize(data []byte)

Deserialize deserialize bytes to message

func (*Msg) Serialize

func (m *Msg) Serialize() []byte

Serialize serializes message to bytes

func (*Msg) String

func (m *Msg) String() string

String returns the string format of the msg

type MsgReadWriter

type MsgReadWriter interface {
	MsgReader
	MsgWriter
}

MsgReadWriter is the interface that groups the p2p message Read and Write methods.

type MsgReader

type MsgReader interface {
	ReadMsg() (Msg, error)
}

MsgReader is the interface p2p message Read methods.

type MsgWriter

type MsgWriter interface {
	WriteMsg(msg Msg) (int, error)
}

MsgWriter is the interface that the p2p message Write methods.

type Peer

type Peer struct {
	ID             PeerID
	LastActiveTime time.Time
	Address        string
	Conn           net.Conn
	// contains filtered or unexported fields
}

Peer represents a peer in blockchain

func NewPeer

func NewPeer(id []byte, conn net.Conn, addr string, protocols []Protocol) *Peer

NewPeer returns a new Peer with input id

func ParsePeer

func ParsePeer(rawurl string) (*Peer, error)

ParsePeer parses a peer designator.

func (*Peer) GetPeerAddress

func (peer *Peer) GetPeerAddress() string

GetPeerAddress returns local peer address info

func (*Peer) String

func (peer *Peer) String() string

String is the representation of a peer as a URL.

type PeerID

type PeerID []byte

PeerID represents the peer identity

func (PeerID) String

func (p PeerID) String() string

type ProtoHandshake

type ProtoHandshake struct {
	Name       string
	Version    string
	ID         []byte
	SrvAddress string
}

ProtoHandshake is protocol handshake. implement the interface of Protocol

func GetProtoHandshake

func GetProtoHandshake() *ProtoHandshake

GetProtoHandshake returns protocol handshake

type Protocol

type Protocol struct {
	BaseCmd uint8
	Name    string
	Version string
	Run     func(p *Peer, rw MsgReadWriter) error
}

Protocol raw structure

type Server

type Server struct {
	Config
	// contains filtered or unexported fields
}

Server represents a p2p network server

func NewServer

func NewServer(db *db.BlockchainDB, cfg *Config) *Server

NewServer returns a new p2p server

func (*Server) Broadcast

func (srv *Server) Broadcast(msg *Msg)

Broadcast broadcasts message to remote peers

func (*Server) GetLocalPeer

func (srv *Server) GetLocalPeer() *Peer

GetLocalPeer returns local peer info

func (Server) GetPeer

func (pm Server) GetPeer(conn net.Conn) *Peer

GetPeer returns a peer according the conn

func (*Server) GetPeers

func (srv *Server) GetPeers() []*Peer

GetPeers returns all peers info

func (*Server) Sign

func (srv *Server) Sign(data []byte) (*crypto.Signature, error)

Sign signs data with node key

func (*Server) Start

func (srv *Server) Start()

Start starts a p2p network run as goroutine

type TCPServer

type TCPServer struct {
	// contains filtered or unexported fields
}

TCPServer represents a tcp server

func (*TCPServer) OnClientClose

func (srv *TCPServer) OnClientClose(callback func(c *Connection))

OnClientClose called when client closed

func (*TCPServer) OnNewClient

func (srv *TCPServer) OnNewClient(callback func(c *Connection))

OnNewClient called when new client connect

func (*TCPServer) OnNewMessage

func (srv *TCPServer) OnNewMessage(callback func(*Connection, *Msg))

OnNewMessage called when received a new message from client

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL