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_GotResponse(object sender, StratumEventArgs e) { StratumResponse Response = (StratumResponse)e.MiningEventArg; Console.Write("Got Response to {0} - ", (string)sender); switch ((string)sender) { case "mining.authorize": if ((bool)Response.result) { Console.WriteLine("Worker authorized"); } else { Console.WriteLine("Worker rejected"); Console.ReadLine(); } break; case "mining.subscribe": stratum.ExtraNonce1 = (string)((object[])Response.result)[1]; Console.WriteLine("Subscribed. ExtraNonce1 set to " + stratum.ExtraNonce1); break; case "mining.submit": if (Response.result != null && (bool)Response.result) { SharesAccepted++; Console.WriteLine("Share accepted ({0} of {1})", SharesAccepted, SharesSubmitted); } else { Console.WriteLine("Share rejected. {0}", Response.error[1]); } break; } }
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); }