mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-22 03:30:37 +00:00
Fix beacon api handler (#8999)
This commit is contained in:
parent
a53b1d8b29
commit
f934ecbd6a
@ -57,7 +57,7 @@ func (r *beaconResponse) withFinalized(finalized bool) (out *beaconResponse) {
|
||||
out.Finalized = new(bool)
|
||||
out.ExecutionOptimistic = new(bool)
|
||||
out.Finalized = &finalized
|
||||
return r
|
||||
return out
|
||||
}
|
||||
|
||||
func (r *beaconResponse) withVersion(version clparams.StateVersion) (out *beaconResponse) {
|
||||
@ -65,7 +65,7 @@ func (r *beaconResponse) withVersion(version clparams.StateVersion) (out *beacon
|
||||
*out = *r
|
||||
out.Version = new(clparams.StateVersion)
|
||||
out.Version = &version
|
||||
return r
|
||||
return out
|
||||
}
|
||||
|
||||
//// In case of it being a json we need to also expose finalization, version, etc...
|
||||
|
@ -18,7 +18,7 @@ import (
|
||||
|
||||
type ApiHandler struct {
|
||||
o sync.Once
|
||||
mux chi.Router
|
||||
mux *chi.Mux
|
||||
|
||||
blockReader freezeblocks.BeaconSnapshotReader
|
||||
indiciesDB kv.RoDB
|
||||
|
@ -148,7 +148,8 @@ func (a *ApiHandler) getStateRoot(r *http.Request) (*beaconResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newBeaconResponse(&rootResponse{Root: stateRoot}).withFinalized(canonicalRoot == root && *slot <= a.forkchoiceStore.FinalizedSlot()), nil
|
||||
return newBeaconResponse(&rootResponse{Root: stateRoot}).
|
||||
withFinalized(canonicalRoot == root && *slot <= a.forkchoiceStore.FinalizedSlot()), nil
|
||||
}
|
||||
|
||||
func (a *ApiHandler) getFullState(r *http.Request) (*beaconResponse, error) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package beacon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
@ -39,7 +40,8 @@ func ListenAndServe(beaconHandler *LayeredBeaconHandler, routerCfg beacon_router
|
||||
mux.HandleFunc("/eth/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
nfw := ¬FoundNoWriter{rw: w}
|
||||
beaconHandler.ValidatorApi.ServeHTTP(nfw, r)
|
||||
if nfw.code == 404 || nfw.code == 0 {
|
||||
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, chi.NewRouteContext()))
|
||||
if isNotFound(nfw.code) || nfw.code == 0 {
|
||||
beaconHandler.ArchiveApi.ServeHTTP(w, r)
|
||||
}
|
||||
})
|
||||
@ -65,14 +67,3 @@ func ListenAndServe(beaconHandler *LayeredBeaconHandler, routerCfg beacon_router
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newBeaconMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
if contentType != "application/json" && contentType != "" {
|
||||
http.Error(w, "Content-Type header must be application/json", http.StatusUnsupportedMediaType)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
@ -11,11 +11,18 @@ type notFoundNoWriter struct {
|
||||
headers http.Header
|
||||
}
|
||||
|
||||
func isNotFound(code int) bool {
|
||||
return code == 404 || code == 405
|
||||
}
|
||||
|
||||
func (f *notFoundNoWriter) Header() http.Header {
|
||||
if f.code == 404 {
|
||||
if isNotFound(f.code) {
|
||||
return make(http.Header)
|
||||
}
|
||||
return f.rw.Header()
|
||||
if f.headers == nil {
|
||||
f.headers = make(http.Header)
|
||||
}
|
||||
return f.headers
|
||||
}
|
||||
|
||||
func (f *notFoundNoWriter) Write(xs []byte) (int, error) {
|
||||
@ -23,7 +30,7 @@ func (f *notFoundNoWriter) Write(xs []byte) (int, error) {
|
||||
if f.code == 0 {
|
||||
f.WriteHeader(200)
|
||||
}
|
||||
if f.code == 404 {
|
||||
if isNotFound(f.code) {
|
||||
return 0, nil
|
||||
}
|
||||
// pass on the write
|
||||
@ -34,9 +41,19 @@ func (f *notFoundNoWriter) WriteHeader(statusCode int) {
|
||||
if f.code != 0 {
|
||||
return
|
||||
}
|
||||
if f.code != 404 {
|
||||
f.rw.WriteHeader(statusCode)
|
||||
}
|
||||
// if it's a 404 and we are not at our last handler, set the target to an io.Discard
|
||||
f.code = statusCode
|
||||
if isNotFound(statusCode) {
|
||||
f.headers = nil
|
||||
return
|
||||
}
|
||||
f.rw.WriteHeader(statusCode)
|
||||
// if we get here, it means it is a successful write.
|
||||
if f.headers != nil {
|
||||
for k, v := range f.headers {
|
||||
for _, x := range v {
|
||||
f.rw.Header().Add(k, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
f.headers = f.rw.Header()
|
||||
}
|
||||
|
@ -17,18 +17,18 @@ type ValidatorApiHandler struct {
|
||||
GenesisCfg *clparams.GenesisConfig
|
||||
|
||||
o sync.Once
|
||||
mux chi.Router
|
||||
mux *chi.Mux
|
||||
}
|
||||
|
||||
func (v *ValidatorApiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
v.o.Do(func() {
|
||||
v.mux = chi.NewRouter()
|
||||
v.init(v.mux)
|
||||
v.Route(v.mux)
|
||||
})
|
||||
v.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (v *ValidatorApiHandler) init(r chi.Router) {
|
||||
func (v *ValidatorApiHandler) Route(r chi.Router) {
|
||||
r.Route("/eth", func(r chi.Router) {
|
||||
r.Route("/v1", func(r chi.Router) {
|
||||
r.Route("/beacon", func(r chi.Router) {
|
||||
|
Loading…
Reference in New Issue
Block a user