PoolManager.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (C) 1883 Thomas Edison - All Rights Reserved
  2. * You may use, distribute and modify this code under the
  3. * terms of the GPLv3 license, which unfortunately won't be
  4. * written for another century.
  5. *
  6. * You should have received a copy of the LICENSE file with
  7. * this file.
  8. */
  9. #pragma once
  10. #include <iostream>
  11. #include <json/json.h>
  12. #include <libdev/Worker.h>
  13. #include <libeth/Farm.h>
  14. #include <libeth/Miner.h>
  15. #include "PoolClient.h"
  16. #include "getwork/EthGetworkClient.h"
  17. #include "stratum/EthStratumClient.h"
  18. #include "testing/SimulateClient.h"
  19. using namespace std;
  20. namespace dev {
  21. namespace eth {
  22. struct PoolSettings {
  23. std::vector<std::shared_ptr<URI>> connections; // List of connection definitions
  24. unsigned getWorkPollInterval = 500; // Interval (ms) between getwork requests
  25. unsigned noWorkTimeout = 180; // If no new jobs in this number of seconds drop connection
  26. unsigned noResponseTimeout = 2; // If no response in this number of seconds drop connection
  27. unsigned poolFailoverTimeout = 0; // Return to primary pool after this number of minutes
  28. bool reportHashrate = false; // Whether or not to report hashrate to pool
  29. unsigned hashRateInterval = 60; // Interval in seconds among hashrate submissions
  30. std::string hashRateId = h256::random().hex(HexPrefix::Add); // Unique identifier for HashRate submission
  31. unsigned connectionMaxRetries = 3; // Max number of connection retries
  32. unsigned delayBeforeRetry = 0; // Delay seconds before connect retry
  33. unsigned benchmarkBlock = 0; // Block number used by SimulateClient to test performances
  34. };
  35. class PoolManager {
  36. public:
  37. PoolManager(PoolSettings _settings);
  38. static PoolManager& p() { return *m_this; }
  39. void addConnection(std::string _connstring);
  40. void addConnection(std::shared_ptr<URI> _uri);
  41. Json::Value getConnectionsJson();
  42. void setActiveConnection(unsigned int idx);
  43. void setActiveConnection(std::string& _connstring);
  44. std::shared_ptr<URI> getActiveConnection();
  45. void removeConnection(unsigned int idx);
  46. void start();
  47. void stop();
  48. bool isConnected() { return p_client->isConnected(); };
  49. bool isRunning() { return m_running; };
  50. int getCurrentEpoch();
  51. uint64_t getCurrentClientDuration() {
  52. if (p_client && isConnected())
  53. return p_client->m_session->usDuration();
  54. else
  55. return 0;
  56. };
  57. double getPoolDifficulty();
  58. unsigned getConnectionSwitches();
  59. unsigned getEpochChanges();
  60. private:
  61. void rotateConnect();
  62. void setClientHandlers();
  63. void showMiningAt();
  64. void setActiveConnectionCommon(unsigned int idx);
  65. void failovertimer_elapsed(const boost::system::error_code& ec);
  66. void submithrtimer_elapsed(const boost::system::error_code& ec);
  67. void reconnecttimer_elapsed(const boost::system::error_code& ec);
  68. PoolSettings m_Settings;
  69. std::atomic<bool> m_running = {false};
  70. std::atomic<bool> m_stopping = {false};
  71. std::atomic<bool> m_async_pending = {false};
  72. unsigned m_connectionAttempt = 0;
  73. std::string m_selectedHost = ""; // Holds host name (and endpoint) of selected connection
  74. std::atomic<unsigned> m_connectionSwitches = {0};
  75. unsigned m_activeConnectionIdx = 0;
  76. WorkPackage m_currentWp;
  77. boost::asio::io_service::strand m_io_strand;
  78. boost::asio::deadline_timer m_failovertimer;
  79. boost::asio::deadline_timer m_submithrtimer;
  80. boost::asio::deadline_timer m_reconnecttimer;
  81. std::unique_ptr<PoolClient> p_client = nullptr;
  82. std::atomic<unsigned> m_epochChanges = {0};
  83. static PoolManager* m_this;
  84. int m_lastBlock;
  85. };
  86. } // namespace eth
  87. } // namespace dev