Beispiel #1
0
        /// <summary>
        /// RabbitMQ发送ForgetMsg信息
        /// </summary>
        /// <param name="account"></param>
        /// <param name="nonce"></param>
        /// <param name="startMsgId"></param>
        public static void SendForgeBlock(string account, long nonce, string startMsgId)
        {
            ForgeMsg forgeMsg = new ForgeMsg();

            forgeMsg.Account    = account;
            forgeMsg.Nonce      = nonce;
            forgeMsg.StartMsgId = startMsgId;
            string json = JsonConvert.SerializeObject(forgeMsg);

            RabbitMqClient.Current.ProduceMessage(RabbitMqName.ForgetBlock, MsgType.ForgetBlock, json);
        }
Beispiel #2
0
        internal static void Received(TcpReceiveState e, PoolCommand cmd)
        {
            var miner = PoolCache.WorkingMiners.FirstOrDefault(m => m.ClientAddress == e.Address && m.IsConnected);

            if (miner == null)
            {
                LogHelper.Info("Received invalid scoop data from " + e.Address);
                LogHelper.Info("Miner logout");
                PoolJob.TcpServer.CloseSocket(e);
                return;
            }

            if (PoolCache.CurrentTask == null || PoolCache.CurrentTask.State != MiningState.Mining)
            {
                return;
            }

            var msg   = new ScoopDataMsg();
            int index = 0;

            msg.Deserialize(cmd.Payload, ref index);


            PoolCache.CurrentTask.LastReceiveTime = Time.EpochTime;

            var minerinfo = PoolCache.CurrentTask.MinerEfforts.FirstOrDefault(x => x.Account == miner.WalletAddress);

            if (minerinfo == null)
            {
                PoolCache.CurrentTask.MinerEfforts.Add(new Models.MinerEffort {
                    Account = miner.WalletAddress, Effort = 1
                });
            }
            else
            {
                if (minerinfo.Effort == Setting.MaxNonceCount)
                {
                    RejectCommand.Send(e);
                    return;
                }
                minerinfo.Effort++;
            }

            if (msg.BlockHeight != PoolCache.CurrentTask.CurrentBlockHeight)
            {
                LogHelper.Info("Received invalid scoop data from " + e.Address + ", nonce is " + msg.Nonce + ", height is " + msg.BlockHeight);
                LogHelper.Info("Block Height invalid , Stop and Send StartMsg");

                var stopMsg = new StopMsg
                {
                    BlockHeight = msg.BlockHeight,
                    Result      = false,
                    StartTime   = Time.EpochTime,
                    StopTime    = Time.EpochTime
                };
                StopCommand.Send(e, stopMsg);
                Task.Delay(1000).Wait();
                var startMsg = PoolCache.CurrentTask.CurrentStartMsg;
                if (startMsg != null)
                {
                    StartCommand.Send(e, startMsg);
                }
                return;
            }

            LogHelper.Info("Received scoop data from " + miner.ClientAddress + ", nonce is " + msg.Nonce + ", scoop number is " + msg.ScoopNumber + ", block height is " + msg.BlockHeight);

            if (msg.ScoopNumber != PoolCache.CurrentTask.CurrentScoopNumber)
            {
                LogHelper.Info("Received invalid scoop data from " + e.Address + ", nonce is " + msg.Nonce + ", ScoopNumber is " + PoolCache.CurrentTask.CurrentScoopNumber + "/" + msg.ScoopNumber);
                LogHelper.Info("Scoop Number invalid");
                return;
            }

            var verResult = POC.Verify(PoolCache.CurrentTask.BaseTarget, msg.Target);

            LogHelper.Debug("Bits:" + POC.ConvertBitsToBigInt(PoolCache.CurrentTask.BaseTarget).ToString("X").PadLeft(64, '0'));
            LogHelper.Debug("Hash:" + Base16.Encode(msg.Target));
            LogHelper.Debug("Verify Result is " + verResult);

            if (!verResult)
            {
                return;
            }

            ForgeMsg forgeMsg = new ForgeMsg();

            forgeMsg.Account    = msg.WalletAddress;
            forgeMsg.Nonce      = msg.Nonce;
            forgeMsg.StartMsgId = PoolCache.CurrentTask.Id;

            //MQApi.SendForgeBlock(msg.WalletAddress, msg.Nonce, PoolCache.CurrentTask.Id);
            RabbitMQApi.SendForgeBlock(msg.WalletAddress, msg.Nonce, PoolCache.CurrentTask.Id);
        }
Beispiel #3
0
        public static void ForgeBlock(ForgeMsg msg)
        {
            try
            {
                LogHelper.Info("Forge Block 3");

                LogHelper.Info($"msg == null : {msg == null} ; PoolCenterJob.Current == null : {PoolCenterJob.Current == null}");
                var startMsg = PoolCenterJob.Current.CurrentStartMiningMsg;

                if (msg != null && PoolCenterJob.Current != null && startMsg != null)
                {
                    LogHelper.Info($"{startMsg.Id},{msg.StartMsgId}");
                }
                if (msg == null || PoolCenterJob.Current == null || startMsg == null || startMsg.Id != msg.StartMsgId)
                {
                    return;
                }
                LogHelper.Info("Forge Block 4");

                var blockMsg = RedisManager.Current.GetDataInRedis <BlockMsg>(msg.StartMsgId);
                if (blockMsg == null || blockMsg.Header.Height < startMsg.BlockHeight)
                {
                    return;
                }

                LogHelper.Info("Forge Block 5");
                if (!(blockMsg.Header.Height > NodeApi.Current.GetBlockHeight()))
                {
                    return;
                }

                LogHelper.Info("Forge Block 6");
                LogHelper.Info("Received Msg [ForgeBlock]");
                BlockMsg successBlock = null;
                if (PoolCenterJob.Current.ForgeBlock(msg.StartMsgId, msg.Account, msg.Nonce, out successBlock))
                {
                    CenterCache.GenarateBlockCount++;
                    PoolCenterJob.Current.StopMining(true, msg.StartMsgId);

                    //分析存币交易
                    ProcessDepositTx(successBlock);
                    Task.Delay(Setting.MaxMiningBlockCount).Wait();
                    PoolCenterJob.Current.Start();

                    Task.Run(() =>
                    {
                        if (PoolCenterJob.Current.RemoveStartMsgIds.ContainsKey(msg.StartMsgId))
                        {
                            PoolCenterJob.Current.RemoveStartMsgIds[msg.StartMsgId] = Time.EpochTime;
                        }
                        Task.Delay(Setting.SAVE_REWARDS_BEHIND_GENERATETIME_BLOCK).Wait();
                        var height = Convert.ToInt32(blockMsg.Header.Height);
                        PoolApi.SaveRewards(msg.StartMsgId, msg.Nonce, height);
                    });
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.ToString());
            }
        }