Truncation bug in GetDifficulty, rpc.cpp #236

issue OverQuantum opened this issue on May 18, 2011
  1. OverQuantum commented at 5:26 PM on May 18, 2011: none

    It seems to me, that GetDifficulty() function have truncation problem which is already observer now and could lead to further problems, up to division by zero.

    1. http://blockexplorer.com/b/124677 -> Difficulty?: 157 416.401843 ("Bits"?: 1a6a93b3)
    2. bitcoin.exe getdifficulty -> 157426.20628986 (at the moment) Why so?

    hextarget = 0000 0000 0000 6A93 B300 00000000000000000000000000000000000000000000 maxtarget = 0000 0000 FFFF 0000 0000 00000000000000000000000000000000000000000000 If we divide them, result will be 157416.4018436490, value in block's info.

    Problem is in getdifficulty function: int nShift = 256 - 32 - 31; // to fit in a uint double dMinimum = (CBigNum().SetCompact(bnProofOfWorkLimit.GetCompact()) >> nShift).getuint(); double dCurrently = (CBigNum().SetCompact(pindexBest->nBits) >> nShift).getuint(); return dMinimum / dCurrently;

    So, bnProofOfWorkLimit >> nShift = 0x7FFF8000 while nBits >> nShift = 0x3549 return will be 157426.20628986100 that we observe in bitcoin client, on blockexplorer's getdifficulty and so on.

    From 24 "mantissa" bits of hextarget only 15 are used, and problem will increase with difficulty. At 2 billions will will get divison by zero.

  2. gavinandresen commented at 5:33 PM on May 18, 2011: contributor

    Seems like a nice, well-defined problem for somebody new to tackle. Conversion of difficulty to floating point isn't a critical function (internally it is all uint256's), and we're not going to get to 2 billion difficulty soon, so not high priority.

  3. TheBlueMatt referenced this in commit ff8bf2fd4d on May 26, 2011
  4. TheBlueMatt referenced this in commit 2cde58c946 on May 26, 2011
  5. gavinandresen closed this on Jun 7, 2011

  6. sipa referenced this in commit a591d98c32 on Apr 22, 2015
  7. TheBlueMatt referenced this in commit 582b2934e6 on Oct 20, 2015
  8. destenson referenced this in commit 2a00b185ee on Jun 26, 2016
  9. deadalnix referenced this in commit 9688030874 on Jan 19, 2017
  10. deadalnix referenced this in commit c6e92520fa on May 10, 2017
  11. attilaaf referenced this in commit eb264d1bb1 on Jan 13, 2020
  12. cryptapus referenced this in commit de19d9b621 on May 3, 2021
  13. rajarshimaitra referenced this in commit 53c49f309d on Aug 5, 2021
  14. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 21:16 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me