p2p

package
v0.0.0-...-6ee0886 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2021 License: LGPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidHandshake           = errors.New("invalid handshake")
	ErrInvalidLength              = errors.New("invalid length")
	ErrUnknownMessage             = errors.New("unknown message")
	ErrNotExistPeer               = errors.New("not exist peer")
	ErrSelfConnection             = errors.New("self connection")
	ErrInvalidUTXO                = errors.New("invalid UTXO")
	ErrTooManyTrasactionInMessage = errors.New("too many transaction in message")
)

errors

View Source
var (
	StatusMessageType          = types.DefineHashedType("p2p.StatusMessage")
	RequestMessageType         = types.DefineHashedType("p2p.RequestMessage")
	BlockMessageType           = types.DefineHashedType("p2p.BlockMessage")
	TransactionMessageType     = types.DefineHashedType("p2p.TransactionMessage")
	PeerListMessageType        = types.DefineHashedType("p2p.PeerListMessage")
	RequestPeerListMessageType = types.DefineHashedType("p2p.RequestPeerListMessage")
)

message types

Functions

func BlockPacketWithCache

func BlockPacketWithCache(msg *RequestMessage, provider types.Provider, batchCache gcache.Cache, singleCache gcache.Cache) ([]byte, error)

func FillBytes

func FillBytes(r io.Reader, bs []byte) (int64, error)

FillBytes reads bytes from the reader until the given bytes array is filled

func MessageToPacket

func MessageToPacket(m interface{}) []byte

MessageToPacket returns packet of the message

func PacketMessageType

func PacketMessageType(bs []byte) uint16

func PacketToMessage

func PacketToMessage(bs []byte) (interface{}, error)

func ReadBool

func ReadBool(r io.Reader) (bool, int64, error)

ReadBool reads a bool using a uint8 from the reader

func ReadBytes

func ReadBytes(r io.Reader) ([]byte, int64, error)

ReadBytes reads a byte array from the reader

func ReadString

func ReadString(r io.Reader) (string, int64, error)

ReadString reads a string array from the reader

func ReadUint16

func ReadUint16(r io.Reader) (uint16, int64, error)

ReadUint16 reads a uint16 number from the reader

func ReadUint32

func ReadUint32(r io.Reader) (uint32, int64, error)

ReadUint32 reads a uint32 number from the reader

func ReadUint64

func ReadUint64(r io.Reader) (uint64, int64, error)

ReadUint64 reads a uint64 number from the reader

func ReadUint8

func ReadUint8(r io.Reader) (uint8, int64, error)

ReadUint8 reads a uint8 number from the reader

Types

type BlockMessage

type BlockMessage struct {
	Blocks []*types.Block
}

BlockMessage used to send a chain block to a peer

type Handler

type Handler interface {
	OnConnected(p peer.Peer)
	OnDisconnected(p peer.Peer)
	OnRecv(p peer.Peer, bs []byte) error
}

Handler is a interface for connection events

type Node

type Node struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Node receives a block by the consensus

func NewNode

func NewNode(key key.Key, SeedNodeMap map[common.PublicHash]string, cn *chain.Chain, peerStorePath string) *Node

NewNode returns a Node

func (*Node) AddTx

func (nd *Node) AddTx(tx types.Transaction, sigs []common.Signature) error

AddTx adds tx to txpool that only have valid signatures

func (*Node) Close

func (nd *Node) Close()

Close terminates the node

func (*Node) GetTxFromTXPool

func (nd *Node) GetTxFromTXPool(TxHash hash.Hash256) *txpool.PoolItem

GetTxFromTXPool returned tx from txpool

func (*Node) Init

func (nd *Node) Init() error

Init initializes node

func (*Node) OnConnected

func (nd *Node) OnConnected(p peer.Peer)

OnConnected called when peer connected

func (*Node) OnDisconnected

func (nd *Node) OnDisconnected(p peer.Peer)

OnDisconnected called when peer disconnected

func (*Node) OnItemExpired

func (nd *Node) OnItemExpired(Interval time.Duration, Key string, Item interface{}, IsLast bool)

OnItemExpired is called when the item is expired

func (*Node) OnRecv

func (nd *Node) OnRecv(p peer.Peer, bs []byte) error

OnRecv called when message received

func (*Node) OnTimerExpired

func (nd *Node) OnTimerExpired(height uint32, value string)

OnTimerExpired called when rquest expired

func (*Node) PushTx

func (nd *Node) PushTx(tx types.Transaction, sigs []common.Signature) error

PushTx pushes transaction

func (*Node) Run

func (nd *Node) Run(BindAddress string)

Run starts the node

func (*Node) TxPoolList

func (nd *Node) TxPoolList() []*txpool.PoolItem

TxPoolList returned tx list from txpool

type NodeMesh

type NodeMesh struct {
	sync.Mutex
	BindAddress string
	// contains filtered or unexported fields
}

NodeMesh is a mesh for networking between nodes

func NewNodeMesh

func NewNodeMesh(ChainID uint8, key key.Key, SeedNodeMap map[common.PublicHash]string, handler Handler, peerStorePath string) *NodeMesh

NewNodeMesh returns a NodeMesh

func (*NodeMesh) AddBadPoint

func (ms *NodeMesh) AddBadPoint(ID string, Point int)

AddBadPoint adds bad points to to the peer

func (*NodeMesh) AddPeerList

func (ms *NodeMesh) AddPeerList(ips []string, hashs []string)

func (*NodeMesh) BroadcastPacket

func (ms *NodeMesh) BroadcastPacket(bs []byte)

BroadcastPacket sends a packet to all peers

func (*NodeMesh) ExceptCast

func (ms *NodeMesh) ExceptCast(ID string, bs []byte)

ExceptCast sends a message except the peer

func (*NodeMesh) GetPeer

func (ms *NodeMesh) GetPeer(ID string) peer.Peer

GetPeer returns the peer of the id

func (*NodeMesh) HasPeer

func (ms *NodeMesh) HasPeer() bool

func (*NodeMesh) Peers

func (ms *NodeMesh) Peers() []peer.Peer

Peers returns peers of the node mesh

func (*NodeMesh) RemovePeer

func (ms *NodeMesh) RemovePeer(ID string)

RemovePeer removes peers from the mesh

func (*NodeMesh) RequestConnect

func (ms *NodeMesh) RequestConnect(Address string, TargetPubHash common.PublicHash)

func (*NodeMesh) RequestPeerList

func (ms *NodeMesh) RequestPeerList(targetHash string)

func (*NodeMesh) Run

func (ms *NodeMesh) Run(BindAddress string)

Run starts the node mesh

func (*NodeMesh) SendPeerList

func (ms *NodeMesh) SendPeerList(targetHash string)

func (*NodeMesh) SendTo

func (ms *NodeMesh) SendTo(pubhash common.PublicHash, bs []byte)

SendTo sends a message to the node

type PeerListMessage

type PeerListMessage struct {
	Ips   []string
	Hashs []string
}

PeerListMessage is a message for a peer list

type RecvMessageItem

type RecvMessageItem struct {
	PeerID string
	Packet []byte
}

RecvMessageItem used to store recv message

type RequestExpireHandler

type RequestExpireHandler interface {
	OnTimerExpired(height uint32, value string)
}

RequestExpireHandler handles a request expire event

type RequestMessage

type RequestMessage struct {
	Height uint32
	Count  uint8
}

RequestMessage used to request a chain data to a peer

type RequestPeerListMessage

type RequestPeerListMessage struct {
}

RequestPeerListMessage is a request message for a peer list

type RequestTimer

type RequestTimer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

RequestTimer triggers a event when a request is expired

func NewRequestTimer

func NewRequestTimer(handler RequestExpireHandler) *RequestTimer

NewRequestTimer returns a RequestTimer

func (*RequestTimer) Add

func (rm *RequestTimer) Add(height uint32, t time.Duration, value string)

Add adds the timer of the request

func (*RequestTimer) Exist

func (rm *RequestTimer) Exist(height uint32) bool

Exist returns the target height request exists or not

func (*RequestTimer) RemovesByValue

func (rm *RequestTimer) RemovesByValue(value string)

RemovesByValue removes requests by the value

func (*RequestTimer) Run

func (rm *RequestTimer) Run()

Run is the main loop of RequestTimer

type SendMessageItem

type SendMessageItem struct {
	Target  common.PublicHash
	Address common.Address
	Except  bool
	Packet  []byte
}

SendMessageItem used to store send message

type Status

type Status struct {
	Height uint32
}

Status represents the status of the peer

type StatusMessage

type StatusMessage struct {
	Version  uint16
	Height   uint32
	LastHash hash.Hash256
}

StatusMessage used to provide the chain information to a peer

type TCPAsyncPeer

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

TCPAsyncPeer manages send and recv of the connection

func NewTCPAsyncPeer

func NewTCPAsyncPeer(conn net.Conn, ID string, Name string, connectedTime int64) *TCPAsyncPeer

NewTCPAsyncPeer returns a TCPAsyncPeer

func (*TCPAsyncPeer) Close

func (p *TCPAsyncPeer) Close()

Close closes TCPAsyncPeer

func (*TCPAsyncPeer) ConnectedTime

func (p *TCPAsyncPeer) ConnectedTime() int64

ConnectedTime returns peer connected time

func (*TCPAsyncPeer) ID

func (p *TCPAsyncPeer) ID() string

ID returns the id of the peer

func (*TCPAsyncPeer) IsClosed

func (p *TCPAsyncPeer) IsClosed() bool

IsClosed returns it is closed or not

func (*TCPAsyncPeer) Name

func (p *TCPAsyncPeer) Name() string

Name returns the name of the peer

func (*TCPAsyncPeer) ReadPacket

func (p *TCPAsyncPeer) ReadPacket() ([]byte, error)

ReadPacket returns a packet data

func (*TCPAsyncPeer) SendPacket

func (p *TCPAsyncPeer) SendPacket(bs []byte)

SendPacket sends packet to the WebsocketPeer

type TCPPeer

type TCPPeer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

TCPPeer manages send and recv of the connection

func NewTCPPeer

func NewTCPPeer(conn net.Conn, ID string, Name string, connectedTime int64) *TCPPeer

NewTCPPeer returns a TCPPeer

func (*TCPPeer) Close

func (p *TCPPeer) Close()

Close closes TCPPeer

func (*TCPPeer) ConnectedTime

func (p *TCPPeer) ConnectedTime() int64

ConnectedTime returns peer connected time

func (*TCPPeer) ID

func (p *TCPPeer) ID() string

ID returns the id of the peer

func (*TCPPeer) IsClosed

func (p *TCPPeer) IsClosed() bool

IsClosed returns it is closed or not

func (*TCPPeer) Name

func (p *TCPPeer) Name() string

Name returns the name of the peer

func (*TCPPeer) ReadPacket

func (p *TCPPeer) ReadPacket() ([]byte, error)

ReadPacket returns a packet data

func (*TCPPeer) SendPacket

func (p *TCPPeer) SendPacket(bs []byte)

SendPacket sends packet to the WebsocketPeer

type TransactionMessage

type TransactionMessage struct {
	Types      []uint16             //MAXLEN : 65535
	Txs        []types.Transaction  //MAXLEN : 65535
	Signatures [][]common.Signature //MAXLEN : 65535
}

TransactionMessage is a message for a transaction

type TxMsgItem

type TxMsgItem struct {
	TxHash hash.Hash256
	Type   uint16
	Tx     types.Transaction
	Sigs   []common.Signature
	PeerID string
	ErrCh  *chan error
}

TxMsgItem used to store transaction message

type WebsocketPeer

type WebsocketPeer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

WebsocketPeer manages send and recv of the connection

func NewWebsocketPeer

func NewWebsocketPeer(conn *websocket.Conn, ID string, Name string, connectedTime int64) *WebsocketPeer

NewWebsocketPeer returns a WebsocketPeer

func (*WebsocketPeer) Close

func (p *WebsocketPeer) Close()

Close closes WebsocketPeer

func (*WebsocketPeer) ConnectedTime

func (p *WebsocketPeer) ConnectedTime() int64

ConnectedTime returns peer connected time

func (*WebsocketPeer) ID

func (p *WebsocketPeer) ID() string

ID returns the id of the peer

func (*WebsocketPeer) IsClosed

func (p *WebsocketPeer) IsClosed() bool

IsClosed returns it is closed or not

func (*WebsocketPeer) Name

func (p *WebsocketPeer) Name() string

Name returns the name of the peer

func (*WebsocketPeer) ReadPacket

func (p *WebsocketPeer) ReadPacket() ([]byte, error)

ReadPacket returns a packet data

func (*WebsocketPeer) SendPacket

func (p *WebsocketPeer) SendPacket(bs []byte)

SendPacket sends packet to the WebsocketPeer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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