Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone miner #208

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ file(GLOB_RECURSE WalletLegacy WalletLegacy/*)
file(GLOB_RECURSE JsonRpcServer JsonRpcServer/*)
file(GLOB_RECURSE PaymentGate PaymentGate/*)
file(GLOB_RECURSE PaymentGateService PaymentGateService/*)
file(GLOB_RECURSE Miner Miner/*)
file(GLOB_RECURSE GreenWallet GreenWallet/*)
file(GLOB_RECURSE AddressGenerator AddressGenerator/*)

Expand Down Expand Up @@ -71,13 +72,15 @@ if (MSVC)
add_executable(Optimizer ${Optimizer} BinaryInfo/optimizer.rc)
add_executable(PaymentGateService ${PaymentGateService} BinaryInfo/walletd.rc)
add_executable(AddressGenerator ${AddressGenerator} BinaryInfo/vanitygen.rc)
add_executable(Miner ${Miner} BinaryInfo/miner.rc)
else()
add_executable(Daemon ${Daemon})
add_executable(SimpleWallet ${SimpleWallet})
add_executable(GreenWallet ${GreenWallet})
add_executable(Optimizer ${Optimizer})
add_executable(PaymentGateService ${PaymentGateService})
add_executable(AddressGenerator ${AddressGenerator})
add_executable(Miner ${Miner})
endif()

if (MINGW)
Expand All @@ -93,6 +96,7 @@ target_link_libraries(Optimizer PaymentGate Rpc Http CryptoNoteCore Logging Seri
target_link_libraries(PaymentGateService PaymentGate JsonRpcServer InProcessNode Wallet Transfers NodeRpcProxy Rpc Http P2P CryptoNoteCore Logging Serialization Crypto System Checkpoints
CryptoNoteProtocol BlockchainExplorer Common upnpc-static ${Boost_LIBRARIES})
target_link_libraries(AddressGenerator CryptoNoteCore Logging Serialization Crypto Mnemonics Common System ${Boost_LIBRARIES})
target_link_libraries(Miner Rpc Http CryptoNoteCore Logging Serialization Crypto System Common ${Boost_LIBRARIES})

if (OPENSSL_FOUND)
target_link_libraries(Daemon ${OPENSSL_LIBRARIES})
Expand All @@ -101,6 +105,7 @@ if (OPENSSL_FOUND)
target_link_libraries(Optimizer ${OPENSSL_LIBRARIES})
target_link_libraries(PaymentGateService ${OPENSSL_LIBRARIES})
target_link_libraries(AddressGenerator ${OPENSSL_LIBRARIES})
target_link_libraries(Miner ${OPENSSL_LIBRARIES})

# prevent error LNK2019: unresolved external symbol _vsnprintf
if(MSVC AND NOT (MSVC_VERSION LESS 1900))
Expand All @@ -110,6 +115,7 @@ if (OPENSSL_FOUND)
target_link_libraries(PaymentGateService "legacy_stdio_definitions.lib")
target_link_libraries(Optimizer "legacy_stdio_definitions.lib")
target_link_libraries(AddressGenerator "legacy_stdio_definitions.lib")
target_link_libraries(Miner "legacy_stdio_definitions.lib")
endif()
endif ()

Expand All @@ -120,13 +126,15 @@ if (MSVC OR MINGW)
target_link_libraries(PaymentGateService Rpcrt4 ws2_32 advapi32 crypt32 gdi32 user32)
target_link_libraries(Optimizer Rpcrt4 ws2_32 advapi32 crypt32 gdi32 user32)
target_link_libraries(AddressGenerator Rpcrt4 ws2_32 advapi32 crypt32 gdi32 user32)
target_link_libraries(Miner Rpcrt4 ws2_32 advapi32 crypt32 gdi32 user32)
else()
target_link_libraries(Daemon ${EXTRA_LIBRARIES})
target_link_libraries(SimpleWallet ${EXTRA_LIBRARIES})
target_link_libraries(GreenWallet ${EXTRA_LIBRARIES})
target_link_libraries(Optimizer ${EXTRA_LIBRARIES})
target_link_libraries(PaymentGateService ${EXTRA_LIBRARIES})
target_link_libraries(AddressGenerator ${EXTRA_LIBRARIES})
target_link_libraries(Miner ${EXTRA_LIBRARIES})
endif()

if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR APPLE AND NOT ANDROID)
Expand All @@ -143,6 +151,7 @@ if (FREEBSD)
target_link_libraries(Daemon -lthr)
target_link_libraries(Optimizer -lthr)
target_link_libraries(PaymentGateService -lthr)
target_link_libraries(Miner -lthr)
target_link_libraries(GreenWallet -lthr)
target_link_libraries(AddressGenerator -lthr)
endif()
Expand All @@ -159,4 +168,5 @@ set_property(TARGET GreenWallet PROPERTY OUTPUT_NAME "greenwallet")
set_property(TARGET Optimizer PROPERTY OUTPUT_NAME "optimizer")
set_property(TARGET PaymentGateService PROPERTY OUTPUT_NAME "walletd")
set_property(TARGET AddressGenerator PROPERTY OUTPUT_NAME "vanitygen")
set_property(TARGET Miner PROPERTY OUTPUT_NAME "miner")

9 changes: 9 additions & 0 deletions src/CryptoNoteCore/Blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1171,11 +1171,20 @@ uint64_t Blockchain::getCurrentCumulativeBlocksizeLimit() {
}

bool Blockchain::getHashingBlob(const uint32_t height, BinaryArray& blob) {
if (height > m_blobs.size()) {
logger(DEBUGGING) << "internal error in getHashingBlob: requested height " << height << " is bigger than blobs height" << m_blobs.size();
return false;
}

blob = m_blobs[height];

return true;
}

std::vector<BinaryArray> Blockchain::getHashingBlobs() {
return m_blobs;
}

bool Blockchain::checkProofOfWork(Crypto::cn_context& context, const Block& block, difficulty_type currentDiffic, Crypto::Hash& proofOfWork) {
std::list<Crypto::Hash> dummy_alt_chain;

Expand Down
1 change: 1 addition & 0 deletions src/CryptoNoteCore/Blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ namespace CryptoNote {
bool isInCheckpointZone(const uint32_t height);

bool getHashingBlob(const uint32_t height, BinaryArray& blob);
std::vector<BinaryArray> getHashingBlobs();

template<class visitor_t> bool scanOutputKeysForIndexes(const KeyInput& tx_in_to_key, visitor_t& vis, uint32_t* pmax_related_block_height = NULL);

Expand Down
8 changes: 8 additions & 0 deletions src/CryptoNoteCore/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,14 @@ bool Core::getBlockLongHash(Crypto::cn_context &context, const Block& b, Crypto:
return m_blockchain.getBlockLongHash(context, b, res);
}

bool Core::getHashingBlob(const uint32_t height, BinaryArray& blob) {
return m_blockchain.getHashingBlob(height, blob);
}

std::vector<BinaryArray> Core::getHashingBlobs() {
return m_blockchain.getHashingBlobs();
}

//void Core::get_all_known_block_ids(std::list<Crypto::Hash> &main, std::list<Crypto::Hash> &alt, std::list<Crypto::Hash> &invalid) {
// m_blockchain.get_all_known_block_ids(main, alt, invalid);
//}
Expand Down
2 changes: 2 additions & 0 deletions src/CryptoNoteCore/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ namespace CryptoNote {
virtual bool handle_block_found(Block& b) override;
virtual bool get_block_template(Block& b, const AccountKeys& acc, difficulty_type& diffic, uint32_t& height, const BinaryArray& ex_nonce) override;
virtual bool getBlockLongHash(Crypto::cn_context &context, const Block& b, Crypto::Hash& res) override;
virtual bool getHashingBlob(const uint32_t height, BinaryArray& blob) override;
virtual std::vector<BinaryArray> getHashingBlobs() override;

bool addObserver(ICoreObserver* observer) override;
bool removeObserver(ICoreObserver* observer) override;
Expand Down
2 changes: 2 additions & 0 deletions src/CryptoNoteCore/ICore.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ class ICore {
virtual bool saveBlockchain() = 0;

virtual bool getBlockLongHash(Crypto::cn_context &context, const Block& b, Crypto::Hash& res) = 0;
virtual bool getHashingBlob(const uint32_t height, BinaryArray& blob) = 0;
virtual std::vector<BinaryArray> getHashingBlobs() = 0;

virtual bool getMixin(const Transaction& transaction, uint64_t& mixin) = 0;
virtual bool isInCheckpointZone(uint32_t height) const = 0;
Expand Down
109 changes: 109 additions & 0 deletions src/Miner/BlockchainMonitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) 2012-2016, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Karbo.
//
// Karbo is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Karbo 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Karbo. If not, see <http://www.gnu.org/licenses/>.

#include "BlockchainMonitor.h"

#include "Common/StringTools.h"

#include <System/EventLock.h>
#include <System/Timer.h>
#include <System/InterruptedException.h>

#include "Rpc/CoreRpcServerCommandsDefinitions.h"
#include "Rpc/JsonRpc.h"
#include "HTTP/httplib.h"

#if defined(WIN32)
#undef ERROR
#endif

BlockchainMonitor::BlockchainMonitor(System::Dispatcher& dispatcher, const std::string& daemonHost, uint16_t daemonPort, size_t pollingInterval, Logging::ILogger& logger):
m_dispatcher(dispatcher),
m_daemonHost(daemonHost),
m_daemonPort(daemonPort),
m_pollingInterval(pollingInterval),
m_stopped(false),
m_httpEvent(dispatcher),
m_sleepingContext(dispatcher),
m_logger(logger, "BlockchainMonitor") {

m_httpEvent.set();
}

void BlockchainMonitor::waitBlockchainUpdate() {
m_logger(Logging::DEBUGGING) << "Waiting for blockchain updates";
m_stopped = false;

Crypto::Hash lastBlockHash = requestLastBlockHash();

while(!m_stopped) {
m_sleepingContext.spawn([this] () {
System::Timer timer(m_dispatcher);
timer.sleep(std::chrono::seconds(m_pollingInterval));
});

m_sleepingContext.wait();

if (lastBlockHash != requestLastBlockHash()) {
m_logger(Logging::DEBUGGING) << "Blockchain has been updated";
break;
}
}

if (m_stopped) {
m_logger(Logging::DEBUGGING) << "Blockchain monitor has been stopped";
throw System::InterruptedException();
}
}

void BlockchainMonitor::stop() {
m_logger(Logging::DEBUGGING) << "Sending stop signal to blockchain monitor";
m_stopped = true;

m_sleepingContext.interrupt();
m_sleepingContext.wait();
}

Crypto::Hash BlockchainMonitor::requestLastBlockHash() {
m_logger(Logging::DEBUGGING) << "Requesting last block hash";

try {
httplib::Client client(m_daemonHost, m_daemonPort);

CryptoNote::COMMAND_RPC_GET_LAST_BLOCK_HEADER::request request;
CryptoNote::COMMAND_RPC_GET_LAST_BLOCK_HEADER::response response;

System::EventLock lk(m_httpEvent);
CryptoNote::JsonRpc::invokeJsonRpcCommand(client, "getlastblockheader", request, response);

if (response.status != CORE_RPC_STATUS_OK) {
throw std::runtime_error("Core responded with wrong status: " + response.status);
}

Crypto::Hash blockHash;
if (!Common::podFromHex(response.block_header.hash, blockHash)) {
throw std::runtime_error("Couldn't parse block hash: " + response.block_header.hash);
}

m_logger(Logging::DEBUGGING) << "Last block hash: " << Common::podToHex(blockHash);

return blockHash;
} catch (std::exception& e) {
m_logger(Logging::ERROR) << "Failed to request last block hash: " << e.what();
throw;
}
}
46 changes: 46 additions & 0 deletions src/Miner/BlockchainMonitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2012-2016, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Karbo.
//
// Karbo is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Karbo 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Karbo. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include "CryptoTypes.h"

#include <System/ContextGroup.h>
#include <System/Dispatcher.h>
#include <System/Event.h>

#include "Logging/LoggerRef.h"

class BlockchainMonitor {
public:
BlockchainMonitor(System::Dispatcher& dispatcher, const std::string& daemonHost, uint16_t daemonPort, size_t pollingInterval, Logging::ILogger& logger);

void waitBlockchainUpdate();
void stop();
private:
System::Dispatcher& m_dispatcher;
std::string m_daemonHost;
uint16_t m_daemonPort;
size_t m_pollingInterval;
bool m_stopped;
System::Event m_httpEvent;
System::ContextGroup m_sleepingContext;

Logging::LoggerRef m_logger;

Crypto::Hash requestLastBlockHash();
};
Loading