Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): serve underlay and connections in /peers response #4469

Merged
merged 1 commit into from
Nov 22, 2023
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
4 changes: 4 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/accounting"
"github.com/ethersphere/bee/pkg/addressbook"
"github.com/ethersphere/bee/pkg/auth"
"github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/feeds"
Expand Down Expand Up @@ -177,6 +178,7 @@ type Service struct {
chequebook chequebook.Service
pseudosettle settlement.Interface
pingpong pingpong.Interface
addressBook addressbook.Getter

batchStore postage.Storer
stamperStore storage.Store
Expand Down Expand Up @@ -248,6 +250,7 @@ type ExtraOptions struct {
Steward steward.Interface
SyncStatus func() (bool, error)
NodeStatus *status.Service
AddressBook addressbook.Getter
}

func New(
Expand Down Expand Up @@ -323,6 +326,7 @@ func (s *Service) Configure(signer crypto.Signer, auth auth.Authenticator, trace
s.postageContract = e.PostageContract
s.steward = e.Steward
s.stakingContract = e.Staking
s.addressBook = e.AddressBook

s.pingpong = e.Pingpong
s.topologyDriver = e.TopologyDriver
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/ethereum/go-ethereum/common"
accountingmock "github.com/ethersphere/bee/pkg/accounting/mock"
"github.com/ethersphere/bee/pkg/addressbook"
"github.com/ethersphere/bee/pkg/api"
"github.com/ethersphere/bee/pkg/auth"
mockauth "github.com/ethersphere/bee/pkg/auth/mock"
Expand Down Expand Up @@ -170,6 +171,8 @@ func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.
chequebook := chequebookmock.NewChequebook(o.ChequebookOpts...)
ln := lightnode.NewContainer(o.Overlay)

addressBook := addressbook.New(statestore.NewStateStore())

transaction := transactionmock.New(o.TransactionOpts...)

storeRecipient := statestore.NewStateStore()
Expand Down Expand Up @@ -200,6 +203,7 @@ func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.
SyncStatus: o.SyncStatus,
Staking: o.StakingContract,
NodeStatus: o.NodeStatus,
AddressBook: addressBook,
}

// By default bee mode is set to full mode.
Expand Down
29 changes: 19 additions & 10 deletions pkg/api/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ func (s *Service) peerDisconnectHandler(w http.ResponseWriter, r *http.Request)

// Peer holds information about a Peer.
type Peer struct {
Address swarm.Address `json:"address"`
FullNode bool `json:"fullNode"`
Underlay []multiaddr.Multiaddr `json:"underlay,omitempty"`
Address swarm.Address `json:"address"`
Underlay multiaddr.Multiaddr `json:"underlay,omitempty"`
FullNode bool `json:"fullNode"`
Connections []multiaddr.Multiaddr `json:"connections,omitempty"`
}

type BlockListedPeer struct {
Expand All @@ -100,7 +101,7 @@ type blockListedPeersResponse struct {

func (s *Service) peersHandler(w http.ResponseWriter, _ *http.Request) {
jsonhttp.OK(w, peersResponse{
Peers: mapPeers(s.p2p.Peers()),
Peers: s.mapPeers(s.p2p.Peers()),
})
}

Expand All @@ -119,14 +120,22 @@ func (s *Service) blocklistedPeersHandler(w http.ResponseWriter, _ *http.Request
})
}

func mapPeers(peers []p2p.Peer) (out []Peer) {
func (s *Service) mapPeers(peers []p2p.Peer) (out []Peer) {
out = make([]Peer, 0, len(peers))
for _, peer := range peers {
out = append(out, Peer{
Address: peer.Address,
FullNode: peer.FullNode,
Underlay: peer.Underlay,
})
bzz, err := s.addressBook.Get(peer.Address)
if err != nil {
s.logger.Debug("get bzz address from addressbook", "address", peer.Address, "error", err)
}
p := Peer{
Address: peer.Address,
FullNode: peer.FullNode,
Connections: peer.Connections,
}
if bzz != nil {
p.Underlay = bzz.Underlay
}
out = append(out, p)
}
return
}
Expand Down
1 change: 1 addition & 0 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,7 @@ func NewBee(
Steward: steward,
SyncStatus: syncStatusFn,
NodeStatus: nodeStatus,
AddressBook: addressbook,
}

if o.APIAddr != "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/p2p/libp2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (r *peerRegistry) peers() []p2p.Peer {
FullNode: r.full[p],
}
for conn := range r.connections[p] {
peer.Underlay = append(peer.Underlay, conn.RemoteMultiaddr())
peer.Connections = append(peer.Connections, conn.RemoteMultiaddr())
}
peers = append(peers, peer)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ type Peer struct {
Address swarm.Address
FullNode bool
EthereumAddress []byte
Underlay []ma.Multiaddr
Connections []ma.Multiaddr
}

// BlockListedPeer holds information about a Peer that is blocked.
Expand Down
Loading