コード例 #1
0
        private long GetTransactionSigOpCost(Transaction tx, CoinView inputs, ConsensusFlags flags)
        {
            long nSigOps = GetLegacySigOpCount(tx) * WITNESS_SCALE_FACTOR;

            if (tx.IsCoinBase)
            {
                return(nSigOps);
            }

            if (flags.ScriptFlags.HasFlag(ScriptVerify.P2SH))
            {
                nSigOps += GetP2SHSigOpCount(tx, inputs) * WITNESS_SCALE_FACTOR;
            }

            for (var i = 0; i < tx.Inputs.Count; i++)
            {
                var prevout = inputs.GetOutputFor(tx.Inputs[i]);
                nSigOps += CountWitnessSigOps(tx.Inputs[i].ScriptSig, prevout.ScriptPubKey, tx.Inputs[i].WitScript, flags);
            }
            return(nSigOps);
        }
コード例 #2
0
        private long WitnessSigOps(WitProgramParameters witParams, WitScript witScript, ConsensusFlags flags)
        {
            if (witParams.Version == 0)
            {
                if (witParams.Program.Length == 20)
                {
                    return(1);
                }

                if (witParams.Program.Length == 32 && witScript.PushCount > 0)
                {
                    Script subscript = Script.FromBytesUnsafe(witScript.GetUnsafePush(witScript.PushCount - 1));
                    return(subscript.GetSigOpCount(true));
                }
            }

            // Future flags may be implemented here.
            return(0);
        }
コード例 #3
0
        private long CountWitnessSigOps(Script scriptSig, Script scriptPubKey, WitScript witness, ConsensusFlags flags)
        {
            witness = witness ?? WitScript.Empty;
            if (!flags.ScriptFlags.HasFlag(ScriptVerify.Witness))
            {
                return(0);
            }
            var witParams = PayToWitTemplate.Instance.ExtractScriptPubKeyParameters2(scriptPubKey);

            if (witParams != null)
            {
                return(WitnessSigOps(witParams, witness, flags));
            }

            if (scriptPubKey.IsPayToScriptHash && scriptSig.IsPushOnly)
            {
                var data      = scriptSig.ToOps().Select(o => o.PushData).LastOrDefault() ?? new byte[0];
                var subScript = Script.FromBytesUnsafe(data);

                witParams = PayToWitTemplate.Instance.ExtractScriptPubKeyParameters2(scriptPubKey);
                if (witParams != null)
                {
                    return(WitnessSigOps(witParams, witness, flags));
                }
            }
            return(0);
        }
コード例 #4
0
        /// <summary>
        /// Pull blocks, validate them and update the UTXO Set
        /// </summary>
        /// <param name="utxo">UTXO Set</param>
        /// <param name="puller">Block source</param>
        /// <returns>Stream of validated blocks</returns>
        public IEnumerable <Block> Run(CoinViewStack utxoStack, BlockPuller puller)
        {
            var utxo            = utxoStack.Top;
            var cache           = utxoStack.Find <CacheCoinView>();
            var lookaheadPuller = puller as ILookaheadBlockPuller;

            puller.SetLocation(utxo.Tip);
            ThresholdConditionCache bip9 = new ThresholdConditionCache(_ConsensusParams);
            StopWatch watch    = new StopWatch();
            bool      rejected = false;

            while (true)
            {
                Block block = null;
                while (true)
                {
                    rejected = false;
                    try
                    {
                        using (watch.Start(o => PerformanceCounter.AddBlockFetchingTime(o)))
                        {
                            block = puller.NextBlock();
                        }
                        ChainedBlock       next;
                        ContextInformation context;
                        ConsensusFlags     flags;
                        using (watch.Start(o => PerformanceCounter.AddBlockProcessingTime(o)))
                        {
                            CheckBlockHeader(block.Header);
                            next    = new ChainedBlock(block.Header, block.Header.GetHash(), utxo.Tip);
                            context = new ContextInformation(next, this._ConsensusParams);
                            ContextualCheckBlockHeader(block.Header, context);
                            var states = bip9.GetStates(utxo.Tip);
                            flags = new ConsensusFlags(next, states, _ConsensusParams);
                            ContextualCheckBlock(block, flags, context);
                            CheckBlock(block);
                        }

                        var commitable = new CommitableCoinView(next, utxo);
                        using (watch.Start(o => PerformanceCounter.AddUTXOFetchingTime(o)))
                        {
                            commitable.FetchCoins(GetIdsToFetch(block, flags.EnforceBIP30));
                        }
                        commitable.SetInner(NullCoinView.Instance);

                        Task prefetching = GetPrefetchingTask(cache, lookaheadPuller, flags);
                        using (watch.Start(o => PerformanceCounter.AddBlockProcessingTime(o)))
                        {
                            ExecuteBlock(block, next, flags, commitable, null);
                        }
                        using (watch.Start(o => PerformanceCounter.AddUTXOFetchingTime(o)))
                        {
                            prefetching.Wait();
                            commitable.Commit(utxo);
                        }
                    }
                    catch (ConsensusErrorException ex)
                    {
                        rejected = true;
                        if (ex.ConsensusError == ConsensusErrors.TimeTooNew)
                        {
                            puller.Reject(block, RejectionMode.Temporary);
                        }
                        else
                        {
                            puller.Reject(block, RejectionMode.Permanent);
                        }
                    }
                    if (!rejected)
                    {
                        yield return(block);
                    }
                }
            }
        }
コード例 #5
0
        public void ExecuteBlock(Block block, ChainedBlock index, ConsensusFlags flags, CommitableCoinView view, TaskScheduler taskScheduler)
        {
            PerformanceCounter.AddProcessedBlocks(1);
            taskScheduler = taskScheduler ?? TaskScheduler.Default;
            if (flags.EnforceBIP30)
            {
                foreach (var tx in block.Transactions)
                {
                    var coins = view.AccessCoins(tx.GetHash());
                    if (coins != null && !coins.IsPrunable)
                    {
                        ConsensusErrors.BadTransactionBIP30.Throw();
                    }
                }
            }
            long  nSigOpsCost = 0;
            Money nFees       = Money.Zero;
            List <Task <bool> > checkInputs = new List <Task <bool> >();

            for (int i = 0; i < block.Transactions.Count; i++)
            {
                PerformanceCounter.AddProcessedTransactions(1);
                var tx = block.Transactions[i];
                if (!tx.IsCoinBase)
                {
                    int[] prevheights;

                    if (!view.HaveInputs(tx))
                    {
                        ConsensusErrors.BadTransactionMissingInput.Throw();
                    }

                    prevheights = new int[tx.Inputs.Count];
                    // Check that transaction is BIP68 final
                    // BIP68 lock checks (as opposed to nLockTime checks) must
                    // be in ConnectBlock because they require the UTXO set
                    for (var j = 0; j < tx.Inputs.Count; j++)
                    {
                        prevheights[j] = (int)view.AccessCoins(tx.Inputs[j].PrevOut.Hash).Height;
                    }

                    if (!tx.CheckSequenceLocks(prevheights, index, flags.LockTimeFlags))
                    {
                        ConsensusErrors.BadTransactionNonFinal.Throw();
                    }
                }
                // GetTransactionSigOpCost counts 3 types of sigops:
                // * legacy (always)
                // * p2sh (when P2SH enabled in flags and excludes coinbase)
                // * witness (when witness enabled in flags and excludes coinbase)
                nSigOpsCost += GetTransactionSigOpCost(tx, view, flags);
                if (nSigOpsCost > MAX_BLOCK_SIGOPS_COST)
                {
                    ConsensusErrors.BadBlockSigOps.Throw();
                }

                if (!tx.IsCoinBase)
                {
                    CheckIntputs(tx, view, index.Height);
                    nFees += view.GetValueIn(tx) - tx.TotalOut;
                    int ii      = i;
                    var localTx = tx;
                    PrecomputedTransactionData txData = new PrecomputedTransactionData(tx);
                    for (int iInput = 0; iInput < tx.Inputs.Count; iInput++)
                    {
                        PerformanceCounter.AddProcessedInputs(1);
                        var input      = tx.Inputs[iInput];
                        int iiIntput   = iInput;
                        var txout      = view.GetOutputFor(input);
                        var checkInput = new Task <bool>(() =>
                        {
                            if (UseConsensusLib)
                            {
                                Script.BitcoinConsensusError error;
                                return(Script.VerifyScriptConsensus(txout.ScriptPubKey, tx, (uint)iiIntput, flags.ScriptFlags, out error));
                            }
                            else
                            {
                                var checker      = new TransactionChecker(tx, iiIntput, txout.Value, txData);
                                var ctx          = new ScriptEvaluationContext();
                                ctx.ScriptVerify = flags.ScriptFlags;
                                return(ctx.VerifyScript(input.ScriptSig, txout.ScriptPubKey, checker));
                            }
                        });
                        checkInput.Start(taskScheduler);
                        checkInputs.Add(checkInput);
                    }
                }
                view.Update(tx, index.Height);
            }
            Money blockReward = nFees + GetBlockSubsidy(index.Height);

            if (block.Transactions[0].TotalOut > blockReward)
            {
                ConsensusErrors.BadCoinbaseAmount.Throw();
            }
            var passed = checkInputs.All(c => c.GetAwaiter().GetResult());

            if (!passed)
            {
                ConsensusErrors.BadTransactionScriptError.Throw();
            }
        }
コード例 #6
0
        public void ContextualCheckBlock(Block block, ConsensusFlags consensusFlags, ContextInformation context)
        {
            int nHeight = context.BestBlock == null ? 0 : context.BestBlock.Height + 1;

            // Start enforcing BIP113 (Median Time Past) using versionbits logic.
            var nLockTimeCutoff = consensusFlags.LockTimeFlags.HasFlag(LockTimeFlags.MedianTimePast) ?
                                  context.BestBlock.MedianTimePast :
                                  block.Header.BlockTime;

            // Check that all transactions are finalized
            foreach (var transaction in block.Transactions)
            {
                if (!transaction.IsFinal(nLockTimeCutoff, nHeight))
                {
                    ConsensusErrors.BadTransactionNonFinal.Throw();
                }
            }

            // Enforce rule that the coinbase starts with serialized block height
            if (consensusFlags.EnforceBIP34)
            {
                Script expect = new Script(Op.GetPushOp(nHeight));
                Script actual = block.Transactions[0].Inputs[0].ScriptSig;
                if (!StartWith(actual.ToBytes(true), expect.ToBytes(true)))
                {
                    ConsensusErrors.BadCoinbaseHeight.Throw();
                }
            }

            // Validation for witness commitments.
            // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
            //   coinbase (where 0x0000....0000 is used instead).
            // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness nonce (unconstrained).
            // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
            // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
            //   {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness nonce). In case there are
            //   multiple, the last one is used.
            bool fHaveWitness = false;

            if (consensusFlags.ScriptFlags.HasFlag(ScriptVerify.Witness))
            {
                int commitpos = GetWitnessCommitmentIndex(block);
                if (commitpos != -1)
                {
                    bool    malleated   = false;
                    uint256 hashWitness = BlockWitnessMerkleRoot(block, ref malleated);
                    // The malleation check is ignored; as the transaction tree itself
                    // already does not permit it, it is impossible to trigger in the
                    // witness tree.
                    var witness = block.Transactions[0].Inputs[0].WitScript;
                    if (witness.PushCount != 1 || witness.Pushes.First().Length != 32)
                    {
                        ConsensusErrors.BadWitnessNonceSize.Throw();
                    }

                    byte[] hashed = new byte[64];
                    Buffer.BlockCopy(hashWitness.ToBytes(), 0, hashed, 0, 32);
                    Buffer.BlockCopy(witness.Pushes.First(), 0, hashed, 32, 32);
                    hashWitness = Hashes.Hash256(hashed);
                    if (!EqualsArray(hashWitness.ToBytes(), block.Transactions[0].Outputs[commitpos].ScriptPubKey.ToBytes(true).Skip(6).ToArray(), 32))
                    {
                        ConsensusErrors.BadWitnessMerkleMatch.Throw();
                    }
                    fHaveWitness = true;
                }
            }

            if (!fHaveWitness)
            {
                for (var i = 0; i < block.Transactions.Count; i++)
                {
                    if (block.Transactions[i].HasWitness)
                    {
                        ConsensusErrors.UnexpectedWitness.Throw();
                    }
                }
            }

            // After the coinbase witness nonce and commitment are verified,
            // we can check if the block weight passes (before we've checked the
            // coinbase witness, it would be possible for the weight to be too
            // large by filling up the coinbase witness, which doesn't change
            // the block hash, so we couldn't mark the block as permanently
            // failed).
            if (GetBlockWeight(block) > MAX_BLOCK_WEIGHT)
            {
                ConsensusErrors.BadCoinbaseHeight.Throw();
            }
        }
コード例 #7
0
        private static Task GetPrefetchingTask(CacheCoinView cache, ILookaheadBlockPuller lookaheadPuller, ConsensusFlags flags)
        {
            Task prefetching = Task.FromResult <bool>(true);

            if (cache != null && lookaheadPuller != null)
            {
                var nextBlock = lookaheadPuller.TryGetLookahead(0);
                if (nextBlock != null)
                {
                    prefetching = Task.Run(() => cache.FetchCoins(GetIdsToFetch(nextBlock, flags.EnforceBIP30)));
                }
            }
            return(prefetching);
        }