libp2p

package
v0.0.0-...-20ef9fc Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0, MIT Imports: 26 Imported by: 0

README

Libp2p Endpoint

Author: Guillaume Michel

Libp2pEndpoint is a message endpoint using Libp2p to exchange messages between Kademlia nodes over a network. It makes use of the Libp2p peerstore to record peers. The implementation is multi thread, as it is a requirement from Libp2p.

When sending a Kademlia request, a new go routine is created to send the request and wait for the response. Once the response is received, the go routine will add a new Action to handle the received response to the Scheduler's event queue and dies. The single worker will pick the response handling Action from the Scheduler once it is available.

When in Server mode, Libp2p stream handlers are added to the Libp2p host. Once a new request is caught by the Libp2p stream handler, it is sent to the Scheduler's event queue, and handled by the single worker.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotPeerAddrInfo         = errors.New("not peer.AddrInfo")
	ErrRequirePeerID           = errors.New("Libp2pEndpoint requires peer.ID")
	ErrRequireProtoKadMessage  = errors.New("Libp2pEndpoint requires ProtoKadMessage")
	ErrRequireProtoKadResponse = errors.New("Libp2pEndpoint requires ProtoKadResponseMessage")
)
View Source
var (
	Message_MessageType_name = map[int32]string{
		0: "PUT_VALUE",
		1: "GET_VALUE",
		2: "ADD_PROVIDER",
		3: "GET_PROVIDERS",
		4: "FIND_NODE",
		5: "PING",
	}
	Message_MessageType_value = map[string]int32{
		"PUT_VALUE":     0,
		"GET_VALUE":     1,
		"ADD_PROVIDER":  2,
		"GET_PROVIDERS": 3,
		"FIND_NODE":     4,
		"PING":          5,
	}
)

Enum value maps for Message_MessageType.

View Source
var (
	Message_ConnectionType_name = map[int32]string{
		0: "NOT_CONNECTED",
		1: "CONNECTED",
		2: "CAN_CONNECT",
		3: "CANNOT_CONNECT",
	}
	Message_ConnectionType_value = map[string]int32{
		"NOT_CONNECTED":  0,
		"CONNECTED":      1,
		"CAN_CONNECT":    2,
		"CANNOT_CONNECT": 3,
	}
)

Enum value maps for Message_ConnectionType.

View Source
var ErrNoValidAddresses = errors.New("no valid addresses")
View Source
var File_message_proto protoreflect.FileDescriptor

Functions

func ParsePeers

func ParsePeers(pbps []*Message_Peer) []kad.NodeInfo[key.Key256, multiaddr.Multiaddr]

func ReadMsg

func WriteMsg

func WriteMsg(s network.Stream, msg protoreflect.ProtoMessage) error

Types

type AddrInfo

type AddrInfo struct {
	peer.AddrInfo
	// contains filtered or unexported fields
}

func NewAddrInfo

func NewAddrInfo(ai peer.AddrInfo) *AddrInfo

func PBPeerToPeerInfo

func PBPeerToPeerInfo(pbp *Message_Peer) (*AddrInfo, error)

func (AddrInfo) Addresses

func (ai AddrInfo) Addresses() []multiaddr.Multiaddr

func (AddrInfo) ID

func (ai AddrInfo) ID() kad.NodeID[key.Key256]

func (AddrInfo) Key

func (ai AddrInfo) Key() key.Key256

func (AddrInfo) PeerID

func (ai AddrInfo) PeerID() *PeerID

func (AddrInfo) String

func (ai AddrInfo) String() string

type DialReportFn

type DialReportFn func(context.Context, bool)

type Libp2pEndpoint

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

func NewLibp2pEndpoint

func NewLibp2pEndpoint(ctx context.Context, host host.Host,
	sched event.Scheduler,
) *Libp2pEndpoint

func (*Libp2pEndpoint) AddRequestHandler

func (e *Libp2pEndpoint) AddRequestHandler(protoID address.ProtocolID,
	req kad.Message, reqHandler endpoint.RequestHandlerFn[key.Key256],
) error

func (*Libp2pEndpoint) AsyncDialAndReport

func (e *Libp2pEndpoint) AsyncDialAndReport(ctx context.Context,
	id kad.NodeID[key.Key256], reportFn DialReportFn,
) error

func (*Libp2pEndpoint) Connectedness

func (e *Libp2pEndpoint) Connectedness(id kad.NodeID[key.Key256]) (endpoint.Connectedness, error)

func (*Libp2pEndpoint) DialPeer

func (e *Libp2pEndpoint) DialPeer(ctx context.Context, id kad.NodeID[key.Key256]) error

func (*Libp2pEndpoint) Key

func (e *Libp2pEndpoint) Key() key.Key256

func (*Libp2pEndpoint) MaybeAddToPeerstore

func (e *Libp2pEndpoint) MaybeAddToPeerstore(ctx context.Context,
	id kad.NodeInfo[key.Key256, multiaddr.Multiaddr], ttl time.Duration,
) error

func (*Libp2pEndpoint) NetworkAddress

func (e *Libp2pEndpoint) NetworkAddress(n kad.NodeID[key.Key256]) (kad.NodeInfo[key.Key256, multiaddr.Multiaddr], error)

func (*Libp2pEndpoint) PeerInfo

func (e *Libp2pEndpoint) PeerInfo(id kad.NodeID[key.Key256]) (peer.AddrInfo, error)

func (*Libp2pEndpoint) RemoveRequestHandler

func (e *Libp2pEndpoint) RemoveRequestHandler(protoID address.ProtocolID)

func (*Libp2pEndpoint) SendRequestHandleResponse

func (e *Libp2pEndpoint) SendRequestHandleResponse(ctx context.Context,
	protoID address.ProtocolID, n kad.NodeID[key.Key256], req kad.Message,
	resp kad.Message, timeout time.Duration,
	responseHandlerFn endpoint.ResponseHandlerFn[key.Key256, multiaddr.Multiaddr],
) error

type Message

type Message struct {

	// defines what type of message it is
	Type Message_MessageType `protobuf:"varint,1,opt,name=type,proto3,enum=dht.pb.Message_MessageType" json:"type,omitempty"`
	// defines what coral cluster level this query/response belongs to
	// in case we want to implement coral's cluster rings in the future
	ClusterLevelRaw int32 `protobuf:"varint,10,opt,name=clusterLevelRaw,proto3" json:"clusterLevelRaw,omitempty"` // NOT USED
	// Used to specify the key associated with this message
	// PUT_VALUE, GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
	Key []byte `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"`
	// Used to return a value
	// PUT_VALUE, GET_VALUE
	Record *Record `protobuf:"bytes,3,opt,name=record,proto3" json:"record,omitempty"`
	// Used to return peers closer to a key in a query
	// GET_VALUE, GET_PROVIDERS, FIND_NODE
	CloserPeers []*Message_Peer `protobuf:"bytes,8,rep,name=closerPeers,proto3" json:"closerPeers,omitempty"`
	// Used to return Providers
	// GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
	ProviderPeers []*Message_Peer `protobuf:"bytes,9,rep,name=providerPeers,proto3" json:"providerPeers,omitempty"`
	// contains filtered or unexported fields
}

func FindPeerRequest

func FindPeerRequest(p *PeerID) *Message

func FindPeerResponse

func FindPeerResponse(peers []kad.NodeID[key.Key256], e endpoint.NetworkedEndpoint[key.Key256, multiaddr.Multiaddr]) *Message

func (*Message) CloserNodes

func (msg *Message) CloserNodes() []kad.NodeInfo[key.Key256, multiaddr.Multiaddr]

func (*Message) Descriptor deprecated

func (*Message) Descriptor() ([]byte, []int)

Deprecated: Use Message.ProtoReflect.Descriptor instead.

func (*Message) EmptyResponse

func (msg *Message) EmptyResponse() kad.Response[key.Key256, multiaddr.Multiaddr]

func (*Message) GetCloserPeers

func (x *Message) GetCloserPeers() []*Message_Peer

func (*Message) GetClusterLevelRaw

func (x *Message) GetClusterLevelRaw() int32

func (*Message) GetKey

func (x *Message) GetKey() []byte

func (*Message) GetProviderPeers

func (x *Message) GetProviderPeers() []*Message_Peer

func (*Message) GetRecord

func (x *Message) GetRecord() *Record

func (*Message) GetType

func (x *Message) GetType() Message_MessageType

func (*Message) ProtoMessage

func (*Message) ProtoMessage()

func (*Message) ProtoReflect

func (x *Message) ProtoReflect() protoreflect.Message

func (*Message) Reset

func (x *Message) Reset()

func (*Message) String

func (x *Message) String() string

func (*Message) Target

func (msg *Message) Target() key.Key256

type Message_ConnectionType

type Message_ConnectionType int32
const (
	// sender does not have a connection to peer, and no extra information
	// (default)
	Message_NOT_CONNECTED Message_ConnectionType = 0
	// sender has a live connection to peer
	Message_CONNECTED Message_ConnectionType = 1
	// sender recently connected to peer
	Message_CAN_CONNECT Message_ConnectionType = 2
	// sender recently tried to connect to peer repeatedly but failed to connect
	// ("try" here is loose, but this should signal "made strong effort,
	// failed")
	Message_CANNOT_CONNECT Message_ConnectionType = 3
)

func (Message_ConnectionType) Descriptor

func (Message_ConnectionType) Enum

func (Message_ConnectionType) EnumDescriptor deprecated

func (Message_ConnectionType) EnumDescriptor() ([]byte, []int)

Deprecated: Use Message_ConnectionType.Descriptor instead.

func (Message_ConnectionType) Number

func (Message_ConnectionType) String

func (x Message_ConnectionType) String() string

func (Message_ConnectionType) Type

type Message_MessageType

type Message_MessageType int32
const (
	Message_PUT_VALUE     Message_MessageType = 0
	Message_GET_VALUE     Message_MessageType = 1
	Message_ADD_PROVIDER  Message_MessageType = 2
	Message_GET_PROVIDERS Message_MessageType = 3
	Message_FIND_NODE     Message_MessageType = 4
	Message_PING          Message_MessageType = 5
)

func (Message_MessageType) Descriptor

func (Message_MessageType) Enum

func (Message_MessageType) EnumDescriptor deprecated

func (Message_MessageType) EnumDescriptor() ([]byte, []int)

Deprecated: Use Message_MessageType.Descriptor instead.

func (Message_MessageType) Number

func (Message_MessageType) String

func (x Message_MessageType) String() string

func (Message_MessageType) Type

type Message_Peer

type Message_Peer struct {

	// ID of a given peer.
	Id []byte `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
	// multiaddrs for a given peer
	Addrs [][]byte `protobuf:"bytes,2,rep,name=addrs,proto3" json:"addrs,omitempty"`
	// used to signal the sender's connection capabilities to the peer
	Connection Message_ConnectionType `protobuf:"varint,3,opt,name=connection,proto3,enum=dht.pb.Message_ConnectionType" json:"connection,omitempty"`
	// contains filtered or unexported fields
}

func NodeIDsToPbPeers

func NodeIDsToPbPeers(peers []kad.NodeID[key.Key256], e endpoint.NetworkedEndpoint[key.Key256, multiaddr.Multiaddr]) []*Message_Peer

func (*Message_Peer) Descriptor deprecated

func (*Message_Peer) Descriptor() ([]byte, []int)

Deprecated: Use Message_Peer.ProtoReflect.Descriptor instead.

func (*Message_Peer) GetAddrs

func (x *Message_Peer) GetAddrs() [][]byte

func (*Message_Peer) GetConnection

func (x *Message_Peer) GetConnection() Message_ConnectionType

func (*Message_Peer) GetId

func (x *Message_Peer) GetId() []byte

func (*Message_Peer) ProtoMessage

func (*Message_Peer) ProtoMessage()

func (*Message_Peer) ProtoReflect

func (x *Message_Peer) ProtoReflect() protoreflect.Message

func (*Message_Peer) Reset

func (x *Message_Peer) Reset()

func (*Message_Peer) String

func (x *Message_Peer) String() string

type PeerID

type PeerID struct {
	peer.ID
}

func NewPeerID

func NewPeerID(p peer.ID) *PeerID

func (PeerID) Key

func (id PeerID) Key() key.Key256

func (PeerID) NodeID

func (id PeerID) NodeID() kad.NodeID[key.Key256]

type ProtoKadMessage

type ProtoKadMessage interface {
	proto.Message
}

type ProtoKadRequestMessage

type ProtoKadRequestMessage[K kad.Key[K], A kad.Address[A]] interface {
	ProtoKadMessage
	kad.Request[K, A]
}

type ProtoKadResponseMessage

type ProtoKadResponseMessage[K kad.Key[K], A kad.Address[A]] interface {
	ProtoKadMessage
	kad.Response[K, A]
}

type Record

type Record struct {

	// The key that references this record
	Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
	// The actual value this record is storing
	Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
	// Time the record was received, set by receiver
	TimeReceived string `protobuf:"bytes,5,opt,name=timeReceived,proto3" json:"timeReceived,omitempty"`
	// The original publisher of the record
	// Not used at the moment, but may be useful for future developments
	Publisher []byte `protobuf:"bytes,666,opt,name=publisher,proto3" json:"publisher,omitempty"`
	// The remaining TTL of the record, in seconds
	// Not used at the moment, but may be useful for future developments
	Ttl uint32 `protobuf:"varint,777,opt,name=ttl,proto3" json:"ttl,omitempty"`
	// contains filtered or unexported fields
}

Record represents a dht record that contains a value for a key value pair

func (*Record) Descriptor deprecated

func (*Record) Descriptor() ([]byte, []int)

Deprecated: Use Record.ProtoReflect.Descriptor instead.

func (*Record) GetKey

func (x *Record) GetKey() []byte

func (*Record) GetPublisher

func (x *Record) GetPublisher() []byte

func (*Record) GetTimeReceived

func (x *Record) GetTimeReceived() string

func (*Record) GetTtl

func (x *Record) GetTtl() uint32

func (*Record) GetValue

func (x *Record) GetValue() []byte

func (*Record) ProtoMessage

func (*Record) ProtoMessage()

func (*Record) ProtoReflect

func (x *Record) ProtoReflect() protoreflect.Message

func (*Record) Reset

func (x *Record) Reset()

func (*Record) String

func (x *Record) String() string

Jump to

Keyboard shortcuts

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