daemon, wallet: new pay for RPC use system
Daemons intended for public use can be set up to require payment in the form of hashes in exchange for RPC service. This enables public daemons to receive payment for their work over a large number of calls. This system behaves similarly to a pool, so payment takes the form of valid blocks every so often, yielding a large one off payment, rather than constant micropayments. This system can also be used by third parties as a "paywall" layer, where users of a service can pay for use by mining Monero to the service provider's address. An example of this for web site access is Primo, a Monero mining based website "paywall": https://github.com/selene-kovri/primo This has some advantages: - incentive to run a node providing RPC services, thereby promoting the availability of third party nodes for those who can't run their own - incentive to run your own node instead of using a third party's, thereby promoting decentralization - decentralized: payment is done between a client and server, with no third party needed - private: since the system is "pay as you go", you don't need to identify yourself to claim a long lived balance - no payment occurs on the blockchain, so there is no extra transactional load - one may mine with a beefy server, and use those credits from a phone, by reusing the client ID (at the cost of some privacy) - no barrier to entry: anyone may run a RPC node, and your expected revenue depends on how much work you do - Sybil resistant: if you run 1000 idle RPC nodes, you don't magically get more revenue - no large credit balance maintained on servers, so they have no incentive to exit scam - you can use any/many node(s), since there's little cost in switching servers - market based prices: competition between servers to lower costs - incentive for a distributed third party node system: if some public nodes are overused/slow, traffic can move to others - increases network security - helps counteract mining pools' share of the network hash rate - zero incentive for a payer to "double spend" since a reorg does not give any money back to the miner And some disadvantages: - low power clients will have difficulty mining (but one can optionally mine in advance and/or with a faster machine) - payment is "random", so a server might go a long time without a block before getting one - a public node's overall expected payment may be small Public nodes are expected to compete to find a suitable level for cost of service. The daemon can be set up this way to require payment for RPC services: monerod --rpc-payment-address 4xxxxxx \ --rpc-payment-credits 250 --rpc-payment-difficulty 1000 These values are an example only. The --rpc-payment-difficulty switch selects how hard each "share" should be, similar to a mining pool. The higher the difficulty, the fewer shares a client will find. The --rpc-payment-credits switch selects how many credits are awarded for each share a client finds. Considering both options, clients will be awarded credits/difficulty credits for every hash they calculate. For example, in the command line above, 0.25 credits per hash. A client mining at 100 H/s will therefore get an average of 25 credits per second. For reference, in the current implementation, a credit is enough to sync 20 blocks, so a 100 H/s client that's just starting to use Monero and uses this daemon will be able to sync 500 blocks per second. The wallet can be set to automatically mine if connected to a daemon which requires payment for RPC usage. It will try to keep a balance of 50000 credits, stopping mining when it's at this level, and starting again as credits are spent. With the example above, a new client will mine this much credits in about half an hour, and this target is enough to sync 500000 blocks (currently about a third of the monero blockchain). There are three new settings in the wallet: - credits-target: this is the amount of credits a wallet will try to reach before stopping mining. The default of 0 means 50000 credits. - auto-mine-for-rpc-payment-threshold: this controls the minimum credit rate which the wallet considers worth mining for. If the daemon credits less than this ratio, the wallet will consider mining to be not worth it. In the example above, the rate is 0.25 - persistent-rpc-client-id: if set, this allows the wallet to reuse a client id across runs. This means a public node can tell a wallet that's connecting is the same as one that connected previously, but allows a wallet to keep their credit balance from one run to the other. Since the wallet only mines to keep a small credit balance, this is not normally worth doing. However, someone may want to mine on a fast server, and use that credit balance on a low power device such as a phone. If left unset, a new client ID is generated at each wallet start, for privacy reasons. To mine and use a credit balance on two different devices, you can use the --rpc-client-secret-key switch. A wallet's client secret key can be found using the new rpc_payments command in the wallet. Note: anyone knowing your RPC client secret key is able to use your credit balance. The wallet has a few new commands too: - start_mining_for_rpc: start mining to acquire more credits, regardless of the auto mining settings - stop_mining_for_rpc: stop mining to acquire more credits - rpc_payments: display information about current credits with the currently selected daemon The node has an extra command: - rpc_payments: display information about clients and their balances The node will forget about any balance for clients which have been inactive for 6 months. Balances carry over on node restart.
This commit is contained in:
@@ -29,12 +29,14 @@
|
||||
include_directories(SYSTEM ${ZMQ_INCLUDE_PATH})
|
||||
|
||||
set(rpc_base_sources
|
||||
rpc_args.cpp)
|
||||
rpc_args.cpp
|
||||
rpc_payment_signature.cpp
|
||||
rpc_handler.cpp)
|
||||
|
||||
set(rpc_sources
|
||||
bootstrap_daemon.cpp
|
||||
core_rpc_server.cpp
|
||||
rpc_handler.cpp
|
||||
rpc_payment.cpp
|
||||
instanciations)
|
||||
|
||||
set(daemon_messages_sources
|
||||
@@ -47,7 +49,9 @@ set(daemon_rpc_server_sources
|
||||
|
||||
|
||||
set(rpc_base_headers
|
||||
rpc_args.h)
|
||||
rpc_args.h
|
||||
rpc_payment_signature.h
|
||||
rpc_handler.h)
|
||||
|
||||
set(rpc_headers
|
||||
rpc_handler.h)
|
||||
@@ -58,6 +62,7 @@ set(daemon_rpc_server_headers)
|
||||
set(rpc_daemon_private_headers
|
||||
bootstrap_daemon.h
|
||||
core_rpc_server.h
|
||||
rpc_payment.h
|
||||
core_rpc_server_commands_defs.h
|
||||
core_rpc_server_error_codes.h)
|
||||
|
||||
|
||||
+641
-116
File diff suppressed because it is too large
Load Diff
@@ -42,6 +42,7 @@
|
||||
#include "cryptonote_core/cryptonote_core.h"
|
||||
#include "p2p/net_node.h"
|
||||
#include "cryptonote_protocol/cryptonote_protocol_handler.h"
|
||||
#include "rpc_payment.h"
|
||||
|
||||
#undef MONERO_DEFAULT_LOG_CATEGORY
|
||||
#define MONERO_DEFAULT_LOG_CATEGORY "daemon.rpc"
|
||||
@@ -71,6 +72,9 @@ namespace cryptonote
|
||||
static const command_line::arg_descriptor<bool> arg_rpc_ssl_allow_any_cert;
|
||||
static const command_line::arg_descriptor<std::string> arg_bootstrap_daemon_address;
|
||||
static const command_line::arg_descriptor<std::string> arg_bootstrap_daemon_login;
|
||||
static const command_line::arg_descriptor<std::string> arg_rpc_payment_address;
|
||||
static const command_line::arg_descriptor<uint64_t> arg_rpc_payment_difficulty;
|
||||
static const command_line::arg_descriptor<uint64_t> arg_rpc_payment_credits;
|
||||
|
||||
typedef epee::net_utils::connection_context_base connection_context;
|
||||
|
||||
@@ -78,6 +82,7 @@ namespace cryptonote
|
||||
core& cr
|
||||
, nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> >& p2p
|
||||
);
|
||||
~core_rpc_server();
|
||||
|
||||
static void init_options(boost::program_options::options_description& desc);
|
||||
bool init(
|
||||
@@ -169,6 +174,12 @@ namespace cryptonote
|
||||
MAP_JON_RPC_WE("get_txpool_backlog", on_get_txpool_backlog, COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG)
|
||||
MAP_JON_RPC_WE("get_output_distribution", on_get_output_distribution, COMMAND_RPC_GET_OUTPUT_DISTRIBUTION)
|
||||
MAP_JON_RPC_WE_IF("prune_blockchain", on_prune_blockchain, COMMAND_RPC_PRUNE_BLOCKCHAIN, !m_restricted)
|
||||
MAP_JON_RPC_WE("rpc_access_info", on_rpc_access_info, COMMAND_RPC_ACCESS_INFO)
|
||||
MAP_JON_RPC_WE("rpc_access_submit_nonce",on_rpc_access_submit_nonce, COMMAND_RPC_ACCESS_SUBMIT_NONCE)
|
||||
MAP_JON_RPC_WE("rpc_access_pay", on_rpc_access_pay, COMMAND_RPC_ACCESS_PAY)
|
||||
MAP_JON_RPC_WE_IF("rpc_access_tracking", on_rpc_access_tracking, COMMAND_RPC_ACCESS_TRACKING, !m_restricted)
|
||||
MAP_JON_RPC_WE_IF("rpc_access_data", on_rpc_access_data, COMMAND_RPC_ACCESS_DATA, !m_restricted)
|
||||
MAP_JON_RPC_WE_IF("rpc_access_account", on_rpc_access_account, COMMAND_RPC_ACCESS_ACCOUNT, !m_restricted)
|
||||
END_JSON_RPC_MAP()
|
||||
END_URI_MAP2()
|
||||
|
||||
@@ -236,6 +247,12 @@ namespace cryptonote
|
||||
bool on_get_txpool_backlog(const COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
|
||||
bool on_get_output_distribution(const COMMAND_RPC_GET_OUTPUT_DISTRIBUTION::request& req, COMMAND_RPC_GET_OUTPUT_DISTRIBUTION::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
|
||||
bool on_prune_blockchain(const COMMAND_RPC_PRUNE_BLOCKCHAIN::request& req, COMMAND_RPC_PRUNE_BLOCKCHAIN::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
|
||||
bool on_rpc_access_info(const COMMAND_RPC_ACCESS_INFO::request& req, COMMAND_RPC_ACCESS_INFO::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
|
||||
bool on_rpc_access_submit_nonce(const COMMAND_RPC_ACCESS_SUBMIT_NONCE::request& req, COMMAND_RPC_ACCESS_SUBMIT_NONCE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
|
||||
bool on_rpc_access_pay(const COMMAND_RPC_ACCESS_PAY::request& req, COMMAND_RPC_ACCESS_PAY::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
|
||||
bool on_rpc_access_tracking(const COMMAND_RPC_ACCESS_TRACKING::request& req, COMMAND_RPC_ACCESS_TRACKING::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
|
||||
bool on_rpc_access_data(const COMMAND_RPC_ACCESS_DATA::request& req, COMMAND_RPC_ACCESS_DATA::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
|
||||
bool on_rpc_access_account(const COMMAND_RPC_ACCESS_ACCOUNT::request& req, COMMAND_RPC_ACCESS_ACCOUNT::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx = NULL);
|
||||
//-----------------------
|
||||
|
||||
private:
|
||||
@@ -252,6 +269,8 @@ private:
|
||||
enum invoke_http_mode { JON, BIN, JON_RPC };
|
||||
template <typename COMMAND_TYPE>
|
||||
bool use_bootstrap_daemon_if_necessary(const invoke_http_mode &mode, const std::string &command_name, const typename COMMAND_TYPE::request& req, typename COMMAND_TYPE::response& res, bool &r);
|
||||
bool get_block_template(const account_public_address &address, const crypto::hash *prev_block, const cryptonote::blobdata &extra_nonce, size_t &reserved_offset, cryptonote::difficulty_type &difficulty, uint64_t &height, uint64_t &expected_reward, block &b, uint64_t &seed_height, crypto::hash &seed_hash, crypto::hash &next_seed_hash, epee::json_rpc::error &error_resp);
|
||||
bool check_payment(const std::string &client, uint64_t payment, const std::string &rpc, bool same_ts, std::string &message, uint64_t &credits, std::string &top_hash);
|
||||
|
||||
core& m_core;
|
||||
nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> >& m_p2p;
|
||||
@@ -260,10 +279,10 @@ private:
|
||||
bool m_should_use_bootstrap_daemon;
|
||||
std::chrono::system_clock::time_point m_bootstrap_height_check_time;
|
||||
bool m_was_bootstrap_ever_used;
|
||||
network_type m_nettype;
|
||||
bool m_restricted;
|
||||
epee::critical_section m_host_fails_score_lock;
|
||||
std::map<std::string, uint64_t> m_host_fails_score;
|
||||
std::unique_ptr<rpc_payment> m_rpc_payment;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -43,5 +43,34 @@
|
||||
#define CORE_RPC_ERROR_CODE_UNSUPPORTED_RPC -11
|
||||
#define CORE_RPC_ERROR_CODE_MINING_TO_SUBADDRESS -12
|
||||
#define CORE_RPC_ERROR_CODE_REGTEST_REQUIRED -13
|
||||
#define CORE_RPC_ERROR_CODE_PAYMENT_REQUIRED -14
|
||||
#define CORE_RPC_ERROR_CODE_INVALID_CLIENT -15
|
||||
#define CORE_RPC_ERROR_CODE_PAYMENT_TOO_LOW -16
|
||||
#define CORE_RPC_ERROR_CODE_DUPLICATE_PAYMENT -17
|
||||
#define CORE_RPC_ERROR_CODE_STALE_PAYMENT -18
|
||||
|
||||
static inline const char *get_rpc_server_error_message(int64_t code)
|
||||
{
|
||||
switch (code)
|
||||
{
|
||||
case CORE_RPC_ERROR_CODE_WRONG_PARAM: return "Invalid parameter";
|
||||
case CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT: return "Height is too large";
|
||||
case CORE_RPC_ERROR_CODE_TOO_BIG_RESERVE_SIZE: return "Reserve size is too large";
|
||||
case CORE_RPC_ERROR_CODE_WRONG_WALLET_ADDRESS: return "Wrong wallet address";
|
||||
case CORE_RPC_ERROR_CODE_INTERNAL_ERROR: return "Internal error";
|
||||
case CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB: return "Wrong block blob";
|
||||
case CORE_RPC_ERROR_CODE_BLOCK_NOT_ACCEPTED: return "Block not accepted";
|
||||
case CORE_RPC_ERROR_CODE_CORE_BUSY: return "Core is busy";
|
||||
case CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB_SIZE: return "Wrong block blob size";
|
||||
case CORE_RPC_ERROR_CODE_UNSUPPORTED_RPC: return "Unsupported RPC";
|
||||
case CORE_RPC_ERROR_CODE_MINING_TO_SUBADDRESS: return "Mining to subaddress is not supported";
|
||||
case CORE_RPC_ERROR_CODE_REGTEST_REQUIRED: return "Regtest mode required";
|
||||
case CORE_RPC_ERROR_CODE_PAYMENT_REQUIRED: return "Payment required";
|
||||
case CORE_RPC_ERROR_CODE_INVALID_CLIENT: return "Invalid client";
|
||||
case CORE_RPC_ERROR_CODE_PAYMENT_TOO_LOW: return "Payment too low";
|
||||
case CORE_RPC_ERROR_CODE_DUPLICATE_PAYMENT: return "Duplicate payment";
|
||||
case CORE_RPC_ERROR_CODE_STALE_PAYMENT: return "Stale payment";
|
||||
default: MERROR("Unknown error: " << code); return "Unknown error";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -80,6 +80,7 @@ namespace rpc
|
||||
uint32_t ip;
|
||||
uint16_t port;
|
||||
uint16_t rpc_port;
|
||||
uint32_t rpc_credits_per_hash;
|
||||
uint64_t last_seen;
|
||||
uint32_t pruning_seed;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,402 @@
|
||||
// Copyright (c) 2018-2019, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <boost/archive/portable_binary_iarchive.hpp>
|
||||
#include <boost/archive/portable_binary_oarchive.hpp>
|
||||
#include "cryptonote_config.h"
|
||||
#include "include_base_utils.h"
|
||||
#include "string_tools.h"
|
||||
#include "file_io_utils.h"
|
||||
#include "int-util.h"
|
||||
#include "common/util.h"
|
||||
#include "serialization/crypto.h"
|
||||
#include "common/unordered_containers_boost_serialization.h"
|
||||
#include "cryptonote_basic/cryptonote_boost_serialization.h"
|
||||
#include "cryptonote_basic/cryptonote_format_utils.h"
|
||||
#include "cryptonote_basic/difficulty.h"
|
||||
#include "core_rpc_server_error_codes.h"
|
||||
#include "rpc_payment.h"
|
||||
|
||||
#undef MONERO_DEFAULT_LOG_CATEGORY
|
||||
#define MONERO_DEFAULT_LOG_CATEGORY "daemon.rpc.payment"
|
||||
|
||||
#define STALE_THRESHOLD 15 /* seconds */
|
||||
|
||||
#define PENALTY_FOR_STALE 0
|
||||
#define PENALTY_FOR_BAD_HASH 20
|
||||
#define PENALTY_FOR_DUPLICATE 20
|
||||
|
||||
#define DEFAULT_FLUSH_AGE (3600 * 24 * 180) // half a year
|
||||
#define DEFAULT_ZERO_FLUSH_AGE (60 * 2) // 2 minutes
|
||||
|
||||
#define RPC_PAYMENT_NONCE_TAIL 0x58
|
||||
|
||||
namespace cryptonote
|
||||
{
|
||||
rpc_payment::client_info::client_info():
|
||||
cookie(0),
|
||||
top(crypto::null_hash),
|
||||
previous_top(crypto::null_hash),
|
||||
credits(0),
|
||||
update_time(time(NULL)),
|
||||
last_request_timestamp(0),
|
||||
block_template_update_time(0),
|
||||
credits_total(0),
|
||||
credits_used(0),
|
||||
nonces_good(0),
|
||||
nonces_stale(0),
|
||||
nonces_bad(0),
|
||||
nonces_dupe(0)
|
||||
{
|
||||
}
|
||||
|
||||
rpc_payment::rpc_payment(const cryptonote::account_public_address &address, uint64_t diff, uint64_t credits_per_hash_found):
|
||||
m_address(address),
|
||||
m_diff(diff),
|
||||
m_credits_per_hash_found(credits_per_hash_found),
|
||||
m_credits_total(0),
|
||||
m_credits_used(0),
|
||||
m_nonces_good(0),
|
||||
m_nonces_stale(0),
|
||||
m_nonces_bad(0),
|
||||
m_nonces_dupe(0)
|
||||
{
|
||||
}
|
||||
|
||||
uint64_t rpc_payment::balance(const crypto::public_key &client, int64_t delta)
|
||||
{
|
||||
client_info &info = m_client_info[client]; // creates if not found
|
||||
uint64_t credits = info.credits;
|
||||
if (delta > 0 && credits > std::numeric_limits<uint64_t>::max() - delta)
|
||||
credits = std::numeric_limits<uint64_t>::max();
|
||||
else if (delta < 0 && credits < (uint64_t)-delta)
|
||||
credits = 0;
|
||||
else
|
||||
credits += delta;
|
||||
if (delta)
|
||||
MINFO("Client " << client << ": balance change from " << info.credits << " to " << credits);
|
||||
return info.credits = credits;
|
||||
}
|
||||
|
||||
bool rpc_payment::pay(const crypto::public_key &client, uint64_t ts, uint64_t payment, const std::string &rpc, bool same_ts, uint64_t &credits)
|
||||
{
|
||||
client_info &info = m_client_info[client]; // creates if not found
|
||||
if (ts < info.last_request_timestamp || (ts == info.last_request_timestamp && !same_ts))
|
||||
{
|
||||
MDEBUG("Invalid ts: " << ts << " <= " << info.last_request_timestamp);
|
||||
return false;
|
||||
}
|
||||
info.last_request_timestamp = ts;
|
||||
if (info.credits < payment)
|
||||
{
|
||||
MDEBUG("Not enough credits: " << info.credits << " < " << payment);
|
||||
credits = info.credits;
|
||||
return false;
|
||||
}
|
||||
info.credits -= payment;
|
||||
add64clamp(&info.credits_used, payment);
|
||||
add64clamp(&m_credits_used, payment);
|
||||
MDEBUG("client " << client << " paying " << payment << " for " << rpc << ", " << info.credits << " left");
|
||||
credits = info.credits;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rpc_payment::get_info(const crypto::public_key &client, const std::function<bool(const cryptonote::blobdata&, cryptonote::block&, uint64_t &seed_height, crypto::hash &seed_hash)> &get_block_template, cryptonote::blobdata &hashing_blob, uint64_t &seed_height, crypto::hash &seed_hash, const crypto::hash &top, uint64_t &diff, uint64_t &credits_per_hash_found, uint64_t &credits, uint32_t &cookie)
|
||||
{
|
||||
client_info &info = m_client_info[client]; // creates if not found
|
||||
const uint64_t now = time(NULL);
|
||||
bool need_template = top != info.top || now >= info.block_template_update_time + STALE_THRESHOLD;
|
||||
if (need_template)
|
||||
{
|
||||
cryptonote::block new_block;
|
||||
uint64_t new_seed_height;
|
||||
crypto::hash new_seed_hash;
|
||||
cryptonote::blobdata extra_nonce("\x42\x42\x42\x42", 4);
|
||||
if (!get_block_template(extra_nonce, new_block, new_seed_height, new_seed_hash))
|
||||
return false;
|
||||
if(!remove_field_from_tx_extra(new_block.miner_tx.extra, typeid(cryptonote::tx_extra_nonce)))
|
||||
return false;
|
||||
char data[33];
|
||||
memcpy(data, &client, 32);
|
||||
data[32] = RPC_PAYMENT_NONCE_TAIL;
|
||||
crypto::hash hash;
|
||||
cn_fast_hash(data, sizeof(data), hash);
|
||||
extra_nonce = cryptonote::blobdata((const char*)&hash, 4);
|
||||
if(!add_extra_nonce_to_tx_extra(new_block.miner_tx.extra, extra_nonce))
|
||||
return false;
|
||||
info.previous_block = std::move(info.block);
|
||||
info.block = std::move(new_block);
|
||||
hashing_blob = get_block_hashing_blob(info.block);
|
||||
info.previous_hashing_blob = info.hashing_blob;
|
||||
info.hashing_blob = hashing_blob;
|
||||
info.previous_top = info.top;
|
||||
info.previous_seed_height = info.seed_height;
|
||||
info.seed_height = new_seed_height;
|
||||
info.previous_seed_hash = info.seed_hash;
|
||||
info.seed_hash = new_seed_hash;
|
||||
std::swap(info.previous_payments, info.payments);
|
||||
info.payments.clear();
|
||||
++info.cookie;
|
||||
info.block_template_update_time = now;
|
||||
}
|
||||
info.top = top;
|
||||
info.update_time = now;
|
||||
hashing_blob = info.hashing_blob;
|
||||
diff = m_diff;
|
||||
credits_per_hash_found = m_credits_per_hash_found;
|
||||
credits = info.credits;
|
||||
seed_height = info.seed_height;
|
||||
seed_hash = info.seed_hash;
|
||||
cookie = info.cookie;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rpc_payment::submit_nonce(const crypto::public_key &client, uint32_t nonce, const crypto::hash &top, int64_t &error_code, std::string &error_message, uint64_t &credits, crypto::hash &hash, cryptonote::block &block, uint32_t cookie, bool &stale)
|
||||
{
|
||||
client_info &info = m_client_info[client]; // creates if not found
|
||||
if (cookie != info.cookie && cookie != info.cookie - 1)
|
||||
{
|
||||
MWARNING("Very stale nonce");
|
||||
++m_nonces_stale;
|
||||
++info.nonces_stale;
|
||||
sub64clamp(&info.credits, PENALTY_FOR_STALE * m_credits_per_hash_found);
|
||||
error_code = CORE_RPC_ERROR_CODE_STALE_PAYMENT;
|
||||
error_message = "Very stale payment";
|
||||
return false;
|
||||
}
|
||||
const bool is_current = cookie == info.cookie;
|
||||
MINFO("client " << client << " sends nonce: " << nonce << ", " << (is_current ? "current" : "stale"));
|
||||
std::unordered_set<uint64_t> &payments = is_current ? info.payments : info.previous_payments;
|
||||
if (!payments.insert(nonce).second)
|
||||
{
|
||||
MWARNING("Duplicate nonce " << nonce << " from " << (is_current ? "current" : "previous"));
|
||||
++m_nonces_dupe;
|
||||
++info.nonces_dupe;
|
||||
sub64clamp(&info.credits, PENALTY_FOR_DUPLICATE * m_credits_per_hash_found);
|
||||
error_code = CORE_RPC_ERROR_CODE_DUPLICATE_PAYMENT;
|
||||
error_message = "Duplicate payment";
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint64_t now = time(NULL);
|
||||
if (!is_current)
|
||||
{
|
||||
if (now > info.update_time + STALE_THRESHOLD)
|
||||
{
|
||||
MWARNING("Nonce is stale (top " << top << ", should be " << info.top << " or within " << STALE_THRESHOLD << " seconds");
|
||||
++m_nonces_stale;
|
||||
++info.nonces_stale;
|
||||
sub64clamp(&info.credits, PENALTY_FOR_STALE * m_credits_per_hash_found);
|
||||
error_code = CORE_RPC_ERROR_CODE_STALE_PAYMENT;
|
||||
error_message = "stale payment";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
cryptonote::blobdata hashing_blob = is_current ? info.hashing_blob : info.previous_hashing_blob;
|
||||
if (hashing_blob.size() < 43)
|
||||
{
|
||||
// not initialized ?
|
||||
error_code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB;
|
||||
error_message = "not initialized";
|
||||
return false;
|
||||
}
|
||||
|
||||
block = is_current ? info.block : info.previous_block;
|
||||
*(uint32_t*)(hashing_blob.data() + 39) = SWAP32LE(nonce);
|
||||
if (block.major_version >= RX_BLOCK_VERSION)
|
||||
{
|
||||
const uint64_t seed_height = is_current ? info.seed_height : info.previous_seed_height;
|
||||
const crypto::hash &seed_hash = is_current ? info.seed_hash : info.previous_seed_hash;
|
||||
const uint64_t height = cryptonote::get_block_height(block);
|
||||
crypto::rx_slow_hash(height, seed_height, seed_hash.data, hashing_blob.data(), hashing_blob.size(), hash.data, 0, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
const int cn_variant = hashing_blob[0] >= 7 ? hashing_blob[0] - 6 : 0;
|
||||
crypto::cn_slow_hash(hashing_blob.data(), hashing_blob.size(), hash, cn_variant, cryptonote::get_block_height(block));
|
||||
}
|
||||
if (!check_hash(hash, m_diff))
|
||||
{
|
||||
MWARNING("Payment too low");
|
||||
++m_nonces_bad;
|
||||
++info.nonces_bad;
|
||||
error_code = CORE_RPC_ERROR_CODE_PAYMENT_TOO_LOW;
|
||||
error_message = "Hash does not meet difficulty (could be wrong PoW hash, or mining at lower difficulty than required, or attempt to defraud)";
|
||||
sub64clamp(&info.credits, PENALTY_FOR_BAD_HASH * m_credits_per_hash_found);
|
||||
return false;
|
||||
}
|
||||
|
||||
add64clamp(&info.credits, m_credits_per_hash_found);
|
||||
MINFO("client " << client << " credited for " << m_credits_per_hash_found << ", now " << info.credits << (is_current ? "" : " (close)"));
|
||||
|
||||
m_hashrate[now] += m_diff;
|
||||
add64clamp(&m_credits_total, m_credits_per_hash_found);
|
||||
add64clamp(&info.credits_total, m_credits_per_hash_found);
|
||||
++m_nonces_good;
|
||||
++info.nonces_good;
|
||||
|
||||
credits = info.credits;
|
||||
block = info.block;
|
||||
block.nonce = nonce;
|
||||
stale = !is_current;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rpc_payment::foreach(const std::function<bool(const crypto::public_key &client, const client_info &info)> &f) const
|
||||
{
|
||||
for (std::unordered_map<crypto::public_key, client_info>::const_iterator i = m_client_info.begin(); i != m_client_info.end(); ++i)
|
||||
{
|
||||
if (!f(i->first, i->second))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rpc_payment::load(std::string directory)
|
||||
{
|
||||
TRY_ENTRY();
|
||||
m_directory = std::move(directory);
|
||||
std::string state_file_path = directory + "/" + RPC_PAYMENTS_DATA_FILENAME;
|
||||
MINFO("loading rpc payments data from " << state_file_path);
|
||||
std::ifstream data;
|
||||
data.open(state_file_path, std::ios_base::binary | std::ios_base::in);
|
||||
if (!data.fail())
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::archive::portable_binary_iarchive a(data);
|
||||
a >> *this;
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
MERROR("Failed to load RPC payments file: " << e.what());
|
||||
m_client_info.clear();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_client_info.clear();
|
||||
}
|
||||
|
||||
CATCH_ENTRY_L0("rpc_payment::load", false);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rpc_payment::store(const std::string &directory_) const
|
||||
{
|
||||
TRY_ENTRY();
|
||||
const std::string &directory = directory_.empty() ? m_directory : directory_;
|
||||
MDEBUG("storing rpc payments data to " << directory);
|
||||
if (!tools::create_directories_if_necessary(directory))
|
||||
{
|
||||
MWARNING("Failed to create data directory: " << directory);
|
||||
return false;
|
||||
}
|
||||
const boost::filesystem::path state_file_path = (boost::filesystem::path(directory) / RPC_PAYMENTS_DATA_FILENAME);
|
||||
if (boost::filesystem::exists(state_file_path))
|
||||
{
|
||||
std::string state_file_path_old = state_file_path.string() + ".old";
|
||||
boost::system::error_code ec;
|
||||
boost::filesystem::remove(state_file_path_old, ec);
|
||||
std::error_code e = tools::replace_file(state_file_path.string(), state_file_path_old);
|
||||
if (e)
|
||||
MWARNING("Failed to rename " << state_file_path << " to " << state_file_path_old << ": " << e);
|
||||
}
|
||||
std::ofstream data;
|
||||
data.open(state_file_path.string(), std::ios_base::binary | std::ios_base::out | std::ios::trunc);
|
||||
if (data.fail())
|
||||
{
|
||||
MWARNING("Failed to save RPC payments to file " << state_file_path);
|
||||
return false;
|
||||
};
|
||||
boost::archive::portable_binary_oarchive a(data);
|
||||
a << *this;
|
||||
return true;
|
||||
CATCH_ENTRY_L0("rpc_payment::store", false);
|
||||
}
|
||||
|
||||
unsigned int rpc_payment::flush_by_age(time_t seconds)
|
||||
{
|
||||
unsigned int count = 0;
|
||||
const time_t now = time(NULL);
|
||||
time_t seconds0 = seconds;
|
||||
if (seconds == 0)
|
||||
{
|
||||
seconds = DEFAULT_FLUSH_AGE;
|
||||
seconds0 = DEFAULT_ZERO_FLUSH_AGE;
|
||||
}
|
||||
const time_t threshold = seconds > now ? 0 : now - seconds;
|
||||
const time_t threshold0 = seconds0 > now ? 0 : now - seconds0;
|
||||
for (std::unordered_map<crypto::public_key, client_info>::iterator i = m_client_info.begin(); i != m_client_info.end(); )
|
||||
{
|
||||
std::unordered_map<crypto::public_key, client_info>::iterator j = i++;
|
||||
const time_t t = std::max(j->second.last_request_timestamp, j->second.update_time);
|
||||
const bool erase = t < ((j->second.credits == 0) ? threshold0 : threshold);
|
||||
if (erase)
|
||||
{
|
||||
MINFO("Erasing " << j->first << " with " << j->second.credits << " credits, inactive for " << (now-t)/86400 << " days");
|
||||
m_client_info.erase(j);
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
uint64_t rpc_payment::get_hashes(unsigned int seconds) const
|
||||
{
|
||||
const uint64_t now = time(NULL);
|
||||
uint64_t hashes = 0;
|
||||
for (std::map<uint64_t, uint64_t>::const_reverse_iterator i = m_hashrate.crbegin(); i != m_hashrate.crend(); ++i)
|
||||
{
|
||||
if (now > i->first + seconds)
|
||||
break;
|
||||
hashes += i->second;
|
||||
}
|
||||
return hashes;
|
||||
}
|
||||
|
||||
void rpc_payment::prune_hashrate(unsigned int seconds)
|
||||
{
|
||||
const uint64_t now = time(NULL);
|
||||
std::map<uint64_t, uint64_t>::iterator i;
|
||||
for (i = m_hashrate.begin(); i != m_hashrate.end(); ++i)
|
||||
{
|
||||
if (now <= i->first + seconds)
|
||||
break;
|
||||
}
|
||||
m_hashrate.erase(m_hashrate.begin(), i);
|
||||
}
|
||||
|
||||
bool rpc_payment::on_idle()
|
||||
{
|
||||
flush_by_age();
|
||||
prune_hashrate(3600);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
// Copyright (c) 2018-2019, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
#include <boost/serialization/version.hpp>
|
||||
#include "cryptonote_basic/blobdatatype.h"
|
||||
#include "cryptonote_basic/cryptonote_basic.h"
|
||||
|
||||
namespace cryptonote
|
||||
{
|
||||
class rpc_payment
|
||||
{
|
||||
public:
|
||||
struct client_info
|
||||
{
|
||||
cryptonote::block block;
|
||||
cryptonote::block previous_block;
|
||||
cryptonote::blobdata hashing_blob;
|
||||
cryptonote::blobdata previous_hashing_blob;
|
||||
uint64_t previous_seed_height;
|
||||
uint64_t seed_height;
|
||||
crypto::hash previous_seed_hash;
|
||||
crypto::hash seed_hash;
|
||||
uint32_t cookie;
|
||||
crypto::hash top;
|
||||
crypto::hash previous_top;
|
||||
uint64_t credits;
|
||||
std::unordered_set<uint64_t> payments;
|
||||
std::unordered_set<uint64_t> previous_payments;
|
||||
uint64_t update_time;
|
||||
uint64_t last_request_timestamp;
|
||||
uint64_t block_template_update_time;
|
||||
uint64_t credits_total;
|
||||
uint64_t credits_used;
|
||||
uint64_t nonces_good;
|
||||
uint64_t nonces_stale;
|
||||
uint64_t nonces_bad;
|
||||
uint64_t nonces_dupe;
|
||||
|
||||
client_info();
|
||||
|
||||
template <class t_archive>
|
||||
inline void serialize(t_archive &a, const unsigned int ver)
|
||||
{
|
||||
a & block;
|
||||
a & previous_block;
|
||||
a & hashing_blob;
|
||||
a & previous_hashing_blob;
|
||||
a & seed_height;
|
||||
a & previous_seed_height;
|
||||
a & seed_hash;
|
||||
a & previous_seed_hash;
|
||||
a & cookie;
|
||||
a & top;
|
||||
a & previous_top;
|
||||
a & credits;
|
||||
a & payments;
|
||||
a & previous_payments;
|
||||
a & update_time;
|
||||
a & last_request_timestamp;
|
||||
a & block_template_update_time;
|
||||
a & credits_total;
|
||||
a & credits_used;
|
||||
a & nonces_good;
|
||||
a & nonces_stale;
|
||||
a & nonces_bad;
|
||||
a & nonces_dupe;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
rpc_payment(const cryptonote::account_public_address &address, uint64_t diff, uint64_t credits_per_hash_found);
|
||||
uint64_t balance(const crypto::public_key &client, int64_t delta = 0);
|
||||
bool pay(const crypto::public_key &client, uint64_t ts, uint64_t payment, const std::string &rpc, bool same_ts, uint64_t &credits);
|
||||
bool get_info(const crypto::public_key &client, const std::function<bool(const cryptonote::blobdata&, cryptonote::block&, uint64_t &seed_height, crypto::hash &seed_hash)> &get_block_template, cryptonote::blobdata &hashing_blob, uint64_t &seed_height, crypto::hash &seed_hash, const crypto::hash &top, uint64_t &diff, uint64_t &credits_per_hash_found, uint64_t &credits, uint32_t &cookie);
|
||||
bool submit_nonce(const crypto::public_key &client, uint32_t nonce, const crypto::hash &top, int64_t &error_code, std::string &error_message, uint64_t &credits, crypto::hash &hash, cryptonote::block &block, uint32_t cookie, bool &stale);
|
||||
const cryptonote::account_public_address &get_payment_address() const { return m_address; }
|
||||
bool foreach(const std::function<bool(const crypto::public_key &client, const client_info &info)> &f) const;
|
||||
unsigned int flush_by_age(time_t seconds = 0);
|
||||
uint64_t get_hashes(unsigned int seconds) const;
|
||||
void prune_hashrate(unsigned int seconds);
|
||||
bool on_idle();
|
||||
|
||||
template <class t_archive>
|
||||
inline void serialize(t_archive &a, const unsigned int ver)
|
||||
{
|
||||
a & m_client_info;
|
||||
a & m_hashrate;
|
||||
a & m_credits_total;
|
||||
a & m_credits_used;
|
||||
a & m_nonces_good;
|
||||
a & m_nonces_stale;
|
||||
a & m_nonces_bad;
|
||||
a & m_nonces_dupe;
|
||||
}
|
||||
|
||||
bool load(std::string directory);
|
||||
bool store(const std::string &directory = std::string()) const;
|
||||
|
||||
private:
|
||||
cryptonote::account_public_address m_address;
|
||||
uint64_t m_diff;
|
||||
uint64_t m_credits_per_hash_found;
|
||||
std::unordered_map<crypto::public_key, client_info> m_client_info;
|
||||
std::string m_directory;
|
||||
std::map<uint64_t, uint64_t> m_hashrate;
|
||||
uint64_t m_credits_total;
|
||||
uint64_t m_credits_used;
|
||||
uint64_t m_nonces_good;
|
||||
uint64_t m_nonces_stale;
|
||||
uint64_t m_nonces_bad;
|
||||
uint64_t m_nonces_dupe;
|
||||
};
|
||||
}
|
||||
|
||||
BOOST_CLASS_VERSION(cryptonote::rpc_payment, 0);
|
||||
BOOST_CLASS_VERSION(cryptonote::rpc_payment::client_info, 0);
|
||||
@@ -0,0 +1,49 @@
|
||||
// Copyright (c) 2019, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#define COST_PER_BLOCK 0.05
|
||||
#define COST_PER_TX_RELAY 100
|
||||
#define COST_PER_OUT 1
|
||||
#define COST_PER_OUTPUT_INDEXES 1
|
||||
#define COST_PER_TX 0.5
|
||||
#define COST_PER_KEY_IMAGE 0.01
|
||||
#define COST_PER_POOL_HASH 0.01
|
||||
#define COST_PER_TX_POOL_STATS 0.2
|
||||
#define COST_PER_BLOCK_HEADER 0.1
|
||||
#define COST_PER_GET_INFO 1
|
||||
#define COST_PER_OUTPUT_HISTOGRAM 25000
|
||||
#define COST_PER_FULL_OUTPUT_HISTOGRAM 5000000
|
||||
#define COST_PER_OUTPUT_DISTRIBUTION_0 20
|
||||
#define COST_PER_OUTPUT_DISTRIBUTION 50000
|
||||
#define COST_PER_COINBASE_TX_SUM_BLOCK 2
|
||||
#define COST_PER_BLOCK_HASH 0.002
|
||||
#define COST_PER_FEE_ESTIMATE 1
|
||||
#define COST_PER_SYNC_INFO 2
|
||||
#define COST_PER_HARD_FORK_INFO 1
|
||||
@@ -0,0 +1,107 @@
|
||||
// Copyright (c) 2018-2019, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <chrono>
|
||||
#include "include_base_utils.h"
|
||||
#include "string_tools.h"
|
||||
#include "rpc_payment_signature.h"
|
||||
|
||||
#undef MONERO_DEFAULT_LOG_CATEGORY
|
||||
#define MONERO_DEFAULT_LOG_CATEGORY "daemon.rpc.payment"
|
||||
|
||||
#define TIMESTAMP_LEEWAY (60 * 1000000) /* 60 seconds, in microseconds */
|
||||
|
||||
namespace cryptonote
|
||||
{
|
||||
std::string make_rpc_payment_signature(const crypto::secret_key &skey)
|
||||
{
|
||||
std::string s;
|
||||
crypto::public_key pkey;
|
||||
crypto::secret_key_to_public_key(skey, pkey);
|
||||
crypto::signature sig;
|
||||
const uint64_t now = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
char ts[17];
|
||||
int ret = snprintf(ts, sizeof(ts), "%16.16" PRIx64, now);
|
||||
CHECK_AND_ASSERT_MES(ret == 16, "", "snprintf failed");
|
||||
ts[16] = 0;
|
||||
CHECK_AND_ASSERT_MES(strlen(ts) == 16, "", "Invalid time conversion");
|
||||
crypto::hash hash;
|
||||
crypto::cn_fast_hash(ts, 16, hash);
|
||||
crypto::generate_signature(hash, pkey, skey, sig);
|
||||
s = epee::string_tools::pod_to_hex(pkey) + ts + epee::string_tools::pod_to_hex(sig);
|
||||
return s;
|
||||
}
|
||||
|
||||
bool verify_rpc_payment_signature(const std::string &message, crypto::public_key &pkey, uint64_t &ts)
|
||||
{
|
||||
if (message.size() != 2 * sizeof(crypto::public_key) + 16 + 2 * sizeof(crypto::signature))
|
||||
{
|
||||
MDEBUG("Bad message size: " << message.size());
|
||||
return false;
|
||||
}
|
||||
const std::string pkey_string = message.substr(0, 2 * sizeof(crypto::public_key));
|
||||
const std::string ts_string = message.substr(2 * sizeof(crypto::public_key), 16);
|
||||
const std::string signature_string = message.substr(2 * sizeof(crypto::public_key) + 16);
|
||||
if (!epee::string_tools::hex_to_pod(pkey_string, pkey))
|
||||
{
|
||||
MDEBUG("Bad client id");
|
||||
return false;
|
||||
}
|
||||
crypto::signature signature;
|
||||
if (!epee::string_tools::hex_to_pod(signature_string, signature))
|
||||
{
|
||||
MDEBUG("Bad signature");
|
||||
return false;
|
||||
}
|
||||
crypto::hash hash;
|
||||
crypto::cn_fast_hash(ts_string.data(), 16, hash);
|
||||
if (!crypto::check_signature(hash, pkey, signature))
|
||||
{
|
||||
MDEBUG("signature does not verify");
|
||||
return false;
|
||||
}
|
||||
char *endptr = NULL;
|
||||
errno = 0;
|
||||
unsigned long long ull = strtoull(ts_string.c_str(), &endptr, 16);
|
||||
if (ull == ULLONG_MAX && errno == ERANGE)
|
||||
{
|
||||
MDEBUG("bad timestamp");
|
||||
return false;
|
||||
}
|
||||
ts = ull;
|
||||
const uint64_t now = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
if (ts > now + TIMESTAMP_LEEWAY)
|
||||
{
|
||||
MDEBUG("Timestamp is in the future");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2018-2019, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include "crypto/crypto.h"
|
||||
|
||||
namespace cryptonote
|
||||
{
|
||||
std::string make_rpc_payment_signature(const crypto::secret_key &skey);
|
||||
bool verify_rpc_payment_signature(const std::string &message, crypto::public_key &pkey, uint64_t &ts);
|
||||
}
|
||||
Reference in New Issue
Block a user