mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-24 12:37:16 +00:00
423b4f68ac
adding in unit tests for healthchecks
36 lines
589 B
Go
36 lines
589 B
Go
package health
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
|
)
|
|
|
|
var (
|
|
errTimestampTooOld = errors.New("timestamp too old")
|
|
)
|
|
|
|
func checkTime(
|
|
r *http.Request,
|
|
seconds int,
|
|
ethAPI EthAPI,
|
|
) error {
|
|
i, err := ethAPI.GetBlockByNumber(r.Context(), rpc.LatestBlockNumber, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
timestamp := 0
|
|
if ts, ok := i["timestamp"]; ok {
|
|
if cs, ok := ts.(uint64); ok {
|
|
timestamp = int(cs)
|
|
}
|
|
}
|
|
if timestamp > seconds {
|
|
return fmt.Errorf("%w: got ts: %d, need: %d", errTimestampTooOld, timestamp, seconds)
|
|
}
|
|
|
|
return nil
|
|
}
|