Skip to content

fix: ensure only one from_block field is set in subscribeToTransactionsWithProofs #2537

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

Open
wants to merge 1 commit into
base: v2.0-dev
Choose a base branch
from

Conversation

thephez
Copy link
Collaborator

@thephez thephez commented Apr 10, 2025

Issue being fixed or feature implemented

This PR resolves a bug in dapi-client where providing fromBlockHash to subscribeToTransactionsWithProofs() would still result in a gRPC error due to unintended Protobuf serialization of fromBlockHeight = 0.

What was done?

  • Updated validation logic to only throw if fromBlockHeight was explicitly passed and is zero.
  • Ensured that fromBlockHeight is only used when fromBlockHash is not present.
  • No functional change to other fields or logic.

How Has This Been Tested?

CI

Breaking Changes

N/A

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

Summary by CodeRabbit

  • Bug Fixes
    • Improved the transaction subscription process by refining how optional parameters are handled. This update enhances reliability by preventing errors when certain identifiers are absent, ensuring that the subscription service functions more predictably.

Copy link
Contributor

coderabbitai bot commented Apr 10, 2025

Walkthrough

The update refines the subscribeToTransactionsWithProofs function within the client library. It adjusts the conditional logic for handling the options object by ensuring that the check for a zero value on fromBlockHeight only occurs when the option is explicitly provided. Additionally, the request construction now gives precedence to fromBlockHash—if present—before considering fromBlockHeight. These modifications enhance the robustness of the parameter handling and error management within the function without altering the exported API.

Changes

File(s) Changed Summary of Changes
packages/js-dapi-client/.../subscribeToTransactionsWithProofsFactory.js Updated the conditionals in subscribeToTransactionsWithProofs: now checks for fromBlockHeight only if explicitly provided and prioritizes fromBlockHash over fromBlockHeight.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Function as subscribeToTransactionsWithProofs
    participant RequestBuilder as Request

    Caller->>Function: Call subscribeToTransactionsWithProofs(options)
    Function->>Function: Check if options.fromBlockHash exists?
    alt fromBlockHash exists
        Function->>RequestBuilder: Set request.fromBlockHash
    else
        Function->>Function: Check if options.fromBlockHeight is explicitly provided
        alt fromBlockHeight provided
            Function->>RequestBuilder: Set request.fromBlockHeight
        end
    end
    Function->>Caller: Return TransactionsWithProofsRequest
Loading

Poem

I'm a bunny in a code-filled glen,
Hoping through conditions now and then.
If a hash is there, I choose it right,
Else height steps in with a delight.
Bugs flee when logic's sound and neat—
Hoppity happy, I make the code complete!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@thephez thephez marked this pull request as ready for review April 10, 2025 15:43
@thephez
Copy link
Collaborator Author

thephez commented Apr 10, 2025

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Apr 10, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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

🧹 Nitpick comments (1)
packages/js-dapi-client/lib/methods/core/subscribeToTransactionsWithProofsFactory.js (1)

86-91: Consider documenting the precedence behavior.

While the implementation correctly prioritizes fromBlockHash over fromBlockHeight, it might be helpful to document this behavior in the JSDoc for clarity.

 /**
  * @typedef {object} subscribeToTransactionsWithProofsOptions
  * @property {string} [fromBlockHash] - Specifies block hash to start syncing from
  * @property {number} [fromBlockHeight] - Specifies block height to start syncing from
+ * @note If both fromBlockHash and fromBlockHeight are provided, fromBlockHash takes precedence
  * @property {number} [count=0] - Number of blocks to sync,
  *                                if set to 0 syncing is continuously sends new data as well
  */
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 196976c and 318924e.

📒 Files selected for processing (1)
  • packages/js-dapi-client/lib/methods/core/subscribeToTransactionsWithProofsFactory.js (2 hunks)
🔇 Additional comments (2)
packages/js-dapi-client/lib/methods/core/subscribeToTransactionsWithProofsFactory.js (2)

42-42: Good fix: Improved validation logic for fromBlockHeight.

The condition now correctly checks if fromBlockHeight is explicitly included in the options before validating its value. This prevents throwing an error when the property is not defined, addressing part of the original bug where unintended serialization would occur.


62-70: Well implemented: Prioritizing fromBlockHash over fromBlockHeight.

This change correctly implements the priority handling logic by first checking for fromBlockHash and only considering fromBlockHeight when the hash is not provided. This ensures mutual exclusivity between these fields in the Protobuf message, preventing the gRPC error mentioned in the PR description.

The handling of the hash value is also robust, with proper checks for Buffer type or conversion from hex string.

@thephez
Copy link
Collaborator Author

thephez commented Apr 10, 2025

I see now that there is a specific test case that does this (block height 0, but with a block hash). It doesn't really make sense to me that you would pass both parameters in the same call, but maybe there is a reason? 🤔

EDIT: actually the fact that core.proto defines those params using a oneof would seem to indicate they should never be both passed. Not sure what the best fix is, but I'm pretty sure there's an issue here.

Based on some testing with gRPCurl, fromBlockHash is the one that actually determines the response if both values are provided. This matches what is described in protobuf oneOf docs:

Note that if multiple values are set, the last set value as determined by the order in the proto will overwrite all previous ones.

Comment on lines +42 to 44
if ('fromBlockHeight' in options && options.fromBlockHeight === 0) {
throw new DAPIClientError('Invalid argument: minimum value for `fromBlockHeight` is 1');
}
Copy link
Collaborator Author

@thephez thephez Apr 10, 2025

Choose a reason for hiding this comment

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

If we added something like this, it could prevent confusion and unexpected behavior. I guess that would be a breaking change though.

Suggested change
if ('fromBlockHeight' in options && options.fromBlockHeight === 0) {
throw new DAPIClientError('Invalid argument: minimum value for `fromBlockHeight` is 1');
}
if ('fromBlockHash' in options && 'fromBlockHeight' in options) {
throw new DAPIClientError('Only one of fromBlockHash or fromBlockHeight may be provided');
}
if ('fromBlockHeight' in options && options.fromBlockHeight === 0) {
throw new DAPIClientError('Invalid argument: minimum value for `fromBlockHeight` is 1');
}

Copy link
Member

Choose a reason for hiding this comment

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

0 is default value for integer in protobuf event if this field is omitted.

Copy link
Collaborator Author

@thephez thephez Apr 14, 2025

Choose a reason for hiding this comment

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

I found/fixed an issue in the code I was trying to use dapi-client with. However, dapi-client's handling of these options is still misleading. Someone should only provide either a height or a hash, but it's possible to provide both and it's not very obvious that the height is ignored in that case. Wouldn't it make sense to notify the dev they should only be passing one of the two so they don't waste time debugging expected behavior?

@thephez thephez changed the base branch from master to v2.0-dev April 10, 2025 16:17
@thephez thephez force-pushed the fix/subscribeToTransactionsWithProofs-oneof branch from 318924e to c8fe881 Compare April 10, 2025 16:22
Comment on lines +42 to 44
if ('fromBlockHeight' in options && options.fromBlockHeight === 0) {
throw new DAPIClientError('Invalid argument: minimum value for `fromBlockHeight` is 1');
}
Copy link
Member

Choose a reason for hiding this comment

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

0 is default value for integer in protobuf event if this field is omitted.

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