Skip to content
This repository has been archived by the owner on Jan 12, 2020. It is now read-only.

Commit

Permalink
Add support for masternodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtl1979 committed Oct 6, 2018
1 parent b38b915 commit c249297
Show file tree
Hide file tree
Showing 33 changed files with 372 additions and 26 deletions.
10 changes: 9 additions & 1 deletion cryptonote/.gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
.git* export-ignore
/CMakeLists.txt export-subst
/CMakeLists.txt export-subst
*.sh text eol=lf
*.h text
*.c text
*.cc text
*.cpp text
*.java text
*.md text
*.rc text eol=crlf
3 changes: 3 additions & 0 deletions cryptonote/include/INode.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -99,6 +100,7 @@ class INode {
virtual uint32_t getLocalBlockCount() const = 0;
virtual uint32_t getKnownBlockCount() const = 0;
virtual uint64_t getLastLocalBlockTimestamp() const = 0;
virtual std::string getLastFeeAddress() const = 0;

virtual void getBlockHashesByTimestamps(uint64_t timestampBegin, size_t secondsCount, std::vector<Crypto::Hash>& blockHashes, const Callback& callback) = 0;
virtual void getTransactionHashesByPaymentId(const Crypto::Hash& paymentId, std::vector<Crypto::Hash>& transactionHashes, const Callback& callback) = 0;
Expand All @@ -116,6 +118,7 @@ class INode {
virtual void getBlocks(const std::vector<Crypto::Hash>& blockHashes, std::vector<BlockDetails>& blocks, const Callback& callback) = 0;
virtual void getBlock(const uint32_t blockHeight, BlockDetails &block, const Callback& callback) = 0;
virtual void getTransactions(const std::vector<Crypto::Hash>& transactionHashes, std::vector<TransactionDetails>& transactions, const Callback& callback) = 0;
virtual void getFeeAddress(std::string &feeAddress, const Callback& callback) = 0;
virtual void isSynchronized(bool& syncStatus, const Callback& callback) = 0;
};

Expand Down
8 changes: 8 additions & 0 deletions cryptonote/src/CryptoNoteCore/TransactionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ const TransactionInput& getInputChecked(const CryptoNote::TransactionPrefix& tra

// TransactionOutput helper functions

uint64_t getTransactionOutputAmount(const TransactionOutput& out) {
if (out.target.type() == typeid(KeyOutput)) {
return out.amount;
}

return 0;
}

TransactionTypes::OutputType getTransactionOutputType(const TransactionOutputTarget& out) {
if (out.type() == typeid(KeyOutput)) {
return TransactionTypes::OutputType::Key;
Expand Down
1 change: 1 addition & 0 deletions cryptonote/src/CryptoNoteCore/TransactionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const TransactionInput& getInputChecked(const CryptoNote::TransactionPrefix& tra
bool isOutToKey(const Crypto::PublicKey& spendPublicKey, const Crypto::PublicKey& outKey, const Crypto::KeyDerivation& derivation, size_t keyIndex);

// TransactionOutput helper functions
uint64_t getTransactionOutputAmount(const TransactionOutput& out);
TransactionTypes::OutputType getTransactionOutputType(const TransactionOutputTarget& out);
const TransactionOutput& getOutputChecked(const CryptoNote::TransactionPrefix& transaction, size_t index);
const TransactionOutput& getOutputChecked(const CryptoNote::TransactionPrefix& transaction, size_t index, TransactionTypes::OutputType type);
Expand Down
24 changes: 23 additions & 1 deletion cryptonote/src/Daemon/Daemon.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2016-2018, The Karbo developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -73,6 +74,8 @@ namespace
const command_line::arg_descriptor<std::vector<std::string>> arg_genesis_block_reward_address = { "genesis-block-reward-address", "" };
const command_line::arg_descriptor<bool> arg_blockexplorer_on = {"enable_blockexplorer", "Enable blockchain explorer RPC", false};
const command_line::arg_descriptor<std::vector<std::string>> arg_enable_cors = { "enable-cors", "Adds header 'Access-Control-Allow-Origin' to the daemon's RPC responses. Uses the value as domain. Use * for all" };
const command_line::arg_descriptor<std::string> arg_set_fee_address = { "fee-address", "Sets fee address for light wallets to the daemon's RPC responses.", "" };
const command_line::arg_descriptor<std::string> arg_set_view_key = { "view-key", "Sets private view key to check for masternode's fee.", "" };
const command_line::arg_descriptor<bool> arg_testnet_on = {"testnet", "Used to deploy test nets. Checkpoints and hardcoded seeds are ignored, "
"network id is changed. Use it with --data-dir flag. The wallet must be launched with --testnet flag.", false};
const command_line::arg_descriptor<std::string> arg_load_checkpoints = {"load-checkpoints", "<default|filename> Use builtin default checkpoints or checkpoint csv file for faster initial blockchain sync", ""};
Expand Down Expand Up @@ -174,6 +177,8 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, arg_console);
command_line::add_arg(desc_cmd_sett, arg_testnet_on);
command_line::add_arg(desc_cmd_sett, arg_enable_cors);
command_line::add_arg(desc_cmd_sett, arg_set_fee_address);
command_line::add_arg(desc_cmd_sett, arg_set_view_key);
command_line::add_arg(desc_cmd_sett, arg_blockexplorer_on);
command_line::add_arg(desc_cmd_sett, arg_print_genesis_tx);
command_line::add_arg(desc_cmd_sett, arg_genesis_block_reward_address);
Expand Down Expand Up @@ -350,7 +355,24 @@ int main(int argc, char* argv[])

logger(INFO) << "Starting core rpc server on address " << rpcConfig.getBindAddress();
rpcServer.start(rpcConfig.bindIp, rpcConfig.bindPort);
rpcServer.enableCors(command_line::get_arg(vm, arg_enable_cors));
rpcServer.enableCors(command_line::get_arg(vm, arg_enable_cors));
if (command_line::has_arg(vm, arg_set_fee_address)) {
std::string addr_str = command_line::get_arg(vm, arg_set_fee_address);
if (!addr_str.empty()) {
AccountPublicAddress acc = boost::value_initialized<AccountPublicAddress>();
if (!currency.parseAccountAddressString(addr_str, acc)) {
logger(ERROR, BRIGHT_RED) << "Bad fee address: " << addr_str;
return 1;
}
rpcServer.setFeeAddress(addr_str, acc);
}
}
if (command_line::has_arg(vm, arg_set_view_key)) {
std::string vk_str = command_line::get_arg(vm, arg_set_view_key);
if (!vk_str.empty()) {
rpcServer.setViewKey(vk_str);
}
}
logger(INFO) << "Core rpc server started ok";

Tools::SignalHandler::install([&dch, &p2psrv] {
Expand Down
10 changes: 10 additions & 0 deletions cryptonote/src/InProcessNode/InProcessNode.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -551,6 +552,10 @@ BlockHeaderInfo InProcessNode::getLastLocalBlockHeaderInfo() const {
return lastLocalBlockHeaderInfo;
}

std::string InProcessNode::getLastFeeAddress() const {
return "";
}

void InProcessNode::getBlockHashesByTimestamps(uint64_t timestampBegin, size_t secondsCount, std::vector<Crypto::Hash>& blockHashes, const Callback& callback) {
std::unique_lock<std::mutex> lock(mutex);
if (state != INITIALIZED) {
Expand Down Expand Up @@ -887,6 +892,11 @@ std::error_code InProcessNode::doGetTransactions(const std::vector<Crypto::Hash>
return std::error_code();
}

void InProcessNode::getFeeAddress(std::string& feeAddress, const Callback& callback) {
feeAddress = "";
callback({});
}

void InProcessNode::isSynchronized(bool& syncStatus, const Callback& callback) {
std::unique_lock<std::mutex> lock(mutex);
if (state != INITIALIZED) {
Expand Down
3 changes: 3 additions & 0 deletions cryptonote/src/InProcessNode/InProcessNode.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -66,6 +67,7 @@ class InProcessNode : public INode, public CryptoNote::ICryptoNoteProtocolObserv
virtual uint32_t getLocalBlockCount() const override;
virtual uint32_t getKnownBlockCount() const override;
virtual uint64_t getLastLocalBlockTimestamp() const override;
virtual std::string getLastFeeAddress() const override;

virtual void getBlockHashesByTimestamps(uint64_t timestampBegin, size_t secondsCount, std::vector<Crypto::Hash>& blockHashes, const Callback& callback) override;
virtual void getTransactionHashesByPaymentId(const Crypto::Hash& paymentId, std::vector<Crypto::Hash>& transactionHashes, const Callback& callback) override;
Expand All @@ -86,6 +88,7 @@ class InProcessNode : public INode, public CryptoNote::ICryptoNoteProtocolObserv
virtual void getBlocks(const std::vector<Crypto::Hash>& blockHashes, std::vector<BlockDetails>& blocks, const Callback& callback) override;
virtual void getBlock(const uint32_t blockHeight, BlockDetails &block, const Callback& callback) override;
virtual void getTransactions(const std::vector<Crypto::Hash>& transactionHashes, std::vector<TransactionDetails>& transactions, const Callback& callback) override;
virtual void getFeeAddress(std::string& feeAddress, const Callback& callback) override;
virtual void isSynchronized(bool& syncStatus, const Callback& callback) override;

private:
Expand Down
43 changes: 43 additions & 0 deletions cryptonote/src/NodeRpcProxy/NodeRpcProxy.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -95,6 +96,7 @@ void NodeRpcProxy::resetInternalState() {
lastLocalBlockHeaderInfo.difficulty = 0;
lastLocalBlockHeaderInfo.reward = 0;
m_knownTxs.clear();
m_feeaddress = "";
}

void NodeRpcProxy::init(const INode::Callback& callback) {
Expand Down Expand Up @@ -141,6 +143,13 @@ bool NodeRpcProxy::shutdown() {
return true;
}

void NodeRpcProxy::feeAddressCallback(std::error_code ec) {
std::lock_guard<std::mutex> lock(m_mutex);
if (ec) {
m_feeaddress = "";
}
}

void NodeRpcProxy::workerThread(const INode::Callback& initialized_callback) {
try {
Dispatcher dispatcher;
Expand All @@ -160,6 +169,11 @@ void NodeRpcProxy::workerThread(const INode::Callback& initialized_callback) {
m_cv_initialized.notify_all();
}

{
std::error_code ec;
getFeeAddress(m_feeaddress, std::bind(&NodeRpcProxy::feeAddressCallback, this, ec));
}

updateNodeStatus();

initialized_callback(std::error_code());
Expand Down Expand Up @@ -347,6 +361,11 @@ BlockHeaderInfo NodeRpcProxy::getLastLocalBlockHeaderInfo() const {
return lastLocalBlockHeaderInfo;
}

std::string NodeRpcProxy::getLastFeeAddress() const {
std::lock_guard<std::mutex> lock(m_mutex);
return m_feeaddress;
}

void NodeRpcProxy::getBlockHashesByTimestamps(uint64_t timestampBegin, size_t secondsCount, std::vector<Crypto::Hash>& blockHashes, const Callback& callback) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_state != STATE_INITIALIZED) {
Expand Down Expand Up @@ -497,6 +516,17 @@ void NodeRpcProxy::getTransactions(const std::vector<Crypto::Hash>& transactionH
scheduleRequest(std::bind(&NodeRpcProxy::doGetTransactions, this, std::cref(transactionHashes), std::ref(transactions)), callback);
}

void NodeRpcProxy::getFeeAddress(std::string &feeAddress, const Callback& callback) {

std::lock_guard<std::mutex> lock(m_mutex);
if (m_state != STATE_INITIALIZED) {
callback(make_error_code(error::NOT_INITIALIZED));
return;
}

scheduleRequest(std::bind(&NodeRpcProxy::doGetFeeAddress, this, std::ref(feeAddress)), callback);
}

void NodeRpcProxy::isSynchronized(bool& syncStatus, const Callback& callback) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_state != STATE_INITIALIZED) {
Expand Down Expand Up @@ -715,6 +745,19 @@ std::error_code NodeRpcProxy::doGetTransactions(const std::vector<Crypto::Hash>&
return ec;
}

std::error_code NodeRpcProxy::doGetFeeAddress(std::string& feeAddress) {
COMMAND_RPC_GET_FEE_ADDRESS::request req = AUTO_VAL_INIT(req);
COMMAND_RPC_GET_FEE_ADDRESS::response resp = AUTO_VAL_INIT(resp);

std::error_code ec = jsonCommand("/feeaddress", req, resp);
if (ec) {
return ec;
}

feeAddress = std::move(resp.fee_address);
return ec;
}

void NodeRpcProxy::scheduleRequest(std::function<std::error_code()>&& procedure, const Callback& callback) {
// callback is located on stack, so copy it inside binder
class Wrapper {
Expand Down
7 changes: 7 additions & 0 deletions cryptonote/src/NodeRpcProxy/NodeRpcProxy.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -64,6 +65,7 @@ class NodeRpcProxy : public CryptoNote::INode {
virtual uint32_t getLocalBlockCount() const override;
virtual uint32_t getKnownBlockCount() const override;
virtual uint64_t getLastLocalBlockTimestamp() const override;
virtual std::string getLastFeeAddress() const override;

virtual void getBlockHashesByTimestamps(uint64_t timestampBegin, size_t secondsCount, std::vector<Crypto::Hash>& blockHashes, const Callback& callback) override;
virtual void getTransactionHashesByPaymentId(const Crypto::Hash& paymentId, std::vector<Crypto::Hash>& transactionHashes, const Callback& callback) override;
Expand All @@ -81,6 +83,7 @@ class NodeRpcProxy : public CryptoNote::INode {
virtual void getBlocks(const std::vector<Crypto::Hash>& blockHashes, std::vector<BlockDetails>& blocks, const Callback& callback) override;
virtual void getBlock(const uint32_t blockHeight, BlockDetails &block, const Callback& callback) override;
virtual void getTransactions(const std::vector<Crypto::Hash>& transactionHashes, std::vector<TransactionDetails>& transactions, const Callback& callback) override;
virtual void getFeeAddress(std::string& feeAddress, const Callback& callback) override;
virtual void isSynchronized(bool& syncStatus, const Callback& callback) override;

unsigned int rpcTimeout() const { return m_rpcTimeout; }
Expand Down Expand Up @@ -114,6 +117,7 @@ class NodeRpcProxy : public CryptoNote::INode {
std::error_code doGetBlock(const uint32_t blockHeight, BlockDetails& block);
std::error_code doGetTransactionHashesByPaymentId(const Crypto::Hash& paymentId, std::vector<Crypto::Hash>& transactionHashes);
std::error_code doGetTransactions(const std::vector<Crypto::Hash>& transactionHashes, std::vector<TransactionDetails>& transactions);
std::error_code doGetFeeAddress(std::string& feeAddress);

void scheduleRequest(std::function<std::error_code()>&& procedure, const Callback& callback);
template <typename Request, typename Response>
Expand Down Expand Up @@ -157,6 +161,9 @@ class NodeRpcProxy : public CryptoNote::INode {
//protect it with mutex if decided to add worker threads
std::unordered_set<Crypto::Hash> m_knownTxs;

void feeAddressCallback(std::error_code ec);
std::string m_feeaddress;

bool m_connected;
};
}
4 changes: 4 additions & 0 deletions cryptonote/src/PaymentGate/NodeFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2018, The Bittorium developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -38,6 +39,7 @@ class NodeRpcStub: public CryptoNote::INode {
virtual uint32_t getLocalBlockCount() const override { return 0; }
virtual uint32_t getKnownBlockCount() const override { return 0; }
virtual uint64_t getLastLocalBlockTimestamp() const override { return 0; }
virtual std::string getLastFeeAddress() const override { return ""; }

virtual void getBlockHashesByTimestamps(uint64_t timestampBegin, size_t secondsCount, std::vector<Crypto::Hash>& blockHashes, const Callback& callback) override {
callback(std::error_code());
Expand Down Expand Up @@ -83,6 +85,8 @@ class NodeRpcStub: public CryptoNote::INode {
virtual void getTransactions(const std::vector<Crypto::Hash>& transactionHashes, std::vector<CryptoNote::TransactionDetails>& transactions,
const Callback& callback) override { }

virtual void getFeeAddress(std::string& feeAddress, const Callback& callback) override { }

virtual void isSynchronized(bool& syncStatus, const Callback& callback) override { }

};
Expand Down
4 changes: 2 additions & 2 deletions cryptonote/src/PaymentGateService/RpcNodeConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ RpcNodeConfiguration::RpcNodeConfiguration() {

void RpcNodeConfiguration::initOptions(boost::program_options::options_description& desc) {
desc.add_options()
("daemon-address", po::value<std::string>()->default_value("localhost"), "PinkstarcoinV2d address")
("daemon-port", po::value<uint16_t>()->default_value(39984), "daemon port");
("daemon-address", po::value<std::string>()->default_value("localhost"), "Bittoriumd address")
("daemon-port", po::value<uint16_t>()->default_value(34916), "daemon port");
}

void RpcNodeConfiguration::init(const boost::program_options::variables_map& options) {
Expand Down
2 changes: 1 addition & 1 deletion cryptonote/src/PaymentGateService/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ int main(int argc, char** argv) {
return 0; //help message requested or so
}

Logging::LoggerRef(pg.getLogger(), "main")(Logging::INFO) << "walled v" << PROJECT_VERSION_LONG;
Logging::LoggerRef(pg.getLogger(), "main")(Logging::INFO) << "walletd v" << PROJECT_VERSION_LONG;

const auto& config = pg.getConfig();

Expand Down
18 changes: 18 additions & 0 deletions cryptonote/src/Rpc/CoreRpcServerCommandsDefinitions.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2017-2018, The Karbo developers
//
// This file is part of Bytecoin.
//
Expand Down Expand Up @@ -286,6 +287,7 @@ struct COMMAND_RPC_GET_INFO {
uint32_t hashrate;
std::string version;
bool synced;
std::string fee_address;

void serialize(ISerializer &s) {
KV_MEMBER(status)
Expand All @@ -303,6 +305,7 @@ struct COMMAND_RPC_GET_INFO {
KV_MEMBER(hashrate)
KV_MEMBER(synced)
KV_MEMBER(version)
KV_MEMBER(fee_address)
}
};
};
Expand All @@ -319,6 +322,21 @@ struct COMMAND_RPC_STOP_DAEMON {
typedef STATUS_STRUCT response;
};

//-----------------------------------------------
struct COMMAND_RPC_GET_FEE_ADDRESS {
typedef EMPTY_STRUCT request;

struct response {
std::string fee_address;
std::string status;

void serialize(ISerializer &s) {
KV_MEMBER(fee_address)
KV_MEMBER(status)
}
};
};

//
struct COMMAND_RPC_GETBLOCKCOUNT {
typedef std::vector<std::string> request;
Expand Down
Loading

0 comments on commit c249297

Please sign in to comment.