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(bens): Added TLD-based fallback for name search in networks #1175

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Ilyak777
Copy link

@Ilyak777 Ilyak777 commented Jan 6, 2025

Closes #917

This PR enhances the names_options_in_network function by introducing a fallback mechanism for names without TLD. If the input name does not include a TLD, the function appends possible TLDs associated with the network's protocols, generating up to a defined maximum number of results (MAX_NAMES_LIMIT).

Summary by CodeRabbit

  • New Features

    • Enhanced domain name search functionality with support for multiple top-level domains (TLDs).
    • Added a limit of 5 names per search to improve performance and user experience.
  • Bug Fixes

    • Improved name resolution logic to handle various domain name formats more accurately.

@Ilyak777 Ilyak777 added the enhancement New feature or request label Jan 6, 2025
@Ilyak777 Ilyak777 requested a review from sevenzing January 6, 2025 13:23
@Ilyak777 Ilyak777 self-assigned this Jan 6, 2025
Copy link

coderabbitai bot commented Jan 6, 2025

Warning

Rate limit exceeded

@Ilyak777 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 38 minutes and 12 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between a113537 and a2b3fed.

📒 Files selected for processing (1)
  • blockscout-ens/bens-logic/src/protocols/protocoler.rs (2 hunks)

Walkthrough

The changes modify the names_options_in_network method in the Protocoler struct to enhance domain name lookup functionality. A new constant MAX_NAMES_LIMIT is introduced to limit the number of name variations returned. The method now accommodates domain names with and without top-level domains (TLDs). For names lacking a TLD, it constructs names by appending various TLDs, up to the specified maximum limit. Additionally, a new helper method find_names_with_tld is created to encapsulate the logic for retrieving names associated with specific TLDs, thereby improving the flexibility and effectiveness of domain name resolution.

Assessment against linked issues

Objective Addressed Explanation
Improve lookup for names without TLD [#917]
Append TLDs to incomplete domain names [#917]

Poem

🐰 A Rabbit's Ode to Domain Discovery 🔍
In the realm of names, where dots dance and play,
We hop through TLDs in a magical way.
From bare names to .eth, we expand with glee,
Five chances to find what was hidden to see.
Lookup magic, unleashed by a code so bright! 🌟


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

@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 (3)
blockscout-ens/bens-logic/src/protocols/protocoler.rs (3)

69-69: Consider making the limit configurable or documenting its rationale.

Currently, MAX_NAMES_LIMIT is a hardcoded magic number. Labeling it as a constant is good, but consider either making it configurable (e.g., via environment variables or config files) or at least documenting why 5 is an appropriate limit for your use case.


207-241: Consider deduplicating or short-circuiting logic for performance.

When appending TLDs in a loop and collecting names, consider:

  1. Maintaining a set to avoid duplicates if multiple protocols yield the same name.
  2. Short-circuiting earlier once the limit is reached, both outside and inside find_names_with_tld, to reduce unnecessary calls.

This can improve performance and clarity.


243-258: Reassess error handling in filter_map to avoid silently discarding errors.

Using .filter_map(|p| DomainNameOnProtocol::new(name_with_tld, p).ok()) silently drops errors. If legitimate errors occur, they won't be surfaced, making debugging more difficult. Consider handling or logging errors before discarding them.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 82dc056 and f38fd92.

📒 Files selected for processing (1)
  • blockscout-ens/bens-logic/src/protocols/protocoler.rs (2 hunks)

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

🧹 Nitpick comments (4)
blockscout-ens/bens-logic/src/protocols/protocoler.rs (4)

293-305: Consider using HashSet for TLD collection.

The current implementation might collect duplicate TLDs. Using a HashSet would be more efficient:

-            .collect::<Vec<Tld>>();
+            .collect::<std::collections::HashSet<Tld>>();

322-324: Enhance error message for invalid names.

The current error message doesn't provide enough context about why the name is invalid.

-            return Err(ProtocolError::InvalidName(name.to_string()));
+            return Err(ProtocolError::InvalidName {
+                name: name.to_string(),
+                reason: "no valid TLD combinations found".to_string(),
+            });

329-344: Add documentation for the helper method.

This public method would benefit from documentation explaining its purpose, parameters, and return value.

+    /// Attempts to find domain names with the specified TLD in the given network.
+    /// 
+    /// # Arguments
+    /// * `name_with_tld` - The domain name including TLD
+    /// * `network_id` - The network to search in
+    /// * `maybe_filter` - Optional protocol filter
+    /// 
+    /// # Returns
+    /// A vector of valid domain names on their respective protocols
     fn find_names_with_tld(

293-344: Well-structured implementation of TLD-based fallback.

The implementation successfully achieves the PR objective of enhancing name resolution with TLD fallback. The separation of concerns between names_options_in_network and find_names_with_tld is clean and maintainable.

A few architectural considerations:

  1. The solution gracefully handles both TLD and non-TLD cases
  2. The result limiting ensures reasonable response sizes
  3. The error handling provides a good foundation for debugging

Consider adding metrics/logging to track:

  • Number of TLD fallbacks attempted
  • Success rate of TLD combinations
  • Performance impact of TLD collection
🧰 Tools
🪛 GitHub Actions: Test, lint and docker (bens)

[error] 303-341: Code formatting issues detected by cargo fmt. Multiple formatting inconsistencies found including incorrect spacing and line breaks. Run 'cargo fmt' to fix these issues.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f38fd92 and a113537.

📒 Files selected for processing (1)
  • blockscout-ens/bens-logic/src/protocols/protocoler.rs (2 hunks)
🧰 Additional context used
🪛 GitHub Actions: Test, lint and docker (bens)
blockscout-ens/bens-logic/src/protocols/protocoler.rs

[error] 303-341: Code formatting issues detected by cargo fmt. Multiple formatting inconsistencies found including incorrect spacing and line breaks. Run 'cargo fmt' to fix these issues.

🔇 Additional comments (1)
blockscout-ens/bens-logic/src/protocols/protocoler.rs (1)

148-148: LGTM! Good use of a constant for limiting results.

The constant helps prevent excessive name variations while providing a reasonable limit for user queries.

Comment on lines 293 to 327
let tlds = self
.networks
.get(&network_id)
.ok_or_else(|| ProtocolError::NetworkNotFound(network_id))?
.use_protocols
.iter()
.filter_map(|protocol_name| {
self.protocols
.get(protocol_name)
.map(|protocol| protocol.info.tld_list.iter().cloned())
})
.flatten()
.collect::<Vec<Tld>>();

if name.contains('.') {
return self.find_names_with_tld(name, network_id, maybe_filter);
}

let mut all_names_with_protocols = Vec::new();
for tld in tlds {
if all_names_with_protocols.len() >= MAX_NAMES_LIMIT {
break;
}
let name_with_tld = format!("{}.{}", name, tld.0);
if let Ok(mut names) = self.find_names_with_tld(&name_with_tld, network_id, maybe_filter.clone()) {
all_names_with_protocols.append(&mut names);
}
}

if all_names_with_protocols.is_empty() {
return Err(ProtocolError::InvalidName(name.to_string()));
}

Ok(all_names_with_protocols.into_iter().take(MAX_NAMES_LIMIT).collect())
}
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Formatting issues confirmed in protocoler.rs

  • Line 317 exceeds Rust's standard 100-character line length limit
  • Multiple consecutive empty lines found between function definitions

Run cargo fmt to automatically fix these issues.

🔗 Analysis chain

Fix code formatting issues.

The pipeline has detected formatting inconsistencies. Please run cargo fmt to fix these issues.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify formatting issues
cargo fmt --all -- --check

Length of output: 124


Script:

#!/bin/bash
# Check for long lines (Rust's default is 100 characters)
rg -l "^.{101,}$" blockscout-ens/bens-logic/src/protocols/protocoler.rs

# Check for multiple consecutive empty lines
rg -l "\n\n\n+" blockscout-ens/bens-logic/src/protocols/protocoler.rs

# Verify code structure
ast-grep --pattern 'fn $_ ($$$) -> Result<$_, $_> {
    $$$
}'  blockscout-ens/bens-logic/src/protocols/protocoler.rs

Length of output: 2026


Script:

#!/bin/bash
# Show the actual long lines with line numbers
rg -n "^.{101,}$" blockscout-ens/bens-logic/src/protocols/protocoler.rs

# Check for multiple empty lines using a different approach
rg -U "(\n\s*){3,}" blockscout-ens/bens-logic/src/protocols/protocoler.rs

Length of output: 310

Comment on lines +307 to +309
if name.contains('.') {
return self.find_names_with_tld(name, network_id, maybe_filter);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

try to avoid if {return} statements because it makes the code harder to read. consider refactoring to reduce nesting and improve flow

Comment on lines +324 to +326
if all_names_with_protocols.is_empty() {
return Err(ProtocolError::InvalidName(name.to_string()));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

also try to rearrange code in such way that you don't need this if-return statement

Copy link
Contributor

@sevenzing sevenzing left a comment

Choose a reason for hiding this comment

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

also add tests for Tld struct :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BENS: better lookup
2 participants