Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ if(CUDA_FOUND)
src/stratum/stratum.cpp
src/miner/miner.cpp
src/util/util.cpp
src/nvml/nvml.cpp)
src/nvml/nvml.cpp
src/merkle/merkle.cpp)

else()
set(COMBINE_LIBS
$<TARGET_FILE:meritminer>
Expand All @@ -68,7 +70,8 @@ else()
src/blake2/blake2b-ref.c
src/stratum/stratum.cpp
src/miner/miner.cpp
src/util/util.cpp)
src/util/util.cpp
src/merkle/merkle.cpp)
endif()

if(CMAKE_HOST_WIN32)
Expand Down Expand Up @@ -117,6 +120,7 @@ file(GLOB H_PICO include/merit/PicoSHA2/*.h)
file(GLOB H_STRATUM include/merit/stratum/*.hpp)
file(GLOB H_UTIL include/merit/util/*.hpp)
file(GLOB H_NVML include/merit/nvml/*.h)
file(GLOB H_MERKLE include/merit/merkle/*.hpp)
install(FILES ${H_PUB} DESTINATION include/merit)
install(FILES ${H_BLAKE} DESTINATION include/merit/blake)
install(FILES ${H_CRYPTO} DESTINATION include/merit/crypto)
Expand All @@ -127,5 +131,6 @@ install(FILES ${H_PICO} DESTINATION include/merit/PicoSHA2)
install(FILES ${H_STRATUM} DESTINATION include/merit/stratum)
install(FILES ${H_UTIL} DESTINATION include/merit/util)
install(FILES ${H_NVML} DESTINATION include/merit/nvml)
install(FILES ${H_MERKLE} DESTINATION include/merit/merkle)
include(CPack)

77 changes: 77 additions & 0 deletions include/merit/merkle/merkle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2018 The Merit Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either vedit_refsion 3 of the License, or
* (at your option) any later vedit_refsion.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* Botan library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects for
* all of the code used other than Botan. If you modify file(s) with
* this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do
* so, delete this exception statement from your version. If you delete
* this exception statement from all source files in the program, then
* also delete it here.
*/

#ifndef MERIT_MERKLE_H
#define MERIT_MERKLE_H

#include <cctype>
#include <list>
#include <vector>
#include <cstdio>
#include <cstring>
#include <string>
#include <memory>
#include <string.h>
#include <bitset>
#include <iostream>

#include "merit/PicoSHA2/picosha2.h"
#include "merit/util/util.hpp"

namespace merit {

namespace merkle {

class MerkleTree {
public:
explicit MerkleTree(const std::vector<char *> &hashes_list) {
steps = calculateSteps(hashes_list);
}

std::vector<std::string> branches();

private:
std::vector<const char *> steps;

/**
* As input takes array of bytes
* So, before use this function you have to convert hex string into byte array(const char*)
*/
std::vector<const char *> calculateSteps(const std::vector<char *> &hashes_list);
void merkleJoin(const char* h1, const char* h2, char* dest);
};

const unsigned char *double_hash(const unsigned char* str, unsigned int size);

}
}

#endif //MERIT_MERKLE_H
11 changes: 9 additions & 2 deletions include/merit/miner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ namespace merit
const char* user,
const char* pass);

bool connect_solo_stratum(
Context* c,
const char* url,
const char* user,
const char* pass);

void disconnect_stratum(Context* c);
bool is_stratum_connected(Context* c);

void init();
bool run_stratum(Context*);
bool run_stratum(Context*, bool solo_mining);
void stop_stratum(Context*);

struct GPUInfo {
Expand All @@ -40,7 +46,8 @@ namespace merit
int fan_speed;
};

bool run_miner(Context*, int workers, int threads_per_worker, const std::vector<int>& gpu_devices);
bool run_miner(Context*, int workers, int threads_per_worker, const std::vector<int>& gpu_devices,
bool solo_mining, const std::string& auth_token);
void stop_miner(Context*);
bool is_stratum_running(Context*);
bool is_miner_running(Context*);
Expand Down
11 changes: 10 additions & 1 deletion include/merit/stratum/stratum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/asio.hpp>
#include <boost/foreach.hpp>
#include <thread>
#include <atomic>

Expand Down Expand Up @@ -88,25 +89,31 @@ namespace merit
void disconnect();
bool subscribe();
bool authorize();
bool run();
bool run(bool solo_mining);
void stop();
bool connected() const;
bool running() const;
bool stopping() const;

MaybeJob get_job();
MaybeJob get_solo_job(const std::string& auth_token);

unsigned int get_solo_job_id();

void submit_work(const util::Work&);

private:
bool reconnect();
bool send(const std::string&);
bool recv(std::string&);
bool recv_with_headers(std::string&);
void cleanup();
bool subscribe_resp();
bool handle_command(const pt::ptree&, const std::string& res);
bool mining_notify(const pt::ptree& params);
bool mining_difficulty(const pt::ptree& params);
bool mining_set_solo_job(const pt::ptree& params);
pt::ptree convert_blocktemplate(const pt::ptree& params);
bool client_reconnect(const pt::ptree& params);
bool client_get_version(const pt::ptree& params);
bool client_show_message(const pt::ptree& params, const pt::ptree& id);
Expand Down Expand Up @@ -139,6 +146,8 @@ namespace merit
std::string _port;
util::bytes _sockbuf;

unsigned int static _solo_job_id;

std::atomic<double> _next_diff;
mutable std::mutex _sock_mutex;
mutable std::mutex _job_mutex;
Expand Down
6 changes: 6 additions & 0 deletions include/merit/util/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ static inline void le32enc(void *pp, uint32_t x)
#if defined(__linux__)
// Linux
#include <endian.h> // for htole32/64
#include <bitset>
#include <netinet/in.h>
#include <iostream>

#elif defined(__APPLE__)
// macOS
Expand Down Expand Up @@ -170,6 +173,9 @@ namespace merit
unsigned char* digest,
const unsigned char* data,
size_t len);

int char2int(char input);
void hex2bin(const char* src, char* target);
}
}
#endif
7 changes: 7 additions & 0 deletions src/merkle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Merkle tree

Contains an implementation of the [Merkle tree](https://en.wikipedia.org/wiki/Merkle_tree) data structure.

| Files | Description |
|:---------------------------------------|:-----------------------------------------|
| [merkle.hpp](merkle.hpp) | Interface to the merkle tree |
121 changes: 121 additions & 0 deletions src/merkle/merkle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (C) 2018 The Merit Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either vedit_refsion 3 of the License, or
* (at your option) any later vedit_refsion.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* Botan library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects for
* all of the code used other than Botan. If you modify file(s) with
* this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do
* so, delete this exception statement from your version. If you delete
* this exception statement from all source files in the program, then
* also delete it here.
*/

#include <merit/merkle/merkle.hpp>

#include "merit/merkle/merkle.hpp"

namespace merit {

namespace merkle {

std::vector<std::string> MerkleTree::branches() {
std::vector<std::string> branches;
std::string tmp_hash;
std::stringstream stream;

for (const auto &step: steps) {
std::string tmp{step, picosha2::k_digest_size};
std::string step_hex;

merit::util::to_hex(tmp.begin(), tmp.end(), step_hex);

branches.push_back(step_hex);
}

return branches;
}

std::vector<const char *> MerkleTree::calculateSteps(const std::vector<char*> &hashes_list) {
std::vector<const char *> steps{};
std::vector<const char *> L{nullptr};
for (const auto &el: hashes_list) {
L.push_back(el);
}

int startL = 2;
unsigned long Ll = L.size();

if (Ll > 1) {
while (true) {

if(Ll == 1)
break;

steps.push_back(L[1]);

if(Ll % 2 == 1)
L.push_back(L[L.size() - 1]);

std::vector<const char*> Ld;

for (int i = startL; i < Ll; i += 2) {
char *join_res = new char[picosha2::k_digest_size];
merkleJoin(L[i], L[i+1], join_res);
Ld.push_back(join_res);
}

L.clear(); L.push_back(nullptr);
for(const auto& el: Ld)
L.push_back(el);

Ll = L.size();

}
}

return steps;
}

void MerkleTree::merkleJoin(const char *h1, const char *h2, char* dest){
std::string joined{h1};
joined += h2;

unsigned char buf[joined.size()]; // array to hold the result.
std::copy(joined.begin(), joined.end(), buf);

auto res = double_hash(buf, joined.size());

// cast from unsigned to the ordinary
std::string final{reinterpret_cast<const char*>(res), 32};

std::copy(final.begin(), final.end(), dest);
}

const unsigned char* double_hash(const unsigned char* str, unsigned int size){
auto *result = new unsigned char[picosha2::k_digest_size];
merit::util::double_sha256(result, str, size);
return result;
}

}
}

6 changes: 6 additions & 0 deletions src/miner/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ namespace merit

void Miner::submit_job(const stratum::Job& j)
{
std::cout << "submit_job" << std::endl;

auto w = stratum::work_from_job(j);
util::MaybeWork prev_work;
{
Expand Down Expand Up @@ -384,9 +386,11 @@ namespace merit
_state = Running;
while(_miner.state() == Miner::Running)
{
// std::cout << "in miner cycle" << std::endl;
auto work = _miner.next_work();

if(!work) {
// std::cout << "There is no work!" << std::endl;
std::this_thread::sleep_for(10ms);
continue;
}
Expand Down Expand Up @@ -469,6 +473,8 @@ namespace merit
stat.attempts++;

if(found) {
std::cout << "CYCLE FOUND" << std::endl;

stat.cycles+=cycles.size();

int idx = 0;
Expand Down
Loading