Skip to content

Commit

Permalink
Change Difficulty type to long
Browse files Browse the repository at this point in the history
  • Loading branch information
earlbread committed Apr 23, 2019
1 parent ea22b56 commit 93c1c94
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public NullPolicy(InvalidBlockException exceptionToThrow = null)
_exceptionToThrow = exceptionToThrow;
}

public int GetNextBlockDifficulty(IReadOnlyList<Block<T>> blocks) =>
public long GetNextBlockDifficulty(IReadOnlyList<Block<T>> blocks) =>
blocks.Any() ? 1 : 0;

public InvalidBlockException ValidateNextBlock(
Expand Down
6 changes: 3 additions & 3 deletions Libplanet.Tests/Blockchain/Policies/BlockPolicyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ private IEnumerable<Block<DumbAction>> MineBlocks(
var miner = default(Address);
long i = 0;
HashDigest<SHA256>? previousHash = null;
foreach ((int timestampHour, int d) in blockArgs)
foreach ((int timestampHour, long d) in blockArgs)
{
int difficulty = d;
long difficulty = d;
var timestamp =
FixtureEpoch + TimeSpan.FromHours(timestampHour);
if (interprocess != null)
Expand Down Expand Up @@ -398,7 +398,7 @@ private IEnumerable<Block<DumbAction>> MineBlocks(
private struct BlockFields
{
internal long Index;
internal int Difficulty;
internal long Difficulty;
internal HashDigest<SHA256>? PreviousHash;
internal DateTimeOffset Timestamp;
}
Expand Down
10 changes: 5 additions & 5 deletions Libplanet.Tests/HashcashTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class HashcashTest
{
[Theory]
[ClassData(typeof(HashcashTestData))]
public void AnswerLessThanTarget(byte[] challenge, int difficulty)
public void AnswerLessThanTarget(byte[] challenge, long difficulty)
{
byte[] Stamp(Nonce nonce) => challenge.Concat(nonce.ToByteArray()).ToArray();
var answer = Hashcash.Answer(Stamp, difficulty);
Expand All @@ -24,10 +24,10 @@ public void TestBytesWithDifficulty()
{
Assert.True(LessThanTarget(new byte[1] { 0x80 }, 0));
Assert.False(LessThanTarget(new byte[1] { 0x80 }, 2));
int[] difficulties = Enumerable.Range(1, 8)
.Select(x => (int)Math.Pow(2, x)).ToArray();
long[] difficulties = Enumerable.Range(1, 8)
.Select(x => (long)Math.Pow(2, x)).ToArray();

foreach (int difficulty in difficulties)
foreach (long difficulty in difficulties)
{
Assert.True(LessThanTarget(new byte[2] { 0x00, 0x80 }, difficulty));
}
Expand All @@ -38,7 +38,7 @@ public void TestBytesWithDifficulty()
Assert.True(LessThanTarget(new byte[2] { 0x00, 0x20 }, 1024));
}

private bool LessThanTarget(byte[] bytes, int difficulty)
private bool LessThanTarget(byte[] bytes, long difficulty)
{
byte[] digest;
if (bytes.Length < HashDigest<SHA256>.Size)
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Tests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ internal static Block<T> MineNext<T>(
}

const long index = 1;
const int difficulty = 1;
const long difficulty = 1;
HashDigest<SHA256> previousHash = previousBlock.Hash;
DateTimeOffset timestamp = previousBlock.Timestamp.AddDays(1);
Address miner = previousBlock.Miner.Value;
Expand Down
2 changes: 1 addition & 1 deletion Libplanet/Blockchain/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ DateTimeOffset currentTime
{
string @namespace = Id.ToString();
long index = Store.CountIndex(@namespace);
int difficulty = Policy.GetNextBlockDifficulty(this);
long difficulty = Policy.GetNextBlockDifficulty(this);
HashDigest<SHA256>? prevHash = Store.IndexBlockHash(
@namespace,
index - 1
Expand Down
14 changes: 7 additions & 7 deletions Libplanet/Blockchain/Policies/BlockPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public InvalidBlockException ValidateNextBlock(
Block<T> nextBlock)
{
int index = blocks.Count;
int difficulty = GetNextBlockDifficulty(blocks);
long difficulty = GetNextBlockDifficulty(blocks);

Block<T> lastBlock = index >= 1 ? blocks[index - 1] : null;
HashDigest<SHA256>? prevHash = lastBlock?.Hash;
Expand Down Expand Up @@ -113,9 +113,9 @@ public InvalidBlockException ValidateNextBlock(
}

/// <inheritdoc />
public int GetNextBlockDifficulty(IReadOnlyList<Block<T>> blocks)
public long GetNextBlockDifficulty(IReadOnlyList<Block<T>> blocks)
{
const int minDifficulty = 1024;
const long minDifficulty = 1024;
const int difficultyBoundDivisor = 512;

int index = blocks.Count;
Expand All @@ -130,15 +130,15 @@ public int GetNextBlockDifficulty(IReadOnlyList<Block<T>> blocks)
DateTimeOffset prevPrevTimestamp = blocks[index - 2].Timestamp;
DateTimeOffset prevTimestamp = prevBlock.Timestamp;
TimeSpan timeDiff = prevTimestamp - prevPrevTimestamp;
int timeDiffMilliseconds = (int)timeDiff.TotalMilliseconds;
int multiplier = Math.Max(
long timeDiffMilliseconds = (long)timeDiff.TotalMilliseconds;
long multiplier = Math.Max(
1 - (timeDiffMilliseconds /
(int)BlockInterval.TotalMilliseconds),
(long)BlockInterval.TotalMilliseconds),
-99);

var prevDifficulty = prevBlock.Difficulty;
var offset = prevDifficulty / difficultyBoundDivisor;
int nextDifficulty = prevDifficulty + (offset * multiplier);
long nextDifficulty = prevDifficulty + (offset * multiplier);

return Math.Max(nextDifficulty, minDifficulty);
}
Expand Down
2 changes: 1 addition & 1 deletion Libplanet/Blockchain/Policies/IBlockPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ InvalidBlockException ValidateNextBlock(
/// followed by a new <see cref="Block{T}"/> to be mined.</param>
/// <returns>A right <see cref="Block{T}.Difficulty"/>
/// for a new <see cref="Block{T}"/> to be mined.</returns>
int GetNextBlockDifficulty(IReadOnlyList<Block<T>> blocks);
long GetNextBlockDifficulty(IReadOnlyList<Block<T>> blocks);
}
}
6 changes: 3 additions & 3 deletions Libplanet/Blocks/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public partial class Block<T> : ISerializable

public Block(
long index,
int difficulty,
long difficulty,
Nonce nonce,
Address? miner,
HashDigest<SHA256>? previousHash,
Expand Down Expand Up @@ -84,7 +84,7 @@ public HashDigest<SHA256> Hash
public long Index { get; }

[Uno.EqualityIgnore]
public int Difficulty { get; }
public long Difficulty { get; }

[Uno.EqualityIgnore]
public Nonce Nonce { get; }
Expand All @@ -103,7 +103,7 @@ public HashDigest<SHA256> Hash

public static Block<T> Mine(
long index,
int difficulty,
long difficulty,
Address miner,
HashDigest<SHA256>? previousHash,
DateTimeOffset timestamp,
Expand Down
6 changes: 3 additions & 3 deletions Libplanet/Blocks/RawBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public RawBlock(
string timestamp,
byte[] nonce,
byte[] miner,
int difficulty,
long difficulty,
byte[] previousHash,
IEnumerable transactions)
: this(
Expand All @@ -32,7 +32,7 @@ public RawBlock(
string timestamp,
byte[] nonce,
byte[] miner,
int difficulty,
long difficulty,
byte[] previousHash,
IEnumerable transactions,
byte[] hash)
Expand Down Expand Up @@ -72,7 +72,7 @@ internal RawBlock(SerializationInfo info, StreamingContext context)

public byte[] Miner { get; }

public int Difficulty { get; }
public long Difficulty { get; }

public byte[] PreviousHash { get; }

Expand Down
2 changes: 1 addition & 1 deletion Libplanet/HashDigest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static HashDigest<T> FromString(string hexDigest)
/// <c>true</c>.
/// </returns>
[Pure]
public bool LessThanTarget(int difficulty)
public bool LessThanTarget(long difficulty)
{
if (difficulty == 0)
{
Expand Down
6 changes: 3 additions & 3 deletions Libplanet/Hashcash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public static class Hashcash
/// should not vary for different <paramref name="nonce"/>s.</para>
/// </summary>
/// <param name="nonce">An arbitrary nonce for an attempt, provided
/// by <see cref="Hashcash.Answer(Stamp, int)"/> method.</param>
/// by <see cref="Hashcash.Answer(Stamp, long)"/> method.</param>
/// <returns>A <see cref="byte"/> array determined from the given
/// <paramref name="nonce"/>. It should return consistently
/// an equivalent array for equivalent <paramref name="nonce"/>
/// values.</returns>
/// <seealso cref="Hashcash.Answer(Stamp, int)"/>
/// <seealso cref="Hashcash.Answer(Stamp, long)"/>
/// <seealso cref="Nonce"/>
public delegate byte[] Stamp(Nonce nonce);

Expand All @@ -45,7 +45,7 @@ public static class Hashcash
/// <returns>A <see cref="Nonce"/> value which satisfies the given
/// <paramref name="difficulty"/>.</returns>
/// <seealso cref="Stamp"/>
public static Nonce Answer(Stamp stamp, int difficulty)
public static Nonce Answer(Stamp stamp, long difficulty)
{
var nonceBytes = new byte[10];
var random = new Random();
Expand Down

0 comments on commit 93c1c94

Please sign in to comment.