Show / Hide Table of Contents

Class BlockPolicy<T>

A default implementation of IBlockPolicy<T> interface.

Inheritance
Object
BlockPolicy<T>
Implements
IBlockPolicy<T>
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 Source

BlockPolicy(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, Int32>>, 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, int>? 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 null by default, which results in no additional execution other than those included in Transaction<T>s.

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 null by default.

Nullable<Func<BlockChain<T>, Block<T>, BlockPolicyViolationException>> validateNextBlock

The predicate that determines if a Block<T> follows the policy. Set to a constant function of null by default.

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, Int32>> getMaxBlockBytes

The function determining the maximum size of a Block<T> in number of bytes given its Index. Goes to GetMaxBlockBytes(Int64). Set to a constant size of 100KiB, i.e. 100 * 1024, by default.

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 0 by default.

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 100 by default.

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 Source

BlockAction

An IAction to execute and be rendered for every block, if any.

Declaration
public IAction BlockAction { get; }
Property Value
Type Description
IAction
| Improve this Doc View Source

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
| Improve this Doc View Source

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
IBlockExcerpt
TotalDifficultyComparer
| Improve this Doc View Source

DifficultyStability

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
| Improve this Doc View Source

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 Source

GetHashAlgorithm(Int64)

Gets the HashAlgorithmType to use for block's proof-of-work.

Declaration
public HashAlgorithmType GetHashAlgorithm(long index)
Parameters
Type Name Description
Int64 index

The Index of the Block<T> to do proof-of-work.

Returns
Type Description
HashAlgorithmType

The HashAlgorithmType to use.

| Improve this Doc View Source

GetMaxBlockBytes(Int64)

Gets the maximum length of a Block<T> in bytes.

Declaration
public int GetMaxBlockBytes(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 length of a Block<T> in bytes to accept.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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 nextBlock.

Block<T> nextBlock

The next block to append to blockChain.

Returns
Type Description
BlockPolicyViolationException

A BlockPolicyViolationException with a description as to why given nextBlock is invalid, or null if nextBlock is valid.

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>).

| Improve this Doc View Source

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.

Transaction<T> transaction

The Transaction<T> to consider.

Returns
Type Description
TxPolicyViolationException

A TxPolicyViolationException with a description as to why given transaction is invalid, or null if transaction is valid.

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>.
This is called separately from ValidateNextBlock(BlockChain<T>, Block<T>) from a BlockChain<T>.

Implements

IBlockPolicy<T>
  • Improve this Doc
  • View Source
In This Article
Back to top Copyright © 2018–2021 Planetarium