Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 276
feat: start mockda server only if it's not running already#1882
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1e3e9c8
Start mockda server only if it's not running already
yarikbratashchuk 6a2fa2b
Fix lint errors
yarikbratashchuk 8e11e23
Update error type'
yarikbratashchuk aa53d55
Update cmd/rollkit/commands/run_node.go
yarikbratashchuk e822e37
Fixes
yarikbratashchuk 2b58700
Update test
yarikbratashchuk 3e3cf1a
Fix lint
yarikbratashchuk ffc76c9
Merge branch 'main' into yarik/check-mockda-port
yarikbratashchuk 7b6f7d3
Update cmd/rollkit/commands/run_node.go
yarikbratashchuk 9fb3956
Update cmd/rollkit/commands/run_node.go
yarikbratashchuk c08e36c
Update cmd/rollkit/commands/run_node.go
yarikbratashchuk 80603b7
Update cmd/rollkit/commands/run_node_test.go
yarikbratashchuk 1fbcb2c
Merge branch 'main' into yarik/check-mockda-port
Manav-Aggarwal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,11 +2,13 @@ package commands | ||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "math/rand" | ||
| "net" | ||
| "net/url" | ||
| "os" | ||
| "syscall" | ||
| "time" | ||
| cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" | ||
| @@ -24,6 +26,7 @@ import ( | ||
| "github.com/mitchellh/mapstructure" | ||
| "google.golang.org/grpc" | ||
| "github.com/rollkit/go-da" | ||
| proxy "github.com/rollkit/go-da/proxy/jsonrpc" | ||
| goDATest "github.com/rollkit/go-da/test" | ||
| seqGRPC "github.com/rollkit/go-sequencing/proxy/grpc" | ||
| @@ -47,6 +50,8 @@ var ( | ||
| // initialize the logger with the cometBFT defaults | ||
| logger = cometlog.NewTMLogger(cometlog.NewSyncWriter(os.Stdout)) | ||
| errDAServerAlreadyRunning = errors.New("DA server already running") | ||
| ) | ||
| // MockSequencerAddress is a sample address used by the mock sequencer | ||
| @@ -127,12 +132,16 @@ func NewRunNodeCmd() *cobra.Command { | ||
| // use mock jsonrpc da server by default | ||
| if !cmd.Flags().Lookup("rollkit.da_address").Changed { | ||
| srv, err := startMockDAServJSONRPC(cmd.Context()) | ||
| if err != nil { | ||
| srv, err := startMockDAServJSONRPC(cmd.Context(), nodeConfig.DAAddress, proxy.NewServer) | ||
| if err != nil && !errors.Is(err, errDAServerAlreadyRunning) { | ||
| return fmt.Errorf("failed to launch mock da server: %w", err) | ||
| } | ||
| // nolint:errcheck,gosec | ||
| defer func() { srv.Stop(cmd.Context()) }() | ||
| defer func() { | ||
| if srv != nil { | ||
| srv.Stop(cmd.Context()) | ||
| } | ||
yarikbratashchuk marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }() | ||
| } | ||
| // use mock grpc sequencer server by default | ||
| @@ -236,13 +245,27 @@ func addNodeFlags(cmd *cobra.Command) { | ||
| } | ||
| // startMockDAServJSONRPC starts a mock JSONRPC server | ||
| func startMockDAServJSONRPC(ctx context.Context) (*proxy.Server, error) { | ||
| addr, _ := url.Parse(nodeConfig.DAAddress) | ||
| srv := proxy.NewServer(addr.Hostname(), addr.Port(), goDATest.NewDummyDA()) | ||
| err := srv.Start(ctx) | ||
| func startMockDAServJSONRPC( | ||
| ctx context.Context, | ||
| daAddress string, | ||
| newServer func(string, string, da.DA) *proxy.Server, | ||
| ) (*proxy.Server, error) { | ||
| addr, err := url.Parse(daAddress) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| srv := newServer(addr.Hostname(), addr.Port(), goDATest.NewDummyDA()) | ||
| if err := srv.Start(ctx); err != nil { | ||
| if errors.Is(err, syscall.EADDRINUSE) { | ||
| logger.Info("DA server is already running", "address", nodeConfig.DAAddress) | ||
| return nil, errDAServerAlreadyRunning | ||
| } | ||
| return nil, err | ||
| } | ||
| logger.Info("Starting mock DA server", "address", nodeConfig.DAAddress) | ||
yarikbratashchuk marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return srv, nil | ||
| } | ||
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 |
|---|---|---|
| @@ -1,9 +1,18 @@ | ||
| package commands | ||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net/url" | ||
| "reflect" | ||
| "syscall" | ||
| "testing" | ||
| "time" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/rollkit/go-da" | ||
| proxy "github.com/rollkit/go-da/proxy/jsonrpc" | ||
| ) | ||
| func TestParseFlags(t *testing.T) { | ||
| @@ -133,3 +142,83 @@ func TestAggregatorFlagInvariants(t *testing.T) { | ||
| } | ||
| } | ||
| } | ||
| // MockServer wraps proxy.Server to allow us to control its behavior in tests | ||
| type MockServer struct { | ||
| *proxy.Server | ||
| StartFunc func(context.Context) error | ||
| StopFunc func(context.Context) error | ||
| } | ||
| func (m *MockServer) Start(ctx context.Context) error { | ||
| if m.StartFunc != nil { | ||
| return m.StartFunc(ctx) | ||
| } | ||
| return m.Server.Start(ctx) | ||
| } | ||
| func (m *MockServer) Stop(ctx context.Context) error { | ||
| if m.StopFunc != nil { | ||
| return m.StopFunc(ctx) | ||
| } | ||
| return m.Server.Stop(ctx) | ||
| } | ||
| func TestStartMockDAServJSONRPC(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| daAddress string | ||
| mockServerErr error | ||
| expectedErr error | ||
| }{ | ||
| { | ||
| name: "Success", | ||
| daAddress: "http://localhost:26657", | ||
| mockServerErr: nil, | ||
| expectedErr: nil, | ||
| }, | ||
| { | ||
| name: "Invalid URL", | ||
| daAddress: "://invalid", | ||
| mockServerErr: nil, | ||
| expectedErr: &url.Error{}, | ||
| }, | ||
| { | ||
| name: "Server Already Running", | ||
| daAddress: "http://localhost:26657", | ||
| mockServerErr: syscall.EADDRINUSE, | ||
| expectedErr: errDAServerAlreadyRunning, | ||
| }, | ||
| { | ||
| name: "Other Server Error", | ||
| daAddress: "http://localhost:26657", | ||
| mockServerErr: errors.New("other error"), | ||
| expectedErr: errors.New("other error"), | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| newServerFunc := func(hostname, port string, da da.DA) *proxy.Server { | ||
| mockServer := &MockServer{ | ||
| Server: proxy.NewServer(hostname, port, da), | ||
| StartFunc: func(ctx context.Context) error { | ||
| return tt.mockServerErr | ||
| }, | ||
| } | ||
| return mockServer.Server | ||
| } | ||
| srv, err := startMockDAServJSONRPC(context.Background(), tt.daAddress, newServerFunc) | ||
| if tt.expectedErr != nil { | ||
| assert.Error(t, err) | ||
| assert.IsType(t, tt.expectedErr, err) | ||
| assert.Nil(t, srv) | ||
yarikbratashchuk marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } else { | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, srv) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.