network

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: May 10, 2023 License: AGPL-3.0, ISC Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrStringInvalidPrivateKey = "invalid private key"
	ErrStringInvalidPublicKey  = "invalid public key"
)
View Source
const (
	ProtocolBlockProposal         = "/ab/block-proposal/0.0.1"
	ProtocolUnicityCertificates   = "/ab/certificates/0.0.1"
	ProtocolBlockCertification    = "/ab/block-certification/0.0.1"
	ProtocolInputForward          = "/ab/input-forward/0.0.1"
	ProtocolLedgerReplicationReq  = "/ab/replication-req/0.0.1"
	ProtocolLedgerReplicationResp = "/ab/replication-resp/0.0.1"
	ProtocolHandshake             = "/ab/handshake/0.0.1"

	ErrStrPeerIsNil       = "peer is nil"
	ErrStrProtocolIDEmpty = "protocol ID is empty"
	ErrStrOutputChIsNil   = "output channel is nil"
	ErrStrTypeFuncIsNil   = "type func is nil"
)

Variables

View Source
var DefaultValidatorNetOptions = ValidatorNetOptions{
	ResponseChannelCapacity:          1000,
	ForwarderTimeout:                 300 * time.Millisecond,
	BlockCertificationTimeout:        300 * time.Millisecond,
	BlockProposalTimeout:             300 * time.Millisecond,
	LedgerReplicationRequestTimeout:  300 * time.Millisecond,
	LedgerReplicationResponseTimeout: 300 * time.Millisecond,
	HandshakeTimeout:                 300 * time.Millisecond,
}
View Source
var ErrNotProtoMessage = errors.New("input isn't protobuf message")
View Source
var (
	ErrPeerConfigurationIsNil = errors.New("peer configuration is nil")
)

Functions

This section is empty.

Types

type LibP2PNetwork

type LibP2PNetwork struct {
	ReceivedMsgCh chan ReceivedMessage // messages from LibP2PNetwork to other components.
	// contains filtered or unexported fields
}

func NewLibP2PNetwork

func NewLibP2PNetwork(self *Peer, capacity uint) (*LibP2PNetwork, error)

NewLibP2PNetwork creates a new libP2P network without protocols.

func NewLibP2PRootChainNetwork

func NewLibP2PRootChainNetwork(self *Peer, capacity uint, sendCertificateTimeout time.Duration) (*LibP2PNetwork, error)

func NewLibP2PValidatorNetwork

func NewLibP2PValidatorNetwork(self *Peer, opts ValidatorNetOptions) (*LibP2PNetwork, error)

NewLibP2PValidatorNetwork creates a new libp2p for a validator.

func (*LibP2PNetwork) Close

func (n *LibP2PNetwork) Close()

func (*LibP2PNetwork) ReceivedChannel

func (n *LibP2PNetwork) ReceivedChannel() <-chan ReceivedMessage

func (*LibP2PNetwork) Send

func (n *LibP2PNetwork) Send(out OutputMessage, receivers []peer.ID) error

type OutputMessage

type OutputMessage struct {
	Protocol string        // protocol to use to send the message
	Message  proto.Message // message to send
}

OutputMessage represents a message that will be sent to other nodes.

type Peer

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

Peer represents a single node in p2p network. It is a wrapper around the libp2p host.Host.

func NewPeer

func NewPeer(conf *PeerConfiguration) (*Peer, error)

NewPeer constructs a new peer node with given configuration. If no peer key is provided, it generates a random Secp256k1 key-pair and derives a new identity from it. If no transport and listen addresses are provided, the node listens to the multiaddresses "/ip4/0.0.0.0/tcp/0".

func (*Peer) Close

func (p *Peer) Close() (res error)

Close shuts down the libp2p host and related services.

func (*Peer) Configuration

func (p *Peer) Configuration() *PeerConfiguration

Configuration returns peer configuration

func (*Peer) CreateStream

func (p *Peer) CreateStream(ctx context.Context, peerID peer.ID, protocolID string) (network.Stream, error)

CreateStream opens a new stream to given peer p, and writes a libp2p protocol header with given ProtocolID.

func (*Peer) GetRandomPeerID

func (p *Peer) GetRandomPeerID() peer.ID

GetRandomPeerID returns a random peer.ID from the peerstore.

func (*Peer) ID

func (p *Peer) ID() peer.ID

ID returns the identifier associated with this Peer.

func (*Peer) MultiAddresses

func (p *Peer) MultiAddresses() []ma.Multiaddr

MultiAddresses the address associated with this Peer.

func (*Peer) Network

func (p *Peer) Network() network.Network

Network returns the Network of the Peer.

func (*Peer) PublicKey

func (p *Peer) PublicKey() (crypto.PubKey, error)

PublicKey returns the public key of the peer.

func (*Peer) RegisterProtocolHandler

func (p *Peer) RegisterProtocolHandler(protocolID string, handler network.StreamHandler)

RegisterProtocolHandler sets the protocol stream handler for given protocol.

func (*Peer) RemoveProtocolHandler

func (p *Peer) RemoveProtocolHandler(protocolID string)

RemoveProtocolHandler removes the given protocol handler.

func (*Peer) Validators

func (p *Peer) Validators() []peer.ID

type PeerConfiguration

type PeerConfiguration struct {
	Address         string       // address to listen for incoming connections. Uses libp2p multiaddress format.
	KeyPair         *PeerKeyPair // keypair for the peer.
	PersistentPeers []*PeerInfo  // a list of known peers (in case of partition node this list must contain all validators).
}

PeerConfiguration includes single peer configuration values.

type PeerInfo

type PeerInfo struct {
	Address   string // address of the peer
	PublicKey []byte // public key of the peer
}

PeerInfo contains a public key and address.

func (*PeerInfo) GetID

func (pi *PeerInfo) GetID() (peer.ID, error)

type PeerKeyPair

type PeerKeyPair struct {
	PublicKey  []byte
	PrivateKey []byte
}

PeerKeyPair contains node's public and private key.

type ProtobufReader

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

func NewProtoBufReader

func NewProtoBufReader(r io.Reader) *ProtobufReader

func (*ProtobufReader) Close

func (pr *ProtobufReader) Close() error

func (*ProtobufReader) Read

func (pr *ProtobufReader) Read(msg proto.Message) error

type ProtobufWriter

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

func NewProtoBufWriter

func NewProtoBufWriter(w io.Writer) *ProtobufWriter

func (*ProtobufWriter) Close

func (pw *ProtobufWriter) Close() error

func (*ProtobufWriter) Write

func (pw *ProtobufWriter) Write(msg interface{}) (err error)

type ReceiveProtocol

type ReceiveProtocol[T proto.Message] struct {
	// contains filtered or unexported fields
}

ReceiveProtocol is used to receive protobuf messages from other peers in the network.

func NewReceiverProtocol

func NewReceiverProtocol[T proto.Message](self *Peer, protocolID string, outCh chan<- ReceivedMessage, typeFunc TypeFunc[T]) (*ReceiveProtocol[T], error)

func (*ReceiveProtocol[T]) Close

func (p *ReceiveProtocol[T]) Close()

func (*ReceiveProtocol[T]) HandleStream

func (p *ReceiveProtocol[T]) HandleStream(s libp2pNetwork.Stream)

func (*ReceiveProtocol[T]) ID

func (p *ReceiveProtocol[T]) ID() string

type ReceivedMessage

type ReceivedMessage struct {
	From     peer.ID
	Protocol string
	Message  proto.Message
}

ReceivedMessage represents a message received over the network.

type SendProtocol

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

SendProtocol is used to send protobuf messages to other peers in the network.

func NewSendProtocol

func NewSendProtocol(self *Peer, protocolID string, timeout time.Duration) (*SendProtocol, error)

func (*SendProtocol) ID

func (p *SendProtocol) ID() string

func (*SendProtocol) Send

func (p *SendProtocol) Send(m proto.Message, receiverID peer.ID) error

type TypeFunc

type TypeFunc[T proto.Message] func() T

TypeFunc creates a new instance of protobuf message.

type ValidatorNetOptions

type ValidatorNetOptions struct {
	ResponseChannelCapacity          uint
	ForwarderTimeout                 time.Duration
	BlockCertificationTimeout        time.Duration
	BlockProposalTimeout             time.Duration
	LedgerReplicationRequestTimeout  time.Duration
	LedgerReplicationResponseTimeout time.Duration
	HandshakeTimeout                 time.Duration
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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