当前位置: 首页>>代码示例>>C#>>正文


C# ImmutableDictionary.ToBuilder方法代码示例

本文整理汇总了C#中ImmutableDictionary.ToBuilder方法的典型用法代码示例。如果您正苦于以下问题:C# ImmutableDictionary.ToBuilder方法的具体用法?C# ImmutableDictionary.ToBuilder怎么用?C# ImmutableDictionary.ToBuilder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ImmutableDictionary的用法示例。


在下文中一共展示了ImmutableDictionary.ToBuilder方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CalculateUtxo

        private ImmutableDictionary<UInt256, UnspentTx> CalculateUtxo(long blockHeight, Block block, ImmutableDictionary<UInt256, UnspentTx> currentUtxo, out ImmutableDictionary<UInt256, ImmutableHashSet<int>> newTransactions, out long txCount, out long inputCount)
        {
            txCount = 0;
            inputCount = 0;

            // create builder for new utxo
            var newUtxoBuilder = currentUtxo.ToBuilder();
            var newTransactionsBuilder = ImmutableDictionary.CreateBuilder<UInt256, ImmutableHashSet<int>>();

            // don't include genesis block coinbase in utxo
            if (blockHeight > 0)
            {
                //TODO apply real coinbase rule
                // https://github.com/bitcoin/bitcoin/blob/481d89979457d69da07edd99fba451fd42a47f5c/src/core.h#L219
                var coinbaseTx = block.Transactions[0];

                // add the coinbase outputs to the utxo
                var coinbaseUnspentTx = new UnspentTx(block.Hash, 0, coinbaseTx.Hash, new ImmutableBitArray(coinbaseTx.Outputs.Length, true));

                // add transaction output to to the utxo
                if (newUtxoBuilder.ContainsKey(coinbaseTx.Hash))
                {
                    // duplicate transaction output
                    Debug.WriteLine("Duplicate transaction at block {0:#,##0}, {1}, coinbase".Format2(blockHeight, block.Hash.ToHexNumberString()));

                    if ((blockHeight == 91842 && coinbaseTx.Hash == UInt256.Parse("d5d27987d2a3dfc724e359870c6644b40e497bdc0589a033220fe15429d88599", NumberStyles.HexNumber))
                        || (blockHeight == 91880 && coinbaseTx.Hash == UInt256.Parse("e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468", NumberStyles.HexNumber)))
                    {
                        newUtxoBuilder.Remove(coinbaseTx.Hash);
                    }
                    else
                    {
                        throw new ValidationException();
                    }
                }

                newTransactionsBuilder.Add(coinbaseTx.Hash, ImmutableHashSet.Create(0));
                newUtxoBuilder.Add(coinbaseTx.Hash, coinbaseUnspentTx);
            }

            // check for double spends
            for (var txIndex = 1; txIndex < block.Transactions.Length; txIndex++)
            {
                var tx = block.Transactions[txIndex];
                txCount++;

                for (var inputIndex = 0; inputIndex < tx.Inputs.Length; inputIndex++)
                {
                    var input = tx.Inputs[inputIndex];
                    inputCount++;

                    if (!newUtxoBuilder.ContainsKey(input.PreviousTxOutputKey.TxHash))
                    {
                        // output wasn't present in utxo, invalid block
                        throw new ValidationException();
                    }

                    var prevUnspentTx = newUtxoBuilder[input.PreviousTxOutputKey.TxHash];

                    if (input.PreviousTxOutputKey.TxOutputIndex >= prevUnspentTx.UnspentOutputs.Length)
                    {
                        // output was out of bounds
                        throw new ValidationException();
                    }

                    if (!prevUnspentTx.UnspentOutputs[input.PreviousTxOutputKey.TxOutputIndex.ToIntChecked()])
                    {                        // output was already spent
                        throw new ValidationException();
                    }


                    // remove the output from the utxo
                    newUtxoBuilder[input.PreviousTxOutputKey.TxHash] =
                        new UnspentTx(prevUnspentTx.BlockHash, prevUnspentTx.TxIndex, prevUnspentTx.TxHash,
                            prevUnspentTx.UnspentOutputs.Set(input.PreviousTxOutputKey.TxOutputIndex.ToIntChecked(), false));

                    // remove fully spent transaction from the utxo
                    if (newUtxoBuilder[input.PreviousTxOutputKey.TxHash].UnspentOutputs.All(x => !x))
                        newUtxoBuilder.Remove(input.PreviousTxOutputKey.TxHash);
                }

                // add the output to the list to be added to the utxo
                var unspentTx = new UnspentTx(block.Hash, (UInt32)txIndex, tx.Hash, new ImmutableBitArray(tx.Outputs.Length, true));

                // add transaction output to to the utxo
                if (newUtxoBuilder.ContainsKey(tx.Hash))
                {
                    // duplicate transaction output
                    Debug.WriteLine("Duplicate transaction at block {0:#,##0}, {1}, tx {2}".Format2(blockHeight, block.Hash.ToHexNumberString(), txIndex));
                    //Debugger.Break();
                    //TODO throw new Validation();

                    //TODO this needs to be tracked so that blocks can be rolled back accurately
                    //TODO track these separately on the blockchain info? gonna be costly to track on every transaction
                }

                if (!newTransactionsBuilder.ContainsKey(tx.Hash))
                    newTransactionsBuilder.Add(tx.Hash, ImmutableHashSet.Create(txIndex));
                else
                    newTransactionsBuilder[tx.Hash] = newTransactionsBuilder[tx.Hash].Add(txIndex);
//.........这里部分代码省略.........
开发者ID:knocte,项目名称:BitSharp,代码行数:101,代码来源:BlockchainCalculator.cs


注:本文中的ImmutableDictionary.ToBuilder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。