-
Notifications
You must be signed in to change notification settings - Fork 48
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
1 parent
a209230
commit 91ceacb
Showing
4 changed files
with
169 additions
and
22 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
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,90 @@ | ||
package op_simulator | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
"strconv" | ||
"sync/atomic" | ||
|
||
ophttp "github.com/ethereum-optimism/optimism/op-service/httputil" | ||
"github.com/ethereum-optimism/supersim/anvil" | ||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
type Config struct { | ||
Port uint64 | ||
} | ||
|
||
type OpSimulator struct { | ||
Log log.Logger | ||
Anvil *anvil.Anvil | ||
HttpServer *ophttp.HTTPServer | ||
|
||
stopped atomic.Bool | ||
|
||
cfg *Config | ||
} | ||
|
||
const ( | ||
host = "127.0.0.1" | ||
) | ||
|
||
func New(log log.Logger, cfg *Config, anvil *anvil.Anvil) *OpSimulator { | ||
return &OpSimulator{ | ||
Log: log, | ||
cfg: cfg, | ||
Anvil: anvil, | ||
} | ||
} | ||
|
||
func (opSim *OpSimulator) Start(ctx context.Context) error { | ||
proxy, err := opSim.createReverseProxy() | ||
if err != nil { | ||
return fmt.Errorf("Error creating reverse proxy: %w", err) | ||
} | ||
mux := http.NewServeMux() | ||
mux.Handle("/", proxy) | ||
endpoint := net.JoinHostPort(host, strconv.Itoa(int(opSim.cfg.Port))) | ||
opSim.Log.Info("Starting simulator", "endpoint", endpoint) | ||
|
||
hs, err := ophttp.StartHTTPServer(endpoint, mux) | ||
if err != nil { | ||
return fmt.Errorf("failed to start HTTP RPC server: %w", err) | ||
} | ||
opSim.HttpServer = hs | ||
|
||
return nil | ||
} | ||
|
||
func (opSim *OpSimulator) Stop(ctx context.Context) error { | ||
if opSim.stopped.Load() { | ||
return errors.New("already stopped") | ||
} | ||
if !opSim.stopped.CompareAndSwap(false, true) { | ||
return nil // someone else stopped | ||
} | ||
|
||
return opSim.HttpServer.Stop(ctx) | ||
} | ||
|
||
func (a *OpSimulator) Stopped() bool { | ||
return a.stopped.Load() | ||
} | ||
|
||
func (opSim *OpSimulator) createReverseProxy() (*httputil.ReverseProxy, error) { | ||
targetURL, err := url.Parse(opSim.Anvil.Endpoint()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse target URL: %w", err) | ||
} | ||
proxy := &httputil.ReverseProxy{ | ||
Rewrite: func(r *httputil.ProxyRequest) { | ||
r.SetURL(targetURL) | ||
}, | ||
} | ||
return proxy, nil | ||
} |
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
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