progminer zano miner fork https://github.com/hyle-team/progminer
This commit is contained in:
103
zano/libpoolprotocols/testing/SimulateClient.cpp
Normal file
103
zano/libpoolprotocols/testing/SimulateClient.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
#include <libdevcore/Log.h>
|
||||
#include <chrono>
|
||||
|
||||
#include "SimulateClient.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
using namespace dev;
|
||||
using namespace eth;
|
||||
|
||||
SimulateClient::SimulateClient(unsigned const& block, float const& difficulty)
|
||||
: PoolClient(), Worker("sim"), m_block(block), m_difficulty(difficulty)
|
||||
{
|
||||
}
|
||||
|
||||
SimulateClient::~SimulateClient() = default;
|
||||
|
||||
void SimulateClient::connect()
|
||||
{
|
||||
// Initialize new session
|
||||
m_connected.store(true, memory_order_relaxed);
|
||||
m_session = unique_ptr<Session>(new Session);
|
||||
m_session->subscribed.store(true, memory_order_relaxed);
|
||||
m_session->authorized.store(true, memory_order_relaxed);
|
||||
|
||||
if (m_onConnected)
|
||||
m_onConnected();
|
||||
|
||||
// No need to worry about starting again.
|
||||
// Worker class prevents that
|
||||
startWorking();
|
||||
}
|
||||
|
||||
void SimulateClient::disconnect()
|
||||
{
|
||||
cnote << "Simulation results : " << EthWhiteBold << "Max "
|
||||
<< dev::getFormattedHashes((double)hr_max, ScaleSuffix::Add, 6) << " Mean "
|
||||
<< dev::getFormattedHashes((double)hr_mean, ScaleSuffix::Add, 6) << EthReset;
|
||||
|
||||
m_conn->addDuration(m_session->duration());
|
||||
m_session = nullptr;
|
||||
m_connected.store(false, memory_order_relaxed);
|
||||
|
||||
if (m_onDisconnected)
|
||||
m_onDisconnected();
|
||||
}
|
||||
|
||||
void SimulateClient::submitHashrate(uint64_t const& rate, string const& id)
|
||||
{
|
||||
(void)rate;
|
||||
(void)id;
|
||||
}
|
||||
|
||||
void SimulateClient::submitSolution(const Solution& solution)
|
||||
{
|
||||
// This is a fake submission only evaluated locally
|
||||
std::chrono::steady_clock::time_point submit_start = std::chrono::steady_clock::now();
|
||||
bool accepted =
|
||||
EthashAux::eval(solution.work.epoch, solution.work.block, solution.work.header, solution.nonce).value <=
|
||||
solution.work.boundary;
|
||||
std::chrono::milliseconds response_delay_ms =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now() - submit_start);
|
||||
|
||||
if (accepted)
|
||||
{
|
||||
if (m_onSolutionAccepted)
|
||||
m_onSolutionAccepted(response_delay_ms, solution.midx, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_onSolutionRejected)
|
||||
m_onSolutionRejected(response_delay_ms, solution.midx);
|
||||
}
|
||||
}
|
||||
|
||||
// Handles all logic here
|
||||
void SimulateClient::workLoop()
|
||||
{
|
||||
m_start_time = std::chrono::steady_clock::now();
|
||||
|
||||
// apply exponential sliding average
|
||||
// ref: https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
|
||||
|
||||
WorkPackage current;
|
||||
current.seed = h256::random(); // We don't actually need a real seed as the epoch
|
||||
// is calculated upon block number (see poolmanager)
|
||||
current.header = h256::random();
|
||||
current.block = m_block;
|
||||
current.boundary = h256(dev::getTargetFromDiff(m_difficulty));
|
||||
m_onWorkReceived(current); // submit new fake job
|
||||
|
||||
cnote << "Using block " << m_block << ", difficulty " << m_difficulty;
|
||||
|
||||
while (m_session)
|
||||
{
|
||||
float hr = Farm::f().HashRate();
|
||||
hr_max = std::max(hr_max, hr);
|
||||
hr_mean = hr_alpha * hr_mean + (1.0f - hr_alpha) * hr;
|
||||
|
||||
this_thread::sleep_for(chrono::milliseconds(200));
|
||||
}
|
||||
}
|
41
zano/libpoolprotocols/testing/SimulateClient.h
Normal file
41
zano/libpoolprotocols/testing/SimulateClient.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <libdevcore/Worker.h>
|
||||
#include <libethcore/EthashAux.h>
|
||||
#include <libethcore/Farm.h>
|
||||
#include <libethcore/Miner.h>
|
||||
|
||||
#include "../PoolClient.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace eth;
|
||||
|
||||
class SimulateClient : public PoolClient, Worker
|
||||
{
|
||||
public:
|
||||
SimulateClient(unsigned const& block, float const& difficulty);
|
||||
~SimulateClient() override;
|
||||
|
||||
void connect() override;
|
||||
void disconnect() override;
|
||||
|
||||
bool isPendingState() override { return false; }
|
||||
string ActiveEndPoint() override { return ""; };
|
||||
|
||||
void submitHashrate(uint64_t const& rate, string const& id) override;
|
||||
void submitSolution(const Solution& solution) override;
|
||||
|
||||
private:
|
||||
|
||||
void workLoop() override;
|
||||
unsigned m_block;
|
||||
float m_difficulty;
|
||||
std::chrono::steady_clock::time_point m_start_time;
|
||||
|
||||
float hr_alpha = 0.45f;
|
||||
float hr_max = 0.0f;
|
||||
float hr_mean = 0.0f;
|
||||
};
|
Reference in New Issue
Block a user