Exemple #1
0
        internal static void Start(MQConnectionPool mqpool)
        {
            if (NXTAccountGenerator.Started)
            {
                Log.Info("NXT充值地址生成器已启动");
                return;
            }

            nxtClient = new NXTClient4Net(Config.NXTServer, Config.SecretPhrase);
            _mqpool = mqpool;
            var currencies = Enum.GetValues(typeof(CurrencyType)).Cast<CurrencyType>();

            if (!Enum.TryParse<CurrencyType>(Config.CoinCode, out _currency))
            {
                Log.Error("配置文件中的CoinCode配置错误:错误值为{0},请使用正确的虚拟币Code", Config.CoinCode);
            }

            StartGenerateNewAddressConsumer(_currency);
        }
Exemple #2
0
        internal static void Start(MQConnectionPool mqpool)
        {
            if (NXTBalanceWatcher.Started)
            {
                Log.Info("{0}余额监控器已启动", Config.CoinCode);
                return;
            }
            _nxtclient = new NXTClient4Net(Config.NXTServer, Config.SecretPhrase);
            _dbContext = new DbContext().ConnectionString(Config.DBConnectString, new MySqlProvider());
            _mqpool = mqpool;

            var currencies = Enum.GetValues(typeof(CurrencyType)).Cast<CurrencyType>();

            if (!Enum.TryParse<CurrencyType>(Config.CoinCode, out _currency))
            {
                Log.Error("配置文件中的CoinCode配置错误:错误值为{0},请使用正确的虚拟币Code", Config.CoinCode);
            }

            StartNXTBalanceWatcher(_currency);
        }
        internal static void Start(MQConnectionPool mqpool)
        {
            if (NXTSendTransactionListener.Started)
            {
                Log.Info("{0}提现处理器已启动", Config.CoinCode);
                return;
            }

            nxtClient = new NXTClient4Net(Config.NXTServer, Config.SecretPhrase);
            _mqpool = mqpool;

            var currencies = Enum.GetValues(typeof(CurrencyType)).Cast<CurrencyType>();

            if (!Enum.TryParse<CurrencyType>(Config.CoinCode, out _currency))
            {
                Log.Error("配置文件中的CoinCode配置错误:错误值为{0},请使用正确的虚拟币Code", Config.CoinCode);
            }

            StartSendPaymentTransactionConsumer(_currency);
        }
        internal static void Start(MQConnectionPool mqpool)
        {
            if (NXTReceiveTransactionListener.Started)
            {
                Log.Info("NXT到款交易监听器已启动");
                return;
            }

            _mqpool = mqpool;
            nxtClient = new NXTClient4Net(Config.NXTServer, Config.SecretPhrase);
            _dbContext = new DbContext().ConnectionString(Config.DBConnectString, new MySqlProvider());

            if (!Enum.TryParse<CurrencyType>(Config.CoinCode, out _currency))
            {
                Log.Error("配置文件中的CoinCode配置错误:错误值为{0},请使用正确的虚拟币Code", Config.CoinCode);
            }

            var thread = new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    if (!NXTReceiveTransactionListener.Started)
                    {
                        Log.Info("{0}到款交易监听器启动成功,监听交易中...", Config.CoinCode);
                        NXTReceiveTransactionListener.Started = true;
                    }
                    if (Program.Fusing)
                    {
                        Log.Info("发生熔断事件,虚拟币交易监听器停止运行");
                        _cancelTokenSource.Cancel();
                        break;
                    }

                    var isProcessToEnd = false;
                    var offset = 0;

                    while (!isProcessToEnd)
                    {
                        var nxtAccounts = GetNXTAccounts(offset);

                        if (nxtAccounts != null && nxtAccounts.Count() > 0)
                        {
                            offset = nxtAccounts.Last().ID;

                            nxtAccounts.AsParallel().ForAll(nc =>
                            {
                                var txIds = GetTxIds(nc.UserID, nc.NxtAccountID, 0);

                                if (txIds != null && txIds.TransactionIds.Length > 0)
                                {
                                    txIds.TransactionIds.ForEach(txid =>
                                    {
                                        if (!ExistTx(nc.Address, txid, _currency))
                                        {
                                            var tx = default(GetTransactionResponse);

                                            if (TryGetTransaction(txid, out tx)) ProcessTx(nc.UserID, tx, nc.NxtAccountID, _currency);
                                        }
                                    });
                                }
                            });
                        }
                        else isProcessToEnd = true;
                    }

                    Thread.Sleep(Config.LoopInterval * 1000);
                }
            }));

            thread.Start();
        }
        private static void ProcessTx(int userID, GetTransactionResponse tx, UInt64 nxtAccountID, CurrencyType currency)
        {
            var cacheKey = currency.ToString() + tx.SenderRS + tx.Transaction + "create";

            if (tx.Type != TransactionType.Ordinary || tx.Recipient != nxtAccountID)
            {
                //如果交易类型不是普通交易或者收款账户非当前账户(非收款),忽略,并加入忽略名单,防止多次请求交易明细,浪费资源
                Cache.Add(cacheKey, tx.Confirmations);
                return;
            }
            object obj = null;
            if (Cache.TryGet(cacheKey, out obj)) return;

            Log.Info("发现新的交易....");

            var actualAmount = tx.AmountNQT / 100000000;

            #region 发送交易到处理消息队列
            var mqname = "NXTReceiveTransactionListener";
            _channel = _mqpool.GetMQChannel(mqname);
            var exchangeName = Utilities.GenerateVirtualCoinReceivePaymentExchangeAndQueueName(currency).Item1;
            var build = new BytesMessageBuilder(_channel);

            var cmd = new CreateReceivePaymentTransaction(tx.Transaction, tx.RecipientRS, actualAmount, currency);
            build.WriteBytes(Encoding.UTF8.GetBytes(IoC.Resolve<IJsonSerializer>().Serialize(cmd)));
            ((IBasicProperties)build.GetContentHeader()).DeliveryMode = 2;

            try
            {
                Log.Info("交易将被发送到Exchange->{0}", exchangeName);

                _channel.BasicPublish(exchangeName, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());

                Log.Info("交易成功发送到Exchange->{0}", exchangeName);

                Cache.Add(cacheKey, tx.Confirmations);
            }
            catch (RabbitMQ.Client.Exceptions.AlreadyClosedException ex)
            {
                Log.Error("发送{0}新交易创建指令时发现消息队列服务器已关闭".FormatWith(Config.CoinCode), ex);

                try
                {
                    _channel = _mqpool.GetMQChannel(mqname);
                    _channel.BasicPublish(exchangeName, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
                    Log.Info("交易成功发送到Exchange->{0}", exchangeName);
                }
                catch (Exception eex)
                {
                    Log.Fatal("重新链接消息队列发送NXT新交易仍然出错了".FormatWith(Config.CoinCode), eex);
                }
            }
            catch (Exception ex)
            {
                Log.Error("发送{0}新交易创建指令时出现错误".FormatWith(Config.CoinCode), ex);
            }
            #endregion

            #region 提取币到总账户中
            try
            {
                var nxtclient = new NXTClient4Net(Config.NXTServer, Config.SecretPhrase + userID);
                var result = nxtclient.SendMoneyAsnc(Config.NxtSumAccount, actualAmount - 1).Result;
                Log.Info("用户充值的币发送到总帐户成功");
            }
            catch (Exception ex)
            {
                Log.Fatal("用户ID={0}充值的{1}NXT发送到总帐户失败了,请及时处理".FormatWith(userID, actualAmount), ex);
            }
            #endregion
        }
        internal static void Start(MQConnectionPool mqpool)
        {
            if (NXTTransactionConfirmationValidator.Started)
            {
                Log.Info("{0}转账确认验证器已启动", Config.CoinCode);
                return;
            }

            nxtClient = new NXTClient4Net(Config.NXTServer, Config.SecretPhrase);
            _dbContext = new DbContext().ConnectionString(Config.DBConnectString, new MySqlProvider());
            _mqpool = mqpool;

            if (!Enum.TryParse<CurrencyType>(Config.CoinCode, out _currency))
            {
                Log.Error("配置文件中的CoinCode配置错误:错误值为{0},请使用正确的虚拟币Code", Config.CoinCode);
            }

            var thread = new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    if (!NXTTransactionConfirmationValidator.Started)
                    {
                        Log.Info("{0}转账确认验证器启动成功", Config.CoinCode);
                        NXTTransactionConfirmationValidator.Started = true;
                    }

                    if (Program.Fusing)
                    {
                        Log.Info("发生熔断事件,虚拟币转账确认验证器停止运行");
                        break;
                    }

                    var unconfirmTxs = GetUnconfirmPaymentTransactions().ToList();

                    if (unconfirmTxs != null && unconfirmTxs.Count() > 0)
                    {
                        var needConfirmations = GetNeedConfirmationCount(_currency);

                        for (int i = 0; i < unconfirmTxs.Count(); i++)
                        {
                            var tx = unconfirmTxs[i];

                            var txRepos = default(GetTransactionResponse);

                            if (TryGetTransaction(tx.TxID, out txRepos))
                            {
                                if (txRepos.Confirmations >= needConfirmations)
                                {
                                    ProcessTx(txRepos, _currency);
                                }
                                else
                                {
                                    Log.Info("txid={0}的交易确认不足{1}个,等待下次处理", tx.TxID, needConfirmations);
                                }
                            }
                        }
                    }

                    Thread.Sleep(Config.LoopInterval * 1000);
                }
            }));

            thread.Start();
        }