public async Task Work(ReturnOutputMessage message)
        {
            var transaction = new Transaction(message.TransactionHex);

            foreach (var address in message.Addresses)
            {
                var unspentOutputs = (await _bitcoinOutputsService.GetOnlyNinjaOutputs(address, 0)).ToList();
                foreach (var input in transaction.Inputs)
                {
                    var coin = unspentOutputs.FirstOrDefault(o => o.Outpoint == input.PrevOut);
                    if (coin != null)
                    {
                        if (address != _settings.FeeAddress)
                        {
                            if (!await _broadcastedOutputRepository.OutputExists(coin.Outpoint.Hash.ToString(), (int)coin.Outpoint.N))
                            {
                                await _broadcastedOutputRepository.InsertOutputs(new List <IBroadcastedOutput>()
                                {
                                    new BroadcastedOutput(coin, coin.Outpoint.Hash.ToString(), _connectionParams.Network)
                                });
                            }
                        }
                        else
                        {
                            if (coin is Coin uncolored)
                            {
                                await _pregeneratedQueue.EnqueueOutputs(uncolored);
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        private async Task RefreshOutputs(string type, string assetId)
        {
            Console.WriteLine($"Start process: type={type}" + (assetId != null ? $", asset={assetId}" : null));

            IPregeneratedOutputsQueue queue;
            string address;

            if (type == "fee")
            {
                queue   = _pregeneratedOutputsQueueFactory.CreateFeeQueue();
                address = _feeAddress;
            }
            else
            {
                var asset = await _assetRepository.GetAssetById(assetId);

                queue   = _pregeneratedOutputsQueueFactory.Create(asset.BlockChainAssetId);
                address = asset.AssetAddress;
                Console.WriteLine("BlockchainAssetId : " + asset.BlockChainAssetId);
            }

            var count = await queue.Count();

            Console.WriteLine($"Start collect {count} outputs from queue");

            var set = new HashSet <OutPoint>();

            while (count-- > 0)
            {
                Coin coin = null;
                try
                {
                    coin = await queue.DequeueCoin();
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (coin != null && !set.Contains(coin.Outpoint))
                    {
                        set.Add(coin.Outpoint);
                        await queue.EnqueueOutputs(coin);
                    }
                }
            }

            Console.WriteLine($"Coins collected");

            var coins = (await _bitcoinOutputsService.GetOnlyNinjaOutputs(address, 1, int.MaxValue)).OfType <Coin>().ToArray();

            Console.WriteLine($"Received {coins.Length} outputs from qbitninja");

            coins = coins.Where(x => !set.Contains(x.Outpoint)).ToArray();

            Console.WriteLine($"Got {coins.Length} missing outputs");

            await queue.EnqueueOutputs(coins);

            //Console.WriteLine("Start remove outputs from queue");
            //int i = 0;
            //while (true)
            //    try
            //    {
            //        await queue.DequeueCoin();
            //        i++;
            //    }
            //    catch (BackendException)
            //    {
            //        break;
            //    }
            //Console.WriteLine($"Removed {i} coins from queue");
            //var coins = (await _bitcoinOutputsService.GetUncoloredUnspentOutputs(address)).OfType<Coin>().ToArray();

            //Console.WriteLine($"Received {coins.Length} outputs from qbitninja");
            //Console.WriteLine("Start add coins to queue");

            //await queue.EnqueueOutputs(coins);

            Console.WriteLine("All coins successfuly added to queue");
            Console.WriteLine(Environment.NewLine);
        }