p2p

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: MIT Imports: 42 Imported by: 2

README

P2P

Direct connections

If you are running multiple nodes in the local network and exposing all of them is not convenient there is an option to setup network manually, by making couple of nodes publicly available and the rest connected to them directly.

Get network id
  • get it with grpcurl

grpcurl -plaintext 127.0.0.1:9092 spacemesh.v1.DebugService.NetworkInfo

{
  "id": "12D3KooWRfy4Sj4rDHDuBaYw3Mg5d2puwiCyqBCWMziFquaGQ5g8"
}
  • get it from stored data

cat ~/spacemesh/p2p/p2p.key

{"Key":"CAESQAQN38GXvr+L+G+/JWoimpqPBK7I6INe+PKYA+hRJg0I65Q3IPK49Ii9dcnC3+UqB+jMEL16sqDfUubxTs62rZU=","ID":"12D3KooWRfy4Sj4rDHDuBaYw3Mg5d2puwiCyqBCWMziFquaGQ5g8"}
Configuration for public node

Public node should have higher peer limits to help with network connectivity and a list of botnodes. Direct connections should be reciprocal, otherwise public node may prune your private node if overloaded.

Setup more than one public node, and perform rolling upgrades or restart for them if needed.

{
    "p2p": {
        "min-peers": 30,
        "low-peers": 60,
        "high-peers": 100,
        "p2p-disable-legacy-discovery": true,
        "direct": [
            "/ip4/0.0.0.0/tcp/6000/p2p/12D3KooWRkBh6QayKLb1pDRJGMHE94Lix4ZBVh2BJJeX6mghk8VH"
        ],
        "bootnodes": [
            "/dns4/mainnet-bootnode-10.spacemesh.network/tcp/5000/p2p/12D3KooWHK5m83sNj2eNMJMGAngcS9gBja27ho83t79Q2CD4iRjQ",
            "/dns4/mainnet-bootnode-11.spacemesh.network/tcp/5000/p2p/12D3KooWFrCDS8tc29nxJEYf4sKFXhXw7wMSdhQP4S7tsbfh6ngn"
        ]
    }
}
Configuration for private node

Set min-peers to the number of peers in the config and disable-dht. low-peers and high-peers should not be lower than min-peers.

{
    "p2p": {
        "listen": "/ip4/0.0.0.0/tcp/6000",
        "min-peers": 1,
        "low-peers": 10,
        "high-peers": 20,
        "p2p-disable-legacy-discovery": true,
        "disable-dht": true,
        "bootnodes": [],
        "direct": [
            "/ip4/0.0.0.0/tcp/7513/p2p/12D3KooWRfy4Sj4rDHDuBaYw3Mg5d2puwiCyqBCWMziFquaGQ5g8"
        ]
    }
}
Expected result

Public node will maintain many open connections

ss -npO4 | rg spacemesh | rg 7513 | rg ESTAB | wc -l 52

Private will connect only to the specified public node:

ss -npO4 | rg spacemesh | rg 6000

tcp   ESTAB      0      0              127.0.0.1:7513        127.0.0.1:6000  users:(("go-spacemesh",pid=39165,fd=11)) 
tcp   ESTAB      0      0              127.0.0.1:6000        127.0.0.1:7513  users:(("go-spacemesh",pid=39202,fd=47))

Documentation

Index

Constants

View Source
const (
	PublicReachability  = "public"
	PrivateReachability = "private"
)

Variables

This section is empty.

Functions

func EnsureIdentity added in v1.0.0

func EnsureIdentity(dir string) (crypto.PrivKey, error)

EnsureIdentity generates an identity key file in given directory.

func IsNoPeer added in v1.0.0

func IsNoPeer(p Peer) bool

IsNoPeer checks if it's any peer.

func PrettyIdentityInfoFromDir added in v1.0.0

func PrettyIdentityInfoFromDir(dir string) (string, error)

PrettyIdentityInfoFromDir returns a printable ID from a given identity directory.

Types

type Config added in v1.0.0

type Config struct {
	DataDir            string
	LogLevel           log.Level
	GracePeersShutdown time.Duration
	MaxMessageSize     int

	// see https://lwn.net/Articles/542629/ for reuseport explanation
	DisableReusePort         bool        `mapstructure:"disable-reuseport"`
	DisableNatPort           bool        `mapstructure:"disable-natport"`
	DisableConnectionManager bool        `mapstructure:"disable-connection-manager"`
	DisableResourceManager   bool        `mapstructure:"disable-resource-manager"`
	DisableDHT               bool        `mapstructure:"disable-dht"`
	Flood                    bool        `mapstructure:"flood"`
	Listen                   string      `mapstructure:"listen"`
	Bootnodes                []string    `mapstructure:"bootnodes"`
	Direct                   []string    `mapstructure:"direct"`
	MinPeers                 int         `mapstructure:"min-peers"`
	LowPeers                 int         `mapstructure:"low-peers"`
	HighPeers                int         `mapstructure:"high-peers"`
	AutoscalePeers           bool        `mapstructure:"autoscale-peers"`
	AdvertiseAddress         string      `mapstructure:"advertise-address"`
	AcceptQueue              int         `mapstructure:"p2p-accept-queue"`
	Metrics                  bool        `mapstructure:"p2p-metrics"`
	Bootnode                 bool        `mapstructure:"p2p-bootnode"`
	ForceReachability        string      `mapstructure:"p2p-reachability"`
	EnableHolepunching       bool        `mapstructure:"p2p-holepunching"`
	DisableLegacyDiscovery   bool        `mapstructure:"p2p-disable-legacy-discovery"`
	PrivateNetwork           bool        `mapstructure:"p2p-private-network"`
	RelayServer              RelayServer `mapstructure:"relay-server"`
}

Config for all things related to p2p layer.

func DefaultConfig added in v1.0.0

func DefaultConfig() Config

DefaultConfig config.

func (*Config) Validate added in v1.0.7

func (cfg *Config) Validate() error

type Host added in v1.0.0

type Host struct {
	host.Host
	*pubsub.PubSub
	// contains filtered or unexported fields
}

Host is a conveniency wrapper for all p2p related functionality required to run a full spacemesh node.

func New

func New(_ context.Context, logger log.Log, cfg Config, prologue []byte, opts ...Opt) (*Host, error)

New initializes libp2p host configured for spacemesh.

func Upgrade added in v1.0.0

func Upgrade(h host.Host, opts ...Opt) (*Host, error)

Upgrade creates Host instance from host.Host.

func (*Host) GetPeers added in v1.0.0

func (fh *Host) GetPeers() []Peer

GetPeers returns connected peers.

func (*Host) PeerCount added in v1.0.0

func (fh *Host) PeerCount() uint64

PeerCount returns number of connected peers.

func (*Host) Start added in v1.0.0

func (fh *Host) Start() error

func (*Host) Stop added in v1.0.0

func (fh *Host) Stop() error

Stop background workers and release external resources.

type Opt added in v1.0.0

type Opt func(fh *Host)

Opt is for configuring Host.

func WithConfig added in v1.0.0

func WithConfig(cfg Config) Opt

WithConfig sets Config for Host.

func WithContext added in v1.0.0

func WithContext(ctx context.Context) Opt

WithContext set context for Host.

func WithLog added in v1.0.0

func WithLog(logger log.Log) Opt

WithLog configures logger for Host.

func WithNodeReporter added in v1.0.0

func WithNodeReporter(reporter func()) Opt

WithNodeReporter updates reporter that is notified every time when node added or removed a peer.

type Peer

type Peer = peer.ID

Peer is an alias to libp2p's peer.ID.

const NoPeer Peer = ""

NoPeer is used when peer doesn't matter.

type RelayServer added in v1.0.7

type RelayServer struct {
	Enable       bool          `mapstructure:"enable"`
	Reservations int           `mapstructure:"reservations"`
	TTL          time.Duration `mapstructure:"ttl"`
}

Directories

Path Synopsis
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
nolint
nolint
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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