Skip to content

Commit

Permalink
Merge pull request #863 from iotaledger/feat/wire-up-peers-api
Browse files Browse the repository at this point in the history
Wire up management API for peers
  • Loading branch information
muXxer authored Mar 22, 2024
2 parents e07c2f6 + 00ac041 commit 9d9695f
Show file tree
Hide file tree
Showing 16 changed files with 341 additions and 152 deletions.
2 changes: 2 additions & 0 deletions components/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/iotaledger/iota-core/components/protocol"
"github.com/iotaledger/iota-core/components/restapi"
coreapi "github.com/iotaledger/iota-core/components/restapi/core"
"github.com/iotaledger/iota-core/components/restapi/management"
"github.com/iotaledger/iota-core/pkg/toolset"
)

Expand Down Expand Up @@ -44,6 +45,7 @@ Command line flags:
profiling.Component,
restapi.Component,
coreapi.Component,
management.Component,
debugapi.Component,
metricstracker.Component,
protocol.Component,
Expand Down
43 changes: 23 additions & 20 deletions components/restapi/management/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/iotaledger/hive.go/app"
"github.com/iotaledger/inx-app/pkg/httpserver"
"github.com/iotaledger/iota-core/pkg/network"
"github.com/iotaledger/iota-core/pkg/network/p2p"
"github.com/iotaledger/iota-core/pkg/protocol"
restapipkg "github.com/iotaledger/iota-core/pkg/restapi"
"github.com/iotaledger/iota.go/v4/api"
Expand All @@ -29,8 +31,10 @@ var (
type dependencies struct {
dig.In

RestRouteManager *restapipkg.RestRouteManager
Protocol *protocol.Protocol
RestRouteManager *restapipkg.RestRouteManager
Protocol *protocol.Protocol
PeeringConfigManager *p2p.ConfigManager
NetworkManager network.Manager
}

func configure() error {
Expand All @@ -42,7 +46,7 @@ func configure() error {
return err
}

return httpserver.JSONResponse(c, http.StatusOK, resp)
return responseByHeader(c, resp, http.StatusOK)
})

routeGroup.DELETE(api.EndpointWithEchoParameters(api.ManagementEndpointPeer), func(c echo.Context) error {
Expand All @@ -54,21 +58,16 @@ func configure() error {
})

routeGroup.GET(api.ManagementEndpointPeers, func(c echo.Context) error {
resp, err := listPeers(c)
if err != nil {
return err
}

return httpserver.JSONResponse(c, http.StatusOK, resp)
return responseByHeader(c, listPeers(), http.StatusOK)
})

routeGroup.POST(api.ManagementEndpointPeers, func(c echo.Context) error {
resp, err := addPeer(c, Component.Logger)
resp, err := addPeer(c)
if err != nil {
return err
}

return httpserver.JSONResponse(c, http.StatusOK, resp)
return responseByHeader(c, resp, http.StatusOK)
})

routeGroup.POST(api.ManagementEndpointDatabasePrune, func(c echo.Context) error {
Expand All @@ -77,17 +76,21 @@ func configure() error {
return err
}

return httpserver.JSONResponse(c, http.StatusOK, resp)
return responseByHeader(c, resp, http.StatusOK)
})

routeGroup.POST(api.ManagementEndpointSnapshotsCreate, func(c echo.Context) error {
resp, err := createSnapshots(c)
if err != nil {
return err
}

return httpserver.JSONResponse(c, http.StatusOK, resp)
})
// routeGroup.POST(api.ManagementEndpointSnapshotsCreate, func(c echo.Context) error {
// resp, err := createSnapshots(c)
// if err != nil {
// return err
// }
//
// return responseByHeader(c, resp, http.StatusOK)
// })

return nil
}

func responseByHeader(c echo.Context, obj any, httpStatusCode ...int) error {
return httpserver.SendResponseByHeader(c, deps.Protocol.CommittedAPI(), obj, httpStatusCode...)
}
195 changes: 108 additions & 87 deletions components/restapi/management/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,132 +2,153 @@ package management

import (
"github.com/labstack/echo/v4"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"

"github.com/iotaledger/hive.go/log"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/inx-app/pkg/httpserver"
"github.com/iotaledger/iota-core/pkg/network"
iotago "github.com/iotaledger/iota.go/v4"
"github.com/iotaledger/iota.go/v4/api"
)

/*
// WrapInfoSnapshot wraps the given peer info snapshot with additional metadata, such as gossip protocol information.
func WrapInfoSnapshot(info *p2p.PeerInfoSnapshot) *api.PeerResponse {
var alias *string
const (
// PeerRelationManual is the relation of a manually peered peer.
PeerRelationManual string = "manual"
// PeerRelationAutopeered is the relation of an autopeered peer.
PeerRelationAutopeered string = "autopeered"
)

if info.Alias != "" {
alias = &info.Alias
// parsePeerIDParam parses the peerID parameter from the request.
func parsePeerIDParam(c echo.Context) (peer.ID, error) {
peerID, err := peer.Decode(c.Param("peerID"))
if err != nil {
return "", ierrors.WithMessagef(httpserver.ErrInvalidParameter, "invalid peerID: %w", err)
}

multiAddresses := make([]string, len(info.Addresses))
for i, multiAddress := range info.Addresses {
multiAddresses[i] = multiAddress.String()
return peerID, nil
}

// getPeerInfo returns the peer info for the given neighbor.
func getPeerInfo(neighbor network.Neighbor) *api.PeerInfo {
peer := neighbor.Peer()

multiAddresses := make([]iotago.PrefixedStringUint8, len(peer.PeerAddresses))
for i, multiAddress := range peer.PeerAddresses {
multiAddresses[i] = iotago.PrefixedStringUint8(multiAddress.String())
}

gossipProto := deps.GossipService.Protocol(info.Peer.ID)
var gossipInfo *gossip.Info
if gossipProto != nil {
gossipInfo = gossipProto.Info()
var alias string
relation := PeerRelationAutopeered

if peerConfigItem := deps.PeeringConfigManager.Peer(neighbor.Peer().ID); peerConfigItem != nil {
alias = peerConfigItem.Alias

// if the peer exists in the config, it is a manual peered peer
relation = PeerRelationManual
}

return &PeerResponse{
ID: info.ID,
return &api.PeerInfo{
ID: peer.ID.String(),
MultiAddresses: multiAddresses,
Alias: alias,
Relation: info.Relation,
Connected: info.Connected,
Gossip: gossipInfo,
Relation: relation,
Connected: peer.ConnStatus.Load() == network.ConnStatusConnected,
GossipMetrics: &api.PeerGossipMetrics{
PacketsReceived: uint32(neighbor.PacketsRead()),
PacketsSent: uint32(neighbor.PacketsWritten()),
},
}
return nil
}
*/

func getPeer(_ echo.Context) (*api.PeerInfo, error) {
/*
peerID, err := restapipkg.ParsePeerIDParam(c)
if err != nil {
return nil, err
}
// getPeer returns the peer info for the given peerID in the request.
func getPeer(c echo.Context) (*api.PeerInfo, error) {
peerID, err := parsePeerIDParam(c)
if err != nil {
return nil, err
}

info := deps.PeeringManager.PeerInfoSnapshot(peerID)
if info == nil {
neighbor, err := deps.NetworkManager.Neighbor(peerID)
if err != nil {
if ierrors.Is(err, network.ErrUnknownPeer) {
return nil, ierrors.WithMessagef(echo.ErrNotFound, "peer not found, peerID: %s", peerID.String())
}

return WrapInfoSnapshot(info), nil
*/
return nil, ierrors.WithMessagef(echo.ErrInternalServerError, "failed to get peer: %w", err)
}

//nolint:nilnil
return nil, nil
return getPeerInfo(neighbor), nil
}

func removePeer(_ echo.Context) error {
/*
peerID, err := restapipkg.ParsePeerIDParam(c)
if err != nil {
return err
}
// removePeer drops the connection to the peer with the given peerID and removes it from the known peers.
func removePeer(c echo.Context) error {
peerID, err := parsePeerIDParam(c)
if err != nil {
return err
}

// error is ignored because we don't care about the config here
_ = deps.PeeringConfigManager.RemovePeer(peerID)
// error is ignored because we don't care about the config here
_ = deps.PeeringConfigManager.RemovePeer(peerID)

return deps.PeeringManager.DisconnectPeer(peerID, ierrors.New("peer was removed via API"))
*/
return nil
return deps.NetworkManager.DropNeighbor(peerID)
}

func listPeers(_ echo.Context) (*api.PeersResponse, error) {
/*
peerInfos := deps.PeeringManager.PeerInfoSnapshots()
results := make([]*PeerResponse, len(peerInfos))
for i, info := range peerInfos {
results[i] = WrapInfoSnapshot(info)
}
// listPeers returns the list of all peers.
func listPeers() *api.PeersResponse {
allNeighbors := deps.NetworkManager.AllNeighbors()

return results, nil
*/
result := &api.PeersResponse{
Peers: make([]*api.PeerInfo, len(allNeighbors)),
}

for i, info := range allNeighbors {
result.Peers[i] = getPeerInfo(info)
}

//nolint:nilnil
return nil, nil
return result
}

func addPeer(_ echo.Context, _ log.Logger) (*api.PeerInfo, error) {
/*
// addPeer tries to establish a connection to the given peer and adds it to the known peers.
func addPeer(c echo.Context) (*api.PeerInfo, error) {
request := &api.AddPeerRequest{}

request := &addPeerRequest{}
if err := c.Bind(request); err != nil {
return nil, ierrors.WithMessagef(httpserver.ErrInvalidParameter, "invalid addPeerRequest: %w", err)
}

if err := c.Bind(request); err != nil {
return nil, ierrors.WithMessagef(httpserver.ErrInvalidParameter, "invalid addPeerRequest, error: %s", err)
}
multiAddr, err := multiaddr.NewMultiaddr(request.MultiAddress)
if err != nil {
return nil, ierrors.WithMessagef(httpserver.ErrInvalidParameter, "invalid multiAddress: %w", err)
}

multiAddr, err := multiaddr.NewMultiaddr(request.MultiAddress)
if err != nil {
return nil, ierrors.WithMessagef(httpserver.ErrInvalidParameter, "invalid multiAddress, error: %s", err)
}
addrInfo, err := peer.AddrInfoFromP2pAddr(multiAddr)
if err != nil {
return nil, ierrors.WithMessagef(httpserver.ErrInvalidParameter, "invalid multiAddress: %w", err)
}

addrInfo, err := peer.AddrInfoFromP2pAddr(multiAddr)
if err != nil {
return nil, ierrors.WithMessagef(httpserver.ErrInvalidParameter, "invalid multiAddress, error: %s", err)
}
if err := deps.NetworkManager.AddManualPeers(multiAddr); err != nil {
return nil, ierrors.WithMessagef(echo.ErrInternalServerError, "failed to add peer: %w", err)
}

var alias string
if request.Alias != nil {
alias = *request.Alias
peerID := addrInfo.ID
neighbor, err := deps.NetworkManager.Neighbor(peerID)
if err != nil {
if ierrors.Is(err, network.ErrUnknownPeer) {
return nil, ierrors.WithMessagef(echo.ErrNotFound, "peer not found, peerID: %s", peerID.String())
}

// error is ignored because the peer is added to the known peers and protected from trimming
_ = deps.PeeringManager.ConnectPeer(addrInfo, p2p.PeerRelationKnown, alias)
info := deps.PeeringManager.PeerInfoSnapshot(addrInfo.ID)
if info == nil {
return nil, ierrors.WithMessagef(echo.ErrNotFound, "peer not found, peerID: %s", addrInfo.ID.String())
}
return nil, ierrors.WithMessagef(echo.ErrInternalServerError, "failed to get peer: %w", err)
}

// error is ignored because we don't care about the config here
if err := deps.PeeringConfigManager.AddPeer(multiAddr, alias); err != nil {
logger.Warn(err.Error())
}
var alias string
if request.Alias != "" {
alias = request.Alias
}

return WrapInfoSnapshot(info), nil
*/
// error is ignored because we don't care about the config here
if err := deps.PeeringConfigManager.AddPeer(multiAddr, alias); err != nil {
Component.LogWarnf("failed to add peer to config, peerID: %s, err: %s", peerID.String(), err.Error())
}

//nolint:nilnil
return nil, nil
return getPeerInfo(neighbor), nil
}
37 changes: 0 additions & 37 deletions components/restapi/management/snapshots.go

This file was deleted.

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-20240320122938-13a946cf3c7a
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-20240321164059-d61818989ece
github.com/iotaledger/iota.go/v4 v4.0.0-20240321174445-4e586367e5bd
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
Loading

0 comments on commit 9d9695f

Please sign in to comment.