public bool ForgeBlock(BlockMsg blockMsg) { LogHelper.Info("Forge Block 8"); var hashResult = BlockHelper.GetMiningWorkResult(blockMsg); LogHelper.Info("Height:" + blockMsg.Header.Height); LogHelper.Info("Bits:" + POC.ConvertBitsToBigInt(blockMsg.Header.Bits).ToString("X").PadLeft(64, '0')); LogHelper.Info("Hash:" + Base16.Encode(hashResult)); LogHelper.Debug("Nonce:" + blockMsg.Header.Nonce); LogHelper.Debug("Address:" + blockMsg.Header.GeneratorId); if (!POC.Verify(blockMsg.Header.Bits, hashResult)) { LogHelper.Info("POC Verify Failed"); return(false); } LogHelper.Info("Forge Block 9"); var result = false; try { if (this.SubmitBlock(blockMsg)) { result = true; } LogHelper.Info($"Forge Block 10, {result}"); } catch (Exception ex) { LogHelper.Error(ex.ToString()); } finally { if (!result) { Thread.Sleep(60 * 1000); var nodeHeight = GetBlockHeight(); LogHelper.Info($"DB nodeHeight:{nodeHeight}, currentHeight:{blockMsg.Header.Height}"); if (nodeHeight >= blockMsg.Header.Height) { result = true; } else { result = false; } } } return(result); }
private void StartMining() { var block = NodeApi.Current.GenerateMiningBlock(_startArgs.Name, _startArgs.Account); var currentScoopNumber = POC.GetScoopNumber(block.Header.PayloadHash, block.Header.Height); _startTime = Time.EpochTime; block.Header.Timestamp = _startTime; if (block.Header.Height != this.currentBlockHeight || this.currentBlockStartTime <= 0) { this.currentBlockStartTime = _startTime; this.currentBlockHeight = block.Header.Height; } StartMiningMsg startMiningMsg = StartMiningMsg.CreateNew(); startMiningMsg.BlockHeight = block.Header.Height; startMiningMsg.ScoopNumber = currentScoopNumber; startMiningMsg.StartTime = _startTime; var genHash = BlockHelper.GenHash(block.Header.PayloadHash, block.Header.Height); startMiningMsg.GenHash = Base16.Encode(genHash); startMiningMsg.BaseTarget = NodeApi.Current.GetBaseTarget(block.Header.Height); var blockKey = KeyHelper.GetBlockKey(startMiningMsg.Id); RedisManager.Current.SaveDataToRedis(blockKey, block); //MQApi.SendStartMsg(startMiningMsg); RabbitMQApi.SendStartMsg(startMiningMsg); CurrentStartMiningMsg = startMiningMsg; LogHelper.Info("Start MiningTask Id=" + startMiningMsg.Id + " ScoopNumber = " + startMiningMsg.ScoopNumber + " Height= " + startMiningMsg.BlockHeight); LogHelper.Info($"Block bits is {POC.ConvertBitsToBigInt(block.Header.Bits).ToString("X").PadLeft(64, '0')}"); LogHelper.Info($"StartMiningMsg.BaseTarget is {POC.ConvertBitsToBigInt(startMiningMsg.BaseTarget).ToString("X").PadLeft(64, '0')}"); LogHelper.Info($"Block height is {block.Header.Height}"); }
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); }