Esempio n. 1
0
        public static PoolList LoadFromFile(string filename)
        {
            PoolList pl = new PoolList();

            pl.pools = new Dictionary <string, PoolInfo>();

            string json = File.ReadAllText(filename);

            JsonData data = json.FromJson <JsonData>();

            foreach (string pool in data.Keys)
            {
                JsonData jinfo = data[pool] as JsonData;
                PoolInfo pi    = new PoolInfo();

                if (!(jinfo.ContainsKey("url") && jinfo.ContainsKey("port") &&
                      jinfo.ContainsKey("emptypassword") && jinfo.ContainsKey("algorithm")))
                {
                    throw new Exception("Invalid entry.");
                }

                pi.Url           = jinfo["url"].GetString();
                pi.EmptyPassword = jinfo["emptypassword"].GetString();

                pi.DefaultAlgorithm = jinfo["algorithm"].GetString();
                pi.Port             = int.Parse(jinfo["port"].GetString());

                if (!AlgorithmHelper.ValidAlgorithm(pi.DefaultAlgorithm))
                {
                    throw new Exception("Invalid algorithm found in pools.json: " + pi.DefaultAlgorithm);
                }

                pl.pools.Add(pool, pi);
            }

            int counter = 0;

            pl.JsonPools = "{\"identifier\":\"" + "poolinfo";

            foreach (string pool in pl.pools.Keys)
            {
                counter++;
                pl.JsonPools += "\",\"pool" + counter.ToString() + "\":\"" + pool;
            }

            pl.JsonPools += "\"}\n";

            return(pl);
        }
Esempio n. 2
0
        private static void ReceiveCallback(IAsyncResult result)
        {
            PoolConnection mypc   = result.AsyncState as PoolConnection;
            TcpClient      client = mypc.TcpClient;

            if (mypc.Closed || !client.Connected)
            {
                return;
            }

            NetworkStream networkStream;

            try { networkStream = client.GetStream(); } catch { return; }

            int bytesread = 0;

            try { bytesread = networkStream.EndRead(result); } catch { return; }

            string json = string.Empty;

            try {
                if (bytesread == 0)                 // disconnected
                {
                    // slow that down a bit to avoid negative feedback loop

                    Task.Run(async delegate {
                        await Task.Delay(TimeSpan.FromSeconds(4));

                        List <Client> cllist = new List <Client> (mypc.WebClients.Values);
                        foreach (Client ev in cllist)
                        {
                            Disconnect(ev, "lost pool connection.");
                        }
                    });

                    return;
                }

                json = Encoding.ASCII.GetString(mypc.ReceiveBuffer, 0, bytesread);

                networkStream.BeginRead(mypc.ReceiveBuffer, 0, mypc.ReceiveBuffer.Length, new AsyncCallback(ReceiveCallback), mypc);
            } catch { return; }

            if (bytesread == 0 || string.IsNullOrEmpty(json))
            {
                return;                                                            //?!
            }
            var msg = json.FromJson <JsonData> ();

            if (msg == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(mypc.PoolId))
            {
                // this "protocol" is strange
                if (!msg.ContainsKey("result"))
                {
                    string additionalInfo = "none";

                    // try to get the error
                    if (msg.ContainsKey("error"))
                    {
                        msg = msg["error"] as JsonData;

                        if (msg != null && msg.ContainsKey("message"))
                        {
                            additionalInfo = msg["message"].GetString();
                        }
                    }

                    List <Client> cllist = new List <Client> (mypc.WebClients.Values);
                    foreach (Client ev in cllist)
                    {
                        Disconnect(ev, "can not connect. additional information: " + additionalInfo);
                    }

                    return;
                }

                msg = msg["result"] as JsonData;

                if (msg == null)
                {
                    return;
                }
                if (!msg.ContainsKey("id"))
                {
                    return;
                }
                if (!msg.ContainsKey("job"))
                {
                    return;
                }

                mypc.PoolId = msg["id"].GetString();

                var lastjob = msg["job"] as JsonData;

                if (!VerifyJob(lastjob))
                {
                    CConsole.ColorWarning(() =>
                                          Console.WriteLine("Failed to verify job: {0}", json));
                    return;
                }

                // extended stratum
                if (!lastjob.ContainsKey("variant"))
                {
                    lastjob.Add("variant", mypc.DefaultVariant);
                }
                if (!lastjob.ContainsKey("algo"))
                {
                    lastjob.Add("algo", mypc.DefaultAlgorithm);
                }
                AlgorithmHelper.NormalizeAlgorithmAndVariant(lastjob);

                mypc.LastJob         = lastjob;
                mypc.LastInteraction = DateTime.Now;

                mypc.LastSolved = new CcHashset <string> ();

                List <Client> cllist2 = new List <Client> (mypc.WebClients.Values);
                foreach (Client ev in cllist2)
                {
                    ReceiveJob(ev, mypc.LastJob, mypc.LastSolved);
                }
            }
            else if (msg.ContainsKey("method") && msg["method"].GetString() == "job")
            {
                if (!msg.ContainsKey("params"))
                {
                    return;
                }

                var lastjob = msg["params"] as JsonData;

                if (!VerifyJob(lastjob))
                {
                    CConsole.ColorWarning(() =>
                                          Console.WriteLine("Failed to verify job: {0}", json));
                    return;
                }

                // extended stratum
                if (!lastjob.ContainsKey("variant"))
                {
                    lastjob.Add("variant", mypc.DefaultVariant);
                }
                if (!lastjob.ContainsKey("algo"))
                {
                    lastjob.Add("algo", mypc.DefaultAlgorithm);
                }
                AlgorithmHelper.NormalizeAlgorithmAndVariant(lastjob);

                mypc.LastJob         = lastjob;
                mypc.LastInteraction = DateTime.Now;
                mypc.LastSolved      = new CcHashset <string> ();

                List <Client> cllist2 = new List <Client> (mypc.WebClients.Values);

                Console.WriteLine("Sending job to {0} client(s)!", cllist2.Count);

                foreach (Client ev in cllist2)
                {
                    ReceiveJob(ev, mypc.LastJob, mypc.LastSolved);
                }
            }
            else
            {
                if (msg.ContainsKey("error"))
                {
                    // who knows?
                    ReceiveError(mypc.LastSender, msg);
                }
                else
                {
                    CConsole.ColorWarning(() =>
                                          Console.WriteLine("Pool is sending nonsense."));
                }
            }
        }