/// <summary> /// Method for ask and update mining methods. /// </summary> private static void AskMiningMethod() { if (ThreadAskMiningMethod != null && (ThreadAskMiningMethod.IsAlive || ThreadAskMiningMethod != null)) { ThreadAskMiningMethod.Abort(); GC.SuppressFinalize(ThreadAskMiningMethod); } ThreadAskMiningMethod = new Thread(async delegate() { while (IsConnected && !Program.Exit) { if (!await classSeedNodeConnector.SendPacketToSeedNodeAsync(ClassSoloMiningPacketEnumeration.SoloMiningSendPacketEnumeration.ReceiveAskListBlockMethod, Program.Certificate, false, true).ConfigureAwait(false)) { IsConnected = false; break; } Thread.Sleep(1000); if (!await classSeedNodeConnector.SendPacketToSeedNodeAsync(ClassSoloMiningPacketEnumeration.SoloMiningSendPacketEnumeration.ReceiveAskCurrentBlockMining, Program.Certificate, false, true).ConfigureAwait(false)) { IsConnected = false; break; } Thread.Sleep(1000); } }); ThreadAskMiningMethod.Start(); }
/// <summary> /// Send packet to the blockchain network, respecting encryption of seed nodes. /// </summary> /// <param name="packet"></param> /// <returns></returns> public async Task <bool> SendPacketBlockchainNetworkSeedNodeMode(string packet) { if (!await SeedNodeConnector.SendPacketToSeedNodeAsync(packet, CertificateConnection, false, true)) { WalletCreateResult = ClassWalletCreatorEnumeration.WalletCreatorError; return(false); } return(true); }
/// <summary> /// Send packet from wallet to seed nodes. /// </summary> /// <param name="packet"></param> /// <param name="certificate"></param> /// <param name="isEncrypted"></param> public async Task <bool> SendPacketWallet(string packet, string certificate, bool isEncrypted) { if (WalletPhase == ClassConnectorSettingEnumeration.WalletCreateType || WalletPhase == string.Empty) // Not allow to create a wallet on non-permanent seed nodes. { if (ClassConnectorSetting.SeedNodeIp.ContainsKey(_seedNodeConnector.ReturnCurrentSeedNodeHost())) { if (!ClassConnectorSetting.SeedNodeIp[_seedNodeConnector.ReturnCurrentSeedNodeHost()].Item2) { return(false); } } else { return(false); } } return(await _seedNodeConnector.SendPacketToSeedNodeAsync(EncryptPacketWallet(packet), certificate, false, isEncrypted)); }
/// <summary> /// Send packet from wallet to seed nodes. /// </summary> /// <param name="packet"></param> /// <param name="certificate"></param> /// <param name="isEncrypted"></param> public async Task <bool> SendPacketWallet(string packet, string certificate, bool isEncrypted) { return(await _seedNodeConnector.SendPacketToSeedNodeAsync(EncryptPacketWallet(packet), certificate, false, isEncrypted)); }
/// <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); }