Class BlockPolicy<T>
A default implementation of IBlockPolicy<T> interface.
Implements
Namespace: Libplanet.Blockchain.Policies
Assembly: Libplanet.dll
Syntax
public class BlockPolicy<T> : object, IBlockPolicy<T> where T : IAction, new()
Type Parameters
Name | Description |
---|---|
T | An IAction type. It should match to Block<T>'s type parameter. |
Constructors
| Improve this Doc View SourceBlockPolicy(IAction, Nullable<TimeSpan>, Nullable<Int64>, Nullable<Int64>, Nullable<Func<BlockChain<T>, Transaction<T>, TxPolicyViolationException>>, Nullable<Func<BlockChain<T>, Block<T>, BlockPolicyViolationException>>, Nullable<IComparer<IBlockExcerpt>>, HashAlgorithmGetter, Nullable<Func<Int64, Int64>>, Nullable<Func<Int64, Int32>>, Nullable<Func<Int64, Int32>>, Nullable<Func<Int64, Int32>>)
Creates a default BlockPolicy<T> instance.
Each unprovided argument will be assigned a default value. See each parameter description for more detail.
Declaration
public BlockPolicy(IAction blockAction = null, TimeSpan? blockInterval = null, long? difficultyStability = null, long? minimumDifficulty = null, Func<BlockChain<T>, Transaction<T>, TxPolicyViolationException>? validateNextBlockTx = null, Func<BlockChain<T>, Block<T>, BlockPolicyViolationException>? validateNextBlock = null, IComparer<IBlockExcerpt>? canonicalChainComparer = null, HashAlgorithmGetter hashAlgorithmGetter = null, Func<long, long>? getMaxBlockBytes = null, Func<long, int>? getMinTransactionsPerBlock = null, Func<long, int>? getMaxTransactionsPerBlock = null, Func<long, int>? getMaxTransactionsPerSignerPerBlock = null)
Parameters
Type | Name | Description |
---|---|---|
IAction | blockAction | A IAction to executed for
every Block<T>. Set to |
Nullable<TimeSpan> | blockInterval | Goes to BlockInterval. Set to DefaultTargetBlockInterval by default. |
Nullable<Int64> | difficultyStability | Goes to DifficultyStability. Set to DefaultDifficultyStability by default. |
Nullable<Int64> | minimumDifficulty | Goes to MinimumDifficulty. Set to DefaultMinimumDifficulty by default. |
Nullable<Func<BlockChain<T>, Transaction<T>, TxPolicyViolationException>> | validateNextBlockTx | The predicate that determines if
a Transaction<T> follows the policy. Set to a constant function of
|
Nullable<Func<BlockChain<T>, Block<T>, BlockPolicyViolationException>> | validateNextBlock | The predicate that determines if a Block<T> follows the policy. Set to a default implementation where block's hash algorithm type, bytes count, and transactions count are validated. |
Nullable<IComparer<IBlockExcerpt>> | canonicalChainComparer | The custom rule to determine which is the canonical chain. If omitted, TotalDifficultyComparer is used by default. |
HashAlgorithmGetter | hashAlgorithmGetter | Configures GetHashAlgorithm(Int64). If omitted, SHA-256 is used for every block. |
Nullable<Func<Int64, Int64>> | getMaxBlockBytes | The function determining the maximum size of
a Block<T> in number of |
Nullable<Func<Int64, Int32>> | getMinTransactionsPerBlock | The function determining the minimum number of
Transaction<T>s that must be included in a Block<T>.
Goes to GetMinTransactionsPerBlock(Int64). Set to a constant function
of |
Nullable<Func<Int64, Int32>> | getMaxTransactionsPerBlock | The function determining how many
Transaction<T>s can be included in a Block<T>.
Goes to GetMaxTransactionsPerBlock(Int64). Set to a constant function
of |
Nullable<Func<Int64, Int32>> | getMaxTransactionsPerSignerPerBlock | The function determining the maximum number of transactions from the same signer that can be included in a Block<T> given the Block<T>'s index. Goes to GetMaxTransactionsPerSignerPerBlock(Int64). Set to GetMaxTransactionsPerBlock(Int64) by default. |
Properties
| Improve this Doc View SourceBlockAction
An IAction to execute and be rendered for every block, if any.
Declaration
public IAction BlockAction { get; }
Property Value
Type | Description |
---|---|
IAction |
BlockInterval
Targeted time interval between two consecutive Block<T>s. See the corresponding parameter description for BaseAlgorithm(BlockChain<T>, TimeSpan, Int64, Int64) for full detail.
Declaration
public TimeSpan BlockInterval { get; }
Property Value
Type | Description |
---|---|
TimeSpan |
CanonicalChainComparer
A comparer to determine which branch is the canonical chain (i.e., best chain). The most greater one according to this comparer is considered to be the canon.
Declaration
public IComparer<IBlockExcerpt> CanonicalChainComparer { get; }
Property Value
Type | Description |
---|---|
IComparer<IBlockExcerpt> |
See Also
| Improve this Doc View SourceDifficultyStability
Stability of a series of difficulties for a BlockChain<T>. See the corresponding parameter description for BaseAlgorithm(BlockChain<T>, TimeSpan, Int64, Int64) for full detail.
Declaration
public long DifficultyStability { get; }
Property Value
Type | Description |
---|---|
Int64 |
MinimumDifficulty
Minimum difficulty for a Block<T>. See the corresponding parameter description for BaseAlgorithm(BlockChain<T>, TimeSpan, Int64, Int64) for full detail.
Declaration
public long MinimumDifficulty { get; }
Property Value
Type | Description |
---|---|
Int64 |
Methods
| Improve this Doc View SourceGetHashAlgorithm(Int64)
Gets the HashAlgorithmType to use for block's proof-of-work.
Declaration
public HashAlgorithmType GetHashAlgorithm(long index)
Parameters
Type | Name | Description |
---|---|---|
Int64 | index |
Returns
Type | Description |
---|---|
HashAlgorithmType | The HashAlgorithmType to use. |
GetMaxBlockBytes(Int64)
Gets the maximum length of a Block<T> in bytes.
Declaration
public long GetMaxBlockBytes(long index)
Parameters
Type | Name | Description |
---|---|---|
Int64 | index | The Index of the Block<T> for which this constraint should apply. |
Returns
Type | Description |
---|---|
Int64 | The maximum length of a Block<T> in bytes to accept. |
GetMaxTransactionsPerBlock(Int64)
Gets the maximum number of Transaction<T>s allowed for a valid Block<T>.
Declaration
public int GetMaxTransactionsPerBlock(long index)
Parameters
Type | Name | Description |
---|---|---|
Int64 | index | The Index of the Block<T> for which this constraint should apply. |
Returns
Type | Description |
---|---|
Int32 | The maximum number of Transaction<T>s allowed for a valid Block<T> can accept. |
GetMaxTransactionsPerSignerPerBlock(Int64)
Gets the maximum number of Transaction<T>s allowed per signer for a valid Block<T>.
Declaration
public int GetMaxTransactionsPerSignerPerBlock(long index)
Parameters
Type | Name | Description |
---|---|---|
Int64 | index | The Index of the Block<T> for which this constraint should apply. |
Returns
Type | Description |
---|---|
Int32 | The maximum number of Transaction<T>s allowed per signer for a valid Block<T> can accept. |
GetMinTransactionsPerBlock(Int64)
Gets the minimum number of Transaction<T>s allowed for a valid Block<T>.
Declaration
public int GetMinTransactionsPerBlock(long index)
Parameters
Type | Name | Description |
---|---|---|
Int64 | index | The Index of the Block<T> for which this constraint should apply. |
Returns
Type | Description |
---|---|
Int32 | The minimum number of Transaction<T>s allowed for a valid Block<T> can accept. |
GetNextBlockDifficulty(BlockChain<T>)
Determines a right Difficulty
for a new Block<T> to be mined
right after the given blockChain
.
Declaration
public virtual long GetNextBlockDifficulty(BlockChain<T> blockChain)
Parameters
Type | Name | Description |
---|---|---|
BlockChain<T> | blockChain | Consecutive Block<T>s to be followed by a new Block<T> to be mined. |
Returns
Type | Description |
---|---|
Int64 | A right Difficulty for a new Block<T> to be mined. |
ValidateNextBlock(BlockChain<T>, Block<T>)
Checks if a Block<T> can be appended to the given BlockChain<T>.
Declaration
public virtual BlockPolicyViolationException ValidateNextBlock(BlockChain<T> blockChain, Block<T> nextBlock)
Parameters
Type | Name | Description |
---|---|---|
BlockChain<T> | blockChain | The target BlockChain<T> to
append |
Block<T> | nextBlock | The next block to append to
|
Returns
Type | Description |
---|---|
BlockPolicyViolationException | A BlockPolicyViolationException with a description
as to why given |
Remarks
Note that ValidateNextBlockTx(BlockChain<T>, Transaction<T>) will be called separately from a BlockChain<T> when appending a Block<T>. Hence, to reduce redundancy, an implementation of this interface should not call ValidateNextBlockTx(BlockChain<T>, Transaction<T>).
ValidateNextBlockTx(BlockChain<T>, Transaction<T>)
Checks if a Transaction<T> can be included in a yet to be mined Block<T> that can be appended to the given BlockChain<T>.
Declaration
public virtual TxPolicyViolationException ValidateNextBlockTx(BlockChain<T> blockChain, Transaction<T> transaction)
Parameters
Type | Name | Description |
---|---|---|
BlockChain<T> | blockChain | The target BlockChain<T> to include
given |
Transaction<T> | transaction | The Transaction<T> to consider. |
Returns
Type | Description |
---|---|
TxPolicyViolationException | A TxPolicyViolationException with a description
as to why given |
Remarks
This is used in two different cases:
- When selecting which Transaction<T> to include when mining a next Block<T>.
- When appending a Block<T>
with
transaction
to a BlockChain<T>.