Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func runNode(ctx context.Context, nc *config) (_ io.Closer, retErr error) {
return nil, errors.Wrap(err, "failed to initialize application")
}

if pErr := spawnPeersByAddresses(ctx, conf.Addresses, peerManager); pErr != nil {
if pErr := spawnPeersByAddresses(conf.Addresses, peerManager); pErr != nil {
return nil, errors.Wrap(pErr, "failed to spawn peers by addresses")
}

Expand Down Expand Up @@ -637,7 +637,7 @@ func embeddedWallet(nc *config, scheme proto.Scheme) (types.EmbeddedWallet, erro
return wal, nil
}

func spawnPeersByAddresses(ctx context.Context, addressesByComma string, pm *peers.PeerManagerImpl) error {
func spawnPeersByAddresses(addressesByComma string, pm *peers.PeerManagerImpl) error {
if addressesByComma == "" { // That means that we don't have any peers to connect to
return nil
}
Expand All @@ -654,7 +654,7 @@ func spawnPeersByAddresses(ctx context.Context, addressesByComma string, pm *pee
fmt.Stringer(pi.Addr), pi.Port,
)
}
if pErr := pm.AddAddress(ctx, tcpAddr); pErr != nil {
if pErr := pm.AddAddress(tcpAddr); pErr != nil {
// That means that we have problems with peers storage
return errors.Wrapf(pErr, "failed to add address %q into known peers storage", tcpAddr.String())
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/node/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func (n *Network) handleConnected(msg *peer.Connected) {
msg.Peer.Direction(), msg.Peer.ID(), err)
return
}
zap.S().Named(logging.NetworkNamespace).Debugf("Established connection with %s peer '%s' (total: %d)",
msg.Peer.Direction(), msg.Peer.ID(), n.peers.ConnectedCount())
if n.peers.ConnectedCount() == n.minPeerMining { // TODO: Consider producing duplicate events here
n.networkInfoCh <- StartMining{}
}
Expand All @@ -129,6 +131,8 @@ func (n *Network) handleConnected(msg *peer.Connected) {

func (n *Network) handleInternalErr(msg peer.InfoMessage) {
n.peers.Disconnect(msg.Peer)
zap.S().Named(logging.NetworkNamespace).Debugf("Disconnected %s peer '%s' (total: %d)",
msg.Peer.Direction(), msg.Peer.ID(), n.peers.ConnectedCount())
if n.peers.ConnectedCount() < n.minPeerMining {
// TODO: Consider handling of duplicate events in consumer
n.networkInfoCh <- StopMining{}
Expand Down
4 changes: 0 additions & 4 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ func (a *Node) SpawnOutgoingConnections(ctx context.Context) {
a.peers.SpawnOutgoingConnections(ctx)
}

func (a *Node) SpawnOutgoingConnection(ctx context.Context, addr proto.TCPAddr) error {
return a.peers.Connect(ctx, addr)
}

func (a *Node) serveIncomingPeers(ctx context.Context) error {
var wg sync.WaitGroup
defer wg.Wait()
Expand Down
16 changes: 5 additions & 11 deletions pkg/node/peers/peer_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ func (a *PeerManagerImpl) Close() error {
func (a *PeerManagerImpl) SpawnOutgoingConnections(ctx context.Context) {
a.mu.Lock()
defer a.mu.Unlock()

if a.unsafeConnectedCount() > a.limitConnections*2 {
return
}
Expand All @@ -274,6 +273,9 @@ func (a *PeerManagerImpl) SpawnOutgoingConnections(ctx context.Context) {
},
)

zap.S().Named(logging.NetworkNamespace).Debugf("Spawning outgoing connections (outgoing: %d, limit: %d)",
outCnt, a.limitConnections)

if outCnt > a.limitConnections {
return
}
Expand All @@ -294,6 +296,7 @@ func (a *PeerManagerImpl) SpawnOutgoingConnections(ctx context.Context) {
}
}
})
zap.S().Named(logging.NetworkNamespace).Debugf("Known peers: %d, active: %d", len(known), len(active))
for _, knowPeer := range known {
ipPort := knowPeer.IpPort()
if _, ok := active[ipPort]; ok {
Expand Down Expand Up @@ -419,20 +422,11 @@ func (a *PeerManagerImpl) Run(ctx context.Context) {
}
}

func (a *PeerManagerImpl) AddAddress(ctx context.Context, addr proto.TCPAddr) error {
func (a *PeerManagerImpl) AddAddress(addr proto.TCPAddr) error {
known := storage.KnownPeer(addr.ToIpPort())
if err := a.peerStorage.AddOrUpdateKnown([]storage.KnownPeer{known}, time.Now()); err != nil {
return errors.Wrapf(err, "failed to add addr %q into known peers storage", addr.String())
}
go func() {
if err := a.spawner.SpawnOutgoing(ctx, addr); err != nil {
// TODO(nickeskov): maybe don't remove from known peers in this case?
if removeErr := a.peerStorage.DeleteKnown([]storage.KnownPeer{known}); removeErr != nil {
zap.S().Errorf("Failed to remove peer %q from known peers storage", known.String())
}
zap.S().Named(logging.NetworkNamespace).Debugf("Error: %v", err)
}
}()
return nil
}

Expand Down
Loading