Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inspect package #580

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions internal/inspect/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// (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")
ErrNoApp = errors.New("no machine for application")
)

type Inspector 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 Inspector.
func New(machines Machines) (*Inspector, error) {
if machines == nil {
return nil, ErrInvalidMachines
}

return &Inspector{machines: machines}, nil
}

func (inspect *Inspector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var (
dapp Address
payload []byte
err error
reports []string
status string
errorMessage string
)

if r.PathValue("dapp") == "" {
GMKrieger marked this conversation as resolved.
Show resolved Hide resolved
slog.Info("Bad request",
"service", "inspect",
"err", "Missing application address")
http.Error(w, "Missing application address", http.StatusBadRequest)
return
}

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 {
if r.PathValue("payload") == "" {
GMKrieger marked this conversation as resolved.
Show resolved Hide resolved
slog.Info("Bad request",
"service", "inspect",
"err", "Missing payload")
http.Error(w, "Missing payload", http.StatusBadRequest)
return
}
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.StatusBadRequest)
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 = "Rejected"
}

if result.Error != nil {
status = "Exception"
errorMessage = fmt.Sprintf("Error on the machine while inspecting: %s", result.Error)
}

response := InspectResponse{
Status: status,
Exception: errorMessage,
Reports: reports,
InputIndex: *result.InputIndex,
}

w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response)
if err != nil {
slog.Info("Internal server error",
"service", "inspect",
"err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

// process sends an inspect request to the machine
func (inspect *Inspector) 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 {
return nil, 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)
}
Loading
Loading