p2p

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2019 License: LGPL-3.0 Imports: 35 Imported by: 0

Documentation

Overview

Package p2p provides common peer-to-peer communications helper methods and definitions.

Index

Constants

This section is empty.

Variables

View Source
var (
	// StreamHeaderProtocolNames represents all stream header protocol names.
	StreamHeaderProtocolNames = []string{
		"pub_transaction",
		"req_config",
		"req_best_transaction",
		"req_transaction",
		"req_genesis_hash",
		"req_transaction_children_hashes",
	}

	// BootstrapNodes represents all default bootstrap nodes on the given network.
	BootstrapNodes = []string{
		"/ip4/108.41.124.60/tcp/3030/ipfs/QmWy8fZPX4hnTmXtFzgUTa8ZGceHhdhUEj3wonj1r3bMEG",
	}

	// WorkingHost is the current global routed host.
	WorkingHost *routed.RoutedHost

	// NodePort is the current node port
	NodePort = 3030

	// ErrTimedOut is an error definition representing a timeout.
	ErrTimedOut = errors.New("timed out")
)
View Source
var (
	// ErrNoWorkingHost represents an error describing a WorkingHost value of nil.
	ErrNoWorkingHost = errors.New("no working host")

	// ErrNilHash defines an error describing a situation in which a message has no hash.
	ErrNilHash = errors.New("hash not set")

	// ErrNoAvailablePeers defines an error describing an available peer sampling set with a length of 0.
	ErrNoAvailablePeers = errors.New("no available peers")
)

Functions

func BootstrapConfig

func BootstrapConfig(ctx context.Context, host *routed.RoutedHost, bootstrapAddress string, network string) (*config.DagConfig, error)

BootstrapConfig bootstraps a dag config to the list of bootstrap nodes.

func BootstrapDht

func BootstrapDht(ctx context.Context, host host.Host) (*dht.IpfsDHT, error)

BootstrapDht bootstraps the WorkingDHT to the list of bootstrap nodes.

func BroadcastDht

func BroadcastDht(ctx context.Context, host *routed.RoutedHost, message []byte, streamProtocol string, dagIdentifier string) error

BroadcastDht attempts to send a given message to all nodes in a dht at a given endpoint.

func BroadcastDhtResult

func BroadcastDhtResult(ctx context.Context, host *routed.RoutedHost, message []byte, streamProtocol string, dagIdentifier string, nPeers int) ([][]byte, error)

BroadcastDhtResult send a given message to all nodes in a dht, and returns the result from each node.

func GetBestBootstrapAddress

func GetBestBootstrapAddress(ctx context.Context, host *routed.RoutedHost) string

GetBestBootstrapAddress attempts to fetch the best bootstrap node.

func GetStreamHeaderProtocolPath

func GetStreamHeaderProtocolPath(network string, streamProtocol StreamHeaderProtocol) string

GetStreamHeaderProtocolPath attempts to determine the libp2p stream header protocol URI from a given stream protocol and network.

func NewHost

func NewHost(ctx context.Context, port int) (*routed.RoutedHost, error)

NewHost initializes a new libp2p host with the given context.

Types

type Client

type Client struct {
	Network string `json:"network"` // Active network

	Validator *validator.Validator // Validator
}

Client represents an active p2p peer, that of which is serving a list of available stream header protocol paths.

func NewClient

func NewClient(network string, validator *validator.Validator) *Client

NewClient initializes a new client

func (*Client) HandlePubTransaction

func (client *Client) HandlePubTransaction(stream inet.Stream)

HandlePubTransaction handles the

func (*Client) HandleReceiveBestTransactionRequest

func (client *Client) HandleReceiveBestTransactionRequest(stream inet.Stream)

HandleReceiveBestTransactionRequest handle a new stream requesting for the best transaction hash.

func (*Client) HandleReceiveConfigRequest

func (client *Client) HandleReceiveConfigRequest(stream inet.Stream)

HandleReceiveConfigRequest handles a new stream requesting the working dag config.

func (*Client) HandleReceiveGenesisHashRequest

func (client *Client) HandleReceiveGenesisHashRequest(stream inet.Stream)

HandleReceiveGenesisHashRequest handles a new stream requesting for the genesis hash of the working dag.

func (*Client) HandleReceiveTransaction

func (client *Client) HandleReceiveTransaction(stream inet.Stream)

HandleReceiveTransaction handles a new stream sending a transaction.

func (*Client) HandleReceiveTransactionChildHashesRequest

func (client *Client) HandleReceiveTransactionChildHashesRequest(stream inet.Stream)

HandleReceiveTransactionChildHashesRequest handles a new stream requesting for the child hashes of a given transaction.

func (*Client) HandleReceiveTransactionRequest

func (client *Client) HandleReceiveTransactionRequest(stream inet.Stream)

HandleReceiveTransactionRequest handles a new stream requesting transaction metadata with a given hash.

func (*Client) PublishTransaction

func (client *Client) PublishTransaction(ctx context.Context, transaction *types.Transaction) error

PublishTransaction publishes a given transaction.

func (*Client) RequestBestTransactionHash

func (client *Client) RequestBestTransactionHash(ctx context.Context, nPeers int) (common.Hash, error)

RequestBestTransactionHash returns the average best tx hash between nPeers.

func (*Client) RequestTransactionChildren

func (client *Client) RequestTransactionChildren(ctx context.Context, parentHash common.Hash, nPeers int) ([]common.Hash, error)

RequestTransactionChildren requests the children of a transaction from a sampling set of nPeers size.

func (*Client) RequestTransactionWithHash

func (client *Client) RequestTransactionWithHash(ctx context.Context, hash common.Hash, nPeers int) (*types.Transaction, error)

RequestTransactionWithHash requests a given transaction with a given hash from the network. Returns best response from peer sampling set nPeers.

func (*Client) StartIntermittentSync

func (client *Client) StartIntermittentSync(ctx context.Context, duration time.Duration)

StartIntermittentSync syncs the dag with a given context and duration.

func (*Client) StartServingStream

func (client *Client) StartServingStream(streamHeaderProtocolPath string, handler func(inet.Stream)) error

StartServingStream starts serving a stream on a given header protocol path.

func (*Client) StartServingStreams

func (client *Client) StartServingStreams(network string) error

StartServingStreams attempts to start serving all necessary streams

func (*Client) SyncBestTransaction

func (client *Client) SyncBestTransaction(ctx context.Context, remoteBestTransactionHash common.Hash) error

SyncBestTransaction syncs the best local and remote transactions.

func (*Client) SyncDag

func (client *Client) SyncDag(ctx context.Context) error

SyncDag syncs the working dag.

func (*Client) SyncGenesis

func (client *Client) SyncGenesis(ctx context.Context) error

SyncGenesis syncs the local genesis transaction set for the working dag.

type StreamHeaderProtocol

type StreamHeaderProtocol int

StreamHeaderProtocol represents the stream protocol type enum.

const (
	PublishTransaction StreamHeaderProtocol = iota

	RequestConfig

	RequestBestTransaction

	RequestTransaction

	RequestGenesisHash

	RequestChildHashes
)

Stream header protocol definitions

Jump to

Keyboard shortcuts

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