Ejemplo n.º 1
0
        protected void UpdateVarDiff(StratumClient client, bool isIdleUpdate = false)
        {
            var context = client.GetContextAs <WorkerContextBase>();

            if (context.VarDiff != null)
            {
                logger.Debug(() => $"[{LogCat}] [{client.ConnectionId}] Updating VarDiff" + (isIdleUpdate ? " [idle]" : string.Empty));

                VarDiffManager varDiffManager;
                var            poolEndpoint = poolConfig.Ports[client.PoolEndpoint.Port];

                lock (varDiffManagers)
                {
                    if (!varDiffManagers.TryGetValue(poolEndpoint, out varDiffManager))
                    {
                        varDiffManager = new VarDiffManager(poolEndpoint.VarDiff, clock);
                        varDiffManagers[poolEndpoint] = varDiffManager;
                    }
                }

                lock (context.VarDiff)
                {
                    StartVarDiffIdleUpdate(client, poolEndpoint);

                    var newDiff = varDiffManager.Update(context.VarDiff, context.Difficulty, isIdleUpdate);

                    if (newDiff != null)
                    {
                        logger.Info(() => $"[{LogCat}] [{client.ConnectionId}] VarDiff update to {Math.Round(newDiff.Value, 2)}");

                        OnVarDiffUpdate(client, newDiff.Value);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void VarDiff_Should_Honor_MinDiff_When_Adjusting_Down()
        {
            var vdm = new VarDiffManager(config);
            var ctx = new WorkerContextBase {
                Difficulty = 1500, VarDiff = new VarDiffContext()
            };

            var shares = new List <long> {
                2000000000, 3000000000, 4000000000
            };
            var newDiff = vdm.Update(ctx, shares, string.Empty, logger);

            Assert.NotNull(newDiff);
            Assert.True(newDiff.Value.EqualsDigitPrecision3(1000));
        }
Ejemplo n.º 3
0
        public void VarDiff_Should_Honor_MaxDelta_When_Adjusting_Up()
        {
            var vdm = new VarDiffManager(config, clock);
            var ctx = new WorkerContextBase {
                Difficulty = 7500, VarDiff = new VarDiffContext()
            };

            var shares = new List <long> {
                2, 3, 4
            };
            var newDiff = vdm.Update(ctx, shares, string.Empty, logger);

            Assert.NotNull(newDiff);
            Assert.True(newDiff.Value.EqualsDigitPrecision3(8500));
        }
Ejemplo n.º 4
0
        protected void UpdateVarDiff(StratumClient <TWorkerContext> client, double networkDifficulty, bool onSubmission)
        {
            var context = client.Context;

            if (context.VarDiff != null)
            {
                logger.Debug(() => $"[{LogCat}] [{client.ConnectionId}] Updating VarDiff");

                // get or create manager
                VarDiffManager varDiffManager;
                var            poolEndpoint = poolConfig.Ports[client.PoolEndpoint.Port];

                lock (varDiffManagers)
                {
                    if (!varDiffManagers.TryGetValue(poolEndpoint, out varDiffManager))
                    {
                        varDiffManager = new VarDiffManager(poolEndpoint.VarDiff);
                        varDiffManagers[poolEndpoint] = varDiffManager;
                    }
                }

                lock (context.VarDiff)
                {
                    if (onSubmission)
                    {
                        StartVarDiffIdleUpdate(client, poolEndpoint, context);
                    }

                    // update it
                    var newDiff = varDiffManager.Update(context.VarDiff, context.Difficulty, networkDifficulty, onSubmission);

                    if (newDiff != null)
                    {
                        logger.Debug(() => $"[{LogCat}] [{client.ConnectionId}] VarDiff update to {newDiff}");

                        context.EnqueueNewDifficulty(newDiff.Value);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        protected async Task UpdateVarDiffAsync(StratumClient client, bool isIdleUpdate = false)
        {
            // var context = client.ContextAs<WorkerContextBase>();
            var context = client.ContextAs <WorkerContextBase>();

            if (context.VarDiff != null)
            {
                logger.Debug(() => $"[{client.ConnectionId}] Updating VarDiff" + (isIdleUpdate ? " [idle]" : string.Empty));

                // get or create manager
                VarDiffManager varDiffManager;
                var            poolEndpoint = poolConfig.Ports[client.PoolEndpoint.Port];

                lock (varDiffManagers)
                {
                    if (!varDiffManagers.TryGetValue(poolEndpoint, out varDiffManager))
                    {
                        varDiffManager = new VarDiffManager(poolEndpoint.VarDiff, clock);
                        varDiffManagers[poolEndpoint] = varDiffManager;
                    }
                }

                double?newDiff = null;

                lock (context.VarDiff)
                {
                    StartVarDiffIdleUpdate(client, poolEndpoint);

                    // update it
                    newDiff = varDiffManager.Update(context.VarDiff, context.Difficulty, isIdleUpdate);
                }

                if (newDiff != null)
                {
                    logger.Info(() => $"[{client.ConnectionId}] VarDiff update to {Math.Round(newDiff.Value, 2)}");

                    await OnVarDiffUpdateAsync(client, newDiff.Value);
                }
            }
        }
Ejemplo n.º 6
0
        protected override void OnConnect(StratumClient <TWorkerContext> client)
        {
            // update stats
            lock (clients)
            {
                poolStats.ConnectedMiners = clients.Count;
            }

            // client setup
            var context = new TWorkerContext();

            var poolEndpoint = poolConfig.Ports[client.PoolEndpoint.Port];

            context.Init(poolConfig, poolEndpoint.Difficulty, poolEndpoint.VarDiff, clock);
            client.Context = context;

            // varDiff setup
            if (context.VarDiff != null)
            {
                // get or create manager
                lock (varDiffManagers)
                {
                    if (!varDiffManagers.TryGetValue(poolEndpoint, out var varDiffManager))
                    {
                        varDiffManager = new VarDiffManager(poolEndpoint.VarDiff, clock);
                        varDiffManagers[poolEndpoint] = varDiffManager;
                    }
                }

                // wire updates
                lock (context.VarDiff)
                {
                    context.VarDiff.Subscription = Shares
                                                   .Where(x => x.Item1 == client)
                                                   .Timestamp()
                                                   .Select(x => x.Timestamp.ToUnixTimeMilliseconds())
                                                   .Buffer(TimeSpan.FromSeconds(poolEndpoint.VarDiff.RetargetTime), VarDiffSampleCount)
                                                   .Subscribe(timestamps =>
                    {
                        try
                        {
                            VarDiffManager varDiffManager;

                            lock (varDiffManagers)
                            {
                                varDiffManager = varDiffManagers[poolEndpoint];
                            }

                            var newDiff = varDiffManager.Update(context, timestamps, client.ConnectionId, logger);

                            if (newDiff.HasValue)
                            {
                                logger.Info(() => $"[{LogCat}] [{client.ConnectionId}] VarDiff update to {newDiff}");

                                OnVarDiffUpdate(client, newDiff.Value);
                            }
                        }

                        catch (Exception ex)
                        {
                            logger.Error(ex);
                        }
                    });
                }
            }

            // expect miner to establish communication within a certain time
            EnsureNoZombieClient(client);
        }