예제 #1
0
        private static bool IsAllowedToMineRaw(
            Address miner,
            long index,
            IVariableSubPolicy <ImmutableHashSet <Address> > authorizedMinersPolicy,
            IVariableSubPolicy <ImmutableHashSet <Address> > permissionedMinersPolicy)
        {
            // For genesis blocks, any miner is allowed to mine.
            if (index == 0)
            {
                return(true);
            }
            else if (authorizedMinersPolicy.IsTargetIndex(index))
            {
                return(authorizedMinersPolicy.Getter(index).Contains(miner));
            }
            else if (permissionedMinersPolicy.IsTargetIndex(index))
            {
                return(permissionedMinersPolicy.Getter(index).Contains(miner));
            }

            // If none of the conditions apply, any miner is allowed to mine.
            return(true);
        }
예제 #2
0
        /// <summary>
        /// Gets a <see cref="BlockPolicy"/> constructed from given parameters.
        /// </summary>
        /// <param name="minimumDifficulty">The minimum difficulty that a <see cref="Block{T}"/>
        /// can have.  This is ignored for genesis blocks.</param>
        /// <param name="minTransactionsPerBlockPolicy">Used for minimum number of transactions
        /// required per block.</param>
        /// <param name="maxTransactionsPerBlockPolicy">The maximum number of
        /// <see cref="Transaction{T}"/>s that a <see cref="Block{T}"/> can have.</param>
        /// <param name="maxTransactionsPerSignerPerBlockPolicy">The maximum number of
        /// <see cref="Transaction{T}"/>s from a single miner that a <see cref="Block{T}"/>
        /// can have.</param>
        /// <param name="authorizedMinersPolicy">Used for authorized mining.</param>
        /// <param name="permissionedMinersPolicy">Used for permissioned mining.</param>
        /// <returns>A <see cref="BlockPolicy"/> constructed from given parameters.</returns>
        internal IBlockPolicy <NCAction> GetPolicy(
            long minimumDifficulty,
            IVariableSubPolicy <HashAlgorithmType> hashAlgorithmTypePolicy,
            IVariableSubPolicy <int> maxBlockBytesPolicy,
            IVariableSubPolicy <int> minTransactionsPerBlockPolicy,
            IVariableSubPolicy <int> maxTransactionsPerBlockPolicy,
            IVariableSubPolicy <int> maxTransactionsPerSignerPerBlockPolicy,
            IVariableSubPolicy <ImmutableHashSet <Address> > authorizedMinersPolicy,
            IVariableSubPolicy <ImmutableHashSet <Address> > permissionedMinersPolicy)
        {
#if UNITY_EDITOR
            return(new DebugPolicy());
#else
            hashAlgorithmTypePolicy = hashAlgorithmTypePolicy
                                      ?? HashAlgorithmTypePolicy.Default;
            maxBlockBytesPolicy = maxBlockBytesPolicy
                                  ?? MaxBlockBytesPolicy.Default;
            minTransactionsPerBlockPolicy = minTransactionsPerBlockPolicy
                                            ?? MinTransactionsPerBlockPolicy.Default;
            maxTransactionsPerBlockPolicy = maxTransactionsPerBlockPolicy
                                            ?? MaxTransactionsPerBlockPolicy.Default;
            maxTransactionsPerSignerPerBlockPolicy = maxTransactionsPerSignerPerBlockPolicy
                                                     ?? MaxTransactionsPerSignerPerBlockPolicy.Default;
            authorizedMinersPolicy = authorizedMinersPolicy
                                     ?? AuthorizedMinersPolicy.Default;
            permissionedMinersPolicy = permissionedMinersPolicy
                                       ?? PermissionedMinersPolicy.Default;

            // FIXME: Ad hoc solution to poorly defined tx validity.
            ImmutableHashSet <Address> allAuthorizedMiners =
                authorizedMinersPolicy.SpannedSubPolicies
                .Select(spannedSubPolicy => spannedSubPolicy.Value)
#pragma warning disable LAA1002
                .Aggregate(
                    authorizedMinersPolicy.DefaultValue,
                    (union, next) => union.Union(next));
#pragma warning restore LAA1002

            Func <BlockChain <NCAction>, Transaction <NCAction>, TxPolicyViolationException> validateNextBlockTx =
                (blockChain, transaction) => ValidateNextBlockTxRaw(
                    blockChain, transaction, allAuthorizedMiners);
            Func <BlockChain <NCAction>, Block <NCAction>, BlockPolicyViolationException> validateNextBlock =
                (blockChain, block) => ValidateNextBlockRaw(
                    blockChain,
                    block,
                    hashAlgorithmTypePolicy,
                    maxBlockBytesPolicy,
                    minTransactionsPerBlockPolicy,
                    maxTransactionsPerBlockPolicy,
                    maxTransactionsPerSignerPerBlockPolicy,
                    authorizedMinersPolicy,
                    permissionedMinersPolicy);
            Func <BlockChain <NCAction>, long> getNextBlockDifficulty = blockChain =>
                                                                        GetNextBlockDifficultyRaw(
                blockChain,
                BlockInterval,
                DifficultyStability,
                minimumDifficulty,
                authorizedMinersPolicy,
                defaultAlgorithm: chain => DifficultyAdjustment <NCAction> .BaseAlgorithm(
                    chain, BlockInterval, DifficultyStability, minimumDifficulty));
            Func <Address, long, bool> isAllowedToMine = (address, index) => IsAllowedToMineRaw(
                address,
                index,
                authorizedMinersPolicy,
                permissionedMinersPolicy);

            // FIXME: Slight inconsistency due to pre-existing delegate.
            HashAlgorithmGetter getHashAlgorithmType =
                index => hashAlgorithmTypePolicy.Getter(index);
            return(new BlockPolicy(
                       new RewardGold(),
                       blockInterval: BlockInterval,
                       difficultyStability: DifficultyStability,
                       minimumDifficulty: minimumDifficulty,
                       canonicalChainComparer: new TotalDifficultyComparer(),
                       hashAlgorithmGetter: getHashAlgorithmType,
                       validateNextBlockTx: validateNextBlockTx,
                       validateNextBlock: validateNextBlock,
                       getMaxBlockBytes: maxBlockBytesPolicy.Getter,
                       getMinTransactionsPerBlock: minTransactionsPerBlockPolicy.Getter,
                       getMaxTransactionsPerBlock: maxTransactionsPerBlockPolicy.Getter,
                       getMaxTransactionsPerSignerPerBlock: maxTransactionsPerSignerPerBlockPolicy.Getter,
                       getNextBlockDifficulty: getNextBlockDifficulty,
                       isAllowedToMine: isAllowedToMine));
#endif
        }