Skip to content

Commit

Permalink
feat: refactor opsimulator to intercept rpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
tremarkley committed Jul 17, 2024
1 parent 65b029d commit aa215f0
Showing 1 changed file with 44 additions and 1 deletion.
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()
}

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

proxy.ServeHTTP(w, r)
}
}

// TODO: 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

0 comments on commit aa215f0

Please sign in to comment.