Giter Club home page Giter Club logo

Comments (3)

styppo avatar styppo commented on August 27, 2024

I modelled the difficulty according to the bitcoin specs (see https://en.bitcoin.it/wiki/Difficulty and https://en.bitcoin.it/wiki/Target). The minimum difficulty is 1, which corresponds to the max target Policy.BLOCK_TARGET_MAX (compact 0x1f00ffff). The maximum difficulty is Policy.BLOCK_TARGET_MAX (target 1, compact 0x01010000).

Current differences to Bitcoin:

  • We currently use a MAX_TARGET of compact 0x1f00ffff (bitcoin 0x1d00ffff), i.e. difficulty 1 is 16 leading zero bits (bitcoin 32). Otherwise, our wimpy browser miner needs like 3 days to mine a block at the lowest difficulty (which really sucks for testing).
  • In some cases, the Bitcoin client uses a target of 0 (difficulty infinity). We enforce a minimum target of 1.

from core-js.

styppo avatar styppo commented on August 27, 2024

The behaviour difficulty !== BlockUtils.compactToDifficulty(BlockUtils.difficultyToCompact(difficulty)) for some difficulties is due to the fact the the compact representation is lossy. The compact representation (or nBits) encodes a 256 bit target value in 32 bits and is passed along in the block headers, so we want it to be small.

When computing BlockUtils.difficultyToCompact(difficulty), we first compute the target value that is difficulty times harder than the Policy.BLOCK_TARGET_MAX: target = Policy.BLOCK_TARGET_MAX / difficulty. For difficulty = 250 this gives a target value of 10623d70a3d70a000000000000000000000000000000000000000000000. The compact representation of this target is 1e010623, which loses a lot of precision. Expanding this to a target again gives a truncated target value of 10623000000000000000000000000000000000000000000000000000000, which corresponds to difficulty = 250.00312933077026.

We only pass around compact target representations, i.e. all clients should work with truncated targets. The behaviour seems indeed unexpected, but in the end, I guess that's how it's supposed to work.

from core-js.

paberr avatar paberr commented on August 27, 2024

Makes sense. I adjusted the test to now only rely on nBits instead of the difficulty.

from core-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.