From 6248a1972f65740fd7ce4a7665db6df19d1be4c5 Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Tue, 23 Apr 2019 14:39:04 +0900 Subject: [PATCH] Change BlockPolicy interface to add difficulty params --- Libplanet/Blockchain/Policies/BlockPolicy.cs | 66 +++++++++++++++----- 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/Libplanet/Blockchain/Policies/BlockPolicy.cs b/Libplanet/Blockchain/Policies/BlockPolicy.cs index ed937b54003..a3e921c8092 100644 --- a/Libplanet/Blockchain/Policies/BlockPolicy.cs +++ b/Libplanet/Blockchain/Policies/BlockPolicy.cs @@ -16,36 +16,73 @@ public class BlockPolicy : IBlockPolicy { /// /// Creates a with configuring - /// in milliseconds. + /// in milliseconds, + /// and + /// . /// /// Configures /// in milliseconds. /// 5000 milliseconds by default. /// - public BlockPolicy(int blockIntervalMilliseconds = 5000) + /// Configures + /// 1024 by default. + /// Configures + /// 128 by default. + public BlockPolicy( + int blockIntervalMilliseconds = 5000, + long minimumDifficulty = 1024, + int difficultyBoundDivisor = 128) : this( - TimeSpan.FromMilliseconds(blockIntervalMilliseconds) - ) + TimeSpan.FromMilliseconds(blockIntervalMilliseconds), + minimumDifficulty, + difficultyBoundDivisor) { } /// /// Creates a with configuring - /// . + /// , and + /// . /// /// Configures . /// - public BlockPolicy(TimeSpan blockInterval) + /// Configures + /// . + /// Configures + /// . + public BlockPolicy( + TimeSpan blockInterval, + long minimumDifficulty, + int difficultyBoundDivisor) { if (blockInterval < TimeSpan.Zero) { throw new ArgumentOutOfRangeException( nameof(blockInterval), - "Interval between blocks cannot be negative." - ); + "Interval between blocks cannot be negative."); + } + + if (minimumDifficulty < 0) + { + throw new ArgumentOutOfRangeException( + nameof(minimumDifficulty), + "Minimum difficulty must be greater than 0"); + } + + if (minimumDifficulty <= difficultyBoundDivisor) + { + const string message = + "Difficulty bound divisor must be less than " + + "minimum difficulty"; + + throw new ArgumentOutOfRangeException( + nameof(difficultyBoundDivisor), + message); } BlockInterval = blockInterval; + MinimumDifficulty = minimumDifficulty; + DifficultyBoundDivisor = difficultyBoundDivisor; } /// @@ -58,6 +95,10 @@ public BlockPolicy(TimeSpan blockInterval) /// public TimeSpan BlockInterval { get; } + private long MinimumDifficulty { get; } + + private int DifficultyBoundDivisor { get; } + /// public InvalidBlockException ValidateNextBlock( IReadOnlyList> blocks, @@ -115,14 +156,11 @@ public InvalidBlockException ValidateNextBlock( /// public long GetNextBlockDifficulty(IReadOnlyList> blocks) { - const long minDifficulty = 1024; - const int difficultyBoundDivisor = 512; - int index = blocks.Count; if (index <= 1) { - return index == 0 ? 0 : minDifficulty; + return index == 0 ? 0 : MinimumDifficulty; } var prevBlock = blocks[index - 1]; @@ -137,10 +175,10 @@ public long GetNextBlockDifficulty(IReadOnlyList> blocks) -99); var prevDifficulty = prevBlock.Difficulty; - var offset = prevDifficulty / difficultyBoundDivisor; + var offset = prevDifficulty / DifficultyBoundDivisor; long nextDifficulty = prevDifficulty + (offset * multiplier); - return Math.Max(nextDifficulty, minDifficulty); + return Math.Max(nextDifficulty, MinimumDifficulty); } } }