p2p

package
v0.0.0-...-4892312 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 37 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxPayloadSize           = 10 * 1024 * 1024
	MaxCompressedPayloadSize = 32 + MaxPayloadSize + MaxPayloadSize/6 + 1024
	MaxErrorMessageSize      = 256

	// MaxSignedBlockSize is the SSZ size of a SignedBlock with every list and
	// bitlist at its limit. A block response chunk declaring more cannot decode.
	MaxSignedBlockSize = 3_182_692
)
View Source
const (
	RespSuccess             byte = 0x00
	RespInvalidRequest      byte = 0x01
	RespServerError         byte = 0x02
	RespResourceUnavailable byte = 0x03
)
View Source
const (
	StatusProtocol        = "/leanconsensus/req/status/1/ssz_snappy"
	BlocksByRootProtocol  = "/leanconsensus/req/blocks_by_root/1/ssz_snappy"
	BlocksByRangeProtocol = "/leanconsensus/req/blocks_by_range/1/ssz_snappy"
)
View Source
const (
	MaxFetchRetries     = 10
	InitialBackoffMs    = 5
	BackoffMultiplier   = 2
	MaxBlocksPerRequest = 10
)
View Source
const (
	GossipMeshN             = 8
	GossipMeshNLow          = 6
	GossipMeshNHigh         = 12
	GossipLazy              = 6
	GossipHeartbeatInterval = 700 * time.Millisecond
	GossipFanoutTTL         = 60 * time.Second
	GossipHistoryLength     = 6
	GossipHistoryGossip     = 3
	GossipDuplicateCache    = 24 * time.Second
	GossipMaxTransmitSize   = MaxCompressedPayloadSize
	GossipMaxMsgPerRPC      = 500
	// GossipValidateQueueSize bounds messages waiting for validation. pubsub's
	// default of 32 drops anything beyond one small burst, silently; this holds
	// several full RPCs.
	GossipValidateQueueSize = 4 * GossipMaxMsgPerRPC
)
View Source
const (
	BlockTopicKind       = "block"
	AttestationTopicKind = "attestation"
	AggregationTopicKind = "aggregation"
)
View Source
const BootnodeRedialSecs = 12
View Source
const ForkDigest = "12345678"
View Source
const ReqRespTimeout = 15 * time.Second

ReqRespTimeout is the idle deadline applied to each individual req/resp read and write (see deadline.go). It bounds how long a single stalled operation may block, not the whole exchange — a transfer that keeps making progress is never cut.

Variables

This section is empty.

Functions

func AggregationTopic

func AggregationTopic() string

func AttestationSubnetTopic

func AttestationSubnetTopic(subnetID uint64) string

func BlockTopic

func BlockTopic() string

func BuildGossipTopic

func BuildGossipTopic(forkDigest, topicName string) string

func ComputeMessageID

func ComputeMessageID(topic string, rawData []byte) []byte

func DecodeBlocksByRootRequest

func DecodeBlocksByRootRequest(payload []byte) ([][32]byte, error)

func DecodeReqRespPayload

func DecodeReqRespPayload(buf []byte) ([]byte, error)

func DecodeResponse

func DecodeResponse(r io.Reader, maxLen uint32) (byte, []byte, error)

func DecodeVarint

func DecodeVarint(buf []byte) (uint32, []byte, error)

func EncodeBlocksByRootRequest

func EncodeBlocksByRootRequest(roots [][32]byte) []byte

func EncodeReqRespPayload

func EncodeReqRespPayload(data []byte) []byte

func EncodeResponse

func EncodeResponse(code byte, data []byte) []byte

func EncodeVarint

func EncodeVarint(value uint32) []byte

func LoadBootnodes

func LoadBootnodes(path string) ([]multiaddr.Multiaddr, error)

func ParseENR

func ParseENR(enrStr string) (multiaddr.Multiaddr, error)

func SetClientGitCommit

func SetClientGitCommit(commit string)

func SnappyRawDecode

func SnappyRawDecode(data []byte) ([]byte, error)

func SnappyRawEncode

func SnappyRawEncode(data []byte) []byte

func SubnetID

func SubnetID(validatorID, committeeCount uint64) uint64

func TopicString

func TopicString(kind string) string

Types

type ENRFields

type ENRFields struct {
	Seq       uint64
	PeerID    peer.ID
	Multiaddr string
}

func DecodeENR

func DecodeENR(enrStr string) (*ENRFields, error)

type Hooks

type Hooks struct {
	GossipBlockSize       func(bytes int)
	GossipAttestationSize func(bytes int)
	GossipAggregationSize func(bytes int)
	PeerConnected         func(direction string)
	PeerDisconnected      func(direction, reason string)
	PeerCount             func(count int)
	PeerStatus            func(peerID peer.ID)
}

type Host

type Host struct {
	Hooks Hooks
	// contains filtered or unexported fields
}

func NewHost

func NewHost(
	ctx context.Context,
	nodeKeyPath string,
	listenPort int,
	committeeCount uint64,
	validatorIDs []uint64,
	isAggregator bool,
	aggregateSubnetIDs []uint64,
) (*Host, error)

func (*Host) Addrs

func (h *Host) Addrs() []multiaddr.Multiaddr

func (*Host) Close

func (h *Host) Close()

Close shuts the host down and waits for its gossip listeners, req/resp handlers and other goroutines to return.

func (*Host) ConnectBootnodes

func (h *Host) ConnectBootnodes(ctx context.Context, addrs []multiaddr.Multiaddr)

func (*Host) ConnectPeer

func (h *Host) ConnectPeer(ctx context.Context, addr multiaddr.Multiaddr) error

func (*Host) ConnectedPeers

func (h *Host) ConnectedPeers() int

func (*Host) FetchBlocksByRange

func (h *Host) FetchBlocksByRange(
	ctx context.Context,
	peerID peer.ID,
	startSlot, count uint64,
) (blocks []*types.SignedBlock, err error)

func (*Host) FetchBlocksByRangeWithRetry

func (h *Host) FetchBlocksByRangeWithRetry(
	ctx context.Context,
	excluded map[peer.ID]bool,
	startSlot, count uint64,
) ([]*types.SignedBlock, peer.ID, error)

FetchBlocksByRangeWithRetry asks peers outside excluded for the range, picked by score and preferring those whose status covers its last slot, and returns the first non-empty response with the peer that served it. Each peer that fails is added to excluded, so a caller fetching several batches can keep it for all of them.

func (*Host) FetchBlocksByRoot

func (h *Host) FetchBlocksByRoot(ctx context.Context, peerID peer.ID, roots [][32]byte) (blocks []*types.SignedBlock, err error)

func (*Host) FetchBlocksByRootBatchWithRetry

func (h *Host) FetchBlocksByRootBatchWithRetry(ctx context.Context, roots [][32]byte) ([]*types.SignedBlock, [][32]byte, error)

func (*Host) FetchBlocksByRootWithRetry

func (h *Host) FetchBlocksByRootWithRetry(ctx context.Context, roots [][32]byte) ([]*SignedBlockResult, error)

func (*Host) JoinTopic

func (h *Host) JoinTopic(topic string) error

func (*Host) LibP2PHost

func (h *Host) LibP2PHost() libp2phost.Host

func (*Host) MeshPeerCount

func (h *Host) MeshPeerCount() int

func (*Host) PeerID

func (h *Host) PeerID() peer.ID

func (*Host) Peers

func (h *Host) Peers() []peer.ID

func (*Host) PublishAggregatedAttestation

func (h *Host) PublishAggregatedAttestation(ctx context.Context, agg *types.SignedAggregatedAttestation) error

func (*Host) PublishAttestation

func (h *Host) PublishAttestation(ctx context.Context, att *types.SignedAttestation, committeeCount uint64) error

func (*Host) PublishBlock

func (h *Host) PublishBlock(ctx context.Context, block *types.SignedBlock) error

func (*Host) ReannounceSubscriptions

func (h *Host) ReannounceSubscriptions() error

func (*Host) RegisterReqRespHandlers

func (h *Host) RegisterReqRespHandlers(
	statusFn func() *StatusMessage,
	blockByRootFn func(root [32]byte) *types.SignedBlock,
	currentSlotFn func() uint64,
	blocksInRangeFn func(startSlot, count uint64) ([]*types.SignedBlock, bool),
)

func (*Host) SendStatusRequest

func (h *Host) SendStatusRequest(ctx context.Context, peerID peer.ID, ourStatus *StatusMessage) (*StatusMessage, error)

func (*Host) StartBootnodeRedial

func (h *Host) StartBootnodeRedial(ctx context.Context, addrs []multiaddr.Multiaddr)

func (*Host) StartGossipListeners

func (h *Host) StartGossipListeners(handler MessageHandler)

func (*Host) TopicMeshSizes

func (h *Host) TopicMeshSizes() map[string]int

type MessageHandler

type MessageHandler interface {
	OnBlock(block *types.SignedBlock)
	OnGossipAttestation(att *types.SignedAttestation)
	OnGossipAggregatedAttestation(agg *types.SignedAggregatedAttestation)

	// The Validate methods run on pubsub's validation goroutines, concurrently
	// with each other and with the node's own loop.
	ValidateGossipBlock(block *types.SignedBlock) Verdict
	ValidateGossipAttestation(att *types.SignedAttestation) Verdict
	ValidateGossipAggregatedAttestation(agg *types.SignedAggregatedAttestation) Verdict
}

type PeerStore

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

func NewPeerStore

func NewPeerStore() *PeerStore

func (*PeerStore) AddNew

func (ps *PeerStore) AddNew(id peer.ID) bool

AddNew tracks a peer at the initial score and reports whether it was new.

func (*PeerStore) AllPeers

func (ps *PeerStore) AllPeers() []peer.ID

func (*PeerStore) Count

func (ps *PeerStore) Count() int

func (*PeerStore) RecordRequest

func (ps *PeerStore) RecordRequest(id peer.ID, success bool)

RecordRequest adjusts a tracked peer's score for one block request. The score gauge changes under the same lock as the peer set, so a peer removed concurrently cannot leave its series behind.

func (*PeerStore) Remove

func (ps *PeerStore) Remove(id peer.ID)

func (*PeerStore) SelectPeer

func (ps *PeerStore) SelectPeer(exclude map[peer.ID]bool, minSlot uint64) peer.ID

SelectPeer picks a peer outside exclude at random, weighted by score. With minSlot above zero it prefers peers whose reported head reaches minSlot and falls back to any peer when none does, since a status can be stale.

func (*PeerStore) UpdateStatus

func (ps *PeerStore) UpdateStatus(id peer.ID, headSlot uint64)

UpdateStatus records the head a tracked peer reported.

type SignedBlockResult

type SignedBlockResult struct {
	Root  [32]byte
	Block []*types.SignedBlock
	Err   error
}

type StatusMessage

type StatusMessage struct {
	FinalizedRoot [32]byte
	FinalizedSlot uint64
	HeadRoot      [32]byte
	HeadSlot      uint64
}

func (*StatusMessage) MarshalSSZ

func (s *StatusMessage) MarshalSSZ() []byte

func (*StatusMessage) UnmarshalSSZ

func (s *StatusMessage) UnmarshalSSZ(buf []byte) error

type Verdict

type Verdict int

Verdict is the node's decision on a received gossip message, made before pubsub relays it.

const (
	// Accept delivers the message to the node and relays it.
	Accept Verdict = iota
	// Ignore drops the message without blaming the sender: it may be valid to
	// a peer with a different view, or later.
	Ignore
	// Reject drops the message as invalid for every node.
	Reject
)

Jump to

Keyboard shortcuts

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