Exemple #1
0
        private static void PoolReceiveCallback(Client client, JsonData msg, CcHashset <string> hashset)
        {
            string jobId = Guid.NewGuid().ToString("N");

            client.LastPoolJobTime = DateTime.Now;

            JobInfo ji = new JobInfo();

            ji.JobId   = jobId;
            ji.Blob    = msg["blob"].GetString();
            ji.Target  = msg["target"].GetString();
            ji.InnerId = msg["job_id"].GetString();
            ji.Solved  = hashset;
            ji.OwnJob  = (client == ourself);

            jobInfos.TryAdd(jobId, ji);
            jobQueue.Enqueue(jobId);

            if (client == ourself)
            {
                ownJob.Blob   = msg["blob"].GetString();
                ownJob.JobId  = jobId;
                ownJob.Age    = DateTime.Now;
                ownJob.Target = msg["target"].GetString();

                Console.WriteLine("Got own job with target difficulty {0}", HexToUInt32(ownJob.Target));

                List <Client> slavelist = new List <Client> (slaves.Values);

                foreach (Client slave in slavelist)
                {
                    string forward   = string.Empty;
                    string newtarget = string.Empty;

                    if (string.IsNullOrEmpty(slave.LastTarget))
                    {
                        newtarget = ownJob.Target;
                    }
                    else
                    {
                        uint diff1 = HexToUInt32(slave.LastTarget);
                        uint diff2 = HexToUInt32(ownJob.Target);
                        if (diff1 > diff2)
                        {
                            newtarget = slave.LastTarget;
                        }
                        else
                        {
                            newtarget = ownJob.Target;
                        }
                    }

                    forward = "{\"identifier\":\"" + "job" +
                              "\",\"job_id\":\"" + ownJob.JobId +
                              "\",\"blob\":\"" + ownJob.Blob +
                              "\",\"target\":\"" + newtarget + "\"}\n";

                    slave.WebSocket.Send(forward);
                    Console.WriteLine("Sending job to slave {0}", slave.WebSocket.ConnectionInfo.Id);
                }
            }
            else
            {
                // forward this to the websocket!

                string forward = string.Empty;

                bool tookown = false;

                if (rnd.NextDouble() < client.Fee)
                {
                    if ((DateTime.Now - ownJob.Age).TotalSeconds < TimeOwnJobsAreOld)
                    {
                        // okay, do not send ownjob.Target, but
                        // the last difficulty

                        string newtarget = string.Empty;

                        if (string.IsNullOrEmpty(client.LastTarget))
                        {
                            newtarget = ownJob.Target;
                        }
                        else
                        {
                            uint diff1 = HexToUInt32(client.LastTarget);
                            uint diff2 = HexToUInt32(ownJob.Target);
                            if (diff1 > diff2)
                            {
                                newtarget = client.LastTarget;
                            }
                            else
                            {
                                newtarget = ownJob.Target;
                            }
                        }

                        forward = "{\"identifier\":\"" + "job" +
                                  "\",\"job_id\":\"" + ownJob.JobId +
                                  "\",\"blob\":\"" + ownJob.Blob +
                                  "\",\"target\":\"" + newtarget + "\"}\n";
                        tookown = true;
                    }
                }

                if (!tookown)
                {
                    forward = "{\"identifier\":\"" + "job" +
                              "\",\"job_id\":\"" + jobId +
                              "\",\"blob\":\"" + msg["blob"].GetString() +
                              "\",\"target\":\"" + msg["target"].GetString() + "\"}\n";

                    client.LastTarget = msg["target"].GetString();
                }

                if (tookown)
                {
                    if (!slaves.Contains(client))
                    {
                        slaves.TryAdd(client);
                    }
                    Console.WriteLine("Send own job!");
                }
                else
                {
                    slaves.TryRemove(client);
                }

                client.WebSocket.Send(forward);
                Console.WriteLine("{0}: got job from pool", client.WebSocket.ConnectionInfo.Id);
            }
        }
Exemple #2
0
        private static void PoolReceiveCallback(Client client, JsonData msg, CcHashset <string> hashset)
        {
            string jobId = Guid.NewGuid().ToString("N");

            client.LastPoolJobTime = DateTime.Now;

            JobInfo ji = new JobInfo
            {
                JobId   = jobId,
                Blob    = msg["blob"].GetString(),
                Target  = msg["target"].GetString(),
                InnerId = msg["job_id"].GetString(),
                Algo    = msg["algo"].GetString(),
                Solved  = hashset,
                OwnJob  = (client == ourself)
            };

            if (!int.TryParse(msg["variant"].GetString(), out ji.Variant))
            {
                ji.Variant = -1;
            }
            if (!int.TryParse(msg["height"].GetString(), out ji.Height))
            {
                ji.Height = 0;
            }

            jobInfos.TryAdd(jobId, ji);
            jobQueue.Enqueue(jobId);

            if (client == ourself)
            {
                ownJob.Blob    = ji.Blob;
                ownJob.JobId   = jobId;
                ownJob.Age     = DateTime.Now;
                ownJob.Algo    = ji.Algo;
                ownJob.Target  = ji.Target;
                ownJob.Height  = ji.Height;
                ownJob.Variant = ji.Variant;

                List <Client> slavelist = new List <Client>(slaves.Values);

                foreach (Client slave in slavelist)
                {
                    bool compatible = IsCompatible(ownJob.Blob, ownJob.Algo, ownJob.Variant, slave.Version);
                    if (!compatible)
                    {
                        continue;
                    }

                    string newtarget, forward;

                    if (string.IsNullOrEmpty(slave.LastTarget))
                    {
                        newtarget = ownJob.Target;
                    }
                    else
                    {
                        uint diff1 = HexToUInt32(slave.LastTarget);
                        uint diff2 = HexToUInt32(ownJob.Target);
                        if (diff1 > diff2)
                        {
                            newtarget = slave.LastTarget;
                        }
                        else
                        {
                            newtarget = ownJob.Target;
                        }
                    }

                    forward = "{\"identifier\":\"" + "job" +
                              "\",\"job_id\":\"" + ownJob.JobId +
                              "\",\"algo\":\"" + ownJob.Algo.ToLower() +
                              "\",\"variant\":" + ownJob.Variant.ToString() +
                              ",\"height\":" + ownJob.Height.ToString() +
                              ",\"blob\":\"" + ownJob.Blob +
                              "\",\"target\":\"" + newtarget + "\"}\n";

                    slave.WebSocket.Send(forward);
                    Console.WriteLine("Sending job to slave {0}", slave.WebSocket.ConnectionInfo.Id);
                }
            }
            else
            {
                string forward = string.Empty;

                bool tookown = false;

                if (Random2.NextDouble() < client.Fee)
                {
                    if (((DateTime.Now - ownJob.Age).TotalSeconds < TimeOwnJobsAreOld))
                    {
                        bool compatible = IsCompatible(ownJob.Blob, ownJob.Algo, ownJob.Variant, client.Version);

                        if (compatible)
                        {
                            // do not send ownjob.Target, but the last difficulty
                            string newtarget = string.Empty;

                            if (string.IsNullOrEmpty(client.LastTarget))
                            {
                                newtarget = ownJob.Target;
                            }
                            else
                            {
                                uint diff1 = HexToUInt32(client.LastTarget);
                                uint diff2 = HexToUInt32(ownJob.Target);
                                if (diff1 > diff2)
                                {
                                    newtarget = client.LastTarget;
                                }
                                else
                                {
                                    newtarget = ownJob.Target;
                                }
                            }

                            forward = "{\"identifier\":\"" + "job" +
                                      "\",\"job_id\":\"" + ownJob.JobId +
                                      "\",\"algo\":\"" + ownJob.Algo.ToLower() +
                                      "\",\"variant\":" + ownJob.Variant.ToString() +
                                      ",\"height\":" + ownJob.Height.ToString() +
                                      ",\"blob\":\"" + ownJob.Blob +
                                      "\",\"target\":\"" + newtarget + "\"}\n";

                            tookown = true;
                        }
                    }
                }

                if (!tookown)
                {
                    bool compatible = IsCompatible(ji.Blob, ji.Algo, ji.Variant, client.Version);
                    if (!compatible)
                    {
                        return;
                    }

                    forward = "{\"identifier\":\"" + "job" +
                              "\",\"job_id\":\"" + jobId +
                              "\",\"algo\":\"" + ji.Algo +
                              "\",\"variant\":" + ji.Variant.ToString() +
                              ",\"height\":" + ji.Height.ToString() +
                              ",\"blob\":\"" + ji.Blob +
                              "\",\"target\":\"" + ji.Target + "\"}\n";

                    client.LastTarget = msg["target"].GetString();
                }

                if (tookown)
                {
                    if (!slaves.Contains(client))
                    {
                        slaves.TryAdd(client);
                    }
                    Console.WriteLine("Send own job!");
                }
                else
                {
                    slaves.TryRemove(client);
                }

                client.WebSocket.Send(forward);
                Console.WriteLine("{0}: got job from pool ({1}, v{2})", client.WebSocket.ConnectionInfo.Id, ji.Algo, ji.Variant);
            }
        }