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"); }
public void GenerateLoginResponse(ref JObject response, string guid, string address) { JObject result = new JObject(); JObject job = new JObject(); if (!Helpers.IsValidAddress(address, uint.Parse(Statics.Config.IniReadValue("base58-prefix")))) { result["error"] = "Invalid Address"; return; } ConnectedWorker 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; int seed = 0; 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)); 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(); Statics.ConnectedClients.Add(guid, worker); //Initialize new client in DB if (Statics.RedisDb.Miners.Any(x => x.Address == worker.Address)) { Miner miner = Statics.RedisDb.Miners.First(x => x.Address == worker.Address); MinerWorker minerWorker = new MinerWorker(guid, miner.Identifier, 0); minerWorker.NewJobRequest(); miner.MinersWorker.Add(guid); Statics.RedisDb.SaveChanges(miner); Statics.RedisDb.SaveChanges(minerWorker); } else { Miner miner = new Miner(worker.Address, 0); MinerWorker minerWorker = new MinerWorker(guid, miner.Identifier, 0); minerWorker.NewJobRequest(); miner.MinersWorker.Add(guid); Statics.RedisDb.SaveChanges(miner); Statics.RedisDb.SaveChanges(minerWorker); } Logger.Log(Logger.LogLevel.Verbose, "Finished login response"); }