Conversation
| let adjustment_interval = self.difficulty_adjustment_interval(); | ||
|
|
||
| if is_for_retarget(adjustment_interval, current_height) { | ||
| let retarget_block_time = retarget_block_time(adjustment_interval, prev_block_index); |
There was a problem hiding this comment.
This doesn't seem to be correct. The block that you use to recalculate the new nBits is the first block in the 2016-block interval. Then, if that is longer than two weeks, the difficulty goes down. If it's less than two weeks, the difficulty goes up.
There was a problem hiding this comment.
I will rename the function to get_starting_block_time.
The function is using the prev_block_index to call the get_ancestor method, based on the adjustment interval.
#99 (comment) #99 (comment) #99 (comment) add num library for min max operations
There was a problem hiding this comment.
I know this would deviate from Bitcoin but the difficulty is determined by the time difference of the last two blocks that are at a height that is a multiple of 2016, so this field is not needed. Even if the field is included, we need to check it has the correct value if it's relied upon for the difficulty check. Have you thought about getting rid of it?
There was a problem hiding this comment.
The bits field is being used in the ff. ways:
- When
no_retargetingfield in the config is set totrue. That only happens for Regtest.mintlayer-core/common/src/chain/pow.rs
Line 59 in 1e390f9
- if it's not due for retargeting yet, and
allow_min_dfficulty_blocksis set tofalse, which is the Mainnet and Signet:mintlayer-core/common/src/chain/pow.rs
Line 65 in 1e390f9
Let's say the current block height is 8067, and it's not due for retarget (since retarget has happened at 8064).
I am assuming that the difficulty is retrieved from the previous block, at height 8066. No calculation needed.
If we remove the bits field, we'll have to keep calculating the time diff of the blocks of height 8064 and 6048.
As for checking whether its value is correct, Did you mean the check_proof_of_work()? Or do you mean checking whether the previous block's bits field is a correct value?
There was a problem hiding this comment.
As for checking whether its value is correct, Did you mean the
check_proof_of_work()? Or do you mean checking whether the previous block's bits field is a correct value?
What I mean is that the value of the bits field cannot be trusted to contain the difficulty anyway. I see the get_work_required method here but I can't see it being used anywhere. I.e. at any point, the following must pass:
assert_eq!(calculate_difficulty_from_the_scratch(...).try_into(), Just(this_block.bits))Blocks that do not satisfy that condition must be rejected, since the check_proof_of_work function relies on the bits field to contain accurate difficulty.
There was a problem hiding this comment.
The get_work_required()(previously check_for_work_required()) will be used in the block production.
This was the earliest implementation : ed8db7d#diff-9a00856cffad13af6904b1cb9d4b5a4fd9e0591f10a35a67ddd805141890eeb6R91
But I removed it in this PR, because it's meant for the block production PR (which is currently closed, until Anton's or this is pushed).
Did you mean because the bits is a u32 representation, while the difficulty is 256, that's why the bits field can't be trusted? Or because the previous block can't be trusted, since the bits field can easily be just any number and not from a calculated value during the generation of the 2016th multiple block?
I haven't checked in bitcoin where that is, but we can actually add that kind of validation.
I somehow do agree with you; removing the bits field is not so bad, since we can always calculate for the difficulty. Your get_block_id_by_height function will be useful (Though I think I have to access it through the BlockIndex).
For now I've been following how Bitcoin does it. But also I don't have problems diverging from Bitcoin.
Although I don't know what Sam @TheQuantumPhysicist thinks, we can wait for his opinion.
#99 (comment) #99 (comment) #99 (comment) add num library for min max operations
…ed few tests. Addressing comments: #99 (comment) #99 (comment) #99 (comment) #99 (comment)
#99 (comment) #99 (comment) #99 (comment) #99 (comment) feat/PoW: add a todo for implementing test cases in work feat/PoW: fix clippy
continue from: #62
address comments:
#62 (comment)
#62 (comment)
#62 (comment)
#62 (comment)
#62 (comment)
#62 (comment)
Another TODO:To be created in another PR.#62 (comment)