示例#1
0
        static void Main(string[] args)
        {
            EnableCatchUnexpectedException();
            Console.CancelKeyPress += Console_CancelKeyPress;

            Thread.CurrentThread.Name = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
            ClassConsole.ConsoleWriteLine(ClassConnectorSetting.CoinName + " Miner - " + Assembly.GetExecutingAssembly().GetName().Version + "R");
            ClassMiningConfig.MiningConfigInitialization();
            StartCommandLine();
            Task.Factory.StartNew(async() =>
            {
                bool connected = await ClassMiningNetwork.StartMiningAsync();
                while (!connected)
                {
                    await Task.Delay(5000);
                    connected = await ClassMiningNetwork.StartMiningAsync();
                }
            }).ConfigureAwait(false);
        }
示例#2
0
        /// <summary>
        /// Start thread mining.
        /// </summary>
        private static async Task StartThreadMiningAsync(int idThread)
        {
            decimal minRange = Math.Round((ClassMiningStats.CurrentBlockDifficulty / ClassMiningConfig.MiningConfigThread) * (idThread - 1), 0);

            if (minRange <= 1)
            {
                minRange = 2;
            }
            decimal maxRange = Math.Round(((ClassMiningStats.CurrentBlockDifficulty / ClassMiningConfig.MiningConfigThread) * idThread), 0);

            ClassConsole.ConsoleWriteLine("Start mining thread id: " + idThread + " on mining job: " + ClassMiningStats.CurrentMiningDifficulty + " with range: " + minRange + "|" + maxRange, ClassConsoleEnumeration.IndexPoolConsoleBlueLog);
            var currentMiningJobIndication = ClassMiningStats.CurrentJobIndication;

            int currentBlockDifficultyLength = ClassMiningStats.CurrentMiningDifficulty.ToString().Length;

            while (ClassMiningNetwork.IsLogged && ThreadMiningRunning)
            {
                if (currentMiningJobIndication != ClassMiningStats.CurrentJobIndication)
                {
                    minRange = Math.Round((ClassMiningStats.CurrentBlockDifficulty / ClassMiningConfig.MiningConfigThread) * (idThread - 1), 0);
                    if (minRange <= 1)
                    {
                        minRange = 2;
                    }
                    maxRange = (Math.Round(((ClassMiningStats.CurrentBlockDifficulty / ClassMiningConfig.MiningConfigThread) * idThread), 0));
                    maxRange = Math.Round(maxRange, 0);
                    currentMiningJobIndication = ClassMiningStats.CurrentJobIndication;
                    ClassConsole.ConsoleWriteLine("Start mining thread id: " + idThread + " on mining job difficulty: " + ClassMiningStats.CurrentMiningDifficulty + " with range: " + minRange + "|" + maxRange, ClassConsoleEnumeration.IndexPoolConsoleBlueLog);
                    currentBlockDifficultyLength = ClassMiningStats.CurrentMiningDifficulty.ToString().Length;
                }
                string firstNumber  = string.Empty;
                string secondNumber = string.Empty;
                if (ClassUtils.GetRandomBetween(0, 100) >= ClassUtils.GetRandomBetween(0, 100))
                {
                    firstNumber = ClassUtility.GenerateNumberMathCalculation(minRange, maxRange, currentBlockDifficultyLength);
                }
                else
                {
                    firstNumber = ClassUtility.GetRandomBetweenJob(minRange, maxRange).ToString();
                }


                if (ClassUtils.GetRandomBetween(0, 100) >= ClassUtils.GetRandomBetween(0, 100))
                {
                    secondNumber = ClassUtility.GenerateNumberMathCalculation(minRange, maxRange, currentBlockDifficultyLength);
                }
                else
                {
                    secondNumber = ClassUtility.GetRandomBetweenJob(minRange, maxRange).ToString();
                }


                for (int i = 0; i < ClassUtility.RandomOperatorCalculation.Length; i++)
                {
                    if (i < ClassUtility.RandomOperatorCalculation.Length)
                    {
                        #region Test unreverted calculation
                        string  calculation       = firstNumber + " " + ClassUtility.RandomOperatorCalculation[i] + " " + secondNumber;
                        decimal calculationResult = ClassUtility.ComputeCalculation(firstNumber, ClassUtility.RandomOperatorCalculation[i], secondNumber);
                        if (calculationResult >= ClassMiningStats.CurrentMinRangeJob && calculationResult <= ClassMiningStats.CurrentBlockDifficulty)
                        {
                            if (calculationResult - Math.Round(calculationResult, 0) == 0) // Check if the result contain decimal places, if yes ignore it.
                            {
                                string encryptedShare = MakeEncryptedShare(calculation, (idThread - 1));
                                if (encryptedShare != ClassAlgoErrorEnumeration.AlgoError)
                                {
                                    // Generate SHA512 hash for block hash indication.
                                    string hashEncryptedShare = ClassUtility.GenerateSHA512(encryptedShare);

                                    string hashEncryptedShareForPool = ClassUtility.EncryptXorShare(hashEncryptedShare, ClassMiningStats.CurrentJobKeyEncryption);
                                    hashEncryptedShareForPool = ClassUtility.HashJobToHexString(hashEncryptedShareForPool);


                                    if (ClassMiningStats.CurrentJobIndication.ContainsKey(hashEncryptedShareForPool) || hashEncryptedShare == ClassMiningStats.CurrentBlockIndication)
                                    {
                                        if (!DictionaryShareSubmittedCache.ContainsKey(hashEncryptedShareForPool))
                                        {
                                            try
                                            {
                                                DictionaryShareSubmittedCache.Add(hashEncryptedShareForPool, calculation);
                                            }
                                            catch
                                            {
                                            }
                                            JObject share = new JObject
                                            {
                                                { "type", ClassMiningRequest.TypeSubmit },
                                                { ClassMiningRequest.SubmitResult, calculationResult.ToString("F0") },
                                                { ClassMiningRequest.SubmitFirstNumber, firstNumber },
                                                { ClassMiningRequest.SubmitSecondNumber, secondNumber },
                                                { ClassMiningRequest.SubmitOperator, ClassUtility.RandomOperatorCalculation[i] },
                                                { ClassMiningRequest.SubmitShare, encryptedShare },
                                                { ClassMiningRequest.SubmitHash, hashEncryptedShareForPool }
                                            };
                                            await ClassMiningNetwork.SendPacketToPoolAsync(share.ToString(Formatting.None)).ConfigureAwait(false);
                                        }
                                    }
                                }
                            }
                            else // Test reverted
                            {
                                #region Test reverted calculation
                                calculation       = secondNumber + " " + ClassUtility.RandomOperatorCalculation[i] + " " + firstNumber;
                                calculationResult = ClassUtility.ComputeCalculation(secondNumber, ClassUtility.RandomOperatorCalculation[i], firstNumber);

                                if (calculationResult >= ClassMiningStats.CurrentMinRangeJob && calculationResult <= ClassMiningStats.CurrentBlockDifficulty)
                                {
                                    if (calculationResult - Math.Round(calculationResult, 0) == 0) // Check if the result contain decimal places, if yes ignore it.
                                    {
                                        string encryptedShare = MakeEncryptedShare(calculation, (idThread - 1));
                                        if (encryptedShare != ClassAlgoErrorEnumeration.AlgoError)
                                        {
                                            // Generate SHA512 hash for block hash indication.
                                            string hashEncryptedShare = ClassUtility.GenerateSHA512(encryptedShare);

                                            string hashEncryptedShareForPool = ClassUtility.EncryptXorShare(hashEncryptedShare, ClassMiningStats.CurrentJobKeyEncryption);
                                            hashEncryptedShareForPool = ClassUtility.HashJobToHexString(hashEncryptedShareForPool);


                                            if (ClassMiningStats.CurrentJobIndication.ContainsKey(hashEncryptedShareForPool) || hashEncryptedShare == ClassMiningStats.CurrentBlockIndication)
                                            {
                                                if (!DictionaryShareSubmittedCache.ContainsKey(hashEncryptedShareForPool))
                                                {
                                                    try
                                                    {
                                                        DictionaryShareSubmittedCache.Add(hashEncryptedShareForPool, calculation);
                                                    }
                                                    catch
                                                    {
                                                    }

                                                    JObject share = new JObject
                                                    {
                                                        { "type", ClassMiningRequest.TypeSubmit },
                                                        { ClassMiningRequest.SubmitResult, calculationResult.ToString("F0") },
                                                        { ClassMiningRequest.SubmitFirstNumber, secondNumber },
                                                        { ClassMiningRequest.SubmitSecondNumber, firstNumber },
                                                        { ClassMiningRequest.SubmitOperator, ClassUtility.RandomOperatorCalculation[i] },
                                                        { ClassMiningRequest.SubmitShare, encryptedShare },
                                                        { ClassMiningRequest.SubmitHash, hashEncryptedShareForPool }
                                                    };
                                                    await ClassMiningNetwork.SendPacketToPoolAsync(share.ToString(Formatting.None)).ConfigureAwait(false);
                                                }
                                            }
                                        }
                                    }
                                }
                                #endregion
                            }
                        }
                        else // Test reverted
                        {
                            #region Test reverted calculation
                            calculation       = secondNumber + " " + ClassUtility.RandomOperatorCalculation[i] + " " + firstNumber;
                            calculationResult = ClassUtility.ComputeCalculation(secondNumber, ClassUtility.RandomOperatorCalculation[i], firstNumber);
                            if (calculationResult >= ClassMiningStats.CurrentMinRangeJob && calculationResult <= ClassMiningStats.CurrentBlockDifficulty)
                            {
                                if (calculationResult - Math.Round(calculationResult, 0) == 0) // Check if the result contain decimal places, if yes ignore it.
                                {
                                    string encryptedShare = MakeEncryptedShare(calculation, (idThread - 1));
                                    if (encryptedShare != ClassAlgoErrorEnumeration.AlgoError)
                                    {
                                        // Generate SHA512 hash for block hash indication.
                                        string hashEncryptedShare = ClassUtility.GenerateSHA512(encryptedShare);

                                        string hashEncryptedShareForPool = ClassUtility.EncryptXorShare(hashEncryptedShare, ClassMiningStats.CurrentJobKeyEncryption);
                                        hashEncryptedShareForPool = ClassUtility.HashJobToHexString(hashEncryptedShareForPool);

                                        if (ClassMiningStats.CurrentJobIndication.ContainsKey(hashEncryptedShareForPool) || hashEncryptedShare == ClassMiningStats.CurrentBlockIndication)
                                        {
                                            if (!DictionaryShareSubmittedCache.ContainsKey(hashEncryptedShareForPool))
                                            {
                                                try
                                                {
                                                    DictionaryShareSubmittedCache.Add(hashEncryptedShareForPool, calculation);
                                                }
                                                catch
                                                {
                                                }
                                                JObject share = new JObject
                                                {
                                                    { "type", ClassMiningRequest.TypeSubmit },
                                                    { ClassMiningRequest.SubmitResult, calculationResult.ToString("F0") },
                                                    { ClassMiningRequest.SubmitFirstNumber, secondNumber },
                                                    { ClassMiningRequest.SubmitSecondNumber, firstNumber },
                                                    { ClassMiningRequest.SubmitOperator, ClassUtility.RandomOperatorCalculation[i] },
                                                    { ClassMiningRequest.SubmitShare, encryptedShare },
                                                    { ClassMiningRequest.SubmitHash, hashEncryptedShareForPool }
                                                };
                                                await ClassMiningNetwork.SendPacketToPoolAsync(share.ToString(Formatting.None)).ConfigureAwait(false);
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                        }


                        #endregion
                    }
                }
            }

            ThreadMiningRunning = false;
        }