Skip to content

refactor: rewrite shannonToCKBFormatter with formatUnit - #3129

Merged
Keith-CY merged 3 commits into
nervosnetwork:developfrom
twhy:refactor-shannonToCKBFormatter-with-formatUnit
Apr 19, 2024
Merged

refactor: rewrite shannonToCKBFormatter with formatUnit#3129
Keith-CY merged 3 commits into
nervosnetwork:developfrom
twhy:refactor-shannonToCKBFormatter-with-formatUnit

Conversation

@twhy

@twhy twhy commented Apr 17, 2024

Copy link
Copy Markdown
Contributor
  1. The third param delimiter: string = ',' has been replaced by showCommaSeparator = true to make things simpler.
  2. showPositiveSign = true takes effect ONLY when shannon value is > 0 . @Keith-CY

@homura

homura commented Apr 17, 2024

Copy link
Copy Markdown
Contributor

Only one line was changed compared to the last PR at #3128

- signDisplay: showPositiveSign ? 'always' : 'auto',
+ signDisplay: showPositiveSign && +shannon > 0 ? 'always' : 'auto',

Since it's a small change, you don't need to close and reopen a new one. You can commit the change after commenting on it. GitHub will mark the previous comment as outdated if you modify the code

@twhy

twhy commented Apr 17, 2024

Copy link
Copy Markdown
Contributor Author

The unit tests pass on Node.js 20 but not Node.js 18. I will fix this.

The cause is that Intl.NumberFormat not working as expected on Node.js 18.12.0

✅ on Node.js 20.11.1 (V8 11.3)

> process.versions.v8
'11.3.244.8-node.17'
> new Intl.NumberFormat('en-US', { useGrouping: true, maximumFractionDigits: 8 }).format("123456789012345678901234567890123456789012345678901234567890123456.78901234")
'123,456,789,012,345,678,901,234,567,890,123,456,789,012,345,678,901,234,567,890,123,456.78901234'

❌ on Node.js 18.12.0 (V8 10.2)

> process.versions.v8
'10.2.154.15-node.12'
> new Intl.NumberFormat('en-US', { useGrouping: true, maximumFractionDigits: 8 }).format("123456789012345678901234567890123456789012345678901234567890123456.78901234")
'123,456,789,012,345,670,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000'

✅ in Electron Chromium (Electron 28, Chromium 120.0.6099.56, V8 12.0, and Node.js 18.18.2)

image

@twhy

twhy commented Apr 17, 2024

Copy link
Copy Markdown
Contributor Author

Problem Description

Intl.NumberFormat.prototype.format()

  1. DOES NOT format very large number properly in Node.js 18.x (V8 10.2).
  2. Function properly in Node.js 20.x (V8 11.3) and Chromium 120+, V8 12.x.

See it in previous comment.
Keep it simple: It functions properly in V8 11.3+

Can the code in this PR run properly ?

YES.
The code is in neuron-ui which run in Chromium 120, V8.12.x, and it works as expected.
See it in the screenshot in previous comment.

Why the Unit Tests workflow failed ?

Unit Tests workflow is run in Node.js 18.12.0 (V8 10.2).
See it in Problem Description.

How to fix it ?

Solution 1

  1. Upgrade Electron to version 29
  2. Update Unit Tests workflow to use Node.js 20.x

Solution 2

  1. Keep using Electron 28.1
  2. Split Unit Tests workflow into two workflows
    • Unit tests in neuron-ui runs in Node.js 20.x
    • Unit tests in neuron-wallet runs in Node.js 18.x

Support

Intl.NumberFormat.prototype.format() works well
Intl.NumberFormat.prototype.format() NOT as expected

Electron Chromium Node.js Note
28 120.0.6099.56, V8 12.0 18.18.2, V8 10.2 We're using version 28.1
29 122.0.6261.39, V8 12.2 20.9.0, V8 11.3 Latest version 30.0.0

Discussion

@Keith-CY @homura @yanguoyu @devchenyan

Please leave comments about your ideas or solutions, or pick one solution from above. Thanks!

@homura

homura commented Apr 17, 2024

Copy link
Copy Markdown
Contributor

The UDT amount data structure is a u128 while the capacity is a u64. For the shannonToCkb function, the number should not exceed 18446744073709551615. I believe this is an overthink.

@yanguoyu

Copy link
Copy Markdown
Contributor

I guess we will update Electron finally, so it's maybe a solution.

@Keith-CY

Copy link
Copy Markdown
Contributor

The UDT amount data structure is a u128 while the capacity is a u64. For the shannonToCkb function, the number should not exceed 18446744073709551615. I believe this is an overthink.

It should work when amount only comes from on-chain data, but it's not reliable when amount comes from outside input, or in some computation.

Say the amount to send is set very large, much over the reasonable value, a message amount exceeds the limit can be appended, but the amount should still be properly formatted.


I would suggest fixing this issue by upgrading Electron because it's introduced by the previous release of Electron.

@twhy

twhy commented Apr 19, 2024

Copy link
Copy Markdown
Contributor Author

This PR has been reviewed before, and all checks have passed after we upgrade Unit Tests Node.js version to 20.11.1
It should be mergable now.

@Keith-CY
Keith-CY added this pull request to the merge queue Apr 19, 2024
Merged via the queue into nervosnetwork:develop with commit 820e073 Apr 19, 2024
@twhy
twhy deleted the refactor-shannonToCKBFormatter-with-formatUnit branch April 19, 2024 10:12
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