Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="json"></param>
        public void ReceiveLoginMsg(string json)
        {
            var msg = JsonConvert.DeserializeObject <MinerLoginMsg>(json);

            if (msg == null)
            {
                return;
            }

            LogHelper.Info("Receive LoginMsg");

            if (msg.ServerId == Setting.PoolId)
            {
                return;
            }

            var miner = PoolCache.WorkingMiners.FirstOrDefault(x => x.SerialNo == msg.SN || x.WalletAddress == msg.Account);

            if (miner == null)
            {
                return;
            }

            TcpState tcpState = new TcpState()
            {
                Client = miner.Client, Stream = miner.Stream, Address = miner.ClientAddress
            };

            StopCommand.Send(tcpState, new StopMsg
            {
                BlockHeight = PoolCache.CurrentTask.CurrentBlockHeight,
                Result      = false,
                StartTime   = PoolCache.CurrentTask.StartTime,
                StopTime    = Time.EpochTime
            });
            RejectCommand.Send(tcpState);
            PoolCache.WorkingMiners.Remove(miner);
        }
Example #2
0
        /// <summary>
        /// 接收停止命令
        /// 接收到的json反序列化StopMiningMsg对象,构造StopMsg类,遍历所有的矿工发送TCP停止命令
        /// </summary>
        /// <param name="json"></param>
        public void ReceiveStopMsg(string json)
        {
            try
            {
                var msg = JsonConvert.DeserializeObject <StopMiningMsg>(json);

                LogHelper.Info("Receive StopMsg");

                if (msg == null || PoolCache.CurrentTask == null)
                {
                    return;
                }

                StopMsg stopMsg = new StopMsg();
                stopMsg.Result      = msg.StopReason == StopReason.MiningSucesses;
                stopMsg.BlockHeight = msg.CurrentHeight;
                stopMsg.StopTime    = msg.StopTime;


                var miners = PoolCache.WorkingMiners.ToArray();
                foreach (Miner item in miners)
                {
                    try
                    {
                        TcpState tcpState = new TcpState()
                        {
                            Client = item.Client, Stream = item.Stream, Address = item.ClientAddress
                        };
                        StopCommand.Send(tcpState, stopMsg);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex.ToString());
                    }
                }

                var stopTask = PoolCache.CurrentTask;
                //计算每个账户的工作量
                if (PoolCache.Efforts.ContainsKey(stopTask.CurrentBlockHeight))
                {
                    var items = PoolCache.Efforts[stopTask.CurrentBlockHeight];
                    stopTask.MinerEfforts.ForEach(x =>
                    {
                        var item = items.FirstOrDefault(p => p.Account == x.Account);
                        if (item == null)
                        {
                            items.Add(new EffortInfo {
                                Account = x.Account, Effort = x.Effort, BlockHeight = stopTask.CurrentBlockHeight
                            });
                        }
                        else
                        {
                            item.Effort += x.Effort;
                        }
                    });
                }
                else
                {
                    var efforts = stopTask.MinerEfforts.Select(x => new EffortInfo {
                        Account = x.Account, Effort = x.Effort, BlockHeight = stopTask.CurrentBlockHeight
                    }).ToList();
                    PoolCache.Efforts.Add(stopTask.CurrentBlockHeight, efforts);
                }
                //成功挖到区块,工作量保存在redis中,清空以前区块的Task
                if (msg.StopReason == StopReason.MiningSucesses)
                {
                    TimerTasks.Current.SaveMinerEffortToRedis(msg.CurrentHeight);
                    PoolCache.poolTasks.RemoveAll(x => x.CurrentBlockHeight <= stopMsg.BlockHeight);
                }
                PoolCache.CurrentTask = null;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.ToString());
            }
        }