-
Notifications
You must be signed in to change notification settings - Fork 5
feat: integrate fault-proof challenger into sysgo #326
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
Merged
Farhad-Shabani
merged 5 commits into
op-succinct-sysgo
from
farhad/l2-challenger-faultproof
Jan 5, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a40c937
feat: integrate fp challenger into sysgo
Farhad-Shabani 75186a8
fix: rm redundant service field
Farhad-Shabani 466fb1e
chore: rename interfaces to ***Backend
Farhad-Shabani d4c8374
fix: consistent metrics registration
Farhad-Shabani 0449578
fix: consistent logging
Farhad-Shabani 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 hidden or 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 hidden or 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,272 @@ | ||
| package sysgo | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "sync" | ||
| "syscall" | ||
|
|
||
| "github.com/ethereum-optimism/optimism/op-chain-ops/devkeys" | ||
| "github.com/ethereum-optimism/optimism/op-devstack/devtest" | ||
| "github.com/ethereum-optimism/optimism/op-devstack/shim" | ||
| "github.com/ethereum-optimism/optimism/op-devstack/stack" | ||
| "github.com/ethereum-optimism/optimism/op-service/logpipe" | ||
| "github.com/ethereum/go-ethereum/common/hexutil" | ||
| "github.com/ethereum/go-ethereum/crypto" | ||
| "github.com/ethereum/go-ethereum/log" | ||
| ) | ||
|
|
||
| // L2SuccinctFaultProofChallenger wraps the OP Succinct fault-proof challenger binary as a subprocess. | ||
| type L2SuccinctFaultProofChallenger struct { | ||
| mu sync.Mutex | ||
| id stack.L2ChallengerID | ||
| execPath string | ||
| args []string | ||
| p devtest.P | ||
| logger log.Logger | ||
| sub *SubProcess | ||
| l2MetricsRegistrar L2MetricsRegistrar | ||
| metricsPort string | ||
| } | ||
|
|
||
| var _ L2ChallengerBackend = (*L2SuccinctFaultProofChallenger)(nil) | ||
|
|
||
| // FaultProofChallenger extends L2ChallengerBackend with faultproof-specific methods. | ||
| type FaultProofChallenger interface { | ||
| L2ChallengerBackend | ||
| Start() | ||
| Stop() | ||
| } | ||
|
|
||
| var _ FaultProofChallenger = (*L2SuccinctFaultProofChallenger)(nil) | ||
|
|
||
| func (c *L2SuccinctFaultProofChallenger) hydrate(system stack.ExtensibleSystem) { | ||
| bFrontend := shim.NewL2Challenger(shim.L2ChallengerConfig{ | ||
| CommonConfig: shim.NewCommonConfig(system.T()), | ||
| ID: c.id, | ||
| Config: nil, // Succinct challenger runs as subprocess, no op-challenger config | ||
| }) | ||
| l2Net := system.L2Network(stack.L2NetworkID(c.id.ChainID())) | ||
| l2Net.(stack.ExtensibleL2Network).AddL2Challenger(bFrontend) | ||
| } | ||
|
|
||
| // Start starts the fault-proof challenger subprocess. | ||
| func (c *L2SuccinctFaultProofChallenger) Start() { | ||
| c.mu.Lock() | ||
| if c.sub != nil { | ||
| c.logger.Warn("Fault Proof Challenger already started") | ||
| c.mu.Unlock() | ||
| return | ||
| } | ||
|
|
||
| // We pipe sub-process logs to the test-logger. | ||
| logOut := logpipe.ToLogger(c.logger.New("src", "stdout")) | ||
| logErr := logpipe.ToLogger(c.logger.New("src", "stderr")) | ||
|
|
||
| stdOutLogs := logpipe.LogProcessor(func(line []byte) { | ||
| e := logpipe.ParseRustStructuredLogs(line) | ||
| logOut(e) | ||
| }) | ||
| stdErrLogs := logpipe.LogProcessor(func(line []byte) { | ||
| e := logpipe.ParseRustStructuredLogs(line) | ||
| logErr(e) | ||
| }) | ||
| c.sub = NewSubProcess(c.p, stdOutLogs, stdErrLogs) | ||
| c.mu.Unlock() | ||
|
|
||
| c.sub.OnExit(func(err error) { | ||
| if errors.Is(err, syscall.ECHILD) { | ||
| return | ||
| } | ||
|
|
||
| var exitErr *exec.ExitError | ||
| if errors.As(err, &exitErr) { | ||
| if ws, ok := exitErr.Sys().(syscall.WaitStatus); ok { | ||
| sig := ws.Signal() | ||
| if sig == syscall.SIGINT || sig == syscall.SIGTERM { | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| c.p.Require().NoError(err, "fault-proof challenger exited unexpectedly") | ||
| }) | ||
|
|
||
| err := c.sub.Start(c.execPath, c.args, []string{}) | ||
| c.p.Require().NoError(err, "Must start challenger") | ||
|
|
||
| if c.metricsPort != "" && c.l2MetricsRegistrar != nil { | ||
| metricsTarget := NewPrometheusMetricsTarget("localhost", c.metricsPort, false) | ||
| c.l2MetricsRegistrar.RegisterL2MetricsTargets(c.id, metricsTarget) | ||
| c.logger.Info("Registered fault-proof challenger metrics", "port", c.metricsPort) | ||
| } | ||
| } | ||
|
|
||
| // Stop stops the fault-proof challenger subprocess. | ||
| func (c *L2SuccinctFaultProofChallenger) Stop() { | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
| if c.sub == nil { | ||
| c.logger.Warn("fault-proof challenger already stopped") | ||
| return | ||
| } | ||
|
|
||
| err := c.sub.Stop(true) | ||
| c.p.Require().NoError(err, "Must stop challenger") | ||
| c.sub = nil | ||
| } | ||
|
|
||
| // WithSuccinctFaultProofChallenger creates a fault-proof challenger after deployment. | ||
| func WithSuccinctFaultProofChallenger(challengerID stack.L2ChallengerID, l1ELID stack.L1ELNodeID, l2ELID stack.L2ELNodeID, opts ...FaultProofChallengerOption) stack.Option[*Orchestrator] { | ||
| return stack.AfterDeploy(func(orch *Orchestrator) { | ||
| WithSuccinctFaultProofChallengerPostDeploy(orch, challengerID, l1ELID, l2ELID, opts...) | ||
| }) | ||
| } | ||
|
|
||
| // WithSuperSuccinctFaultProofChallenger creates a fault-proof challenger in the Finally phase. | ||
| func WithSuperSuccinctFaultProofChallenger(challengerID stack.L2ChallengerID, | ||
| l1ELID stack.L1ELNodeID, l2ELID stack.L2ELNodeID, opts ...FaultProofChallengerOption) stack.Option[*Orchestrator] { | ||
| return stack.Finally(func(orch *Orchestrator) { | ||
| WithSuccinctFaultProofChallengerPostDeploy(orch, challengerID, l1ELID, l2ELID, opts...) | ||
| }) | ||
| } | ||
|
|
||
| // WithSuccinctFaultProofChallengerPostDeploy sets up and starts the OP Succinct fault-proof challenger. | ||
| func WithSuccinctFaultProofChallengerPostDeploy(orch *Orchestrator, challengerID stack.L2ChallengerID, l1ELID stack.L1ELNodeID, l2ELID stack.L2ELNodeID, opts ...FaultProofChallengerOption) { | ||
| ctx := stack.ContextWithID(orch.P().Ctx(), challengerID) | ||
| p := orch.P().WithCtx(ctx) | ||
| logger := p.Logger().New("component", "succinct-fp-challenger") | ||
|
|
||
| require := p.Require() | ||
| require.False(orch.challengers.Has(challengerID), "challenger must not already exist") | ||
|
|
||
| l2Net, ok := orch.l2Nets.Get(challengerID.ChainID()) | ||
| require.True(ok, "l2 network required") | ||
|
|
||
| l1EL, ok := orch.GetL1EL(l1ELID) | ||
| require.True(ok, "l1 EL node required") | ||
|
|
||
| l2EL, ok := orch.GetL2EL(l2ELID) | ||
| require.True(ok, "l2 EL node required") | ||
|
|
||
| // Use ChallengerRole for the challenger key | ||
| challengerKey, err := orch.GetKeys().Secret(devkeys.ChallengerRole.Key(challengerID.ChainID().ToBig())) | ||
| require.NoError(err, "failed to get challenger key") | ||
| challengerKeyStr := hexutil.Encode(crypto.FromECDSA(challengerKey)) | ||
|
|
||
| cfg := &FaultProofChallengerConfig{} | ||
| for _, opt := range opts { | ||
| opt(p, challengerID, cfg) | ||
| } | ||
|
|
||
| l1RPC := l1EL.UserRPC() | ||
| l2RPC := strings.ReplaceAll(l2EL.UserRPC(), "ws://", "http://") | ||
| anchorStateRegistryAddr := l2Net.deployment.anchorStateRegistry | ||
| factoryAddr := l2Net.deployment.disputeGameFactoryProxy | ||
|
|
||
| logger.Info("L1_RPC", "url", l1RPC) | ||
| logger.Info("L2_RPC", "url", l2RPC) | ||
| logger.Info("ANCHOR_STATE_REGISTRY_ADDRESS", "address", anchorStateRegistryAddr) | ||
| logger.Info("FACTORY_ADDRESS", "address", factoryAddr) | ||
|
|
||
| envVars := map[string]string{ | ||
| "L1_RPC": l1RPC, | ||
| "L2_RPC": l2RPC, | ||
| "ANCHOR_STATE_REGISTRY_ADDRESS": anchorStateRegistryAddr.String(), | ||
| "FACTORY_ADDRESS": factoryAddr.String(), | ||
| "GAME_TYPE": "42", | ||
| "PRIVATE_KEY": challengerKeyStr, | ||
| "LOG_FORMAT": "json", | ||
| } | ||
|
|
||
| // Optional parameters (override defaults if set) | ||
| setEnvIfNotNil(envVars, "FETCH_INTERVAL", cfg.fetchInterval) | ||
| setEnvIfNotNil(envVars, "MALICIOUS_CHALLENGE_PERCENTAGE", cfg.maliciousChallengePercentage) | ||
| setEnvIfNotNil(envVars, "RUST_LOG", cfg.rustLog) | ||
|
|
||
| var metricsPort string | ||
| if areMetricsEnabled() { | ||
| metricsPort, err = getAvailableLocalPort() | ||
| require.NoError(err, "failed to get available port for challenger metrics") | ||
| envVars["CHALLENGER_METRICS_PORT"] = metricsPort | ||
| } | ||
|
|
||
| envDir := p.TempDir() | ||
| envFile := filepath.Join(envDir, fmt.Sprintf("fp-challenger-%s.env", challengerID.String())) | ||
| err = WriteEnvFile(envFile, envVars) | ||
| p.Require().NoError(err, "must write fault proof challenger env file") | ||
|
|
||
| if cfg.envFilePath != nil { | ||
| err = WriteEnvFile(*cfg.envFilePath, envVars) | ||
| p.Require().NoError(err, "must write challenger env file") | ||
| logger.Info("challenger env file written", "path", *cfg.envFilePath) | ||
| } | ||
|
|
||
| execPath := os.Getenv("FAULT_PROOF_CHALLENGER_EXEC_PATH") | ||
| p.Require().NotEmpty(execPath, "FAULT_PROOF_CHALLENGER_EXEC_PATH environment variable must be set") | ||
| _, err = os.Stat(execPath) | ||
| p.Require().NotErrorIs(err, os.ErrNotExist, "challenger executable must exist") | ||
|
|
||
| c := &L2SuccinctFaultProofChallenger{ | ||
| id: challengerID, | ||
| execPath: execPath, | ||
| args: []string{"--env-file", envFile}, | ||
| p: p, | ||
| logger: logger, | ||
| l2MetricsRegistrar: orch, | ||
| metricsPort: metricsPort, | ||
| } | ||
| logger.Info("Starting fault-proof challenger") | ||
| c.Start() | ||
| p.Cleanup(func() { | ||
| logger.Info("Stopping fault-proof challenger") | ||
| c.Stop() | ||
| }) | ||
| logger.Info("fault-proof challenger is running") | ||
|
|
||
| // Store the challenger in the orchestrator's challengers map | ||
| require.True(orch.challengers.SetIfMissing(challengerID, c), "challenger must not already exist") | ||
| } | ||
|
|
||
| // FaultProofChallengerConfig holds configuration for the OP Succinct fault-proof challenger. | ||
| type FaultProofChallengerConfig struct { | ||
| fetchInterval *uint64 | ||
| maliciousChallengePercentage *float64 | ||
| rustLog *string | ||
| envFilePath *string | ||
| } | ||
|
|
||
| // FaultProofChallengerOption is a function that configures the FaultProofChallengerConfig. | ||
| type FaultProofChallengerOption func(p devtest.P, id stack.L2ChallengerID, cfg *FaultProofChallengerConfig) | ||
|
|
||
| // WithFPChallengerFetchInterval sets the polling interval in seconds. | ||
| func WithFPChallengerFetchInterval(n uint64) FaultProofChallengerOption { | ||
| return FaultProofChallengerOption(func(p devtest.P, id stack.L2ChallengerID, cfg *FaultProofChallengerConfig) { | ||
| cfg.fetchInterval = &n | ||
| }) | ||
| } | ||
|
|
||
| // WithFPChallengerMaliciousChallengePercentage sets the percentage of valid games to challenge maliciously (for testing). | ||
| func WithFPChallengerMaliciousChallengePercentage(pct float64) FaultProofChallengerOption { | ||
| return FaultProofChallengerOption(func(p devtest.P, id stack.L2ChallengerID, cfg *FaultProofChallengerConfig) { | ||
| cfg.maliciousChallengePercentage = &pct | ||
| }) | ||
| } | ||
|
|
||
| // WithFPChallengerRustLog sets the RUST_LOG environment variable. | ||
| func WithFPChallengerRustLog(level string) FaultProofChallengerOption { | ||
| return FaultProofChallengerOption(func(p devtest.P, id stack.L2ChallengerID, cfg *FaultProofChallengerConfig) { | ||
| cfg.rustLog = &level | ||
| }) | ||
| } | ||
|
|
||
| // WithFPChallengerWriteEnvFile enables writing environment variables to a file. | ||
| func WithFPChallengerWriteEnvFile(path string) FaultProofChallengerOption { | ||
| return FaultProofChallengerOption(func(p devtest.P, id stack.L2ChallengerID, cfg *FaultProofChallengerConfig) { | ||
| cfg.envFilePath = &path | ||
| }) | ||
| } | ||
This file contains hidden or 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 hidden or 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.
The lock is not held before c.sub.Start() is called. I think this could cause race when concurrent Start()/Stop() calls are made. Maybe low priority if we don't need such tests to be covered.
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.
Good catch. A proper fix isn't trivial though: holding the lock through
Start()risks deadlocks sincesub.Start()can block andOnExitruns async. Given current usage (single Start/Stop, no concurrency), I'll leave as-is for now.