-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
429 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package inspect | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/cartesi/rollups-node/internal/node/advancer/machines" | ||
. "github.com/cartesi/rollups-node/internal/node/model" | ||
"github.com/cartesi/rollups-node/internal/nodemachine" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
) | ||
|
||
var ( | ||
ErrInvalidMachines = errors.New("machines must not be nil") | ||
ErrInvalidRepository = errors.New("repository must not be nil") | ||
|
||
ErrNoApp = errors.New("no machine for application") | ||
ErrBadRequest = errors.New("inspect bad request") | ||
) | ||
|
||
type Inspect struct { | ||
machines Machines | ||
} | ||
|
||
type InspectResponse struct { | ||
Status string `json:"status"` | ||
Exception string `json:"exception"` | ||
Reports []string `json:"reports"` | ||
InputIndex uint64 `json:"processed_input_count"` | ||
} | ||
|
||
// New instantiates a new Inspect. | ||
func New(machines Machines) (*Inspect, error) { | ||
if machines == nil { | ||
return nil, ErrInvalidMachines | ||
} | ||
|
||
return &Inspect{machines: machines}, nil | ||
} | ||
|
||
func (inspect *Inspect) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
var ( | ||
dapp Address | ||
payload []byte | ||
err error | ||
reports []string | ||
status string | ||
) | ||
|
||
dapp = common.HexToAddress(r.PathValue("dapp")) | ||
if r.Method == "POST" { | ||
payload, err = io.ReadAll(r.Body) | ||
if err != nil { | ||
slog.Info("Bad request", | ||
"service", "inspect", | ||
"err", err) | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
} else { | ||
payload, err = hexutil.Decode(r.PathValue("payload")) | ||
if err != nil { | ||
slog.Info("Internal server error", | ||
"service", "inspect", | ||
"err", err) | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
result, err := inspect.process(r.Context(), dapp, payload) | ||
if err != nil { | ||
slog.Info("Internal server error", | ||
"service", "inspect", | ||
"err", err) | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
for _, report := range result.Reports { | ||
reports = append(reports, hexutil.Encode(report)) | ||
} | ||
|
||
if result.Accepted { | ||
status = "Accepted" | ||
} else { | ||
status = "Refused" | ||
} | ||
|
||
response := InspectResponse{ | ||
Status: status, | ||
Exception: fmt.Sprintf("Error on the machine while inspecting: %s", result.Error), | ||
Reports: reports, | ||
InputIndex: *result.InputIndex, | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(response) | ||
} | ||
|
||
// process sends an inspect request to the machine | ||
func (inspect *Inspect) process(ctx context.Context, app Address, query []byte) (*nodemachine.InspectResult, error) { | ||
// Asserts that the app has an associated machine. | ||
machine := inspect.machines.GetInspectMachine(app) | ||
if machine == nil { | ||
panic(fmt.Errorf("%w %s", ErrNoApp, app.String())) | ||
} | ||
|
||
res, err := machine.Inspect(ctx, query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
|
||
type Machines interface { | ||
GetInspectMachine(app Address) machines.InspectMachine | ||
} | ||
|
||
type Machine interface { | ||
Inspect(_ context.Context, query []byte) (*nodemachine.InspectResult, error) | ||
} |
Oops, something went wrong.