devp2p

package module
v0.0.0-...-e2a46ae Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: MPL-2.0 Imports: 16 Imported by: 0

README

Go-devp2p

Implementation of the Ethereum DevP2P stack in Go.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Capabilities

type Capabilities []*Capability

Capabilities is a list of capabilities of the peer

type Capability

type Capability struct {
	Protocol Protocol
}

Capability is a feature of the peer

type Config

type Config struct {
	Logger           *log.Logger
	Name             string
	BindAddress      string
	BindPort         int
	MaxPeers         int
	Bootnodes        []string
	DialTasks        int
	DialBusyInterval time.Duration
	PeerStore        PeerStore
	Protocols        []*Protocol
}

Config is the p2p server configuration

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default configuration

type ConfigOption

type ConfigOption func(*Config)

func WithBindAddress

func WithBindAddress(addr string) ConfigOption

func WithBindPort

func WithBindPort(port int) ConfigOption

func WithBootnodes

func WithBootnodes(bootnodes []string) ConfigOption

func WithLogger

func WithLogger(logger *log.Logger) ConfigOption

func WithMaxPeers

func WithMaxPeers(maxPeers int) ConfigOption

func WithName

func WithName(name string) ConfigOption

func WithPeerStore

func WithPeerStore(peerstore PeerStore) ConfigOption

func WithProtocol

func WithProtocol(p *Protocol) ConfigOption

type Dispatcher

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

Dispatcher is used to track and launch pJobs

func NewDispatcher

func NewDispatcher() *Dispatcher

NewDispatcher creates a new dispatcher

func (*Dispatcher) Add

func (p *Dispatcher) Add(job Job, period time.Duration) error

Add adds a new job with an interval period to dispatch the job

func (*Dispatcher) Contains

func (p *Dispatcher) Contains(id string) bool

Contains check if the job is on the dispatcher

func (*Dispatcher) Events

func (p *Dispatcher) Events() chan Job

Events returns the channel of events

func (*Dispatcher) Remove

func (p *Dispatcher) Remove(value string) error

Remove removes a job from the dispatcher

func (*Dispatcher) SetEnabled

func (p *Dispatcher) SetEnabled(enabled bool)

SetEnabled is used to control if the periodic dispatcher is enabled

func (*Dispatcher) Tracked

func (p *Dispatcher) Tracked() []Job

Tracked returns the object being tracked

type EventType

type EventType int
const (
	NodeJoin EventType = iota
	NodeLeave
	NodeHandshakeFail
)

func (EventType) String

func (t EventType) String() string

type Info

type Info struct {
	Client       string
	Enode        *enode.Enode
	Capabilities Capabilities
	ListenPort   uint64
}

Info is the information of a peer

type Instance

type Instance struct {
	Protocol *Protocol
}

type JSONPeerStore

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

JSONPeerStore stores the peers locally in json format

func NewJSONPeerStore

func NewJSONPeerStore(path string) *JSONPeerStore

NewJSONPeerStore creates a json peerstore

func (*JSONPeerStore) Close

func (p *JSONPeerStore) Close() error

Close implements the PeerStore interface

func (*JSONPeerStore) Load

func (p *JSONPeerStore) Load() ([]string, error)

Load implements the PeerStore interface

func (*JSONPeerStore) Update

func (p *JSONPeerStore) Update(addr string, status Status) error

Update implements the PeerStore interface

type Job

type Job interface {
	ID() string
}

Job is a job in the dispatcher

type MemberEvent

type MemberEvent struct {
	Type EventType
	Peer *Peer
}

type NoopPeerStore

type NoopPeerStore struct {
}

NoopPeerStore is a peerstore that does not store peers

func (*NoopPeerStore) Close

func (i *NoopPeerStore) Close() error

Close implements the PeerStore interface

func (*NoopPeerStore) Load

func (i *NoopPeerStore) Load() ([]string, error)

Load implements the PeerStore interface

func (*NoopPeerStore) Update

func (i *NoopPeerStore) Update(addr string, status Status) error

Update implements the PeerStore interface

type Peer

type Peer struct {
	Enode *enode.Enode
	Info  Info
	ID    string

	Status Status
	// contains filtered or unexported fields
}

Peer is each of the connected peers

func (*Peer) Close

func (p *Peer) Close() error

Close closes the peer connection

func (*Peer) GetProtocol

func (p *Peer) GetProtocol(name string) (*Instance, bool)

GetProtocol returns the protocol by name

func (*Peer) GetProtocols

func (p *Peer) GetProtocols() []*Instance

GetProtocols returns all the protocols of the peer

func (*Peer) IsClosed

func (p *Peer) IsClosed() bool

IsClosed checks if the connection is closed

func (*Peer) PrettyID

func (p *Peer) PrettyID() string

PrettyID returns a pretty version of the id

func (*Peer) Session

func (p *Peer) Session() Session

Session returns the session of the peer

type PeerStore

type PeerStore interface {
	Load() ([]string, error)
	Update(addr string, status Status) error
	Close() error
}

PeerStore stores peers id

type PeriodicDial

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

PeriodicDial is the periodic dial of busy peers

func (*PeriodicDial) ID

func (p *PeriodicDial) ID() string

ID returns the id of the enode

type Protocol

type Protocol struct {
	Spec      ProtocolSpec
	HandlerFn func(conn Stream, peer *Peer) error
}

Protocol is a wire protocol

type ProtocolSpec

type ProtocolSpec struct {
	Name    string
	Version uint
	Length  uint64
}

ProtocolSpec is a specification of an etheruem protocol

type Server

type Server struct {
	Name string

	EventCh chan MemberEvent

	Discovery discovery.Discovery
	Enode     *enode.Enode
	// contains filtered or unexported fields
}

Server is the ethereum client

func NewServer

func NewServer(key *ecdsa.PrivateKey, transport Transport, opts ...ConfigOption) (*Server, error)

NewServer creates a new node

func (*Server) Close

func (s *Server) Close()

func (*Server) Dial

func (s *Server) Dial(enode string)

Dial dials an enode (async)

func (*Server) DialSync

func (s *Server) DialSync(enode string) error

DialSync dials and waits for the result

func (*Server) Disconnect

func (s *Server) Disconnect()

func (*Server) GetPeer

func (s *Server) GetPeer(id string) *Peer

func (*Server) GetPeerByPrefix

func (s *Server) GetPeerByPrefix(search string) (*Peer, bool)

GetPeerByPrefix searches a peer by his prefix

func (*Server) GetPeers

func (s *Server) GetPeers() []string

GetPeers returns a copy of list of peers

func (*Server) ID

func (s *Server) ID() enode.ID

func (*Server) Start

func (s *Server) Start() error

Schedule starts all the tasks once all the protocols have been loaded

type Session

type Session interface {
	// Stream returns the set of streams inside the session
	Streams() []Stream

	// Info returns the information of the network
	GetInfo() Info

	// CloseChan returns a read-only channel which is closed as
	// soon as the session is closed.
	CloseChan() <-chan struct{}

	// IsClosed returns if the session has been closed
	IsClosed() bool

	// Close closes the connection
	Close() error
}

Session is an open connection between two peers

type Status

type Status int
const (
	PeerActive Status = iota
	PeerPending
	PeerDisconnected
	PeerBusy
)

func (Status) String

func (s Status) String() string

type Stream

type Stream interface {
	// WriteMsg writes a message
	WriteMsg(code uint64, b []byte) error

	// ReadMsg reads a message from the stream
	ReadMsg() ([]byte, uint16, error)

	// Close closes the connection
	Close() error

	Protocol() ProtocolSpec
}

Stream is a stream inside a session

type Transport

type Transport interface {
	// Setup starts the protocol with the given private key
	Setup(priv *ecdsa.PrivateKey, backends []*Protocol, info *Info, config map[string]interface{}) error

	// DialTimeout connects to the address within a given timeout.
	DialTimeout(addr string, timeout time.Duration) (Session, error)

	// Accept accepts the new session
	Accept() (Session, error)

	// Close closes the transport
	Close() error
}

Transport is a generic network transport protocol

Directories

Path Synopsis
wire
eth

Jump to

Keyboard shortcuts

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