Skip to content

Commit

Permalink
feat(network): implementing connection manager (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Oct 24, 2023
1 parent 34eff4f commit 57af41e
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 291 deletions.
13 changes: 7 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func SaveTestnetConfig(path string, numValidators int) error {
"/ip4/0.0.0.0/tcp/21777", "/ip4/0.0.0.0/udp/21777/quic-v1",
"/ip6/::/tcp/21777", "/ip6/::/udp/21777/quic-v1",
}
conf.Network.Bootstrap.Addresses = []string{
conf.Network.BootstrapAddrs = []string{
"/ip4/94.101.184.118/tcp/21777/p2p/12D3KooWCwQZt8UriVXobQHPXPR8m83eceXVoeT6brPNiBHomebc",
"/ip4/94.101.184.118/udp/21777/quic-v1/p2p/12D3KooWCwQZt8UriVXobQHPXPR8m83eceXVoeT6brPNiBHomebc",
"/ip4/172.104.46.145/tcp/21777/p2p/12D3KooWNYD4bB82YZRXv6oNyYPwc5ozabx2epv75ATV3D8VD3Mq",
Expand All @@ -119,8 +119,8 @@ func SaveTestnetConfig(path string, numValidators int) error {
"/ip6/2001:bc8:700:8017::1/tcp/21777/p2p/12D3KooWDF8a4goNCHriP1y922y4jagaPwHdX4eSrG5WtQpjzS6k",
"/ip6/2001:bc8:700:8017::1/udp/21777/quic-v1/p2p/12D3KooWDF8a4goNCHriP1y922y4jagaPwHdX4eSrG5WtQpjzS6k",
}
conf.Network.Bootstrap.MinThreshold = 4
conf.Network.Bootstrap.MaxThreshold = 8
conf.Network.MinConns = 8
conf.Network.MaxConns = 16
conf.Network.EnableRelay = true
conf.Network.RelayAddrs = []string{
"/ip4/139.162.153.10/tcp/4002/p2p/12D3KooWNR79jqHVVNhNVrqnDbxbJJze4VjbEsBjZhz6mkvinHAN",
Expand Down Expand Up @@ -148,10 +148,11 @@ func SaveLocalnetConfig(path string, numValidators int) error {
conf := DefaultConfig()
conf.Node.NumValidators = numValidators
conf.Network.Listens = []string{}
conf.Network.EnableRelay = false
conf.Network.EnableNAT = false
conf.Network.Bootstrap.Addresses = []string{}
conf.Network.Bootstrap.MinThreshold = 4
conf.Network.Bootstrap.MaxThreshold = 8
conf.Network.BootstrapAddrs = []string{}
conf.Network.MinConns = 0
conf.Network.MaxConns = 0
conf.GRPC.Enable = true
conf.GRPC.Listen = "[::]:0"
conf.GRPC.Gateway.Enable = true
Expand Down
45 changes: 19 additions & 26 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,27 @@
# `network` contains configuration options for the network module, which manages communication between nodes.
[network]

# `network_key` specifies the private key filename to use for node authentication and encryption in the p2p protocol.
## network_key = "network_key"

# `listens` specifies the addresses and ports where the node will listen for incoming connections from other nodes.
## listens = ["/ip4/0.0.0.0/tcp/21888", "/ip6/::/tcp/21888", "/ip4/0.0.0.0/udp/21888/quic-v1", "/ip6/::/udp/21888/quic-v1"]

# `network_key` specifies the private key filename to use for node authentication and encryption in the p2p protocol.
## network_key = "network_key"
# `relay_addresses` provides the necessary relay addresses. These should be specified if 'enable_relay' is 'true'.
# Note: this parameter will be ignored if 'enable_relay' is 'false'.
## relay_addresses = []

# `bootstrap_addresses` is a list of peer addresses needed for peer discovery.
# These addresses are used by the Pactus node to discover and connect to other peers on the network.
## bootstrap_addresses = ["/ip4/172.104.46.145/tcp/21777/p2p/12D3KooWNYD4bB82YZRXv6oNyYPwc5ozabx2epv75ATV3D8VD3Mq"]

# `min_connections` is the minimum number of connections that the Pactus node should maintain.
# Default is 8
## min_connections = 8

# `max_connections` is the maximum number of connections that the Pactus node should maintain.
# Default is 16
## max_connections = 16

# `enable_nat` indicates whether NAT service should be enabled or not.
# NAT service allows many machines to share a single public address.
Expand All @@ -34,13 +50,9 @@

# `enable_relay` indicates whether relay service should be enabled or not.
# Relay service is a transport protocol that routes traffic between two peers over a third-party “relay” peer.
# Default is true.
# Default is false.
## enable_relay = false

# `relay_addresses` provides the necessary relay addresses. These should be specified if 'enable_relay' is 'true'.
# Note: this parameter will be ignored if 'enable_relay' is 'false'.
## relay_addresses = []

# `enable_mdns` indicates whether MDNS should be enabled or not.
# MDNS is a protocol to discover local peers quickly and efficiently.
# Default is false.
Expand All @@ -54,25 +66,6 @@
# Default is false.
## bootstrapper = false

# `network.bootstrap` contains configuration for bootstrapping the node.
[network.bootstrap]

# `addresses` is a list of peer addresses needed for peer discovery.
# These addresses are used by the Pactus node to discover and connect to other peers on the network.
## addresses = ["/ip4/172.104.46.145/tcp/21777/p2p/12D3KooWNYD4bB82YZRXv6oNyYPwc5ozabx2epv75ATV3D8VD3Mq"]

# `min_threshold` is the minimum number of connections that the Pactus node should maintain.
# Default is 8
## min_threshold = 8

# `max_threshold` is the maximum number of connections that the Pactus node should maintain.
# Default is 16
## max_threshold = 16

# `period` periodically checks to see if the threshold is maintained.
# Default is 1 minute
## period = "1m0s"

# `sync` contains configuration of sync module.
[sync]

Expand Down
25 changes: 0 additions & 25 deletions network/address.go

This file was deleted.

152 changes: 0 additions & 152 deletions network/bootstrap.go

This file was deleted.

60 changes: 27 additions & 33 deletions network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,23 @@ package network

import (
"fmt"
"time"

"github.com/multiformats/go-multiaddr"
"github.com/pactus-project/pactus/util/errors"
)

type Config struct {
Listens []string `toml:"listens"`
NetworkKey string `toml:"network_key"`
EnableNAT bool `toml:"enable_nat"`
EnableRelay bool `toml:"enable_relay"`
RelayAddrs []string `toml:"relay_addresses"`
EnableMdns bool `toml:"enable_mdns"`
EnableMetrics bool `toml:"enable_metrics"`
Bootstrapper bool `toml:"bootstrapper"`
Bootstrap *BootstrapConfig `toml:"bootstrap"`
}

// BootstrapConfig holds all configuration options related to bootstrap nodes.
type BootstrapConfig struct {
Addresses []string `toml:"addresses"`
MinThreshold int `toml:"min_threshold"`
MaxThreshold int `toml:"max_threshold"`
Period time.Duration `toml:"period"`
NetworkKey string `toml:"network_key"`
Listens []string `toml:"listens"`
RelayAddrs []string `toml:"relay_addresses"`
BootstrapAddrs []string `toml:"bootstrap_addresses"`
MinConns int `toml:"min_connections"`
MaxConns int `toml:"max_connections"`
EnableNAT bool `toml:"enable_nat"`
EnableRelay bool `toml:"enable_relay"`
EnableMdns bool `toml:"enable_mdns"`
EnableMetrics bool `toml:"enable_metrics"`
Bootstrapper bool `toml:"bootstrapper"`
}

func DefaultConfig() *Config {
Expand All @@ -41,29 +34,27 @@ func DefaultConfig() *Config {
},
}

addresses := []string{}
bootstrapAddrs := []string{}
for _, n := range nodes {
addresses = append(addresses,
bootstrapAddrs = append(bootstrapAddrs,
fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s", n.ip, n.port, n.id))
}

return &Config{
NetworkKey: "network_key",
Listens: []string{
"/ip4/0.0.0.0/tcp/21888", "/ip6/::/tcp/21888",
"/ip4/0.0.0.0/udp/21888/quic-v1", "/ip6/::/udp/21888/quic-v1",
},
NetworkKey: "network_key",
EnableNAT: true,
EnableRelay: false,
EnableMdns: false,
EnableMetrics: false,
Bootstrapper: false,
Bootstrap: &BootstrapConfig{
Addresses: addresses,
MinThreshold: 8,
MaxThreshold: 16,
Period: 1 * time.Minute,
},
RelayAddrs: []string{},
BootstrapAddrs: bootstrapAddrs,
MinConns: 8,
MaxConns: 16,
EnableNAT: true,
EnableRelay: false,
EnableMdns: false,
EnableMetrics: false,
Bootstrapper: false,
}
}

Expand All @@ -87,5 +78,8 @@ func (conf *Config) BasicCheck() error {
if err := validateAddresses(conf.RelayAddrs); err != nil {
return err
}
return validateAddresses(conf.Listens)
if err := validateAddresses(conf.Listens); err != nil {
return err
}
return validateAddresses(conf.BootstrapAddrs)
}
Loading

0 comments on commit 57af41e

Please sign in to comment.