2021-09-28 09:27:57 +00:00
|
|
|
package health
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-06 07:43:22 +00:00
|
|
|
"errors"
|
2021-09-28 09:27:57 +00:00
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2022-07-06 07:43:22 +00:00
|
|
|
var (
|
|
|
|
errNotEnoughPeers = errors.New("not enough peers")
|
|
|
|
)
|
|
|
|
|
2021-09-28 09:27:57 +00:00
|
|
|
func checkMinPeers(minPeerCount uint, api NetAPI) error {
|
|
|
|
if api == nil {
|
|
|
|
return fmt.Errorf("no connection to the Erigon server or `net` namespace isn't enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
peerCount, err := api.PeerCount(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint64(peerCount) < uint64(minPeerCount) {
|
2022-07-06 07:43:22 +00:00
|
|
|
return fmt.Errorf("%w: %d (minimum %d)", errNotEnoughPeers, peerCount, minPeerCount)
|
2021-09-28 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|