go-ethereum/health/check_peers.go
2024-02-22 15:12:27 +08:00

23 lines
496 B
Go

package health
import (
"context"
"errors"
"fmt"
)
var (
errNotEnoughPeers = errors.New("not enough peers")
)
// checkMinPeers returns 'errNotEnoughPeers' if the current peer count its lower than 'minPeerCount'
func checkMinPeers(ec ethClient, minPeerCount uint64) error {
peerCount, err := ec.PeerCount(context.TODO())
if err != nil {
return err
}
if peerCount < minPeerCount {
return fmt.Errorf("%w: %d (minimum %d)", errNotEnoughPeers, peerCount, minPeerCount)
}
return nil
}