예제 #1
0
        public static void GenerateGetJobResponse(ref JObject response, string guid)
        {
            JObject job = new JObject();

            ConnectedWorker worker = Statics.ConnectedClients.First(x => x.Key == guid).Value;

            worker.LastSeen = DateTime.Now;

            /*if (worker.ShareDifficulty.Count >= 4)
             *  worker.LastDifficulty = Helpers.WorkerVardiffDifficulty(worker);  */


            Logger.Log(Logger.LogLevel.General, "Getwork request from {0}", guid);

            //result["id"] = guid;

            int seed = 0;

            if (worker.PendingDifficulty != worker.LastDifficulty || worker.CurrentBlock != Statics.CurrentBlockHeight)
            {
                worker.CurrentBlock   = Statics.CurrentBlockHeight;
                worker.LastDifficulty = worker.PendingDifficulty;
                job["blob"]           = Helpers.GenerateUniqueWork(ref seed);

                job["job_id"] = Guid.NewGuid().ToString();
                ShareJob shareJob = new ShareJob();
                shareJob.CurrentDifficulty = worker.LastDifficulty;
                shareJob.Seed = seed;
                worker.JobSeed.Add(new KeyValuePair <string, ShareJob>((string)job["job_id"], shareJob));


                if (worker.JobSeed.Count > int.Parse(Statics.Config.IniReadValue("max-concurrent-works")))
                {
                    worker.JobSeed.RemoveAt(0);
                }

                job["target"] =
                    BitConverter.ToString(
                        BitConverter.GetBytes(Helpers.GetTargetFromDifficulty((uint)shareJob.CurrentDifficulty)))
                    .Replace("-", "");
            }
            else
            {
                job["blob"]   = "";
                job["job_id"] = "";
                job["target"] = "";
            }
            response["result"] = job;

            MinerWorker minerWorker = Statics.RedisDb.MinerWorkers.First(x => x.Identifier == guid);

            minerWorker.NewJobRequest();
            Statics.RedisDb.SaveChanges(minerWorker);
            Statics.ConnectedClients[guid] = worker;
            Logger.Log(Logger.LogLevel.Verbose, "Finsihed getjob response");
        }
예제 #2
0
        public static double GetMinerWorkerHashRate(MinerWorker worker)
        {
            //don't covnert to dictionary, rare but as seen in testing time stamps may be same
            double time       = 0;
            double difficulty = 0;

            foreach (var shareDifficulty in worker.ShareDifficulty)
            {
                time       += shareDifficulty.Key.TotalSeconds;
                difficulty += shareDifficulty.Value;
            }
            return(GetHashRate(difficulty, (ulong)time));
        }
        public void SaveChanges(MinerWorker minerWorker)
        {
            SaveChanges <MinerWorker>(minerWorker);


            for (int i = 0; i < MinerWorkers.Count; i++)
            {
                if (MinerWorkers[i].Identifier == minerWorker.Identifier)
                {
                    MinerWorkers.RemoveAt(i);
                    MinerWorkers.Insert(i, minerWorker);
                    return;
                }
            }
            MinerWorkers.Add(minerWorker);
        }
예제 #4
0
 /// <summary>
 /// Remove a worker form the pool.
 /// </summary>
 /// <param name="worker"></param>
 public void Remove(MinerWorker worker)
 {
     Remove <MinerWorker>(worker);
     MinerWorkers.Remove(worker);
 }
예제 #5
0
        public void GenerateLoginResponse(ref JObject response, string guid, string address)
        {
            var result = new JObject();
            var job = new JObject();

            if (!Helpers.IsValidAddress(address, uint.Parse(Statics.Config.IniReadValue("base58-prefix"))))
            {
                result["error"] = "Invalid Address";
                return;
            }

            var worker = new ConnectedWorker();
            worker.Address = address;
            worker.LastSeen = DateTime.Now;
            worker.LastDifficulty = uint.Parse(Statics.Config.IniReadValue("base-difficulty"));
            worker.CurrentBlock = Statics.CurrentBlockHeight;

            Logger.Log(Logger.LogLevel.General, "Adding {0} to connected clients", guid);

            result["id"] = guid;

            var seed = 0;

            job["blob"] = Helpers.GenerateUniqueWork(ref seed);

            job["job_id"] = Guid.NewGuid().ToString();

            var shareJob = new ShareJob();
            shareJob.CurrentDifficulty = worker.LastDifficulty;
            shareJob.Seed = seed;
            worker.JobSeed.Add(new KeyValuePair<string, ShareJob>((string) job["job_id"], shareJob));

            job["target"] =
                BitConverter.ToString(
                        BitConverter.GetBytes(Helpers.GetTargetFromDifficulty((uint) shareJob.CurrentDifficulty)))
                    .Replace("-", "");

            Logger.Log(Logger.LogLevel.General, "Sending new work with target {0}", (string) job["target"]);

            result["job"] = job;
            result["status"] = "OK";

            response["result"] = result;

            worker.NewJobRequest();

            Program..ConnectedClients.Add(guid, worker);

            // Add a new client in the database.
            if (Program.RedisPoolDatabase.Miners.Any(x => x.Address == worker.Address))
            {
                var miner = Program.RedisPoolDatabase.Miners.First(x => x.Address == worker.Address);
                var minerWorker = new MinerWorker(guid, miner.Identifier, 0);
                minerWorker.NewJobRequest();
                miner.MinersWorker.Add(guid);
                Program.RedisPoolDatabase.SaveChanges(miner);
                Program.RedisPoolDatabase.SaveChanges(minerWorker);
            }
            else
            {
                var miner = new Miner(worker.Address, 0);
                var minerWorker = new MinerWorker(guid, miner.Identifier, 0);
                minerWorker.NewJobRequest();
                miner.MinersWorker.Add(guid);
                Program.RedisPoolDatabase.SaveChanges(miner);
                Program.RedisPoolDatabase.SaveChanges(minerWorker);
            }

            Logger.Log(Logger.LogLevel.Verbose, "Finished login response");
        }