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

supersim: config refactors #52

Merged
merged 4 commits into from
Jul 18, 2024
Merged

supersim: config refactors #52

merged 4 commits into from
Jul 18, 2024

Conversation

hamdiallam
Copy link
Contributor

@hamdiallam hamdiallam commented Jul 17, 2024

Not every package needs to define it's external configuration and can instead be shared.

  1. Expand on the config package and fold chainapi into config
  2. Make startup start/stop logs less noisy. Switch a lot of detail logs to debug so that they are available if needed

Summary by CodeRabbit

  • New Features

    • Introduced WebSocket endpoint support.
    • Added name handling for configurations and simulator components.
  • Bug Fixes

    • Improved logging with more detailed information.
  • Refactor

    • Consolidated configuration handling into a single ChainConfig structure.
    • Updated method signatures to accept ChainConfig instead of individual parameters.
    • Simplified error handling and initialization logic in multiple components.
  • Tests

    • Adjusted test configurations to align with the refactored ChainConfig structure.
  • Chores

    • Removed unused imports and variables for cleaner codebase.

@hamdiallam hamdiallam requested a review from a team as a code owner July 17, 2024 19:10
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range, codebase verification and nitpick comments (3)
orchestrator/fork.go (1)

36-41: Ensure consistency in struct initialization.

When initializing config.ChainConfig, it's good practice to specify field names for all fields to avoid future errors if the struct definition changes.

- chainConfigs = append(chainconfigs, config.ChainConfig{
+ chainConfigs = append(chainConfigs, config.ChainConfig{
+     Port: 0,
+     Name: cfg.Network,
+     ChainID: superchain.Config.L1.ChainID,
+     SecretsConfig: config.DefaultSecretsConfig,
+     ForkConfig: &config.ForkConfig{
+         RPCUrl: superchain.Config.L1.PublicRPC,
+         BlockNumber: l1Header.Number.Uint64(),
+     },
+ })
orchestrator/orchestrator.go (1)

Update test file to match new function signature

The orchestrator/orchestrator_test.go file is still using the old OrchestratorConfig structure. This needs to be updated to pass a slice of config.ChainConfig directly to the NewOrchestrator function.

  • File: orchestrator/orchestrator_test.go
  • Lines to update:
    • Replace OrchestratorConfig with a direct slice of config.ChainConfig in createTestSuite and TestTooManyL1sError.

Please ensure these updates are made to match the new function signature and maintain consistency across the codebase.

Analysis chain

Line range hint 33-82: Refactor NewOrchestrator to handle multiple chain configurations.

The refactoring of NewOrchestrator to accept a slice of config.ChainConfig instead of a single OrchestratorConfig is a significant change. This allows for more flexible configuration, especially in environments with multiple chains. The error handling when more than one L1 chain is configured is appropriate and prevents potential runtime issues.

However, ensure that all calls to this function across the codebase have been updated to pass the correct parameters.


The search results indicate that the NewOrchestrator function is being called with the new signature in supersim.go and orchestrator/orchestrator_test.go. The next step is to verify that the chainConfigs parameter is correctly passed as a slice of config.ChainConfig in these files.


The verification for supersim.go confirms that chainConfigs is correctly defined and passed. However, we need to verify the same for orchestrator/orchestrator_test.go.


To ensure that the chainConfigs parameter is correctly defined and passed in orchestrator/orchestrator_test.go, we need to search for the NewOrchestrator function calls and inspect the surrounding lines for the definition of chainConfigs.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `NewOrchestrator` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go -A 5 $'NewOrchestrator'

Length of output: 1260


Script:

#!/bin/bash
# Verify that the `chainConfigs` parameter is correctly passed as a slice of `config.ChainConfig` in `supersim.go` and `orchestrator/orchestrator_test.go`.

# Check `supersim.go` for the definition and passing of `chainConfigs`.
rg --type go -A 10 'chainConfigs' supersim.go

# Check `orchestrator/orchestrator_test.go` for the definition and passing of `chainConfigs`.
rg --type go -A 10 'chainConfigs' orchestrator/orchestrator_test.go

Length of output: 1162


Script:

#!/bin/bash
# Verify that the `chainConfigs` parameter is correctly passed as a slice of `config.ChainConfig` in `orchestrator/orchestrator_test.go`.

# Check `orchestrator/orchestrator_test.go` for the definition and passing of `chainConfigs`.
rg --type go -A 10 'chainConfigs' orchestrator/orchestrator_test.go

Length of output: 67


Script:

#!/bin/bash
# Verify the definition and usage of `chainConfigs` in `orchestrator/orchestrator_test.go` by inspecting the lines around `NewOrchestrator` function calls.

# Search for `NewOrchestrator` function calls in `orchestrator/orchestrator_test.go` and include surrounding lines for context.
rg --type go -A 10 -B 10 'NewOrchestrator' orchestrator/orchestrator_test.go

Length of output: 1007

anvil/anvil.go (1)

Line range hint 76-154: Refactor Start method to handle dynamic configurations.

The Start method has been significantly refactored to handle dynamic configurations, especially related to genesis and forking. The method now properly handles temporary files and logs, which enhances both security and maintainability.

However, the error handling could be improved by ensuring that temporary files are cleaned up in error scenarios to prevent potential resource leaks.

if _, err = tempFile.Write(a.cfg.Genesis); err != nil {
+   tempFile.Close()
+   os.Remove(tempFile.Name())
    return fmt.Errorf("error writing to genesis file: %w", err)
}
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 65b029d and c97e733.

Files selected for processing (7)
  • anvil/anvil.go (10 hunks)
  • config/chain.go (1 hunks)
  • opsimulator/deposits.go (2 hunks)
  • opsimulator/opsimulator.go (3 hunks)
  • orchestrator/fork.go (3 hunks)
  • orchestrator/orchestrator.go (7 hunks)
  • supersim.go (3 hunks)
Additional comments not posted (10)
opsimulator/deposits.go (1)

9-9: Verify compatibility of config.Chain with chainapi.Chain.

The import path and type of l1Chain have been changed from chainapi.Chain to config.Chain. It's crucial to ensure that the config.Chain type supports all functionalities of the chainapi.Chain, especially the SubscribeFilterLogs method used here.

Also applies to: 36-36

Verification successful

Verified compatibility of config.Chain with chainapi.Chain.

The config.Chain type includes the SubscribeFilterLogs method, ensuring compatibility with the existing usage in the SubscribeDepositTx function in deposits.go.

  • config/chain.go:76: SubscribeFilterLogs method in Chain interface.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Searching for the 'SubscribeFilterLogs' method implementation in the 'config.Chain' interface.
ast-grep --lang go --pattern $'type Chain interface { $$$ SubscribeFilterLogs($_, $_, $_) $_ $$$ }' src/config/chain.go

Length of output: 187


Script:

#!/bin/bash
# Searching for the 'Chain' interface definition in the entire repository.
ast-grep --lang go --pattern $'type Chain interface { $$$ }' 

# Searching for the 'SubscribeFilterLogs' method in the entire repository.
rg 'SubscribeFilterLogs'

Length of output: 1485

supersim.go (1)

9-9: Review refactoring of configuration handling in NewSupersim.

The NewSupersim function has been refactored to use cliConfig instead of a generic config. Ensure that all properties accessed from cliConfig are correctly initialized and used within the function. Additionally, verify that the new chainConfigs setup does not omit any necessary configuration details previously handled.

Also applies to: 21-50

config/chain.go (1)

1-105: Review the new config/chain.go file for correctness and integration.

This new file introduces several key configurations and interfaces related to chain management. Verify that all configurations are correctly defined and that the interfaces (Chain) are properly implemented in other parts of the system. Additionally, ensure that the default configurations do not expose sensitive information or lead to unintended behavior.

opsimulator/opsimulator.go (1)

16-16: Review structural changes in OpSimulator.

The OpSimulator struct has undergone significant changes, including the removal of the Config struct and the integration of its fields directly. Verify that all functionalities previously handled by Config are preserved and correctly implemented in the new structure. Additionally, ensure that the new methods and fields integrate seamlessly with the rest of the system.

Also applies to: 36-119

Verification successful

Let's adjust the script to locate the correct directory structure and re-run the verification.


The import statement for config is necessary and correctly implemented.

The OpSimulator struct relies on the config.Chain type for its l1Chain and l2Chain fields. The methods within OpSimulator utilize these fields, confirming the necessity of the import statement.

  • opsimulator/opsimulator.go:
    • OpSimulator struct definition
    • Methods utilizing l1Chain and l2Chain
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correct implementation of new methods and fields in `OpSimulator`.

# Test: Search for usage of new methods and fields in `OpSimulator`.
rg --type go -A 5 $'OpSimulator' src/

Length of output: 81


Script:

#!/bin/bash
# Locate the directory containing `opsimulator.go`
opsim_dir=$(fd opsimulator.go | xargs dirname)

# Verify the correct implementation of new methods and fields in `OpSimulator`.
if [ -n "$opsim_dir" ]; then
  rg --type go -A 5 $'OpSimulator' "$opsim_dir"
else
  echo "opsimulator.go not found"
fi

Length of output: 3222

orchestrator/fork.go (2)

50-60: Refactor error handling in loop for better clarity.

The error handling inside the loop could be refactored to reduce nesting and improve readability.

- l2ForkHeight, err := latestL2HeightFromL1Header(chainCfg, l1Header)
- if err != nil {
-     return nil, fmt.Errorf("failed to find right l2 height: %w", err)
- }
+ l2ForkHeight, err := latestL2HeightFromL1Header(chainCfg, l1Header)
+ if err != nil {
+     return nil, fmt.Errorf("failed to find right l2 height for chain %s: %w", chainCfg.Chain, err)
+ }

Likely invalid or redundant comment.


70-70: Visibility change of function needs review.

The function latestL2HeightFromL1Header was changed from public to private. Verify that this change does not affect other parts of the codebase where it might be used.

Verification successful

Visibility change of function is safe.

The function latestL2HeightFromL1Header is not used outside the fork.go file, so changing its visibility from public to private does not impact other parts of the codebase.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the function `latestL2HeightFromL1Header` is not used outside this file.

# Test: Search for the function usage outside this file. Expect: No occurrences.
rg --type python -g '!fork.go' 'latestL2HeightFromL1Header'

Length of output: 90


Script:

#!/bin/bash
# Description: Verify that the function `latestL2HeightFromL1Header` is not used outside this file.

# Test: Search for the function usage outside this file. Expect: No occurrences.
rg --type go -g '!fork.go' 'latestL2HeightFromL1Header'

Length of output: 55

orchestrator/orchestrator.go (2)

92-99: Enhanced error handling in Start method.

The enhanced error handling and logging in the Start method improve the observability and robustness of the orchestrator. The use of formatted error messages with wrapped errors (%w) is a best practice that aids in debugging.


Line range hint 107-127: Refined logging in Stop method.

The changes to the Stop method, including the addition of debug-level logging, align with the PR's objective to reduce verbosity at higher log levels. This is a good practice as it helps in reducing log clutter in production environments while still providing necessary information when debugging.

anvil/anvil.go (2)

Line range hint 215-262: Addition of Name and String methods to improve usability.

The addition of the Name method and the modification of the String method to include more details about the Anvil instance are useful for debugging and logging purposes. These changes enhance the usability of the Anvil class by making it easier to identify instances in logs and debug outputs.


Line range hint 16-36: Configuration management improvements in Config struct.

The integration of config.ChainConfig directly into the Config struct simplifies the configuration management for Anvil instances. This change, coupled with the removal of ForkConfig, indicates a shift towards a more unified configuration approach.

Ensure that all dependent code has been updated to work with these changes.

orchestrator/fork.go Outdated Show resolved Hide resolved
@ethereum-optimism ethereum-optimism deleted a comment from coderabbitai bot Jul 17, 2024
Copy link
Contributor

coderabbitai bot commented Jul 17, 2024

Walkthrough

Walkthrough

The update involves significant restructuring and refactoring across multiple files to streamline the configuration management of blockchain chains. The main changes include the consolidation of configuration structs, improved logging, and error handling, and the introduction of new methods and functions to better manage and interact with the blockchain chains. This refactor also replaces old import paths with a new config package, providing a more organized and modular configuration setup.

Changes

File Change Summary
anvil/anvil.go Replaced chainapi with config, removed ForkConfig, added ChainConfig embedding, updated logging, error handling, and initialization methods.
anvil/anvil_test.go Updated imports, modified cfg initialization to include ChainConfig.
config/chain.go Introduced new configuration file defining SecretsConfig, ChainConfig, ForkConfig, and a Chain interface.
opsimulator/deposits.go Changed import path and type for l1Chain parameter from chainapi.Chain to config.Chain.
opsimulator/deposits_test.go Updated imports, changed MockChain declaration to use config.Chain.
opsimulator/opsimulator.go Removed Config struct, integrated its fields into OpSimulator, updated initialization and logging methods, added Name method.
orchestrator/fork.go Renamed and refactored function, updated variables and struct fields, modified method names.
orchestrator/orchestrator.go Removed OrchestratorConfig, updated NewOrchestrator to accept chainConfigs slice, adjusted initialization of OpSimulators and Anvils.
orchestrator/orchestrator_test.go Updated TestSuite to use chainConfigs, modified createTestSuite and TestTooManyL1sError functions to use chainConfigs.
supersim.go Refactored NewSupersim function to use cliConfig, simplified chain configuration handling, updated logging, removed unused methods.
testutils/mockchain.go Replaced import path with config, added Name() method to MockChain.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
orchestrator/orchestrator_test.go (1)

22-25: Inconsistency in createTestSuite function usage

The createTestSuite function in supersim_test.go does not use the new chainConfigs initialization, while the one in orchestrator/orchestrator_test.go does. Please update the createTestSuite function in supersim_test.go to match the new chainConfigs initialization.

  • supersim_test.go: Update the createTestSuite function to use the new chainConfigs initialization.
Analysis chain

LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to createTestSuite match the new chainConfigs initialization.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `createTestSuite` match the new `chainConfigs` initialization.

# Test: Search for the function usage. Expect: Only occurances of the new `chainConfigs` initialization.
rg --type go -A 5 $'createTestSuite'

Length of output: 2452

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 65b029d and e7e5f0a.

Files selected for processing (11)
  • anvil/anvil.go (9 hunks)
  • anvil/anvil_test.go (1 hunks)
  • config/chain.go (1 hunks)
  • opsimulator/deposits.go (2 hunks)
  • opsimulator/deposits_test.go (2 hunks)
  • opsimulator/opsimulator.go (3 hunks)
  • orchestrator/fork.go (2 hunks)
  • orchestrator/orchestrator.go (7 hunks)
  • orchestrator/orchestrator_test.go (3 hunks)
  • supersim.go (3 hunks)
  • testutils/mockchain.go (2 hunks)
Files skipped from review as they are similar to previous changes (7)
  • anvil/anvil.go
  • config/chain.go
  • opsimulator/deposits.go
  • opsimulator/opsimulator.go
  • orchestrator/fork.go
  • orchestrator/orchestrator.go
  • supersim.go
Additional comments not posted (9)
anvil/anvil_test.go (2)

8-8: Import change approved.

The import change aligns with the refactoring goals mentioned in the PR summary.


18-18: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to TestAnvil match the new cfg initialization.

Verification successful

Let's verify if there are any other references to TestAnvil in the entire codebase to ensure comprehensive verification.


Verified: The changes to the cfg initialization in TestAnvil do not impact other parts of the codebase.

  • The TestAnvil function is only defined and used within anvil/anvil_test.go.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `TestAnvil` match the new `cfg` initialization.

# Test: Search for the function usage. Expect: Only occurances of the new `cfg` initialization.
rg --type go -A 5 $'TestAnvil'

Length of output: 372


Script:

#!/bin/bash
# Description: Verify if there are any other references to `TestAnvil` in the entire codebase.

# Test: Search for any references to `TestAnvil` in the entire codebase.
rg 'TestAnvil'

Length of output: 65

testutils/mockchain.go (2)

6-6: Import change approved.

The import change aligns with the refactoring goals mentioned in the PR summary.


29-31: Addition of Name method approved.

The addition of the Name method enhances the MockChain struct by providing a way to retrieve the name of the chain.

orchestrator/orchestrator_test.go (2)

7-7: Import change approved.

The import change aligns with the refactoring goals mentioned in the PR summary.


56-59: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to TestTooManyL1sError match the new chainConfigs initialization.

Verification successful

Verification successful!

The function TestTooManyL1sError is not called elsewhere in the codebase, so there are no additional instances to verify for the new chainConfigs initialization.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `TestTooManyL1sError` match the new `chainConfigs` initialization.

# Test: Search for the function usage. Expect: Only occurances of the new `chainConfigs` initialization.
rg --type go -A 5 $'TestTooManyL1sError'

Length of output: 421


Script:

#!/bin/bash
# Description: Verify all function calls to `TestTooManyL1sError` match the new `chainConfigs` initialization.

# Test: Search for any occurrences of `TestTooManyL1sError` being called in the codebase.
rg --type go 'TestTooManyL1sError'

Length of output: 109

opsimulator/deposits_test.go (3)

12-12: Import changes approved.

The import changes align with the refactoring goals mentioned in the PR summary.

Also applies to: 19-19


22-22: Declaration of _ config.Chain approved.

The declaration ensures that MockChainWithSubscriptions implements the config.Chain interface.


Line range hint 70-70:
Usage of MockChainWithSubscriptions approved.

The TestSubscribeDepositTx function correctly utilizes the updated MockChainWithSubscriptions.

Copy link
Contributor

@jakim929 jakim929 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

I do think the chainApi stuff should live in a separate package - since it's not exactly "config". But it will prob get clearer where we want to keep it as add more method interfaces so we can just change it then.

@hamdiallam
Copy link
Contributor Author

a separate package - since it's not exactly "config". But it will prob get clearer where we want to keep it as add more me

okay sounds good! yeah i think it's useful for now to reduce the number of packages but we can rip it out again as more methods get added

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e7e5f0a and 37e456e.

Files selected for processing (11)
  • anvil/anvil.go (9 hunks)
  • anvil/anvil_test.go (1 hunks)
  • config/chain.go (1 hunks)
  • opsimulator/deposits.go (2 hunks)
  • opsimulator/deposits_test.go (2 hunks)
  • opsimulator/opsimulator.go (4 hunks)
  • orchestrator/fork.go (2 hunks)
  • orchestrator/orchestrator.go (7 hunks)
  • orchestrator/orchestrator_test.go (3 hunks)
  • supersim.go (3 hunks)
  • testutils/mockchain.go (2 hunks)
Files skipped from review as they are similar to previous changes (11)
  • anvil/anvil.go
  • anvil/anvil_test.go
  • config/chain.go
  • opsimulator/deposits.go
  • opsimulator/deposits_test.go
  • opsimulator/opsimulator.go
  • orchestrator/fork.go
  • orchestrator/orchestrator.go
  • orchestrator/orchestrator_test.go
  • supersim.go
  • testutils/mockchain.go

@hamdiallam hamdiallam merged commit 6073ce7 into main Jul 18, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants