Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: sasageyo on August 29, 2018, 09:05:31 PM



Title: Which statement about the difficulty target is the correct one?
Post by: sasageyo on August 29, 2018, 09:05:31 PM
1) The SHA-256 hash of a block's header must be lower than or equal to the current target for the block to be accepted by the network. As mentioned in https://en.bitcoin.it/wiki/Target

or,

2) The hash of the block has to be less than the target. As mentioned by Andreas Antonopoulos here: https://youtu.be/h429LCTRmQw?t=1m28s


Title: Re: Which statement about the difficulty target is the correct one?
Post by: TheArchaeologist on August 29, 2018, 09:23:46 PM
The second one is correct. Each block header must hash to a value below the target threshold.

For more in-depth documentation you can check the developers guide on proof of work here:
https://bitcoin.org/en/developer-guide#proof-of-work (https://bitcoin.org/en/developer-guide#proof-of-work)

Update: see below for proof my linked documentation does not match the reality. #1 is correct.



Title: Re: Which statement about the difficulty target is the correct one?
Post by: odolvlobo on August 30, 2018, 02:51:08 AM
1) The SHA-256 hash of a block's header must be lower than or equal to the current target for the block to be accepted by the network. As mentioned in https://en.bitcoin.it/wiki/Target

or,

2) The hash of the block has to be less than the target. As mentioned by Andreas Antonopoulos here: https://youtu.be/h429LCTRmQw?t=1m28s

It looks to me like the answer is #1.


bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
    bool fNegative;
    bool fOverflow;
    arith_uint256 bnTarget;

    bnTarget.SetCompact(nBits, &fNegative, &fOverflow);

    // Check range
    if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
        return false;

    // Check proof of work matches claimed amount
    if (UintToArith256(hash) > bnTarget)
        return false;


    return true;
}



Title: Re: Which statement about the difficulty target is the correct one?
Post by: TheArchaeologist on August 30, 2018, 06:38:59 AM

bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
    bool fNegative;
    bool fOverflow;
    arith_uint256 bnTarget;

    bnTarget.SetCompact(nBits, &fNegative, &fOverflow);

    // Check range
    if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
        return false;

    // Check proof of work matches claimed amount
    if (UintToArith256(hash) > bnTarget)
        return false;


    return true;
}


Code doesn't lie :) So I agree it should be #1 then. Another case where the (official) documentation doesn't project the reality then!

Anyhow, learned something again :)


Title: Re: Which statement about the difficulty target is the correct one?
Post by: mocacinno on August 30, 2018, 06:43:52 AM

bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
    bool fNegative;
    bool fOverflow;
    arith_uint256 bnTarget;

    bnTarget.SetCompact(nBits, &fNegative, &fOverflow);

    // Check range
    if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
        return false;

    // Check proof of work matches claimed amount
    if (UintToArith256(hash) > bnTarget)
        return false;


    return true;
}


Code doesn't lie :) So I agree it should be #2 then. Another case where the (official) documentation doesn't project the reality then!

Anyhow, learned something again :)

If the hash is bigger than the target, return false.. So if the hash is smaller than OR EQUAL TO the target, true is returned... #1 is correct (if i'm not mistaking)


Title: Re: Which statement about the difficulty target is the correct one?
Post by: TheArchaeologist on August 30, 2018, 07:50:48 AM
If the hash is bigger than the target, return false.. So if the hash is smaller than OR EQUAL TO the target, true is returned... #1 is correct (if i'm not mistaking)

Let's break this down to a simple example:
Code:
Target = 10

Scenario A: Check value 9
Scenario B: Check value 10
Scenario C: Check value 11

Code:
return = true;

#Scenario A:
if (9 > 10 )
        return false;
#Scenario A:  returns ->true

#Scenario B:
if (10 > 10 )
        return false;
#Scenario B:  returns -> true

#Scenario C:
if (11 > 10)
        return false;
#Scenario C:  returns  -> false  

Conclusion: EQAUL TO (as in scenario B) returns true, meaning it is accepted. In other words: #1 is the correct one.


Title: Re: Which statement about the difficulty target is the correct one?
Post by: waxload on August 30, 2018, 08:34:43 AM
Hi,

Actually it seems that the wiki have some errors : and a lot.

Definitely the second answer is correct ! From my past experience in mining sha-256 ...hash need to be greater to be accepted and if it is lower you try again and every re targeting period the difficulty increase.



Title: Re: Which statement about the difficulty target is the correct one?
Post by: bob123 on August 30, 2018, 09:03:37 AM
From my past experience in mining sha-256 ...hash need to be greater to be accepted and if it is lower you try again and every re targeting period the difficulty increase.

You obviously don't have any experience at all.
The hash needs to be LOWER or EQUAL TO the target. This has been discussed in this thread and should be clear if you have completely read it.

The difficulty also does NOT increase each 2016 blocks. This depends on time it took to mine these blocks.
Overall, the difficulty rises. But there also were shorter streaks where the difficulty and hashrate was dropping over multiple periods.


Title: Re: Which statement about the difficulty target is the correct one?
Post by: mocacinno on August 30, 2018, 09:40:20 AM
If the hash is bigger than the target, return false.. So if the hash is smaller than OR EQUAL TO the target, true is returned... #1 is correct (if i'm not mistaking)

Let's break this down to a simple example:
Code:
Target = 10

Scenario A: Check value 9
Scenario B: Check value 10
Scenario C: Check value 11

Code
return = true;

#Scenario A:
if (9 > 10 )
        return false;
#Scenario A:  returns ->true

#Scenario B:
if (10 > 10 )
        return false;
#Scenario B:  returns -> true

#Scenario C:
if (11 > 10)
        return false;
#Scenario B:  returns  -> false  

Conclusion: EQAUL TO (as in scenario B) returns true, meaning it is accepted. In other words: #2 is the correct one.

I'm on my employer's laptop, which only has php installed, so i wrote this analogy with the code:
Code:
<?php
$target 
10;
$hash $target 2;
$end $target 2;

while (
$hash $end)
{
        echo 
"testing hash $hash vs target $target => ";
        if (
$hash $target)
        {
                echo 
"POW does not match claimed ammount\n";
        }
        else
        {
                echo 
"OK!\n";
        }
        
$hash++;
}
?>


response:
Code:
testing hash 8 vs target 10 => OK!
testing hash 9 vs target 10 => OK!
testing hash 10 vs target 10 => OK!
testing hash 11 vs target 10 => POW does not match claimed ammount

Since the OP was:
1) The SHA-256 hash of a block's header must be lower than or equal to the current target for the block to be accepted by the network. As mentioned in https://en.bitcoin.it/wiki/Target

or,

2) The hash of the block has to be less than the target. As mentioned by Andreas Antonopoulos here: https://youtu.be/h429LCTRmQw?t=1m28s

I still think #1 is correct, the function will return true if the hash is lower than or equal to the target ;)

EDIT: after re-reading your reply and noticing your conclusion was the same as mine: the hash has to be lower than or equal to, i just realised one of us is just misinterpreting the OP...
We both conclude that hashes lower than or equal to the target are fine... It's just that my interpretation of sollution one is exactly correct, while my interpretation of sollution 2 says that only hashes lower than the target are true (which is not correct if i'm not mistaking)...


Title: Re: Which statement about the difficulty target is the correct one?
Post by: TheArchaeologist on August 30, 2018, 09:50:32 AM

I still think #1 is correct, the function will return true if the hash is lower than or equal to the target ;)


We came to the same conclusion :) I mixed up by claiming #2 was right while delivering proof #1 was right (just as you did). I edited my post to match the right number.

End conclusion: We agree #1 is the correct one. We even both delivered examples as proof :)


Title: Re: Which statement about the difficulty target is the correct one?
Post by: bob123 on August 30, 2018, 10:11:03 AM
End conclusion: We agree #1 is the correct one. We even both delivered examples as proof :)

Note: An example can NEVER proof a theory. It can only disprove one.

The real prove is this:


bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
~snip~
    // Check proof of work matches claimed amount
    if (UintToArith256(hash) > bnTarget)
        return false;

    return true;
}


The statement is:
Hash > Target  THEN  Not valid

If we negate that statement (to match the statement 'valid'):
Hash <= Target THEN Valid

And this matches #1 from OP.