-
Notifications
You must be signed in to change notification settings - Fork 107
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
change(rpc): Simplify getdifficulty
RPC implementation
#6105
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #6105 +/- ##
==========================================
+ Coverage 77.98% 78.03% +0.05%
==========================================
Files 304 304
Lines 39042 39087 +45
==========================================
+ Hits 30446 30501 +55
+ Misses 8596 8586 -10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the code changes are ok however i am not convinced with the differences, they are pretty big when compared with the original implementation which i suppose is what the users will be expecting to see.
Sure, I was thinking about that as well. I can try some different calculations that are more accurate: Just divide the first 128 bits, because floats only have 53 bits of accuracy anyway:
Just divide the first 64 non-zero bits:
|
65dd2f9
to
62ad153
Compare
This worked, and it is accurate to 6 significant figures. I also think it might be more accurate than |
It looks like actual difficulties are almost identical, which makes sense because we're now doing the same calculation as Mainnet: $ zcash-rpc-diff 28232 getdifficulty
Checking first node release info...
Checking second node release info...
Connected to zebrad (port 28232) and zcashd (zcash-cli zcash.conf port).
Checking zebrad network and tip height...
Checking zcashd network and tip height...
Request:
getdifficulty
Querying zebrad main chain at height >=1975206...
real 0m0.007s
user 0m0.004s
sys 0m0.004s
Querying zcashd main chain at height >=1975206...
real 0m0.007s
user 0m0.004s
sys 0m0.003s
Response diff between zebrad and zcashd:
RPC responses were identical
/run/user/1000/tmp.rzDcrX0wwL.rpc-diff/zebrad-main-1975206-getdifficulty.json:
87521404.99683589 I also got identical responses for heights 1975207, 1975209, and 1975211. Testnet: $ ZCASH_CLI=zcash-cli-testnet zcash-rpc-diff 38232 getdifficulty
Checking first node release info...
Checking second node release info...
Connected to zebrad (port 38232) and zcashd (zcash-cli-testnet zcash.conf port).
Checking zebrad network and tip height...
Checking zcashd network and tip height...
Request:
getdifficulty
Querying zebrad test chain at height >=2219908...
real 0m0.011s
user 0m0.007s
sys 0m0.004s
Querying zcashd test chain at height >=2219908...
real 0m0.011s
user 0m0.005s
sys 0m0.006s
Response diff between zebrad and zcashd:
--- /run/user/1000/tmp.SXZe2QzSL1.rpc-diff/zebrad-test-2219908-getdifficulty.json 2023-02-07 11:47:27.609433705 +1000
+++ /run/user/1000/tmp.SXZe2QzSL1.rpc-diff/zcashd-test-2219908-getdifficulty.json 2023-02-07 11:47:27.620433601 +1000
@@ -1 +1 @@
-16.902352256844026
+16.90235225684403 I got an identical response for height 2219910. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks the same as the current implementation in regards to precision. Thanks.
Motivation
We already have
ExpandedDifficulty
andWork
types that implement the calculations required for thegetdifficulty
RPC.Specifications
https://zcash.github.io/rpc/getdifficulty.html
ToTarget()
from:https://zips.z.cash/protocol/protocol.pdf#nbits
Complex Code or Requirements
We're using floating-point here, so the results from
zcashd
and Zebra will be slightly different.zcashd
does(double)limit.mantissa / (double)bits.mantissa * 256.0^(limit.exponent - bits.exponent)
:https://github.com/zcash/zcash/blob/7b28054e8b46eb46a9589d0bdc8e29f9fa1dc82d/src/rpc/blockchain.cpp#L46-L73
This PR makes Zebra just divide the first 128 bits, because floats only have 53 bits of accuracy anyway:
A previous version of this PR did:
(2^256 / (bits.mantissa * 256^bits.exponent)) as f64 / (2^256 / (limit.mantissa * 256^limit.exponent)) as f64
, which was less accurate due to the integer division:zebra/zebra-chain/src/work/difficulty.rs
Lines 314 to 329 in d67b3b6
Solution
getdifficulty
RPC implementationReview
This is a suggestion for PR #6099.
Reviewer Checklist
Follow Up Work
I added some TODOs to this PR, but we don't need to fix them any time soon. The current code works.