go-ethereum/health/check_time.go
crypto-services cc3cd2f775 Add testing
2024-02-20 14:35:41 +08:00

29 lines
449 B
Go

package health
import (
"context"
"errors"
"fmt"
"net/http"
)
var (
errTimestampTooOld = errors.New("timestamp too old")
)
func checkTime(
ec ethClient,
r *http.Request,
seconds int,
) error {
i, err := ec.BlockByNumber(context.TODO(), nil)
if err != nil {
return err
}
timestamp := i.Time()
if timestamp < uint64(seconds) {
return fmt.Errorf("%w: got ts: %d, need: %d", errTimestampTooOld, timestamp, seconds)
}
return nil
}