<pre><code>    if (nActualTimespan &lt; params.nPowTargetTimespan/4)
        nActualTimespan = params.nPowTargetTimespan/4;
    if (nActualTimespan &gt; params.nPowTargetTimespan*2)
        nActualTimespan = params.nPowTargetTimespan*2;
</code></pre>

<p>This does the opposite of what you want. It increases the difficulty quickly, but decreases the difficulty slowly.</p>

<p>Here's what you meant to do:</p>

<pre><code>    if (nActualTimespan &lt; params.nPowTargetTimespan/2)
        nActualTimespan = params.nPowTargetTimespan/2;
    if (nActualTimespan &gt; params.nPowTargetTimespan*4)
        nActualTimespan = params.nPowTargetTimespan*4;
</code></pre>

CC BY-SA 3.0
2017-08-08T01:33:00.357Z
what parameters need to use for decrees it quickly , but increase slowly . End exit any alternative  method to add on blockchain - mine block with low diff for reset diff  for new block's ?
mraksoll
CC BY-SA 3.0
2017-08-08T02:00:32.593Z
I do not understand if it's only for increase , where the decrease setting's ???

I think it's rule with /4 it's for decrease end rule with* for increase :/
mraksoll
CC BY-SA 3.0
2017-08-08T02:02:01.567Z
The code you posted caps the measured timespan at one fourth target, or twice the target. This limits the difficulty change in one retarget period to increasing by a factor of 4, or decreasing by a factor of 2.
Nick ODell
CC BY-SA 3.0
2017-08-08T02:06:54.057Z
I need use

/2 /2 *4 *4 or it's again wrong ? You can make little example with any setting how it be correct :/ Or for decrease x4 end increase only x2
mraksoll
CC BY-SA 3.0
2017-08-08T02:08:17.707Z
See edit I added.
Nick ODell
CC BY-SA 3.0
2017-08-08T02:16:58.423Z
End about block , it's possible to add low diff block after huge to blockchain ? For new rules start work after this block, i try with low diff block with parameter only for they height ,  but it's stop accept old block's and how i understand it's wrong to.  But cannot find any information about this.
mraksoll
CC BY-SA 3.0
2017-08-08T02:44:10.717Z
Bitcoin's rules require a miner to set a difficulty before trying to mine a block, and the difficulty must exactly match what the validating node expects. That is why it won't accept old blocks.
Nick ODell
CC BY-SA 3.0
2017-08-08T03:05:16.700Z
I understand this , but with retarget system it's work , why not work with min diff block's  , for example the latest block is 2000 , i try == 2001, and >2000. Maybe exist other way to do this and I simple not find it.
mraksoll
CC BY-SA 3.0
2017-08-08T03:05:58.010Z
I'm not sure I understand the question. Can you rephrase?
Nick ODell
CC BY-SA 3.0
2017-08-08T03:07:39.937Z
Example on fist post , latest code
if(pindexLast->nHeight+1 > 2001)  code else code

i try this trick to min diff block  to , but it now work , but for retarget work perfect
mraksoll
CC BY-SA 3.0
2017-08-08T03:08:26.407Z
Ok. What's the problem with that code?
Nick ODell
CC BY-SA 3.0
2017-08-08T03:12:06.457Z
code before retarget with this trick, i try do the same with min diff block's ... For mine  new block after block with huge diff and after this block disable low diff block's for normal mining with normal diff.
mraksoll
CC BY-SA 3.0
2017-08-08T03:16:15.407Z
I need low diff only for one block after 2000 for drop diff. But rule with block height not work for this function, but perfect work for retarget.
mraksoll
CC BY-SA 3.0
2017-08-08T03:20:31.780Z
So you want a one-time difficulty drop at block 2000?
Nick ODell
CC BY-SA 3.0
2017-08-08T03:25:42.520Z
Yes this why i try use function low diff block with rule only for block after 2000. for fully reset diff, then  new block's start work with new retarget rules  for more stable diff increase.
mraksoll
CC BY-SA 3.0
2017-08-08T03:37:14.857Z
I try find solution to do this , but not find any solution and try this.
mraksoll
CC BY-SA 3.0
2017-08-08T03:59:40.823Z
exist any solution for this ? :/
mraksoll
CC BY-SA 3.0
2017-08-08T04:34:04.773Z
what i try to do , create new block with low diff and start calculate new diff from them.
mraksoll
CC BY-SA 3.0
2017-08-08T01:28:26.057Z
2017-08-08T02:08:02.273Z
2017-08-08T02:08:02.273Z
Nick ODell
<p>the standard values</p>

<pre><code>    if (nActualTimespan &lt; params.nPowTargetTimespan/4)
        nActualTimespan = params.nPowTargetTimespan/4;
    if (nActualTimespan &gt; params.nPowTargetTimespan*4)
        nActualTimespan = params.nPowTargetTimespan*4;
</code></pre>

<p>but if i use </p>

<pre><code>        if (nActualTimespan &lt; params.nPowTargetTimespan/4)
            nActualTimespan = params.nPowTargetTimespan/4;
        if (nActualTimespan &gt; params.nPowTargetTimespan*2)
            nActualTimespan = params.nPowTargetTimespan*2;
</code></pre>

<p>It's be valid or it's can broke diff system ? </p>

<p>What values can i add for diff is not grow very quickly, but drop faster than grow for it be more stable for fast block time. </p>

<p>setting of block time is</p>

<pre><code>consensus.nPowTargetTimespan = 10 * 60; // 10 min
consensus.nPowTargetSpacing = 5 * 60; //5 min
</code></pre>

<p>And another question</p>

<p>I try use the exist function low diff block what created for testnet for only block with Height 2001 .
Previous exist block is 2000  and latest now in blockchain.</p>

<pre><code>if ((pindexLast-&gt;nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
{
    if(pindexLast-&gt;nHeight+1 == 2001)
    {
        // Special difficulty rule for testnet:
        // If the new block's timestamp is more than 2* 10 minutes
        // then allow mining of a min-difficulty block.
        if (pblock-&gt;GetBlockTime() &gt; pindexLast-&gt;GetBlockTime() + params.nPowTargetSpacing*2)
            return nProofOfWorkLimit;
        else
        {
            // Return the last non-special-min-difficulty-rules-block
            const CBlockIndex* pindex = pindexLast;
            while (pindex-&gt;pprev &amp;&amp; pindex-&gt;nHeight % params.DifficultyAdjustmentInterval() != 0 &amp;&amp; pindex-&gt;nBits == nProofOfWorkLimit)
                pindex = pindex-&gt;pprev;
            return pindex-&gt;nBits;
        }
    }
    return pindexLast-&gt;nBits;           
}
else
{           
    if (params.fPowAllowMinDifficultyBlocks)
    {
        // Special difficulty rule for testnet:
        // If the new block's timestamp is more than 2* 10 minutes
        // then allow mining of a min-difficulty block.
        if (pblock-&gt;GetBlockTime() &gt; pindexLast-&gt;GetBlockTime() + params.nPowTargetSpacing*2)
            return nProofOfWorkLimit;
        else
        {
            // Return the last non-special-min-difficulty-rules-block
            const CBlockIndex* pindex = pindexLast;
            while (pindex-&gt;pprev &amp;&amp; pindex-&gt;nHeight % params.DifficultyAdjustmentInterval() != 0 &amp;&amp; pindex-&gt;nBits == nProofOfWorkLimit)
                pindex = pindex-&gt;pprev;
            return pindex-&gt;nBits;
        }
    }
    return pindexLast-&gt;nBits;
}
</code></pre>

<p>And use new rule retarget for new block's after 2001 and accept old block with old retarget rule.</p>

<pre><code>   if(pindexLast-&gt;nHeight+1 &gt; 2001)
    {
        if (nActualTimespan &lt; params.nPowTargetTimespan/4)
            nActualTimespan = params.nPowTargetTimespan/4;
        if (nActualTimespan &gt; params.nPowTargetTimespan*2)
            nActualTimespan = params.nPowTargetTimespan*2;
    }
    else
    {
        if (nActualTimespan &lt; params.nPowTargetTimespan/4)
            nActualTimespan = params.nPowTargetTimespan/4;
        if (nActualTimespan &gt; params.nPowTargetTimespan*4)
            nActualTimespan = params.nPowTargetTimespan*4;
    }
</code></pre>

<p>About what i asking , is this solution is right and it will work, or this solution is wrong end exist another way to do this ?</p>

<block><altcoin-development><c++><difficulty-retargetting><c>
Question about retarget system bitcoin, add hardcodet block to exist blockchain
mraksoll
# Question about retarget system bitcoin, add hardcodet block to exist blockchain

(Asked by: username_0 on 2017-08-07)

the standard values

```
    if (nActualTimespan < params.nPowTargetTimespan/4)
        nActualTimespan = params.nPowTargetTimespan/4;
    if (nActualTimespan > params.nPowTargetTimespan*4)
        nActualTimespan = params.nPowTargetTimespan*4;

```

but if i use

```
        if (nActualTimespan < params.nPowTargetTimespan/4)
            nActualTimespan = params.nPowTargetTimespan/4;
        if (nActualTimespan > params.nPowTargetTimespan*2)
            nActualTimespan = params.nPowTargetTimespan*2;

```

It's be valid or it's can broke diff system ?

What values can i add for diff is not grow very quickly, but drop faster than grow for it be more stable for fast block time.

setting of block time is

```
consensus.nPowTargetTimespan = 10 * 60; // 10 min
consensus.nPowTargetSpacing = 5 * 60; //5 min

```

And another question

I try use the exist function low diff block what created for testnet for only block with Height 2001 .
Previous exist block is 2000 and latest now in blockchain.

```
if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
{
    if(pindexLast->nHeight+1 == 2001)
    {
        // Special difficulty rule for testnet:
        // If the new block's timestamp is more than 2* 10 minutes
        // then allow mining of a min-difficulty block.
        if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
            return nProofOfWorkLimit;
        else
        {
            // Return the last non-special-min-difficulty-rules-block
            const CBlockIndex* pindex = pindexLast;
            while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
                pindex = pindex->pprev;
            return pindex->nBits;
        }
    }
    return pindexLast->nBits;           
}
else
{           
    if (params.fPowAllowMinDifficultyBlocks)
    {
        // Special difficulty rule for testnet:
        // If the new block's timestamp is more than 2* 10 minutes
        // then allow mining of a min-difficulty block.
        if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
            return nProofOfWorkLimit;
        else
        {
            // Return the last non-special-min-difficulty-rules-block
            const CBlockIndex* pindex = pindexLast;
            while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
                pindex = pindex->pprev;
            return pindex->nBits;
        }
    }
    return pindexLast->nBits;
}

```

And use new rule retarget for new block's after 2001 and accept old block with old retarget rule.

```
   if(pindexLast->nHeight+1 > 2001)
    {
        if (nActualTimespan < params.nPowTargetTimespan/4)
            nActualTimespan = params.nPowTargetTimespan/4;
        if (nActualTimespan > params.nPowTargetTimespan*2)
            nActualTimespan = params.nPowTargetTimespan*2;
    }
    else
    {
        if (nActualTimespan < params.nPowTargetTimespan/4)
            nActualTimespan = params.nPowTargetTimespan/4;
        if (nActualTimespan > params.nPowTargetTimespan*4)
            nActualTimespan = params.nPowTargetTimespan*4;
    }

```

About what i asking , is this solution is right and it will work, or this solution is wrong end exist another way to do this ?

**Tags:** block, altcoin-development, c++, difficulty-retargetting, c


---

## 1 answers

### Answer by username_1 on 2017-08-08. Score: 0 | Accepted answer

```
    if (nActualTimespan < params.nPowTargetTimespan/4)
        nActualTimespan = params.nPowTargetTimespan/4;
    if (nActualTimespan > params.nPowTargetTimespan*2)
        nActualTimespan = params.nPowTargetTimespan*2;

```

This does the opposite of what you want. It increases the difficulty quickly, but decreases the difficulty slowly.

Here's what you meant to do:

```
    if (nActualTimespan < params.nPowTargetTimespan/2)
        nActualTimespan = params.nPowTargetTimespan/2;
    if (nActualTimespan > params.nPowTargetTimespan*4)
        nActualTimespan = params.nPowTargetTimespan*4;

```

> **Comments:**
> **username_0**: what parameters need to use for decrees it quickly , but increase slowly . End exit any alternative  method to add on blockchain - mine block with low diff for reset diff  for new block's ?  
> **username_0**: I do not understand if it's only for increase , where the decrease setting's ???  I think it's rule with /4 it's for decrease end rule with* for increase :/  
> **username_1**: The code you posted caps the measured timespan at one fourth target, or twice the target. This limits the difficulty change in one retarget period to increasing by a factor of 4, or decreasing by a factor of 2.  
> **username_0**: I need use  /2 /2 *4 *4 or it's again wrong ? You can make little example with any setting how it be correct :/ Or for decrease x4 end increase only x2  
> **username_1**: See edit I added.  
> **username_0**: End about block , it's possible to add low diff block after huge to blockchain ? For new rules start work after this block, i try with low diff block with parameter only for they height ,  but it's stop accept old block's and how i understand it's wrong to.  But cannot find any information about this.  
> **username_1**: Bitcoin's rules require a miner to set a difficulty before trying to mine a block, and the difficulty must exactly match what the validating node expects. That is why it won't accept old blocks.  
> **username_0**: I understand this , but with retarget system it's work , why not work with min diff block's  , for example the latest block is 2000 , i try == 2001, and >2000. Maybe exist other way to do this and I simple not find it.  
> **username_1**: I'm not sure I understand the question. Can you rephrase?  
> **username_0**: Example on fist post , latest code if(pindexLast->nHeight+1 > 2001)  code else code  i try this trick to min diff block  to , but it now work , but for retarget work perfect  
> **username_1**: Ok. What's the problem with that code?  
> **username_0**: code before retarget with this trick, i try do the same with min diff block's ... For mine  new block after block with huge diff and after this block disable low diff block's for normal mining with normal diff.  
> **username_0**: I need low diff only for one block after 2000 for drop diff. But rule with block height not work for this function, but perfect work for retarget.  
> **username_1**: So you want a one-time difficulty drop at block 2000?  
> **username_0**: Yes this why i try use function low diff block with rule only for block after 2000. for fully reset diff, then  new block's start work with new retarget rules  for more stable diff increase.  
> **username_0**: I try find solution to do this , but not find any solution and try this.  
> **username_0**: exist any solution for this ? :/  
> **username_0**: what i try to do , create new block with low diff and start calculate new diff from them.  

---

