Skip to content

Conversation

@nischitpra
Copy link
Collaborator

@nischitpra nischitpra commented Sep 3, 2025

Summary by CodeRabbit

  • New Features

    • Published block messages now include metadata headers for chain ID and block number.
  • Tests

    • Updated unit tests to initialize processing configuration and pass execution context during cleanup scenarios.
  • Chores

    • No changes to public APIs; message topic, key, and value content remain unchanged.

@zeet-co
Copy link

zeet-co bot commented Sep 3, 2025

We're building your pull request over on Zeet.
Click me for more info about your build and deployment.
Once built, this branch can be tested at: https://polygon-indexer-q05q-np-block-headers.insight.zeet.app before merging 😉

@coderabbitai
Copy link

coderabbitai bot commented Sep 3, 2025

Walkthrough

Added Kafka headers (chain_id, block_number) to block publish records; updated committer tests and implementation to accept a context and mock GetBlocksPerRequest returning rpc.BlocksPerRequestConfig{Blocks:100}. No exported/public signatures were changed.

Changes

Cohort / File(s) Summary of Changes
Publisher headers
internal/publisher/publisher.go
Extended createBlockMessage to attach Kafka Headers with chain_id and block_number (stringified). Topic, Key, and Value remain unchanged; no API/signature changes.
Orchestrator committer (context + test mocks)
internal/orchestrator/committer_test.go, internal/orchestrator/...
Tests updated to pass a context.Context to cleanupProcessedStagingBlocks and to mock GetBlocksPerRequest returning rpc.BlocksPerRequestConfig{Blocks: 100}. Implied cleanupProcessedStagingBlocks now accepts a context.Context in tests.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Caller
    participant Publisher
    participant Kafka
    Caller->>Publisher: createBlockMessage(block)
    note right of Publisher: Build kgo.Record (Topic/Key/Value)
    rect rgba(220,245,235,0.6)
    note right of Publisher: New: add Headers\n`chain_id`, `block_number`
    end
    Publisher->>Kafka: Produce(kgo.Record with headers)
    Kafka-->>Publisher: Ack/Result
Loading
sequenceDiagram
    autonumber
    participant Test as Test Harness
    participant Committer as Committer
    participant RPC as mockRPC
    Test->>RPC: mock GetBlocksPerRequest() -> BlocksPerRequestConfig{Blocks:100}
    Test->>Committer: cleanupProcessedStagingBlocks(ctx)
    rect rgba(255,245,230,0.6)
    note right of Committer: Now receives context.Context\nand uses RPC config inside
    end
    Committer-->>Test: result/cleanup status
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch np/block_headers

🪧 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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit 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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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

@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

🧹 Nitpick comments (5)
internal/publisher/publisher.go (5)

274-283: Also set a content_type header for easier broker-side routing and debugging.

Many consumers key off a content_type header; adding it is low-cost and avoids payload peeks.

Apply this diff within the headers list:

 		Headers: []kgo.RecordHeader{
 			{
 				Key:   "chain_id",
 				Value: []byte(block.ChainId.String()),
 			},
 			{
 				Key:   "block_number",
 				Value: []byte(block.Number.String()),
 			},
+			{
+				Key:   "content_type",
+				Value: []byte("application/json"),
+			},
 		},

274-283: Deduplicate header keys via constants; reuse across producers.

Defining header keys once prevents drift and enables reuse in tx/event/trace records.

Outside the shown lines, add:

const (
	headerChainID     = "chain_id"
	headerBlockNumber = "block_number"
	headerContentType = "content_type"
)

Then replace string literals here with the constants.


274-283: Consider adding chain_id header to tx/event/trace messages for uniform filtering.

Even if block_number isn’t available everywhere, a consistent chain_id header across all topics simplifies consumer configs.

If you choose to do this, mirror the pattern:

// in createTransactionMessage(...)
 return &kgo.Record{
   Topic: p.getTopicName("transactions"),
   Key:   []byte(fmt.Sprintf("transaction-%s-%s-%s", status, tx.ChainId.String(), tx.Hash)),
   Value: msgJson,
+  Headers: []kgo.RecordHeader{
+    {Key: headerChainID, Value: []byte(tx.ChainId.String())},
+    {Key: headerContentType, Value: []byte("application/json")},
+  },
 }, nil
// in createEventMessage(...)
 return &kgo.Record{
   Topic: p.getTopicName("events"),
   Key:   []byte(fmt.Sprintf("event-%s-%s-%s-%d", status, event.ChainId.String(), event.TransactionHash, event.LogIndex)),
   Value: msgJson,
+  Headers: []kgo.RecordHeader{
+    {Key: headerChainID, Value: []byte(event.ChainId.String())},
+    {Key: headerContentType, Value: []byte("application/json")},
+  },
 }, nil
// in createTraceMessage(...)
 return &kgo.Record{
   Topic: p.getTopicName("traces"),
   Key:   []byte(fmt.Sprintf("trace-%s-%s-%s-%v", status, trace.ChainID.String(), trace.TransactionHash, strings.Join(traceAddressStr, ","))),
   Value: msgJson,
+  Headers: []kgo.RecordHeader{
+    {Key: headerChainID, Value: []byte(trace.ChainID.String())},
+    {Key: headerContentType, Value: []byte("application/json")},
+  },
 }, nil

274-283: Micro: avoid repeated String() conversions.

Hoist chainIDStr and blockNumStr once and reuse for Key and Headers.

Outside the shown lines:

chainIDStr := block.ChainId.String()
blockNumStr := block.Number.String()

return &kgo.Record{
  Topic: p.getTopicName("blocks"),
  Key:   []byte(fmt.Sprintf("block-%s-%s-%s", status, chainIDStr, block.Hash)),
  Value: msgJson,
  Headers: []kgo.RecordHeader{
    {Key: headerChainID, Value: []byte(chainIDStr)},
    {Key: headerBlockNumber, Value: []byte(blockNumStr)},
    {Key: headerContentType, Value: []byte("application/json")},
  },
}, nil

274-283: Add a lightweight unit test to assert headers presence/values.

Guard against regressions by asserting header keys and values for createBlockMessage.

I can add a test that builds a minimal common.Block and checks that the produced kgo.Record includes chain_id, block_number, and content_type with expected values—want me to open a follow-up PR?

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between e9f7efc and eb143fb.

📒 Files selected for processing (1)
  • internal/publisher/publisher.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Test
🔇 Additional comments (1)
internal/publisher/publisher.go (1)

274-283: Good addition: block-level Kafka headers are clear and additive-only.

Attaching chain_id and block_number as headers improves consumer-side routing/filters without breaking payload contracts.

Copy link

@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

🧹 Nitpick comments (4)
internal/orchestrator/committer_test.go (4)

25-27: Good: stub GetBlocksPerRequest for NewWorker initialization. Consider constraining call count.

To avoid accidental extra invocations (now or in future refactors), pin the expectation count (e.g., Times(1)/Once if available) so the test fails on unexpected retries.


71-73: Duplicate stubbing here is correct; same suggestion on call count.

Apply the same Times(1)/Once guard to keep the test strict and non-flaky.


81-82: Context-aware path covered; consider a canceled-context subtest.

Add a subtest that passes a canceled ctx and asserts no RPC/storage calls are made, to verify early-exit behavior.

I can draft the subtest if you’d like.


88-88: Nice: verifies cleanup when lastPublishedBlock > 0. Add explicit assertion on calls.

Optionally assert the exact invocation count on DeleteStagingDataOlderThan (e.g., AssertNumberOfCalls or typed expecter Times(1)) for stronger guarantees.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between eb143fb and 602f75c.

📒 Files selected for processing (1)
  • internal/orchestrator/committer_test.go (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
internal/orchestrator/committer_test.go (2)
internal/rpc/rpc.go (1)
  • BlocksPerRequestConfig (35-40)
internal/orchestrator/committer.go (1)
  • NewCommitter (41-78)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Test
🔇 Additional comments (2)
internal/orchestrator/committer_test.go (2)

4-4: Import of context is appropriate.

Needed for passing ctx into cleanupProcessedStagingBlocks.


10-10: Import of rpc package is correct.

Used for rpc.BlocksPerRequestConfig in mocks.

@nischitpra nischitpra merged commit 7325d08 into main Sep 3, 2025
5 of 6 checks passed
@nischitpra nischitpra deleted the np/block_headers branch September 3, 2025 13:28
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