Skip to content

Conversation

NumberFour8
Copy link
Contributor

This PR adds a new table into the Logs DB, that collects smart contract topics assignments that were used to sync the database.

If the DB is empty, it populates it with the given array of smart contract topics. Once the DB is not empty, it checks each time that Indexer starts, that the SC topics in the DB are consistent with what the user attempts to sync.

Closes #6660

@NumberFour8 NumberFour8 added this to the 2.2.0-rc.1 milestone Jan 7, 2025
@NumberFour8 NumberFour8 requested a review from a team January 7, 2025 19:23
@NumberFour8 NumberFour8 self-assigned this Jan 7, 2025
Copy link
Contributor

coderabbitai bot commented Jan 7, 2025

📝 Walkthrough

Walkthrough

This pull request introduces enhancements to the log indexing and database management system. The changes focus on improving log origin validation and consistency checks across multiple components. A new database migration is added to create a LogTopicInfo table, and modifications are made to the indexer and database API to support more robust log tracking. The changes include adding a new error type for inconsistent logs, implementing a method to ensure log origins, and updating package versions accordingly.

Changes

File Change Summary
chain/indexer/src/block.rs Added address_topics variable and modified log processing logic to include consistency check via db.ensure_logs_origin()
db/api/Cargo.toml Updated package version from 0.5.3 to 0.6.0
db/api/src/errors.rs Added new InconsistentLogs error variant to DbError enum
db/api/src/logs.rs Added ensure_logs_origin async method to HoprDbLogOperations trait
db/migration/src/lib.rs Included new migration module for logs metadata table
db/migration/src/m20250107_000019_logs_meta_table.rs New migration file to create LogTopicInfo table with address and topic columns
db/sql/Cargo.toml Updated package version from 0.9.0 to 0.10.0
db/sql/src/logs.rs Implemented ensure_logs_origin method and added a test for log consistency

Assessment against linked issues

Objective Addressed Explanation
Network name in logs database filename [#6660] No changes related to filename modification were implemented.

Possibly related PRs

Suggested labels

crate:hopr-db-entity

Suggested reviewers

  • tolbrino

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f9f2222 and 184f9c3.

📒 Files selected for processing (1)
  • chain/indexer/src/block.rs (12 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • chain/indexer/src/block.rs
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: tests-smoke-websocket
  • GitHub Check: tests-unit-nightly
  • GitHub Check: tests-unit
  • GitHub Check: hoprd / docker
  • GitHub Check: hopli / docker
  • GitHub Check: Linter

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.

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.

@NumberFour8 NumberFour8 marked this pull request as ready for review January 7, 2025 19:39
@github-actions github-actions bot added the dependencies Pull requests that update a dependency file label Jan 7, 2025
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

🧹 Nitpick comments (3)
db/sql/src/logs.rs (3)

349-400: Optimize existence checks using exists instead of count.

Using count queries to check for the existence of records can be less efficient than using exists. Consider using exists to improve performance.

Consider applying the following changes:

-let log_count = Log::find()
-    .count(tx.as_ref())
-    .await
-    .map_err(|e| DbError::from(DbSqlError::from(e)))?;
+let log_exists = Log::find()
+    .exists(tx.as_ref())
+    .await
+    .map_err(|e| DbError::from(DbSqlError::from(e)))?;

-let log_topic_count = LogTopicInfo::find()
-    .count(tx.as_ref())
-    .await
-    .map_err(|e| DbError::from(DbSqlError::from(e)))?;
+let log_topic_exists = LogTopicInfo::find()
+    .exists(tx.as_ref())
+    .await
+    .map_err(|e| DbError::from(DbSqlError::from(e)))?;

391-392: Correct condition in existence check.

In the condition if log_topic_count != 1, it's more accurate to check if log_topic_count == 0 to detect missing address-topic pairs, as there should not be multiple identical entries.

Consider changing the condition:

-if log_topic_count != 1 {
+if log_topic_count == 0 {

865-885: Enhance test assertions to verify specific errors.

In the test function, verify that the errors returned are specifically DbError::InconsistentLogs to ensure the test checks for the correct error type.

Consider modifying the test:

-db.ensure_logs_origin(vec![(addr_1, topic_2)])
-    .await
-    .expect_err("expected error due to inconsistent logs in the db");
+let result = db.ensure_logs_origin(vec![(addr_1, topic_2)]).await;
+assert!(matches!(result, Err(DbError::InconsistentLogs)), "Expected InconsistentLogs error");
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 93b091c and f9f2222.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (8)
  • chain/indexer/src/block.rs (1 hunks)
  • db/api/Cargo.toml (1 hunks)
  • db/api/src/errors.rs (1 hunks)
  • db/api/src/logs.rs (1 hunks)
  • db/migration/src/lib.rs (2 hunks)
  • db/migration/src/m20250107_000019_logs_meta_table.rs (1 hunks)
  • db/sql/Cargo.toml (1 hunks)
  • db/sql/src/logs.rs (3 hunks)
✅ Files skipped from review due to trivial changes (2)
  • db/api/Cargo.toml
  • db/sql/Cargo.toml
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: hopli / docker
  • GitHub Check: hoprd / docker
  • GitHub Check: Docs / Rust docs
  • GitHub Check: tests-unit-nightly
  • GitHub Check: tests-unit
  • GitHub Check: tests-smart-contracts
  • GitHub Check: Linter
🔇 Additional comments (7)
db/sql/src/logs.rs (1)

16-17: LGTM!

The added imports for LogTopicInfo are appropriate.

db/api/src/errors.rs (1)

16-18: LGTM!

The new InconsistentLogs error variant is appropriately added to handle log inconsistency errors.

db/migration/src/m20250107_000019_logs_meta_table.rs (3)

8-39: LGTM! Well-designed schema for Ethereum log tracking.

The table schema is well-structured with appropriate column sizes for Ethereum addresses (40 chars) and topics (64 chars). The unique index on (Address, Topic) correctly enforces the business requirement of unique address-topic pairs.


41-54: LGTM! Proper cleanup order in down migration.

The down migration correctly drops the index before dropping the table, following database best practices.


57-65: LGTM! Clear and well-documented enum.

The LogTopicInfo enum provides type-safe identifiers with helpful documentation for each field.

db/api/src/logs.rs (1)

4-4: LGTM! Well-designed trait extension with clear documentation.

The new ensure_logs_origin method is well-documented and aligns perfectly with the PR objectives. The method signature correctly uses Vec<(Address, Hash)> for address-topic pairs.

Let's verify that all implementations of this trait are updated:

Also applies to: 10-21

✅ Verification successful

Implementation verified and properly tested

The trait is implemented in db/sql/src/logs.rs for HoprDb with proper error handling and transaction support. The implementation is also well-tested with both success and error cases.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Find all implementations of HoprDbLogOperations to ensure they're updated
ast-grep --pattern 'impl $_ for $_ {
  $$$
  async fn ensure_logs_origin($$$) {
    $$$
  }
  $$$
}'

Length of output: 102


Script:

#!/bin/bash
# First, find the trait definition to confirm its name
rg "trait\s+HoprDbLogOperations" -A 2

# Then find all implementations
rg "impl.*HoprDbLogOperations.*for"

Length of output: 418


Script:

#!/bin/bash
# Check the implementation file for the ensure_logs_origin method
rg "ensure_logs_origin" db/sql/src/logs.rs -B 1 -A 10

Length of output: 950

db/migration/src/lib.rs (1)

21-21: LGTM! Proper registration of the new migration.

The migration is correctly registered in MigratorChainLogs, following the documented separation of logs from the main database.

Also applies to: 135-135

@NumberFour8 NumberFour8 merged commit 1441f19 into master Jan 8, 2025
28 checks passed
@NumberFour8 NumberFour8 deleted the lukas/add-logs-topic-table branch January 8, 2025 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The hopr_logs.db should have the network name in the filename (e.g. hopr_logs_dufour.db)
2 participants