Ejemplo n.º 1
0
        /// <summary>
        /// Application Entry Point.
        /// </summary>
        /// <param name="args"></param>
        private static void Main(string[] args)
        {
            // Get a new global configuration instance.
            Configuration = new Configuration();

            // Initialize redis configuration
            RedisConfigurationOptions = new ConfigurationOptions
            {
                ResolveDns = true
            };

            // Add redis connection.
            RedisConfigurationOptions.EndPoints.Add(
                Configuration.GetRedisAddress(),
                Configuration.GetRedisPort()
                );

            // Initialize Redis Connection.
            InitializeRedis();

            // Initialize Block Objects.
            HashRate               = new PoolHashRateCalculation();
            BlocksPendingPayment   = new List <PoolBlock>();
            BlocksPendingSubmition = new List <PoolBlock>();
            ConnectedClients       = new Dictionary <string, ConnectedWorker>();
            DaemonJson             = new JsonRPC(Configuration.GetDaemonRpc());
            WalletJson             = new JsonRPC(Configuration.GetWalletRpc());

            // Create local instances to keep classes in memory.
            var backgroundSaticUpdater = new BackgroundStaticUpdater();
            var blockPayment           = new BlockPayment();
            var blockSubmitter         = new BlockSubmitter();
            var difficultyRetargeter   = new DifficultyRetargeter();
            var cryptoNightPool        = new CryptoNightPool();

            // Start routines.
            backgroundSaticUpdater.Start();
            blockPayment.Start();
            blockSubmitter.Start();
            difficultyRetargeter.Start();
            cryptoNightPool.Start();


            // Pointless loop to keep the application running.
            while (true)
            {
                Thread.Sleep(Timeout.Infinite);
            }
        }
Ejemplo n.º 2
0
        public async void Start()
        {
            Logger.Log(Logger.LogLevel.General, "Beginning Block Submittion thread!");

            await Task.Yield();

            while (true)
            {
                Thread.Sleep(5000);
                for (var i = 0; i < Statics.BlocksPendingSubmition.Count; i++)
                {
                    try
                    {
                        var block = Statics.BlocksPendingSubmition[i];

                        if (!Statics.BlocksPendingPayment.Any(x => x.BlockHeight == block.BlockHeight))
                        {
                            Logger.Log(Logger.LogLevel.Special, "Submitting block with height {0}", block.BlockHeight);
                            var submitblock =
                                await
                                Statics.DaemonJson.InvokeMethodAsync("submitblock",
                                                                     new JArray(
                                                                         BitConverter.ToString(block.BlockData)
                                                                         .Replace("-", "")));

                            try
                            {
                                if ((string)submitblock["result"]["status"] == "OK")
                                {
                                    Logger.Log(Logger.LogLevel.Special,
                                               "Block submitted was accepted! Adding for payment");
                                    var rBlock = Statics.RedisDb.Blocks.First(x => x.BlockHeight == block.BlockHeight);
                                    //
                                    rBlock.Found         = true;
                                    rBlock.Founder       = block.Founder;
                                    rBlock.FoundDateTime = DateTime.Now;

                                    Statics.RedisDb.SaveChanges(rBlock);

                                    var param = new JObject();
                                    param["height"] = block.BlockHeight;
                                    block.BlockHash =
                                        (string)
                                        (await
                                         Statics.DaemonJson.InvokeMethodAsync("getblockheaderbyheight",
                                                                              param))
                                        [
                                            "result"]["block_header"]["hash"];

                                    Statics.BlocksPendingPayment.Add(block);

                                    BackgroundStaticUpdater.ForceUpdate();
                                    //Force statics update to prevent creating orhpans ourselves, you don't want that now do you?
                                }
                                else
                                {
                                    Logger.Log(Logger.LogLevel.Error,
                                               "Block submittance failed with height {0} and error {1}!",
                                               block.BlockHeight, submitblock["result"]["status"]);
                                }
                            }
                            catch
                            {
                            }

                            Statics.BlocksPendingSubmition.RemoveAt(i);
                            i--;
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Log(Logger.LogLevel.Error, e.ToString());
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private static void Main(string[] args)
        {
            ConfigurationOptions configR = new ConfigurationOptions();

            configR.ResolveDns = true;

            string host = config.IniReadValue("redis-server");
            int    port = 6379;

            if (host.Split(':').Length == 2)
            {
                port = int.Parse(host.Split(':')[1]);
            }

            host = host.Split(':')[0];

            configR.EndPoints.Add(Dns.GetHostAddresses(host)[0], port);

            Logger.AppLogLevel = Logger.LogLevel.Debug;
            Logger.Log(Logger.LogLevel.General, "Starting up!");

            Statics.HashRate = new PoolHashRateCalculation();

            Logger.Log(Logger.LogLevel.Debug, "Initialized PoolHashRateCalculation");
            try
            {
                Statics.RedisDb =
                    new RedisPoolDatabase(
                        ConnectionMultiplexer.Connect(configR)
                        .GetDatabase(int.Parse(config.IniReadValue("redis-database"))));
                Logger.Log(Logger.LogLevel.Debug, "Initialized RedisDb");
            }
            catch (StackExchange.Redis.RedisConnectionException)
            {
                if (NativeFunctions.IsLinux)
                {
                    Logger.Log(Logger.LogLevel.Error, "Redis connection failed.Retrying after 3 seconds");
                    Thread.Sleep(3 * 1000);
                    while (true)
                    {
                        try
                        {
                            Statics.RedisDb =
                                new RedisPoolDatabase(
                                    ConnectionMultiplexer.Connect(configR)
                                    .GetDatabase(int.Parse(config.IniReadValue("redis-database"))));
                            break;
                        }
                        catch
                        {
                        }
                        Logger.Log(Logger.LogLevel.Error, "Redis connection failed.Retrying after 3 seconds");
                        Thread.Sleep(3 * 1000);
                    }
                }
                else
                {
                    Logger.Log(Logger.LogLevel.Error, "Redis connection failed. Shutting down");
                    Environment.Exit(-1);
                }
            }
            Statics.BlocksPendingPayment = new List <PoolBlock>();
            Logger.Log(Logger.LogLevel.Debug, "Initialized BlocksPendingPayment");

            Statics.BlocksPendingSubmition = new List <PoolBlock>();
            Logger.Log(Logger.LogLevel.Debug, "Initialized BlocksPendingSubmition");

            Statics.Config = new IniFile("config.txt");
            Logger.Log(Logger.LogLevel.Debug, "Initialized Config");

            Statics.ConnectedClients = new Dictionary <string, ConnectedWorker>();
            Logger.Log(Logger.LogLevel.Debug, "Initialized ConnectedClients");

            Statics.DaemonJson = new JsonRPC(config.IniReadValue("daemon-json-rpc"));
            Logger.Log(Logger.LogLevel.Debug, "Initialized DaemonJson");

            Statics.WalletJson = new JsonRPC(config.IniReadValue("wallet-json-rpc"));
            Logger.Log(Logger.LogLevel.Debug, "Initialized WalletJson");

            Logger.Log(Logger.LogLevel.General, "Initialized Statics, initializing classes");



            BackgroundStaticUpdater backgroundSaticUpdater = new BackgroundStaticUpdater();

            backgroundSaticUpdater.Start();
            Logger.Log(Logger.LogLevel.Debug, "Initialized backgroundSaticUpdater");

            BlockPayment blockPayment = new BlockPayment();

            blockPayment.Start();
            Logger.Log(Logger.LogLevel.Debug, "Initialized BlockPayment");

            BlockSubmitter blockSubmitter = new BlockSubmitter();

            blockSubmitter.Start();
            Logger.Log(Logger.LogLevel.Debug, "Initialized BlockSubmitter");

            DifficultyRetargeter difficultyRetargeter = new DifficultyRetargeter();

            difficultyRetargeter.Start();
            Logger.Log(Logger.LogLevel.Debug, "Initialized DifficultyRetargeter");


            CryptoNightPool cryptoNightPool = new CryptoNightPool();

            cryptoNightPool.Start();
            Logger.Log(Logger.LogLevel.Debug, "Initialized CryptoNightPool");

            Logger.Log(Logger.LogLevel.General, "Initialized Classes");

            while (true)
            {
                Logger.Log(Logger.LogLevel.Debug, "Put the main thread into sleep...");
                Thread.Sleep(Timeout.Infinite);
            }
        }