|
| 1 | +package beacon |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/OffchainLabs/prysm/v6/api" |
| 10 | + "github.com/OffchainLabs/prysm/v6/api/server/structs" |
| 11 | + "github.com/OffchainLabs/prysm/v6/beacon-chain/rpc/eth/shared" |
| 12 | + "github.com/OffchainLabs/prysm/v6/beacon-chain/rpc/lookup" |
| 13 | + "github.com/OffchainLabs/prysm/v6/encoding/ssz/query" |
| 14 | + "github.com/OffchainLabs/prysm/v6/monitoring/tracing/trace" |
| 15 | + "github.com/OffchainLabs/prysm/v6/network/httputil" |
| 16 | + sszquerypb "github.com/OffchainLabs/prysm/v6/proto/ssz_query" |
| 17 | + "github.com/OffchainLabs/prysm/v6/runtime/version" |
| 18 | +) |
| 19 | + |
| 20 | +// QueryBeaconState handles SSZ Query request for BeaconState. |
| 21 | +// Returns as bytes serialized SSZQueryResponse. |
| 22 | +func (s *Server) QueryBeaconState(w http.ResponseWriter, r *http.Request) { |
| 23 | + ctx, span := trace.StartSpan(r.Context(), "beacon.QueryBeaconState") |
| 24 | + defer span.End() |
| 25 | + |
| 26 | + stateID := r.PathValue("state_id") |
| 27 | + if stateID == "" { |
| 28 | + httputil.HandleError(w, "state_id is required in URL params", http.StatusBadRequest) |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + // Validate path before lookup: it might be expensive. |
| 33 | + var req structs.SSZQueryRequest |
| 34 | + err := json.NewDecoder(r.Body).Decode(&req) |
| 35 | + switch { |
| 36 | + case errors.Is(err, io.EOF): |
| 37 | + httputil.HandleError(w, "No data submitted", http.StatusBadRequest) |
| 38 | + return |
| 39 | + case err != nil: |
| 40 | + httputil.HandleError(w, "Could not decode request body: "+err.Error(), http.StatusBadRequest) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + if len(req.Query) == 0 { |
| 45 | + httputil.HandleError(w, "Empty query submitted", http.StatusBadRequest) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + path, err := query.ParsePath(req.Query) |
| 50 | + if err != nil { |
| 51 | + httputil.HandleError(w, "Could not parse path '"+req.Query+"': "+err.Error(), http.StatusBadRequest) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + stateRoot, err := s.Stater.StateRoot(ctx, []byte(stateID)) |
| 56 | + if err != nil { |
| 57 | + var rootNotFoundErr *lookup.StateRootNotFoundError |
| 58 | + if errors.As(err, &rootNotFoundErr) { |
| 59 | + httputil.HandleError(w, "State root not found: "+rootNotFoundErr.Error(), http.StatusNotFound) |
| 60 | + return |
| 61 | + } |
| 62 | + httputil.HandleError(w, "Could not get state root: "+err.Error(), http.StatusInternalServerError) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + st, err := s.Stater.State(ctx, []byte(stateID)) |
| 67 | + if err != nil { |
| 68 | + shared.WriteStateFetchError(w, err) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + // NOTE: Using unsafe conversion to proto is acceptable here, |
| 73 | + // as we play with a copy of the state returned by Stater. |
| 74 | + sszObject, ok := st.ToProtoUnsafe().(query.SSZObject) |
| 75 | + if !ok { |
| 76 | + httputil.HandleError(w, "Unsupported state version for querying: "+version.String(st.Version()), http.StatusBadRequest) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + info, err := query.AnalyzeObject(sszObject) |
| 81 | + if err != nil { |
| 82 | + httputil.HandleError(w, "Could not analyze state object: "+err.Error(), http.StatusInternalServerError) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + _, offset, length, err := query.CalculateOffsetAndLength(info, path) |
| 87 | + if err != nil { |
| 88 | + httputil.HandleError(w, "Could not calculate offset and length for path '"+req.Query+"': "+err.Error(), http.StatusInternalServerError) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + encodedState, err := st.MarshalSSZ() |
| 93 | + if err != nil { |
| 94 | + httputil.HandleError(w, "Could not marshal state to SSZ: "+err.Error(), http.StatusInternalServerError) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + response := &sszquerypb.SSZQueryResponse{ |
| 99 | + Root: stateRoot, |
| 100 | + Result: encodedState[offset : offset+length], |
| 101 | + } |
| 102 | + |
| 103 | + responseSsz, err := response.MarshalSSZ() |
| 104 | + if err != nil { |
| 105 | + httputil.HandleError(w, "Could not marshal response to SSZ: "+err.Error(), http.StatusInternalServerError) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + w.Header().Set(api.VersionHeader, version.String(st.Version())) |
| 110 | + httputil.WriteSsz(w, responseSsz) |
| 111 | +} |
| 112 | + |
| 113 | +// QueryBeaconState handles SSZ Query request for BeaconState. |
| 114 | +// Returns as bytes serialized SSZQueryResponse. |
| 115 | +func (s *Server) QueryBeaconBlock(w http.ResponseWriter, r *http.Request) { |
| 116 | + ctx, span := trace.StartSpan(r.Context(), "beacon.QueryBeaconBlock") |
| 117 | + defer span.End() |
| 118 | + |
| 119 | + blockId := r.PathValue("block_id") |
| 120 | + if blockId == "" { |
| 121 | + httputil.HandleError(w, "block_id is required in URL params", http.StatusBadRequest) |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + // Validate path before lookup: it might be expensive. |
| 126 | + var req structs.SSZQueryRequest |
| 127 | + err := json.NewDecoder(r.Body).Decode(&req) |
| 128 | + switch { |
| 129 | + case errors.Is(err, io.EOF): |
| 130 | + httputil.HandleError(w, "No data submitted", http.StatusBadRequest) |
| 131 | + return |
| 132 | + case err != nil: |
| 133 | + httputil.HandleError(w, "Could not decode request body: "+err.Error(), http.StatusBadRequest) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + if len(req.Query) == 0 { |
| 138 | + httputil.HandleError(w, "Empty query submitted", http.StatusBadRequest) |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + path, err := query.ParsePath(req.Query) |
| 143 | + if err != nil { |
| 144 | + httputil.HandleError(w, "Could not parse path '"+req.Query+"': "+err.Error(), http.StatusBadRequest) |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + signedBlock, err := s.Blocker.Block(ctx, []byte(blockId)) |
| 149 | + if !shared.WriteBlockFetchError(w, signedBlock, err) { |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + protoBlock, err := signedBlock.Block().Proto() |
| 154 | + if err != nil { |
| 155 | + httputil.HandleError(w, "Could not convert block to proto: "+err.Error(), http.StatusInternalServerError) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + block, ok := protoBlock.(query.SSZObject) |
| 160 | + if !ok { |
| 161 | + httputil.HandleError(w, "Unsupported block version for querying: "+version.String(signedBlock.Version()), http.StatusBadRequest) |
| 162 | + return |
| 163 | + } |
| 164 | + |
| 165 | + info, err := query.AnalyzeObject(block) |
| 166 | + if err != nil { |
| 167 | + httputil.HandleError(w, "Could not analyze block object: "+err.Error(), http.StatusInternalServerError) |
| 168 | + return |
| 169 | + } |
| 170 | + |
| 171 | + _, offset, length, err := query.CalculateOffsetAndLength(info, path) |
| 172 | + if err != nil { |
| 173 | + httputil.HandleError(w, "Could not calculate offset and length for path '"+req.Query+"': "+err.Error(), http.StatusInternalServerError) |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + encodedBlock, err := signedBlock.Block().MarshalSSZ() |
| 178 | + if err != nil { |
| 179 | + httputil.HandleError(w, "Could not marshal block to SSZ: "+err.Error(), http.StatusInternalServerError) |
| 180 | + return |
| 181 | + } |
| 182 | + |
| 183 | + blockRoot, err := block.HashTreeRoot() |
| 184 | + if err != nil { |
| 185 | + httputil.HandleError(w, "Could not compute block root: "+err.Error(), http.StatusInternalServerError) |
| 186 | + return |
| 187 | + } |
| 188 | + |
| 189 | + response := &sszquerypb.SSZQueryResponse{ |
| 190 | + Root: blockRoot[:], |
| 191 | + Result: encodedBlock[offset : offset+length], |
| 192 | + } |
| 193 | + |
| 194 | + responseSsz, err := response.MarshalSSZ() |
| 195 | + if err != nil { |
| 196 | + httputil.HandleError(w, "Could not marshal response to SSZ: "+err.Error(), http.StatusInternalServerError) |
| 197 | + return |
| 198 | + } |
| 199 | + |
| 200 | + w.Header().Set(api.VersionHeader, version.String(signedBlock.Version())) |
| 201 | + httputil.WriteSsz(w, responseSsz) |
| 202 | +} |
0 commit comments