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

feat: add rpc intercept to opsimulator #54

Merged
merged 1 commit into from
Jul 17, 2024
Merged
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
45 changes: 44 additions & 1 deletion opsimulator/opsimulator.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package opsimulator

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -37,6 +40,13 @@ type OpSimulator struct {
cfg *Config
}

type JSONRPCRequest struct {
Method string `json:"method"`
Params []interface{} `json:"params"`
ID int `json:"id"`
JSONRPC string `json:"jsonrpc"`
}

func New(log log.Logger, cfg *Config, l1Chain chainapi.Chain, l2Chain chainapi.Chain) *OpSimulator {
return &OpSimulator{
log: log,
Expand All @@ -53,7 +63,7 @@ func (opSim *OpSimulator) Start(ctx context.Context) error {
}

mux := http.NewServeMux()
mux.Handle("/", proxy)
mux.Handle("/", handler(proxy))

hs, err := ophttp.StartHTTPServer(net.JoinHostPort(host, fmt.Sprintf("%d", opSim.cfg.Port)), mux)
if err != nil {
Expand Down Expand Up @@ -90,6 +100,39 @@ func (a *OpSimulator) Stopped() bool {
return a.stopped.Load()
}

func handler(proxy *httputil.ReverseProxy) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
defer r.Body.Close()

var req JSONRPCRequest
err = json.Unmarshal(body, &req)
if err != nil {
http.Error(w, "Failed to parse JSON-RPC request", http.StatusBadRequest)
return
}

// TODO(https://github.com/ethereum-optimism/supersim/issues/55): support batch txs

if req.Method == "eth_sendRawTransaction" {
checkInteropInvariants()
tremarkley marked this conversation as resolved.
Show resolved Hide resolved
}

r.Body = io.NopCloser(bytes.NewReader(body))

proxy.ServeHTTP(w, r)
}
}

// TODO(https://github.com/ethereum-optimism/supersim/issues/19): add logic for checking that an interop transaction is valid.
func checkInteropInvariants() bool {
return true
}

func (opSim *OpSimulator) createReverseProxy() (*httputil.ReverseProxy, error) {
targetURL, err := url.Parse(opSim.l2Chain.Endpoint())
if err != nil {
Expand Down
Loading