Ejemplo n.º 1
0
        protected virtual void OnSubscribe(StratumClient <BitcoinWorkerContext> client, Timestamped <JsonRpcRequest> tsRequest)
        {
            var request = tsRequest.Value;

            if (request.Id == null)
            {
                client.RespondError(StratumError.Other, "missing request id", request.Id);
                return;
            }

            var requestParams = request.ParamsAs <string[]>();

            var data = new object[]
            {
                new object[]
                {
                    new object[] { BitcoinStratumMethods.SetDifficulty, client.ConnectionId },
                    new object[] { BitcoinStratumMethods.MiningNotify, client.ConnectionId }
                }
            }
            .Concat(manager.GetSubscriberData(client))
            .ToArray();

            client.Respond(data, request.Id);

            // setup worker context
            client.Context.IsSubscribed = true;
            client.Context.UserAgent    = requestParams?.Length > 0 ? requestParams[0].Trim() : null;

            // send intial update
            client.Notify(BitcoinStratumMethods.SetDifficulty, new object[] { client.Context.Difficulty });
            client.Notify(BitcoinStratumMethods.MiningNotify, currentJobParams);
        }
Ejemplo n.º 2
0
        private void OnAuthorize(StratumClient <EthereumWorkerContext> client, Timestamped <JsonRpcRequest> tsRequest)
        {
            var request = tsRequest.Value;

            if (request.Id == null)
            {
                client.RespondError(StratumError.Other, "missing request id", request.Id);
                return;
            }

            var requestParams = request.ParamsAs <string[]>();
            var workerValue   = requestParams?.Length > 0 ? requestParams[0] : null;
            //var password = requestParams?.Length > 1 ? requestParams[1] : null;

            // extract worker/miner
            var split      = workerValue?.Split('.');
            var minerName  = split?.FirstOrDefault();
            var workerName = split?.LastOrDefault();

            // assumes that workerName is an address
            client.Context.IsAuthorized = manager.ValidateAddress(minerName);
            client.Context.MinerName    = minerName;
            client.Context.WorkerName   = workerName;
            client.Respond(client.Context.IsAuthorized, request.Id);

            // send intial update
            client.Notify(EthereumStratumMethods.SetDifficulty, new object[] { client.Context.Difficulty });
            client.Notify(EthereumStratumMethods.MiningNotify, currentJobParams);
        }
Ejemplo n.º 3
0
        private void EnsureInitialWorkSent(StratumClient client)
        {
            var       context         = client.ContextAs <AionWorkerContext>();
            ArrayList arrayTarget     = new ArrayList();
            var       sendInitialWork = false;

            lock (context)
            {
                if (context.IsSubscribed && context.IsAuthorized && !context.IsInitialWorkSent)
                {
                    context.IsInitialWorkSent = true;
                    string newTarget = AionUtils.diffToTarget(context.Difficulty);
                    arrayTarget.Add(newTarget);
                    sendInitialWork = true;
                }
            }

            if (sendInitialWork)
            {
                // send intial update
                // await client.NotifyAsync(AionStratumMethods.MiningNotify, currentJobParams);
                // await client.NotifyAsync(AionStratumMethods.SetTarget, arrayTarget);
                client.Notify(AionStratumMethods.MiningNotify, currentJobParams);
                client.Notify(AionStratumMethods.SetTarget, arrayTarget);
            }
        }
Ejemplo n.º 4
0
        protected override async Task OnAuthorizeAsync(StratumClient <BitcoinWorkerContext> client, Timestamped <JsonRpcRequest> tsRequest)
        {
            await base.OnAuthorizeAsync(client, tsRequest);

            if (client.Context.IsAuthorized)
            {
                // send intial update
                client.Notify(ZCashStratumMethods.SetTarget, new object[] { EncodeTarget(client.Context.Difficulty) });
                client.Notify(BitcoinStratumMethods.MiningNotify, currentJobParams);
            }
        }
Ejemplo n.º 5
0
        protected override void UpdateVarDiffAndNotifyClient(StratumClient <BitcoinWorkerContext> client)
        {
            UpdateVarDiff(client, manager.BlockchainStats.NetworkDifficulty);

            if (client.Context.ApplyPendingDifficulty())
            {
                client.Notify(BitcoinStratumMethods.SetDifficulty, new object[] { client.Context.Difficulty });

                // send job
                client.Notify(BitcoinStratumMethods.MiningNotify, currentJobParams);
            }
        }
Ejemplo n.º 6
0
        protected override void OnVarDiffUpdate(StratumClient <BitcoinWorkerContext> client, double newDiff)
        {
            client.Context.EnqueueNewDifficulty(newDiff);

            // apply immediately and notify client
            if (client.Context.HasPendingDifficulty)
            {
                client.Context.ApplyPendingDifficulty();

                client.Notify(BitcoinStratumMethods.SetDifficulty, new object[] { client.Context.Difficulty });
                client.Notify(BitcoinStratumMethods.MiningNotify, currentJobParams);
            }
        }
Ejemplo n.º 7
0
        protected override void OnVarDiffUpdate(StratumClient client, double newDiff)
        {
            base.OnVarDiffUpdate(client, newDiff);

            var context = client.GetContextAs <EthereumWorkerContext>();

            if (context.HasPendingDifficulty)
            {
                context.ApplyPendingDifficulty();

                client.Notify(EthereumStratumMethods.SetDifficulty, new object[] { context.Difficulty });
                client.Notify(EthereumStratumMethods.MiningNotify, currentJobParams);
            }
        }
Ejemplo n.º 8
0
        private void EnsureInitialWorkSent(StratumClient <EthereumWorkerContext> client)
        {
            lock (client.Context)
            {
                if (client.Context.IsAuthorized && client.Context.IsAuthorized && !client.Context.IsInitialWorkSent)
                {
                    client.Context.IsInitialWorkSent = true;

                    // send intial update
                    client.Notify(EthereumStratumMethods.SetDifficulty, new object[] { client.Context.Difficulty });
                    client.Notify(EthereumStratumMethods.MiningNotify, currentJobParams);
                }
            }
        }
Ejemplo n.º 9
0
        protected override void OnVarDiffUpdate(StratumClient <EthereumWorkerContext> client, double newDiff)
        {
            base.OnVarDiffUpdate(client, newDiff);

            // apply immediately and notify client
            if (client.Context.HasPendingDifficulty)
            {
                client.Context.ApplyPendingDifficulty();

                // send job
                client.Notify(EthereumStratumMethods.SetDifficulty, new object[] { client.Context.Difficulty });
                client.Notify(EthereumStratumMethods.MiningNotify, currentJobParams);
            }
        }
Ejemplo n.º 10
0
        protected override void OnVarDiffUpdate(StratumClient client, double newDiff)
        {
            var context = client.GetContextAs <BitcoinWorkerContext>();

            context.EnqueueNewDifficulty(newDiff);

            if (context.HasPendingDifficulty)
            {
                context.ApplyPendingDifficulty();

                client.Notify(BitcoinStratumMethods.SetDifficulty, new object[] { context.Difficulty });
                client.Notify(BitcoinStratumMethods.MiningNotify, currentJobParams);
            }
        }
Ejemplo n.º 11
0
        protected override void OnVarDiffUpdate(StratumClient client, double newDiff)
        {
            var context = client.GetContextAs <BitcoinWorkerContext>();

            context.EnqueueNewDifficulty(newDiff);

            // apply immediately and notify client
            if (context.HasPendingDifficulty)
            {
                context.ApplyPendingDifficulty();

                client.Notify(ZCashStratumMethods.SetTarget, new object[] { EncodeTarget(context.Difficulty) });
                client.Notify(BitcoinStratumMethods.MiningNotify, currentJobParams);
            }
        }
Ejemplo n.º 12
0
        private void EnsureInitialWorkSent(StratumClient client)
        {
            var context = client.GetContextAs <EthereumWorkerContext>();

            lock (context)
            {
                if (context.IsAuthorized && context.IsAuthorized && !context.IsInitialWorkSent)
                {
                    context.IsInitialWorkSent = true;

                    client.Notify(EthereumStratumMethods.SetDifficulty, new object[] { context.Difficulty });
                    client.Notify(EthereumStratumMethods.MiningNotify, currentJobParams);
                }
            }
        }
Ejemplo n.º 13
0
        private void OnSuggestDifficulty(StratumClient <BitcoinWorkerContext> client, Timestamped <JsonRpcRequest> tsRequest)
        {
            var request = tsRequest.Value;

            // acknowledge
            client.Respond(true, request.Id);

            try
            {
                var requestedDiff = (double)Convert.ChangeType(request.Params, TypeCode.Double);

                // client may suggest higher-than-base difficulty, but not a lower one
                var poolEndpoint = poolConfig.Ports[client.PoolEndpoint.Port];

                if (requestedDiff > poolEndpoint.Difficulty)
                {
                    client.Context.SetDifficulty(requestedDiff);
                    client.Notify(BitcoinStratumMethods.SetDifficulty, new object[] { client.Context.Difficulty });

                    logger.Info(() => $"[{LogCat}] [{client.ConnectionId}] Difficulty set to {requestedDiff} as requested by miner");
                }
            }

            catch (Exception ex)
            {
                logger.Error(ex, () => $"[{LogCat}] Unable to convert suggested difficulty {request.Params}");
            }
        }
Ejemplo n.º 14
0
        protected override void OnVarDiffUpdate(StratumClient client, double newDiff)
        {
            base.OnVarDiffUpdate(client, newDiff);

            // apply immediately and notify client
            var context = client.GetContextAs <AionWorkerContext>();

            if (context.HasPendingDifficulty)
            {
                context.ApplyPendingDifficulty();

                // send job
                client.Notify(AionStratumMethods.SetDifficulty, new object[] { context.Difficulty });
                client.Notify(AionStratumMethods.MiningNotify, currentJobParams);
            }
        }
Ejemplo n.º 15
0
        private void EnsureInitialWorkSent(StratumClient client)
        {
            var context = client.GetContextAs <EthereumWorkerContext>();

            lock (context)
            {
                if (context.IsAuthorized && context.IsAuthorized && !context.IsInitialWorkSent && context.IsNiceHashClient)
                {
                    context.IsInitialWorkSent = true;

                    // send intial update
                    client.Notify(EthereumStratumMethods.SetDifficulty, new object[] { context.Difficulty *EthereumConstants.NicehashStratumDiffFactor });
                    client.Notify(EthereumStratumMethods.MiningNotify, currentJobParams);
                }
            }
        }
Ejemplo n.º 16
0
        protected override void UpdateVarDiffAndNotifyClient(StratumClient <MoneroWorkerContext> client)
        {
            UpdateVarDiff(client, manager.BlockchainStats.NetworkDifficulty);

            if (client.Context.ApplyPendingDifficulty())
            {
                // send job
                var job = CreateWorkerJob(client);
                client.Notify(MoneroStratumMethods.JobNotify, job);
            }
        }
Ejemplo n.º 17
0
        protected override void OnVarDiffUpdate(StratumClient client, double newDiff)
        {
            base.OnVarDiffUpdate(client, newDiff);

            // apply immediately and notify client
            var context = client.GetContextAs <AionWorkerContext>();

            if (context.HasPendingDifficulty)
            {
                context.ApplyPendingDifficulty();
                string    newTarget   = AionUtils.diffToTarget(newDiff);
                ArrayList targetArray = new ArrayList();
                targetArray.Add(newTarget);

                // send job
                client.Notify(AionStratumMethods.SetDifficulty, new object[] { context.Difficulty });
                client.Notify(AionStratumMethods.MiningNotify, currentJobParams);
                client.Notify(AionStratumMethods.SetTarget, targetArray);
            }
        }
Ejemplo n.º 18
0
        protected virtual async Task OnAuthorizeAsync(StratumClient client, Timestamped <JsonRpcRequest> tsRequest)
        {
            var request = tsRequest.Value;

            if (request.Id == null)
            {
                client.RespondError(StratumError.Other, "missing request id", request.Id);
                return;
            }

            var context       = client.GetContextAs <BitcoinWorkerContext>();
            var requestParams = request.ParamsAs <string[]>();
            var workerValue   = requestParams?.Length > 0 ? requestParams[0] : null;
            var password      = requestParams?.Length > 1 ? requestParams[1] : null;
            var passParts     = password?.Split(PasswordControlVarsSeparator);

            var split      = workerValue?.Split('.');
            var minerName  = split?.FirstOrDefault()?.Trim();
            var workerName = split?.Skip(1).FirstOrDefault()?.Trim() ?? string.Empty;

            context.IsAuthorized = !string.IsNullOrEmpty(minerName) && await manager.ValidateAddressAsync(minerName);

            context.MinerName  = minerName;
            context.WorkerName = workerName;

            if (context.IsAuthorized)
            {
                client.Respond(context.IsAuthorized, request.Id);

                logger.Info(() => $"[{LogCat}] [{client.ConnectionId}] = {workerValue} = {client.RemoteEndpoint.Address}");

                var staticDiff = GetStaticDiffFromPassparts(passParts);
                if (staticDiff.HasValue &&
                    (context.VarDiff != null && staticDiff.Value >= context.VarDiff.Config.MinDiff ||
                     context.VarDiff == null && staticDiff.Value > context.Difficulty))
                {
                    context.VarDiff = null; context.SetDifficulty(staticDiff.Value);

                    client.Notify(BitcoinStratumMethods.SetDifficulty, new object[] { context.Difficulty });
                }
            }

            else
            {
                client.RespondError(StratumError.UnauthorizedWorker, "Authorization failed", request.Id, context.IsAuthorized);

                logger.Info(() => $"[{LogCat}] [{client.ConnectionId}] Banning unauthorized worker for 60 sec");

                banManager.Ban(client.RemoteEndpoint.Address, TimeSpan.FromSeconds(60));

                DisconnectClient(client);
            }
        }
Ejemplo n.º 19
0
        protected override void OnVarDiffUpdate(StratumClient <MoneroWorkerContext> client, double newDiff)
        {
            base.OnVarDiffUpdate(client, newDiff);

            // apply immediately and notify client
            if (client.Context.HasPendingDifficulty)
            {
                client.Context.ApplyPendingDifficulty();

                // re-send job
                var job = CreateWorkerJob(client);
                client.Notify(MoneroStratumMethods.JobNotify, job);
            }
        }
Ejemplo n.º 20
0
        private void OnSuggestTarget(StratumClient client, Timestamped <JsonRpcRequest> tsRequest)
        {
            var request = tsRequest.Value;
            var context = client.GetContextAs <BitcoinWorkerContext>();

            if (request.Id == null)
            {
                client.RespondError(StratumError.Other, "missing request id", request.Id);
                return;
            }

            var requestParams = request.ParamsAs <string[]>();
            var target        = requestParams.FirstOrDefault();

            if (!string.IsNullOrEmpty(target))
            {
                if (System.Numerics.BigInteger.TryParse(target, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var targetBig))
                {
                    var newDiff      = (double)new BigRational(ZCashConstants.Diff1b, targetBig);
                    var poolEndpoint = poolConfig.Ports[client.PoolEndpoint.Port];

                    if (newDiff >= poolEndpoint.Difficulty)
                    {
                        context.EnqueueNewDifficulty(newDiff);
                        context.ApplyPendingDifficulty();

                        client.Notify(ZCashStratumMethods.SetTarget, new object[] { EncodeTarget(context.Difficulty) });
                    }

                    else
                    {
                        client.RespondError(StratumError.Other, "suggested difficulty too low", request.Id);
                    }
                }

                else
                {
                    client.RespondError(StratumError.Other, "invalid target", request.Id);
                }
            }

            else
            {
                client.RespondError(StratumError.Other, "invalid target", request.Id);
            }
        }
Ejemplo n.º 21
0
        private void EnsureInitialWorkSent(StratumClient client)
        {
            var context = client.GetContextAs <AionWorkerContext>();

            lock (context)
            {
                if (context.IsAuthorized && context.IsAuthorized && !context.IsInitialWorkSent)
                {
                    context.IsInitialWorkSent = true;
                    string newTarget = AionUtils.diffToTarget(context.Difficulty);
                    // update the difficulty
                    (((object[])currentJobParams)[2]) = newTarget;

                    // send intial update
                    client.Notify(AionStratumMethods.MiningNotify, currentJobParams);
                }
            }
        }