Skip to content

fix(api): XDPoS_getRewardByAccount to be able to parse scientific not#2301

Open
wanwiset25 wants to merge 1 commit intodev-upgradefrom
fix-reward-scientific-notation
Open

fix(api): XDPoS_getRewardByAccount to be able to parse scientific not#2301
wanwiset25 wants to merge 1 commit intodev-upgradefrom
fix-reward-scientific-notation

Conversation

@wanwiset25
Copy link
Copy Markdown
Collaborator

Proposed changes

fix api XDPoS_getRewardByAccount to be able to parse scientific not

added unit tests

Types of changes

What types of changes does your code introduce to XDC network?
Put an in the boxes that apply

  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Changes that don't change source code or tests
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • revert: Revert something
  • style: Changes that do not affect the meaning of the code
  • test: Adding missing tests or correcting existing tests

Impacted Components

Which parts of the codebase does this PR touch?
Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Tested on a private network from the genesis block and monitored the chain operating correctly for multiple epochs.
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 13, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2a785cda-84f9-4930-8270-02e611ff97c6

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-reward-scientific-notation

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

Comment @coderabbitai help to get the list of available commands and usage tips.

…ion in json file

added unit tests

formatting
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes the XDPoS_getRewardByAccount reward parsing path to handle reward values encoded in JSON using scientific notation, and adds unit tests to validate the conversion behavior.

Changes:

  • Added jsonNumberToBigInt to convert json.Number values that may be in scientific notation into *big.Int.
  • Updated reward extraction logic to use the new conversion helper instead of big.Int.SetString.
  • Added unit tests covering integer, scientific-notation, and invalid-number inputs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
consensus/XDPoS/api.go Adds jsonNumberToBigInt and wires it into reward parsing for masternode/protector/observer reward fields.
consensus/XDPoS/api_test.go Adds unit tests for jsonNumberToBigInt and imports encoding/json.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +626 to +630
// Fall back to big.Float to handle scientific notation.
f, _, err := new(big.Float).SetPrec(256).Parse(s, 10)
if err != nil {
log.Warn("[jsonNumberToBigInt] Failed to parse json.Number:", "number", s, "err", err)
return nil, false
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

Using big.Float with a fixed 256-bit precision to parse JSON numbers can round large values; when this happens, converting to *big.Int may yield an incorrect integer. For reward amounts (which should be exact), consider an exact parsing strategy (e.g., mantissa+exponent to big.Int/big.Rat) instead of big.Float.

Copilot uses AI. Check for mistakes.
Comment on lines +635 to +636
// The value had a fractional part; truncate is the best we can do
log.Debug("[jsonNumberToBigInt] json.Number had fractional part, truncated to integer", "number", s, "truncated", i.String(), "accuracy", acc)
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

When acc != big.Exact, jsonNumberToBigInt currently logs and still returns (i, true). That means fractional inputs (or rounded big.Float parses) are silently truncated and treated as valid rewards. Consider returning ok=false (or error) when acc != big.Exact so callers don't accept corrupted/non-integer amounts.

Suggested change
// The value had a fractional part; truncate is the best we can do
log.Debug("[jsonNumberToBigInt] json.Number had fractional part, truncated to integer", "number", s, "truncated", i.String(), "accuracy", acc)
log.Warn("[jsonNumberToBigInt] json.Number is not an exact integer value", "number", s, "truncated", i.String(), "accuracy", acc)
return nil, false

Copilot uses AI. Check for mistakes.
Comment on lines +157 to +161
got, ok := jsonNumberToBigInt(tt.input)
if tt.wantOk {
assert.True(t, ok, "input %q: parse failed, expected %s", tt.input, tt.want)
assert.Equal(t, 0, tt.want.Cmp(got), "input %q: expected %s but got %s", tt.input, tt.want, got)
} else {
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

In the wantOk branch, assert.True(t, ok, ...) doesn't stop the test on failure, so the next line can call tt.want.Cmp(got) with got=nil and panic. Use require.True (or guard with if !ok { return }) before comparing.

Copilot uses AI. Check for mistakes.
Comment on lines +162 to +163
assert.False(t, ok, "input %q: expected parse failure but got %s", tt.input, got)
assert.Nil(t, got, "input %q: expected nil but got %s", tt.input, got)
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

The failure-branch assertion messages format got with %s (e.g., "... got %s") even though got can be nil; this can lead to confusing output or a nil formatting panic depending on the formatter path. Prefer %v (or print got.String() only after a nil check).

Suggested change
assert.False(t, ok, "input %q: expected parse failure but got %s", tt.input, got)
assert.Nil(t, got, "input %q: expected nil but got %s", tt.input, got)
assert.False(t, ok, "input %q: expected parse failure but got %v", tt.input, got)
assert.Nil(t, got, "input %q: expected nil but got %v", tt.input, got)

Copilot uses AI. Check for mistakes.
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.

4 participants