Beispiel #1
0
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var completionSource = new TaskCompletionSource();
            var task             = completionSource.Task;

            _logger.LogInformation("Configuration: UniqueEndpoint {UniqueEndpoint}, MatcherContractAddress {MatcherContractAddress}, MarketplaceUniqueAddress {MarketplaceUniqueAddress}",
                                   _configuration.UniqueEndpoint,
                                   _configuration.MatcherContractAddress,
                                   _configuration.MarketplaceUniqueAddress);

            SafeApplication?application = null;

            application = SafeApplication.CreateApplication(ex =>
            {
                _logger.LogError(ex, "{ServiceName} failed", GetType().FullName);
                Interlocked.Exchange <TaskCompletionSource?>(ref completionSource, null)?.SetException(ex);
                application?.Dispose();
            }, _logger, _configuration.MatcherContractPublicKey);
            application.Application.Connect(_configuration.UniqueEndpoint);
            application.Application !.SubscribeAccountInfo(_configuration.MarketplaceUniqueAddress, info =>
            {
                Interlocked.Exchange <TaskCompletionSource?>(ref completionSource, null)?.SetResult();
                application.Dispose();
                _logger.LogInformation("Admins balance: Free: {Free}, Reserved: {Reserved}, FeeFrozen: {FeeFrozen}, MiscFrozen: {MiscFrozen}",
                                       info.AccountData.Free,
                                       info.AccountData.Reserved,
                                       info.AccountData.FeeFrozen,
                                       info.AccountData.MiscFrozen);
            });

            return(task);
        }
Beispiel #2
0
        public static Task CallSubstrate(this BackgroundService service, ILogger logger, PublicKey contractKey, string nodeEndpoint, Address from, byte[] privateKey, Func <IApplication, IExtrinsicCall> callGenerator)
        {
            var completionSource = new TaskCompletionSource();

            Task.Run(async() =>
            {
                SafeApplication application = SafeApplication.CreateApplication(
                    ex =>
                {
                    logger.LogError(ex, "{ServiceName} substrate api failed", service.GetType().FullName);
                    Interlocked.Exchange <TaskCompletionSource?>(ref completionSource, null)?.SetException(ex);
                }, logger, contractKey);
                try
                {
                    application.Application.Connect(nodeEndpoint);
                    var call = callGenerator(application.Application !);
                    application.HealthCheck(TimeSpan.FromMinutes(10), () =>
                    {
                        Interlocked.Exchange <TaskCompletionSource?>(ref completionSource, null)?.SetException(new TimeoutException());
                        application.Dispose();
                    });
                    var result =
                        await application.Application.SignWaitRetryOnLowPriority(from,
                                                                                 privateKey, call);
                    application.CancelHealthCheck();
                    result.Switch(_ =>
                    {
                        Interlocked.Exchange <TaskCompletionSource?>(ref completionSource, null)?.SetResult();
                    }, fail =>
                    {
                        var error = fail.ErrorMessage(application.Application);
                        logger.LogError("Failed to call substrate, {ErrorText}", error);
                        Interlocked.Exchange <TaskCompletionSource?>(ref completionSource, null)?.SetException(new ApplicationException(error));
                    });
                }
                catch (Exception ex)
                {
                    Interlocked.Exchange <TaskCompletionSource?>(ref completionSource, null)?.SetException(ex);
                }
                finally
                {
                    application.Dispose();
                }
            });
            return(completionSource.Task);
        }
        protected override Task ProcessBlock(ulong blockNumber, CancellationToken stoppingToken)
        {
            TaskCompletionSource <int>?myTask = new ();
            var task = myTask.Task;

            if (_application == null)
            {
                _application = SafeApplication.CreateApplication(ex =>
                {
                    _logger.LogError(ex, "{ServiceName} listener failed", GetType().FullName);
                    Interlocked.Exchange <TaskCompletionSource <int>?>(ref myTask, null)?.SetException(ex);

                    _application?.Dispose();
                    _application = null;
                }, _logger, _matcherContract);
                _application.Application.Connect(_nodeEndpoint);
            }

            Run(blockNumber, stoppingToken).ContinueWith(t =>
            {
                if (t.IsCanceled)
                {
                    // ReSharper disable once MethodSupportsCancellation
                    Interlocked.Exchange <TaskCompletionSource <int>?>(ref myTask, null)?.SetCanceled();
                }

                if (t.IsFaulted)
                {
                    _logger.LogError(t.Exception, "Extrinsic listener failed");
                    Interlocked.Exchange <TaskCompletionSource <int>?>(ref myTask, null)?.SetException(t.Exception !);
                }

                if (t.IsCompletedSuccessfully)
                {
                    Interlocked.Exchange <TaskCompletionSource <int>?>(ref myTask, null)?.SetResult(0);
                }
            });

            return(task);
        }