Ejemplo n.º 1
0
        private static async Task <bool> Run(int blockchainID, string webSocketsUrl, string rpcUrl, BlockchainType blockchainType,
                                             BlockchainNetwork blockchainNetwork)
        {
            bool hasFailed = false;

            using (var client = new StreamingWebSocketClient(webSocketsUrl))
            {
                EthLogsObservableSubscription logsSubscription = new EthLogsObservableSubscription(client);

                Web3 cl = new Web3(rpcUrl);

                RequestInterceptor r = new RPCInterceptor(blockchainType);
                cl.Client.OverridingRequestInterceptor = r;
                EthApiService eth = new EthApiService(cl.Client);

                logsSubscription.GetSubscriptionDataResponsesAsObservable().Subscribe(async filterLog =>
                {
                    FilterLog transaction = filterLog;

                    if (transaction.Removed)
                    {
                        return;
                    }

                    if (SmartContractManager.TryGetAddress(blockchainID, filterLog.Address, out ContractTypeEnum type))
                    {
                        await ProcessSmartContractEvent(blockchainID, blockchainType, blockchainNetwork, type, eth, filterLog,
                                                        transaction, cl);
                    }
                });

                _dictionary[blockchainID] = new Subscription(client, logsSubscription);

                await client.StartAsync();

                client.Error += Client_Error;

                while (!client.IsStarted)
                {
                    await Task.Delay(1000);
                }

                await logsSubscription.SubscribeAsync();

                while (!hasFailed)
                {
                    try
                    {
                        var handler = new EthBlockNumberObservableHandler(client);
                        handler.GetResponseAsObservable().Subscribe(integer => { });
                        await handler.SendRequestAsync();

                        SystemStatus status = new SystemStatus(TaskNames.WebSockets, blockchainID);
                        await using (var connection = new MySqlConnection(OTHubSettings.Instance.MariaDB.ConnectionString))
                        {
                            await status.InsertOrUpdate(connection, true, null, false, "Blockchain Sync");
                        }
                    }

                    catch (Exception ex) when(errorCounter <= 100)
                    {
                        hasFailed = true;
                        _dictionary.Remove(blockchainID, out _);
                        client.Dispose();
                        //try
                        //{
                        //    await client.StopAsync();
                        //    await client.StartAsync();
                        //}
                        //catch (Exception)
                        //{
                        //    Logger.WriteLine(Source.BlockchainSync, ex.ToString());
                        //}
                    }

                    await Task.Delay(120000);
                }
            }

            return(!hasFailed);
        }
Ejemplo n.º 2
0
        public static EventLog <TEventDTO> DecodeEvent <TEventDTO>(this EventABI eventABI, FilterLog log) where TEventDTO : new()
        {
            if (!IsLogForEvent(eventABI, log))
            {
                return(null);
            }
            var eventDecoder = new EventTopicDecoder(eventABI.IsAnonymous);
            var eventObject  = eventDecoder.DecodeTopics <TEventDTO>(log.Topics, log.Data);

            return(new EventLog <TEventDTO>(eventObject, log));
        }
Ejemplo n.º 3
0
        public static TEventDTO DecodeEvent <TEventDTO>(this TEventDTO eventDTO, FilterLog log) where TEventDTO : IEventDTO
        {
            var eventABI = ABITypedRegistry.GetEvent <TEventDTO>();

            return(DecodeEvent <TEventDTO>(eventDTO, eventABI, log));
        }
Ejemplo n.º 4
0
 public EventLog(T eventObject, FilterLog log)
 {
     Event = eventObject;
     Log   = log;
 }
Ejemplo n.º 5
0
        public static bool IsLogForEvent <TEventDTO>(this FilterLog log)
        {
            var eventABI = ABITypedRegistry.GetEvent <TEventDTO>();

            return(eventABI.IsLogForEvent(log));
        }
Ejemplo n.º 6
0
 public bool IsLogForEvent(FilterLog log)
 {
     return(EventABI.IsLogForEvent(log));
 }
Ejemplo n.º 7
0
 public static bool IsLogForEvent(this EventABI eventABI, FilterLog log)
 {
     return(IsLogForEvent(log, eventABI.Sha3Signature));
 }
        protected override Task ExecuteInternalAsync(FilterLog value)
        {
            var eventLog = value.DecodeEvent <TEvent>();

            return(_eventAction(eventLog));
        }
        public bool IsStandardDataChangeLog(FilterLog log)
        {
            var dataChangeEvent = GetStandardDataChangedEvent();

            return(dataChangeEvent.IsLogForEvent(log));
        }
Ejemplo n.º 10
0
 public static GenericSearchDocument ToAzureDocument(
     this FilterLog log,
     SearchField[] searchFields)
 {
     return(CreateFieldWithValueDictionary(log, searchFields, (field) => field.GetFilterLogValue(log)));
 }
Ejemplo n.º 11
0
 protected virtual async Task CrawlFilterLog(CrawlerStepCompleted <TransactionReceiptVO> completedStep, FilterLog filterLog)
 {
     var currentStepCompleted = await FilterLogCrawlerStep.ExecuteStepAsync(
         new FilterLogVO(completedStep.StepData.Transaction, completedStep.StepData.TransactionReceipt, filterLog), completedStep.ExecutedStepsCollection);
 }
Ejemplo n.º 12
0
 public LogWithReceiptAndTransaction(Transaction transaction, TransactionReceipt receipt, FilterLog log)
 {
     Transaction = transaction;
     Receipt     = receipt;
     Log         = log;
 }
Ejemplo n.º 13
0
        private EthereumEventData ExtractEventData(List <ParameterOutput> eventParams, FilterLog log)
        {
            Dictionary <string, string> values = eventParams.ToDictionary(eventParam => eventParam.Parameter.Name, eventParam => eventParam.Result.ToString());

            return(new EthereumEventData
            {
                Values = values,
                BlockNumber = log.BlockNumber
            });
        }
 public static GenericSearchDocument Map(FilterLog filterLog)
 {
     return(filterLog.ToAzureDocument(PresetSearchFields.LogFields));
 }
 public virtual bool IsLogForEvent(FilterLog log) => log.IsLogForEvent <TEventDto>();
 public static JObject ConvertToJObject(this FilterLog filterLog)
 {
     return(JObject.FromObject(filterLog));
 }
Ejemplo n.º 17
0
 public bool IsLogForEvent(FilterLog log)
 {
     return(log.IsLogForEvent <TEvent>());
 }
Ejemplo n.º 18
0
        /*==================================================================================================================================
        * Public Methods
        *=================================================================================================================================*/
        public questStatus Save(FilterId filterId, Filter filter)
        {
            // Initialize
            questStatus      status     = null;
            Mgr              mgr        = new Mgr(this.UserSession);
            DbMgrTransaction trans      = null;
            FiltersMgr       filtersMgr = new FiltersMgr(this.UserSession);
            ColumnsMgr       columnMgr  = new ColumnsMgr(this.UserSession);


            try
            {
                // Validate filter
                status = Verify(filterId, filter);
                if (!questStatusDef.IsSuccess(status))
                {
                    return(status);
                }


                // BEGIN TRANSACTION
                status = mgr.BeginTransaction("SaveFilter" + Guid.NewGuid().ToString(), out trans);
                if (!questStatusDef.IsSuccess(status))
                {
                    return(status);
                }


                // Save the filter
                status = Save(trans, filterId, filter);
                if (!questStatusDef.IsSuccess(status))
                {
                    mgr.RollbackTransaction(trans);

                    if (bLogging)
                    {
                        Filter _filter = null;
                        status = filtersMgr.Read(filterId, out _filter);
                        if (!questStatusDef.IsSuccess(status))
                        {
                            return(status);
                        }

                        FilterLog filterLog = new FilterLog();
                        filterLog.Database = "";
                        filterLog.Tableset = "Tableset.Id=" + filter.TablesetId.ToString();
                        filterLog.Name     = _filter.Name;
                        filterLog.Event    = "SAVE";
                        filterLog.Data     = status.ToString();
                        FilterLogId filterLogId = null;
                        _dbFilterLogsMgr.Create(filterLog, out filterLogId);
                    }
                    return(status);
                }


                // COMMIT TRANSACTION
                status = mgr.CommitTransaction(trans);
                if (!questStatusDef.IsSuccess(status))
                {
                    return(status);
                }

                // Logging
                if (bLogging)
                {
                    Filter _filter = null;
                    status = filtersMgr.Read(filterId, out _filter);
                    if (!questStatusDef.IsSuccess(status))
                    {
                        return(status);
                    }

                    FilterLog filterLog = new FilterLog();
                    filterLog.Database = "";
                    filterLog.Tableset = "Tableset.Id=" + filter.TablesetId.ToString();
                    filterLog.Name     = _filter.Name;
                    filterLog.Event    = "SAVE";
                    filterLog.Data     = status.ToString();
                    FilterLogId filterLogId = null;
                    _dbFilterLogsMgr.Create(filterLog, out filterLogId);
                }

                // TODO: REFACTOR TO GET ALL-IN-ONE TRANSACTION
                // Generate filter SQL
                Filter      filterWithSQL = null;
                DbFilterMgr dbFilterMgr   = new DbFilterMgr(this.UserSession);
                status = dbFilterMgr.GenerateFilterSQL(filterId, out filterWithSQL);
                if (!questStatusDef.IsSuccess(status))
                {
                    return(status);
                }

                // Update filter
                status = filtersMgr.Update(filterWithSQL);
                if (!questStatusDef.IsSuccess(status))
                {
                    return(status);
                }
            }
            catch (System.Exception ex)
            {
                if (trans != null)
                {
                    mgr.RollbackTransaction(trans);
                }
                return(new questStatus(Severity.Fatal, String.Format("EXCEPTION: {0}.{1}: {2}",
                                                                     this.GetType().Name, MethodBase.GetCurrentMethod().Name,
                                                                     ex.InnerException != null ? ex.InnerException.Message : ex.Message)));
            }
            return(new questStatus(Severity.Success));
        }
 public MessageToQueue(FilterLog log)
 {
     BlockNumber     = log.BlockNumber.Value.ToString();
     TransactionHash = log.TransactionHash;
     LogIndex        = log.LogIndex.ToString();
 }
Ejemplo n.º 20
0
        public static bool HasSameNumberOfIndexes <TEventDTO>(FilterLog log) where TEventDTO : IEventDTO
        {
            var eventABI = ABITypedRegistry.GetEvent <TEventDTO>();

            return(eventABI.HasSameNumberOfIndexes(log));
        }
Ejemplo n.º 21
0
 public static bool IsLogForEvent <TEventDTO>(this TEventDTO eventDTO, FilterLog log) where TEventDTO : IEventDTO
 {
     return(IsLogForEvent <TEventDTO>(log));
 }
Ejemplo n.º 22
0
 public static bool HasSameNumberOfIndexes <TEventDTO>(this TEventDTO eventDTO, FilterLog log) where TEventDTO : IEventDTO
 {
     return(eventDTO.GetEventABI().HasSameNumberOfIndexes(log));
 }
Ejemplo n.º 23
0
 public bool IsLogForEvent(FilterLog log)
 {
     return(EventBuilder.IsLogForEvent(log));
 }
Ejemplo n.º 24
0
 public static bool HasSameNumberOfIndexes(this EventABI eventAbi, FilterLog log)
 {
     return(eventAbi.NumberOfIndexes == (log.Topics?.Length - 1));
 }
Ejemplo n.º 25
0
        public static EventLog <List <ParameterOutput> > DecodeEventDefaultTopics(this EventABI eventABI, FilterLog log)
        {
            if (!IsLogForEvent(eventABI, log))
            {
                return(null);
            }
            var eventDecoder = new EventTopicDecoder();
            var eventObject  = eventDecoder.DecodeDefaultTopics(eventABI, log.Topics, log.Data);

            return(new EventLog <List <ParameterOutput> >(eventObject, log));
        }
Ejemplo n.º 26
0
 public static string EventSignature(this FilterLog log) => log.GetTopic(0);
Ejemplo n.º 27
0
        public static EventLog <TEventDTO> DecodeEvent <TEventDTO>(this FilterLog log) where TEventDTO : new()
        {
            var eventABI = ABITypedRegistry.GetEvent <TEventDTO>();

            return(eventABI.DecodeEvent <TEventDTO>(log));
        }
Ejemplo n.º 28
0
 public static string IndexedVal3(this FilterLog log) => log.GetTopic(3);
Ejemplo n.º 29
0
        public static TEventDTO DecodeEvent <TEventDTO>(this TEventDTO eventDTO, EventABI eventABI, FilterLog log) where TEventDTO : IEventDTO
        {
            if (!IsLogForEvent(eventABI, log))
            {
                return(default(TEventDTO));
            }
            var eventDecoder = new EventTopicDecoder();

            return(eventDecoder.DecodeTopics(eventDTO, log.Topics, log.Data));
        }
Ejemplo n.º 30
0
        private static async Task ProcessHoldingSmartContractEvent(int blockchainID, BlockchainType blockchainType,
                                                                   BlockchainNetwork blockchainNetwork, EthApiService eth, FilterLog filterLog, FilterLog transaction, Web3 cl)
        {
            var contract = new Contract(eth,
                                        AbiHelper.GetContractAbi(ContractTypeEnum.Holding, blockchainType, blockchainNetwork), filterLog.Address);

            var offerCreatedEvent   = contract.GetEvent("OfferCreated");
            var offerFinalizedEvent = contract.GetEvent("OfferFinalized");
            var offerTaskEvent      = contract.GetEvent("OfferTask");
            var paidOutEvent        = contract.GetEvent("PaidOut");

            await using (var connection =
                             new MySqlConnection(OTHubSettings.Instance.MariaDB.ConnectionString))
            {
                try
                {
                    if (offerCreatedEvent.IsLogForEvent(transaction))
                    {
                        var events =
                            offerCreatedEvent.DecodeAllEventsForEvent <Models.Program.OfferCreated>(
                                new[] { filterLog });

                        foreach (EventLog <Models.Program.OfferCreated> eventLog in events)
                        {
                            await SyncHoldingContractTask.ProcessOfferCreated(connection, blockchainID, cl,
                                                                              filterLog.Address, eventLog);
                        }

                        await ProcessJobsTask.Execute(connection, blockchainID, blockchainType, blockchainNetwork);
                    }

                    if (offerFinalizedEvent.IsLogForEvent(transaction))
                    {
                        var events =
                            offerFinalizedEvent.DecodeAllEventsDefaultForEvent(new[] { filterLog });

                        foreach (EventLog <List <ParameterOutput> > eventLog in events)
                        {
                            await SyncHoldingContractTask.ProcessOfferFinalised(connection, blockchainID, cl,
                                                                                filterLog.Address,
                                                                                eventLog);
                        }

                        await ProcessJobsTask.Execute(connection, blockchainID, blockchainType, blockchainNetwork);
                    }
                    if (paidOutEvent.IsLogForEvent(transaction))
                    {
                        var events =
                            paidOutEvent.DecodeAllEventsDefaultForEvent(new[] { filterLog });

                        foreach (EventLog <List <ParameterOutput> > eventLog in events)
                        {
                            await SyncHoldingContractTask.ProcessPayout(connection, blockchainID, cl,
                                                                        filterLog.Address, eventLog);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteLine(Source.BlockchainSync, ex.ToString());
                }
            }
        }