p2pgo

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: Apache-2.0 Imports: 7 Imported by: 2

README

Go p2p library for cess distributed storage system

GitHub license Go Reference Go Report Card

The go p2p library implementation of the CESS distributed storage network, which is customized based on go-libp2p, has many advantages, and at the same time abandons the bandwidth and space redundancy caused by multiple file backups, making nodes more focused on storage allocation Give yourself data to make it more in line with the needs of the CESS network.

📝 Reporting a Vulnerability

If you find out any system bugs or you have a better suggestions, please send an email to frode@cess.one or join CESS discord to communicate with us.

📢 Announcement

CESS test network rpc endpoints

wss://testnet-rpc0.cess.cloud/ws/
wss://testnet-rpc1.cess.cloud/ws/
wss://testnet-rpc2.cess.cloud/ws/

CESS test network bootstrap node

_dnsaddr.boot-kldr-testnet.cess.cloud

🏗 Usage

To get the package use the standard:

go get -u "github.com/CESSProject/p2p-go"

📖 Documentation

Please refer to https://pkg.go.dev/github.com/CESSProject/p2p-go

💡 Examples

The following code demonstrates how to create a p2p node and perform node discovery:

package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"strconv"
	"time"

	p2pgo "github.com/CESSProject/p2p-go"
	"github.com/CESSProject/p2p-go/config"
	"github.com/CESSProject/p2p-go/core"
	"github.com/CESSProject/p2p-go/out"
	"golang.org/x/time/rate"
)

func main() {
	var ok bool
	var node = &core.Node{}
	ctx := context.Background()
	port, err := strconv.Atoi(os.Args[1])
	if err != nil {
		fmt.Println("please enter os.Args[1] as port")
		os.Exit(1)
	}

	h1, err := p2pgo.New(
		ctx,
		p2pgo.PrivatekeyFile(".private1"),
		p2pgo.ListenPort(port),
		p2pgo.Workspace("."),
		p2pgo.BootPeers([]string{
			"_dnsaddr.boot-kldr-testnet.cess.cloud",
		}),
		p2pgo.ProtocolPrefix(config.TestnetProtocolPrefix),
	)
	if err != nil {
		panic(err)
	}
	defer h1.Close()

	node, ok = h1.(*core.Node)
	if !ok {
		panic(err)
	}

	fmt.Println(node.Addrs(), node.ID())

	node.RouteTableFindPeers(0)

	tick := time.NewTicker(time.Second * 30)
	defer tick.Stop()

	var r = rate.Every(time.Second * 3)
	var limit = rate.NewLimiter(r, 1)

	for {
		select {
		case peer, ok := <-node.GetDiscoveredPeers():
			if !ok {
				break
			}
			if limit.Allow() {
				tick.Reset(time.Second * 30)
			}
			if len(peer.Responses) == 0 {
				break
			}
			for _, v := range peer.Responses {
				log.Println("found: ", v.ID.Pretty(), v.Addrs)
			}
		case <-tick.C:
			node.RouteTableFindPeers(0)
		}
	}
}

License

Licensed under Apache 2.0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultBucketSize = func(cfg *Config) error {
	return cfg.Apply(BucketSize(100))
}

DefaultWorkSpace configures libp2p to use default work space.

View Source
var DefaultConnectionManager = func(cfg *Config) error {
	mgr, err := connmgr.NewConnManager(10, 100, connmgr.WithGracePeriod(time.Minute), connmgr.WithSilencePeriod(time.Minute))
	if err != nil {
		return err
	}
	cfg.ConnManager = mgr
	return nil
}

DefaultConnectionManager creates a default connection manager.

View Source
var DefaultListenPort = func(cfg *Config) error {
	port := 4001
	return cfg.Apply(ListenPort(port))
}

DefaultListenPort configures libp2p to use default port.

View Source
var DefaultWorkSpace = func(cfg *Config) error {
	dir, err := os.Getwd()
	if err != nil {
		return err
	}
	return cfg.Apply(Workspace(dir))
}

DefaultWorkSpace configures libp2p to use default work space.

Functions

func New

func New(ctx context.Context, opts ...Option) (*core.PeerNode, error)

New constructs a new libp2p node with the given options, falling back on reasonable defaults. The defaults are:

- If no listening port is provided, the host listens on the default port: 4001

- If no bootstrap nodes is provided, the host will run as a server

- If no protocol version is provided, The host uses the default protocol version: /kldr/1.0

- If no DHT protocol version is provided, The host uses the default DHT protocol version: /kldr/kad/1.0

To stop/shutdown the returned p2p node, the user needs to cancel the passed context and call `Close` on the returned Host.

func NewWithoutDefaults

func NewWithoutDefaults(ctx context.Context, opts ...Option) (*core.PeerNode, error)

NewWithoutDefaults constructs a new libp2p node with the given options but *without* falling back on reasonable defaults.

Warning: This function should not be considered a stable interface. We may choose to add required services at any time and, by using this function, you opt-out of any defaults we may provide.

Types

type Config

type Config = config.Config

Config describes a set of settings for a p2p peer.

type Option

type Option = config.Option

Option is a p2p config option that can be given to the p2p constructor.

var FallbackDefaults Option = func(cfg *Config) error {
	for _, def := range defaults {
		if !def.fallback(cfg) {
			continue
		}
		if err := cfg.Apply(def.opt); err != nil {
			return err
		}
	}
	return nil
}

FallbackDefaults applies default options to the libp2p node if and only if no other relevant options have been applied. will be appended to the options passed into New.

func BootPeers added in v0.0.8

func BootPeers(bootpeers []string) Option

BootPeers configuration bootstrap nodes

func BucketSize added in v0.3.4

func BucketSize(size int) Option

BucketSize configuration bucket size

func ListenPort added in v0.0.24

func ListenPort(port int) Option

ListenPort configuration listening port

func MaxConnection added in v0.3.4

func MaxConnection(low, high int) Option

MaxConnection configuration max connection

func PrivatekeyFile added in v0.0.32

func PrivatekeyFile(privatekey string) Option

PrivatekeyFile configuration privatekey file

func ProtocolPrefix added in v0.0.37

func ProtocolPrefix(protocolPrefix string) Option

PrivatekeyFile configuration privatekey file

func PublicIpv4 added in v0.1.0

func PublicIpv4(ip string) Option

PrivatekeyFile configuration privatekey file

func Version added in v0.3.4

func Version(ver string) Option

Version configuration version

func Workspace

func Workspace(workspace string) Option

Workspace configuration working directory

Directories

Path Synopsis
examples
pub
rpc
sub

Jump to

Keyboard shortcuts

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