Example #1
0
        private static TransformManyBlock<DecodedBlockTx, Tuple<TxOutputKey, CompletionCount, DecodedBlockTx>> InitQueueUnspentTxLookup(CancellationToken cancelToken)
        {
            return new TransformManyBlock<DecodedBlockTx, Tuple<TxOutputKey, CompletionCount, DecodedBlockTx>>(
                blockTx =>
                {
                    var tx = blockTx.Transaction;

                    var outputCount = tx.Outputs.Length;
                    var inputCount = !blockTx.IsCoinbase ? tx.Inputs.Length * 2 : 0;

                    var txOutputKeys = new Tuple<TxOutputKey, CompletionCount, DecodedBlockTx>[1 + outputCount + inputCount];
                    var completionCount = new CompletionCount(txOutputKeys.Length);
                    var keyIndex = 0;

                    // warm-up the UnspentTx entry that will be added for the new tx
                    txOutputKeys[keyIndex++] = Tuple.Create(new TxOutputKey(blockTx.Hash, uint.MaxValue), completionCount, blockTx);

                    // warm-up the TxOutput entries that will be added for each of the tx's outputs
                    for (var outputIndex = 0; outputIndex < tx.Outputs.Length; outputIndex++)
                    {
                        var txOutputKey = new TxOutputKey(blockTx.Hash, (uint)outputIndex);
                        txOutputKeys[keyIndex++] = Tuple.Create(txOutputKey, completionCount, blockTx);
                    }

                    // warm-up the previous UnspentTx and TxOutput entries that will be needed for each of the tx's inputs
                    if (!blockTx.IsCoinbase)
                    {
                        for (var inputIndex = 0; inputIndex < tx.Inputs.Length; inputIndex++)
                        {
                            var input = tx.Inputs[inputIndex];

                            // input's previous tx's UnspentTx entry
                            var txOutputKey = new TxOutputKey(input.PrevTxHash, uint.MaxValue);
                            txOutputKeys[keyIndex++] = Tuple.Create(txOutputKey, completionCount, blockTx);

                            // input's previous tx outputs's TxOutput entry
                            txOutputKey = input.PrevTxOutputKey;
                            txOutputKeys[keyIndex++] = Tuple.Create(txOutputKey, completionCount, blockTx);
                        }
                    }

                    Debug.Assert(txOutputKeys.All(x => x != null));

                    return txOutputKeys;
                },
                new ExecutionDataflowBlockOptions { CancellationToken = cancelToken });
        }