discovery

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: Apache-2.0, UNKNOWN not legal advice Imports: 0 Imported by: 0

Documentation

Overview

Package discovery contains the p2p peer discovery service (Reactor). The purpose of the peer discovery service is to gather peer lists from known peers, and attempt to fill out open peer connection slots in order to build out a fuller mesh.

The implementation of the peer discovery protocol is relatively simple. In essence, it pings a random peer at a specific interval (3s), for a list of their known peers (max 30). After receiving the list, and verifying it, the node attempts to establish outbound connections to the given peers.

Index

Constants

View Source
const (
	// Channel is the unique channel for the peer discovery protocol
	Channel = byte(0x50)
)

Variables

View Source
var Package = amino.RegisterPackage(amino.NewPackage(
	"github.com/gnolang/gno/tm2/pkg/p2p/discovery",
	"p2p",
	amino.GetCallersDirname(),
).
	WithTypes(
		&Request{},
		&Response{},
	),
)

Functions

This section is empty.

Types

type Message

type Message interface {
	ValidateBasic() error
}

Message is the wrapper for the discovery message

type Option

type Option func(*Reactor)

func WithDiscoveryInterval

func WithDiscoveryInterval(interval time.Duration) Option

WithDiscoveryInterval sets the discovery crawl interval

type Reactor

type Reactor struct {
	// This embed and the usage of "services"
	// like the peer discovery reactor highlight the
	// flipped design of the p2p package.
	// The peer exchange service needs to be instantiated _outside_
	// the p2p module, because of this flipped design.
	// Peers communicate with each other through Reactor channels,
	// which are instantiated outside the p2p module
	p2p.BaseReactor
	// contains filtered or unexported fields
}

Reactor wraps the logic for the peer exchange protocol

func NewReactor

func NewReactor(store *Store, opts ...Option) *Reactor

NewReactor creates a new peer discovery reactor. The store is used to persist discovered peers across restarts.

func (*Reactor) GetChannels

func (r *Reactor) GetChannels() []*conn.ChannelDescriptor

GetChannels returns the channels associated with peer discovery

func (*Reactor) OnStart

func (r *Reactor) OnStart() error

OnStart runs the peer discovery protocol

func (*Reactor) OnStop

func (r *Reactor) OnStop()

OnStop stops the peer discovery protocol

func (*Reactor) Receive

func (r *Reactor) Receive(chID byte, peer p2p.PeerConn, msgBytes []byte)

Receive handles incoming messages for the peer discovery reactor

type Request

type Request struct{}

Request is the peer discovery request. It is empty by design, since it's used as a notification type

func (*Request) ValidateBasic

func (r *Request) ValidateBasic() error

type Response

type Response struct {
	Peers []*types.NetAddress // the peer set returned by the peer
}

Response is the peer discovery response

func (*Response) ValidateBasic

func (r *Response) ValidateBasic() error

type Store

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

Store persists discovered peer addresses to disk so they survive node restarts. It is safe for concurrent use.

func NewStore

func NewStore(filePath string, self types.NetAddress, opts ...StoreOption) (*Store, error)

NewStore creates a new peer store backed by the given file path. self is the node's own address and is never stored. Existing peers are loaded from disk on construction.

func (*Store) AddPeers

func (s *Store) AddPeers(addrs ...*types.NetAddress)

AddPeers adds the given peer addresses to the store. The node's own address is ignored, as are nil entries. When the store exceeds its capacity, the oldest entries are evicted.

func (*Store) Flush

func (s *Store) Flush() error

Flush forces a save of the peer store to disk regardless of dirty state.

func (*Store) GetPeers

func (s *Store) GetPeers() []*types.NetAddress

GetPeers returns all stored peer addresses.

func (*Store) Save

func (s *Store) Save() error

Save persists the peer store to disk atomically. It is a no-op when the store has not changed since the last successful save.

func (*Store) Size

func (s *Store) Size() int

Size returns the number of stored peer addresses.

type StoreOption

type StoreOption func(*Store)

StoreOption configures the peer Store.

func WithLogger

func WithLogger(logger *slog.Logger) StoreOption

WithLogger sets the store logger.

func WithMaxPeers

func WithMaxPeers(maxPeers int) StoreOption

WithMaxPeers sets the maximum number of peers the store keeps. When the limit is reached, the oldest entries are evicted.

Jump to

Keyboard shortcuts

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