Exemplo n.º 1
0
        /// <summary>
        /// Get transactions in Azure Table
        /// </summary>
        /// <param name="txIds"></param>
        /// <returns>All transactions (with null entries for unfound transactions)</returns>
        public async Task <TransactionEntry[]> GetTransactionsAsync(bool lazyLoadPreviousOutput, bool fetchColor, uint256[] txIds)
        {
            var result  = new TransactionEntry[txIds.Length];
            var queries = new TableQuery[txIds.Length];
            var tasks   = Enumerable.Range(0, txIds.Length)
                          .Select(i => new
            {
                TxId  = txIds[i],
                Index = i
            })
                          .GroupBy(o => o.TxId, o => o.Index)
                          .Select(async(o) =>
            {
                var transaction = await GetTransactionAsync(lazyLoadPreviousOutput, fetchColor, o.Key).ConfigureAwait(false);
                foreach (var index in o)
                {
                    result[index] = transaction;
                }
            }).ToArray();

            await Task.WhenAll(tasks).ConfigureAwait(false);

            return(result);
        }
Exemplo n.º 2
0
        public async Task <TransactionEntry> GetTransactionAsync(bool loadPreviousOutput, bool fetchColor, uint256 txId)
        {
            if (txId == null)
            {
                return(null);
            }
            TransactionEntry result = null;

            var table          = Configuration.GetTransactionTable();
            var searchedEntity = new TransactionEntry.Entity(txId);
            var query          = new TableQuery()
                                 .Where(
                TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, searchedEntity.PartitionKey),
                    TableOperators.And,
                    TableQuery.CombineFilters(
                        TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThan, txId.ToString() + "-"),
                        TableOperators.And,
                        TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThan, txId.ToString() + "|")
                        )
                    ));

            query.TakeCount = 10; //Should not have more
            List <TransactionEntry.Entity> entities = new List <TransactionEntry.Entity>();

            foreach (var e in await table.ExecuteQuerySegmentedAsync(query, null).ConfigureAwait(false))
            {
                if (e.IsFat())
                {
                    entities.Add(new TransactionEntry.Entity(await FetchFatEntity(e).ConfigureAwait(false)));
                }
                else
                {
                    entities.Add(new TransactionEntry.Entity(e));
                }
            }
            if (entities.Count == 0)
            {
                result = null;
            }
            else
            {
                result = new TransactionEntry(entities.ToArray());
                if (result.Transaction == null)
                {
                    foreach (var block in result.BlockIds.Select(id => GetBlock(id)).Where(b => b != null))
                    {
                        result.Transaction      = block.Transactions.FirstOrDefault(t => t.GetHash() == txId);
                        entities[0].Transaction = result.Transaction;
                        if (entities[0].Transaction != null)
                        {
                            await UpdateEntity(table, entities[0].CreateTableEntity()).ConfigureAwait(false);
                        }
                        break;
                    }
                }

                if (fetchColor && result.ColoredTransaction == null)
                {
                    result.ColoredTransaction = await ColoredTransaction.FetchColorsAsync(txId, result.Transaction, new CachedColoredTransactionRepository(new IndexerColoredTransactionRepository(Configuration))).ConfigureAwait(false);

                    entities[0].ColoredTransaction = result.ColoredTransaction;
                    if (entities[0].ColoredTransaction != null)
                    {
                        await UpdateEntity(table, entities[0].CreateTableEntity()).ConfigureAwait(false);
                    }
                }
                var needTxOut = result.SpentCoins == null && loadPreviousOutput && result.Transaction != null;
                if (needTxOut)
                {
                    var inputs  = result.Transaction.Inputs.Select(o => o.PrevOut).ToArray();
                    var parents = await
                                  GetTransactionsAsync(false, false, inputs
                                                       .Select(i => i.Hash)
                                                       .ToArray()).ConfigureAwait(false);

                    for (int i = 0; i < parents.Length; i++)
                    {
                        if (parents[i] == null)
                        {
                            IndexerTrace.MissingTransactionFromDatabase(result.Transaction.Inputs[i].PrevOut.Hash);
                            return(null);
                        }
                    }

                    var outputs = parents.Select((p, i) => p.Transaction.Outputs[inputs[i].N]).ToArray();

                    result.SpentCoins = Enumerable
                                        .Range(0, inputs.Length)
                                        .Select(i => new Spendable(inputs[i], outputs[i]))
                                        .ToList();
                    entities[0].PreviousTxOuts.Clear();
                    entities[0].PreviousTxOuts.AddRange(outputs);
                    if (entities[0].IsLoaded)
                    {
                        await UpdateEntity(table, entities[0].CreateTableEntity()).ConfigureAwait(false);
                    }
                }
            }
            return(result != null && result.Transaction != null ? result : null);
        }