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

feat(evm): evm tx indexer service implemented #2044

Merged
merged 8 commits into from
Sep 23, 2024
Merged

Conversation

onikonychev
Copy link
Contributor

@onikonychev onikonychev commented Sep 20, 2024

NOTES

  • EVMTxIndexer is a service which indexes EVM transactions and blocks to allow queries like eth_getTransactionByHash, eth_getTransactionByBlockNumberAndIndex and similar on the full nodes / archive nodes.

  • The indexer gets block/tx info from the block store and state db.

  • EVMTxIndexer is enabled by the configuration parameter of the app.toml, see:

# EnableIndexer enables the custom transaction indexer for the EVM (ethereum transactions).
enable-indexer = true

EVM Indexer Background Service

  • If the indexer is enabled, then EVMTxIndexerService is starting in background along with the JSON RPC server. It listens for the new blocks, filters EVM txs from block results and passes to EVMTxIndexer for indexing.

EVM Indexer CMD for Filling Gaps

  • If the node had evm indexer disabled for a time being or was not gracefully stopped and there is a chance that evm indexer is not in sync, there is a CLI command nibid evm-tx-index is available.

  • The default run of the CMD is:

nibid evm-tx-index last-indexed latest

this will index all blocks that are not yet indexed.

Alternatively, you can index a supply a specific block range:

nibid evm-tx-index 20000 100500

The CMD checks for the first (base) latest block available on the node, so if the node is pruning, then the base block will be the first available block for evm indexing.

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a command-line interface for indexing Ethereum transactions within a specified block range.
    • Added an Ethereum transaction indexer service for enhanced transaction monitoring and indexing.
    • New configuration setting to enable the EVM indexer in the application.
  • Bug Fixes

    • Updated transaction index references in backend methods for improved clarity and functionality.
  • Tests

    • Renamed and updated test cases to reflect the new Ethereum transaction indexer.
  • Chores

    • Cleaned up changelog entries for better organization and clarity.

Copy link
Contributor

coderabbitai bot commented Sep 20, 2024

Walkthrough

The changes introduce a new Ethereum Virtual Machine (EVM) transaction indexer service, including a command-line interface for indexing transactions within specified block ranges. The codebase has been refactored to replace the previous key-value indexer with the new EVM indexer. Additionally, various components, such as the JSON-RPC server and backend, have been updated to integrate this new indexing functionality, improving overall structure and error handling.

Changes

Files Change Summary
CHANGELOG.md Removed duplicate entry for PR #2039; added entries for PR #2044 (EVM transaction indexer) and PR #2045 (backend tests).
app/server/evm_tx_indexer_cli.go Introduced CLI command evm-tx-index for indexing EVM transactions within a block range.
app/server/evm_tx_indexer_service.go Added EVMTxIndexerService for indexing Ethereum transactions, managing block events, and maintaining chain height.
app/server/json_rpc.go Renamed variables for clarity regarding Tendermint WebSocket client connections.
app/server/start.go Improved initialization of the EVM indexer and updated terminology in comments and logging.
app/server/util.go Updated error logging messages for Tendermint WebSocket client creation failures.
cmd/nibid/cmd/root.go Added NewEVMTxIndexCmd() to the root command for EVM transaction indexing.
contrib/scripts/localnet.sh Modified script to enable the EVM indexer in the configuration file.
e2e/evm/test/utils.ts Added logging of txResponse in the sendTestNibi function.
eth/indexer/evm_tx_indexer.go Refactored indexer from KVIndexer to EVMTxIndexer, updating method signatures and adding CloseDBAndExit.
eth/indexer/evm_tx_indexer_test.go Renamed test function from TestKVIndexer to TestEVMTxIndexer and updated indexer instantiation.
eth/rpc/backend/backend.go Renamed indexer field and parameter from indexer to evmTxIndexer in the Backend struct.
eth/rpc/backend/tx_info.go Updated transaction retrieval methods to reference evmTxIndexer.
eth/rpc/backend/tx_info_test.go Adjusted test cases to expect transaction index of 0 instead of 1.
x/common/testutil/testnetwork/start_node.go Modified initialization of the EVM indexer to use an in-memory database and improved error handling.
x/common/testutil/testnetwork/validator_node.go Added EthTxIndexerService field to the Validator struct and updated shutdown logic to include this service.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant EVMIndexerService
    participant RPCClient
    participant Database

    User->>CLI: Execute evm-tx-index command
    CLI->>EVMIndexerService: Start indexing process
    EVMIndexerService->>Database: Open indexer database
    EVMIndexerService->>RPCClient: Retrieve block data
    RPCClient-->>EVMIndexerService: Return block data
    EVMIndexerService->>Database: Index transactions
    EVMIndexerService->>CLI: Output completion message
Loading

🐰 In the garden where bunnies play,
New features hop in, brightening the day!
With EVM indexing, swift and neat,
Transactions dance, oh what a treat!
From CLI to RPC, all in sync,
Hooray for the changes, let's all think! 🌼


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

codecov bot commented Sep 22, 2024

Codecov Report

Attention: Patch coverage is 60.00000% with 20 lines in your changes missing coverage. Please review.

Project coverage is 65.58%. Comparing base (5214349) to head (f61e012).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
eth/indexer/evm_tx_indexer.go 50.00% 14 Missing and 2 partials ⚠️
x/common/testutil/testnetwork/validator_node.go 50.00% 1 Missing and 2 partials ⚠️
x/common/testutil/testnetwork/start_node.go 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2044      +/-   ##
==========================================
- Coverage   65.76%   65.58%   -0.18%     
==========================================
  Files         264      264              
  Lines       16636    16653      +17     
==========================================
- Hits        10940    10922      -18     
- Misses       4808     4846      +38     
+ Partials      888      885       -3     
Files with missing lines Coverage Δ
cmd/nibid/cmd/root.go 77.46% <100.00%> (+0.15%) ⬆️
eth/rpc/backend/backend.go 85.71% <100.00%> (ø)
eth/rpc/backend/tx_info.go 47.05% <100.00%> (-8.83%) ⬇️
x/common/testutil/testnetwork/start_node.go 76.08% <83.33%> (-0.26%) ⬇️
x/common/testutil/testnetwork/validator_node.go 59.13% <50.00%> (-0.64%) ⬇️
eth/indexer/evm_tx_indexer.go 65.11% <50.00%> (ø)

@onikonychev onikonychev marked this pull request as ready for review September 22, 2024 10:39
@onikonychev onikonychev requested a review from a team as a code owner September 22, 2024 10:39
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: 12

Outside diff range and nitpick comments (6)
eth/indexer/evm_tx_indexer.go (2)

Line range hint 50-127: LGTM with a suggestion to improve test coverage!

The IndexBlock method implementation follows the steps described in the comment and handles various scenarios appropriately. It uses appropriate error handling, logging, and a batch to write the indexed transaction results for better performance.

However, the static analysis hints indicate that some lines in the method are not covered by tests. Please consider adding test coverage for the following lines:

  • Line 72: Error handling when decoding the transaction fails.
  • Line 82: Error handling when parsing the transaction result fails.
  • Lines 109-114: Logging when the Ethereum transaction index doesn't match the expected value.

Do you want me to generate the test cases or open a GitHub issue to track this task?

Tools
GitHub Check: codecov/patch

[warning] 72-72: eth/indexer/evm_tx_indexer.go#L72
Added line #L72 was not covered by tests


211-219: LGTM with a suggestion to improve test coverage!

The CloseDBAndExit method is responsible for closing the indexer's database and logging the action when the indexer is stopped. It logs an informational message before closing the database, which is a good practice for tracking the indexer's lifecycle. The method also handles the error returned by the Close method of the database and returns it wrapped with an appropriate error message.

However, the static analysis hints indicate that some lines in the method are not covered by tests. Please consider adding test coverage for the following lines:

  • Lines 212-216: The entire CloseDBAndExit method.
  • Line 218: The return statement when no error occurs.

Do you want me to generate the test cases or open a GitHub issue to track this task?

Tools
GitHub Check: codecov/patch

[warning] 212-216: eth/indexer/evm_tx_indexer.go#L212-L216
Added lines #L212 - L216 were not covered by tests


[warning] 218-218: eth/indexer/evm_tx_indexer.go#L218
Added line #L218 was not covered by tests

x/common/testutil/testnetwork/validator_node.go (1)

178-178: Consider adding a test case for the error scenario.

The static analysis hint indicates that line 178, which logs an error message if the Stop method of the EthTxIndexerService returns an error, is not covered by tests. To improve the overall test coverage and ensure the correct handling of errors during shutdown, consider adding a test case that simulates an error scenario and verifies that the appropriate error message is logged.

Do you want me to generate a test case for this scenario or open a GitHub issue to track this task?

Tools
GitHub Check: codecov/patch

[warning] 178-178: x/common/testutil/testnetwork/validator_node.go#L178
Added line #L178 was not covered by tests

app/server/evm_tx_indexer_service.go (1)

50-51: Simplify initialization of chainHeightStorage

The use of atomic.StoreInt64 for initializing chainHeightStorage is unnecessary at this point since no other goroutines are accessing it yet.

Suggestion: Directly assign the initial value to chainHeightStorage.

 var chainHeightStorage int64
-atomic.StoreInt64(&chainHeightStorage, status.SyncInfo.LatestBlockHeight)
+chainHeightStorage = status.SyncInfo.LatestBlockHeight
app/server/evm_tx_indexer_cli.go (1)

76-78: Improve error message clarity when fromBlock exceeds maximum available height

When fromBlock is greater than the maximum available block height, the error message could be more informative to help the user understand the issue.

Apply this diff to enhance the error message:

                 if fromBlock > maxAvailableHeight {
-                    return fmt.Errorf("maximum available block is: %d", maxAvailableHeight)
+                    return fmt.Errorf("minBlockNumber (%d) cannot be greater than the maximum available block height (%d)", fromBlock, maxAvailableHeight)
                 }
app/server/start.go (1)

430-430: Nitpick: Clarify terminology in comment

The terminology in the comment can be improved for clarity and consistency.

Apply this diff to refine the comment:

-            // If grpc is enabled, configure grpc rpcClient for grpc gateway and json-rpc.
+            // If gRPC is enabled, configure gRPC client for gRPC gateway and JSON-RPC.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5214349 and f61e012.

Files selected for processing (16)
  • CHANGELOG.md (1 hunks)
  • app/server/evm_tx_indexer_cli.go (1 hunks)
  • app/server/evm_tx_indexer_service.go (1 hunks)
  • app/server/json_rpc.go (3 hunks)
  • app/server/start.go (7 hunks)
  • app/server/util.go (1 hunks)
  • cmd/nibid/cmd/root.go (1 hunks)
  • contrib/scripts/localnet.sh (1 hunks)
  • e2e/evm/test/utils.ts (1 hunks)
  • eth/indexer/evm_tx_indexer.go (7 hunks)
  • eth/indexer/evm_tx_indexer_test.go (2 hunks)
  • eth/rpc/backend/backend.go (3 hunks)
  • eth/rpc/backend/tx_info.go (4 hunks)
  • eth/rpc/backend/tx_info_test.go (3 hunks)
  • x/common/testutil/testnetwork/start_node.go (3 hunks)
  • x/common/testutil/testnetwork/validator_node.go (3 hunks)
Files skipped from review due to trivial changes (1)
  • app/server/util.go
Additional context used
Shellcheck
contrib/scripts/localnet.sh

[warning] 169-169: Quotes/backslashes in this variable will not be respected.

(SC2090)

GitHub Check: codecov/patch
eth/indexer/evm_tx_indexer.go

[warning] 72-72: eth/indexer/evm_tx_indexer.go#L72
Added line #L72 was not covered by tests


[warning] 82-82: eth/indexer/evm_tx_indexer.go#L82
Added line #L82 was not covered by tests


[warning] 109-114: eth/indexer/evm_tx_indexer.go#L109-L114
Added lines #L109 - L114 were not covered by tests


[warning] 212-216: eth/indexer/evm_tx_indexer.go#L212-L216
Added lines #L212 - L216 were not covered by tests


[warning] 218-218: eth/indexer/evm_tx_indexer.go#L218
Added line #L218 was not covered by tests

x/common/testutil/testnetwork/start_node.go

[warning] 139-139: x/common/testutil/testnetwork/start_node.go#L139
Added line #L139 was not covered by tests

x/common/testutil/testnetwork/validator_node.go

[warning] 178-178: x/common/testutil/testnetwork/validator_node.go#L178
Added line #L178 was not covered by tests

Additional comments not posted (31)
e2e/evm/test/utils.ts (1)

50-50: LGTM!

The addition of the console.log statement to log the txResponse object is a useful change for debugging purposes. It provides visibility into the transaction response without altering the existing logic or control flow of the function.

This change aligns with the PR objective of implementing an EVM transaction indexer service, as logging the transaction response can help with monitoring and indexing transactions.

eth/rpc/backend/backend.go (1)

29-29: LGTM!

The field renaming aligns with the naming convention and improves code readability. The underlying data type remains unchanged.

app/server/json_rpc.go (2)

24-32: LGTM!

The variable renaming from tmWsClient to tmWsClientForRPCApi improves clarity by specifying the purpose of the WebSocket client connection for the JSON-RPC API. The change is consistent with the AI-generated summary and does not introduce any issues.


112-113: LGTM!

The variable renaming from tmWsClient to tmWsClientForRPCWs improves clarity by specifying the purpose of the WebSocket client connection for the WebSocket server. The change is consistently applied in the NewWebsocketsServer function call and does not introduce any issues.

x/common/testutil/testnetwork/start_node.go (5)

9-9: LGTM!

The import statement is valid and follows the common practice of using an alias for the package name.


136-143: LGTM!

The changes to the EVM indexer initialization are valid and improve the test network setup:

  • Using an in-memory database is a good choice for the test environment.
  • The enhanced error handling provides clearer error messages.
  • Assigning the evmTxIndexerService to the validator ensures its accessibility.
Tools
GitHub Check: codecov/patch

[warning] 139-139: x/common/testutil/testnetwork/start_node.go#L139
Added line #L139 was not covered by tests


145-146: LGTM!

Passing the val.EthTxIndexer to the StartJSONRPC function is necessary to enable the JSON-RPC server to handle queries related to EVM transactions and blocks.


165-165: LGTM!

Passing the val.EthTxIndexer to the NewBackend function is necessary for the Ethereum RPC backend to handle queries and requests related to EVM transactions and blocks.


139-139: Skipping the static analysis hint.

The lack of test coverage for the error handling block at line 139 is not a critical issue in the context of a test network setup. Simulating an error condition during the indexer initialization to cover this line may not be a high priority.

Tools
GitHub Check: codecov/patch

[warning] 139-139: x/common/testutil/testnetwork/start_node.go#L139
Added line #L139 was not covered by tests

eth/rpc/backend/tx_info_test.go (3)

105-105: LGTM!

The change in the expected transaction index from 1 to 0 is a valid correction and aligns with the PR objective of introducing the EVMTxIndexer service.


146-146: LGTM!

The changes in the expected transaction index from 1 to 0 are valid corrections and align with the PR objective of introducing the EVMTxIndexer service.

Also applies to: 152-152


182-182: LGTM!

The change in the expected transaction index from 1 to 0 is a valid correction and aligns with the updates made to the test cases and the PR objective of introducing the EVMTxIndexer service.

eth/indexer/evm_tx_indexer_test.go (2)

26-26: LGTM!

The test function name change from TestKVIndexer to TestEVMTxIndexer accurately reflects the focus on testing the Ethereum transaction indexer. The change is appropriate and maintains the overall structure of the test.


164-164: LGTM!

The change in the indexer instantiation from indexer.NewKVIndexer to indexer.NewEVMTxIndexer aligns with the transition to an Ethereum transaction indexer. The rest of the test case logic remains intact, suggesting that the new indexer is expected to exhibit the same behavior as the previous one. The change is appropriate and maintains the integrity of the test.

cmd/nibid/cmd/root.go (1)

139-141: LGTM!

The addition of the server.NewEVMTxIndexCmd() command to the initRootCmd function is a valuable enhancement to the CLI tool. It provides users with the ability to manually trigger the indexing of EVM transactions, which is particularly useful in scenarios where the indexer was disabled for a period or if the node was not stopped gracefully, potentially leading to synchronization issues.

The command integrates well with the existing command structure and aligns with the purpose of the initRootCmd function. The comment above the command clearly indicates its purpose, making it easy for users to understand its functionality.

Great work on adding this command to improve the usability and robustness of the EVM transaction indexer!

contrib/scripts/localnet.sh (1)

168-170: LGTM!

The code segment correctly enables the EVM indexer in the config/app.toml file, which is consistent with the PR objective of implementing the EVM indexer service.

The static analysis warning about quotes/backslashes in the variable not being respected is a false positive because the sed command is not using any variables that contain quotes or backslashes.

Tools
Shellcheck

[warning] 169-169: Quotes/backslashes in this variable will not be respected.

(SC2090)

eth/indexer/evm_tx_indexer.go (6)

31-31: LGTM!

The type assertion ensures that the EVMTxIndexer type implements the eth.EVMTxIndexer interface, which is a good practice.


33-34: LGTM!

The EVMTxIndexer type definition is clear and concise, with fields for the database, logger, and client context, which are necessary for indexing transactions.


40-42: LGTM!

The NewEVMTxIndexer function correctly initializes and returns a new instance of the EVMTxIndexer type.


46-50: LGTM!

The comment provides a clear and concise overview of the indexing process performed by the IndexBlock method, which is helpful for understanding its purpose and functionality.


136-142: LGTM!

The LastIndexedBlock and FirstIndexedBlock methods are simple and clear wrappers around the LoadLastBlock and LoadFirstBlock functions, respectively, passing the indexer's database as an argument.


146-170: LGTM!

The GetByTxHash and GetByBlockAndIndex methods retrieve indexed Ethereum transaction results by transaction hash and block number + transaction index, respectively. They handle errors appropriately and return clear error messages when the requested transaction is not found. The GetByBlockAndIndex method promotes code reuse by calling the GetByTxHash method internally after retrieving the transaction hash.

x/common/testutil/testnetwork/validator_node.go (3)

15-16: LGTM!

The import statement for the appserver package is valid and correctly imports the package from the specified path.


90-94: LGTM!

The new fields added to the Validator struct are correctly defined with their respective types. These fields are likely used to integrate the EVM transaction indexer functionality into the validator node, as mentioned in the PR objectives.


167-182: LGTM!

The added code block correctly handles the shutdown of the EthTxIndexerService if it exists. Calling the Stop method ensures a graceful shutdown of the indexer service, and logging the success or failure message helps in monitoring the shutdown process. This aligns with the updated shutdown logic mentioned in the PR objectives.

Tools
GitHub Check: codecov/patch

[warning] 178-178: x/common/testutil/testnetwork/validator_node.go#L178
Added line #L178 was not covered by tests

eth/rpc/backend/tx_info.go (3)

Line range hint 305-317: LGTM! The changes optimize transaction retrieval performance.

The updated GetTxByEthHash function prioritizes using the evmTxIndexer to retrieve transactions by Ethereum hash, which is more efficient compared to querying the Tendermint indexer. Falling back to the Tendermint indexer query provides backward compatibility.

The error wrapping using errorsmod.Wrapf adds useful context to any errors that occur during the retrieval process.


Line range hint 323-339: LGTM! The changes optimize transaction retrieval performance.

The updated GetTxByTxIndex function prioritizes using the evmTxIndexer to retrieve transactions by block height and transaction index, which is more efficient compared to querying the Tendermint indexer. Falling back to the Tendermint indexer query provides backward compatibility.

The error wrapping using errorsmod.Wrapf adds useful context to any errors that occur during the retrieval process.


Line range hint 341-363: LGTM! The function provides a generic way to query the Tendermint indexer with validation checks.

The queryTendermintTxIndexer function offers a flexible way to query the Tendermint transaction indexer using a provided query and a callback function to extract the desired transaction from the results.

The function includes important validation checks using rpc.TxIsValidEnough to ensure the transaction is valid. It also handles cases where the transaction result code is not 0 by decoding the transaction, which is necessary when the transaction exceeds the block gas limit.

The usage of rpc.ParseTxIndexerResult ensures that the transaction indexer result is parsed into a structured format for further processing.

CHANGELOG.md (1)

123-125: LGTM!

The changes look good:

app/server/evm_tx_indexer_service.go (1)

70-73: Add type assertion check for message data

The code assumes that msg.Data is of type types.EventDataNewBlockHeader. If the type assertion fails, it could cause a panic.

Suggestion: Include a type assertion check to prevent potential panics.

 		case msg := <-blockHeadersChan:
-			eventDataHeader := msg.Data.(types.EventDataNewBlockHeader)
+			eventDataHeader, ok := msg.Data.(types.EventDataNewBlockHeader)
+			if !ok {
+				service.Logger.Error("Unexpected message data type", "data", msg.Data)
+				continue
+			}

Likely invalid or redundant comment.

app/server/start.go (1)

20-20: Approved: Added rpcclient import

The addition of rpcclient import is necessary for the integration with CometBFT's RPC client.

eth/rpc/backend/backend.go Show resolved Hide resolved
Comment on lines +111 to +120
block, err := service.rpcClient.Block(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block", "height", i, "err", err)
break
}
blockResult, err := service.rpcClient.BlockResults(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block result", "height", i, "err", err)
break
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Continue indexing after transient RPC errors

When an error occurs while fetching a block or block result, the current implementation breaks out of the loop. This could halt the indexing process indefinitely due to a transient error.

Suggestion: Modify the error handling to log the error and continue with the next block instead of breaking the loop.

 			block, err := service.rpcClient.Block(ctx, &i)
 			if err != nil {
 				service.Logger.Error("failed to fetch block", "height", i, "err", err)
-				break
+				continue
 			}
 
 			blockResult, err := service.rpcClient.BlockResults(ctx, &i)
 			if err != nil {
 				service.Logger.Error("failed to fetch block result", "height", i, "err", err)
-				break
+				continue
 			}
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
block, err := service.rpcClient.Block(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block", "height", i, "err", err)
break
}
blockResult, err := service.rpcClient.BlockResults(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block result", "height", i, "err", err)
break
}
block, err := service.rpcClient.Block(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block", "height", i, "err", err)
continue
}
blockResult, err := service.rpcClient.BlockResults(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block result", "height", i, "err", err)
continue
}

Comment on lines +65 to +89
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
service.Logger.Info("Stopping indexer goroutine")
err := service.evmTxIndexer.CloseDBAndExit()
if err != nil {
service.Logger.Error("Error closing indexer DB", "err", err)
}
return
case msg := <-blockHeadersChan:
eventDataHeader := msg.Data.(types.EventDataNewBlockHeader)
currentChainHeight := eventDataHeader.Header.Height
chainHeight := atomic.LoadInt64(&chainHeightStorage)
if currentChainHeight > chainHeight {
atomic.StoreInt64(&chainHeightStorage, currentChainHeight)
// notify
select {
case newBlockSignal <- struct{}{}:
default:
}
}
}
}
}(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure proper resource cleanup on service stop

The goroutine listening for new blocks calls service.evmTxIndexer.CloseDBAndExit() when the context is canceled. This could lead to race conditions or multiple calls to close the database if there are other goroutines or if OnStop also attempts to close the database.

Suggestion: Move the database closure to the OnStop method to ensure it is called only once during the service shutdown process.

Apply the following changes:

--- a/app/server/evm_tx_indexer_service.go
+++ b/app/server/evm_tx_indexer_service.go
@@ -69,10 +69,7 @@
 		case <-ctx.Done():
 			service.Logger.Info("Stopping indexer goroutine")
-			err := service.evmTxIndexer.CloseDBAndExit()
-			if err != nil {
-				service.Logger.Error("Error closing indexer DB", "err", err)
-			}
+			// Context canceled, exiting goroutine
 			return
 		case msg := <-blockHeadersChan:

Then, in the OnStop method:

 func (service *EVMTxIndexerService) OnStop() {
 	service.Logger.Info("Stopping EVMTxIndexerService")
 	if service.cancelFunc != nil {
 		service.Logger.Info("Calling EVMIndexerService CancelFunc")
 		service.cancelFunc()
 	}
+	err := service.evmTxIndexer.CloseDBAndExit()
+	if err != nil {
+		service.Logger.Error("Error closing indexer DB", "err", 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
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
service.Logger.Info("Stopping indexer goroutine")
err := service.evmTxIndexer.CloseDBAndExit()
if err != nil {
service.Logger.Error("Error closing indexer DB", "err", err)
}
return
case msg := <-blockHeadersChan:
eventDataHeader := msg.Data.(types.EventDataNewBlockHeader)
currentChainHeight := eventDataHeader.Header.Height
chainHeight := atomic.LoadInt64(&chainHeightStorage)
if currentChainHeight > chainHeight {
atomic.StoreInt64(&chainHeightStorage, currentChainHeight)
// notify
select {
case newBlockSignal <- struct{}{}:
default:
}
}
}
}
}(ctx)
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
service.Logger.Info("Stopping indexer goroutine")
// Context canceled, exiting goroutine
return
case msg := <-blockHeadersChan:
eventDataHeader := msg.Data.(types.EventDataNewBlockHeader)
currentChainHeight := eventDataHeader.Header.Height
chainHeight := atomic.LoadInt64(&chainHeightStorage)
if currentChainHeight > chainHeight {
atomic.StoreInt64(&chainHeightStorage, currentChainHeight)
// notify
select {
case newBlockSignal <- struct{}{}:
default:
}
}
}
}
}(ctx)

Comment on lines +100 to +127
for {
chainHeight := atomic.LoadInt64(&chainHeightStorage)
if chainHeight <= lastIndexedHeight {
// nothing to index. wait for signal of new block
select {
case <-newBlockSignal:
case <-time.After(NewBlockWaitTimeout):
}
continue
}
for i := lastIndexedHeight + 1; i <= chainHeight; i++ {
block, err := service.rpcClient.Block(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block", "height", i, "err", err)
break
}
blockResult, err := service.rpcClient.BlockResults(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block result", "height", i, "err", err)
break
}
if err := service.evmTxIndexer.IndexBlock(block.Block, blockResult.TxsResults); err != nil {
service.Logger.Error("failed to index block", "height", i, "err", err)
}
lastIndexedHeight = blockResult.Height
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Handle context cancellation in the indexer loop

The indexer loop does not currently check for context cancellation. If the service is stopping, the loop may continue running, preventing a graceful shutdown.

Suggestion: Add a context check at the beginning of the loop to exit when the context is canceled.

 for {
+	select {
+	case <-ctx.Done():
+		service.Logger.Info("Context canceled, stopping indexer loop")
+		return
+	default:
+	}
+
 	chainHeight := atomic.LoadInt64(&chainHeightStorage)
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
for {
chainHeight := atomic.LoadInt64(&chainHeightStorage)
if chainHeight <= lastIndexedHeight {
// nothing to index. wait for signal of new block
select {
case <-newBlockSignal:
case <-time.After(NewBlockWaitTimeout):
}
continue
}
for i := lastIndexedHeight + 1; i <= chainHeight; i++ {
block, err := service.rpcClient.Block(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block", "height", i, "err", err)
break
}
blockResult, err := service.rpcClient.BlockResults(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block result", "height", i, "err", err)
break
}
if err := service.evmTxIndexer.IndexBlock(block.Block, blockResult.TxsResults); err != nil {
service.Logger.Error("failed to index block", "height", i, "err", err)
}
lastIndexedHeight = blockResult.Height
}
}
}
for {
select {
case <-ctx.Done():
service.Logger.Info("Context canceled, stopping indexer loop")
return
default:
}
chainHeight := atomic.LoadInt64(&chainHeightStorage)
if chainHeight <= lastIndexedHeight {
// nothing to index. wait for signal of new block
select {
case <-newBlockSignal:
case <-time.After(NewBlockWaitTimeout):
}
continue
}
for i := lastIndexedHeight + 1; i <= chainHeight; i++ {
block, err := service.rpcClient.Block(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block", "height", i, "err", err)
break
}
blockResult, err := service.rpcClient.BlockResults(ctx, &i)
if err != nil {
service.Logger.Error("failed to fetch block result", "height", i, "err", err)
break
}
if err := service.evmTxIndexer.IndexBlock(block.Block, blockResult.TxsResults); err != nil {
service.Logger.Error("failed to index block", "height", i, "err", err)
}
lastIndexedHeight = blockResult.Height
}
}
}

Comment on lines +95 to +97
toBlock = maxAvailableHeight
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Add check for toBlock less than minAvailableHeight

Currently, there is no validation to ensure that toBlock is not less than the minimum available block height. This could cause the indexing process to malfunction if toBlock is set to a height lower than what's available.

Consider adding this check:

if toBlock < minAvailableHeight {
    return fmt.Errorf("maxBlockNumber (%d) cannot be less than the minimum available block height (%d)", toBlock, minAvailableHeight)
}

Comment on lines +124 to +127
err = evmTxIndexer.CloseDBAndExit()
if err != nil {
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Inconsistent error handling for CloseDBAndExit in app/server/evm_tx_indexer_cli.go.

  • Consider adding error logging or additional context when returning the error to ensure proper cleanup handling.
Analysis chain

Ensure proper cleanup when closing the indexer

The error returned by evmTxIndexer.CloseDBAndExit() is currently being returned without additional context. Confirm whether any additional cleanup is necessary or if error logging is sufficient to handle potential issues during the closing process.

Run the following script to search for other usages of CloseDBAndExit() and verify error handling practices:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all usages of CloseDBAndExit in the codebase to verify error handling.

# Search for CloseDBAndExit method calls and display surrounding context.
rg --type go -C 3 $'CloseDBAndExit\(\)'

Length of output: 1298

app/server/start.go Show resolved Hide resolved
@@ -437,7 +442,7 @@
}

clientCtx = clientCtx.WithGRPCClient(grpcClient)
ctx.Logger.Debug("gRPC client assigned to client context", "address", grpcAddress)
ctx.Logger.Debug("gRPC rpcClient assigned to rpcClient context", "address", grpcAddress)
Copy link
Contributor

Choose a reason for hiding this comment

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

Improve clarity of debug log message

The debug log message uses "rpcClient context," which might be unclear. Clarify the message for better understanding.

Apply this diff to enhance the log message:

-       ctx.Logger.Debug("gRPC rpcClient assigned to rpcClient context", "address", grpcAddress)
+       ctx.Logger.Debug("gRPC client assigned to client context", "address", grpcAddress)
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
ctx.Logger.Debug("gRPC rpcClient assigned to rpcClient context", "address", grpcAddress)
ctx.Logger.Debug("gRPC client assigned to client context", "address", grpcAddress)

app/server/start.go Show resolved Hide resolved
app/server/start.go Show resolved Hide resolved
@k-yang k-yang merged commit 81ea61d into main Sep 23, 2024
18 checks passed
@k-yang k-yang deleted the on/evm-indexer-service branch September 23, 2024 04:08
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