p2p

package
v0.0.0-...-b6e904f Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2023 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateKeyPairWithPath

func GenerateKeyPairWithPath(path string) error

func GetKeyPairFromPath

func GetKeyPairFromPath(path string) (crypto.PrivKey, error)

func GetPeerIDFromPath

func GetPeerIDFromPath(path string) (string, error)

Types

type Channel

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

func NewChannel

func NewChannel(id int32, conn *DefaultConn, log libs.Logger) *Channel

type Config

type Config struct {
	Address    string
	BootStrap  []string
	PrivateKey string // only for networking
	PublicKey  string // only for networking

	TickerTimeSec int64
}

---------------------------------------------------------------------------------------------------

type DefaultConn

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

func NewDefaultConn

func NewDefaultConn(peer NodeInfo, netStream network.Stream,
	onReceiveIdx map[Module]libs.Reactor, logger libs.Logger) (*DefaultConn, error)

func (*DefaultConn) AddChannel

func (dc *DefaultConn) AddChannel(id int32) error

func (*DefaultConn) FlushStop

func (dc *DefaultConn) FlushStop()

FlushStop replicates the logic of OnStop. It additionally ensures that all successful .Send() calls will get flushed before closing the connection.

func (*DefaultConn) Send

func (dc *DefaultConn) Send(chID int32, msgBytes []byte) bool

func (*DefaultConn) Start

func (dc *DefaultConn) Start()

type DefaultNodeInfo

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

func (*DefaultNodeInfo) CompatibleWith

func (n *DefaultNodeInfo) CompatibleWith(other NodeInfo) error

func (*DefaultNodeInfo) ID

func (n *DefaultNodeInfo) ID() PeerID

func (*DefaultNodeInfo) NetAddress

func (n *DefaultNodeInfo) NetAddress() (*NetAddress, error)

func (*DefaultNodeInfo) Validate

func (n *DefaultNodeInfo) Validate() error

for use in the handshake.

type DefaultPeer

type DefaultPeer struct {
	*DefaultNodeInfo
	// contains filtered or unexported fields
}

func (*DefaultPeer) FlushStop

func (p *DefaultPeer) FlushStop()

func (*DefaultPeer) Send

func (p *DefaultPeer) Send(chID int32, msgBytes []byte) bool

func (*DefaultPeer) Start

func (dc *DefaultPeer) Start()

type Module

type Module string

type NetAddress

type NetAddress struct {
	Name string `json:"id"`
	IP   net.IP `json:"ip"`
	Port uint16 `json:"port"`
}

NetAddress defines information about a peer on the network including its ID, IP address, and port.

type NodeInfo

type NodeInfo interface {
	ID() PeerID
	NetAddress() (*NetAddress, error)

	// for use in the handshake.
	Validate() error
	CompatibleWith(other NodeInfo) error
}

NodeInfo exposes basic info of a node and determines if we're compatible.

type Peer

type Peer interface {
	RawConn
	NodeInfo
}

Peer is an interface representing a peer connected on a reactor.

func NewDefaultPeer

func NewDefaultPeer(peer *peer.AddrInfo, netStream network.Stream,
	onReceiveIdx map[Module]libs.Reactor, logger libs.Logger) (Peer, error)

type PeerID

type PeerID string

type PeerSet

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

PeerSet is a special structure for keeping a table of peers. Iteration over the peers is super fast and thread-safe.

func NewPeerSet

func NewPeerSet() *PeerSet

func (*PeerSet) Add

func (s *PeerSet) Add(peer Peer) error

func (*PeerSet) Find

func (s *PeerSet) Find(id PeerID) (Peer, error)

func (*PeerSet) Range

func (s *PeerSet) Range(f func(Peer) bool) chan bool

type RawConn

type RawConn interface {
	Start()
	FlushStop()
	Send(int32, []byte) bool
}

type Switch

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

func NewSwitch

func NewSwitch(cfg *Config, logger libs.Logger) (*Switch, error)

func (*Switch) AddReactor

func (sw *Switch) AddReactor(mo Module, f libs.Reactor) error

AddReactor should be invoked before switch.Start(), consensus module must be registered.

func (*Switch) Broadcast

func (sw *Switch) Broadcast(chID int32, msgBytes []byte)

Broadcast runs a go routine for each attempted send, which will block trying to send for defaultSendTimeoutSeconds. Returns a channel which receives success values for each attempted send (false if times out). Channel will be closed once msg bytes are sent to all peers (or time out).

NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.

func (*Switch) GetP2PID

func (sw *Switch) GetP2PID(peerID string) (string, error)

func (*Switch) Send

func (sw *Switch) Send(peer string, chID int32, msgBytes []byte) error

func (*Switch) Start

func (sw *Switch) Start() error

func (*Switch) Stop

func (sw *Switch) Stop() error

Jump to

Keyboard shortcuts

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