Skip to content

Handle positive infinity and negative infinity#593

Merged
scravy merged 3 commits into
dtr-org:masterfrom
scravy:deal-with-zero-difficulty
Feb 13, 2019
Merged

Handle positive infinity and negative infinity#593
scravy merged 3 commits into
dtr-org:masterfrom
scravy:deal-with-zero-difficulty

Conversation

@scravy

@scravy scravy commented Feb 12, 2019

Copy link
Copy Markdown
Member

If for whatever reason nBits is zero, the difficulty calculation (which is carried out using double types) will yield positive or negative infinity.

Infinity fails to properly serialize using univalue which will lead to broken json.

This patch will simply set it to the maximum and minimum finite values in case we have positive or negative infinity. These values are still strange enough that they allow you to diagnose the problem.

This is extracted from #577

Signed-off-by: Julian Fleischer julian@thirdhash.com

@scravy scravy added the bug A problem of existing functionality label Feb 12, 2019
@scravy scravy added this to the 0.1 milestone Feb 12, 2019
@scravy scravy self-assigned this Feb 12, 2019
@scravy scravy requested review from a team and nzmdn February 12, 2019 21:34
@scravy scravy force-pushed the deal-with-zero-difficulty branch from 184343d to ec65dcf Compare February 12, 2019 21:35
@scravy scravy requested a review from castarco February 12, 2019 22:01
@thothd

thothd commented Feb 12, 2019

Copy link
Copy Markdown
Member

In which cases nBits is 0 ?

@scravy

scravy commented Feb 12, 2019

Copy link
Copy Markdown
Member Author
  • When you set it to zero during development and lose a day on broken json.
  • When a block was forged that way, maliciously or by accident
  • When you're experimenting on Adjust difficulty #285 and you're function unluckily hits zero, try to debug it, and all you get is broken json

I would love to patch univalue to just render infinities, but it's one of these git subtree thingies.

@scravy

scravy commented Feb 12, 2019

Copy link
Copy Markdown
Member Author

Alternative: Could check for infinity before creating the univalue, same file, and then render the string "+Inf" or "-Inf" instead. The function patched here is exclusively used in this RPC call.

Signed-off-by: Julian Fleischer <julian@thirdhash.com>
@scravy scravy force-pushed the deal-with-zero-difficulty branch from ec65dcf to fc12e5c Compare February 12, 2019 22:27
@scravy

scravy commented Feb 12, 2019

Copy link
Copy Markdown
Member Author

Pushed the alternative.

Comment thread src/rpc/blockchain.cpp Outdated
Signed-off-by: Julian Fleischer <julian@thirdhash.com>
@scravy scravy force-pushed the deal-with-zero-difficulty branch from 984e52b to 4a79f5f Compare February 12, 2019 22:39
@scravy

scravy commented Feb 12, 2019

Copy link
Copy Markdown
Member Author

Refactored.

@thothd thothd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

utACK 4a79f5f

Comment thread src/rpc/util.cpp Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For reviewers, in case you're not aware:

[cling]$ double x;
[cling]$ double z = x / x;
[cling]$ z == z
(bool) false

NaN is like Drölf (Drölf is 12 and 13 at the same time, and thus x == x + 1 holds true for it only).

@thothd

thothd commented Feb 12, 2019

Copy link
Copy Markdown
Member

utACK 4e8ae4c

@scravy scravy requested a review from a team February 12, 2019 23:41
@scravy scravy merged commit 3e448bd into dtr-org:master Feb 13, 2019
@scravy scravy deleted the deal-with-zero-difficulty branch February 18, 2019 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug A problem of existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants