programmingbitcoin icon indicating copy to clipboard operation
programmingbitcoin copied to clipboard

MAX_TARGET in Chapter 9 Exercise 13

Open Engelberg opened this issue 6 years ago • 3 comments

The instructions for the exercise in chapter 9's helper.py says:

# if the new target is bigger than MAX_TARGET, set to MAX_TARGET

However, MAX_TARGET is not defined in helper.py, and the answer in answers.py makes no reference to MAX_TARGET.

Engelberg avatar May 15 '19 21:05 Engelberg

I think MAX_TARGET must be 0xffff * 256**(0x1d-3), since Block.difficulty in block.py has these comments:

    def difficulty(self):
        '''Returns the block difficulty based on the bits'''
        # note difficulty is (target of lowest difficulty) / (self's target)
        # lowest difficulty has bits that equal 0xffff001d
        target = self.target()
        difficulty = 0xffff * 256**(0x1d-3) / target
        return difficulty

chanhosuh avatar Jun 02 '19 23:06 chanhosuh

Yes, I used that value in my code, and it seemed to be essential for verifying the difficulty adjustments for the initial blocks of the main chain, right after the genesis block, back when very few computers were contributing hash power. I was just surprised not to see the value in the actual sample code, only the comments, given that it seems to be important.

Engelberg avatar Jun 03 '19 00:06 Engelberg

@Engelberg Yes, I agree! As far as resolving the issue with a PR goes, seems like:

  • [ ] helper.py should have the constant MAX_TARGET
  • [ ] answer should be updated to use MAX_TARGET,
  • [ ] a sentence or two in the chapter text explaining this

chanhosuh avatar Jun 03 '19 00:06 chanhosuh