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

Fix type of nonce in account #121

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

marc-aurele-besner
Copy link
Collaborator

@marc-aurele-besner marc-aurele-besner commented Oct 11, 2024

User description

Fix type of nonce in account

Nonce was returning a BN, I parse it to bigint and made a parseBN function


PR Type

Bug fix, Enhancement


Description

  • Fixed the type of nonce in account by parsing it to bigint using a new parseBN function.
  • Updated the AccountData type to reflect the change from BN to bigint for nonce.
  • Enhanced the parseBalance function to utilize the new parseBN function for consistency.

Changes walkthrough 📝

Relevant files
Bug fix
account.ts
Fix nonce type by parsing with parseBN                                     

packages/auto-consensus/src/account.ts

  • Added parseBN function to parse nonce.
  • Changed the type of nonce to use parseBN.
  • +2/-2     
    Enhancement
    account.ts
    Update nonce type to bigint                                                           

    packages/auto-consensus/src/types/account.ts

    • Changed nonce type from BN to bigint.
    +1/-1     
    parse.ts
    Add parseBN function and update parseBalance                         

    packages/auto-consensus/src/utils/parse.ts

  • Introduced parseBN function to convert BN to bigint.
  • Updated parseBalance to use parseBN.
  • +7/-5     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Function Consistency
    The new parseBN function should include error handling similar to parseBalance to ensure consistency and robustness in error scenarios.

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Implement error handling in parseBN to manage conversion failures

    Add error handling for the BigInt conversion in parseBN to manage cases where
    value.toString() might not be convertible to a BigInt.

    packages/auto-consensus/src/utils/parse.ts [18]

    -export const parseBN = (value: BN): bigint => BigInt(value.toString())
    +export const parseBN = (value: BN): bigint => {
    +  try {
    +    return BigInt(value.toString());
    +  } catch (error) {
    +    console.error('Error converting BN to bigint:', error);
    +    throw new Error('Failed to convert BN to bigint');
    +  }
    +}
    Suggestion importance[1-10]: 8

    Why: Adding error handling to the parseBN function is a valuable enhancement, as it ensures that any conversion issues are caught and logged, preventing unexpected crashes and aiding in debugging.

    8
    Add null checks to prevent runtime errors when parsing values

    Ensure that the parseBN function can handle possible null or undefined values to
    prevent runtime errors.

    packages/auto-consensus/src/account.ts [14]

    -nonce: parseBN(nonce),
    +nonce: nonce ? parseBN(nonce) : null,
    Suggestion importance[1-10]: 7

    Why: Adding null checks can prevent potential runtime errors when nonce is null or undefined, which improves the robustness of the code. However, the improved code returns null instead of a default value, which might not be the intended behavior.

    7
    Enhancement
    Improve robustness of parseBalance by handling exceptions per property

    Refactor the parseBalance function to handle potential exceptions for each property
    individually, allowing partial success in parsing balance data.

    packages/auto-consensus/src/utils/parse.ts [23-26]

     return {
    -  free: parseBN(data.free),
    -  reserved: parseBN(data.reserved),
    -  frozen: parseBN(data.frozen),
    -  flags: parseBN(data.flags),
    +  free: tryParseBN(data.free),
    +  reserved: tryParseBN(data.reserved),
    +  frozen: tryParseBN(data.frozen),
    +  flags: tryParseBN(data.flags),
     }
    Suggestion importance[1-10]: 6

    Why: Handling exceptions for each property individually in parseBalance can improve robustness by allowing partial success, but it requires a new function tryParseBN which is not defined, making the suggestion incomplete.

    6
    Performance
    Optimize parseBN by reducing redundant toString() calls

    Consider caching the toString() result in parseBN to avoid multiple calls to
    toString() in the parseBalance function, enhancing performance.

    packages/auto-consensus/src/utils/parse.ts [18]

    -export const parseBN = (value: BN): bigint => BigInt(value.toString())
    +export const parseBN = (value: BN): bigint => {
    +  const stringValue = value.toString();
    +  return BigInt(stringValue);
    +}
    Suggestion importance[1-10]: 5

    Why: While caching the toString() result is a minor optimization, it can slightly improve performance by reducing redundant calls, especially if parseBN is called frequently.

    5

    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.

    auto-consensus account return a nonce in BN type instead BigInt
    1 participant