public async Task <bool> EnsurePreviousLoadedAsync(OrderedBalanceChange change) { if (!NeedLoading(change)) { return(true); } var parentIds = change.SpentOutpoints.Select(s => s.Hash).ToArray(); var parents = await GetTransactionsAsync(false, ColoredBalance, parentIds).ConfigureAwait(false); if (change.SpentCoins == null) { var success = await change.EnsureSpentCoinsLoadedAsync(parentIds, parents.Select(t => t == null ? null : t.Transaction).ToArray()).ConfigureAwait(false); if (!success) { return(false); } } if (ColoredBalance && change.ColoredTransaction == null) { var success = await change.EnsureColoredTransactionLoadedAsync(new IndexerColoredTransactionRepository(Configuration)).ConfigureAwait(false); if (!success) { return(false); } } var entity = change.ToEntity(); await Configuration.GetBalanceTable().ExecuteAsync(TableOperation.Merge(entity)).ConfigureAwait(false); return(true); }
public async Task <bool> EnsurePreviousLoadedAsync(OrderedBalanceChange change) { if (!NeedLoading(change)) { return(true); } var parentIds = change.SpentOutpoints.Select(s => s.Hash).ToArray(); var parents = await GetTransactionsAsync(false, ColoredBalance, parentIds).ConfigureAwait(false); var cache = new NoSqlTransactionRepository(); foreach (var parent in parents.Where(p => p != null)) { cache.Put(parent.TransactionId, parent.Transaction); } if (change.SpentCoins == null) { var success = await change.EnsureSpentCoinsLoadedAsync(cache).ConfigureAwait(false); if (!success) { return(false); } } if (ColoredBalance && change.ColoredTransaction == null) { var indexerRepo = new IndexerColoredTransactionRepository(Configuration); indexerRepo.Transactions = new CompositeTransactionRepository(new[] { new ReadOnlyTransactionRepository(cache), indexerRepo.Transactions }); var success = await change.EnsureColoredTransactionLoadedAsync(indexerRepo).ConfigureAwait(false); if (!success) { return(false); } } var entity = change.ToEntity(ConsensusFactory); if (!change.IsEmpty) { await Configuration.GetBalanceTable().ExecuteAsync(TableOperation.Merge(entity)).ConfigureAwait(false); } else { try { await Configuration.GetTransactionTable().ExecuteAsync(TableOperation.Delete(entity)).ConfigureAwait(false); } catch (StorageException ex) { if (ex.RequestInformation == null || ex.RequestInformation.HttpStatusCode != 404) { throw; } } } return(true); }
public async Task <bool> EnsurePreviousLoadedAsync(OrderedBalanceChange change) { if (!NeedLoading(change)) { return(true); } var transactions = await GetTransactionsAsync(false, ColoredBalance, change.SpentOutpoints.Select(s => s.Hash).ToArray()).ConfigureAwait(false); CoinCollection result = new CoinCollection(); for (int i = 0; i < transactions.Length; i++) { var outpoint = change.SpentOutpoints[i]; if (outpoint.IsNull) { continue; } var prev = transactions[i]; if (prev == null) { return(false); } if (ColoredBalance && prev.ColoredTransaction == null) { return(false); } result.Add(new Coin(outpoint, prev.Transaction.Outputs[change.SpentOutpoints[i].N])); } change.SpentCoins = result; if (ColoredBalance && change.ColoredBalanceChangeEntry == null) { var thisTransaction = await GetTransactionAsync(false, ColoredBalance, change.TransactionId).ConfigureAwait(false); if (thisTransaction.ColoredTransaction == null) { return(false); } change.ColoredBalanceChangeEntry = new ColoredBalanceChangeEntry(change, thisTransaction.ColoredTransaction); } var entity = change.ToEntity(); var spentCoins = Helper.GetEntityProperty(entity, "b"); var coloredTx = ColoredBalance ? entity.Properties["g"].BinaryValue : null; entity.Properties.Clear(); if (coloredTx != null) { entity.Properties.Add("g", new EntityProperty(coloredTx)); } Helper.SetEntityProperty(entity, "b", spentCoins); Configuration.GetBalanceTable().Execute(TableOperation.Merge(entity)); change.AddRedeemInfo(); return(true); }
public async Task<bool> EnsurePreviousLoadedAsync(OrderedBalanceChange change) { if(!NeedLoading(change)) return true; var parentIds = change.SpentOutpoints.Select(s => s.Hash).ToArray(); var parents = await GetTransactionsAsync(false, ColoredBalance, parentIds).ConfigureAwait(false); var cache = new NoSqlTransactionRepository(); foreach(var parent in parents.Where(p => p != null)) cache.Put(parent.TransactionId, parent.Transaction); if(change.SpentCoins == null) { var success = await change.EnsureSpentCoinsLoadedAsync(cache).ConfigureAwait(false); if(!success) return false; } if(ColoredBalance && change.ColoredTransaction == null) { var indexerRepo = new IndexerColoredTransactionRepository(Configuration); indexerRepo.Transactions = new CompositeTransactionRepository(new[] { new ReadOnlyTransactionRepository(cache), indexerRepo.Transactions }); var success = await change.EnsureColoredTransactionLoadedAsync(indexerRepo).ConfigureAwait(false); if(!success) return false; } var entity = change.ToEntity(); if(!change.IsEmpty) { await Configuration.GetBalanceTable().ExecuteAsync(TableOperation.Merge(entity)).ConfigureAwait(false); } else { try { await Configuration.GetTransactionTable().ExecuteAsync(TableOperation.Delete(entity)).ConfigureAwait(false); } catch(StorageException ex) { if(ex.RequestInformation == null || ex.RequestInformation.HttpStatusCode != 404) throw; } } return true; }