예제 #1
0
        public bool Process(ParseMessage pm, ref int actionCounter)
        {
            // read stratum lines from network
            List <string> receivedLines = ReadNetworkData();

            // parse stratum lines into stratum messages
            foreach (string line in receivedLines)
            {
                actionCounter++;
                Logging.Log(5, "Received from miner: " + line);
                StratumMessageClient smc = StratumMessage.ParseClientMessage(line);
                if (smc != null)
                {
                    receivedMessages.Enqueue(smc);
                }
                else
                {
                    Logging.Log(5, "Invalid stratum message: " + line);

                    // todo: handle non supported messages?
                }
            }

            // process stratum messages
            while (receivedMessages.Count > 0)
            {
                actionCounter++;
                StratumMessageClient smc = receivedMessages.Dequeue();
                if (!pm(this, smc))
                {
                    return(false);
                }
            }

            // check if connection is dead
            if (connection.Client.Poll(0, SelectMode.SelectRead) && connection.Client.Available == 0)
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        /// <summary>
        /// Parse incoming JSON message in string format.
        /// </summary>
        /// <param name="line">JSON message.</param>
        /// <returns>StratumMessageClient object or null if unable to determine type of message.</returns>
        public static StratumMessageClient ParseClientMessage(string line)
        {
            try
            {
                StratumMessageClient smsg = JsonConvert.DeserializeObject <StratumMessageClient>(line);

                if (smsg.id == null)
                {
                    throw new Exception("Incorrect id.");
                }

                switch (smsg.method)
                {
                case "mining.subscribe":
                    smsg.type = StratumMessageClientType.Subscribe;
                    break;

                case "mining.extranonce.subscribe":
                    smsg.type = StratumMessageClientType.SubscribeExtranonce;
                    break;

                case "mining.authorize":
                    smsg.type = StratumMessageClientType.Authorize;
                    break;

                case "mining.submit":
                    smsg.type = StratumMessageClientType.Submit;
                    break;

                default:
                    throw new Exception("Unknown stratum message.");
                }

                return(smsg);
            }
            catch (Exception ex)
            {
                Logging.Log(4, "JSON parse error: " + ex.Message);
                return(null);
            }
        }
예제 #3
0
        private bool ProcessClientMessage(Miner m, StratumMessageClient smc)
        {
            StratumMessageServerResult smsr;

            switch (smc.type)
            {
            case StratumMessageClientType.Subscribe:
                if (!m.isSubscribed &&
                    smc.parameters != null &&
                    smc.parameters.Length > 1 &&
                    smc.parameters[1] == Config.ETHEREUM_STRATUM_VERSION)
                {
                    m.isSubscribed = true;

                    m.extraNonce = GetExtraNonce();

                    smsr        = new StratumMessageServerResult();
                    smsr.id     = smc.id;
                    smsr.error  = null;
                    smsr.result = StratumMessageServerResult.CreateMiningNotifyResult(m.extraNonce);

                    m.Send(smsr);

                    break;
                }
                else
                {
                    return(false);
                }

            case StratumMessageClientType.SubscribeExtranonce:
                smsr        = new StratumMessageServerResult();
                smsr.id     = smc.id;
                smsr.error  = null;
                smsr.result = true;
                m.Send(smsr);
                break;

            case StratumMessageClientType.Authorize:
                if (!m.isAuthorized &&
                    smc.parameters != null &&
                    smc.parameters.Length == 2)
                {
                    // accept all miners with all usernames for now
                    m.userName     = smc.parameters[0];
                    m.isAuthorized = true;

                    smsr        = new StratumMessageServerResult();
                    smsr.id     = smc.id;
                    smsr.error  = null;
                    smsr.result = true;

                    m.Send(smsr);
                    break;
                }
                else
                {
                    return(false);
                }

            case StratumMessageClientType.Submit:
                if (m.initialWorkSent &&
                    smc.parameters != null &&
                    smc.parameters.Length == 3)
                {
                    if (smc.parameters[0] == m.userName)
                    {
                        ProcessShare(m, smc.id, smc.parameters[1], smc.parameters[2]);
                    }
                }
                break;
            }

            if (!m.initialWorkSent && m.isSubscribed && m.isAuthorized)
            {
                double minerDiff = Config.ConfigData.StratumDifficulty;
                if (currentJob.difficulty < minerDiff)
                {
                    minerDiff = currentJob.difficulty;
                }

                if (m.isAuthorized && m.isSubscribed)
                {
                    m.SetDifficultyAndJob(minerDiff, currentJob);
                }

                m.initialWorkSent = true;
            }

            return(true);
        }