Skip to content

Commit

Permalink
add health api method for checking the health of a node through RPC (#18
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nibty authored Mar 22, 2024
1 parent b510cac commit 0abc8ec
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
23 changes: 22 additions & 1 deletion ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ import (
)

var (
noUncles = []evmcore.EvmHeader{}
noUncles = []evmcore.EvmHeader{}
startTime = time.Now()
)

// PublicEthereumAPI provides an API to access Ethereum related information.
Expand Down Expand Up @@ -187,6 +188,26 @@ func (s *PublicEthereumAPI) Syncing() (interface{}, error) {
}, nil
}

// Health returns true if node is healthy and synced
func (s *PublicEthereumAPI) Health(blockTimeSecThreshold uint64, uptimeSecThreshold uint, peerCountThreshold uint) (interface{}, error) {
blockTime := time.Duration(blockTimeSecThreshold) * time.Second
uptimeThreshold := time.Duration(uptimeSecThreshold) * time.Second
uptime := time.Since(startTime)
progress := s.b.Progress()
peerCount := s.b.PeerCount()
log.Debug("Health check", "peerCount", peerCount, "peerCountThreshold", peerCountThreshold, "uptime", uptime, "uptimeThreshold", uptimeThreshold, "progress", progress, "currentBlockTime", time.Since(progress.CurrentBlockTime.Time()), "blockTime", blockTime)

if uptime < uptimeThreshold || progress.CurrentBlock < progress.HighestBlock || peerCount < int(peerCountThreshold) {
return false, nil
}

if blockTime > 0 && time.Since(progress.CurrentBlockTime.Time()) > blockTime {
return false, nil
}

return true, nil
}

// PublicTxPoolAPI offers and API for the transaction pool. It only operates on data that is non confidential.
type PublicTxPoolAPI struct {
b Backend
Expand Down
1 change: 1 addition & 0 deletions ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type PeerProgress struct {
type Backend interface {
// General Ethereum API
Progress() PeerProgress
PeerCount() int
SuggestGasTipCap(ctx context.Context, certainty uint64) *big.Int
EffectiveMinGasPrice(ctx context.Context) *big.Int
ChainDb() ethdb.Database
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/ethereum/go-ethereum => github.com/faircrypto/go-ethereum v1.1.4-rc1-4
replace github.com/ethereum/go-ethereum => github.com/faircrypto/go-ethereum v1.1.4-rc1-5

replace github.com/Fantom-foundation/lachesis-base => github.com/faircrypto/lachesis-base v0.0.0-20230817040848-1326ba9aa59b

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw=
github.com/evalphobia/logrus_sentry v0.8.2 h1:dotxHq+YLZsT1Bb45bB5UQbfCh3gM/nFFetyN46VoDQ=
github.com/evalphobia/logrus_sentry v0.8.2/go.mod h1:pKcp+vriitUqu9KiWj/VRFbRfFNUwz95/UkgG8a6MNc=
github.com/faircrypto/go-ethereum v1.1.4-rc1-4 h1:1C5SKbOuwFyuMrut1SvD+qyFUlC52cA8P+TKC6igKjg=
github.com/faircrypto/go-ethereum v1.1.4-rc1-4/go.mod h1:ah5rnRobPJSTUKXIETbrkyrrEhWPTUPfmF1gPPNr0Tg=
github.com/faircrypto/go-ethereum v1.1.4-rc1-5 h1:CQu2ZtQHeTROdJse4+FzwQ+qrJO297HrVIup+3yCc1I=
github.com/faircrypto/go-ethereum v1.1.4-rc1-5/go.mod h1:ah5rnRobPJSTUKXIETbrkyrrEhWPTUPfmF1gPPNr0Tg=
github.com/faircrypto/lachesis-base v0.0.0-20230817040848-1326ba9aa59b h1:mEofwrV6bMlbuneVS8tnZmNZut7WY2YH++2GI7DxzHg=
github.com/faircrypto/lachesis-base v0.0.0-20230817040848-1326ba9aa59b/go.mod h1:Ogv5etzSmM2rQ4eN3OfmyitwWaaPjd4EIDiW/NAbYGk=
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
Expand Down
4 changes: 4 additions & 0 deletions gossip/ethapi_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,7 @@ func (b *EthAPIBackend) SealedEpochTiming(ctx context.Context) (start inter.Time
es := b.svc.store.GetEpochState()
return es.PrevEpochStart, es.EpochStart
}

func (b *EthAPIBackend) PeerCount() int {
return b.svc.handler.peers.Len()
}

0 comments on commit 0abc8ec

Please sign in to comment.