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

Standardize syncer peers response #496

Merged
merged 1 commit into from
Nov 1, 2024
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
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
Loading