From 7133ebb3c08240ea8da99b900362ccd16c771e85 Mon Sep 17 00:00:00 2001 From: Nicholas Pettas Date: Fri, 22 Mar 2024 17:09:11 -0700 Subject: [PATCH 1/3] add health api method for checking the health of a node throuugh RPC --- ethapi/api.go | 31 ++++++++++++++++++++++++++----- ethapi/backend.go | 1 + go.mod | 2 +- go.sum | 4 ++-- gossip/ethapi_backend.go | 4 ++++ 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/ethapi/api.go b/ethapi/api.go index 777544971..6d7ba5d7d 100644 --- a/ethapi/api.go +++ b/ethapi/api.go @@ -56,7 +56,8 @@ import ( ) var ( - noUncles = []evmcore.EvmHeader{} + noUncles = []evmcore.EvmHeader{} + startTime = time.Now() ) // PublicEthereumAPI provides an API to access Ethereum related information. @@ -166,6 +167,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 @@ -803,10 +824,10 @@ func (s *PublicBlockChainAPI) calculateExtBlockApi(ctx context.Context, blkNumbe } // GetBlockByNumber returns the requested canonical block. -// * When blockNr is -1 the chain head is returned. -// * When blockNr is -2 the pending chain head is returned. -// * When fullTx is true all transactions in the block are returned, otherwise -// only the transaction hash is returned. +// - When blockNr is -1 the chain head is returned. +// - When blockNr is -2 the pending chain head is returned. +// - When fullTx is true all transactions in the block are returned, otherwise +// only the transaction hash is returned. func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { block, err := s.b.BlockByNumber(ctx, number) if block != nil && err == nil { diff --git a/ethapi/backend.go b/ethapi/backend.go index 214c939f0..f1971c508 100644 --- a/ethapi/backend.go +++ b/ethapi/backend.go @@ -53,6 +53,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 diff --git a/go.mod b/go.mod index bdb8bf52d..ec22eeb3a 100644 --- a/go.mod +++ b/go.mod @@ -44,6 +44,6 @@ require ( gopkg.in/urfave/cli.v1 v1.20.0 ) -replace github.com/ethereum/go-ethereum => github.com/Fantom-foundation/go-ethereum v1.10.8-ftm-rc9 +replace github.com/ethereum/go-ethereum => github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240322234738-3d5e1d6e7883 replace github.com/dvyukov/go-fuzz => github.com/guzenok/go-fuzz v0.0.0-20210103140116-f9104dfb626f diff --git a/go.sum b/go.sum index b9bb3a984..a9903f0cc 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,6 @@ github.com/CloudyKit/jet v2.1.3-0.20180809161101-62edd43e4f88+incompatible/go.mo github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ= github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo= -github.com/Fantom-foundation/go-ethereum v1.10.8-ftm-rc9 h1:aB5yATSn4a2SmEr717Tq/YBWHcobLnRgAjXuLnqt420= -github.com/Fantom-foundation/go-ethereum v1.10.8-ftm-rc9/go.mod h1:IeQDjWCNBj/QiWIPosfF6/kRC6pHPNs7W7LfBzjj+P4= github.com/Fantom-foundation/lachesis-base v0.0.0-20221101131534-22299068014e h1:MrNcnBTl5UOn7d1qVb+BfKx+BPZDdfQm1CUDzQkOhEg= github.com/Fantom-foundation/lachesis-base v0.0.0-20221101131534-22299068014e/go.mod h1:CFMonaK1v/QHwpjLszuycFDgLXNazZqZxciwhKwXDVQ= github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY= @@ -164,6 +162,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.10.8-ftm-rc9.0.20240322234738-3d5e1d6e7883 h1:ajz2flH7mV4U4WwXAP6XKssEXL8W4i53d/a/2QLPRdc= +github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240322234738-3d5e1d6e7883/go.mod h1:IeQDjWCNBj/QiWIPosfF6/kRC6pHPNs7W7LfBzjj+P4= github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= diff --git a/gossip/ethapi_backend.go b/gossip/ethapi_backend.go index f4873b71b..1d04aef90 100644 --- a/gossip/ethapi_backend.go +++ b/gossip/ethapi_backend.go @@ -536,3 +536,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() +} From 01a4aeef8b837bbcb6085d2c6cfaca9d44e8faeb Mon Sep 17 00:00:00 2001 From: Nicholas Pettas Date: Tue, 26 Mar 2024 08:51:36 -0700 Subject: [PATCH 2/3] return 503 on unhealthy response --- ethapi/api.go | 14 +++++++++++--- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/ethapi/api.go b/ethapi/api.go index 6d7ba5d7d..023daabe7 100644 --- a/ethapi/api.go +++ b/ethapi/api.go @@ -176,12 +176,20 @@ func (s *PublicEthereumAPI) Health(blockTimeSecThreshold uint64, uptimeSecThresh 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 uptime < uptimeThreshold { + return false, errors.New(fmt.Sprintf("Uptime threshold not reached")) + } + + if progress.CurrentBlock < progress.HighestBlock { + return false, errors.New(fmt.Sprintf("The current block is less than the highest block")) + } + + if peerCount < int(peerCountThreshold) { + return false, errors.New(fmt.Sprintf("Peer count threshold not reached")) } if blockTime > 0 && time.Since(progress.CurrentBlockTime.Time()) > blockTime { - return false, nil + return false, errors.New("block time threshold exceeded") } return true, nil diff --git a/go.mod b/go.mod index ec22eeb3a..58e8dfd4b 100644 --- a/go.mod +++ b/go.mod @@ -44,6 +44,6 @@ require ( gopkg.in/urfave/cli.v1 v1.20.0 ) -replace github.com/ethereum/go-ethereum => github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240322234738-3d5e1d6e7883 +replace github.com/ethereum/go-ethereum => github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240326154128-612f38eade68 replace github.com/dvyukov/go-fuzz => github.com/guzenok/go-fuzz v0.0.0-20210103140116-f9104dfb626f diff --git a/go.sum b/go.sum index a9903f0cc..77e8fa3ac 100644 --- a/go.sum +++ b/go.sum @@ -162,8 +162,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.10.8-ftm-rc9.0.20240322234738-3d5e1d6e7883 h1:ajz2flH7mV4U4WwXAP6XKssEXL8W4i53d/a/2QLPRdc= -github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240322234738-3d5e1d6e7883/go.mod h1:IeQDjWCNBj/QiWIPosfF6/kRC6pHPNs7W7LfBzjj+P4= +github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240326154128-612f38eade68 h1:CJNZXs0JDx5kafKqlWzQf7uFgxvtnrC5jr6sZWLiGfw= +github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240326154128-612f38eade68/go.mod h1:IeQDjWCNBj/QiWIPosfF6/kRC6pHPNs7W7LfBzjj+P4= github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= From 982bc7bb5ce077db29259a3e66590bb01d2679a3 Mon Sep 17 00:00:00 2001 From: Nicholas Pettas Date: Tue, 26 Mar 2024 09:56:49 -0700 Subject: [PATCH 3/3] fix 503 on unhealthy response --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 58e8dfd4b..b81fb8122 100644 --- a/go.mod +++ b/go.mod @@ -44,6 +44,6 @@ require ( gopkg.in/urfave/cli.v1 v1.20.0 ) -replace github.com/ethereum/go-ethereum => github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240326154128-612f38eade68 +replace github.com/ethereum/go-ethereum => github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240326165528-4d2aa2159de9 replace github.com/dvyukov/go-fuzz => github.com/guzenok/go-fuzz v0.0.0-20210103140116-f9104dfb626f diff --git a/go.sum b/go.sum index 77e8fa3ac..e71e5a44d 100644 --- a/go.sum +++ b/go.sum @@ -162,8 +162,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.10.8-ftm-rc9.0.20240326154128-612f38eade68 h1:CJNZXs0JDx5kafKqlWzQf7uFgxvtnrC5jr6sZWLiGfw= -github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240326154128-612f38eade68/go.mod h1:IeQDjWCNBj/QiWIPosfF6/kRC6pHPNs7W7LfBzjj+P4= +github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240326165528-4d2aa2159de9 h1:0V03X2gNOx60uRKxjWX1HnmWYt+QaM0Isr9S804jD+c= +github.com/faircrypto/go-ethereum v1.10.8-ftm-rc9.0.20240326165528-4d2aa2159de9/go.mod h1:IeQDjWCNBj/QiWIPosfF6/kRC6pHPNs7W7LfBzjj+P4= github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=