/** * stratum_client.hpp */ #include #include #include #include #include #include "nlohmann/json.hpp" #include "ee382n_bitcoin/mining_work.hpp" #ifndef EE382N_BITCOIN_STRATUM_CLIENT_H #define EE382N_BITCOIN_STRATUM_CLIENT_H using json = nlohmann::json; namespace stratum_v1 { class StratumClient { public: StratumClient(const std::string& worker_name, const std::string& worker_pass); bool mining_subscribe(); bool mining_authorize(); bool connect(const std::string& url, const uint16_t port); bool reconnect(); bool mining_submit(const std::string& job_id, const std::string& xnonce2, const std::string& ntime, const std::string& nonce, const std::string& version); bool mining_submit(const stratum_v1::MiningWork& mining_work, uint32_t nonce); void spin_forever(); bool work_available(); stratum_v1::MiningWork get_work(); double get_difficulty() const; std::function new_work_callback; std::function set_difficulty_callback; private: static const size_t MAX_RETRIES = 10; int _id; // Serial message ID int _sock; std::string _xnonce1; size_t _xnonce2_size; double _difficulty; std::string _url; uint16_t _port; std::string _worker_name; std::string _worker_pass; std::unordered_map _response_map; std::queue _recv_q; std::queue _work_q; std::mutex _work_m; std::condition_variable _work_cond; bool transmit(const json& msg) const; bool recv_once(); }; } // namespace stratum_v1 #endif //EE382N_BITCOIN_STRATUM_CLIENT_H