public void SendSUBSCRIBE() { Byte[] bytesSent; StratumCommand Command = new StratumCommand(); Command.id = ID++; Command.method = "mining.subscribe"; Command.parameters = new ArrayList(); string request = Utilities.JsonSerialize(Command) + "\n"; bytesSent = Encoding.ASCII.GetBytes(request); try { tcpClient.GetStream().Write(bytesSent, 0, bytesSent.Length); PendingACKs.Add(Command.id, Command.method); } catch (Exception ex) { Console.WriteLine("Socket error:" + ex.Message); ConnectToServer(Server, Port, Username, Password); } Console.WriteLine("Sent mining.subscribe"); }
public void SendSUBMIT(string JobID, string nTime, string Nonce, int Difficulty) { StratumCommand Command = new StratumCommand(); Command.id = ID++; Command.method = "mining.submit"; Command.parameters = new ArrayList(); Command.parameters.Add(Username); Command.parameters.Add(JobID); Command.parameters.Add(ExtraNonce2.ToString("x8")); Command.parameters.Add(nTime); Command.parameters.Add(Nonce); string SubmitString = Utilities.JsonSerialize(Command) + "\n"; Byte[] bytesSent = Encoding.ASCII.GetBytes(SubmitString); try { tcpClient.GetStream().Write(bytesSent, 0, bytesSent.Length); PendingACKs.Add(Command.id, Command.method); } catch (Exception ex) { Console.WriteLine("Socket error:" + ex.Message); ConnectToServer(Server, Port, Username, Password); } SharesSubmitted++; Console.WriteLine("{0} - Submit (Difficulty {1})", DateTime.Now, Difficulty); Debug.WriteLine("[{0}] Submit (Difficulty {1}) : {2}", DateTime.Now, Difficulty, SubmitString); }
static void stratum_GotSetDifficulty(object sender, StratumEventArgs e) { StratumCommand Command = (StratumCommand)e.MiningEventArg; CurrentDifficulty = Convert.ToInt32(Command.parameters[0]); Console.WriteLine("Got Set_Difficulty " + CurrentDifficulty); }
static void stratum_GotNotify(object sender, StratumEventArgs e) { Job ThisJob = new Job(); StratumCommand Command = (StratumCommand)e.MiningEventArg; ThisJob.JobID = (string)Command.parameters[0]; ThisJob.PreviousHash = (string)Command.parameters[1]; ThisJob.Coinb1 = (string)Command.parameters[2]; ThisJob.Coinb2 = (string)Command.parameters[3]; Array a = (Array)Command.parameters[4]; ThisJob.Version = (string)Command.parameters[5]; ThisJob.NetworkDifficulty = (string)Command.parameters[6]; ThisJob.NetworkTime = (string)Command.parameters[7]; ThisJob.CleanJobs = (bool)Command.parameters[8]; ThisJob.MerkleNumbers = new string[a.Length]; int i = 0; foreach (string s in a) { ThisJob.MerkleNumbers[i++] = s; } // Cancel the existing mining threads and clear the queue if CleanJobs = true if (ThisJob.CleanJobs) { Console.WriteLine("Stratum detected a new block. Stopping old threads."); IncomingJobs.Clear(); CoinMiner.done = true; } // Add the new job to the queue IncomingJobs.Enqueue(ThisJob); }
// Callback for Read operation private void ReadCallback(IAsyncResult result) { NetworkStream networkStream; int bytesread; byte[] buffer = result.AsyncState as byte[]; try { networkStream = tcpClient.GetStream(); bytesread = networkStream.EndRead(result); } catch (Exception ex) { Console.WriteLine("Socket error:" + ex.Message); return; } if (bytesread == 0) { Console.WriteLine(DateTime.Now + " Disconnected. Reconnecting..."); Debug.WriteLine(DateTime.Now + " Disconnected. Reconnecting..."); tcpClient.Close(); tcpClient = null; PendingACKs.Clear(); ConnectToServer(Server, Port, Username, Password); return; } // Get the data string data = ASCIIEncoding.ASCII.GetString(buffer, 0, bytesread); Debug.WriteLine(data); page = page + data; int FoundClose = page.IndexOf('}'); while (FoundClose > 0) { string CurrentString = page.Substring(0, FoundClose + 1); // We can get either a command or response from the server. Try to deserialise both StratumCommand Command = Utilities.JsonDeserialize <StratumCommand>(CurrentString); StratumResponse Response = Utilities.JsonDeserialize <StratumResponse>(CurrentString); StratumEventArgs e = new StratumEventArgs(); if (Command.method != null) // We got a command { Debug.WriteLine(DateTime.Now + " Got Command: " + CurrentString); e.MiningEventArg = Command; switch (Command.method) { case "mining.notify": if (GotNotify != null) { GotNotify(this, e); } break; case "mining.set_difficulty": if (GotSetDifficulty != null) { GotSetDifficulty(this, e); } break; } } else if (Response.error != null || Response.result != null) // We got a response { Debug.WriteLine(DateTime.Now + " Got Response: " + CurrentString); e.MiningEventArg = Response; // Find the command that this is the response to and remove it from the list of commands that we're waiting on a response to string Cmd = (string)PendingACKs[Response.id]; PendingACKs.Remove(Response.id); if (Cmd == null) { Console.WriteLine("Unexpected Response"); } else if (GotResponse != null) { GotResponse(Cmd, e); } } page = page.Remove(0, FoundClose + 2); FoundClose = page.IndexOf('}'); } // Then start reading from the network again. networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer); }