jimmysong / programmingbitcoin

Repository for the book
Other
1.75k stars 656 forks source link

MAX_TARGET in Chapter 9 Exercise 13 #142

Open Engelberg opened 5 years ago

Engelberg commented 5 years ago

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.

chanhosuh commented 5 years ago

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
Engelberg commented 5 years ago

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.

chanhosuh commented 5 years ago

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