Skip to content

Commit

Permalink
chore: fixing linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Oct 24, 2023
1 parent e1441d0 commit 4bcc433
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 39 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ 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.BootstrapAddrs = []string{}
conf.Network.MinConns = 0
Expand Down
6 changes: 3 additions & 3 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
# Note: this parameter will be ignored if 'enable_relay' is 'false'.
## relay_addresses = []

# `addresses` is a list of peer addresses needed for peer discovery.
# `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 = []
## 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
Expand All @@ -50,7 +50,7 @@

# `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

# `enable_mdns` indicates whether MDNS should be enabled or not.
Expand Down
15 changes: 3 additions & 12 deletions network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package network
import (
"fmt"

lp2ppeer "github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/pactus-project/pactus/util/errors"
)
Expand Down Expand Up @@ -35,9 +34,9 @@ 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))
}

Expand All @@ -48,7 +47,7 @@ func DefaultConfig() *Config {
"/ip4/0.0.0.0/udp/21888/quic-v1", "/ip6/::/udp/21888/quic-v1",
},
RelayAddrs: []string{},
BootstrapAddrs: []string{},
BootstrapAddrs: bootstrapAddrs,
MinConns: 8,
MaxConns: 16,
EnableNAT: true,
Expand Down Expand Up @@ -84,11 +83,3 @@ func (conf *Config) BasicCheck() error {
}
return validateAddresses(conf.BootstrapAddrs)
}

func (conf *Config) RelayAddrInfos() []lp2ppeer.AddrInfo {
return PeerAddrsToAddrInfo(conf.RelayAddrs)
}

func (conf *Config) BootstrapAddrInfos() []lp2ppeer.AddrInfo {
return PeerAddrsToAddrInfo(conf.BootstrapAddrs)
}
16 changes: 13 additions & 3 deletions network/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,38 @@ import (

func TestDefaultConfigCheck(t *testing.T) {
conf := DefaultConfig()

conf.EnableRelay = true
assert.Error(t, conf.BasicCheck())

conf = DefaultConfig()
conf.Listens = []string{""}
assert.Error(t, conf.BasicCheck())

conf = DefaultConfig()
conf.Listens = []string{"127.0.0.1"}
assert.Error(t, conf.BasicCheck())

conf = DefaultConfig()
conf.Listens = []string{"/ip4"}
assert.Error(t, conf.BasicCheck())

conf = DefaultConfig()
conf.RelayAddrs = []string{"/ip4"}
assert.Error(t, conf.BasicCheck())

conf.RelayAddrs = []string{}
conf.Listens = []string{}
conf = DefaultConfig()
conf.BootstrapAddrs = []string{"/ip4"}
assert.Error(t, conf.BasicCheck())

conf = DefaultConfig()
conf.RelayAddrs = []string{"/ip4/127.0.0.1"}
assert.NoError(t, conf.BasicCheck())

conf = DefaultConfig()
conf.Listens = []string{"/ip4/127.0.0.1"}
assert.NoError(t, conf.BasicCheck())

conf = DefaultConfig()
conf.BootstrapAddrs = []string{"/ip4/127.0.0.1"}
assert.NoError(t, conf.BasicCheck())
}
2 changes: 1 addition & 1 deletion network/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func newDHTService(ctx context.Context, host lp2phost.Host, protocolID lp2pcore.
if conf.Bootstrapper {
mode = lp2pdht.ModeServer
}
bootsrapAddrs := conf.BootstrapAddrInfos()
bootsrapAddrs := PeerAddrsToAddrInfo(conf.BootstrapAddrs)
opts := []lp2pdht.Option{
lp2pdht.Mode(mode),
lp2pdht.ProtocolPrefix(protocolID),
Expand Down
5 changes: 1 addition & 4 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,7 @@ func (n *network) Start() error {

if n.config.EnableRelay {
for _, relayAddr := range n.config.RelayAddrs {
addrInfo, err := MakeAddressInfo(relayAddr)
if err != nil {
return err
}
addrInfo, _ := MakeAddressInfo(relayAddr)
ConnectAsync(n.ctx, n.host, *addrInfo, n.logger)
}
}
Expand Down
24 changes: 8 additions & 16 deletions network/peermgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ type peerMgr struct {
logger *logger.SubLogger
}

// newPeerMgr returns a new Bootstrap that will attempt to keep connected
// to the network by connecting to the given bootstrap peers.
// newPeerMgr creates a new Peer Manager instance.
// Peer Manager attempts to establish connections with other nodes when the
// number of connections falls below the minimum threshold.
func newPeerMgr(ctx context.Context, h lp2phost.Host, d lp2pnet.Dialer, dht *lp2pdht.IpfsDHT,
bootstrapAddrs []lp2ppeer.AddrInfo, minConns int, maxConns int, logger *logger.SubLogger,
) *peerMgr {
Expand All @@ -50,7 +51,7 @@ func newPeerMgr(ctx context.Context, h lp2phost.Host, d lp2pnet.Dialer, dht *lp2
return b
}

// Start starts the Bootstrap bootstrapping. Cancel `ctx` or call Stop() to stop it.
// Start starts the Peer Manager.
func (mgr *peerMgr) Start() {
mgr.checkConnectivity()

Expand All @@ -71,11 +72,11 @@ func (mgr *peerMgr) Start() {

// Stop stops the Bootstrap.
func (mgr *peerMgr) Stop() {
// TODO: complete me
}

// checkConnectivity does the actual work. If the number of connected peers
// has fallen below b.MinPeerThreshold it will attempt to connect to
// a random subset of its bootstrap peers.
// checkConnectivity performs the actual work of maintaining connections.
// It ensures that the number of connections stays within the minimum and maximum thresholds.
func (mgr *peerMgr) checkConnectivity() {
currentPeers := mgr.dialer.Peers()
mgr.logger.Debug("check connectivity", "peers", len(currentPeers))
Expand Down Expand Up @@ -107,7 +108,7 @@ func (mgr *peerMgr) checkConnectivity() {
mgr.logger.Debug("try connecting to a bootstrap peer", "peer", pi.String())

// Don't try to connect to an already connected peer.
if hasPID(connectedPeers, pi.ID) {
if HasPID(connectedPeers, pi.ID) {
mgr.logger.Trace("already connected", "peer", pi.String())
continue
}
Expand All @@ -127,12 +128,3 @@ func (mgr *peerMgr) checkConnectivity() {
}
}
}

func hasPID(pids []lp2ppeer.ID, pid lp2ppeer.ID) bool {
for _, p := range pids {
if p == pid {
return true
}
}
return false
}
10 changes: 10 additions & 0 deletions network/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ func MakeAddressInfo(addr string) (*lp2ppeer.AddrInfo, error) {
return lp2ppeer.AddrInfoFromP2pAddr(maddr)
}

// HasPID checks if a peer ID exists in a list of peer IDs.
func HasPID(pids []lp2ppeer.ID, pid lp2ppeer.ID) bool {
for _, p := range pids {
if p == pid {
return true
}
}
return false
}

func ConnectAsync(ctx context.Context, h lp2phost.Host, addrInfo lp2ppeer.AddrInfo, logger *logger.SubLogger) {
go func() {
err := h.Connect(lp2pnetwork.WithDialPeerTimeout(ctx, 30*time.Second), addrInfo)
Expand Down

0 comments on commit 4bcc433

Please sign in to comment.