public Block TryGetLookahead(int count)
        {
            var chainedBlock = _Chain.GetBlock(_Location.Height + 1 + count);

            if (chainedBlock == null)
            {
                return(null);
            }
            var block = _DownloadedBlocks.TryGet(chainedBlock.HashBlock);

            if (block == null)
            {
                return(null);
            }
            return(block.Block);
        }
Example #2
0
        protected void Notify(ConcurrentDictionary <string, ConcurrentDictionary <IEventSubscription, bool> > map,
                              string key, string selector, object message, string channel = null)
        {
            var subs = map.TryGet(key);

            if (subs == null)
            {
                return;
            }

            var expired = new List <IEventSubscription>();
            var now     = DateTime.UtcNow;

            foreach (var sub in subs.KeysWithoutLock())
            {
                if (sub.HasChannel(channel))
                {
                    if (now - sub.LastPulseAt > IdleTimeout || sub.IsClosed)
                    {
                        if (Log.IsDebugEnabled)
                        {
                            Log.DebugFormat("[SSE-SERVER] Expired {0} Sub {1} on ({2})", selector, sub.SubscriptionId,
                                            string.Join(", ", sub.Channels));
                        }

                        expired.Add(sub);
                        continue;
                    }

                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("[SSE-SERVER] Sending {0} msg to {1} on ({2})", selector, sub.SubscriptionId,
                                        string.Join(", ", sub.Channels));
                    }

                    sub.Publish(selector, Serialize(message));
                }
            }

            foreach (var sub in expired)
            {
                sub.Unsubscribe();
            }
        }
        public Task StartAsync(CancellationToken cancellationToken)
        {
            _RunningTask = new TaskCompletionSource <bool>();
            _Cts         = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            leases.Add(_Aggregator.Subscribe <Events.NBXplorerStateChangedEvent>(async nbxplorerEvent =>
            {
                if (nbxplorerEvent.NewState == NBXplorerState.Ready)
                {
                    var wallet = _Wallets.GetWallet(nbxplorerEvent.Network);
                    if (_Wallets.IsAvailable(wallet.Network))
                    {
                        await Listen(wallet);
                    }
                }
            }));

            _ListenPoller = new Timer(async s =>
            {
                foreach (var wallet in _Wallets.GetWallets())
                {
                    if (_Wallets.IsAvailable(wallet.Network))
                    {
                        await Listen(wallet);
                    }
                }
            }, null, 0, (int)PollInterval.TotalMilliseconds);
            leases.Add(_ListenPoller);

            leases.Add(_Aggregator.Subscribe <Events.InvoiceEvent>(async inv =>
            {
                if (inv.Name == "invoice_created")
                {
                    var invoice = await _InvoiceRepository.GetInvoice(null, inv.InvoiceId);
                    await Task.WhenAll(invoice.GetSupportedPaymentMethod <DerivationStrategy>(_NetworkProvider)
                                       .Select(s => (Session: _SessionsByCryptoCode.TryGet(s.PaymentId.CryptoCode),
                                                     DerivationStrategy: s.DerivationStrategyBase))
                                       .Where(s => s.Session != null)
                                       .Select(s => s.Session.ListenDerivationSchemesAsync(new[] { s.DerivationStrategy }))
                                       .ToArray()).ConfigureAwait(false);
                }
            }));
            return(Task.CompletedTask);
        }
Example #4
0
        public object GetService(Type serviceType)
        {
            var output = default(object);

            try
            {
                if (serviceType.Equals(typeof(Container)))
                {
                    output = this;
                }
                else
                {
                    var builder = _builders.TryGet(serviceType);
                    output = builder.Build(this);
                }
            }
            catch (Exception ex)
            {
                throw new IocRetrievalException($"Could not resolve object: {serviceType.FullName}", ex);
            }

            return(output);
        }
Example #5
0
 Transaction FindTransaction(uint256 id)
 {
     return(_Builder.Mempool.TryGet(id) ?? _Transactions.TryGet(id) ?? _ReceivedTransactions.TryGet(id));
 }
Example #6
0
 public NBXplorerSummary GetSummary(string cryptoCode)
 {
     return(_summaries.TryGet(cryptoCode.ToUpperInvariant()));
 }
 public bool HasPermission(Type permType)
 {
     return(_permissions.TryGet(permType) != null);
 }
 public NBXplorerSummary GetSummary(string cryptoCode)
 {
     return(_summaries.TryGet(cryptoCode));
 }
Example #9
0
        private async Task UpdateRepository(RPCClient client, DerivationSchemeTrackedSource trackedSource, Repository repo, ScanTxoutOutput[] outputs, ScannedItems scannedItems, ScanUTXOProgress progressObj)
        {
            var clientBatch      = client.PrepareBatch();
            var blockIdsByHeight = new ConcurrentDictionary <int, uint256>();
            await Task.WhenAll(outputs.Select(async o =>
            {
                blockIdsByHeight.TryAdd(o.Height, await clientBatch.GetBlockHashAsync(o.Height));
            }).Concat(new[] { clientBatch.SendBatchAsync() }).ToArray());

            var data = outputs
                       .GroupBy(o => o.Coin.Outpoint.Hash)
                       .Select(o => (Coins: o.Select(c => c.Coin).ToList(),
                                     BlockId: blockIdsByHeight.TryGet(o.First().Height),
                                     TxId: o.Select(c => c.Coin.Outpoint.Hash).FirstOrDefault(),
                                     KeyPathInformations: o.Select(c => scannedItems.KeyPathInformations[c.Coin.ScriptPubKey]).ToList()))
                       .Where(o => o.BlockId != null)
                       .Select(o =>
            {
                foreach (var keyInfo in o.KeyPathInformations)
                {
                    var index   = keyInfo.KeyPath.Indexes.Last();
                    var highest = progressObj.HighestKeyIndexFound[keyInfo.Feature];
                    if (highest == null || index > highest.Value)
                    {
                        progressObj.HighestKeyIndexFound[keyInfo.Feature] = (int)index;
                    }
                }
                return(o);
            }).ToList();

            var blockHeadersByBlockId = new ConcurrentDictionary <uint256, BlockHeader>();

            clientBatch = client.PrepareBatch();
            var gettingBlockHeaders = Task.WhenAll(data.Select(async o =>
            {
                blockHeadersByBlockId.TryAdd(o.BlockId, await clientBatch.GetBlockHeaderAsync(o.BlockId));
            }).Concat(new[] { clientBatch.SendBatchAsync() }).ToArray());
            await repo.SaveKeyInformations(scannedItems.
                                           KeyPathInformations.
                                           Select(p => p.Value).
                                           Where(p =>
            {
                var highest = progressObj.HighestKeyIndexFound[p.Feature];
                if (highest == null)
                {
                    return(false);
                }
                return(p.KeyPath.Indexes.Last() <= highest.Value);
            }).ToArray());

            await repo.UpdateAddressPool(trackedSource, progressObj.HighestKeyIndexFound);

            await          gettingBlockHeaders;
            DateTimeOffset now = DateTimeOffset.UtcNow;
            await repo.SaveMatches(data.Select(o =>
            {
                var trackedTransaction = repo.CreateTrackedTransaction(trackedSource, new TrackedTransactionKey(o.TxId, o.BlockId, true), o.Coins, ToDictionary(o.KeyPathInformations));
                trackedTransaction.Inserted = now;
                trackedTransaction.FirstSeen = blockHeadersByBlockId.TryGetValue(o.BlockId, out var header) && header != null ? header.BlockTime : NBitcoin.Utils.UnixTimeToDateTime(0);
                return(trackedTransaction);
            }).ToArray());
        }
Example #10
0
 GetSetting(Guid siteGUID)
 {
     settings.TryGet(siteGuid);
     Lastrefreshed = DateTime.Now;
     //other code
 }
Example #11
0
			private Coin[] GetCoins(List<Tuple<Coin, string>> coins, ConcurrentDictionary<string, TrackedScript> trackedScripts, string wallet)
			{
				return coins.Select(c => new
									{
										Coin = c.Item1,
										TrackedScript = trackedScripts.TryGet(c.Item2)
									})
									.Where(o => o.TrackedScript != null)
									.Where(c => c.TrackedScript.Wallet.Equals(wallet, StringComparison.Ordinal))
									.Select(c => c.TrackedScript.RedeemScript != null ? c.Coin.ToScriptCoin(c.TrackedScript.RedeemScript) : c.Coin)
									.ToArray();
			}
Example #12
0
 public PacketHandler <TPacket> GetHandler(int opCode)
 {
     return(_handlers.TryGet(opCode));
 }