2023-07-24 22:16:45 +00:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
2023-08-08 22:54:04 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-07-24 22:16:45 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2023-08-08 22:54:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/blockchain"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/sync"
|
2023-07-24 22:16:45 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
2023-07-31 17:32:39 +00:00
|
|
|
http2 "github.com/prysmaticlabs/prysm/v4/network/http"
|
2023-07-24 22:16:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ValidateHex(w http.ResponseWriter, name string, s string) bool {
|
|
|
|
if s == "" {
|
2023-07-31 17:32:39 +00:00
|
|
|
errJson := &http2.DefaultErrorJson{
|
2023-07-24 22:16:45 +00:00
|
|
|
Message: name + " is required",
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
}
|
2023-07-31 17:32:39 +00:00
|
|
|
http2.WriteError(w, errJson)
|
2023-07-24 22:16:45 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !bytesutil.IsHex([]byte(s)) {
|
2023-07-31 17:32:39 +00:00
|
|
|
errJson := &http2.DefaultErrorJson{
|
2023-07-24 22:16:45 +00:00
|
|
|
Message: name + " is invalid",
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
}
|
2023-07-31 17:32:39 +00:00
|
|
|
http2.WriteError(w, errJson)
|
2023-07-24 22:16:45 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidateUint(w http.ResponseWriter, name string, s string) (uint64, bool) {
|
|
|
|
if s == "" {
|
2023-07-31 17:32:39 +00:00
|
|
|
errJson := &http2.DefaultErrorJson{
|
2023-07-24 22:16:45 +00:00
|
|
|
Message: name + " is required",
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
}
|
2023-07-31 17:32:39 +00:00
|
|
|
http2.WriteError(w, errJson)
|
2023-07-24 22:16:45 +00:00
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseUint(s, 10, 64)
|
|
|
|
if err != nil {
|
2023-07-31 17:32:39 +00:00
|
|
|
errJson := &http2.DefaultErrorJson{
|
2023-07-24 22:16:45 +00:00
|
|
|
Message: name + " is invalid: " + err.Error(),
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
}
|
2023-07-31 17:32:39 +00:00
|
|
|
http2.WriteError(w, errJson)
|
2023-07-24 22:16:45 +00:00
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return v, true
|
|
|
|
}
|
2023-08-08 22:54:04 +00:00
|
|
|
|
|
|
|
// IsSyncing checks whether the beacon node is currently syncing and writes out the sync status.
|
|
|
|
func IsSyncing(
|
|
|
|
ctx context.Context,
|
|
|
|
w http.ResponseWriter,
|
|
|
|
syncChecker sync.Checker,
|
|
|
|
headFetcher blockchain.HeadFetcher,
|
|
|
|
timeFetcher blockchain.TimeFetcher,
|
|
|
|
optimisticModeFetcher blockchain.OptimisticModeFetcher,
|
|
|
|
) bool {
|
|
|
|
if !syncChecker.Syncing() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
headSlot := headFetcher.HeadSlot()
|
|
|
|
isOptimistic, err := optimisticModeFetcher.IsOptimistic(ctx)
|
|
|
|
if err != nil {
|
|
|
|
errJson := &http2.DefaultErrorJson{
|
|
|
|
Message: "Could not check optimistic status: " + err.Error(),
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
}
|
|
|
|
http2.WriteError(w, errJson)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
syncDetails := &SyncDetailsContainer{
|
|
|
|
Data: &SyncDetails{
|
|
|
|
HeadSlot: strconv.FormatUint(uint64(headSlot), 10),
|
|
|
|
SyncDistance: strconv.FormatUint(uint64(timeFetcher.CurrentSlot()-headSlot), 10),
|
|
|
|
IsSyncing: true,
|
|
|
|
IsOptimistic: isOptimistic,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := "Beacon node is currently syncing and not serving request on that endpoint"
|
|
|
|
details, err := json.Marshal(syncDetails)
|
|
|
|
if err == nil {
|
|
|
|
msg += " Details: " + string(details)
|
|
|
|
}
|
|
|
|
errJson := &http2.DefaultErrorJson{
|
|
|
|
Message: msg,
|
|
|
|
Code: http.StatusServiceUnavailable}
|
|
|
|
http2.WriteError(w, errJson)
|
|
|
|
return true
|
|
|
|
}
|