Esempio n. 1
0
        private static void CheckBlockchainConnection()
        {
            _lastPacketReceivedFromBlockchain = DateTimeOffset.Now.ToUnixTimeSeconds();
            var threadCheckConnection = new Thread(async delegate()
            {
                while (true)
                {
                    Thread.Sleep(1000);
                    if (!IsConnected || !classSeedNodeConnector.ReturnStatus())
                    {
                        if (ThreadListenBlockchain != null && (ThreadListenBlockchain.IsAlive || ThreadListenBlockchain != null))
                        {
                            ThreadListenBlockchain.Abort();
                            GC.SuppressFinalize(ThreadListenBlockchain);
                        }
                        if (ThreadAskMiningMethod != null && (ThreadAskMiningMethod.IsAlive || ThreadAskMiningMethod != null))
                        {
                            ThreadAskMiningMethod.Abort();
                            GC.SuppressFinalize(ThreadAskMiningMethod);
                        }
                        IsConnected   = false;
                        LoginAccepted = false;
                        NetworkProxy.StopProxy();
                        while (!await ConnectToBlockchainAsync())
                        {
                            ConsoleLog.WriteLine("Can't connect to the network, retry in 5 seconds..", ClassConsoleColorEnumeration.IndexConsoleRedLog);
                            Thread.Sleep(5000);
                        }
                        ConsoleLog.WriteLine("Connection success, generate dynamic certificate for the network.", ClassConsoleColorEnumeration.IndexConsoleGreenLog);
                        Program.NetworkCertificate = ClassUtils.GenerateCertificate();
                        ConsoleLog.WriteLine("Certificate generate, send to the network..", ClassConsoleColorEnumeration.IndexConsoleYellowLog);
                        if (!await SendPacketAsync(Program.NetworkCertificate, false))
                        {
                            ConsoleLog.WriteLine("Can't send certificate, reconnect now..", ClassConsoleColorEnumeration.IndexConsoleRedLog);
                            IsConnected = false;
                        }
                        else
                        {
                            Thread.Sleep(1000);
                            ConsoleLog.WriteLine("Certificate sent, start to login..", ClassConsoleColorEnumeration.IndexConsoleYellowLog);
                            ListenBlockchain();
                            if (!await SendPacketAsync(ClassConnectorSettingEnumeration.MinerLoginType + "|" + Config.WalletAddress, true))
                            {
                                ConsoleLog.WriteLine("Can't login to the network, reconnect now.", ClassConsoleColorEnumeration.IndexConsoleRedLog);
                                IsConnected = false;
                            }
                            else
                            {
                                ConsoleLog.WriteLine("Login successfully sent, waiting confirmation.. (Wait 5 seconds maximum.)", ClassConsoleColorEnumeration.IndexConsoleYellowLog);
                                IsConnected = true;
                                Thread.Sleep(ClassConnectorSetting.MaxTimeoutConnect);
                                if (!LoginAccepted)
                                {
                                    IsConnected = false;
                                }
                            }
                        }
                    }
                }
            });

            threadCheckConnection.Start();
        }
Esempio n. 2
0
        /// <summary>
        /// Receive packet from the blockchain.
        /// </summary>
        /// <param name="packet"></param>
        private static async Task <bool> HandlePacketBlockchainAsync(string packet)
        {
            var splitPacket = packet.Split(new[] { "|" }, StringSplitOptions.None);

            switch (splitPacket[0])
            {
            case ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.SendLoginAccepted:
                LoginAccepted = true;
                IsConnected   = true;
                ConsoleLog.WriteLine("Proxy login accepted, ask mining methods.", ClassConsoleColorEnumeration.IndexConsoleGreenLog);
                if (!NetworkProxy.ProxyStarted)
                {
                    NetworkProxy.StartProxy();
                }
                AskMiningMethod();
                break;

            case ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.SendListBlockMethod:
                var methodList = splitPacket[1];
                if (methodList.Contains("#"))
                {
                    var splitMethodList = methodList.Split(new[] { "#" }, StringSplitOptions.None);
                    if (ListOfMiningMethodName.Count > 1)
                    {
                        foreach (var methodName in splitMethodList)
                        {
                            if (!string.IsNullOrEmpty(methodName))
                            {
                                if (ListOfMiningMethodName.Contains(methodName) == false)
                                {
                                    ListOfMiningMethodName.Add(methodName);
                                }
                                if (!await classSeedNodeConnector.SendPacketToSeedNodeAsync(ClassSoloMiningPacketEnumeration.SoloMiningSendPacketEnumeration.ReceiveAskContentBlockMethod + "|" + methodName, Program.NetworkCertificate, false, true).ConfigureAwait(false))
                                {
                                    return(false);
                                }
                                await Task.Delay(1000);
                            }
                        }
                    }
                    else
                    {
                        foreach (var methodName in splitMethodList)
                        {
                            if (!string.IsNullOrEmpty(methodName))
                            {
                                if (ListOfMiningMethodName.Contains(methodName) == false)
                                {
                                    ListOfMiningMethodName.Add(methodName);
                                }
                                if (!await classSeedNodeConnector.SendPacketToSeedNodeAsync(ClassSoloMiningPacketEnumeration.SoloMiningSendPacketEnumeration.ReceiveAskContentBlockMethod + "|" + methodName, Program.NetworkCertificate, false, true).ConfigureAwait(false))
                                {
                                    return(false);
                                }
                                await Task.Delay(1000);
                            }
                        }
                    }
                }
                else
                {
                    if (ListOfMiningMethodName.Contains(methodList) == false)
                    {
                        ListOfMiningMethodName.Add(methodList);
                    }
                    if (!await classSeedNodeConnector.SendPacketToSeedNodeAsync(ClassSoloMiningPacketEnumeration.SoloMiningSendPacketEnumeration.ReceiveAskContentBlockMethod + "|" + methodList, Program.NetworkCertificate, false, true).ConfigureAwait(false))
                    {
                        return(false);
                    }
                }
                break;

            case ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.SendContentBlockMethod:
                if (ListOfMiningMethodContent.Count == 0)
                {
                    ListOfMiningMethodContent.Add(splitPacket[1]);
                }
                else
                {
                    ListOfMiningMethodContent[0] = splitPacket[1];
                }
                break;

            case ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.SendCurrentBlockMining:
                var splitBlockContent = splitPacket[1].Split(new[] { "&" }, StringSplitOptions.None);

                if (Blocktemplate != splitPacket[1])
                {
                    ConsoleLog.WriteLine("New block to mining: " + splitBlockContent[0], ClassConsoleColorEnumeration.IndexConsoleYellowLog);
                    Blocktemplate = splitPacket[1];
                    await Task.Factory.StartNew(() => SpreadJobAsync(), CancellationToken.None, TaskCreationOptions.RunContinuationsAsynchronously, TaskScheduler.Current).ConfigureAwait(false);
                }
                break;

            case ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.SendJobStatus:
                switch (splitPacket[1])
                {
                case ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.ShareUnlock:
                    TotalBlockUnlocked++;
                    ConsoleLog.WriteLine("Block accepted, stop mining, wait new block.", ClassConsoleColorEnumeration.IndexConsoleGreenLog);
                    for (int i = 0; i < NetworkProxy.ListOfMiners.Count; i++)
                    {
                        if (i < NetworkProxy.ListOfMiners.Count)
                        {
                            if (NetworkProxy.ListOfMiners[i].MinerConnected)
                            {
                                if (!await NetworkProxy.ListOfMiners[i].SendPacketAsync(ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.SendJobStatus + "|" + ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.ShareUnlock).ConfigureAwait(false))
                                {
                                    NetworkProxy.ListOfMiners[i].DisconnectMiner();
                                }
                            }
                        }
                    }
                    break;

                case ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.ShareWrong:
                    for (int i = 0; i < NetworkProxy.ListOfMiners.Count; i++)
                    {
                        if (i < NetworkProxy.ListOfMiners.Count)
                        {
                            if (NetworkProxy.ListOfMiners[i].MinerConnected)
                            {
                                if (!await NetworkProxy.ListOfMiners[i].SendPacketAsync(ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.SendJobStatus + "|" + ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.ShareWrong).ConfigureAwait(false))
                                {
                                    NetworkProxy.ListOfMiners[i].DisconnectMiner();
                                }
                            }
                        }
                    }
                    TotalBlockWrong++;
                    ConsoleLog.WriteLine("Block not accepted, stop mining, wait new block.", ClassConsoleColorEnumeration.IndexConsoleRedLog);
                    break;

                case ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.ShareBad:
                    for (int i = 0; i < NetworkProxy.ListOfMiners.Count; i++)
                    {
                        if (i < NetworkProxy.ListOfMiners.Count)
                        {
                            if (NetworkProxy.ListOfMiners[i].MinerConnected)
                            {
                                if (!await NetworkProxy.ListOfMiners[i].SendPacketAsync(ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.SendJobStatus + "|" + ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.ShareBad).ConfigureAwait(false))
                                {
                                    NetworkProxy.ListOfMiners[i].DisconnectMiner();
                                }
                            }
                        }
                    }
                    TotalBlockWrong++;
                    ConsoleLog.WriteLine("Block not accepted, stop mining, wait new block.", ClassConsoleColorEnumeration.IndexConsoleRedLog);
                    break;

                case ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.ShareAleady:
                    for (int i = 0; i < NetworkProxy.ListOfMiners.Count; i++)
                    {
                        if (i < NetworkProxy.ListOfMiners.Count)
                        {
                            if (NetworkProxy.ListOfMiners[i].MinerConnected)
                            {
                                if (!await NetworkProxy.ListOfMiners[i].SendPacketAsync(ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.SendJobStatus + "|" + ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.ShareBad).ConfigureAwait(false))
                                {
                                    NetworkProxy.ListOfMiners[i].DisconnectMiner();
                                }
                            }
                        }
                    }
                    ConsoleLog.WriteLine("Block already mined, stop mining, wait new block.", ClassConsoleColorEnumeration.IndexConsoleRedLog);
                    break;

                case ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.ShareNotExist:
                    for (int i = 0; i < NetworkProxy.ListOfMiners.Count; i++)
                    {
                        if (i < NetworkProxy.ListOfMiners.Count)
                        {
                            if (NetworkProxy.ListOfMiners[i].MinerConnected)
                            {
                                if (!await NetworkProxy.ListOfMiners[i].SendPacketAsync(ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.SendJobStatus + "|" + ClassSoloMiningPacketEnumeration.SoloMiningRecvPacketEnumeration.ShareNotExist).ConfigureAwait(false))
                                {
                                    NetworkProxy.ListOfMiners[i].DisconnectMiner();
                                }
                            }
                        }
                    }
                    ConsoleLog.WriteLine("Block mined not exist, stop mining, wait new block.", ClassConsoleColorEnumeration.IndexConsoleBlueLog);
                    break;
                }
                break;
            }
            return(true);
        }