Skip to content

Commit

Permalink
Merge pull request #496 from SiaFoundation/nate/standardize-peer-api
Browse files Browse the repository at this point in the history
Standardize syncer peers response
  • Loading branch information
n8maninger authored Nov 1, 2024
2 parents 7d61fc0 + bfa2d27 commit 9a39669
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type (
Syncer interface {
Addr() string
Peers() []*syncer.Peer
PeerInfo(string) (syncer.PeerInfo, error)
Connect(ctx context.Context, addr string) (*syncer.Peer, error)

BroadcastTransactionSet(txns []types.Transaction)
Expand Down
29 changes: 22 additions & 7 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

rhp3 "go.sia.tech/core/rhp/v3"
"go.sia.tech/core/types"
"go.sia.tech/coreutils/syncer"
"go.sia.tech/hostd/build"
"go.sia.tech/hostd/host/contracts"
"go.sia.tech/hostd/host/metrics"
Expand Down Expand Up @@ -112,15 +113,29 @@ func (a *api) handleGETSyncerAddr(jc jape.Context) {
}

func (a *api) handleGETSyncerPeers(jc jape.Context) {
p := a.syncer.Peers()
peers := make([]Peer, len(p))
for i, peer := range p {
peers[i] = Peer{
Address: peer.ConnAddr,
Version: peer.Version(),
var peers []Peer
for _, p := range a.syncer.Peers() {
// create peer response with known fields
peer := Peer{
Address: p.Addr(),
Inbound: p.Inbound,
Version: p.Version(),
}
// add more info if available
info, err := a.syncer.PeerInfo(p.Addr())
if errors.Is(err, syncer.ErrPeerNotFound) {
continue
} else if err != nil {
jc.Error(err, http.StatusInternalServerError)
return
}
peer.FirstSeen = info.FirstSeen
peer.ConnectedSince = info.LastConnect
peer.SyncedBlocks = info.SyncedBlocks
peer.SyncDuration = info.SyncDuration
peers = append(peers, peer)
}
a.writeResponse(jc, PeerResp(peers))
jc.Encode(peers)
}

func (a *api) handlePUTSyncerPeer(jc jape.Context) {
Expand Down
8 changes: 7 additions & 1 deletion api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,16 @@ type (
SubtractMinerFee bool `json:"subtractMinerFee"`
}

// A Peer is a peer in the network.
// A Peer is a currently-connected peer.
Peer struct {
Address string `json:"address"`
Inbound bool `json:"inbound"`
Version string `json:"version"`

FirstSeen time.Time `json:"firstSeen,omitempty"`
ConnectedSince time.Time `json:"connectedSince,omitempty"`
SyncedBlocks uint64 `json:"syncedBlocks,omitempty"`
SyncDuration time.Duration `json:"syncDuration,omitempty"`
}

// A Setting updates a single setting on the host. It can be combined with
Expand Down

0 comments on commit 9a39669

Please sign in to comment.