Help us shape the future of Go! Take the 2026 Go Developer Survey

gossip

package
v1.17.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidNumValidators     = errors.New("num validators cannot be negative")
	ErrInvalidNumNonValidators  = errors.New("num non-validators cannot be negative")
	ErrInvalidNumPeers          = errors.New("num peers cannot be negative")
	ErrInvalidNumToGossip       = errors.New("must gossip to at least one peer")
	ErrInvalidDiscardedSize     = errors.New("discarded size cannot be negative")
	ErrInvalidTargetGossipSize  = errors.New("target gossip size cannot be negative")
	ErrInvalidRegossipFrequency = errors.New("re-gossip frequency cannot be negative")
)

Functions

func Every

func Every(ctx context.Context, log luxlog.Logger, gossiper Gossiper, frequency time.Duration)

Every calls [Gossip] every [frequency] amount of time.

func MarshalAppGossip

func MarshalAppGossip(gossip [][]byte) ([]byte, error)

func MarshalAppRequest

func MarshalAppRequest(filter, salt []byte) ([]byte, error)

func MarshalAppResponse

func MarshalAppResponse(gossip [][]byte) ([]byte, error)

func ParseAppGossip

func ParseAppGossip(bytes []byte) ([][]byte, error)

func ParseAppRequest

func ParseAppRequest(bytes []byte) (*bloom.ReadFilter, ids.ID, error)

func ParseAppResponse

func ParseAppResponse(bytes []byte) ([][]byte, error)

func ResetBloomFilterIfNeeded

func ResetBloomFilterIfNeeded(
	bloomFilter *BloomFilter,
	targetElements int,
) (bool, error)

ResetBloomFilterIfNeeded resets a bloom filter if it breaches [targetFalsePositiveProbability].

If [targetElements] exceeds [minTargetElements], the size of the bloom filter will grow to maintain the same [targetFalsePositiveProbability].

Returns true if the bloom filter was reset.

Types

type BloomFilter

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

func NewBloomFilter

func NewBloomFilter(
	registerer metric.Registerer,
	namespace string,
	minTargetElements int,
	targetFalsePositiveProbability,
	resetFalsePositiveProbability float64,
) (*BloomFilter, error)

NewBloomFilter returns a new instance of a bloom filter with at least [minTargetElements] elements anticipated at any moment, and a false positive probability of [targetFalsePositiveProbability]. If the false positive probability exceeds [resetFalsePositiveProbability], the bloom filter will be reset.

Invariant: The returned bloom filter is not safe to reset concurrently with other operations. However, it is otherwise safe to access concurrently.

func (*BloomFilter) Add

func (b *BloomFilter) Add(gossipable Gossipable)

func (*BloomFilter) Has

func (b *BloomFilter) Has(gossipable Gossipable) bool

func (*BloomFilter) Marshal

func (b *BloomFilter) Marshal() ([]byte, []byte)

type BranchingFactor

type BranchingFactor struct {
	// StakePercentage determines the percentage of stake that should have
	// gossip sent to based on the inverse CDF of stake weights. This value does
	// not account for the connectivity of the nodes.
	StakePercentage float64
	// Validators specifies the number of connected validators, in addition to
	// any validators sent from the StakePercentage parameter, to send gossip
	// to. These validators are sampled uniformly rather than by stake.
	Validators int
	// NonValidators specifies the number of connected non-validators to send
	// gossip to.
	NonValidators int
	// Peers specifies the number of connected validators or non-validators, in
	// addition to the number sent due to other configs, to send gossip to.
	Peers int
}

func (*BranchingFactor) Verify

func (b *BranchingFactor) Verify() error

type EmptySet added in v1.1.11

type EmptySet[T Gossipable] struct{}

func (EmptySet[T]) Add added in v1.1.11

func (EmptySet[T]) Add(T) error

func (EmptySet[_]) GetFilter added in v1.1.11

func (EmptySet[_]) GetFilter() ([]byte, []byte)

func (EmptySet[_]) Gossip added in v1.1.11

func (EmptySet[_]) Gossip(context.Context) error

func (EmptySet[T]) Has added in v1.1.11

func (EmptySet[T]) Has(ids.ID) bool

func (EmptySet[T]) Iterate added in v1.1.11

func (EmptySet[T]) Iterate(func(gossipable T) bool)

type FakeSender added in v1.16.16

type FakeSender struct {
	SendAppRequestF            func(context.Context, set.Set[ids.NodeID], uint32, []byte) error
	SendAppResponseF           func(context.Context, ids.NodeID, uint32, []byte) error
	SendAppErrorF              func(context.Context, ids.NodeID, uint32, int32, string) error
	SendAppGossipF             func(context.Context, set.Set[ids.NodeID], []byte) error
	SendCrossChainAppRequestF  func(context.Context, ids.ID, uint32, []byte) error
	SendCrossChainAppResponseF func(context.Context, ids.ID, uint32, []byte) error

	// Fields for test channels
	SentAppRequest  chan []byte
	SentAppResponse chan []byte
	SentAppGossip   chan []byte
}

FakeSender is a test implementation

func (*FakeSender) SendAppError added in v1.16.16

func (f *FakeSender) SendAppError(ctx context.Context, nodeID ids.NodeID, requestID uint32, errorCode int32, errorMessage string) error

func (*FakeSender) SendAppGossip added in v1.16.16

func (f *FakeSender) SendAppGossip(ctx context.Context, nodeIDs set.Set[ids.NodeID], gossip []byte) error

func (*FakeSender) SendAppGossipSpecific added in v1.16.16

func (f *FakeSender) SendAppGossipSpecific(ctx context.Context, nodeIDs set.Set[ids.NodeID], gossip []byte) error

func (*FakeSender) SendAppRequest added in v1.16.16

func (f *FakeSender) SendAppRequest(ctx context.Context, nodeIDs set.Set[ids.NodeID], requestID uint32, request []byte) error

func (*FakeSender) SendAppResponse added in v1.16.16

func (f *FakeSender) SendAppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error

func (*FakeSender) SendCrossChainAppRequest added in v1.16.16

func (f *FakeSender) SendCrossChainAppRequest(ctx context.Context, chainID ids.ID, requestID uint32, request []byte) error

func (*FakeSender) SendCrossChainAppResponse added in v1.16.16

func (f *FakeSender) SendCrossChainAppResponse(ctx context.Context, chainID ids.ID, requestID uint32, response []byte) error

type FakeSenderAdapter added in v1.16.16

type FakeSenderAdapter struct {
	*FakeSender
}

FakeSenderAdapter wraps FakeSender to implement ExtendedAppSender

func (*FakeSenderAdapter) SendAppError added in v1.16.16

func (f *FakeSenderAdapter) SendAppError(ctx context.Context, nodeID ids.NodeID, requestID uint32, errorCode int32, errorMessage string) error

SendAppError implements ExtendedAppSender

func (*FakeSenderAdapter) SendAppGossip added in v1.16.16

func (f *FakeSenderAdapter) SendAppGossip(ctx context.Context, nodeIDs set.Set[ids.NodeID], appGossipBytes []byte) error

SendAppGossip implements ExtendedAppSender

func (*FakeSenderAdapter) SendAppGossipSpecific added in v1.16.16

func (f *FakeSenderAdapter) SendAppGossipSpecific(ctx context.Context, nodeIDs set.Set[ids.NodeID], appGossipBytes []byte) error

SendAppGossipSpecific implements ExtendedAppSender

func (*FakeSenderAdapter) SendAppRequest added in v1.16.16

func (f *FakeSenderAdapter) SendAppRequest(ctx context.Context, nodeIDs set.Set[ids.NodeID], requestID uint32, appRequestBytes []byte) error

SendAppRequest implements ExtendedAppSender

func (*FakeSenderAdapter) SendAppResponse added in v1.16.16

func (f *FakeSenderAdapter) SendAppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, appResponseBytes []byte) error

SendAppResponse implements ExtendedAppSender

func (*FakeSenderAdapter) SendCrossChainAppError added in v1.16.16

func (f *FakeSenderAdapter) SendCrossChainAppError(ctx context.Context, chainID ids.ID, requestID uint32, errorCode int32, errorMessage string) error

SendCrossChainAppError implements ExtendedAppSender

func (*FakeSenderAdapter) SendCrossChainAppRequest added in v1.16.16

func (f *FakeSenderAdapter) SendCrossChainAppRequest(ctx context.Context, chainID ids.ID, requestID uint32, appRequestBytes []byte) error

SendCrossChainAppRequest implements ExtendedAppSender

func (*FakeSenderAdapter) SendCrossChainAppResponse added in v1.16.16

func (f *FakeSenderAdapter) SendCrossChainAppResponse(ctx context.Context, chainID ids.ID, requestID uint32, appResponseBytes []byte) error

SendCrossChainAppResponse implements ExtendedAppSender

type FullSet

type FullSet[T Gossipable] struct{}

func (FullSet[T]) Add

func (FullSet[T]) Add(T) error

func (FullSet[_]) GetFilter

func (FullSet[_]) GetFilter() ([]byte, []byte)

func (FullSet[_]) Gossip

func (FullSet[_]) Gossip(context.Context) error

func (FullSet[T]) Has

func (FullSet[T]) Has(ids.ID) bool

func (FullSet[T]) Iterate

func (FullSet[T]) Iterate(func(gossipable T) bool)

type Gossipable

type Gossipable interface {
	GossipID() ids.ID
}

Gossipable is an item that can be gossiped across the network

type Gossiper

type Gossiper interface {
	// Gossip runs a cycle of gossip. Returns an error if we failed to gossip.
	Gossip(ctx context.Context) error
}

Gossiper gossips Gossipables to other nodes

type Handler

type Handler[T Gossipable] struct {
	p2p.Handler
	// contains filtered or unexported fields
}

func NewHandler

func NewHandler[T Gossipable](
	log luxlog.Logger,
	marshaller Marshaller[T],
	set Set[T],
	metrics Metrics,
	targetResponseSize int,
) *Handler[T]

func (Handler[_]) AppGossip

func (h Handler[_]) AppGossip(_ context.Context, nodeID ids.NodeID, gossipBytes []byte)

func (Handler[T]) AppRequest

func (h Handler[T]) AppRequest(_ context.Context, _ ids.NodeID, _ time.Time, requestBytes []byte) ([]byte, *core.AppError)

type Marshaller

type Marshaller[T Gossipable] interface {
	MarshalGossip(T) ([]byte, error)
	UnmarshalGossip([]byte) (T, error)
}

Marshaller handles parsing logic for a concrete Gossipable type

type Metrics

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

Metrics that are tracked across a gossip protocol. A given protocol should only use a single instance of Metrics.

func NewMetrics

func NewMetrics(
	metrics metric.Registerer,
	namespace string,
) (Metrics, error)

NewMetrics returns a common set of metrics

type NoOpGossiper

type NoOpGossiper struct{}

func (NoOpGossiper) Gossip

type PullGossiper

type PullGossiper[T Gossipable] struct {
	// contains filtered or unexported fields
}

func NewPullGossiper

func NewPullGossiper[T Gossipable](
	log luxlog.Logger,
	marshaller Marshaller[T],
	set Set[T],
	client *p2p.Client,
	metrics Metrics,
	pollSize int,
) *PullGossiper[T]

func (*PullGossiper[_]) Gossip

func (p *PullGossiper[_]) Gossip(ctx context.Context) error

type PushGossiper

type PushGossiper[T Gossipable] struct {
	// contains filtered or unexported fields
}

PushGossiper broadcasts gossip to peers randomly in the network

func NewPushGossiper

func NewPushGossiper[T Gossipable](
	marshaller Marshaller[T],
	mempool Set[T],
	validators p2p.ValidatorSubset,
	client *p2p.Client,
	metrics Metrics,
	gossipParams BranchingFactor,
	regossipParams BranchingFactor,
	discardedSize int,
	targetGossipSize int,
	maxRegossipFrequency time.Duration,
) (*PushGossiper[T], error)

NewPushGossiper returns an instance of PushGossiper

func (*PushGossiper[T]) Add

func (p *PushGossiper[T]) Add(gossipables ...T)

Add enqueues new gossipables to be pushed. If a gossipable is already tracked, it is not added again.

func (*PushGossiper[T]) Gossip

func (p *PushGossiper[T]) Gossip(ctx context.Context) error

Gossip flushes any queued gossipables.

type Set

type Set[T Gossipable] interface {
	// Add adds a Gossipable to the set. Returns an error if gossipable was not
	// added.
	Add(gossipable T) error
	// Has returns true if the gossipable is in the set.
	Has(gossipID ids.ID) bool
	// Iterate iterates over elements until [f] returns false
	Iterate(f func(gossipable T) bool)
	// GetFilter returns the byte representation of bloom filter and its
	// corresponding salt.
	GetFilter() (bloom []byte, salt []byte)
}

Set holds a set of known Gossipable items

type TestGossiper

type TestGossiper struct {
	GossipF func(ctx context.Context) error
}

func (*TestGossiper) Gossip

func (t *TestGossiper) Gossip(ctx context.Context) error

type ValidatorGossiper

type ValidatorGossiper struct {
	Gossiper

	NodeID     ids.NodeID
	Validators p2p.ValidatorSet
}

ValidatorGossiper only calls [Gossip] if the given node is a validator

func (ValidatorGossiper) Gossip

func (v ValidatorGossiper) Gossip(ctx context.Context) error

Jump to

Keyboard shortcuts

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