forked from planetarium/libplanet
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request planetarium#82 from earlbread/fix-block-policy
Use the minimum difficulty in blocks for authorized miners
- Loading branch information
Showing
3 changed files
with
229 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,132 @@ | ||
using Lib9c; | ||
using Libplanet; | ||
using Libplanet.Action; | ||
using Libplanet.Blockchain; | ||
using Libplanet.Blockchain.Policies; | ||
using Libplanet.Blocks; | ||
using Libplanet.Tx; | ||
using Nekoyume.Action; | ||
using System; | ||
using Lib9c; | ||
using Libplanet; | ||
using Nekoyume.Model.State; | ||
using NCAction = Libplanet.Action.PolymorphicAction<Nekoyume.Action.ActionBase>; | ||
|
||
namespace Nekoyume.BlockChain | ||
{ | ||
public class BlockPolicy : IBlockPolicy<NCAction> | ||
public class BlockPolicy : BlockPolicy<NCAction> | ||
{ | ||
private readonly IBlockPolicy<NCAction> _impl; | ||
|
||
private readonly Func<Block<NCAction>, InvalidBlockException> _blockValidator; | ||
private readonly long _minimumDifficulty; | ||
private readonly long _difficultyBoundDivisor; | ||
|
||
public BlockPolicy( | ||
IBlockPolicy<NCAction> impl, | ||
Func<Block<NCAction>, InvalidBlockException> blockValidator | ||
) | ||
IAction blockAction, | ||
TimeSpan blockInterval, | ||
long minimumDifficulty, | ||
int difficultyBoundDivisor, | ||
Func<Transaction<NCAction>, BlockChain<NCAction>, bool> doesTransactionFollowPolicy = null | ||
) : base( | ||
blockAction, | ||
blockInterval, | ||
minimumDifficulty, | ||
difficultyBoundDivisor, | ||
doesTransactionFollowPolicy) | ||
{ | ||
_impl = impl; | ||
_blockValidator = blockValidator; | ||
_minimumDifficulty = minimumDifficulty; | ||
_difficultyBoundDivisor = difficultyBoundDivisor; | ||
} | ||
|
||
public IAction BlockAction => _impl.BlockAction; | ||
public AuthorizedMinersState AuthorizedMinersState { get; set; } | ||
|
||
public bool DoesTransactionFollowsPolicy( | ||
Transaction<NCAction> transaction, | ||
BlockChain<NCAction> blockChain | ||
) => _impl.DoesTransactionFollowsPolicy(transaction, blockChain); | ||
public override InvalidBlockException ValidateNextBlock(BlockChain<NCAction> blocks, Block<NCAction> nextBlock) | ||
{ | ||
InvalidBlockException e = ValidateMinerAuthority(nextBlock); | ||
return e ?? base.ValidateNextBlock(blocks, nextBlock); | ||
} | ||
|
||
public long GetNextBlockDifficulty(BlockChain<NCAction> blocks) | ||
=> _impl.GetNextBlockDifficulty(blocks); | ||
public override long GetNextBlockDifficulty(BlockChain<NCAction> blocks) | ||
{ | ||
if (AuthorizedMinersState is null) | ||
{ | ||
return base.GetNextBlockDifficulty(blocks); | ||
} | ||
|
||
public InvalidBlockException ValidateNextBlock(BlockChain<NCAction> blocks, Block<NCAction> nextBlock) | ||
long index = blocks.Count; | ||
|
||
if (index < 0) | ||
{ | ||
throw new InvalidBlockIndexException( | ||
$"index must be 0 or more, but its index is {index}."); | ||
} | ||
|
||
if (index <= 1) | ||
{ | ||
return index == 0 ? 0 : _minimumDifficulty; | ||
} | ||
|
||
var prevIndex = IsTargetBlock(index - 1) ? index - 2 : index - 1; | ||
var beforePrevIndex = IsTargetBlock(prevIndex - 1) ? prevIndex - 2 : prevIndex - 1; | ||
|
||
if (beforePrevIndex > AuthorizedMinersState.ValidUntil) | ||
{ | ||
return base.GetNextBlockDifficulty(blocks); | ||
} | ||
|
||
if (IsTargetBlock(index) || prevIndex <= 1 || beforePrevIndex <= 1) | ||
{ | ||
return _minimumDifficulty; | ||
} | ||
|
||
var prevBlock = blocks[prevIndex]; | ||
var beforePrevBlock = blocks[beforePrevIndex]; | ||
|
||
DateTimeOffset beforePrevTimestamp = beforePrevBlock.Timestamp; | ||
DateTimeOffset prevTimestamp = prevBlock.Timestamp; | ||
TimeSpan timeDiff = prevTimestamp - beforePrevTimestamp; | ||
long timeDiffMilliseconds = (long)timeDiff.TotalMilliseconds; | ||
const long minimumMultiplier = -99; | ||
long multiplier = 1 - timeDiffMilliseconds / (long)BlockInterval.TotalMilliseconds; | ||
multiplier = Math.Max(multiplier, minimumMultiplier); | ||
|
||
var prevDifficulty = prevBlock.Difficulty; | ||
var offset = prevDifficulty / _difficultyBoundDivisor; | ||
long nextDifficulty = prevDifficulty + (offset * multiplier); | ||
|
||
return Math.Max(nextDifficulty, _minimumDifficulty); | ||
} | ||
|
||
private InvalidBlockException ValidateMinerAuthority(Block<NCAction> block) | ||
{ | ||
InvalidBlockException excFromValidator = _blockValidator(nextBlock); | ||
if (!(excFromValidator is null)) | ||
if (AuthorizedMinersState is null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (!(block.Miner is Address miner)) | ||
{ | ||
return excFromValidator; | ||
return null; | ||
} | ||
|
||
return _impl.ValidateNextBlock(blocks, nextBlock); | ||
if (!IsTargetBlock(block.Index)) | ||
{ | ||
return null; | ||
} | ||
|
||
bool minedByAuthorities = AuthorizedMinersState.Miners.Contains(miner); | ||
|
||
if (minedByAuthorities) | ||
{ | ||
return null; | ||
} | ||
|
||
return new InvalidMinerException( | ||
$"The given block[{block}] isn't mined by authorities.", | ||
miner | ||
); | ||
} | ||
|
||
private bool IsTargetBlock(long blockIndex) | ||
{ | ||
return blockIndex > 0 | ||
&& blockIndex <= AuthorizedMinersState.ValidUntil | ||
&& blockIndex % AuthorizedMinersState.Interval == 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters