Example #1
0
        public void CreateAndDeleteSeed()
        {
            // creation
            LibRandomX.CreateSeed(realm, seedHex);
            Assert.True(LibRandomX.realms.ContainsKey(realm));
            Assert.True(LibRandomX.realms[realm].ContainsKey(seedHex));

            // accessing the created seed should work
            Assert.NotNull(LibRandomX.GetSeed(realm, seedHex));

            // creating the same realm and key twice should not result in duplicates
            LibRandomX.CreateSeed(realm, seedHex);
            Assert.Equal(LibRandomX.realms.Count, 1);
            Assert.Equal(LibRandomX.realms[realm].Count, 1);

            // deletion
            LibRandomX.DeleteSeed(realm, seedHex);
            Assert.False(LibRandomX.realms[realm].ContainsKey(seedHex));
        }
Example #2
0
        public void CalculateHashFast()
        {
            var buf = new byte[32];

            // fast-mode
            LibRandomX.CreateSeed(realm, seedHex, null, LibRandomX.randomx_flags.RANDOMX_FLAG_FULL_MEM);

            LibRandomX.CalculateHash("xmr", seedHex, data, buf);
            var result = buf.ToHexString();

            Assert.Equal(hashExpected, result);

            Array.Clear(buf, 0, buf.Length);

            // second invocation should give the same result
            LibRandomX.CalculateHash("xmr", seedHex, data, buf);
            result = buf.ToHexString();
            Assert.Equal(hashExpected, result);

            LibRandomX.DeleteSeed(realm, seedHex);
        }
Example #3
0
        public void CalculateHashSlow()
        {
            var buf = new byte[32];

            // light-mode
            LibRandomX.CreateSeed(realm, seedHex);

            LibRandomX.CalculateHash("xmr", seedHex, data, buf);
            var result = buf.ToHexString();

            Assert.Equal(hashExpected, result);

            Array.Clear(buf, 0, buf.Length);

            // second invocation should give the same result
            LibRandomX.CalculateHash("xmr", seedHex, data, buf);
            result = buf.ToHexString();
            Assert.Equal(hashExpected, result);

            LibRandomX.DeleteSeed(realm, seedHex);
        }
Example #4
0
        public CryptonoteJob(GetBlockTemplateResponse blockTemplate, byte[] instanceId, string jobId,
                             CryptonoteCoinTemplate coin, PoolConfig poolConfig, ClusterConfig clusterConfig, string prevHash, string randomXRealm)
        {
            Contract.RequiresNonNull(blockTemplate, nameof(blockTemplate));
            Contract.RequiresNonNull(poolConfig, nameof(poolConfig));
            Contract.RequiresNonNull(clusterConfig, nameof(clusterConfig));
            Contract.RequiresNonNull(instanceId, nameof(instanceId));
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(jobId), $"{nameof(jobId)} must not be empty");

            BlockTemplate = blockTemplate;
            PrepareBlobTemplate(instanceId);
            PrevHash = prevHash;

            switch (coin.Hash)
            {
            case CryptonightHashType.RandomX:
                hashFunc = ((seedHex, data, result, height) =>
                {
                    LibRandomX.CalculateHash(randomXRealm, seedHex, data, result);
                });
                break;
            }
        }
        protected async Task <bool> UpdateJob(string via = null, string json = null)
        {
            logger.LogInvoke();

            try
            {
                var response = string.IsNullOrEmpty(json) ? await GetBlockTemplateAsync() : GetBlockTemplateFromJson(json);

                // may happen if daemon is currently not connected to peers
                if (response.Error != null)
                {
                    logger.Warn(() => $"Unable to update job. Daemon responded with: {response.Error.Message} Code {response.Error.Code}");
                    return(false);
                }

                var blockTemplate = response.Response;
                var job           = currentJob;
                var newHash       = blockTemplate.Blob.HexToByteArray().Slice(7, 32).ToHexString();

                var isNew = job == null || newHash != job.PrevHash;

                if (isNew)
                {
                    messageBus.NotifyChainHeight(poolConfig.Id, blockTemplate.Height, poolConfig.Template);

                    if (via != null)
                    {
                        logger.Info(() => $"Detected new block {blockTemplate.Height} [{via}]");
                    }
                    else
                    {
                        logger.Info(() => $"Detected new block {blockTemplate.Height}");
                    }

                    // detect seed hash change
                    if (currentSeedHash != blockTemplate.SeedHash)
                    {
                        logger.Info(() => $"Detected new seed hash {blockTemplate.SeedHash} starting @ height {blockTemplate.Height}");

                        if (poolConfig.EnableInternalStratum == true)
                        {
                            LibRandomX.WithLock(() =>
                            {
                                // delete old seed
                                if (currentSeedHash != null)
                                {
                                    LibRandomX.DeleteSeed(randomXRealm, currentSeedHash);
                                }

                                // activate new one
                                currentSeedHash = blockTemplate.SeedHash;
                                LibRandomX.CreateSeed(randomXRealm, currentSeedHash, randomXFlagsOverride, randomXFlagsAdd, extraPoolConfig.RandomXVMCount);
                            });
                        }

                        else
                        {
                            currentSeedHash = blockTemplate.SeedHash;
                        }
                    }

                    // init job
                    job        = new CryptonoteJob(blockTemplate, instanceId, NextJobId(), coin, poolConfig, clusterConfig, newHash, randomXRealm);
                    currentJob = job;

                    // update stats
                    BlockchainStats.LastNetworkBlockTime = clock.Now;
                    BlockchainStats.BlockHeight          = job.BlockTemplate.Height;
                    BlockchainStats.NetworkDifficulty    = job.BlockTemplate.Difficulty;
                    BlockchainStats.NextNetworkTarget    = "";
                    BlockchainStats.NextNetworkBits      = "";
                }

                else
                {
                    if (via != null)
                    {
                        logger.Debug(() => $"Template update {blockTemplate.Height} [{via}]");
                    }
                    else
                    {
                        logger.Debug(() => $"Template update {blockTemplate.Height}");
                    }
                }

                return(isNew);
            }

            catch (Exception ex)
            {
                logger.Error(ex, () => $"Error during {nameof(UpdateJob)}");
            }

            return(false);
        }