static async Task <NewTransactionEvent> WaitTransaction(LongPollingNotificationSession evts, DerivationStrategyBase derivationStrategy) { while (true) { var evt = await evts.NextEventAsync(); if (evt is NBXplorer.Models.NewTransactionEvent tx) { if (tx.DerivationStrategy == derivationStrategy) { return(tx); } } } }
public static void WaitForTransaction(this LongPollingNotificationSession session, TrackedSource trackedSource, uint256 txId) { using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))) { while (true) { if (session.NextEvent(cts.Token) is NewTransactionEvent evts) { if (evts.TrackedSource == trackedSource && evts.TransactionData.TransactionHash == txId) { break; } } } } }
public static void WaitForBlocks(this LongPollingNotificationSession session, params uint256[] txIds) { if (txIds == null || txIds.Length == 0) { return; } HashSet <uint256> txidsSet = new HashSet <uint256>(txIds); using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(20))) { while (true) { if (session.NextEvent(cts.Token) is NewBlockEvent evts) { txidsSet.Remove(evts.Hash); if (txidsSet.Count == 0) { break; } } } } }
private async Task ListenToSessionLoop(LongPollingNotificationSession session, CancellationToken stoppingToken) { var client = session.Client; while (!stoppingToken.IsCancellationRequested) { try { var events = (await session.GetEventsAsync(lastEventId, 10000, true, stoppingToken)) .Where(e => e.EventType == "newblock") .Select(e => (NewBlockEvent)e) .OrderBy(e => e.Height); foreach (var e in events) { var newBlock = await client.RPCClient.GetBlockAsync(e.Hash).ConfigureAwait(false); _peerManager.BlockNotifier.BlockConnected(newBlock, (uint)e.Height); lastEventId = e.EventId > lastEventId ? e.EventId : lastEventId; } try { var h = await _explorerClient.GetFeeRateAsync(FeeRateSet.HighPriorityBlockCount, stoppingToken); var n = await _explorerClient.GetFeeRateAsync(FeeRateSet.NormalBlockCount, stoppingToken); var b = await _explorerClient.GetFeeRateAsync(FeeRateSet.BackgroundBlockCount, stoppingToken); _feeRateWriter.TryWrite(new FeeRateSet() { HighPriority = h.FeeRate, Normal = n.FeeRate, Background = b.FeeRate }); } catch (NBXplorerException ex) { _logger.LogError($"Failed to estimate fee by nbxplorer: \"{ex.Message}\""); } // nbx does not return blocks aligned with eventId(i.e. sometimes block height will decrease when // eventId increase. event if there are no forks. This is especially true in regtest, where many blocks // are generated at once.) // so to get the blocks in batch and to sort it by its height in our side, we will limit our query // frequency by waiting in here. await Task.Delay(6000, stoppingToken); } catch (HttpRequestException ex) when(ex.InnerException is TimeoutException) { } catch (OperationCanceledException ex) { if (stoppingToken.IsCancellationRequested) { break; } } catch (Exception ex) { _logger.LogError($"Failed to listen on nbx {ex}"); break; } } _logger.LogInformation($"Shutting down nbx listener session loop..."); }
public static void WaitForTransaction(this LongPollingNotificationSession session, BitcoinAddress address, uint256 txId) { session.WaitForTransaction(TrackedSource.Create(address), txId); }
public static void WaitForTransaction(this LongPollingNotificationSession session, DerivationStrategyBase derivationStrategy, uint256 txId) { session.WaitForTransaction(TrackedSource.Create(derivationStrategy), txId); }