Skip to content

Commit

Permalink
Merge pull request #34 from SiaFoundation/chris/peerinfo
Browse files Browse the repository at this point in the history
Add PeerInfo back to PeerStore interface
  • Loading branch information
lukechampine authored Mar 8, 2024
2 parents bcb0fb4 + 0834cb4 commit c2b0885
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type PeerStore interface {
AddPeer(addr string) error
// Peers returns the set of known peers.
Peers() ([]PeerInfo, error)
// PeerInfo returns the metadata for the specified peer or ErrPeerNotFound
// if the peer wasn't found in the store.
PeerInfo(addr string) (PeerInfo, error)
// UpdatePeerInfo updates the metadata for the specified peer. If the peer
// is not found, the error should be ErrPeerNotFound.
UpdatePeerInfo(addr string, fn func(*PeerInfo)) error
Expand Down Expand Up @@ -562,10 +565,13 @@ func (s *Syncer) syncLoop() error {
}
sentBlocks += uint64(len(blocks))
endTime, endHeight := time.Now(), s.cm.Tip().Height
s.pm.UpdatePeerInfo(p.t.Addr, func(info *PeerInfo) {
err = s.pm.UpdatePeerInfo(p.t.Addr, func(info *PeerInfo) {
info.SyncedBlocks += endHeight - startHeight
info.SyncDuration += endTime.Sub(startTime)
})
if err != nil {
return fmt.Errorf("syncLoop: failed to update peer info: %w", err)
}
startTime, startHeight = endTime, endHeight
if time.Since(lastPrint) > 30*time.Second {
s.log.Debug("syncing with peer", zap.Stringer("peer", p), zap.Uint64("blocks", sentBlocks), zap.Duration("elapsed", endTime.Sub(oldTime)))
Expand Down Expand Up @@ -717,6 +723,12 @@ func (s *Syncer) Peers() []*Peer {
return peers
}

// PeerInfo returns the metadata for the specified peer or ErrPeerNotFound if
// the peer wasn't found in the store.
func (s *Syncer) PeerInfo(addr string) (PeerInfo, error) {
return s.pm.PeerInfo(addr)
}

// Addr returns the address of the Syncer.
func (s *Syncer) Addr() string {
return s.l.Addr().String()
Expand Down

0 comments on commit c2b0885

Please sign in to comment.