-
Notifications
You must be signed in to change notification settings - Fork 70
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: create op-simulator as basic proxy #33
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,99 @@ | ||
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))) | ||
|
||
hs, err := ophttp.StartHTTPServer(endpoint, mux) | ||
if err != nil { | ||
return fmt.Errorf("failed to start HTTP RPC server: %w", err) | ||
} | ||
opSim.log.Info(fmt.Sprintf("listening on %v", endpoint), "chain.id", opSim.ChainId()) | ||
|
||
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 | ||
} | ||
|
||
func (opSim *OpSimulator) Endpoint() string { | ||
return fmt.Sprintf("http://%s:%d", host, opSim.cfg.Port) | ||
} | ||
|
||
func (opSim *OpSimulator) ChainId() uint64 { | ||
return opSim.anvil.ChainId() | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
In order to simplify the changes in this file, I wonder if we should simply have OpSimulator manage the lifecycle of the the underlying chain, in this case anvil. So that we dont need reference to each individual pieces here
Not worth addressing in the PR. Just some food for thought for ways to clean up this code in the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I wont address in this PR, since I want to keep this change small.
If we want to stick to the original design, we should create an
Orchestrator
service that handles spinning up all the anvil instances andOpSimulator
instances.