public void MinerDataReceived(byte[] data, TcpConnection connection)
        {
            Log.Verbose("{0} sent: {1}", connection.endPoint, data.GetString());

            Miner miner = _minerManager.GetMiner(connection);

            int    id         = -999;
            string jsonMethod = "";

            foreach (string s in data.GetString().Split('\r', '\n'))
            {
                if (s.Length <= 1)
                {
                    continue;
                }

                dynamic dyn = new object();

                try { dyn = JsonConvert.DeserializeObject(s.TrimNewLine()); } catch (Exception ex) { Log.Error(ex, "DeserializeObject Json error: " + s); continue; }

                if (Helpers.JsonHelper.DoesJsonObjectExist(dyn.id))
                {
                    id = (int)dyn.id;
                }

                if (JsonHelper.DoesJsonObjectExist(dyn.method))
                {
                    jsonMethod = dyn.method;

                    if (miner == null && !jsonMethod.ToLower().Contains("login"))
                    {
                        Log.Verbose("{0}: Miner does not exist; disconnecting..", connection.endPoint);
                        _minerServer.DisconnectConnection(connection);
                        return;
                    }

                    switch (jsonMethod.ToLower())
                    {
                    case "eth_getwork":
                        Log.Verbose("{0} requested work.", miner.workerIdentifier);

                        _minerManager.AddMinerID(miner, id);

                        if (_pool.currentPoolWork.Length > 0)
                        {
                            dyn.id = miner.minerID;
                            dynamic currentWork = _pool.currentPoolWorkDynamic;
                            currentWork.id = miner.minerID;
                            _minerServer.SendToMiner(JsonConvert.SerializeObject(currentWork), miner.connection);
                        }
                        else
                        {
                            _pool.DoPoolGetWork();
                        }
                        break;

                    case "eth_submitwork":
                        Log.Verbose("{0} found a share!", miner.workerIdentifier);
                        _pool.SubmitShareToPool(s.GetBytes(), miner);
                        break;

                    case "eth_submithashrate":
                        string hash     = dyn.@params[0];
                        long   hashrate = Convert.ToInt64(hash, 16);
                        _minerManager.UpdateMinerHashrate(hashrate, miner);
                        Log.Debug("{0} sent hashrate: {1}", miner.workerIdentifier, hashrate.ToString("#,##0,Mh/s").Replace(",", "."));
                        break;

                    case "eth_login":     // DevFee only?
                    case "eth_submitlogin":

                        string worker = dyn.@params[0];

                        if (worker.Contains("."))
                        {
                            worker = worker.Split(".")[1];
                        }
                        else if (worker != _pool.poolWallet)
                        {
                            worker = "DevFee";
                        }
                        else
                        {
                            worker = connection.endPoint.ToString();
                        }

                        miner = new Miner(worker, connection);

                        _minerManager.AddMiner(miner);
                        Log.Information("[{0}] new miner: {1}!", _pool.poolWorkerName, miner.workerIdentifier);
                        _minerServer.SendToMiner("{\"id\":" + id + ",\"jsonrpc\":\"2.0\",\"result\":true}", connection);
                        //_minerManager.AddMinerId(miner, id);

                        break;

                    default:
                        Log.Warning("MinerHandler Method Unhandled {0} ", s);
                        _pool.SendToPool(s.GetBytes());
                        break;
                    }

                    continue;
                }

                if (id > -999)
                {
                    switch (id)
                    {
                    default:
                        Log.Warning("MinerHandler id Unhandled {0} ", s);
                        _pool.SendToPool(s.GetBytes());
                        break;
                    }
                }
            }
        }
Esempio n. 2
0
        public void MinerDataReceived(byte[] data, TcpConnection connection)
        {
            Log.Verbose("{0} sent: {1}", connection.endPoint, data.GetString());

            Miner miner = _minerManager.GetMiner(connection);

            if (miner == null)
            {
                Log.Verbose("{0}: Miner does not exist.", connection.endPoint);
            }

            string minerData  = data.GetString();
            int    id         = -999;
            string jsonMethod = "";

            if (minerData.Length <= 1)
            {
                return;
            }

            dynamic dyn = new object();

            try { dyn = JsonConvert.DeserializeObject(minerData.TrimNewLine()); } catch (Exception ex) { Log.Error(ex, "DeserializeObject Json error"); return; }

            if (Helpers.JsonHelper.DoesJsonObjectExist(dyn.id))
            {
                id = (int)dyn.id;
            }

            if (JsonHelper.DoesJsonObjectExist(dyn.method))
            {
                jsonMethod = dyn.method;

                switch (jsonMethod.ToLower())
                {
                case "login":
                    string worker = [email protected];

                    if (worker.Contains("."))
                    {
                        worker = worker.Split(".")[1];
                    }
                    else if (!_pool.poolWallet.Contains(worker))        // if our wallet address isn't part of the worker address, probably a devfee?
                    {
                        worker = "DevFee";
                    }
                    else
                    {
                        worker = connection.endPoint.ToString();
                    }

                    miner = new Miner(worker, connection);

                    _minerManager.AddMiner(miner);
                    Log.Information("{0} has authenticated for [{1}]!", miner.workerIdentifier, _pool.poolWorkerName);
                    //SendToMiner("{\"id\":" + id + ",\"jsonrpc\":\"2.0\",\"error\":null,\"result\":{\"status\": \"OK\"}}", connection);

                    _minerManager.AddMinerID(miner, id);

                    if (_pool.currentPoolTarget.Length != 0)
                    {
                        SendToMiner(_pool.currentPoolWork.GetBytes(), connection);
                    }
                    return;

                case "submit":

                    if (miner == null)
                    {
                        Log.Error("submit from non-existent miner!");
                        return;
                    }

                    Log.Verbose("{0} found a share!", miner.workerIdentifier);

                    _minerManager.AddMinerID(miner, id);

                    _pool.SubmitShareToPool(minerData.GetBytes(), miner);
                    return;

                case "keepalived":
                    //{"id":2,"jsonrpc":"2.0","method":"keepalived","params":{"id":""}}
                    Log.Verbose($"Keepalive from {miner.workerIdentifier}");
                    SendToMiner($"{{\"id\":{id},\"jsonrpc\":\"2.0\",\"error\":null,\"result\":{{\"status\":\"KEEPALIVED\"}}}}", connection);
                    return;
                }
            }
        }