go-ethereum/health/check_time.go
crypto-services 96968b119e Initial cut
2024-02-15 16:25:35 +08:00

31 lines
503 B
Go

package health
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/ethereum/go-ethereum/ethclient"
)
var (
errTimestampTooOld = errors.New("timestamp too old")
)
func checkTime(
ec *ethclient.Client,
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
}