Skip to content

Commit

Permalink
Return correct target for TestNet blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Enthusiast committed Jul 28, 2024
1 parent 839819a commit 5d99eb9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
30 changes: 27 additions & 3 deletions Src/Autarkysoft.Bitcoin/Blockchain/Chain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,36 @@ public Target GetNextTarget(BlockHeader first, BlockHeader last)
}

/// <inheritdoc/>
public Target GetNextTarget()
public Target GetNextTarget(in BlockHeader hdr)
{
// TODO: difficulty calculation is different for testnet
int height = headerList.Count;
if (height % Constants.DifficultyAdjustmentInterval != 0)
{
if (Consensus.AllowMinDifficultyBlocks)
{
// 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 (hdr.BlockTime > headerList[^1].BlockTime + TimeConstants.Seconds.TwentyMin)
{
var temp = new BigInteger(Consensus.PowLimit.ToByteArray());
return new Target(temp);
}
else
{
var temp = new BigInteger(Consensus.PowLimit.ToByteArray());
Target min = new Target(temp);

// Return the last non-special-min-difficulty-rules-block
height--;
while (height > 0 && height % Constants.DifficultyAdjustmentInterval != 0 && headerList[height].NBits == min)
{
height--;
}
return headerList[height].NBits;
}
}

return headerList[^1].NBits;
}
else
Expand Down Expand Up @@ -564,7 +588,7 @@ public bool ProcessHeaders(BlockHeader[] headers, INodeStatus nodeStatus)
int count = 0;
for (int i = arrIndex; i < headers.Length; i++)
{
if (ProcessHeader(headers[i], headerList[^1], headerList.Count, GetNextTarget()))
if (ProcessHeader(headers[i], headerList[^1], headerList.Count, GetNextTarget(headers[i])))
{
headerList.Add(headers[i]);
count++;
Expand Down
2 changes: 1 addition & 1 deletion Src/Autarkysoft.Bitcoin/Blockchain/IChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface IChain
/// Returns the next difficulty target based on the best stored chain.
/// </summary>
/// <returns>Next difficulty target</returns>
Target GetNextTarget();
Target GetNextTarget(in BlockHeader hdr);

/// <summary>
/// Processes all the blocks that this peer received in its <see cref="INodeStatus.DownloadedBlocks"/>
Expand Down
6 changes: 6 additions & 0 deletions Src/Autarkysoft.Bitcoin/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Distributed under the MIT software license, see the accompanying
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.

using System.ComponentModel;

namespace Autarkysoft.Bitcoin
{
/// <summary>
Expand Down Expand Up @@ -227,6 +229,10 @@ public struct TimeConstants
/// </summary>
public struct Seconds
{
/// <summary>
/// 20 minutes in seconds
/// </summary>
public const int TwentyMin = 20 * 60;
/// <summary>
/// One day or 24 hours in seconds
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions Src/Tests/Bitcoin/Blockchain/MockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Autarkysoft.Bitcoin.P2PNetwork.Messages;
using System;
using System.Collections.Generic;
using Xunit;

namespace Tests.Bitcoin.Blockchain
{
Expand Down Expand Up @@ -55,7 +54,7 @@ public int Height
public Digest256 Tip => _tip;

internal Target? targetToReturn;
public Target GetNextTarget()
public Target GetNextTarget(in BlockHeader hdr)
{
Assert.True(targetToReturn.HasValue, UnexpectedCall);
return targetToReturn.Value;
Expand Down

0 comments on commit 5d99eb9

Please sign in to comment.