Skip to content

Conversation

@mkysel
Copy link
Collaborator

@mkysel mkysel commented Feb 24, 2025

If you were to try to run the XMTP API/SYNC without providing the payer key, the node will crash:

$ ./dev/run
2025-02-24T14:01:43.485-0500    INFO    replication     Version: v0.2.0-9-g1f8192f
2025-02-24T14:01:43.495-0500    INFO    replication     Successfully connected to DB    {"namespace": "xmtpd_c04b2306614f"}
2025-02-24T14:01:43.512-0500    FATAL   replication     initializing signer     {"error": "unable to parse private key: invalid length, need 256 bits"}
exit status 1

The blockchain publisher is only required for the payer. If we ever need it for the node, it will have to use the node key.

I will break apart the XMTPD image and the payer image in the next few days. But I need this to get the infra code moving.

Summary by CodeRabbit

  • Refactor

    • Streamlined and centralized the startup processes for replication services, reducing configuration overhead and enhancing operational resilience.
    • Improved error handling during service initialization for a smoother and more reliable user experience.
    • These refinements contribute to overall system stability and maintainability.
  • Tests

    • Updated test configurations to reflect the revised initialization flows by removing deprecated parameters for a cleaner testing process.

@mkysel mkysel requested a review from a team as a code owner February 24, 2025 19:05
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 24, 2025

Walkthrough

This pull request removes the external initialization of the blockchain signer and publisher from the replication entry point. The changes update the main.go to eliminate creating a signer and blockchain publisher and adjust corresponding function calls. In the server package, the blockchainPublisher parameter is removed from the function signatures, and its instantiation is moved inside the startAPIServer function where error handling is introduced. The related test logic is updated by removing mock blockchain publisher references and associated payer configuration.

Changes

Files Change Summary
cmd/replication/main.go Removed the instantiation of a blockchain signer using blockchain.NewPrivateKeySigner and the blockchain publisher via blockchain.NewBlockchainPublisher. Also eliminated the corresponding error handling and the passing of the publisher to the replication server.
pkg/server/server.go Removed the blockchainPublisher parameter from the NewReplicationServer and startAPIServer function signatures. Moved the initialization of the signer and blockchain publisher inside startAPIServer, adding error handling (with fatal logging on errors).
pkg/server/server_test.go Removed references to a mock blockchain publisher and eliminated payer configuration settings from the test server setup. Updated the NewTestServer function signature by removing the private key parameter.

Sequence Diagram(s)

sequenceDiagram
    participant Main
    participant ReplicationServer
    participant API
    participant PayerService
    participant Signer
    participant BlockchainClient
    participant BlockchainPublisher

    Main->>ReplicationServer: Initialize replication server (without blockchainPublisher)
    ReplicationServer->>API: Call startAPIServer(...)
    API->>PayerService: Initialize payer service
    API->>Signer: Create signer (using private key & chain ID)
    Signer-->>API: Return signer
    API->>BlockchainClient: Connect using RPC URL
    BlockchainClient-->>API: Return blockchain client
    API->>BlockchainPublisher: Initialize publisher using signer & client
    BlockchainPublisher-->>API: Return blockchain publisher
    API->>PayerService: Configure payer service with blockchain publisher
Loading

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 golangci-lint (1.62.2)

Error: can't load config: the Go language version (go1.23) used to build golangci-lint is lower than the targeted Go version (1.24)
Failed executing command with error: can't load config: the Go language version (go1.23) used to build golangci-lint is lower than the targeted Go version (1.24)

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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: 1

🔭 Outside diff range comments (1)
pkg/server/server_test.go (1)

132-133: ⚠️ Potential issue

Fix inconsistency in test function calls.

The NewTestServer function signature no longer accepts the privateKey parameter, but it's still being passed in the test cases.

Apply this diff to fix the test cases:

-	server1 := NewTestServer(t, server1Port, dbs[0], registry, privateKey1)
-	server2 := NewTestServer(t, server2Port, dbs[1], registry, privateKey2)
+	server1 := NewTestServer(t, server1Port, dbs[0], registry)
+	server2 := NewTestServer(t, server2Port, dbs[1], registry)

-	server1 := NewTestServer(t, server1Port, dbs[0], registry, privateKey1)
+	server1 := NewTestServer(t, server1Port, dbs[0], registry)

Also applies to: 246-246

🧹 Nitpick comments (1)
pkg/server/server_test.go (1)

43-49: Update function signature documentation.

The function signature has been updated to remove the privateKey parameter, but the documentation should be updated to reflect this change.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9ede122 and 34c8a57.

📒 Files selected for processing (3)
  • cmd/replication/main.go (0 hunks)
  • pkg/server/server.go (1 hunks)
  • pkg/server/server_test.go (1 hunks)
💤 Files with no reviewable changes (1)
  • cmd/replication/main.go
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build pre-baked anvil-xmtpd
  • GitHub Check: Test (Node)
🔇 Additional comments (1)
pkg/server/server.go (1)

219-247: LGTM! The changes align with the PR objective.

Moving the blockchain publisher initialization inside the payer service block makes the payer key optional, which addresses the issue where XMTP API/SYNC crashes if the payer key is not provided.

Comment on lines +224 to +246
signer, err := blockchain.NewPrivateKeySigner(
options.Payer.PrivateKey,
options.Contracts.ChainID,
)
if err != nil {
log.Fatal("initializing signer", zap.Error(err))
}

ethclient, err := blockchain.NewClient(ctx, options.Contracts.RpcUrl)
if err != nil {
log.Fatal("initializing blockchain client", zap.Error(err))
}

blockchainPublisher, err := blockchain.NewBlockchainPublisher(
ctx,
log,
ethclient,
signer,
options.Contracts,
)
if err != nil {
log.Fatal("initializing message publisher", zap.Error(err))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider using graceful error handling instead of log.Fatal.

The current implementation uses log.Fatal for error handling, which immediately terminates the program. Consider returning the errors to allow graceful shutdown and proper cleanup.

Apply this diff to improve error handling:

 			signer, err := blockchain.NewPrivateKeySigner(
 				options.Payer.PrivateKey,
 				options.Contracts.ChainID,
 			)
 			if err != nil {
-				log.Fatal("initializing signer", zap.Error(err))
+				log.Error("initializing signer", zap.Error(err))
+				return err
 			}

 			ethclient, err := blockchain.NewClient(ctx, options.Contracts.RpcUrl)
 			if err != nil {
-				log.Fatal("initializing blockchain client", zap.Error(err))
+				log.Error("initializing blockchain client", zap.Error(err))
+				return err
 			}

 			blockchainPublisher, err := blockchain.NewBlockchainPublisher(
 				ctx,
 				log,
 				ethclient,
 				signer,
 				options.Contracts,
 			)
 			if err != nil {
-				log.Fatal("initializing message publisher", zap.Error(err))
+				log.Error("initializing message publisher", zap.Error(err))
+				return err
 			}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
signer, err := blockchain.NewPrivateKeySigner(
options.Payer.PrivateKey,
options.Contracts.ChainID,
)
if err != nil {
log.Fatal("initializing signer", zap.Error(err))
}
ethclient, err := blockchain.NewClient(ctx, options.Contracts.RpcUrl)
if err != nil {
log.Fatal("initializing blockchain client", zap.Error(err))
}
blockchainPublisher, err := blockchain.NewBlockchainPublisher(
ctx,
log,
ethclient,
signer,
options.Contracts,
)
if err != nil {
log.Fatal("initializing message publisher", zap.Error(err))
}
signer, err := blockchain.NewPrivateKeySigner(
options.Payer.PrivateKey,
options.Contracts.ChainID,
)
if err != nil {
log.Error("initializing signer", zap.Error(err))
return err
}
ethclient, err := blockchain.NewClient(ctx, options.Contracts.RpcUrl)
if err != nil {
log.Error("initializing blockchain client", zap.Error(err))
return err
}
blockchainPublisher, err := blockchain.NewBlockchainPublisher(
ctx,
log,
ethclient,
signer,
options.Contracts,
)
if err != nil {
log.Error("initializing message publisher", zap.Error(err))
return err
}

@mkysel mkysel merged commit b5dfeff into main Feb 24, 2025
7 checks passed
@mkysel mkysel deleted the mkysel/allow-running-payer branch February 24, 2025 21:36
mkysel added a commit that referenced this pull request Mar 4, 2025
If you were to try to run the XMTP API/SYNC without providing the payer
key, the node will crash:
```
$ ./dev/run
2025-02-24T14:01:43.485-0500    INFO    replication     Version: v0.2.0-9-g1f8192f
2025-02-24T14:01:43.495-0500    INFO    replication     Successfully connected to DB    {"namespace": "xmtpd_c04b2306614f"}
2025-02-24T14:01:43.512-0500    FATAL   replication     initializing signer     {"error": "unable to parse private key: invalid length, need 256 bits"}
exit status 1
```

The blockchain publisher is only required for the payer. If we ever need
it for the node, it will have to use the node key.

I will break apart the XMTPD image and the payer image in the next few
days. But I need this to get the infra code moving.
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.

3 participants