Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
Add wait until neighbor is connected
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Mar 26, 2024
1 parent 6b87629 commit 71c2063
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 13 deletions.
26 changes: 25 additions & 1 deletion components/restapi/management/peers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package management

import (
"context"
"time"

"github.com/labstack/echo/v4"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -90,7 +93,7 @@ func removePeer(c echo.Context) error {
// error is ignored because we don't care about the config here
_ = deps.PeeringConfigManager.RemovePeer(peerID)

return deps.NetworkManager.DropNeighbor(peerID)
return deps.NetworkManager.RemoveNeighbor(peerID)
}

// listPeers returns the list of all peers.
Expand Down Expand Up @@ -126,10 +129,31 @@ func addPeer(c echo.Context) (*api.PeerInfo, error) {
return nil, ierrors.WithMessagef(httpserver.ErrInvalidParameter, "invalid address info from multiAddress (%s): %w", request.MultiAddress, err)
}

if deps.NetworkManager.ManualNeighborExists(addrInfo.ID) {
return nil, ierrors.WithMessagef(echo.ErrBadRequest, "manual peer already exists, peerID: %s", addrInfo.ID.String())
}

ctx, cancel := context.WithTimeout(c.Request().Context(), 5*time.Second)
defer cancel()

unhook := deps.NetworkManager.OnNeighborAdded(func(neighbor network.Neighbor) {
if neighbor.Peer().ID == addrInfo.ID {
// cancel the context to stop waiting
cancel()
}
}).Unhook
defer unhook()

if err := deps.NetworkManager.AddManualPeers(multiAddr); err != nil {
return nil, ierrors.WithMessagef(echo.ErrInternalServerError, "failed to add peer: %w", err)
}

// wait for the peer to be added or the context to be done
<-ctx.Done()
if ierrors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, ierrors.WithMessagef(echo.ErrInternalServerError, "failed to add peer: timeout")
}

peerID := addrInfo.ID
neighbor, err := deps.NetworkManager.Neighbor(peerID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20240326102522-2e37ab3611a3
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240307101848-db58eb9353ec
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022
github.com/iotaledger/iota.go/v4 v4.0.0-20240322114706-82a1f8a8b70c
github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0
github.com/labstack/echo/v4 v4.11.4
github.com/labstack/gommon v0.4.2
github.com/libp2p/go-libp2p v0.33.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022 h1:I178Sa
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022/go.mod h1:jTFxIWiMUdAwO263jlJCSWcNLqEkgYEVOFXfjp5aNJM=
github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff h1:Do8fakxvFaj7dLckoo/z+mRyBdZo8QvT8HcgnQlG2Sg=
github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff/go.mod h1:aVEutEWFnhDNJBxtVuzy2BeTN+8FAlnR83k7hKV0CFE=
github.com/iotaledger/iota.go/v4 v4.0.0-20240322114706-82a1f8a8b70c h1:0uqpCv2txjbVi1E5AFvXkUGmTMiEX1nPzmTFH1Bfk6c=
github.com/iotaledger/iota.go/v4 v4.0.0-20240322114706-82a1f8a8b70c/go.mod h1:qn/63CB0/jE1em6ewqDSiz+ovS+E/os7K5b7g2pmJFg=
github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0 h1:e0CGNpvHDaAO1Xj/LUu9f+GZrixncJgyId4o4iS55RU=
github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0/go.mod h1:qn/63CB0/jE1em6ewqDSiz+ovS+E/os7K5b7g2pmJFg=
github.com/ipfs/boxo v0.18.0 h1:MOL9/AgoV3e7jlVMInicaSdbgralfqSsbkc31dZ9tmw=
github.com/ipfs/boxo v0.18.0/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down
17 changes: 12 additions & 5 deletions pkg/network/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ type Manager interface {
OnNeighborAdded(handler func(Neighbor)) *event.Hook[func(Neighbor)]
OnNeighborRemoved(handler func(Neighbor)) *event.Hook[func(Neighbor)]

// Neighbor returns the neighbor with the given ID.
Neighbor(peerID peer.ID) (Neighbor, error)
// NeighborExists checks if a neighbor with the given ID exists.
NeighborExists(peerID peer.ID) bool
// ManualNeighborExists checks if a neighbor with the given ID exists in the manual peering layer.
ManualNeighborExists(peerID peer.ID) bool
// RemoveNeighbor disconnects the neighbor with the given ID
// and removes it from manual peering in case it was added manually.
RemoveNeighbor(peerID peer.ID) error
// DropNeighbor disconnects the neighbor with the given ID.
DropNeighbor(peerID peer.ID) error

AllNeighbors() []Neighbor
AutopeeringNeighbors() []Neighbor

DropNeighbor(peerID peer.ID) error
NeighborExists(peerID peer.ID) bool
AddManualPeers(multiAddresses ...multiaddr.Multiaddr) error

P2PHost() host.Host

Start(ctx context.Context, networkID string) error
Shutdown()

AddManualPeers(multiAddresses ...multiaddr.Multiaddr) error
}
1 change: 1 addition & 0 deletions pkg/network/neighbor.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package network

// Neighbor is a Peer with an active connection.
type Neighbor interface {
Peer() *Peer
PacketsRead() uint64
Expand Down
25 changes: 24 additions & 1 deletion pkg/network/p2p/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,26 @@ func (m *Manager) P2PHost() host.Host {
return m.libp2pHost
}

// DropNeighbor disconnects the neighbor with the given ID and the group.
// RemoveNeighbor disconnects the neighbor with the given ID
// and removes it from manual peering in case it was added manually.
func (m *Manager) RemoveNeighbor(id peer.ID) error {
if m.manualPeering.IsPeerKnown(id) {
// RemovePeer calls DropNeighbor internally
if err := m.manualPeering.RemovePeer(id); err != nil {
return err
}

return nil
}

if err := m.DropNeighbor(id); err != nil && !ierrors.Is(err, network.ErrUnknownPeer) {
return ierrors.Wrapf(err, "failed to drop peer %s in the gossip layer", id.String())
}

return nil
}

// DropNeighbor disconnects the neighbor with the given ID.
func (m *Manager) DropNeighbor(id peer.ID) error {
nbr, err := m.neighbor(id)
if err != nil {
Expand Down Expand Up @@ -409,6 +428,10 @@ func (m *Manager) NeighborExists(id peer.ID) bool {
return m.neighbors.Has(id)
}

func (m *Manager) ManualNeighborExists(id peer.ID) bool {
return m.manualPeering.IsPeerKnown(id)
}

func (m *Manager) deleteNeighbor(nbr *neighbor) {
// Close the connection to the peer.
_ = m.libp2pHost.Network().ClosePeer(nbr.Peer().ID)
Expand Down
2 changes: 1 addition & 1 deletion tools/gendoc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ require (
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240307101848-db58eb9353ec // indirect
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022 // indirect
github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20240322114706-82a1f8a8b70c // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0 // indirect
github.com/ipfs/boxo v0.18.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions tools/gendoc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022 h1:I178Sa
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022/go.mod h1:jTFxIWiMUdAwO263jlJCSWcNLqEkgYEVOFXfjp5aNJM=
github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff h1:Do8fakxvFaj7dLckoo/z+mRyBdZo8QvT8HcgnQlG2Sg=
github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff/go.mod h1:aVEutEWFnhDNJBxtVuzy2BeTN+8FAlnR83k7hKV0CFE=
github.com/iotaledger/iota.go/v4 v4.0.0-20240322114706-82a1f8a8b70c h1:0uqpCv2txjbVi1E5AFvXkUGmTMiEX1nPzmTFH1Bfk6c=
github.com/iotaledger/iota.go/v4 v4.0.0-20240322114706-82a1f8a8b70c/go.mod h1:qn/63CB0/jE1em6ewqDSiz+ovS+E/os7K5b7g2pmJFg=
github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0 h1:e0CGNpvHDaAO1Xj/LUu9f+GZrixncJgyId4o4iS55RU=
github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0/go.mod h1:qn/63CB0/jE1em6ewqDSiz+ovS+E/os7K5b7g2pmJFg=
github.com/ipfs/boxo v0.18.0 h1:MOL9/AgoV3e7jlVMInicaSdbgralfqSsbkc31dZ9tmw=
github.com/ipfs/boxo v0.18.0/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down

0 comments on commit 71c2063

Please sign in to comment.