6013 lines
242 KiB
C++
6013 lines
242 KiB
C++
// Copyright (c) 2014-2020, 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.
|
|
//
|
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
|
|
|
#include <boost/preprocessor/stringize.hpp>
|
|
#include <boost/uuid/nil_generator.hpp>
|
|
#include <boost/filesystem.hpp>
|
|
#include "include_base_utils.h"
|
|
#include "string_tools.h"
|
|
using namespace epee;
|
|
|
|
#include "core_rpc_server.h"
|
|
#include "common/command_line.h"
|
|
#include "common/updates.h"
|
|
#include "common/download.h"
|
|
#include "common/util.h"
|
|
#include "common/ipfs.h"
|
|
#include "common/perf_timer.h"
|
|
#include "int-util.h"
|
|
#include "cryptonote_basic/cryptonote_format_utils.h"
|
|
#include "cryptonote_basic/account.h"
|
|
#include "cryptonote_basic/cryptonote_basic_impl.h"
|
|
#include "cryptonote_basic/merge_mining.h"
|
|
#include "cryptonote_core/tx_sanity_check.h"
|
|
#include "misc_language.h"
|
|
#include "net/parse.h"
|
|
#include "storages/http_abstract_invoke.h"
|
|
#include "crypto/hash.h"
|
|
#include "rpc/rpc_args.h"
|
|
#include "rpc/rpc_handler.h"
|
|
#include "rpc/rpc_payment_costs.h"
|
|
#include "rpc/rpc_payment_signature.h"
|
|
#include "core_rpc_server_error_codes.h"
|
|
#include "p2p/net_node.h"
|
|
#include "blockchain_db/locked_txn.h"
|
|
#include "serialization/variant.h"
|
|
#include "cc/cc_config.h"
|
|
#include "cc/cc_potential.h"
|
|
#include "cc/cc_influence.h"
|
|
#include "cc/cc_game_events.h"
|
|
#include "cc/cc_discoveries.h"
|
|
#include "cc/cc_badge.h"
|
|
#include "cc/cc_invitation.h"
|
|
#include "cc/cc_calendar.h"
|
|
#include "cc/cc_food.h"
|
|
#include "cc/cc_temperature.h"
|
|
#include "cc/cc_command_handler_game_update.h"
|
|
#include "cc/cc_script.h"
|
|
#include "cc/cc_blockchain_state_proxy.h"
|
|
#include "cc/cc_mortgage.h"
|
|
#include "cc/cc_collectible_coin.h"
|
|
#include "version.h"
|
|
|
|
#undef MONERO_DEFAULT_LOG_CATEGORY
|
|
#define MONERO_DEFAULT_LOG_CATEGORY "daemon.rpc"
|
|
|
|
#define MAX_RESTRICTED_FAKE_OUTS_COUNT 40
|
|
#define MAX_RESTRICTED_GLOBAL_FAKE_OUTS_COUNT 5000
|
|
|
|
#define OUTPUT_HISTOGRAM_RECENT_CUTOFF_RESTRICTION (3 * 86400) // 3 days max, the wallet requests 1.8 days
|
|
|
|
#define DEFAULT_PAYMENT_DIFFICULTY 1000
|
|
#define DEFAULT_PAYMENT_CREDITS_PER_HASH 100
|
|
|
|
#define RESTRICTED_BLOCK_HEADER_RANGE 1000
|
|
#define RESTRICTED_TRANSACTIONS_COUNT 100
|
|
#define RESTRICTED_SPENT_KEY_IMAGES_COUNT 5000
|
|
#define RESTRICTED_BLOCK_COUNT 1000
|
|
|
|
#define RPC_TRACKER(rpc) \
|
|
PERF_TIMER(rpc); \
|
|
RPCTracker tracker(#rpc, PERF_TIMER_NAME(rpc))
|
|
|
|
class RPCLockedTXN
|
|
{
|
|
public:
|
|
RPCLockedTXN(cryptonote::core &core):
|
|
core(core)
|
|
{
|
|
core.lock_txpool();
|
|
core.get_blockchain_storage().lock();
|
|
child.reset(new cryptonote::LockedTXN(core.get_blockchain_storage().get_db()));
|
|
}
|
|
virtual ~RPCLockedTXN()
|
|
{
|
|
child.reset(nullptr);
|
|
core.get_blockchain_storage().unlock();
|
|
core.unlock_txpool();
|
|
}
|
|
|
|
void commit()
|
|
{
|
|
child->commit();
|
|
}
|
|
|
|
private:
|
|
cryptonote::core &core;
|
|
std::unique_ptr<cryptonote::LockedTXN> child;
|
|
};
|
|
|
|
namespace
|
|
{
|
|
class RPCTracker
|
|
{
|
|
public:
|
|
struct entry_t
|
|
{
|
|
uint64_t count;
|
|
uint64_t time;
|
|
uint64_t credits;
|
|
};
|
|
|
|
RPCTracker(const char *rpc, tools::LoggingPerformanceTimer &timer): rpc(rpc), timer(timer) {
|
|
}
|
|
~RPCTracker() {
|
|
try
|
|
{
|
|
boost::unique_lock<boost::mutex> lock(mutex);
|
|
auto &e = tracker[rpc];
|
|
++e.count;
|
|
e.time += timer.value();
|
|
}
|
|
catch (...) { /* ignore */ }
|
|
}
|
|
void pay(uint64_t amount) {
|
|
boost::unique_lock<boost::mutex> lock(mutex);
|
|
auto &e = tracker[rpc];
|
|
e.credits += amount;
|
|
}
|
|
const std::string &rpc_name() const { return rpc; }
|
|
static void clear() { boost::unique_lock<boost::mutex> lock(mutex); tracker.clear(); }
|
|
static std::unordered_map<std::string, entry_t> data() { boost::unique_lock<boost::mutex> lock(mutex); return tracker; }
|
|
private:
|
|
std::string rpc;
|
|
tools::LoggingPerformanceTimer &timer;
|
|
static boost::mutex mutex;
|
|
static std::unordered_map<std::string, entry_t> tracker;
|
|
};
|
|
boost::mutex RPCTracker::mutex;
|
|
std::unordered_map<std::string, RPCTracker::entry_t> RPCTracker::tracker;
|
|
|
|
void add_reason(std::string &reasons, const char *reason)
|
|
{
|
|
if (!reasons.empty())
|
|
reasons += ", ";
|
|
reasons += reason;
|
|
}
|
|
|
|
uint64_t round_up(uint64_t value, uint64_t quantum)
|
|
{
|
|
return (value + quantum - 1) / quantum * quantum;
|
|
}
|
|
|
|
void store_128(boost::multiprecision::uint128_t value, uint64_t &slow64, std::string &swide, uint64_t &stop64)
|
|
{
|
|
slow64 = (value & 0xffffffffffffffff).convert_to<uint64_t>();
|
|
swide = cryptonote::hex(value);
|
|
stop64 = ((value >> 64) & 0xffffffffffffffff).convert_to<uint64_t>();
|
|
}
|
|
|
|
void store_difficulty(cryptonote::difficulty_type difficulty, uint64_t &sdiff, std::string &swdiff, uint64_t &stop64)
|
|
{
|
|
store_128(difficulty, sdiff, swdiff, stop64);
|
|
}
|
|
|
|
uint32_t get_city_specialization_bitfield(const std::map<uint32_t, uint64_t> &specializations)
|
|
{
|
|
uint32_t bits = 0;
|
|
for (const auto &e: specializations)
|
|
bits |= 1 << e.first;
|
|
return bits;
|
|
}
|
|
}
|
|
|
|
namespace cryptonote
|
|
{
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
void core_rpc_server::init_options(boost::program_options::options_description& desc)
|
|
{
|
|
command_line::add_arg(desc, arg_rpc_bind_port);
|
|
command_line::add_arg(desc, arg_rpc_restricted_bind_port);
|
|
command_line::add_arg(desc, arg_restricted_rpc);
|
|
command_line::add_arg(desc, arg_bootstrap_daemon_address);
|
|
command_line::add_arg(desc, arg_bootstrap_daemon_login);
|
|
command_line::add_arg(desc, arg_bootstrap_daemon_proxy);
|
|
cryptonote::rpc_args::init_options(desc, true);
|
|
command_line::add_arg(desc, arg_rpc_payment_address);
|
|
command_line::add_arg(desc, arg_rpc_payment_difficulty);
|
|
command_line::add_arg(desc, arg_rpc_payment_credits);
|
|
command_line::add_arg(desc, arg_rpc_payment_allow_free_loopback);
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
core_rpc_server::core_rpc_server(
|
|
core& cr
|
|
, nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> >& p2p
|
|
)
|
|
: m_core(cr)
|
|
, m_p2p(p2p)
|
|
, m_was_bootstrap_ever_used(false)
|
|
, disable_rpc_ban(false)
|
|
, m_rpc_payment_allow_free_loopback(false)
|
|
{}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::set_bootstrap_daemon(
|
|
const std::string &address,
|
|
const std::string &username_password,
|
|
const std::string &proxy)
|
|
{
|
|
boost::optional<epee::net_utils::http::login> credentials;
|
|
const auto loc = username_password.find(':');
|
|
if (loc != std::string::npos)
|
|
{
|
|
credentials = epee::net_utils::http::login(username_password.substr(0, loc), username_password.substr(loc + 1));
|
|
}
|
|
return set_bootstrap_daemon(address, credentials, proxy);
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
std::map<std::string, bool> core_rpc_server::get_public_nodes(uint32_t credits_per_hash_threshold/* = 0*/)
|
|
{
|
|
COMMAND_RPC_GET_PUBLIC_NODES::request request;
|
|
COMMAND_RPC_GET_PUBLIC_NODES::response response;
|
|
|
|
request.gray = true;
|
|
request.white = true;
|
|
request.include_blocked = false;
|
|
if (!on_get_public_nodes(request, response) || response.status != CORE_RPC_STATUS_OK)
|
|
{
|
|
return {};
|
|
}
|
|
|
|
std::map<std::string, bool> result;
|
|
|
|
const auto append = [&result, &credits_per_hash_threshold](const std::vector<public_node> &nodes, bool white) {
|
|
for (const auto &node : nodes)
|
|
{
|
|
const bool rpc_payment_enabled = credits_per_hash_threshold > 0;
|
|
const bool node_rpc_payment_enabled = node.rpc_credits_per_hash > 0;
|
|
if (!node_rpc_payment_enabled ||
|
|
(rpc_payment_enabled && node.rpc_credits_per_hash >= credits_per_hash_threshold))
|
|
{
|
|
result.insert(std::make_pair(node.host + ":" + std::to_string(node.rpc_port), white));
|
|
}
|
|
}
|
|
};
|
|
|
|
append(response.white, true);
|
|
append(response.gray, false);
|
|
|
|
return result;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::set_bootstrap_daemon(
|
|
const std::string &address,
|
|
const boost::optional<epee::net_utils::http::login> &credentials,
|
|
const std::string &proxy)
|
|
{
|
|
boost::unique_lock<boost::shared_mutex> lock(m_bootstrap_daemon_mutex);
|
|
|
|
constexpr const uint32_t credits_per_hash_threshold = 0;
|
|
constexpr const bool rpc_payment_enabled = credits_per_hash_threshold != 0;
|
|
|
|
if (address.empty())
|
|
{
|
|
m_bootstrap_daemon.reset(nullptr);
|
|
}
|
|
else if (address == "auto")
|
|
{
|
|
auto get_nodes = [this]() {
|
|
return get_public_nodes(credits_per_hash_threshold);
|
|
};
|
|
m_bootstrap_daemon.reset(new bootstrap_daemon(std::move(get_nodes), rpc_payment_enabled, proxy));
|
|
}
|
|
else
|
|
{
|
|
m_bootstrap_daemon.reset(new bootstrap_daemon(address, credentials, rpc_payment_enabled, proxy));
|
|
}
|
|
|
|
m_should_use_bootstrap_daemon = m_bootstrap_daemon.get() != nullptr;
|
|
|
|
return true;
|
|
}
|
|
core_rpc_server::~core_rpc_server()
|
|
{
|
|
if (m_rpc_payment)
|
|
m_rpc_payment->store();
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::init(
|
|
const boost::program_options::variables_map& vm
|
|
, const bool restricted
|
|
, const std::string& port
|
|
, bool allow_rpc_payment
|
|
)
|
|
{
|
|
m_restricted = restricted;
|
|
m_net_server.set_threads_prefix("RPC");
|
|
m_net_server.set_connection_filter(&m_p2p);
|
|
|
|
auto rpc_config = cryptonote::rpc_args::process(vm, true);
|
|
if (!rpc_config)
|
|
return false;
|
|
|
|
std::string bind_ip_str = rpc_config->bind_ip;
|
|
std::string bind_ipv6_str = rpc_config->bind_ipv6_address;
|
|
if (restricted)
|
|
{
|
|
const auto restricted_rpc_port_arg = cryptonote::core_rpc_server::arg_rpc_restricted_bind_port;
|
|
const bool has_restricted_rpc_port_arg = !command_line::is_arg_defaulted(vm, restricted_rpc_port_arg);
|
|
if (has_restricted_rpc_port_arg && port == command_line::get_arg(vm, restricted_rpc_port_arg))
|
|
{
|
|
bind_ip_str = rpc_config->restricted_bind_ip;
|
|
bind_ipv6_str = rpc_config->restricted_bind_ipv6_address;
|
|
}
|
|
}
|
|
disable_rpc_ban = rpc_config->disable_rpc_ban;
|
|
const std::string data_dir{command_line::get_arg(vm, cryptonote::arg_data_dir)};
|
|
std::string address = command_line::get_arg(vm, arg_rpc_payment_address);
|
|
if (!address.empty() && allow_rpc_payment)
|
|
{
|
|
if (!m_restricted && nettype() != FAKECHAIN)
|
|
{
|
|
MFATAL("RPC payment enabled, but server is not restricted, anyone can adjust their balance to bypass payment");
|
|
return false;
|
|
}
|
|
cryptonote::address_parse_info info;
|
|
if (!get_account_address_from_str(info, nettype(), address))
|
|
{
|
|
MFATAL("Invalid payment address: " << address);
|
|
return false;
|
|
}
|
|
if (info.is_subaddress)
|
|
{
|
|
MFATAL("Payment address may not be a subaddress: " << address);
|
|
return false;
|
|
}
|
|
uint64_t diff = command_line::get_arg(vm, arg_rpc_payment_difficulty);
|
|
uint64_t credits = command_line::get_arg(vm, arg_rpc_payment_credits);
|
|
if (diff == 0 || credits == 0)
|
|
{
|
|
MFATAL("Payments difficulty and/or payments credits are 0, but a payment address was given");
|
|
return false;
|
|
}
|
|
m_rpc_payment_allow_free_loopback = command_line::get_arg(vm, arg_rpc_payment_allow_free_loopback);
|
|
m_rpc_payment.reset(new rpc_payment(info.address, diff, credits));
|
|
m_rpc_payment->load(data_dir);
|
|
m_p2p.set_rpc_credits_per_hash(RPC_CREDITS_PER_HASH_SCALE * (credits / (float)diff));
|
|
}
|
|
|
|
if (!m_rpc_payment)
|
|
{
|
|
uint32_t bind_ip;
|
|
bool ok = epee::string_tools::get_ip_int32_from_string(bind_ip, bind_ip_str);
|
|
if (ok & !epee::net_utils::is_ip_loopback(bind_ip))
|
|
MWARNING("The RPC server is accessible from the outside, but no RPC payment was setup. RPC access will be free for all.");
|
|
}
|
|
|
|
if (!set_bootstrap_daemon(
|
|
command_line::get_arg(vm, arg_bootstrap_daemon_address),
|
|
command_line::get_arg(vm, arg_bootstrap_daemon_login),
|
|
command_line::get_arg(vm, arg_bootstrap_daemon_proxy)))
|
|
{
|
|
MFATAL("Failed to parse bootstrap daemon address");
|
|
return false;
|
|
}
|
|
|
|
boost::optional<epee::net_utils::http::login> http_login{};
|
|
|
|
if (rpc_config->login)
|
|
http_login.emplace(std::move(rpc_config->login->username), std::move(rpc_config->login->password).password());
|
|
|
|
if (m_rpc_payment)
|
|
m_net_server.add_idle_handler([this](){ return m_rpc_payment->on_idle(); }, 60 * 1000);
|
|
|
|
bool store_ssl_key = !restricted && rpc_config->ssl_options && rpc_config->ssl_options.auth.certificate_path.empty();
|
|
const auto ssl_base_path = (boost::filesystem::path{data_dir} / "rpc_ssl").string();
|
|
if (store_ssl_key && boost::filesystem::exists(ssl_base_path + ".crt"))
|
|
{
|
|
// load key from previous run, password prompted by OpenSSL
|
|
store_ssl_key = false;
|
|
rpc_config->ssl_options.auth =
|
|
epee::net_utils::ssl_authentication_t{ssl_base_path + ".key", ssl_base_path + ".crt"};
|
|
}
|
|
|
|
auto rng = [](size_t len, uint8_t *ptr){ return crypto::rand(len, ptr); };
|
|
const bool inited = epee::http_server_impl_base<core_rpc_server, connection_context>::init(
|
|
rng, std::move(port), std::move(bind_ip_str),
|
|
std::move(bind_ipv6_str), std::move(rpc_config->use_ipv6), std::move(rpc_config->require_ipv4),
|
|
std::move(rpc_config->access_control_origins), std::move(http_login), std::move(rpc_config->ssl_options)
|
|
);
|
|
|
|
if (store_ssl_key && inited)
|
|
{
|
|
// new keys were generated, store for next run
|
|
const auto error = epee::net_utils::store_ssl_keys(m_net_server.get_ssl_context(), ssl_base_path);
|
|
if (error)
|
|
MFATAL("Failed to store HTTP SSL cert/key for " << (restricted ? "restricted " : "") << "RPC server: " << error.message());
|
|
return !bool(error);
|
|
}
|
|
return inited;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::check_payment(const std::string &client_message, uint64_t payment, const std::string &rpc, bool same_ts, std::string &message, uint64_t &credits, std::string &top_hash)
|
|
{
|
|
if (m_rpc_payment == NULL)
|
|
{
|
|
credits = 0;
|
|
return true;
|
|
}
|
|
uint64_t height;
|
|
crypto::hash hash;
|
|
m_core.get_blockchain_top(height, hash);
|
|
top_hash = epee::string_tools::pod_to_hex(hash);
|
|
crypto::public_key client;
|
|
uint64_t ts;
|
|
#ifndef NDEBUG
|
|
if (nettype() == TESTNET && client_message == "debug")
|
|
{
|
|
credits = 0;
|
|
return true;
|
|
}
|
|
#endif
|
|
if (!cryptonote::verify_rpc_payment_signature(client_message, client, ts))
|
|
{
|
|
credits = 0;
|
|
message = "Client signature does not verify for " + rpc;
|
|
return false;
|
|
}
|
|
if (!m_rpc_payment->pay(client, ts, payment, rpc, same_ts, credits))
|
|
{
|
|
message = CORE_RPC_STATUS_PAYMENT_REQUIRED;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
#define CHECK_PAYMENT_BASE(req, res, payment, same_ts) do { if (!ctx) break; uint64_t P = (uint64_t)payment; if (P > 0 && !check_payment(req.client, P, tracker.rpc_name(), same_ts, res.status, res.credits, res.top_hash)){return true;} tracker.pay(P); } while(0)
|
|
#define CHECK_PAYMENT(req, res, payment) CHECK_PAYMENT_BASE(req, res, payment, false)
|
|
#define CHECK_PAYMENT_SAME_TS(req, res, payment) CHECK_PAYMENT_BASE(req, res, payment, true)
|
|
#define CHECK_PAYMENT_MIN1(req, res, payment, same_ts) do { if (!ctx || (m_rpc_payment_allow_free_loopback && ctx->m_remote_address.is_loopback())) break; uint64_t P = (uint64_t)payment; if (P == 0) P = 1; if(!check_payment(req.client, P, tracker.rpc_name(), same_ts, res.status, res.credits, res.top_hash)){return true;} tracker.pay(P); } while(0)
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::check_core_ready()
|
|
{
|
|
if(!m_p2p.get_payload_object().is_synchronized())
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::add_host_fail(const connection_context *ctx, unsigned int score)
|
|
{
|
|
if(!ctx || !ctx->m_remote_address.is_blockable() || disable_rpc_ban)
|
|
return false;
|
|
|
|
CRITICAL_REGION_LOCAL(m_host_fails_score_lock);
|
|
uint64_t fails = m_host_fails_score[ctx->m_remote_address.host_str()] += score;
|
|
MDEBUG("Host " << ctx->m_remote_address.host_str() << " fail score=" << fails);
|
|
if(fails > RPC_IP_FAILS_BEFORE_BLOCK)
|
|
{
|
|
auto it = m_host_fails_score.find(ctx->m_remote_address.host_str());
|
|
CHECK_AND_ASSERT_MES(it != m_host_fails_score.end(), false, "internal error");
|
|
it->second = RPC_IP_FAILS_BEFORE_BLOCK/2;
|
|
m_p2p.block_host(ctx->m_remote_address);
|
|
}
|
|
return true;
|
|
}
|
|
#define CHECK_CORE_READY() do { if(!check_core_ready()){res.status = CORE_RPC_STATUS_BUSY;return true;} } while(0)
|
|
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_height(const COMMAND_RPC_GET_HEIGHT::request& req, COMMAND_RPC_GET_HEIGHT::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_height);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_HEIGHT>(invoke_http_mode::JON, "/getheight", req, res, r))
|
|
return r;
|
|
|
|
crypto::hash hash;
|
|
m_core.get_blockchain_top(res.height, hash);
|
|
++res.height; // block height to chain height
|
|
res.hash = string_tools::pod_to_hex(hash);
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_info(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_info);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_INFO>(invoke_http_mode::JON, "/getinfo", req, res, r))
|
|
{
|
|
{
|
|
boost::shared_lock<boost::shared_mutex> lock(m_bootstrap_daemon_mutex);
|
|
if (m_bootstrap_daemon.get() != nullptr)
|
|
{
|
|
res.bootstrap_daemon_address = m_bootstrap_daemon->address();
|
|
}
|
|
}
|
|
crypto::hash top_hash;
|
|
m_core.get_blockchain_top(res.height_without_bootstrap, top_hash);
|
|
++res.height_without_bootstrap; // turn top block height into blockchain height
|
|
res.was_bootstrap_ever_used = true;
|
|
return r;
|
|
}
|
|
|
|
CHECK_PAYMENT_MIN1(req, res, COST_PER_GET_INFO, false);
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
|
|
crypto::hash top_hash;
|
|
m_core.get_blockchain_top(res.height, top_hash);
|
|
++res.height; // turn top block height into blockchain height
|
|
res.top_block_hash = string_tools::pod_to_hex(top_hash);
|
|
res.target_height = m_p2p.get_payload_object().is_synchronized() ? 0 : m_core.get_target_blockchain_height();
|
|
store_difficulty(m_core.get_blockchain_storage().get_difficulty_for_next_block(), res.difficulty, res.wide_difficulty, res.difficulty_top64);
|
|
res.target = m_core.get_blockchain_storage().get_difficulty_target();
|
|
res.tx_count = m_core.get_blockchain_storage().get_total_transactions() - res.height; //without coinbase
|
|
res.tx_pool_size = m_core.get_pool_transactions_count(!restricted);
|
|
res.alt_blocks_count = restricted ? 0 : m_core.get_blockchain_storage().get_alternative_blocks_count();
|
|
uint64_t total_conn = restricted ? 0 : m_p2p.get_public_connections_count();
|
|
res.outgoing_connections_count = restricted ? 0 : m_p2p.get_public_outgoing_connections_count();
|
|
res.incoming_connections_count = restricted ? 0 : (total_conn - res.outgoing_connections_count);
|
|
res.rpc_connections_count = restricted ? 0 : get_connections_count();
|
|
res.white_peerlist_size = restricted ? 0 : m_p2p.get_public_white_peers_count();
|
|
res.grey_peerlist_size = restricted ? 0 : m_p2p.get_public_gray_peers_count();
|
|
|
|
cryptonote::network_type net_type = nettype();
|
|
res.mainnet = net_type == MAINNET;
|
|
res.testnet = net_type == TESTNET;
|
|
res.stagenet = net_type == STAGENET;
|
|
res.nettype = net_type == MAINNET ? "mainnet" : net_type == TESTNET ? "testnet" : net_type == STAGENET ? "stagenet" : "fakechain";
|
|
store_difficulty(m_core.get_blockchain_storage().get_db().get_block_cumulative_difficulty(res.height - 1),
|
|
res.cumulative_difficulty, res.wide_cumulative_difficulty, res.cumulative_difficulty_top64);
|
|
res.block_size_limit = res.block_weight_limit = m_core.get_blockchain_storage().get_current_cumulative_block_weight_limit();
|
|
res.block_size_median = res.block_weight_median = m_core.get_blockchain_storage().get_current_cumulative_block_weight_median();
|
|
res.adjusted_time = m_core.get_blockchain_storage().get_adjusted_time(res.height);
|
|
|
|
res.start_time = restricted ? 0 : (uint64_t)m_core.get_start_time();
|
|
res.free_space = restricted ? std::numeric_limits<uint64_t>::max() : m_core.get_free_space();
|
|
res.offline = m_core.offline();
|
|
res.height_without_bootstrap = restricted ? 0 : res.height;
|
|
if (restricted)
|
|
{
|
|
res.bootstrap_daemon_address = "";
|
|
res.was_bootstrap_ever_used = false;
|
|
}
|
|
else
|
|
{
|
|
boost::shared_lock<boost::shared_mutex> lock(m_bootstrap_daemon_mutex);
|
|
if (m_bootstrap_daemon.get() != nullptr)
|
|
{
|
|
res.bootstrap_daemon_address = m_bootstrap_daemon->address();
|
|
}
|
|
res.was_bootstrap_ever_used = m_was_bootstrap_ever_used;
|
|
}
|
|
res.database_size = m_core.get_blockchain_storage().get_db().get_database_size();
|
|
if (restricted)
|
|
res.database_size = round_up(res.database_size, 5ull* 1024 * 1024 * 1024);
|
|
res.update_available = restricted ? false : m_core.is_update_available();
|
|
res.version = restricted ? "" : MONERO_VERSION_FULL;
|
|
res.synchronized = check_core_ready();
|
|
res.busy_syncing = m_p2p.get_payload_object().is_busy_syncing();
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_net_stats(const COMMAND_RPC_GET_NET_STATS::request& req, COMMAND_RPC_GET_NET_STATS::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_net_stats);
|
|
// No bootstrap daemon check: Only ever get stats about local server
|
|
res.start_time = (uint64_t)m_core.get_start_time();
|
|
{
|
|
CRITICAL_REGION_LOCAL(epee::net_utils::network_throttle_manager::m_lock_get_global_throttle_in);
|
|
epee::net_utils::network_throttle_manager::get_global_throttle_in().get_stats(res.total_packets_in, res.total_bytes_in);
|
|
}
|
|
{
|
|
CRITICAL_REGION_LOCAL(epee::net_utils::network_throttle_manager::m_lock_get_global_throttle_out);
|
|
epee::net_utils::network_throttle_manager::get_global_throttle_out().get_stats(res.total_packets_out, res.total_bytes_out);
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
class pruned_transaction {
|
|
transaction& tx;
|
|
public:
|
|
pruned_transaction(transaction& tx) : tx(tx) {}
|
|
BEGIN_SERIALIZE_OBJECT()
|
|
bool r = tx.serialize_base(ar);
|
|
if (!r) return false;
|
|
END_SERIALIZE()
|
|
};
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_blocks(const COMMAND_RPC_GET_BLOCKS_FAST::request& req, COMMAND_RPC_GET_BLOCKS_FAST::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_blocks);
|
|
|
|
bool use_bootstrap_daemon;
|
|
{
|
|
boost::shared_lock<boost::shared_mutex> lock(m_bootstrap_daemon_mutex);
|
|
use_bootstrap_daemon = m_should_use_bootstrap_daemon;
|
|
}
|
|
if (use_bootstrap_daemon)
|
|
{
|
|
bool r;
|
|
return use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_BLOCKS_FAST>(invoke_http_mode::BIN, "/getblocks.bin", req, res, r);
|
|
}
|
|
|
|
CHECK_PAYMENT(req, res, 1);
|
|
|
|
// quick check for noop
|
|
if (!req.block_ids.empty())
|
|
{
|
|
uint64_t last_block_height;
|
|
crypto::hash last_block_hash;
|
|
m_core.get_blockchain_top(last_block_height, last_block_hash);
|
|
if (last_block_hash == req.block_ids.front())
|
|
{
|
|
res.start_height = 0;
|
|
res.current_height = m_core.get_current_blockchain_height();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
size_t max_blocks = COMMAND_RPC_GET_BLOCKS_FAST_MAX_BLOCK_COUNT;
|
|
if (m_rpc_payment)
|
|
{
|
|
max_blocks = res.credits / COST_PER_BLOCK;
|
|
if (max_blocks > COMMAND_RPC_GET_BLOCKS_FAST_MAX_BLOCK_COUNT)
|
|
max_blocks = COMMAND_RPC_GET_BLOCKS_FAST_MAX_BLOCK_COUNT;
|
|
if (max_blocks == 0)
|
|
{
|
|
res.status = CORE_RPC_STATUS_PAYMENT_REQUIRED;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
std::vector<std::pair<std::pair<cryptonote::blobdata, crypto::hash>, std::vector<std::pair<crypto::hash, cryptonote::blobdata> > > > bs;
|
|
if(!m_core.find_blockchain_supplement(req.start_height, req.block_ids, bs, res.current_height, res.start_height, req.prune, !req.no_miner_tx, max_blocks, COMMAND_RPC_GET_BLOCKS_FAST_MAX_TX_COUNT))
|
|
{
|
|
res.status = "Failed";
|
|
add_host_fail(ctx);
|
|
return true;
|
|
}
|
|
|
|
CHECK_PAYMENT_SAME_TS(req, res, bs.size() * COST_PER_BLOCK);
|
|
|
|
size_t size = 0, ntxes = 0;
|
|
res.blocks.reserve(bs.size());
|
|
res.output_indices.reserve(bs.size());
|
|
for(auto& bd: bs)
|
|
{
|
|
res.blocks.resize(res.blocks.size()+1);
|
|
res.blocks.back().pruned = req.prune;
|
|
res.blocks.back().block = bd.first.first;
|
|
size += bd.first.first.size();
|
|
res.output_indices.push_back(COMMAND_RPC_GET_BLOCKS_FAST::block_output_indices());
|
|
ntxes += bd.second.size();
|
|
res.output_indices.back().indices.reserve(1 + bd.second.size());
|
|
if (req.no_miner_tx)
|
|
res.output_indices.back().indices.push_back(COMMAND_RPC_GET_BLOCKS_FAST::tx_output_indices());
|
|
res.blocks.back().txs.reserve(bd.second.size());
|
|
for (std::vector<std::pair<crypto::hash, cryptonote::blobdata>>::iterator i = bd.second.begin(); i != bd.second.end(); ++i)
|
|
{
|
|
res.blocks.back().txs.push_back({std::move(i->second), crypto::null_hash});
|
|
i->second.clear();
|
|
i->second.shrink_to_fit();
|
|
size += res.blocks.back().txs.back().blob.size();
|
|
}
|
|
|
|
const size_t n_txes_to_lookup = bd.second.size() + (req.no_miner_tx ? 0 : 1);
|
|
if (n_txes_to_lookup > 0)
|
|
{
|
|
std::vector<std::vector<uint64_t>> indices;
|
|
bool r = m_core.get_tx_outputs_gindexs(req.no_miner_tx ? bd.second.front().first : bd.first.second, n_txes_to_lookup, indices);
|
|
if (!r)
|
|
{
|
|
res.status = "Failed";
|
|
return true;
|
|
}
|
|
if (indices.size() != n_txes_to_lookup || res.output_indices.back().indices.size() != (req.no_miner_tx ? 1 : 0))
|
|
{
|
|
res.status = "Failed";
|
|
return true;
|
|
}
|
|
for (size_t i = 0; i < indices.size(); ++i)
|
|
res.output_indices.back().indices.push_back({std::move(indices[i])});
|
|
}
|
|
}
|
|
|
|
MDEBUG("on_get_blocks: " << bs.size() << " blocks, " << ntxes << " txes, size " << size);
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
bool core_rpc_server::on_get_alt_blocks_hashes(const COMMAND_RPC_GET_ALT_BLOCKS_HASHES::request& req, COMMAND_RPC_GET_ALT_BLOCKS_HASHES::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_alt_blocks_hashes);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_ALT_BLOCKS_HASHES>(invoke_http_mode::JON, "/get_alt_blocks_hashes", req, res, r))
|
|
return r;
|
|
|
|
std::vector<block> blks;
|
|
|
|
if(!m_core.get_alternative_blocks(blks))
|
|
{
|
|
res.status = "Failed";
|
|
return true;
|
|
}
|
|
|
|
res.blks_hashes.reserve(blks.size());
|
|
|
|
for (auto const& blk: blks)
|
|
{
|
|
res.blks_hashes.push_back(epee::string_tools::pod_to_hex(get_block_hash(blk)));
|
|
}
|
|
|
|
MDEBUG("on_get_alt_blocks_hashes: " << blks.size() << " blocks " );
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_blocks_by_height(const COMMAND_RPC_GET_BLOCKS_BY_HEIGHT::request& req, COMMAND_RPC_GET_BLOCKS_BY_HEIGHT::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_blocks_by_height);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_BLOCKS_BY_HEIGHT>(invoke_http_mode::BIN, "/getblocks_by_height.bin", req, res, r))
|
|
return r;
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
if (restricted && req.heights.size() > RESTRICTED_BLOCK_COUNT)
|
|
{
|
|
res.status = "Too many blocks requested in restricted mode";
|
|
return true;
|
|
}
|
|
|
|
res.status = "Failed";
|
|
res.blocks.clear();
|
|
res.blocks.reserve(req.heights.size());
|
|
CHECK_PAYMENT_MIN1(req, res, req.heights.size() * COST_PER_BLOCK, false);
|
|
for (uint64_t height : req.heights)
|
|
{
|
|
block blk;
|
|
try
|
|
{
|
|
blk = m_core.get_blockchain_storage().get_db().get_block_from_height(height);
|
|
}
|
|
catch (...)
|
|
{
|
|
res.status = "Error retrieving block at height " + std::to_string(height);
|
|
return true;
|
|
}
|
|
std::vector<transaction> txs;
|
|
std::vector<crypto::hash> missed_txs;
|
|
m_core.get_transactions(blk.tx_hashes, txs, missed_txs);
|
|
res.blocks.resize(res.blocks.size() + 1);
|
|
res.blocks.back().block = block_to_blob(blk);
|
|
for (auto& tx : txs)
|
|
res.blocks.back().txs.push_back({tx_to_blob(tx), crypto::null_hash});
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_hashes(const COMMAND_RPC_GET_HASHES_FAST::request& req, COMMAND_RPC_GET_HASHES_FAST::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_hashes);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_HASHES_FAST>(invoke_http_mode::BIN, "/gethashes.bin", req, res, r))
|
|
return r;
|
|
|
|
CHECK_PAYMENT(req, res, 1);
|
|
|
|
res.start_height = req.start_height;
|
|
if(!m_core.get_blockchain_storage().find_blockchain_supplement(req.block_ids, res.m_block_ids, NULL, res.start_height, res.current_height, false))
|
|
{
|
|
res.status = "Failed";
|
|
add_host_fail(ctx);
|
|
return true;
|
|
}
|
|
|
|
CHECK_PAYMENT_SAME_TS(req, res, res.m_block_ids.size() * COST_PER_BLOCK_HASH);
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_outs_bin(const COMMAND_RPC_GET_OUTPUTS_BIN::request& req, COMMAND_RPC_GET_OUTPUTS_BIN::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_outs_bin);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_OUTPUTS_BIN>(invoke_http_mode::BIN, "/get_outs.bin", req, res, r))
|
|
return r;
|
|
|
|
CHECK_PAYMENT_MIN1(req, res, req.outputs.size() * COST_PER_OUT, false);
|
|
|
|
res.status = "Failed";
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
if (restricted)
|
|
{
|
|
if (req.outputs.size() > MAX_RESTRICTED_GLOBAL_FAKE_OUTS_COUNT)
|
|
{
|
|
res.status = "Too many outs requested";
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if(!m_core.get_outs(req, res))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_outs(const COMMAND_RPC_GET_OUTPUTS::request& req, COMMAND_RPC_GET_OUTPUTS::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_outs);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_OUTPUTS>(invoke_http_mode::JON, "/get_outs", req, res, r))
|
|
return r;
|
|
|
|
CHECK_PAYMENT_MIN1(req, res, req.outputs.size() * COST_PER_OUT, false);
|
|
|
|
res.status = "Failed";
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
if (restricted)
|
|
{
|
|
if (req.outputs.size() > MAX_RESTRICTED_GLOBAL_FAKE_OUTS_COUNT)
|
|
{
|
|
res.status = "Too many outs requested";
|
|
return true;
|
|
}
|
|
}
|
|
|
|
cryptonote::COMMAND_RPC_GET_OUTPUTS_BIN::request req_bin;
|
|
req_bin.outputs = req.outputs;
|
|
req_bin.get_txid = req.get_txid;
|
|
cryptonote::COMMAND_RPC_GET_OUTPUTS_BIN::response res_bin;
|
|
if(!m_core.get_outs(req_bin, res_bin))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
// convert to text
|
|
for (const auto &i: res_bin.outs)
|
|
{
|
|
res.outs.push_back(cryptonote::COMMAND_RPC_GET_OUTPUTS::outkey());
|
|
cryptonote::COMMAND_RPC_GET_OUTPUTS::outkey &outkey = res.outs.back();
|
|
outkey.key = epee::string_tools::pod_to_hex(i.key);
|
|
outkey.mask = epee::string_tools::pod_to_hex(i.mask);
|
|
outkey.unlocked = i.unlocked;
|
|
outkey.height = i.height;
|
|
if (req.get_txid)
|
|
outkey.txid = epee::string_tools::pod_to_hex(i.txid);
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_indexes(const COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::request& req, COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_indexes);
|
|
bool ok;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES>(invoke_http_mode::BIN, "/get_o_indexes.bin", req, res, ok))
|
|
return ok;
|
|
|
|
CHECK_PAYMENT_MIN1(req, res, COST_PER_OUTPUT_INDEXES, false);
|
|
|
|
bool r = m_core.get_tx_outputs_gindexs(req.txid, res.o_indexes);
|
|
if(!r)
|
|
{
|
|
res.status = "Failed";
|
|
return true;
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
LOG_PRINT_L2("COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES: [" << res.o_indexes.size() << "]");
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_transactions(const COMMAND_RPC_GET_TRANSACTIONS::request& req, COMMAND_RPC_GET_TRANSACTIONS::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_transactions);
|
|
bool ok;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_TRANSACTIONS>(invoke_http_mode::JON, "/gettransactions", req, res, ok))
|
|
return ok;
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
const bool request_has_rpc_origin = ctx != NULL;
|
|
|
|
if (restricted && req.txs_hashes.size() > RESTRICTED_TRANSACTIONS_COUNT)
|
|
{
|
|
res.status = "Too many transactions requested in restricted mode";
|
|
return true;
|
|
}
|
|
|
|
CHECK_PAYMENT_MIN1(req, res, req.txs_hashes.size() * COST_PER_TX, false);
|
|
|
|
std::vector<crypto::hash> vh;
|
|
for(const auto& tx_hex_str: req.txs_hashes)
|
|
{
|
|
blobdata b;
|
|
if(!string_tools::parse_hexstr_to_binbuff(tx_hex_str, b))
|
|
{
|
|
res.status = "Failed to parse hex representation of transaction hash";
|
|
return true;
|
|
}
|
|
if(b.size() != sizeof(crypto::hash))
|
|
{
|
|
res.status = "Failed, size of data mismatch";
|
|
return true;
|
|
}
|
|
vh.push_back(*reinterpret_cast<const crypto::hash*>(b.data()));
|
|
}
|
|
std::vector<crypto::hash> missed_txs;
|
|
std::vector<std::tuple<crypto::hash, cryptonote::blobdata, crypto::hash, cryptonote::blobdata>> txs;
|
|
bool r = m_core.get_split_transactions_blobs(vh, txs, missed_txs);
|
|
if(!r)
|
|
{
|
|
res.status = "Failed";
|
|
return true;
|
|
}
|
|
LOG_PRINT_L2("Found " << txs.size() << "/" << vh.size() << " transactions on the blockchain");
|
|
|
|
// try the pool for any missing txes
|
|
size_t found_in_pool = 0;
|
|
std::unordered_set<crypto::hash> pool_tx_hashes;
|
|
std::unordered_map<crypto::hash, tx_info> per_tx_pool_tx_info;
|
|
if (!missed_txs.empty())
|
|
{
|
|
std::vector<tx_info> pool_tx_info;
|
|
std::vector<spent_key_image_info> pool_key_image_info;
|
|
std::vector<used_nonce_info> pool_used_onnce_info;
|
|
bool r = m_core.get_pool_transactions_and_spent_keys_info(pool_tx_info, pool_key_image_info, pool_used_onnce_info, !request_has_rpc_origin || !restricted);
|
|
if(r)
|
|
{
|
|
// sort to match original request
|
|
std::vector<std::tuple<crypto::hash, cryptonote::blobdata, crypto::hash, cryptonote::blobdata>> sorted_txs;
|
|
std::vector<tx_info>::const_iterator i;
|
|
unsigned txs_processed = 0;
|
|
for (const crypto::hash &h: vh)
|
|
{
|
|
if (std::find(missed_txs.begin(), missed_txs.end(), h) == missed_txs.end())
|
|
{
|
|
if (txs.size() == txs_processed)
|
|
{
|
|
res.status = "Failed: internal error - txs is empty";
|
|
return true;
|
|
}
|
|
// core returns the ones it finds in the right order
|
|
if (std::get<0>(txs[txs_processed]) != h)
|
|
{
|
|
res.status = "Failed: tx hash mismatch";
|
|
return true;
|
|
}
|
|
sorted_txs.push_back(std::move(txs[txs_processed]));
|
|
++txs_processed;
|
|
}
|
|
else if ((i = std::find_if(pool_tx_info.begin(), pool_tx_info.end(), [h](const tx_info &txi) { return epee::string_tools::pod_to_hex(h) == txi.id_hash; })) != pool_tx_info.end())
|
|
{
|
|
cryptonote::transaction tx;
|
|
if (!cryptonote::parse_and_validate_tx_from_blob(i->tx_blob, tx))
|
|
{
|
|
res.status = "Failed to parse and validate tx from blob";
|
|
return true;
|
|
}
|
|
std::stringstream ss;
|
|
binary_archive<true> ba(ss);
|
|
bool r = const_cast<cryptonote::transaction&>(tx).serialize_base(ba);
|
|
if (!r)
|
|
{
|
|
res.status = "Failed to serialize transaction base";
|
|
return true;
|
|
}
|
|
const cryptonote::blobdata pruned = ss.str();
|
|
const crypto::hash prunable_hash = tx.version == 1 ? crypto::null_hash : get_transaction_prunable_hash(tx);
|
|
sorted_txs.push_back(std::make_tuple(h, pruned, prunable_hash, std::string(i->tx_blob, pruned.size())));
|
|
missed_txs.erase(std::find(missed_txs.begin(), missed_txs.end(), h));
|
|
pool_tx_hashes.insert(h);
|
|
const std::string hash_string = epee::string_tools::pod_to_hex(h);
|
|
for (const auto &ti: pool_tx_info)
|
|
{
|
|
if (ti.id_hash == hash_string)
|
|
{
|
|
per_tx_pool_tx_info.insert(std::make_pair(h, ti));
|
|
break;
|
|
}
|
|
}
|
|
++found_in_pool;
|
|
}
|
|
}
|
|
txs = sorted_txs;
|
|
}
|
|
LOG_PRINT_L2("Found " << found_in_pool << "/" << vh.size() << " transactions in the pool");
|
|
}
|
|
|
|
std::vector<std::string>::const_iterator txhi = req.txs_hashes.begin();
|
|
std::vector<crypto::hash>::const_iterator vhi = vh.begin();
|
|
for(auto& tx: txs)
|
|
{
|
|
res.txs.push_back(COMMAND_RPC_GET_TRANSACTIONS::entry());
|
|
COMMAND_RPC_GET_TRANSACTIONS::entry &e = res.txs.back();
|
|
|
|
crypto::hash tx_hash = *vhi++;
|
|
e.tx_hash = *txhi++;
|
|
e.prunable_hash = epee::string_tools::pod_to_hex(std::get<2>(tx));
|
|
if (req.split || req.prune || std::get<3>(tx).empty())
|
|
{
|
|
// use splitted form with pruned and prunable (filled only when prune=false and the daemon has it), leaving as_hex as empty
|
|
e.pruned_as_hex = string_tools::buff_to_hex_nodelimer(std::get<1>(tx));
|
|
if (!req.prune)
|
|
e.prunable_as_hex = string_tools::buff_to_hex_nodelimer(std::get<3>(tx));
|
|
if (req.decode_as_json)
|
|
{
|
|
cryptonote::blobdata tx_data;
|
|
cryptonote::transaction t;
|
|
if (req.prune || std::get<3>(tx).empty())
|
|
{
|
|
// decode pruned tx to JSON
|
|
tx_data = std::get<1>(tx);
|
|
if (cryptonote::parse_and_validate_tx_base_from_blob(tx_data, t))
|
|
{
|
|
pruned_transaction pruned_tx{t};
|
|
e.as_json = obj_to_json_str(pruned_tx);
|
|
}
|
|
else
|
|
{
|
|
res.status = "Failed to parse and validate pruned tx from blob";
|
|
return true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// decode full tx to JSON
|
|
tx_data = std::get<1>(tx) + std::get<3>(tx);
|
|
if (cryptonote::parse_and_validate_tx_from_blob(tx_data, t))
|
|
{
|
|
e.as_json = obj_to_json_str(t);
|
|
}
|
|
else
|
|
{
|
|
res.status = "Failed to parse and validate tx from blob";
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// use non-splitted form, leaving pruned_as_hex and prunable_as_hex as empty
|
|
cryptonote::blobdata tx_data = std::get<1>(tx) + std::get<3>(tx);
|
|
e.as_hex = string_tools::buff_to_hex_nodelimer(tx_data);
|
|
if (req.decode_as_json)
|
|
{
|
|
cryptonote::transaction t;
|
|
if (cryptonote::parse_and_validate_tx_from_blob(tx_data, t))
|
|
{
|
|
e.as_json = obj_to_json_str(t);
|
|
}
|
|
else
|
|
{
|
|
res.status = "Failed to parse and validate tx from blob";
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
e.in_pool = pool_tx_hashes.find(tx_hash) != pool_tx_hashes.end();
|
|
if (e.in_pool)
|
|
{
|
|
e.block_height = e.block_timestamp = std::numeric_limits<uint64_t>::max();
|
|
auto it = per_tx_pool_tx_info.find(tx_hash);
|
|
if (it != per_tx_pool_tx_info.end())
|
|
{
|
|
e.double_spend_seen = it->second.double_spend_seen;
|
|
e.relayed = it->second.relayed;
|
|
e.received_timestamp = it->second.receive_time;
|
|
}
|
|
else
|
|
{
|
|
MERROR("Failed to determine pool info for " << tx_hash);
|
|
e.double_spend_seen = false;
|
|
e.relayed = false;
|
|
e.received_timestamp = 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
e.block_height = m_core.get_blockchain_storage().get_db().get_tx_block_height(tx_hash);
|
|
e.block_timestamp = m_core.get_blockchain_storage().get_db().get_block_timestamp(e.block_height);
|
|
e.received_timestamp = 0;
|
|
e.double_spend_seen = false;
|
|
e.relayed = false;
|
|
}
|
|
|
|
// fill up old style responses too, in case an old wallet asks
|
|
res.txs_as_hex.push_back(e.as_hex);
|
|
if (req.decode_as_json)
|
|
res.txs_as_json.push_back(e.as_json);
|
|
|
|
// output indices too if not in pool
|
|
if (pool_tx_hashes.find(tx_hash) == pool_tx_hashes.end())
|
|
{
|
|
bool r = m_core.get_tx_outputs_gindexs(tx_hash, e.output_indices);
|
|
if (!r)
|
|
{
|
|
res.status = "Failed";
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// CC
|
|
e.cc_nonce = 0;
|
|
e.cc_fee = 0;
|
|
if (req.get_cc_data)
|
|
{
|
|
cryptonote::transaction_prefix t;
|
|
if (!cryptonote::parse_and_validate_tx_prefix_from_blob(std::get<1>(tx), t))
|
|
{
|
|
res.status = "Failed";
|
|
return false;
|
|
}
|
|
if (t.version == 2 && t.minor_version > 0)
|
|
{
|
|
const cryptonote::cc_command_basenonce_t *basenonce = cryptonote::get_cc_command_basenonce(t.cc_cmd);
|
|
if (basenonce)
|
|
{
|
|
e.cc_nonce = basenonce->cc_nonce;
|
|
}
|
|
if (t.vin.empty() && t.vout.empty())
|
|
{
|
|
e.cc_fee = t.cc_fee;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for(const auto& miss_tx: missed_txs)
|
|
{
|
|
res.missed_tx.push_back(string_tools::pod_to_hex(miss_tx));
|
|
}
|
|
|
|
LOG_PRINT_L2(res.txs.size() << " transactions found, " << res.missed_tx.size() << " not found");
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_is_key_image_spent(const COMMAND_RPC_IS_KEY_IMAGE_SPENT::request& req, COMMAND_RPC_IS_KEY_IMAGE_SPENT::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(is_key_image_spent);
|
|
bool ok;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_IS_KEY_IMAGE_SPENT>(invoke_http_mode::JON, "/is_key_image_spent", req, res, ok))
|
|
return ok;
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
const bool request_has_rpc_origin = ctx != NULL;
|
|
|
|
if (restricted && req.key_images.size() > RESTRICTED_SPENT_KEY_IMAGES_COUNT)
|
|
{
|
|
res.status = "Too many key images queried in restricted mode";
|
|
return true;
|
|
}
|
|
|
|
CHECK_PAYMENT_MIN1(req, res, req.key_images.size() * COST_PER_KEY_IMAGE, false);
|
|
|
|
std::vector<crypto::key_image> key_images;
|
|
for(const auto& ki_hex_str: req.key_images)
|
|
{
|
|
blobdata b;
|
|
if(!string_tools::parse_hexstr_to_binbuff(ki_hex_str, b))
|
|
{
|
|
res.status = "Failed to parse hex representation of key image";
|
|
return true;
|
|
}
|
|
if(b.size() != sizeof(crypto::key_image))
|
|
{
|
|
res.status = "Failed, size of data mismatch";
|
|
}
|
|
key_images.push_back(*reinterpret_cast<const crypto::key_image*>(b.data()));
|
|
}
|
|
std::vector<bool> spent_status;
|
|
bool r = m_core.are_key_images_spent(key_images, spent_status);
|
|
if(!r)
|
|
{
|
|
res.status = "Failed";
|
|
return true;
|
|
}
|
|
res.spent_status.clear();
|
|
for (size_t n = 0; n < spent_status.size(); ++n)
|
|
res.spent_status.push_back(spent_status[n] ? COMMAND_RPC_IS_KEY_IMAGE_SPENT::SPENT_IN_BLOCKCHAIN : COMMAND_RPC_IS_KEY_IMAGE_SPENT::UNSPENT);
|
|
|
|
// check the pool too
|
|
std::vector<cryptonote::tx_info> txs;
|
|
std::vector<cryptonote::spent_key_image_info> ki;
|
|
std::vector<cryptonote::used_nonce_info> nonces;
|
|
r = m_core.get_pool_transactions_and_spent_keys_info(txs, ki, nonces, !request_has_rpc_origin || !restricted);
|
|
if(!r)
|
|
{
|
|
res.status = "Failed";
|
|
return true;
|
|
}
|
|
for (std::vector<cryptonote::spent_key_image_info>::const_iterator i = ki.begin(); i != ki.end(); ++i)
|
|
{
|
|
crypto::hash hash;
|
|
crypto::key_image spent_key_image;
|
|
if (parse_hash256(i->id_hash, hash))
|
|
{
|
|
memcpy(&spent_key_image, &hash, sizeof(hash)); // a bit dodgy, should be other parse functions somewhere
|
|
for (size_t n = 0; n < res.spent_status.size(); ++n)
|
|
{
|
|
if (res.spent_status[n] == COMMAND_RPC_IS_KEY_IMAGE_SPENT::UNSPENT)
|
|
{
|
|
if (key_images[n] == spent_key_image)
|
|
{
|
|
res.spent_status[n] = COMMAND_RPC_IS_KEY_IMAGE_SPENT::SPENT_IN_POOL;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_send_raw_tx(const COMMAND_RPC_SEND_RAW_TX::request& req, COMMAND_RPC_SEND_RAW_TX::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(send_raw_tx);
|
|
|
|
{
|
|
bool ok;
|
|
use_bootstrap_daemon_if_necessary<COMMAND_RPC_SEND_RAW_TX>(invoke_http_mode::JON, "/sendrawtransaction", req, res, ok);
|
|
}
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
|
|
bool skip_validation = false;
|
|
if (!restricted)
|
|
{
|
|
boost::shared_lock<boost::shared_mutex> lock(m_bootstrap_daemon_mutex);
|
|
if (m_should_use_bootstrap_daemon)
|
|
{
|
|
skip_validation = !check_core_ready();
|
|
}
|
|
else
|
|
{
|
|
CHECK_CORE_READY();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CHECK_CORE_READY();
|
|
}
|
|
|
|
CHECK_PAYMENT_MIN1(req, res, COST_PER_TX_RELAY, false);
|
|
|
|
std::string tx_blob;
|
|
if(!string_tools::parse_hexstr_to_binbuff(req.tx_as_hex, tx_blob))
|
|
{
|
|
LOG_PRINT_L0("[on_send_raw_tx]: Failed to parse tx from hexbuff: " << req.tx_as_hex);
|
|
res.status = "Failed";
|
|
return true;
|
|
}
|
|
|
|
if (req.do_sanity_checks && !cryptonote::tx_sanity_check(tx_blob, m_core.get_blockchain_storage().get_num_mature_outputs(0)))
|
|
{
|
|
res.status = "Failed";
|
|
res.reason = "Sanity check failed";
|
|
res.sanity_check_failed = true;
|
|
return true;
|
|
}
|
|
res.sanity_check_failed = false;
|
|
|
|
if (!skip_validation)
|
|
{
|
|
tx_verification_context tvc{};
|
|
if(!m_core.handle_incoming_tx({tx_blob, crypto::null_hash}, tvc, (req.do_not_relay ? relay_method::none : relay_method::fluff), false) || tvc.m_verifivation_failed)
|
|
{
|
|
res.status = "Failed";
|
|
std::string reason = "";
|
|
if ((res.low_mixin = tvc.m_low_mixin))
|
|
add_reason(reason, "bad ring size");
|
|
if ((res.double_spend = tvc.m_double_spend))
|
|
add_reason(reason, "double spend");
|
|
if ((res.invalid_input = tvc.m_invalid_input))
|
|
add_reason(reason, "invalid input");
|
|
if ((res.invalid_output = tvc.m_invalid_output))
|
|
add_reason(reason, "invalid output");
|
|
if ((res.too_big = tvc.m_too_big))
|
|
add_reason(reason, "too big");
|
|
if ((res.overspend = tvc.m_overspend))
|
|
add_reason(reason, "overspend");
|
|
if ((res.fee_too_low = tvc.m_fee_too_low))
|
|
add_reason(reason, "fee too low");
|
|
if ((res.too_few_outputs = tvc.m_too_few_outputs))
|
|
add_reason(reason, "too few outputs");
|
|
|
|
// CC
|
|
if ((res.cc_invalid_account = tvc.m_cc_invalid_account))
|
|
add_reason(reason, "invalid account");
|
|
if ((res.cc_invalid_flag = tvc.m_cc_invalid_flag))
|
|
add_reason(reason, "invalid flag");
|
|
if ((res.cc_not_owned = tvc.m_cc_not_owned))
|
|
add_reason(reason, "resource not owned by account");
|
|
if ((res.cc_bad_role = tvc.m_cc_bad_role))
|
|
add_reason(reason, "invalid role in this context");
|
|
if ((res.cc_balance = tvc.m_cc_balance))
|
|
add_reason(reason, "insufficient balance");
|
|
if ((res.cc_overflow = tvc.m_cc_overflow))
|
|
add_reason(reason, "overflow");
|
|
if ((res.cc_bad_item = tvc.m_cc_bad_item))
|
|
add_reason(reason, "bad item");
|
|
if ((res.cc_bad_amount = tvc.m_cc_bad_amount))
|
|
add_reason(reason, "bad amount");
|
|
if ((res.cc_bad_price = tvc.m_cc_bad_price))
|
|
add_reason(reason, "bad price");
|
|
if ((res.cc_too_large = tvc.m_cc_too_large))
|
|
add_reason(reason, "data is too large");
|
|
if ((res.cc_flag_too_small = tvc.m_cc_flag_too_small))
|
|
add_reason(reason, "flag is too small");
|
|
if ((res.cc_bad_coords = tvc.m_cc_bad_coords))
|
|
add_reason(reason, "bad coordinates");
|
|
if ((res.cc_bad_economic_power = tvc.m_cc_bad_economic_power))
|
|
add_reason(reason, "invalid economic power");
|
|
if ((res.cc_out_of_range = tvc.m_cc_out_of_range))
|
|
add_reason(reason, "out of range");
|
|
if ((res.cc_invalid_city = tvc.m_cc_invalid_city))
|
|
add_reason(reason, "invalid city");
|
|
if ((res.cc_bad_cost = tvc.m_cc_bad_cost))
|
|
add_reason(reason, "invalid cost");
|
|
if ((res.cc_conflict = tvc.m_cc_conflict))
|
|
add_reason(reason, "command conflicts with another");
|
|
if ((res.cc_fee = tvc.m_cc_fee))
|
|
add_reason(reason, "bad fee");
|
|
if ((res.cc_invalid_nonce = tvc.m_cc_invalid_nonce))
|
|
add_reason(reason, "invalid nonce");
|
|
if ((res.cc_bad_match = tvc.m_cc_bad_match))
|
|
add_reason(reason, "bad matched command");
|
|
if ((res.cc_expired = tvc.m_cc_expired))
|
|
add_reason(reason, "command expired");
|
|
if ((res.cc_bad_expiry = tvc.m_cc_bad_expiry))
|
|
add_reason(reason, "invalid expiry time");
|
|
if ((res.cc_invalid_utf8 = tvc.m_cc_invalid_utf8))
|
|
add_reason(reason, "invalid character");
|
|
if ((res.cc_forbidden_character = tvc.m_cc_forbidden_character))
|
|
add_reason(reason, "forbidden character");
|
|
if ((res.cc_bad_block_data = tvc.m_cc_bad_block_data))
|
|
add_reason(reason, "bad block data");
|
|
if ((res.cc_bad_palette_data = tvc.m_cc_bad_palette_data))
|
|
add_reason(reason, "bad palette data");
|
|
if ((res.cc_bad_name = tvc.m_cc_bad_name))
|
|
add_reason(reason, "bad name");
|
|
if ((res.cc_bad_repair = tvc.m_cc_bad_repair))
|
|
add_reason(reason, "bad repair");
|
|
if ((res.cc_inactive = tvc.m_cc_inactive))
|
|
add_reason(reason, "active flag is not as expected");
|
|
if ((res.cc_on_fire = tvc.m_cc_on_fire))
|
|
add_reason(reason, "flag is on fire");
|
|
if ((res.cc_invalid_attribute = tvc.m_cc_invalid_attribute))
|
|
add_reason(reason, "invalid attribute");
|
|
if ((res.cc_duplicate = tvc.m_cc_duplicate))
|
|
add_reason(reason, "duplicate");
|
|
if ((res.cc_invalid_badge = tvc.m_cc_invalid_badge))
|
|
add_reason(reason, "invalid badge");
|
|
if ((res.cc_name_already_in_use = tvc.m_cc_name_already_in_use))
|
|
add_reason(reason, "name already in use");
|
|
if ((res.cc_palette_in_use = tvc.m_cc_palette_in_use))
|
|
add_reason(reason, "palette entry still in use");
|
|
if ((res.cc_level = tvc.m_cc_level))
|
|
add_reason(reason, "level too low");
|
|
if ((res.cc_invalid_signature = tvc.m_cc_invalid_signature))
|
|
add_reason(reason, "invalid signature");
|
|
if ((res.cc_locked = tvc.m_cc_locked))
|
|
add_reason(reason, "feature not unlocked yet");
|
|
if ((res.cc_invalid_script = tvc.m_cc_invalid_script))
|
|
add_reason(reason, "invalid script");
|
|
if ((res.cc_mortgaged = tvc.m_cc_mortgaged))
|
|
add_reason(reason, "flag is mortgaged");
|
|
if ((res.cc_auctioned = tvc.m_cc_auctioned))
|
|
add_reason(reason, "flag is auctioned");
|
|
if ((res.cc_carved_runestone = tvc.m_cc_carved_runestone))
|
|
add_reason(reason, "there is a carved runestone");
|
|
if ((res.cc_no_runestone = tvc.m_cc_no_runestone))
|
|
add_reason(reason, "no runestone found");
|
|
if ((res.cc_invalid_previous_state = tvc.m_cc_invalid_previous_state))
|
|
add_reason(reason, "invalid previous state");
|
|
if ((res.cc_invalid_auction = tvc.m_cc_invalid_auction))
|
|
add_reason(reason, "invalid auction");
|
|
|
|
const std::string punctuation = reason.empty() ? "" : ": ";
|
|
if (tvc.m_verifivation_failed)
|
|
{
|
|
LOG_PRINT_L0("[on_send_raw_tx]: tx verification failed" << punctuation << reason);
|
|
}
|
|
else
|
|
{
|
|
LOG_PRINT_L0("[on_send_raw_tx]: Failed to process tx" << punctuation << reason);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if(tvc.m_relay == relay_method::none)
|
|
{
|
|
LOG_PRINT_L0("[on_send_raw_tx]: tx accepted, but not relayed");
|
|
res.reason = "Not relayed";
|
|
res.not_relayed = true;
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
NOTIFY_NEW_TRANSACTIONS::request r;
|
|
r.txs.push_back(std::move(tx_blob));
|
|
m_core.get_protocol()->relay_transactions(r, boost::uuids::nil_uuid(), epee::net_utils::zone::invalid, relay_method::local);
|
|
//TODO: make sure that tx has reached other nodes here, probably wait to receive reflections from other nodes
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_start_mining(const COMMAND_RPC_START_MINING::request& req, COMMAND_RPC_START_MINING::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(start_mining);
|
|
CHECK_CORE_READY();
|
|
cryptonote::address_parse_info info;
|
|
if(!get_account_address_from_str(info, nettype(), req.miner_address))
|
|
{
|
|
res.status = "Failed, wrong address";
|
|
LOG_PRINT_L0(res.status);
|
|
return true;
|
|
}
|
|
if (info.is_subaddress)
|
|
{
|
|
res.status = "Mining to subaddress isn't supported yet";
|
|
LOG_PRINT_L0(res.status);
|
|
return true;
|
|
}
|
|
crypto::public_key game_account_key;
|
|
if (!req.game_account_key.empty())
|
|
{
|
|
if (!epee::string_tools::hex_to_pod(req.game_account_key, game_account_key))
|
|
{
|
|
res.status = "Invalid game account key";
|
|
LOG_PRINT_L0(res.status);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
unsigned int concurrency_count = boost::thread::hardware_concurrency() * 4;
|
|
|
|
// if we couldn't detect threads, set it to a ridiculously high number
|
|
if(concurrency_count == 0)
|
|
{
|
|
concurrency_count = 257;
|
|
}
|
|
|
|
// if there are more threads requested than the hardware supports
|
|
// then we fail and log that.
|
|
if(req.threads_count > concurrency_count)
|
|
{
|
|
res.status = "Failed, too many threads relative to CPU cores.";
|
|
LOG_PRINT_L0(res.status);
|
|
return true;
|
|
}
|
|
|
|
cryptonote::miner &miner= m_core.get_miner();
|
|
if (miner.is_mining())
|
|
{
|
|
res.status = "Already mining";
|
|
return true;
|
|
}
|
|
if(!miner.start(info.address, game_account_key, static_cast<size_t>(req.threads_count), req.do_background_mining, req.ignore_battery))
|
|
{
|
|
res.status = "Failed, mining not started";
|
|
LOG_PRINT_L0(res.status);
|
|
return true;
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_stop_mining(const COMMAND_RPC_STOP_MINING::request& req, COMMAND_RPC_STOP_MINING::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(stop_mining);
|
|
cryptonote::miner &miner= m_core.get_miner();
|
|
if(!miner.is_mining())
|
|
{
|
|
res.status = "Mining never started";
|
|
LOG_PRINT_L0(res.status);
|
|
return true;
|
|
}
|
|
if(!miner.stop())
|
|
{
|
|
res.status = "Failed, mining not stopped";
|
|
LOG_PRINT_L0(res.status);
|
|
return true;
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_mining_status(const COMMAND_RPC_MINING_STATUS::request& req, COMMAND_RPC_MINING_STATUS::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(mining_status);
|
|
|
|
const miner& lMiner = m_core.get_miner();
|
|
res.active = lMiner.is_mining();
|
|
res.is_background_mining_enabled = lMiner.get_is_background_mining_enabled();
|
|
store_difficulty(m_core.get_blockchain_storage().get_difficulty_for_next_block(), res.difficulty, res.wide_difficulty, res.difficulty_top64);
|
|
|
|
res.block_target = m_core.get_blockchain_storage().get_current_hard_fork_version() < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2;
|
|
if ( lMiner.is_mining() ) {
|
|
res.speed = lMiner.get_speed();
|
|
res.threads_count = lMiner.get_threads_count();
|
|
res.block_reward = lMiner.get_block_reward();
|
|
}
|
|
const account_public_address& lMiningAdr = lMiner.get_mining_address();
|
|
if (lMiner.is_mining() || lMiner.get_is_background_mining_enabled())
|
|
res.address = get_account_address_as_str(nettype(), false, lMiningAdr);
|
|
const uint8_t major_version = m_core.get_blockchain_storage().get_current_hard_fork_version();
|
|
const unsigned variant = major_version >= 7 ? major_version - 6 : 0;
|
|
switch (variant)
|
|
{
|
|
case 0: res.pow_algorithm = "Cryptonight"; break;
|
|
case 1: res.pow_algorithm = "CNv1 (Cryptonight variant 1)"; break;
|
|
case 2: case 3: res.pow_algorithm = "CNv2 (Cryptonight variant 2)"; break;
|
|
case 4: case 5: res.pow_algorithm = "CNv4 (Cryptonight variant 4)"; break;
|
|
case 6: case 7: case 8: case 9: res.pow_algorithm = "RandomX"; break;
|
|
default: res.pow_algorithm = "RandomX"; break; // assumed
|
|
}
|
|
if (res.is_background_mining_enabled)
|
|
{
|
|
res.bg_idle_threshold = lMiner.get_idle_threshold();
|
|
res.bg_min_idle_seconds = lMiner.get_min_idle_seconds();
|
|
res.bg_ignore_battery = lMiner.get_ignore_battery();
|
|
res.bg_target = lMiner.get_mining_target();
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_save_bc(const COMMAND_RPC_SAVE_BC::request& req, COMMAND_RPC_SAVE_BC::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(save_bc);
|
|
if( !m_core.get_blockchain_storage().store_blockchain() )
|
|
{
|
|
res.status = "Error while storing blockchain";
|
|
return true;
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_peer_list(const COMMAND_RPC_GET_PEER_LIST::request& req, COMMAND_RPC_GET_PEER_LIST::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_peer_list);
|
|
std::vector<nodetool::peerlist_entry> white_list;
|
|
std::vector<nodetool::peerlist_entry> gray_list;
|
|
|
|
if (req.public_only)
|
|
{
|
|
m_p2p.get_public_peerlist(gray_list, white_list);
|
|
}
|
|
else
|
|
{
|
|
m_p2p.get_peerlist(gray_list, white_list);
|
|
}
|
|
|
|
for (auto & entry : white_list)
|
|
{
|
|
if (!req.include_blocked && m_p2p.is_host_blocked(entry.adr, NULL))
|
|
continue;
|
|
if (entry.adr.get_type_id() == epee::net_utils::ipv4_network_address::get_type_id())
|
|
res.white_list.emplace_back(entry.id, entry.adr.as<epee::net_utils::ipv4_network_address>().ip(),
|
|
entry.adr.as<epee::net_utils::ipv4_network_address>().port(), entry.last_seen, entry.pruning_seed, entry.rpc_port, entry.rpc_credits_per_hash);
|
|
else if (entry.adr.get_type_id() == epee::net_utils::ipv6_network_address::get_type_id())
|
|
res.white_list.emplace_back(entry.id, entry.adr.as<epee::net_utils::ipv6_network_address>().host_str(),
|
|
entry.adr.as<epee::net_utils::ipv6_network_address>().port(), entry.last_seen, entry.pruning_seed, entry.rpc_port, entry.rpc_credits_per_hash);
|
|
else
|
|
res.white_list.emplace_back(entry.id, entry.adr.str(), entry.last_seen, entry.pruning_seed, entry.rpc_port, entry.rpc_credits_per_hash);
|
|
}
|
|
|
|
for (auto & entry : gray_list)
|
|
{
|
|
if (!req.include_blocked && m_p2p.is_host_blocked(entry.adr, NULL))
|
|
continue;
|
|
if (entry.adr.get_type_id() == epee::net_utils::ipv4_network_address::get_type_id())
|
|
res.gray_list.emplace_back(entry.id, entry.adr.as<epee::net_utils::ipv4_network_address>().ip(),
|
|
entry.adr.as<epee::net_utils::ipv4_network_address>().port(), entry.last_seen, entry.pruning_seed, entry.rpc_port, entry.rpc_credits_per_hash);
|
|
else if (entry.adr.get_type_id() == epee::net_utils::ipv6_network_address::get_type_id())
|
|
res.gray_list.emplace_back(entry.id, entry.adr.as<epee::net_utils::ipv6_network_address>().host_str(),
|
|
entry.adr.as<epee::net_utils::ipv6_network_address>().port(), entry.last_seen, entry.pruning_seed, entry.rpc_port, entry.rpc_credits_per_hash);
|
|
else
|
|
res.gray_list.emplace_back(entry.id, entry.adr.str(), entry.last_seen, entry.pruning_seed, entry.rpc_port, entry.rpc_credits_per_hash);
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_public_nodes(const COMMAND_RPC_GET_PUBLIC_NODES::request& req, COMMAND_RPC_GET_PUBLIC_NODES::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_public_nodes);
|
|
|
|
COMMAND_RPC_GET_PEER_LIST::request peer_list_req;
|
|
COMMAND_RPC_GET_PEER_LIST::response peer_list_res;
|
|
peer_list_req.include_blocked = req.include_blocked;
|
|
const bool success = on_get_peer_list(peer_list_req, peer_list_res, ctx);
|
|
res.status = peer_list_res.status;
|
|
if (!success)
|
|
{
|
|
res.status = "Failed to get peer list";
|
|
return true;
|
|
}
|
|
if (res.status != CORE_RPC_STATUS_OK)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
const auto collect = [](const std::vector<peer> &peer_list, std::vector<public_node> &public_nodes)
|
|
{
|
|
for (const auto &entry : peer_list)
|
|
{
|
|
if (entry.rpc_port != 0)
|
|
{
|
|
public_nodes.emplace_back(entry);
|
|
}
|
|
}
|
|
};
|
|
|
|
if (req.white)
|
|
{
|
|
collect(peer_list_res.white_list, res.white);
|
|
}
|
|
if (req.gray)
|
|
{
|
|
collect(peer_list_res.gray_list, res.gray);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_set_log_hash_rate(const COMMAND_RPC_SET_LOG_HASH_RATE::request& req, COMMAND_RPC_SET_LOG_HASH_RATE::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(set_log_hash_rate);
|
|
if(m_core.get_miner().is_mining())
|
|
{
|
|
m_core.get_miner().do_print_hashrate(req.visible);
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
}
|
|
else
|
|
{
|
|
res.status = CORE_RPC_STATUS_NOT_MINING;
|
|
}
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_set_log_level(const COMMAND_RPC_SET_LOG_LEVEL::request& req, COMMAND_RPC_SET_LOG_LEVEL::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(set_log_level);
|
|
if (req.level < 0 || req.level > 4)
|
|
{
|
|
res.status = "Error: log level not valid";
|
|
return true;
|
|
}
|
|
mlog_set_log_level(req.level);
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_set_log_categories(const COMMAND_RPC_SET_LOG_CATEGORIES::request& req, COMMAND_RPC_SET_LOG_CATEGORIES::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(set_log_categories);
|
|
mlog_set_log(req.categories.c_str());
|
|
res.categories = mlog_get_categories();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_transaction_pool(const COMMAND_RPC_GET_TRANSACTION_POOL::request& req, COMMAND_RPC_GET_TRANSACTION_POOL::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_transaction_pool);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_TRANSACTION_POOL>(invoke_http_mode::JON, "/get_transaction_pool", req, res, r))
|
|
return r;
|
|
|
|
CHECK_PAYMENT(req, res, 1);
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
const bool request_has_rpc_origin = ctx != NULL;
|
|
const bool allow_sensitive = !request_has_rpc_origin || !restricted;
|
|
|
|
size_t n_txes = m_core.get_pool_transactions_count(allow_sensitive);
|
|
if (n_txes > 0)
|
|
{
|
|
CHECK_PAYMENT_SAME_TS(req, res, n_txes * COST_PER_TX);
|
|
m_core.get_pool_transactions_and_spent_keys_info(res.transactions, res.spent_key_images, res.used_nonces, allow_sensitive);
|
|
for (tx_info& txi : res.transactions)
|
|
txi.tx_blob = epee::string_tools::buff_to_hex_nodelimer(txi.tx_blob);
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_transaction_pool_hashes_bin(const COMMAND_RPC_GET_TRANSACTION_POOL_HASHES_BIN::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_HASHES_BIN::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_transaction_pool_hashes);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_TRANSACTION_POOL_HASHES_BIN>(invoke_http_mode::JON, "/get_transaction_pool_hashes.bin", req, res, r))
|
|
return r;
|
|
|
|
CHECK_PAYMENT(req, res, 1);
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
const bool request_has_rpc_origin = ctx != NULL;
|
|
const bool allow_sensitive = !request_has_rpc_origin || !restricted;
|
|
|
|
size_t n_txes = m_core.get_pool_transactions_count(allow_sensitive);
|
|
if (n_txes > 0)
|
|
{
|
|
CHECK_PAYMENT_SAME_TS(req, res, n_txes * COST_PER_POOL_HASH);
|
|
m_core.get_pool_transaction_hashes(res.tx_hashes, allow_sensitive);
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_transaction_pool_hashes(const COMMAND_RPC_GET_TRANSACTION_POOL_HASHES::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_HASHES::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_transaction_pool_hashes);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_TRANSACTION_POOL_HASHES>(invoke_http_mode::JON, "/get_transaction_pool_hashes", req, res, r))
|
|
return r;
|
|
|
|
CHECK_PAYMENT(req, res, 1);
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
const bool request_has_rpc_origin = ctx != NULL;
|
|
const bool allow_sensitive = !request_has_rpc_origin || !restricted;
|
|
|
|
size_t n_txes = m_core.get_pool_transactions_count(allow_sensitive);
|
|
if (n_txes > 0)
|
|
{
|
|
CHECK_PAYMENT_SAME_TS(req, res, n_txes * COST_PER_POOL_HASH);
|
|
std::vector<crypto::hash> tx_hashes;
|
|
m_core.get_pool_transaction_hashes(tx_hashes, allow_sensitive);
|
|
res.tx_hashes.reserve(tx_hashes.size());
|
|
for (const crypto::hash &tx_hash: tx_hashes)
|
|
res.tx_hashes.push_back(epee::string_tools::pod_to_hex(tx_hash));
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_transaction_pool_stats(const COMMAND_RPC_GET_TRANSACTION_POOL_STATS::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_STATS::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_transaction_pool_stats);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_TRANSACTION_POOL_STATS>(invoke_http_mode::JON, "/get_transaction_pool_stats", req, res, r))
|
|
return r;
|
|
|
|
CHECK_PAYMENT_MIN1(req, res, COST_PER_TX_POOL_STATS, false);
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
const bool request_has_rpc_origin = ctx != NULL;
|
|
m_core.get_pool_transaction_stats(res.pool_stats, !request_has_rpc_origin || !restricted);
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_set_bootstrap_daemon(const COMMAND_RPC_SET_BOOTSTRAP_DAEMON::request& req, COMMAND_RPC_SET_BOOTSTRAP_DAEMON::response& res, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_set_bootstrap_daemon);
|
|
|
|
boost::optional<epee::net_utils::http::login> credentials;
|
|
if (!req.username.empty() || !req.password.empty())
|
|
{
|
|
credentials = epee::net_utils::http::login(req.username, req.password);
|
|
}
|
|
|
|
if (set_bootstrap_daemon(req.address, credentials, req.proxy))
|
|
{
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
}
|
|
else
|
|
{
|
|
res.status = "Failed to set bootstrap daemon";
|
|
}
|
|
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_stop_daemon(const COMMAND_RPC_STOP_DAEMON::request& req, COMMAND_RPC_STOP_DAEMON::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(stop_daemon);
|
|
// FIXME: replace back to original m_p2p.send_stop_signal() after
|
|
// investigating why that isn't working quite right.
|
|
m_p2p.send_stop_signal();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_getblockcount(const COMMAND_RPC_GETBLOCKCOUNT::request& req, COMMAND_RPC_GETBLOCKCOUNT::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(getblockcount);
|
|
{
|
|
boost::shared_lock<boost::shared_mutex> lock(m_bootstrap_daemon_mutex);
|
|
if (m_should_use_bootstrap_daemon)
|
|
{
|
|
res.status = "This command is unsupported for bootstrap daemon";
|
|
return true;
|
|
}
|
|
}
|
|
res.count = m_core.get_current_blockchain_height();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_getblockhash(const COMMAND_RPC_GETBLOCKHASH::request& req, COMMAND_RPC_GETBLOCKHASH::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(getblockhash);
|
|
{
|
|
boost::shared_lock<boost::shared_mutex> lock(m_bootstrap_daemon_mutex);
|
|
if (m_should_use_bootstrap_daemon)
|
|
{
|
|
res = "This command is unsupported for bootstrap daemon";
|
|
return true;
|
|
}
|
|
}
|
|
if(req.size() != 1)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Wrong parameters, expected height";
|
|
return false;
|
|
}
|
|
uint64_t h = req[0];
|
|
if(m_core.get_current_blockchain_height() <= h)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT;
|
|
error_resp.message = std::string("Requested block height: ") + std::to_string(h) + " greater than current top block height: " + std::to_string(m_core.get_current_blockchain_height() - 1);
|
|
}
|
|
res = string_tools::pod_to_hex(m_core.get_block_id_by_height(h));
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
// equivalent of strstr, but with arbitrary bytes (ie, NULs)
|
|
// This does not differentiate between "not found" and "found at offset 0"
|
|
size_t slow_memmem(const void* start_buff, size_t buflen,const void* pat,size_t patlen)
|
|
{
|
|
const void* buf = start_buff;
|
|
const void* end=(const char*)buf+buflen;
|
|
if (patlen > buflen || patlen == 0) return 0;
|
|
while(buflen>0 && (buf=memchr(buf,((const char*)pat)[0],buflen-patlen+1)))
|
|
{
|
|
if(memcmp(buf,pat,patlen)==0)
|
|
return (const char*)buf - (const char*)start_buff;
|
|
buf=(const char*)buf+1;
|
|
buflen = (const char*)end - (const char*)buf;
|
|
}
|
|
return 0;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::get_block_template(const account_public_address &address, const crypto::public_key &game_account_key, 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)
|
|
{
|
|
b = boost::value_initialized<cryptonote::block>();
|
|
if(!m_core.get_block_template(b, prev_block, address, game_account_key, difficulty, height, expected_reward, extra_nonce, seed_height, seed_hash))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: failed to create block template";
|
|
LOG_ERROR("Failed to create block template");
|
|
return false;
|
|
}
|
|
blobdata block_blob = t_serializable_object_to_blob(b);
|
|
crypto::public_key tx_pub_key = cryptonote::get_tx_pub_key_from_extra(b.miner_tx);
|
|
if(tx_pub_key == crypto::null_pkey)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: failed to create block template";
|
|
LOG_ERROR("Failed to get tx pub key in coinbase extra");
|
|
return false;
|
|
}
|
|
|
|
uint64_t next_height;
|
|
crypto::rx_seedheights(height, &seed_height, &next_height);
|
|
if (next_height != seed_height)
|
|
next_seed_hash = m_core.get_block_id_by_height(next_height);
|
|
else
|
|
next_seed_hash = seed_hash;
|
|
|
|
if (extra_nonce.empty())
|
|
{
|
|
reserved_offset = 0;
|
|
return true;
|
|
}
|
|
|
|
reserved_offset = slow_memmem((void*)block_blob.data(), block_blob.size(), &tx_pub_key, sizeof(tx_pub_key));
|
|
if(!reserved_offset)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: failed to create block template";
|
|
LOG_ERROR("Failed to find tx pub key in blockblob");
|
|
return false;
|
|
}
|
|
reserved_offset += sizeof(tx_pub_key) + 2; //2 bytes: tag for TX_EXTRA_NONCE(1 byte), counter in TX_EXTRA_NONCE(1 byte)
|
|
if(reserved_offset + extra_nonce.size() > block_blob.size())
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: failed to create block template";
|
|
LOG_ERROR("Failed to calculate offset for ");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_getblocktemplate(const COMMAND_RPC_GETBLOCKTEMPLATE::request& req, COMMAND_RPC_GETBLOCKTEMPLATE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(getblocktemplate);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GETBLOCKTEMPLATE>(invoke_http_mode::JON_RPC, "getblocktemplate", req, res, r))
|
|
return r;
|
|
|
|
if(!check_core_ready())
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_CORE_BUSY;
|
|
error_resp.message = "Core is busy";
|
|
return false;
|
|
}
|
|
|
|
if(req.reserve_size > 255)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_RESERVE_SIZE;
|
|
error_resp.message = "Too big reserved size, maximum 255";
|
|
return false;
|
|
}
|
|
|
|
if(req.reserve_size && !req.extra_nonce.empty())
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Cannot specify both a reserve_size and an extra_nonce";
|
|
return false;
|
|
}
|
|
|
|
if(req.extra_nonce.size() > 510)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_RESERVE_SIZE;
|
|
error_resp.message = "Too big extra_nonce size, maximum 510 hex chars";
|
|
return false;
|
|
}
|
|
|
|
cryptonote::address_parse_info info;
|
|
|
|
if(!req.wallet_address.size() || !cryptonote::get_account_address_from_str(info, nettype(), req.wallet_address))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_WALLET_ADDRESS;
|
|
error_resp.message = "Failed to parse wallet address";
|
|
return false;
|
|
}
|
|
if (info.is_subaddress)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_MINING_TO_SUBADDRESS;
|
|
error_resp.message = "Mining to subaddress is not supported yet";
|
|
return false;
|
|
}
|
|
|
|
block b;
|
|
cryptonote::blobdata blob_reserve;
|
|
size_t reserved_offset;
|
|
if(!req.extra_nonce.empty())
|
|
{
|
|
if(!string_tools::parse_hexstr_to_binbuff(req.extra_nonce, blob_reserve))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Parameter extra_nonce should be a hex string";
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
blob_reserve.resize(req.reserve_size, 0);
|
|
cryptonote::difficulty_type wdiff;
|
|
crypto::hash prev_block;
|
|
if (!req.prev_block.empty())
|
|
{
|
|
if (!epee::string_tools::hex_to_pod(req.prev_block, prev_block))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Invalid prev_block";
|
|
return false;
|
|
}
|
|
}
|
|
crypto::public_key game_account_key;
|
|
if (!req.game_account_key.empty())
|
|
{
|
|
if (!epee::string_tools::hex_to_pod(req.game_account_key, game_account_key))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Invalid game account key";
|
|
return false;
|
|
}
|
|
}
|
|
crypto::hash seed_hash, next_seed_hash;
|
|
if (!get_block_template(info.address, game_account_key, req.prev_block.empty() ? NULL : &prev_block, blob_reserve, reserved_offset, wdiff, res.height, res.expected_reward, b, res.seed_height, seed_hash, next_seed_hash, error_resp))
|
|
return false;
|
|
if (b.major_version >= RX_BLOCK_VERSION)
|
|
{
|
|
res.seed_hash = string_tools::pod_to_hex(seed_hash);
|
|
if (seed_hash != next_seed_hash)
|
|
res.next_seed_hash = string_tools::pod_to_hex(next_seed_hash);
|
|
}
|
|
|
|
res.reserved_offset = reserved_offset;
|
|
store_difficulty(wdiff, res.difficulty, res.wide_difficulty, res.difficulty_top64);
|
|
blobdata block_blob = t_serializable_object_to_blob(b);
|
|
blobdata hashing_blob = get_block_hashing_blob(b);
|
|
res.prev_hash = string_tools::pod_to_hex(b.prev_id);
|
|
res.blocktemplate_blob = string_tools::buff_to_hex_nodelimer(block_blob);
|
|
res.blockhashing_blob = string_tools::buff_to_hex_nodelimer(hashing_blob);
|
|
res.block_header = string_tools::buff_to_hex_nodelimer(cryptonote::t_serializable_object_to_blob(static_cast<block_header&>(b)));
|
|
res.auxpow_hash = string_tools::pod_to_hex(cryptonote::get_aux_block_hash(b));
|
|
res.chains.push_back({epee::string_tools::pod_to_hex(m_core.get_block_id_by_height(0)), res.difficulty, res.wide_difficulty, res.difficulty_top64, res.expected_reward, res.height});
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_add_aux_pow(const COMMAND_RPC_ADD_AUX_POW::request& req, COMMAND_RPC_ADD_AUX_POW::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(add_aux_pow);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_ADD_AUX_POW>(invoke_http_mode::JON_RPC, "add_aux_pow", req, res, r))
|
|
return r;
|
|
|
|
if (req.aux_pow.empty())
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Empty aux pow hash vector";
|
|
return false;
|
|
}
|
|
|
|
crypto::hash merkle_root;
|
|
size_t merkle_tree_depth = 0;
|
|
std::vector<std::pair<crypto::hash, crypto::hash>> aux_pow;
|
|
std::vector<crypto::hash> aux_pow_raw;
|
|
aux_pow.reserve(req.aux_pow.size());
|
|
aux_pow_raw.reserve(req.aux_pow.size());
|
|
for (const auto &s: req.aux_pow)
|
|
{
|
|
aux_pow.push_back({});
|
|
if (!epee::string_tools::hex_to_pod(s.id, aux_pow.back().first))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Invalid aux pow id";
|
|
return false;
|
|
}
|
|
if (!epee::string_tools::hex_to_pod(s.hash, aux_pow.back().second))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Invalid aux pow hash";
|
|
return false;
|
|
}
|
|
aux_pow_raw.push_back(aux_pow.back().second);
|
|
}
|
|
|
|
size_t path_domain = 1;
|
|
while ((1u << path_domain) < aux_pow.size())
|
|
++path_domain;
|
|
uint32_t nonce;
|
|
const uint32_t max_nonce = 65535;
|
|
bool collision = true;
|
|
for (nonce = 0; nonce <= max_nonce; ++nonce)
|
|
{
|
|
std::vector<bool> slots(aux_pow.size(), false);
|
|
collision = false;
|
|
for (size_t idx = 0; idx < aux_pow.size(); ++idx)
|
|
{
|
|
const uint32_t slot = cryptonote::get_aux_slot(aux_pow[idx].first, nonce, aux_pow.size());
|
|
if (slot >= aux_pow.size())
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Computed slot is out of range";
|
|
return false;
|
|
}
|
|
if (slots[slot])
|
|
{
|
|
collision = true;
|
|
break;
|
|
}
|
|
slots[slot] = true;
|
|
}
|
|
if (!collision)
|
|
break;
|
|
}
|
|
if (collision)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to find a suitable nonce";
|
|
return false;
|
|
}
|
|
|
|
crypto::tree_hash((const char(*)[crypto::HASH_SIZE])aux_pow_raw.data(), aux_pow_raw.size(), merkle_root.data);
|
|
res.merkle_root = epee::string_tools::pod_to_hex(merkle_root);
|
|
res.merkle_tree_depth = cryptonote::encode_mm_depth(aux_pow.size(), nonce);
|
|
|
|
blobdata blocktemplate_blob;
|
|
if (!epee::string_tools::parse_hexstr_to_binbuff(req.blocktemplate_blob, blocktemplate_blob))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Invalid blocktemplate_blob";
|
|
return false;
|
|
}
|
|
|
|
block b;
|
|
if (!parse_and_validate_block_from_blob(blocktemplate_blob, b))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB;
|
|
error_resp.message = "Wrong blocktemplate_blob";
|
|
return false;
|
|
}
|
|
|
|
if (!remove_field_from_tx_extra(b.miner_tx.extra, typeid(cryptonote::tx_extra_merge_mining_tag)))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error removing existing merkle root";
|
|
return false;
|
|
}
|
|
if (!add_mm_merkle_root_to_tx_extra(b.miner_tx.extra, merkle_root, merkle_tree_depth))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error adding merkle root";
|
|
return false;
|
|
}
|
|
b.invalidate_hashes();
|
|
b.miner_tx.invalidate_hashes();
|
|
|
|
const blobdata block_blob = t_serializable_object_to_blob(b);
|
|
const blobdata hashing_blob = get_block_hashing_blob(b);
|
|
|
|
res.blocktemplate_blob = string_tools::buff_to_hex_nodelimer(block_blob);
|
|
res.blockhashing_blob = string_tools::buff_to_hex_nodelimer(hashing_blob);
|
|
res.aux_pow = req.aux_pow;
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_submitblock(const COMMAND_RPC_SUBMITBLOCK::request& req, COMMAND_RPC_SUBMITBLOCK::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(submitblock);
|
|
{
|
|
boost::shared_lock<boost::shared_mutex> lock(m_bootstrap_daemon_mutex);
|
|
if (m_should_use_bootstrap_daemon)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_UNSUPPORTED_BOOTSTRAP;
|
|
error_resp.message = "This command is unsupported for bootstrap daemon";
|
|
return false;
|
|
}
|
|
}
|
|
CHECK_CORE_READY();
|
|
if(req.size()!=1)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Wrong param";
|
|
return false;
|
|
}
|
|
blobdata blockblob;
|
|
if(!string_tools::parse_hexstr_to_binbuff(req[0], blockblob))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB;
|
|
error_resp.message = "Wrong block blob";
|
|
return false;
|
|
}
|
|
|
|
// Fixing of high orphan issue for most pools
|
|
// Thanks Boolberry!
|
|
block b;
|
|
if(!parse_and_validate_block_from_blob(blockblob, b))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB;
|
|
error_resp.message = "Wrong block";
|
|
return false;
|
|
}
|
|
|
|
// Fix from Boolberry neglects to check block
|
|
// size, do that with the function below
|
|
if(!m_core.check_incoming_block_size(blockblob))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB_SIZE;
|
|
error_resp.message = "Block bloc size is too big, rejecting block";
|
|
return false;
|
|
}
|
|
|
|
block_verification_context bvc;
|
|
if(!m_core.handle_block_found(b, bvc))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_BLOCK_NOT_ACCEPTED;
|
|
error_resp.message = "Block not accepted";
|
|
return false;
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_generateblocks(const COMMAND_RPC_GENERATEBLOCKS::request& req, COMMAND_RPC_GENERATEBLOCKS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(generateblocks);
|
|
|
|
CHECK_CORE_READY();
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
|
|
/*
|
|
if(m_core.get_nettype() != FAKECHAIN)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_REGTEST_REQUIRED;
|
|
error_resp.message = "Regtest required when generating blocks";
|
|
return false;
|
|
}
|
|
*/
|
|
|
|
COMMAND_RPC_GETBLOCKTEMPLATE::request template_req;
|
|
COMMAND_RPC_GETBLOCKTEMPLATE::response template_res;
|
|
COMMAND_RPC_SUBMITBLOCK::request submit_req;
|
|
COMMAND_RPC_SUBMITBLOCK::response submit_res;
|
|
|
|
template_req.reserve_size = 1;
|
|
template_req.wallet_address = req.wallet_address;
|
|
template_req.prev_block = req.prev_block;
|
|
submit_req.push_back(std::string{});
|
|
res.height = m_core.get_blockchain_storage().get_current_blockchain_height();
|
|
|
|
for(size_t i = 0; i < req.amount_of_blocks; i++)
|
|
{
|
|
bool r = on_getblocktemplate(template_req, template_res, error_resp, ctx);
|
|
res.status = template_res.status;
|
|
template_req.prev_block.clear();
|
|
|
|
if (!r) return false;
|
|
|
|
blobdata blockblob;
|
|
if(!string_tools::parse_hexstr_to_binbuff(template_res.blocktemplate_blob, blockblob))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB;
|
|
error_resp.message = "Wrong block blob";
|
|
return false;
|
|
}
|
|
block b;
|
|
if(!parse_and_validate_block_from_blob(blockblob, b))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB;
|
|
error_resp.message = "Wrong block blob";
|
|
return false;
|
|
}
|
|
b.nonce = req.starting_nonce;
|
|
crypto::hash seed_hash = crypto::null_hash;
|
|
if (b.major_version >= RX_BLOCK_VERSION && !epee::string_tools::hex_to_pod(template_res.seed_hash, seed_hash))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error converting seed hash";
|
|
return false;
|
|
}
|
|
miner::find_nonce_for_given_block([this](const cryptonote::block &b, uint64_t height, const crypto::hash *seed_hash, unsigned int threads, crypto::hash &hash) {
|
|
return cryptonote::get_block_longhash(&(m_core.get_blockchain_storage()), b, hash, height, seed_hash, threads);
|
|
}, b, template_res.difficulty, template_res.height, &seed_hash);
|
|
|
|
submit_req.front() = string_tools::buff_to_hex_nodelimer(block_to_blob(b));
|
|
r = on_submitblock(submit_req, submit_res, error_resp, ctx);
|
|
res.status = submit_res.status;
|
|
|
|
if (!r) return false;
|
|
|
|
res.blocks.push_back(epee::string_tools::pod_to_hex(get_block_hash(b)));
|
|
template_req.prev_block = res.blocks.back();
|
|
res.height = template_res.height;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
uint64_t core_rpc_server::get_block_reward(const block& blk)
|
|
{
|
|
uint64_t reward = 0;
|
|
for(const tx_out& out: blk.miner_tx.vout)
|
|
{
|
|
reward += out.amount;
|
|
}
|
|
return reward;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::fill_block_header_response(const block& blk, bool orphan_status, uint64_t height, const crypto::hash& hash, block_header_response& response, bool fill_pow_hash)
|
|
{
|
|
PERF_TIMER(fill_block_header_response);
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
response.major_version = blk.major_version;
|
|
response.minor_version = blk.minor_version;
|
|
response.timestamp = blk.timestamp;
|
|
response.prev_hash = string_tools::pod_to_hex(blk.prev_id);
|
|
response.nonce = blk.nonce;
|
|
response.orphan_status = orphan_status;
|
|
response.height = height;
|
|
response.depth = m_core.get_current_blockchain_height() - height - 1;
|
|
response.hash = string_tools::pod_to_hex(hash);
|
|
response.reward = get_block_reward(blk);
|
|
if (!orphan_status)
|
|
{
|
|
store_difficulty(m_core.get_blockchain_storage().block_difficulty(height),
|
|
response.difficulty, response.wide_difficulty, response.difficulty_top64);
|
|
store_difficulty(db.get_block_cumulative_difficulty(height),
|
|
response.cumulative_difficulty, response.wide_cumulative_difficulty, response.cumulative_difficulty_top64);
|
|
const cryptonote::blobdata blob = db.get_block_blob_from_height(height);
|
|
response.block_size = blob.size();
|
|
response.block_weight = db.get_block_weight(height);
|
|
}
|
|
else
|
|
{
|
|
store_difficulty(0, response.difficulty, response.wide_difficulty, response.difficulty_top64);
|
|
store_difficulty(0, response.cumulative_difficulty, response.wide_cumulative_difficulty, response.cumulative_difficulty_top64);
|
|
response.block_size = 0;
|
|
response.block_weight = 0;
|
|
}
|
|
response.num_txes = blk.tx_hashes.size();
|
|
response.pow_hash = fill_pow_hash ? string_tools::pod_to_hex(get_block_longhash(&(m_core.get_blockchain_storage()), blk, height, 0)) : "";
|
|
response.long_term_weight = orphan_status ? 0 : db.get_block_long_term_weight(height);
|
|
response.miner_tx_hash = string_tools::pod_to_hex(cryptonote::get_transaction_hash(blk.miner_tx));
|
|
response.merged_mined = !!blk.aux_header;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
template <typename COMMAND_TYPE>
|
|
bool core_rpc_server::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)
|
|
{
|
|
res.untrusted = false;
|
|
|
|
boost::upgrade_lock<boost::shared_mutex> upgrade_lock(m_bootstrap_daemon_mutex);
|
|
|
|
if (m_bootstrap_daemon.get() == nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!m_should_use_bootstrap_daemon)
|
|
{
|
|
MINFO("The local daemon is fully synced. Not switching back to the bootstrap daemon");
|
|
return false;
|
|
}
|
|
|
|
auto current_time = std::chrono::system_clock::now();
|
|
if (current_time - m_bootstrap_height_check_time > std::chrono::seconds(30)) // update every 30s
|
|
{
|
|
{
|
|
boost::upgrade_to_unique_lock<boost::shared_mutex> lock(upgrade_lock);
|
|
m_bootstrap_height_check_time = current_time;
|
|
}
|
|
|
|
boost::optional<std::pair<uint64_t, uint64_t>> bootstrap_daemon_height_info = m_bootstrap_daemon->get_height();
|
|
if (!bootstrap_daemon_height_info)
|
|
{
|
|
MERROR("Failed to fetch bootstrap daemon height");
|
|
return false;
|
|
}
|
|
|
|
const uint64_t bootstrap_daemon_height = bootstrap_daemon_height_info->first;
|
|
const uint64_t bootstrap_daemon_target_height = bootstrap_daemon_height_info->second;
|
|
if (bootstrap_daemon_height < bootstrap_daemon_target_height)
|
|
{
|
|
MINFO("Bootstrap daemon is out of sync");
|
|
return m_bootstrap_daemon->handle_result(false, {});
|
|
}
|
|
|
|
if (!m_p2p.get_payload_object().no_sync())
|
|
{
|
|
uint64_t top_height = m_core.get_current_blockchain_height();
|
|
m_should_use_bootstrap_daemon = top_height + 10 < bootstrap_daemon_height;
|
|
MINFO((m_should_use_bootstrap_daemon ? "Using" : "Not using") << " the bootstrap daemon (our height: " << top_height << ", bootstrap daemon's height: " << bootstrap_daemon_height << ")");
|
|
|
|
if (!m_should_use_bootstrap_daemon)
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (mode == invoke_http_mode::JON)
|
|
{
|
|
r = m_bootstrap_daemon->invoke_http_json(command_name, req, res);
|
|
}
|
|
else if (mode == invoke_http_mode::BIN)
|
|
{
|
|
r = m_bootstrap_daemon->invoke_http_bin(command_name, req, res);
|
|
}
|
|
else if (mode == invoke_http_mode::JON_RPC)
|
|
{
|
|
r = m_bootstrap_daemon->invoke_http_json_rpc(command_name, req, res);
|
|
}
|
|
else
|
|
{
|
|
MERROR("Unknown invoke_http_mode: " << mode);
|
|
return false;
|
|
}
|
|
|
|
{
|
|
boost::upgrade_to_unique_lock<boost::shared_mutex> lock(upgrade_lock);
|
|
m_was_bootstrap_ever_used = true;
|
|
}
|
|
|
|
if (r && res.status != CORE_RPC_STATUS_PAYMENT_REQUIRED && res.status != CORE_RPC_STATUS_OK)
|
|
{
|
|
MINFO("Failing RPC " << command_name << " due to peer return status " << res.status);
|
|
r = false;
|
|
}
|
|
res.untrusted = true;
|
|
return r;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_last_block_header(const COMMAND_RPC_GET_LAST_BLOCK_HEADER::request& req, COMMAND_RPC_GET_LAST_BLOCK_HEADER::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_last_block_header);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_LAST_BLOCK_HEADER>(invoke_http_mode::JON_RPC, "getlastblockheader", req, res, r))
|
|
return r;
|
|
|
|
CHECK_CORE_READY();
|
|
CHECK_PAYMENT_MIN1(req, res, COST_PER_BLOCK_HEADER, false);
|
|
uint64_t last_block_height;
|
|
crypto::hash last_block_hash;
|
|
m_core.get_blockchain_top(last_block_height, last_block_hash);
|
|
block last_block;
|
|
bool have_last_block = m_core.get_block_by_hash(last_block_hash, last_block);
|
|
if (!have_last_block)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: can't get last block.";
|
|
return false;
|
|
}
|
|
const bool restricted = m_restricted && ctx;
|
|
bool response_filled = fill_block_header_response(last_block, false, last_block_height, last_block_hash, res.block_header, req.fill_pow_hash && !restricted);
|
|
if (!response_filled)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: can't produce valid response.";
|
|
return false;
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_block_header_by_hash(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_block_header_by_hash);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH>(invoke_http_mode::JON_RPC, "getblockheaderbyhash", req, res, r))
|
|
return r;
|
|
|
|
CHECK_PAYMENT_MIN1(req, res, COST_PER_BLOCK_HEADER, false);
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
if (restricted && req.hashes.size() > RESTRICTED_BLOCK_COUNT)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_RESTRICTED;
|
|
error_resp.message = "Too many block headers requested in restricted mode";
|
|
return false;
|
|
}
|
|
|
|
auto get = [this](const std::string &hash, bool fill_pow_hash, block_header_response &block_header, bool restricted, epee::json_rpc::error& error_resp) -> bool {
|
|
crypto::hash block_hash;
|
|
bool hash_parsed = parse_hash256(hash, block_hash);
|
|
if(!hash_parsed)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Failed to parse hex representation of block hash. Hex = " + hash + '.';
|
|
return false;
|
|
}
|
|
block blk;
|
|
bool orphan = false;
|
|
bool have_block = m_core.get_block_by_hash(block_hash, blk, &orphan);
|
|
if (!have_block)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: can't get block by hash. Hash = " + hash + '.';
|
|
return false;
|
|
}
|
|
if (blk.miner_tx.vin.size() != 1 || blk.miner_tx.vin.front().type() != typeid(txin_gen))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: coinbase transaction in the block has the wrong type";
|
|
return false;
|
|
}
|
|
uint64_t block_height = boost::get<txin_gen>(blk.miner_tx.vin.front()).height;
|
|
bool response_filled = fill_block_header_response(blk, orphan, block_height, block_hash, block_header, fill_pow_hash && !restricted);
|
|
if (!response_filled)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: can't produce valid response.";
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
if (!req.genesis.empty())
|
|
{
|
|
crypto::hash genesis;
|
|
if (!epee::string_tools::hex_to_pod(req.genesis, genesis))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Invalid genesis hash";
|
|
return false;
|
|
}
|
|
if (m_core.get_block_id_by_height(0) != genesis)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Genesis hash does not match";
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!req.hash.empty())
|
|
{
|
|
if (!get(req.hash, req.fill_pow_hash, res.block_header, restricted, error_resp))
|
|
return false;
|
|
}
|
|
res.block_headers.reserve(req.hashes.size());
|
|
for (const std::string &hash: req.hashes)
|
|
{
|
|
res.block_headers.push_back({});
|
|
if (!get(hash, req.fill_pow_hash, res.block_headers.back(), restricted, error_resp))
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_block_headers_range(const COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::request& req, COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_block_headers_range);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_BLOCK_HEADERS_RANGE>(invoke_http_mode::JON_RPC, "getblockheadersrange", req, res, r))
|
|
return r;
|
|
|
|
const uint64_t bc_height = m_core.get_current_blockchain_height();
|
|
if (req.start_height >= bc_height || req.end_height >= bc_height || req.start_height > req.end_height)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT;
|
|
error_resp.message = "Invalid start/end heights.";
|
|
return false;
|
|
}
|
|
const bool restricted = m_restricted && ctx;
|
|
if (restricted && req.end_height - req.start_height > RESTRICTED_BLOCK_HEADER_RANGE)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_RESTRICTED;
|
|
error_resp.message = "Too many block headers requested.";
|
|
return false;
|
|
}
|
|
|
|
CHECK_PAYMENT_MIN1(req, res, (req.end_height - req.start_height + 1) * COST_PER_BLOCK_HEADER, false);
|
|
for (uint64_t h = req.start_height; h <= req.end_height; ++h)
|
|
{
|
|
crypto::hash block_hash = m_core.get_block_id_by_height(h);
|
|
block blk;
|
|
bool have_block = m_core.get_block_by_hash(block_hash, blk);
|
|
if (!have_block)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: can't get block by height. Height = " + boost::lexical_cast<std::string>(h) + ". Hash = " + epee::string_tools::pod_to_hex(block_hash) + '.';
|
|
return false;
|
|
}
|
|
if (blk.miner_tx.vin.size() != 1 || blk.miner_tx.vin.front().type() != typeid(txin_gen))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: coinbase transaction in the block has the wrong type";
|
|
return false;
|
|
}
|
|
uint64_t block_height = boost::get<txin_gen>(blk.miner_tx.vin.front()).height;
|
|
if (block_height != h)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: coinbase transaction in the block has the wrong height";
|
|
return false;
|
|
}
|
|
res.headers.push_back(block_header_response());
|
|
bool response_filled = fill_block_header_response(blk, false, block_height, block_hash, res.headers.back(), req.fill_pow_hash && !restricted);
|
|
if (!response_filled)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: can't produce valid response.";
|
|
return false;
|
|
}
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_block_header_by_height(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_block_header_by_height);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT>(invoke_http_mode::JON_RPC, "getblockheaderbyheight", req, res, r))
|
|
return r;
|
|
|
|
if(m_core.get_current_blockchain_height() <= req.height)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT;
|
|
error_resp.message = std::string("Requested block height: ") + std::to_string(req.height) + " greater than current top block height: " + std::to_string(m_core.get_current_blockchain_height() - 1);
|
|
return false;
|
|
}
|
|
CHECK_PAYMENT_MIN1(req, res, COST_PER_BLOCK_HEADER, false);
|
|
crypto::hash block_hash = m_core.get_block_id_by_height(req.height);
|
|
block blk;
|
|
bool have_block = m_core.get_block_by_hash(block_hash, blk);
|
|
if (!have_block)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: can't get block by height. Height = " + std::to_string(req.height) + '.';
|
|
return false;
|
|
}
|
|
const bool restricted = m_restricted && ctx;
|
|
bool response_filled = fill_block_header_response(blk, false, req.height, block_hash, res.block_header, req.fill_pow_hash && !restricted);
|
|
if (!response_filled)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: can't produce valid response.";
|
|
return false;
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_block(const COMMAND_RPC_GET_BLOCK::request& req, COMMAND_RPC_GET_BLOCK::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_block);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_BLOCK>(invoke_http_mode::JON_RPC, "getblock", req, res, r))
|
|
return r;
|
|
|
|
CHECK_PAYMENT_MIN1(req, res, COST_PER_BLOCK, false);
|
|
|
|
crypto::hash block_hash;
|
|
if (!req.hash.empty())
|
|
{
|
|
bool hash_parsed = parse_hash256(req.hash, block_hash);
|
|
if(!hash_parsed)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Failed to parse hex representation of block hash. Hex = " + req.hash + '.';
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(m_core.get_current_blockchain_height() <= req.height)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT;
|
|
error_resp.message = std::string("Requested block height: ") + std::to_string(req.height) + " greater than current top block height: " + std::to_string(m_core.get_current_blockchain_height() - 1);
|
|
return false;
|
|
}
|
|
block_hash = m_core.get_block_id_by_height(req.height);
|
|
}
|
|
block blk;
|
|
bool orphan = false;
|
|
bool have_block = m_core.get_block_by_hash(block_hash, blk, &orphan);
|
|
if (!have_block)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: can't get block by hash. Hash = " + req.hash + '.';
|
|
return false;
|
|
}
|
|
if (blk.miner_tx.vin.size() != 1 || blk.miner_tx.vin.front().type() != typeid(txin_gen))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: coinbase transaction in the block has the wrong type";
|
|
return false;
|
|
}
|
|
uint64_t block_height = boost::get<txin_gen>(blk.miner_tx.vin.front()).height;
|
|
const bool restricted = m_restricted && ctx;
|
|
bool response_filled = fill_block_header_response(blk, orphan, block_height, block_hash, res.block_header, req.fill_pow_hash && !restricted);
|
|
if (!response_filled)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Internal error: can't produce valid response.";
|
|
return false;
|
|
}
|
|
res.miner_tx_hash = res.block_header.miner_tx_hash;
|
|
for (size_t n = 0; n < blk.tx_hashes.size(); ++n)
|
|
{
|
|
res.tx_hashes.push_back(epee::string_tools::pod_to_hex(blk.tx_hashes[n]));
|
|
}
|
|
const cryptonote::blobdata blob = t_serializable_object_to_blob(blk);
|
|
res.bytes = blob.size();
|
|
if (req.include_blob)
|
|
res.blob = string_tools::buff_to_hex_nodelimer(blob);
|
|
if (req.include_json)
|
|
res.json = obj_to_json_str(blk);
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_connections);
|
|
|
|
res.connections = m_p2p.get_payload_object().get_connections();
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_info_json(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
if (!on_get_info(req, res, ctx) || res.status != CORE_RPC_STATUS_OK)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = res.status;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_hard_fork_info(const COMMAND_RPC_HARD_FORK_INFO::request& req, COMMAND_RPC_HARD_FORK_INFO::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(hard_fork_info);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_HARD_FORK_INFO>(invoke_http_mode::JON_RPC, "hard_fork_info", req, res, r))
|
|
return r;
|
|
|
|
CHECK_PAYMENT(req, res, COST_PER_HARD_FORK_INFO);
|
|
const Blockchain &blockchain = m_core.get_blockchain_storage();
|
|
uint8_t version = req.version > 0 ? req.version : blockchain.get_next_hard_fork_version();
|
|
res.version = blockchain.get_current_hard_fork_version();
|
|
res.enabled = blockchain.get_hard_fork_voting_info(version, res.window, res.votes, res.threshold, res.earliest_height, res.voting);
|
|
res.state = blockchain.get_hard_fork_state();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_bans(const COMMAND_RPC_GETBANS::request& req, COMMAND_RPC_GETBANS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_bans);
|
|
|
|
auto now = time(nullptr);
|
|
std::map<std::string, time_t> blocked_hosts = m_p2p.get_blocked_hosts();
|
|
for (std::map<std::string, time_t>::const_iterator i = blocked_hosts.begin(); i != blocked_hosts.end(); ++i)
|
|
{
|
|
if (i->second > now) {
|
|
COMMAND_RPC_GETBANS::ban b;
|
|
b.host = i->first;
|
|
b.ip = 0;
|
|
uint32_t ip;
|
|
if (epee::string_tools::get_ip_int32_from_string(ip, b.host))
|
|
b.ip = ip;
|
|
b.seconds = i->second - now;
|
|
res.bans.push_back(b);
|
|
}
|
|
}
|
|
std::map<epee::net_utils::ipv4_network_subnet, time_t> blocked_subnets = m_p2p.get_blocked_subnets();
|
|
for (std::map<epee::net_utils::ipv4_network_subnet, time_t>::const_iterator i = blocked_subnets.begin(); i != blocked_subnets.end(); ++i)
|
|
{
|
|
if (i->second > now) {
|
|
COMMAND_RPC_GETBANS::ban b;
|
|
b.host = i->first.host_str();
|
|
b.ip = 0;
|
|
b.seconds = i->second - now;
|
|
res.bans.push_back(b);
|
|
}
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_banned(const COMMAND_RPC_BANNED::request& req, COMMAND_RPC_BANNED::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_banned);
|
|
|
|
auto na_parsed = net::get_network_address(req.address, 0);
|
|
if (!na_parsed)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Unsupported host type";
|
|
return false;
|
|
}
|
|
epee::net_utils::network_address na = std::move(*na_parsed);
|
|
|
|
time_t seconds;
|
|
if (m_p2p.is_host_blocked(na, &seconds))
|
|
{
|
|
res.banned = true;
|
|
res.seconds = seconds;
|
|
}
|
|
else
|
|
{
|
|
res.banned = false;
|
|
res.seconds = 0;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_set_bans(const COMMAND_RPC_SETBANS::request& req, COMMAND_RPC_SETBANS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(set_bans);
|
|
|
|
for (auto i = req.bans.begin(); i != req.bans.end(); ++i)
|
|
{
|
|
epee::net_utils::network_address na;
|
|
|
|
// try subnet first
|
|
if (!i->host.empty())
|
|
{
|
|
auto ns_parsed = net::get_ipv4_subnet_address(i->host);
|
|
if (ns_parsed)
|
|
{
|
|
if (i->ban)
|
|
m_p2p.block_subnet(*ns_parsed, i->seconds);
|
|
else
|
|
m_p2p.unblock_subnet(*ns_parsed);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// then host
|
|
if (!i->host.empty())
|
|
{
|
|
auto na_parsed = net::get_network_address(i->host, 0);
|
|
if (!na_parsed)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Unsupported host/subnet type";
|
|
return false;
|
|
}
|
|
na = std::move(*na_parsed);
|
|
}
|
|
else
|
|
{
|
|
na = epee::net_utils::ipv4_network_address{i->ip, 0};
|
|
}
|
|
if (i->ban)
|
|
m_p2p.block_host(na, i->seconds);
|
|
else
|
|
m_p2p.unblock_host(na);
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_flush_txpool(const COMMAND_RPC_FLUSH_TRANSACTION_POOL::request& req, COMMAND_RPC_FLUSH_TRANSACTION_POOL::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(flush_txpool);
|
|
|
|
bool failed = false;
|
|
std::vector<crypto::hash> txids;
|
|
if (req.txids.empty())
|
|
{
|
|
std::vector<transaction> pool_txs;
|
|
bool r = m_core.get_pool_transactions(pool_txs, true);
|
|
if (!r)
|
|
{
|
|
res.status = "Failed to get txpool contents";
|
|
return true;
|
|
}
|
|
for (const auto &tx: pool_txs)
|
|
{
|
|
txids.push_back(cryptonote::get_transaction_hash(tx));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (const auto &str: req.txids)
|
|
{
|
|
crypto::hash txid;
|
|
if(!epee::string_tools::hex_to_pod(str, txid))
|
|
{
|
|
failed = true;
|
|
}
|
|
else
|
|
{
|
|
txids.push_back(txid);
|
|
}
|
|
}
|
|
}
|
|
if (!m_core.get_blockchain_storage().flush_txes_from_pool(txids))
|
|
{
|
|
res.status = "Failed to remove one or more tx(es)";
|
|
return true;
|
|
}
|
|
|
|
if (failed)
|
|
{
|
|
if (txids.empty())
|
|
res.status = "Failed to parse txid";
|
|
else
|
|
res.status = "Failed to parse some of the txids";
|
|
return true;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_output_histogram(const COMMAND_RPC_GET_OUTPUT_HISTOGRAM::request& req, COMMAND_RPC_GET_OUTPUT_HISTOGRAM::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_output_histogram);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_OUTPUT_HISTOGRAM>(invoke_http_mode::JON_RPC, "get_output_histogram", req, res, r))
|
|
return r;
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
size_t amounts = req.amounts.size();
|
|
if (restricted && amounts == 0)
|
|
{
|
|
res.status = "Restricted RPC will not serve histograms on the whole blockchain. Use your own node.";
|
|
return true;
|
|
}
|
|
|
|
uint64_t cost = req.amounts.empty() ? COST_PER_FULL_OUTPUT_HISTOGRAM : (COST_PER_OUTPUT_HISTOGRAM * amounts);
|
|
CHECK_PAYMENT_MIN1(req, res, cost, false);
|
|
|
|
if (restricted && req.recent_cutoff > 0 && req.recent_cutoff < (uint64_t)time(NULL) - OUTPUT_HISTOGRAM_RECENT_CUTOFF_RESTRICTION)
|
|
{
|
|
res.status = "Recent cutoff is too old";
|
|
return true;
|
|
}
|
|
|
|
std::map<uint64_t, std::tuple<uint64_t, uint64_t, uint64_t>> histogram;
|
|
try
|
|
{
|
|
histogram = m_core.get_blockchain_storage().get_output_histogram(req.amounts, req.unlocked, req.recent_cutoff, req.min_count);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
res.status = "Failed to get output histogram";
|
|
return true;
|
|
}
|
|
|
|
res.histogram.clear();
|
|
res.histogram.reserve(histogram.size());
|
|
for (const auto &i: histogram)
|
|
{
|
|
if (std::get<0>(i.second) >= req.min_count && (std::get<0>(i.second) <= req.max_count || req.max_count == 0))
|
|
res.histogram.push_back(COMMAND_RPC_GET_OUTPUT_HISTOGRAM::entry(i.first, std::get<0>(i.second), std::get<1>(i.second), std::get<2>(i.second)));
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_version(const COMMAND_RPC_GET_VERSION::request& req, COMMAND_RPC_GET_VERSION::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_version);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_VERSION>(invoke_http_mode::JON_RPC, "get_version", req, res, r))
|
|
return r;
|
|
|
|
res.version = CORE_RPC_VERSION;
|
|
res.release = MONERO_VERSION_IS_RELEASE;
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_coinbase_tx_sum(const COMMAND_RPC_GET_COINBASE_TX_SUM::request& req, COMMAND_RPC_GET_COINBASE_TX_SUM::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_coinbase_tx_sum);
|
|
const uint64_t bc_height = m_core.get_current_blockchain_height();
|
|
if (req.height >= bc_height || req.count > bc_height)
|
|
{
|
|
res.status = "height or count is too large";
|
|
return true;
|
|
}
|
|
CHECK_PAYMENT_MIN1(req, res, COST_PER_COINBASE_TX_SUM_BLOCK * req.count, false);
|
|
typedef boost::multiprecision::uint128_t uint128_t;
|
|
std::tuple<uint128_t, uint128_t, uint128_t> amounts = m_core.get_coinbase_tx_sum(req.height, req.count);
|
|
store_128(std::get<0>(amounts), res.emission_amount, res.wide_emission_amount, res.emission_amount_top64);
|
|
store_128(std::get<1>(amounts), res.fee_amount, res.wide_fee_amount, res.fee_amount_top64);
|
|
store_128(std::get<2>(amounts), res.game_subsidy, res.wide_game_subsidy, res.game_subsidy_top64);
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_base_fee_estimate(const COMMAND_RPC_GET_BASE_FEE_ESTIMATE::request& req, COMMAND_RPC_GET_BASE_FEE_ESTIMATE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_base_fee_estimate);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_BASE_FEE_ESTIMATE>(invoke_http_mode::JON_RPC, "get_fee_estimate", req, res, r))
|
|
return r;
|
|
|
|
CHECK_PAYMENT(req, res, COST_PER_FEE_ESTIMATE);
|
|
res.fee = m_core.get_blockchain_storage().get_dynamic_base_fee_estimate(req.grace_blocks);
|
|
res.quantization_mask = Blockchain::get_fee_quantization_mask();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_alternate_chains(const COMMAND_RPC_GET_ALTERNATE_CHAINS::request& req, COMMAND_RPC_GET_ALTERNATE_CHAINS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_alternate_chains);
|
|
try
|
|
{
|
|
std::vector<std::pair<Blockchain::block_extended_info, std::vector<crypto::hash>>> chains = m_core.get_blockchain_storage().get_alternative_chains();
|
|
for (const auto &i: chains)
|
|
{
|
|
difficulty_type wdiff = i.first.cumulative_difficulty;
|
|
res.chains.push_back(COMMAND_RPC_GET_ALTERNATE_CHAINS::chain_info{epee::string_tools::pod_to_hex(get_block_hash(i.first.bl)), i.first.height, i.second.size(), 0, "", 0, {}, std::string()});
|
|
store_difficulty(wdiff, res.chains.back().difficulty, res.chains.back().wide_difficulty, res.chains.back().difficulty_top64);
|
|
res.chains.back().block_hashes.reserve(i.second.size());
|
|
for (const crypto::hash &block_id: i.second)
|
|
res.chains.back().block_hashes.push_back(epee::string_tools::pod_to_hex(block_id));
|
|
if (i.first.height < i.second.size())
|
|
{
|
|
res.status = "Error finding alternate chain attachment point";
|
|
return true;
|
|
}
|
|
cryptonote::block main_chain_parent_block;
|
|
try { main_chain_parent_block = m_core.get_blockchain_storage().get_db().get_block_from_height(i.first.height - i.second.size()); }
|
|
catch (const std::exception &e) { res.status = "Error finding alternate chain attachment point"; return true; }
|
|
res.chains.back().main_chain_parent_block = epee::string_tools::pod_to_hex(get_block_hash(main_chain_parent_block));
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
}
|
|
catch (...)
|
|
{
|
|
res.status = "Error retrieving alternate chains";
|
|
}
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_limit(const COMMAND_RPC_GET_LIMIT::request& req, COMMAND_RPC_GET_LIMIT::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_limit);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_LIMIT>(invoke_http_mode::JON, "/get_limit", req, res, r))
|
|
return r;
|
|
|
|
res.limit_down = epee::net_utils::connection_basic::get_rate_down_limit();
|
|
res.limit_up = epee::net_utils::connection_basic::get_rate_up_limit();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_set_limit(const COMMAND_RPC_SET_LIMIT::request& req, COMMAND_RPC_SET_LIMIT::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(set_limit);
|
|
// -1 = reset to default
|
|
// 0 = do not modify
|
|
|
|
if (req.limit_down > 0)
|
|
{
|
|
epee::net_utils::connection_basic::set_rate_down_limit(req.limit_down);
|
|
}
|
|
else if (req.limit_down < 0)
|
|
{
|
|
if (req.limit_down != -1)
|
|
{
|
|
res.status = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
return true;
|
|
}
|
|
epee::net_utils::connection_basic::set_rate_down_limit(nodetool::default_limit_down);
|
|
}
|
|
|
|
if (req.limit_up > 0)
|
|
{
|
|
epee::net_utils::connection_basic::set_rate_up_limit(req.limit_up);
|
|
}
|
|
else if (req.limit_up < 0)
|
|
{
|
|
if (req.limit_up != -1)
|
|
{
|
|
res.status = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
return true;
|
|
}
|
|
epee::net_utils::connection_basic::set_rate_up_limit(nodetool::default_limit_up);
|
|
}
|
|
|
|
res.limit_down = epee::net_utils::connection_basic::get_rate_down_limit();
|
|
res.limit_up = epee::net_utils::connection_basic::get_rate_up_limit();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_out_peers(const COMMAND_RPC_OUT_PEERS::request& req, COMMAND_RPC_OUT_PEERS::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(out_peers);
|
|
if (req.set)
|
|
m_p2p.change_max_out_public_peers(req.out_peers);
|
|
res.out_peers = m_p2p.get_max_out_public_peers();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_in_peers(const COMMAND_RPC_IN_PEERS::request& req, COMMAND_RPC_IN_PEERS::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(in_peers);
|
|
if (req.set)
|
|
m_p2p.change_max_in_public_peers(req.in_peers);
|
|
res.in_peers = m_p2p.get_max_in_public_peers();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_update(const COMMAND_RPC_UPDATE::request& req, COMMAND_RPC_UPDATE::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(update);
|
|
|
|
res.update = false;
|
|
if (m_core.offline())
|
|
{
|
|
res.status = "Daemon is running offline";
|
|
return true;
|
|
}
|
|
|
|
static const char software[] = "townforge";
|
|
#ifdef BUILD_TAG
|
|
static const char buildtag[] = BOOST_PP_STRINGIZE(BUILD_TAG);
|
|
static const char subdir[] = "cli";
|
|
#else
|
|
static const char buildtag[] = "source";
|
|
static const char subdir[] = "source";
|
|
#endif
|
|
|
|
if (req.command != "check" && req.command != "download" && req.command != "update")
|
|
{
|
|
res.status = std::string("unknown command: '") + req.command + "'";
|
|
return true;
|
|
}
|
|
|
|
std::string version, hash;
|
|
if (!tools::check_updates(software, buildtag, version, hash))
|
|
{
|
|
res.status = "Error checking for updates";
|
|
return true;
|
|
}
|
|
if (tools::vercmp(version.c_str(), MONERO_VERSION) <= 0)
|
|
{
|
|
res.update = false;
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
res.update = true;
|
|
res.version = version;
|
|
res.user_uri = tools::get_update_url(software, subdir, buildtag, version, true);
|
|
res.auto_uri = tools::get_update_url(software, subdir, buildtag, version, false);
|
|
res.hash = hash;
|
|
if (req.command == "check")
|
|
{
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
|
|
boost::filesystem::path path;
|
|
if (req.path.empty())
|
|
{
|
|
std::string filename;
|
|
const char *slash = strrchr(res.auto_uri.c_str(), '/');
|
|
if (slash)
|
|
filename = slash + 1;
|
|
else
|
|
filename = std::string(software) + "-update-" + version;
|
|
path = epee::string_tools::get_current_module_folder();
|
|
path /= filename;
|
|
}
|
|
else
|
|
{
|
|
path = req.path;
|
|
}
|
|
|
|
crypto::hash file_hash;
|
|
if (!tools::sha256sum(path.string(), file_hash) || (hash != epee::string_tools::pod_to_hex(file_hash)))
|
|
{
|
|
MDEBUG("We don't have that file already, downloading");
|
|
if (!tools::download(path.string(), res.auto_uri))
|
|
{
|
|
MERROR("Failed to download " << res.auto_uri);
|
|
return true;
|
|
}
|
|
if (!tools::sha256sum(path.string(), file_hash))
|
|
{
|
|
MERROR("Failed to hash " << path);
|
|
return true;
|
|
}
|
|
if (hash != epee::string_tools::pod_to_hex(file_hash))
|
|
{
|
|
MERROR("Download from " << res.auto_uri << " does not match the expected hash");
|
|
return true;
|
|
}
|
|
MINFO("New version downloaded to " << path);
|
|
}
|
|
else
|
|
{
|
|
MDEBUG("We already have " << path << " with expected hash");
|
|
}
|
|
res.path = path.string();
|
|
|
|
if (req.command == "download")
|
|
{
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
|
|
res.status = "'update' not implemented yet";
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_pop_blocks(const COMMAND_RPC_POP_BLOCKS::request& req, COMMAND_RPC_POP_BLOCKS::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(pop_blocks);
|
|
|
|
if (!m_core.get_blockchain_storage().pop_blocks(req.nblocks))
|
|
{
|
|
res.status = "Failed to pop blocks";
|
|
return false;
|
|
}
|
|
|
|
res.height = m_core.get_current_blockchain_height();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_relay_tx(const COMMAND_RPC_RELAY_TX::request& req, COMMAND_RPC_RELAY_TX::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(relay_tx);
|
|
CHECK_PAYMENT_MIN1(req, res, req.txids.size() * COST_PER_TX_RELAY, false);
|
|
|
|
const bool restricted = m_restricted && ctx;
|
|
|
|
bool failed = false;
|
|
res.status = "";
|
|
for (const auto &str: req.txids)
|
|
{
|
|
crypto::hash txid;
|
|
if(!epee::string_tools::hex_to_pod(str, txid))
|
|
{
|
|
if (!res.status.empty()) res.status += ", ";
|
|
res.status += std::string("invalid transaction id: ") + str;
|
|
failed = true;
|
|
continue;
|
|
}
|
|
|
|
//TODO: The get_pool_transaction could have an optional meta parameter
|
|
bool broadcasted = false;
|
|
cryptonote::blobdata txblob;
|
|
if ((broadcasted = m_core.get_pool_transaction(txid, txblob, relay_category::broadcasted)) || (!restricted && m_core.get_pool_transaction(txid, txblob, relay_category::all)))
|
|
{
|
|
// The settings below always choose i2p/tor if enabled. Otherwise, do fluff iff previously relayed else dandelion++ stem.
|
|
NOTIFY_NEW_TRANSACTIONS::request r;
|
|
r.txs.push_back(std::move(txblob));
|
|
const auto tx_relay = broadcasted ? relay_method::fluff : relay_method::local;
|
|
m_core.get_protocol()->relay_transactions(r, boost::uuids::nil_uuid(), epee::net_utils::zone::invalid, tx_relay);
|
|
//TODO: make sure that tx has reached other nodes here, probably wait to receive reflections from other nodes
|
|
}
|
|
else
|
|
{
|
|
if (!res.status.empty()) res.status += ", ";
|
|
res.status += std::string("transaction not found in pool: ") + str;
|
|
failed = true;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (failed)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = res.status;
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_sync_info(const COMMAND_RPC_SYNC_INFO::request& req, COMMAND_RPC_SYNC_INFO::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(sync_info);
|
|
CHECK_PAYMENT(req, res, COST_PER_SYNC_INFO);
|
|
|
|
crypto::hash top_hash;
|
|
m_core.get_blockchain_top(res.height, top_hash);
|
|
++res.height; // turn top block height into blockchain height
|
|
res.target_height = m_p2p.get_payload_object().is_synchronized() ? 0 : m_core.get_target_blockchain_height();
|
|
res.next_needed_pruning_seed = m_p2p.get_payload_object().get_next_needed_pruning_stripe().second;
|
|
|
|
for (const auto &c: m_p2p.get_payload_object().get_connections())
|
|
res.peers.push_back({c});
|
|
const cryptonote::block_queue &block_queue = m_p2p.get_payload_object().get_block_queue();
|
|
block_queue.foreach([&](const cryptonote::block_queue::span &span) {
|
|
const std::string span_connection_id = epee::string_tools::pod_to_hex(span.connection_id);
|
|
uint32_t speed = (uint32_t)(100.0f * block_queue.get_speed(span.connection_id) + 0.5f);
|
|
res.spans.push_back({span.start_block_height, span.nblocks, span_connection_id, (uint32_t)(span.rate + 0.5f), speed, span.size, span.origin.str()});
|
|
return true;
|
|
});
|
|
res.overview = block_queue.get_overview(res.height);
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::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)
|
|
{
|
|
RPC_TRACKER(get_txpool_backlog);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG>(invoke_http_mode::JON_RPC, "get_txpool_backlog", req, res, r))
|
|
return r;
|
|
size_t n_txes = m_core.get_pool_transactions_count();
|
|
CHECK_PAYMENT_MIN1(req, res, COST_PER_TX_POOL_STATS * n_txes, false);
|
|
|
|
if (!m_core.get_txpool_backlog(res.backlog))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get txpool backlog";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::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)
|
|
{
|
|
RPC_TRACKER(get_output_distribution);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_OUTPUT_DISTRIBUTION>(invoke_http_mode::JON_RPC, "get_output_distribution", req, res, r))
|
|
return r;
|
|
|
|
size_t n_0 = 0, n_non0 = 0;
|
|
for (uint64_t amount: req.amounts)
|
|
if (amount) ++n_non0; else ++n_0;
|
|
CHECK_PAYMENT_MIN1(req, res, n_0 * COST_PER_OUTPUT_DISTRIBUTION_0 + n_non0 * COST_PER_OUTPUT_DISTRIBUTION, false);
|
|
|
|
try
|
|
{
|
|
// 0 is placeholder for the whole chain
|
|
const uint64_t req_to_height = req.to_height ? req.to_height : (m_core.get_current_blockchain_height() - 1);
|
|
for (uint64_t amount: req.amounts)
|
|
{
|
|
auto data = rpc::RpcHandler::get_output_distribution([this](uint64_t amount, uint64_t from, uint64_t to, uint64_t &start_height, std::vector<uint64_t> &distribution, uint64_t &base) { return m_core.get_output_distribution(amount, from, to, start_height, distribution, base); }, amount, req.from_height, req_to_height, [this](uint64_t height) { return m_core.get_blockchain_storage().get_db().get_block_hash_from_height(height); }, req.cumulative, m_core.get_current_blockchain_height());
|
|
if (!data)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get output distribution";
|
|
return false;
|
|
}
|
|
|
|
res.distributions.push_back({std::move(*data), amount, "", req.binary, req.compress});
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get output distribution";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_get_output_distribution_bin(const COMMAND_RPC_GET_OUTPUT_DISTRIBUTION::request& req, COMMAND_RPC_GET_OUTPUT_DISTRIBUTION::response& res, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(get_output_distribution_bin);
|
|
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_OUTPUT_DISTRIBUTION>(invoke_http_mode::BIN, "/get_output_distribution.bin", req, res, r))
|
|
return r;
|
|
|
|
size_t n_0 = 0, n_non0 = 0;
|
|
for (uint64_t amount: req.amounts)
|
|
if (amount) ++n_non0; else ++n_0;
|
|
CHECK_PAYMENT_MIN1(req, res, n_0 * COST_PER_OUTPUT_DISTRIBUTION_0 + n_non0 * COST_PER_OUTPUT_DISTRIBUTION, false);
|
|
|
|
res.status = "Failed";
|
|
|
|
if (!req.binary)
|
|
{
|
|
res.status = "Binary only call";
|
|
return true;
|
|
}
|
|
try
|
|
{
|
|
// 0 is placeholder for the whole chain
|
|
const uint64_t req_to_height = req.to_height ? req.to_height : (m_core.get_current_blockchain_height() - 1);
|
|
for (uint64_t amount: req.amounts)
|
|
{
|
|
auto data = rpc::RpcHandler::get_output_distribution([this](uint64_t amount, uint64_t from, uint64_t to, uint64_t &start_height, std::vector<uint64_t> &distribution, uint64_t &base) { return m_core.get_output_distribution(amount, from, to, start_height, distribution, base); }, amount, req.from_height, req_to_height, [this](uint64_t height) { return m_core.get_blockchain_storage().get_db().get_block_hash_from_height(height); }, req.cumulative, m_core.get_current_blockchain_height());
|
|
if (!data)
|
|
{
|
|
res.status = "Failed to get output distribution";
|
|
return true;
|
|
}
|
|
|
|
res.distributions.push_back({std::move(*data), amount, "", req.binary, req.compress});
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
res.status = "Failed to get output distribution";
|
|
return true;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::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)
|
|
{
|
|
RPC_TRACKER(prune_blockchain);
|
|
|
|
try
|
|
{
|
|
if (!(req.check ? m_core.check_blockchain_pruning() : m_core.prune_blockchain()))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = req.check ? "Failed to check blockchain pruning" : "Failed to prune blockchain";
|
|
return false;
|
|
}
|
|
res.pruning_seed = m_core.get_blockchain_pruning_seed();
|
|
res.pruned = res.pruning_seed != 0;
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to prune blockchain";
|
|
return false;
|
|
}
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::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)
|
|
{
|
|
RPC_TRACKER(rpc_access_info);
|
|
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_ACCESS_INFO>(invoke_http_mode::JON_RPC, "rpc_access_info", req, res, r))
|
|
return r;
|
|
|
|
// if RPC payment is not enabled
|
|
if (m_rpc_payment == NULL)
|
|
{
|
|
res.diff = 0;
|
|
res.credits_per_hash_found = 0;
|
|
res.credits = 0;
|
|
res.height = 0;
|
|
res.seed_height = 0;
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
|
|
crypto::public_key client;
|
|
uint64_t ts;
|
|
if (!cryptonote::verify_rpc_payment_signature(req.client, client, ts))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INVALID_CLIENT;
|
|
error_resp.message = "Invalid client ID";
|
|
return false;
|
|
}
|
|
|
|
crypto::hash top_hash;
|
|
m_core.get_blockchain_top(res.height, top_hash);
|
|
++res.height;
|
|
cryptonote::blobdata hashing_blob;
|
|
crypto::hash seed_hash, next_seed_hash;
|
|
if (!m_rpc_payment->get_info(client, [&](const cryptonote::blobdata &extra_nonce, cryptonote::block &b, uint64_t &seed_height, crypto::hash &seed_hash)->bool{
|
|
cryptonote::difficulty_type difficulty;
|
|
uint64_t height, expected_reward;
|
|
size_t reserved_offset;
|
|
if (!get_block_template(m_rpc_payment->get_payment_address(), crypto::null_pkey, NULL, extra_nonce, reserved_offset, difficulty, height, expected_reward, b, seed_height, seed_hash, next_seed_hash, error_resp))
|
|
return false;
|
|
return true;
|
|
}, hashing_blob, res.seed_height, seed_hash, top_hash, res.diff, res.credits_per_hash_found, res.credits, res.cookie))
|
|
{
|
|
return false;
|
|
}
|
|
if (hashing_blob.empty())
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB;
|
|
error_resp.message = "Invalid hashing blob";
|
|
return false;
|
|
}
|
|
res.hashing_blob = epee::string_tools::buff_to_hex_nodelimer(hashing_blob);
|
|
res.top_hash = epee::string_tools::pod_to_hex(top_hash);
|
|
if (hashing_blob[0] >= RX_BLOCK_VERSION)
|
|
{
|
|
res.seed_hash = string_tools::pod_to_hex(seed_hash);
|
|
if (seed_hash != next_seed_hash)
|
|
res.next_seed_hash = string_tools::pod_to_hex(next_seed_hash);
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_flush_cache(const COMMAND_RPC_FLUSH_CACHE::request& req, COMMAND_RPC_FLUSH_CACHE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
RPC_TRACKER(flush_cache);
|
|
if (req.bad_txs)
|
|
m_core.flush_bad_txs_cache();
|
|
if (req.bad_blocks)
|
|
m_core.flush_invalid_blocks();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::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)
|
|
{
|
|
RPC_TRACKER(rpc_access_submit_nonce);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_ACCESS_SUBMIT_NONCE>(invoke_http_mode::JON_RPC, "rpc_access_submit_nonce", req, res, r))
|
|
return r;
|
|
|
|
// if RPC payment is not enabled
|
|
if (m_rpc_payment == NULL)
|
|
{
|
|
res.status = "Payment not necessary";
|
|
return true;
|
|
}
|
|
|
|
crypto::public_key client;
|
|
uint64_t ts;
|
|
if (!cryptonote::verify_rpc_payment_signature(req.client, client, ts))
|
|
{
|
|
res.credits = 0;
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INVALID_CLIENT;
|
|
error_resp.message = "Invalid client ID";
|
|
return false;
|
|
}
|
|
|
|
crypto::hash hash;
|
|
cryptonote::block block;
|
|
crypto::hash top_hash;
|
|
uint64_t height;
|
|
bool stale;
|
|
m_core.get_blockchain_top(height, top_hash);
|
|
if (!m_rpc_payment->submit_nonce(client, req.nonce, top_hash, error_resp.code, error_resp.message, res.credits, hash, block, req.cookie, stale))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!stale)
|
|
{
|
|
// it might be a valid block!
|
|
const difficulty_type current_difficulty = m_core.get_blockchain_storage().get_difficulty_for_next_block();
|
|
if (check_hash(hash, current_difficulty))
|
|
{
|
|
MINFO("This payment meets the current network difficulty");
|
|
block_verification_context bvc;
|
|
if(m_core.handle_block_found(block, bvc))
|
|
MGINFO_GREEN("Block found by RPC user at height " << get_block_height(block) << ": " <<
|
|
print_money(cryptonote::get_outs_money_amount(block.miner_tx)));
|
|
else
|
|
MERROR("Seemingly valid block was not accepted");
|
|
}
|
|
}
|
|
|
|
m_core.get_blockchain_top(height, top_hash);
|
|
res.top_hash = epee::string_tools::pod_to_hex(top_hash);
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::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)
|
|
{
|
|
RPC_TRACKER(rpc_access_pay);
|
|
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_ACCESS_PAY>(invoke_http_mode::JON_RPC, "rpc_access_pay", req, res, r))
|
|
return r;
|
|
|
|
// if RPC payment is not enabled
|
|
if (m_rpc_payment == NULL)
|
|
{
|
|
res.status = "Payment not necessary";
|
|
return true;
|
|
}
|
|
|
|
crypto::public_key client;
|
|
uint64_t ts;
|
|
if (!cryptonote::verify_rpc_payment_signature(req.client, client, ts))
|
|
{
|
|
res.credits = 0;
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INVALID_CLIENT;
|
|
error_resp.message = "Invalid client ID";
|
|
return false;
|
|
}
|
|
|
|
RPCTracker ext_tracker(("external:" + req.paying_for).c_str(), PERF_TIMER_NAME(rpc_access_pay));
|
|
if (!check_payment(req.client, req.payment, req.paying_for, false, res.status, res.credits, res.top_hash))
|
|
return true;
|
|
ext_tracker.pay(req.payment);
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::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)
|
|
{
|
|
RPC_TRACKER(rpc_access_tracking);
|
|
|
|
if (req.clear)
|
|
{
|
|
RPCTracker::clear();
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
|
|
auto data = RPCTracker::data();
|
|
for (const auto &d: data)
|
|
{
|
|
res.data.resize(res.data.size() + 1);
|
|
res.data.back().rpc = d.first;
|
|
res.data.back().count = d.second.count;
|
|
res.data.back().time = d.second.time;
|
|
res.data.back().credits = d.second.credits;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::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)
|
|
{
|
|
RPC_TRACKER(rpc_access_data);
|
|
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_ACCESS_DATA>(invoke_http_mode::JON_RPC, "rpc_access_data", req, res, r))
|
|
return r;
|
|
|
|
if (!m_rpc_payment)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_PAYMENTS_NOT_ENABLED;
|
|
error_resp.message = "Payments not enabled";
|
|
return false;
|
|
}
|
|
|
|
m_rpc_payment->foreach([&](const crypto::public_key &client, const rpc_payment::client_info &info){
|
|
res.entries.push_back({
|
|
epee::string_tools::pod_to_hex(client), info.credits, std::max(info.last_request_timestamp / 1000000, info.update_time),
|
|
info.credits_total, info.credits_used, info.nonces_good, info.nonces_stale, info.nonces_bad, info.nonces_dupe
|
|
});
|
|
return true;
|
|
});
|
|
|
|
res.hashrate = m_rpc_payment->get_hashes(600) / 600;
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::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)
|
|
{
|
|
RPC_TRACKER(rpc_access_account);
|
|
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_ACCESS_ACCOUNT>(invoke_http_mode::JON_RPC, "rpc_access_account", req, res, r))
|
|
return r;
|
|
|
|
if (!m_rpc_payment)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_PAYMENTS_NOT_ENABLED;
|
|
error_resp.message = "Payments not enabled";
|
|
return false;
|
|
}
|
|
|
|
crypto::public_key client;
|
|
if (!epee::string_tools::hex_to_pod(req.client.substr(0, 2 * sizeof(client)), client))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INVALID_CLIENT;
|
|
error_resp.message = "Invalid client ID";
|
|
return false;
|
|
}
|
|
|
|
res.credits = m_rpc_payment->balance(client, req.delta_balance);
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_lookup_account(const COMMAND_RPC_CC_LOOKUP_ACCOUNT::request& req, COMMAND_RPC_CC_LOOKUP_ACCOUNT::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
crypto::public_key public_key;
|
|
if (!epee::string_tools::hex_to_pod(req.public_key, public_key))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PUBLIC_KEY;
|
|
error_resp.message = "Invalid public key";
|
|
return true;
|
|
}
|
|
if (!m_core.get_blockchain_storage().get_db().lookup_cc_account(public_key, res.id))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Account not found";
|
|
return false;
|
|
}
|
|
cryptonote::cc_account_data_t ad;
|
|
if (!m_core.get_blockchain_storage().get_db().get_cc_account_data(res.id, ad))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error looking up account balance";
|
|
return false;
|
|
}
|
|
res.balance = ad.balance;
|
|
res.reserved_balance = 0;
|
|
for (const auto &e: ad.reserve)
|
|
res.reserved_balance += e.first;
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_account(const COMMAND_RPC_CC_GET_ACCOUNT::request& req, COMMAND_RPC_CC_GET_ACCOUNT::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
try
|
|
{
|
|
cryptonote::cc_account_data_t data;
|
|
if (!req.data.empty())
|
|
{
|
|
cryptonote::blobdata bd;
|
|
if (!string_tools::parse_hexstr_to_binbuff(req.data, bd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Invalid hex data";
|
|
return false;
|
|
}
|
|
if (!m_core.get_blockchain_storage().get_db().get_as_cc_account(bd, data))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Account could not be parsed";
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!m_core.get_blockchain_storage().get_db().get_cc_account_data(req.id, data))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Account not found";
|
|
return false;
|
|
}
|
|
}
|
|
res.balance = data.balance;
|
|
res.name = std::move(data.name);
|
|
res.description = std::move(data.description);
|
|
res.ignore = data.ignore;
|
|
res.moose_killed = data.moose_killed;
|
|
res.bears_killed = data.bears_killed;
|
|
res.public_key = epee::string_tools::pod_to_hex(data.public_key);
|
|
res.pmspk = epee::string_tools::pod_to_hex(data.pmspk);
|
|
res.pmvpk = epee::string_tools::pod_to_hex(data.pmvpk);
|
|
for (const auto &e: data.item_balances)
|
|
if (e.second > 0)
|
|
res.item_balances.push_back({e.first, e.second});
|
|
res.flags = std::move(data.flags);
|
|
for (const auto &e: data.badges)
|
|
res.badges.push_back({e.first, e.second.first, e.second.second});
|
|
for (const auto &e: data.attributes)
|
|
res.attributes.push_back({e.first, e.second});
|
|
res.inviting_account = data.inviting_account;
|
|
res.num_invited = data.num_invited;
|
|
res.first_flag = data.first_flag;
|
|
res.num_fires_put_out = data.num_fires_put_out;
|
|
res.num_buildings_demolished = data.num_buildings_demolished;
|
|
res.num_discoveries = data.num_discoveries;
|
|
res.script = data.script;
|
|
res.script_state = data.script_state;
|
|
res.script_city = data.script_city;
|
|
res.script_owner = data.script_owner;
|
|
res.creation_height = data.creation_height;
|
|
res.script_variables.reserve(data.script_variables.size());
|
|
for (const auto &e: data.script_variables)
|
|
res.script_variables.push_back({e.first, e.second});
|
|
res.script_local_variables.reserve(data.script_local_variables.size());
|
|
for (const auto &e: data.script_local_variables)
|
|
res.script_local_variables.push_back({e.first, e.second});
|
|
res.script_string_overrides.reserve(data.script_string_overrides.size());
|
|
for (const auto &e: data.script_string_overrides)
|
|
res.script_string_overrides.push_back({e.first, e.second});
|
|
res.prestige = data.prestige;
|
|
res.level = cc::get_badge_score(data.badges).second;
|
|
|
|
for (const auto &e: data.reserve)
|
|
{
|
|
res.reserve.push_back({});
|
|
res.reserve.back().account = e.first;
|
|
res.reserve.back().balance = e.second.first;
|
|
for (const auto &f: e.second.second)
|
|
res.reserve.back().items.push_back({f.first, f.second});
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get account";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_city(const COMMAND_RPC_CC_GET_CITY::request& req, COMMAND_RPC_CC_GET_CITY::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
cryptonote::cc_city_data_t cd;
|
|
if (!req.data.empty())
|
|
{
|
|
cryptonote::blobdata bd;
|
|
if (!string_tools::parse_hexstr_to_binbuff(req.data, bd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Invalid hex data";
|
|
return false;
|
|
}
|
|
if (!m_core.get_blockchain_storage().get_db().get_as_cc_city(bd, cd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "City could not be parsed";
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!db.get_cc_city_data(req.id, cd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "City not found";
|
|
return false;
|
|
}
|
|
}
|
|
cc::get_city_origin(req.id, res.ox, res.oy);
|
|
res.seed = cd.seed;
|
|
res.treasury = cd.treasury;
|
|
res.mayor = cd.mayor;
|
|
res.name = cd.name;
|
|
res.ignore = cd.ignore;
|
|
res.specializations = 0;
|
|
for (const auto &e: cd.specializations)
|
|
res.specializations |= 1 << e.first;
|
|
res.max_level = cd.max_level;
|
|
res.moose = cd.moose;
|
|
res.bears = cd.bears;
|
|
res.allow_styling = cd.allow_styling;
|
|
|
|
std::vector<cc::special_event_data_t> sed;
|
|
m_core.get_blockchain_storage().get_db().get_cc_special_events(req.id, sed);
|
|
res.special_event = sed.empty() || sed.back().duration > 0 ? cc::SPECIAL_EVENT_NONE : sed.back().special_event;
|
|
|
|
res.treasury_balance = 0;
|
|
bool r = db.get_cc_account_balance(cd.treasury, res.treasury_balance);
|
|
if (!r)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get city economic data";
|
|
return false;
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get city";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_flag(const COMMAND_RPC_CC_GET_FLAG::request& req, COMMAND_RPC_CC_GET_FLAG::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
try
|
|
{
|
|
cryptonote::cc_flag_data_t fd;
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
if (!req.data.empty())
|
|
{
|
|
cryptonote::blobdata bd;
|
|
if (!string_tools::parse_hexstr_to_binbuff(req.data, bd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Invalid hex data";
|
|
return false;
|
|
}
|
|
if (!m_core.get_blockchain_storage().get_db().get_as_cc_flag(bd, fd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Flag could not be parsed";
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!db.get_cc_flag_data(req.id, fd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Flag not found";
|
|
return false;
|
|
}
|
|
}
|
|
std::vector<cc::special_event_data_t> sed;
|
|
m_core.get_blockchain_storage().get_db().get_cc_special_events(fd.city, sed);
|
|
const uint32_t special_event = sed.empty() || sed.back().duration > 0 ? cc::SPECIAL_EVENT_NONE : sed.back().special_event;
|
|
|
|
res.owner = fd.owner;
|
|
res.city = fd.city;
|
|
res.role = fd.role;
|
|
res.x0 = fd.x0;
|
|
res.y0 = fd.y0;
|
|
res.x1 = fd.x1;
|
|
res.y1 = fd.y1;
|
|
res.repair = fd.repair;
|
|
res.economic_power = fd.economic_power;
|
|
res.influence = cc::get_cc_influence(fd.x0, fd.y0, fd.x1, fd.y1, fd.role, fd.economic_power, fd.elevation_bonus, fd.potential, special_event);
|
|
res.construction_height = fd.construction_height;
|
|
res.base_height = fd.base_height;
|
|
for (const auto &e: fd.budget)
|
|
if (e.second > 0)
|
|
res.budget.push_back({e.first, e.second});
|
|
res.fire_state = fd.fire_state;
|
|
res.active = fd.active;
|
|
res.name = fd.name;
|
|
res.ignore = fd.ignore;
|
|
res.last_service_height = fd.last_service_height;
|
|
res.service_price = fd.service_price;
|
|
res.elevation_bonus = fd.elevation_bonus;
|
|
res.crop = fd.crop;
|
|
res.sow_height = fd.sow_height;
|
|
res.vegetables_nutrients = fd.vegetables_nutrients;
|
|
res.grain_nutrients = fd.grain_nutrients;
|
|
res.crop_temperature = cc::get_crop_temperature(cc::get_actual_temperature(db, db.height() - 1, fd.city), fd.potential[POTENTIAL_GEOTHERMAL], fd.base_height);
|
|
res.mortgage = fd.mortgage;
|
|
res.num_missed_ticks = fd.num_missed_ticks;
|
|
res.palette = fd.palette;
|
|
if (req.data.empty())
|
|
{
|
|
if (req.get_unpacked_tiles)
|
|
{
|
|
std::vector<std::vector<uint8_t>> tile_data;
|
|
if (!cc::unpack_tiles(tile_data, fd.tiles, fd.x1 - fd.x0 + 1, fd.y1 - fd.y0 + 1))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to unpack tiles";
|
|
return false;
|
|
}
|
|
res.tiles.resize(tile_data.size());
|
|
for (size_t i = 0; i < tile_data.size(); ++i)
|
|
res.tiles[i].data = std::move(tile_data[i]);
|
|
}
|
|
if (req.get_packed_tiles)
|
|
res.packed_tiles = std::move(fd.tiles);
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get flag";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_find_flag(const COMMAND_RPC_CC_FIND_FLAG::request& req, COMMAND_RPC_CC_FIND_FLAG::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
try
|
|
{
|
|
if (!m_core.get_blockchain_storage().get_db().find_cc_flag(req.city, req.x, req.y, res.id))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Flag not found";
|
|
return false;
|
|
}
|
|
cryptonote::cc_flag_data_t fd;
|
|
if (!m_core.get_blockchain_storage().get_db().get_cc_flag_data(res.id, fd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get flag information";
|
|
return false;
|
|
}
|
|
res.owner = fd.owner;
|
|
res.city = fd.city;
|
|
res.x0 = fd.x0;
|
|
res.y0 = fd.y0;
|
|
res.x1 = fd.x1;
|
|
res.y1 = fd.y1;
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to find flag";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_new_flag_cost(const COMMAND_RPC_CC_GET_NEW_FLAG_COST::request& req, COMMAND_RPC_CC_GET_NEW_FLAG_COST::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
try
|
|
{
|
|
uint32_t ox, oy;
|
|
cc::get_city_origin(req.city, ox, oy);
|
|
if (!cc::get_new_flag_cost(req.x0, req.y0, req.x1, req.y1, ox, oy, res.cost))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error calculating new flag cost: impossible to buy this land";
|
|
return false;
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error calculating new flag cost: impossible to buy this land";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_snapshot(const COMMAND_RPC_CC_GET_SNAPSHOT_FAST::request& req, COMMAND_RPC_CC_GET_SNAPSHOT_FAST::response& res, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_snapshot);
|
|
bool r;
|
|
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_CC_GET_SNAPSHOT_FAST>(invoke_http_mode::BIN, "/cc_get_state.bin", req, res, r))
|
|
return r;
|
|
|
|
try
|
|
{
|
|
RPCLockedTXN lock(m_core);
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
crypto::hash top_hash = db.top_block_hash(&res.snapshot.height);
|
|
res.snapshot.top_hash = epee::string_tools::pod_to_hex(top_hash);
|
|
|
|
uint32_t max_flags = db.get_highest_cc_flag_id();
|
|
res.snapshot.flags.reserve(max_flags);
|
|
res.snapshot.shares.reserve(NUM_ROLES);
|
|
|
|
uint32_t flag_id = 1;
|
|
while (max_flags > 0)
|
|
{
|
|
cryptonote::cc_snapshot::flag_state_t flag_state;
|
|
cryptonote::cc_flag_data_t fd;
|
|
if (db.get_cc_flag_data(flag_id, fd))
|
|
{
|
|
if (true)
|
|
{
|
|
flag_state.id = flag_id;
|
|
flag_state.owner = fd.owner;
|
|
flag_state.city = fd.city;
|
|
flag_state.role = fd.role;
|
|
flag_state.x0 = fd.x0;
|
|
flag_state.y0 = fd.y0;
|
|
flag_state.x1 = fd.x1;
|
|
flag_state.y1 = fd.y1;
|
|
flag_state.repair = fd.repair;
|
|
flag_state.economic_power = fd.economic_power;
|
|
flag_state.potential.resize(NUM_POTENTIALS);
|
|
memcpy(flag_state.potential.data(), fd.potential, NUM_POTENTIALS);
|
|
flag_state.construction_height = fd.construction_height;
|
|
flag_state.base_height = fd.base_height;
|
|
flag_state.fire_state = fd.fire_state;
|
|
flag_state.last_service_height = fd.last_service_height;
|
|
flag_state.service_price = fd.service_price;
|
|
for (const auto &e: fd.budget)
|
|
if (e.second > 0)
|
|
flag_state.budget.push_back({e.first, e.second});
|
|
flag_state.active = fd.active;
|
|
flag_state.name = fd.name;
|
|
flag_state.ignore = fd.ignore;
|
|
flag_state.elevation_bonus = fd.elevation_bonus;
|
|
flag_state.crop = fd.crop;
|
|
flag_state.sow_height = fd.sow_height;
|
|
flag_state.vegetables_nutrients = fd.vegetables_nutrients;
|
|
flag_state.grain_nutrients = fd.grain_nutrients;
|
|
flag_state.mortgage = fd.mortgage;
|
|
flag_state.num_missed_ticks = fd.num_missed_ticks;
|
|
flag_state.palette = fd.palette;
|
|
if (fd.city == req.city)
|
|
{
|
|
flag_state.tiles = std::move(fd.tiles);
|
|
}
|
|
res.snapshot.flags.push_back(std::move(flag_state));
|
|
}
|
|
}
|
|
--max_flags;
|
|
++flag_id;
|
|
}
|
|
|
|
std::map<uint32_t, std::pair<std::map<uint32_t, std::pair<uint8_t, uint64_t>>, std::map<uint32_t, std::pair<uint8_t, uint64_t>>>> weekly_badges;
|
|
uint32_t n_game_updates = 86400 * 7 / DIFFICULTY_TARGET_V2 / GAME_UPDATE_FREQUENCY;
|
|
uint64_t last_update_height = res.snapshot.height;
|
|
while (last_update_height > 0 && !cryptonote::is_game_update_block(last_update_height))
|
|
--last_update_height;
|
|
for (uint64_t update_height = last_update_height; update_height >= GAME_UPDATE_FREQUENCY && n_game_updates-- > 0; update_height -= GAME_UPDATE_FREQUENCY)
|
|
{
|
|
const cryptonote::block b = db.get_block_from_height(update_height);
|
|
const cryptonote::cc_command_game_update_t &game_update = boost::get<cryptonote::cc_command_game_update_t>(b.miner_tx.cc_cmd);
|
|
for (const auto &e: game_update.badges)
|
|
{
|
|
auto &v = weekly_badges[e.account];
|
|
if (v.first.empty())
|
|
{
|
|
cryptonote::cc_account_data_t ad;
|
|
if (!db.get_cc_account_data(e.account, ad))
|
|
{
|
|
res.status = "Internal error: can't get account data";
|
|
MERROR(res.status);
|
|
return false;
|
|
}
|
|
v.first = v.second = ad.badges;
|
|
}
|
|
v.second[e.badge].first -= e.delta_level;
|
|
if (v.second[e.badge].first == 0)
|
|
v.second.erase(e.badge);
|
|
}
|
|
}
|
|
|
|
uint32_t n_accounts = db.get_num_cc_accounts();
|
|
uint32_t account_id = 0;
|
|
while (n_accounts > 0)
|
|
{
|
|
cryptonote::cc_account_data_t data;
|
|
if (db.get_cc_account_data(account_id, data))
|
|
{
|
|
std::vector<uint32_t> badges(NUM_BADGE_LEVELS, 0);
|
|
for (const auto &e: data.badges)
|
|
if (e.second.first > 0 && e.second.first <= NUM_BADGE_LEVELS)
|
|
badges[e.second.first - 1] += 1;
|
|
uint32_t weekly_level_increase = 0;
|
|
const auto it = weekly_badges.find(account_id);
|
|
if (it != weekly_badges.end())
|
|
{
|
|
const uint32_t new_level = cc::get_badge_score(it->second.first).second;
|
|
const uint32_t old_level = cc::get_badge_score(it->second.second).second;
|
|
weekly_level_increase = new_level - old_level;
|
|
}
|
|
res.snapshot.player_data.push_back({account_id, std::move(data.name), data.ignore, std::move(badges), weekly_level_increase, data.prestige});
|
|
--n_accounts;
|
|
}
|
|
++account_id;
|
|
}
|
|
|
|
res.top_hash = db.top_block_hash(&res.height);
|
|
|
|
cryptonote::cc_shares_data_t sd;
|
|
if (!db.get_cc_shares(req.city, db.height() - 1, sd))
|
|
{
|
|
res.status = "Internal error: can't get shares data";
|
|
MERROR(res.status);
|
|
return false;
|
|
}
|
|
for (int role = 1; role < NUM_ROLES; ++role)
|
|
{
|
|
res.snapshot.shares.push_back({(uint8_t)role, sd.payout[role], sd.shares[role]});
|
|
}
|
|
res.snapshot.weighted_shares = sd.weighted_shares;
|
|
|
|
for (uint32_t discovery = 0; discovery < NUM_DISCOVERIES; ++discovery)
|
|
{
|
|
const char *name, *icon, *desc, *unlock_flag;
|
|
std::vector<uint32_t> prerequisites;
|
|
uint64_t difficulty, patent_time;
|
|
uint32_t item;
|
|
if (!cc::get_discovery_data(discovery, name, icon, desc, prerequisites, difficulty, item, patent_time, unlock_flag))
|
|
{
|
|
res.status = "Error retrieving discovery data";
|
|
MERROR(res.status);
|
|
return false;
|
|
}
|
|
if (unlock_flag && !db.get_cc_script_variable(unlock_flag))
|
|
continue;
|
|
uint64_t research_start_height, budget, discovery_height;
|
|
uint32_t discoverer;
|
|
db.get_cc_discovery(discovery, research_start_height, discoverer, discovery_height, budget);
|
|
res.snapshot.discoveries.push_back({discovery, discoverer, research_start_height, discovery_height, budget});
|
|
}
|
|
|
|
db.for_all_cc_cities([&res, &db](const cryptonote::cc_city_data_t &cd){
|
|
std::vector<cc::special_event_data_t> special_events;
|
|
db.get_cc_special_events(cd.id, special_events);
|
|
const uint32_t special_event = special_events.empty() || special_events.back().duration > 0 ? cc::SPECIAL_EVENT_NONE : special_events.back().special_event;
|
|
uint32_t ox, oy;
|
|
cc::get_city_origin(cd.id, ox, oy);
|
|
res.snapshot.cities.push_back({cd.id, ox, oy, cd.seed, cd.name, cd.ignore, cd.mayor, cd.treasury, special_event, get_city_specialization_bitfield(cd.specializations), std::move(cd.allow_styling)});
|
|
return true;
|
|
});
|
|
|
|
block blk;
|
|
bool orphan = false;
|
|
bool have_block = m_core.get_block_by_hash(res.top_hash, blk, &orphan);
|
|
if (!have_block)
|
|
{
|
|
res.status = "Internal error: can't get top block by hash";
|
|
MERROR(res.status);
|
|
return false;
|
|
}
|
|
if (orphan)
|
|
{
|
|
res.status = "Internal error: top block is orphan";
|
|
MERROR(res.status);
|
|
return false;
|
|
}
|
|
std::vector<transaction> txs;
|
|
std::vector<crypto::hash> missed_txs;
|
|
m_core.get_transactions(blk.tx_hashes, txs, missed_txs);
|
|
if (!missed_txs.empty())
|
|
{
|
|
res.status = "Internal error: can't get top block transactions";
|
|
MERROR(res.status);
|
|
return false;
|
|
}
|
|
res.top_commands.reserve(txs.size());
|
|
for (const auto &tx: txs)
|
|
res.top_commands.push_back({tx.cc_cmd});
|
|
|
|
res.snapshot.subsidy = cc::get_game_subsidy(db, last_update_height);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
res.status = "Failed";
|
|
MERROR(res.status << ": " << e.what());
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_order_book(const COMMAND_RPC_CC_GET_ORDER_BOOK::request& req, COMMAND_RPC_CC_GET_ORDER_BOOK::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_order_book);
|
|
|
|
try
|
|
{
|
|
RPCLockedTXN lock(m_core);
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
std::vector<cryptonote::order_t<crypto::hash>> orders;
|
|
db.get_cc_orders(orders, req.bids, req.offers, req.type, req.id);
|
|
|
|
for (const auto &order: orders)
|
|
{
|
|
std::vector<cryptonote::matchable_order_as_string_t> &dst = order.bid ? res.bids : res.offers;
|
|
dst.push_back({{epee::string_tools::pod_to_hex(order.txid), order.mined, order.account, order.bid, order.nonce, order.type, order.id, order.amount, order.price, order.accrual_start_height, order.accrual, order.accrual_price_limit, order.expiration}, 0});
|
|
}
|
|
|
|
std::sort(res.bids.begin(), res.bids.end(), [](const cryptonote::order_t<std::string> &o0, const cryptonote::order_t<std::string> &o1) {
|
|
if (o0.price != o1.price)
|
|
return o0.price > o1.price;
|
|
return o0.txid < o1.txid;
|
|
});
|
|
std::sort(res.offers.begin(), res.offers.end(), [](const cryptonote::order_t<std::string> &o0, const cryptonote::order_t<std::string> &o1) {
|
|
if (o0.price != o1.price)
|
|
return o0.price < o1.price;
|
|
return o0.txid < o1.txid;
|
|
});
|
|
|
|
if (req.calculate_matchable)
|
|
{
|
|
std::map<uint32_t, uint64_t> running_balances;
|
|
for (auto &e: res.bids)
|
|
{
|
|
auto it = running_balances.find(e.account);
|
|
if (it == running_balances.end())
|
|
{
|
|
uint64_t balance;
|
|
if (!db.get_cc_account_balance(e.account, balance))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get account balance";
|
|
return false;
|
|
}
|
|
it = running_balances.insert(std::make_pair(e.account, balance)).first;
|
|
}
|
|
const uint64_t balance = it->second;
|
|
const uint64_t max_amount = balance / e.price;
|
|
const uint32_t already_matched = db.get_cc_trade_used(e.nonce);
|
|
e.matchable = (uint32_t)std::min<uint64_t>(max_amount, e.amount - std::min(already_matched, e.amount));
|
|
it->second -= e.matchable * e.price;
|
|
}
|
|
|
|
std::map<std::tuple<uint32_t, uint32_t, uint32_t>, uint32_t> running_item_balances;
|
|
for (auto &e: res.offers)
|
|
{
|
|
auto it = running_item_balances.find(std::make_tuple(e.account, e.type, e.id));
|
|
if (it == running_item_balances.end())
|
|
{
|
|
cryptonote::cc_account_data_t ad;
|
|
if (!db.get_cc_account_data(e.account, ad))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get account data";
|
|
return false;
|
|
}
|
|
switch (e.type)
|
|
{
|
|
case cryptonote::cc_command_trade_t::type_item:
|
|
{
|
|
for (const auto &i: ad.item_balances)
|
|
running_item_balances[std::make_tuple(e.account, e.type, i.first)] = i.second;
|
|
break;
|
|
}
|
|
case cryptonote::cc_command_trade_t::type_flag:
|
|
running_item_balances[std::make_tuple(e.account, e.type, e.id)] = std::find(ad.flags.begin(), ad.flags.end(), e.id) == ad.flags.end() ? 0 : 1;
|
|
break;
|
|
case cryptonote::cc_command_trade_t::type_city:
|
|
{
|
|
cryptonote::cc_city_data_t cd;
|
|
if (!db.get_cc_city_data(e.id, cd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get city data";
|
|
return false;
|
|
}
|
|
running_item_balances[std::make_tuple(e.account, e.type, e.id)] = cd.mayor == e.account ? 1 : 0;
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
it = running_item_balances.find(std::make_tuple(e.account, e.type, e.id));
|
|
if (it == running_item_balances.end())
|
|
it = running_item_balances.insert(std::make_pair(std::make_tuple(e.account, e.type, e.id), 0)).first;
|
|
}
|
|
const uint32_t item_balance = it->second;
|
|
const uint32_t already_matched = db.get_cc_trade_used(e.nonce);
|
|
e.matchable = (uint32_t)std::min<uint32_t>(item_balance, e.amount - std::min(already_matched, e.amount));
|
|
it->second -= e.matchable;
|
|
}
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving order book";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_nonces_mined(const COMMAND_RPC_CC_GET_NONCES_MINED::request& req, COMMAND_RPC_CC_GET_NONCES_MINED::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_nonces_mined);
|
|
|
|
try
|
|
{
|
|
RPCLockedTXN lock(m_core);
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
cryptonote::blobdata bd;
|
|
crypto::hash txid;
|
|
bool mined;
|
|
for (uint64_t nonce: req.nonces)
|
|
{
|
|
if (!db.get_cc_transaction_hash_from_nonce(nonce, txid, mined))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Nonce not found";
|
|
return false;
|
|
}
|
|
if (mined)
|
|
{
|
|
res.mined_heights.push_back({true, m_core.get_blockchain_storage().get_db().get_tx_block_height(txid)});
|
|
}
|
|
else
|
|
{
|
|
res.mined_heights.push_back({false, 0});
|
|
}
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving nonces data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_shares(const COMMAND_RPC_CC_GET_SHARES::request& req, COMMAND_RPC_CC_GET_SHARES::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_shares);
|
|
|
|
try
|
|
{
|
|
RPCLockedTXN lock(m_core);
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
cryptonote::cc_shares_data_t sd;
|
|
if (!db.get_cc_shares(req.city, db.height() - 1, sd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving share data";
|
|
return false;
|
|
}
|
|
if (sd.city != req.city)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Database returned data for the wrong city";
|
|
return false;
|
|
}
|
|
if (sd.height >= db.height())
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Database returned data for the wrong height";
|
|
return false;
|
|
}
|
|
res.weighted_shares = sd.weighted_shares;
|
|
for (int role = 1; role < NUM_ROLES; ++role)
|
|
{
|
|
res.shares.push_back({(uint8_t)role, sd.payout[role], sd.shares[role]});
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving shares";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_last_update_events(const COMMAND_RPC_CC_GET_LAST_UPDATE_EVENTS::request& req, COMMAND_RPC_CC_GET_LAST_UPDATE_EVENTS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_last_update_events);
|
|
|
|
try
|
|
{
|
|
const uint64_t blockchain_height = m_core.get_current_blockchain_height();
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
uint64_t height = std::min(req.height, blockchain_height - 1);
|
|
if (height == 0)
|
|
height = blockchain_height - 1;
|
|
|
|
while (height > 0 && !cryptonote::is_game_update_block(height))
|
|
--height;
|
|
|
|
std::vector<cc::game_event_t> events;
|
|
if (!db.get_cc_events(height, 0, 0, events))
|
|
{
|
|
res.status = "Internal error: can't get events data";
|
|
MERROR(res.status);
|
|
return false;
|
|
}
|
|
res.events.reserve(events.size());
|
|
for (auto &e: events)
|
|
{
|
|
if (e.cmd == get_cc_tag<uint8_t>(cryptonote::cc_command_game_update_t()))
|
|
{
|
|
res.events.push_back({e.account, std::move(e.counterparties), std::move(e.flags), {}, e.balance, std::move(e.event)});
|
|
for (const auto &i: e.items)
|
|
res.events.back().items.push_back({i.first, i.second});
|
|
}
|
|
}
|
|
|
|
res.height = height;
|
|
res.block_hash = epee::string_tools::pod_to_hex(db.get_block_hash_from_height(height));
|
|
res.timestamp = db.get_block_timestamp(height);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving last update events";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_game_events(const COMMAND_RPC_CC_GET_GAME_EVENTS::request& req, COMMAND_RPC_CC_GET_GAME_EVENTS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_game_events);
|
|
|
|
try
|
|
{
|
|
const uint64_t blockchain_height = m_core.get_current_blockchain_height();
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
uint64_t max_height = std::min(req.max_height, blockchain_height - 1);
|
|
if (max_height < req.min_height)
|
|
{
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
|
|
std::vector<cc::game_event_t> events;
|
|
if (!db.get_cc_game_events(req.min_height, max_height, req.account, req.flag, req.item, req.cmd, events))
|
|
{
|
|
res.status = "Internal error: can't get events data";
|
|
MERROR(res.status);
|
|
return false;
|
|
}
|
|
res.events.reserve(events.size());
|
|
for (auto &e: events)
|
|
{
|
|
res.events.push_back({e.height, e.cmd, e.account, std::move(e.counterparties), std::move(e.flags), {}, e.balance, e.nonce, e.tx_fee, std::move(e.event)});
|
|
for (const auto &i: e.items)
|
|
res.events.back().items.push_back({i.first, i.second});
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving game events";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_discoveries(const COMMAND_RPC_CC_GET_DISCOVERIES::request& req, COMMAND_RPC_CC_GET_DISCOVERIES::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_discoveries);
|
|
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
for (uint32_t discovery = 0; discovery < NUM_DISCOVERIES; ++discovery)
|
|
{
|
|
const char *name, *icon, *desc, *unlock_flag;
|
|
std::vector<uint32_t> prerequisites;
|
|
uint64_t difficulty, patent_time;
|
|
uint32_t item;
|
|
if (!cc::get_discovery_data(discovery, name, icon, desc, prerequisites, difficulty, item, patent_time, unlock_flag))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving discovery data";
|
|
return false;
|
|
}
|
|
if (unlock_flag && !db.get_cc_script_variable(unlock_flag))
|
|
continue;
|
|
uint64_t research_start_height, budget, discovery_height;
|
|
uint32_t discoverer;
|
|
db.get_cc_discovery(discovery, research_start_height, discoverer, discovery_height, budget);
|
|
bool enabled = cc::is_discovery_enabled(db, discovery, req.account);
|
|
res.discoveries.push_back({discovery, name, icon, desc, prerequisites, difficulty, research_start_height, discoverer, discovery_height, budget, item, patent_time, enabled});
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving last update events";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_cities(const COMMAND_RPC_CC_GET_CITIES::request& req, COMMAND_RPC_CC_GET_CITIES::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_cities);
|
|
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
const uint64_t blockchain_height = db.height();
|
|
bool ret = db.for_all_cc_cities([&res, &db, blockchain_height](const cc_city_data_t &cd) {
|
|
cc_shares_data_t sd;
|
|
if (!db.get_cc_shares(cd.id, blockchain_height, sd))
|
|
return false;
|
|
uint64_t treasury_balance;
|
|
if (!db.get_cc_account_balance(cd.treasury, treasury_balance))
|
|
return false;
|
|
uint32_t ox, oy;
|
|
cc::get_city_origin(cd.id, ox, oy);
|
|
res.cities.push_back({cd.id, ox, oy, cd.name, cd.ignore, cd.mayor, cd.treasury, treasury_balance, 0, 0, cc::SPECIAL_EVENT_NONE, get_city_specialization_bitfield(cd.specializations)});
|
|
return true;
|
|
});
|
|
if (!ret)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving city data";
|
|
return false;
|
|
}
|
|
uint64_t weighted_city_growth_shares = 0;
|
|
std::map<uint32_t, uint64_t> city_shares;
|
|
if (!cc::get_weighted_city_shares(db, blockchain_height, weighted_city_growth_shares, &city_shares))
|
|
return false;
|
|
res.total_shares = 0;
|
|
for (auto &e: res.cities)
|
|
{
|
|
e.shares = city_shares[e.city_id];
|
|
e.weighted_city_growth_shares = weighted_city_growth_shares;
|
|
res.total_shares += weighted_city_growth_shares;
|
|
|
|
std::vector<cc::special_event_data_t> sed;
|
|
db.get_cc_special_events(e.city_id, sed);
|
|
if (!sed.empty() && sed.back().duration == 0)
|
|
e.special_event = sed.back().special_event;
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving city data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_special_events(const COMMAND_RPC_CC_GET_SPECIAL_EVENTS::request& req, COMMAND_RPC_CC_GET_SPECIAL_EVENTS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_special_events);
|
|
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
if (!db.does_cc_city_exist(req.city))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM;
|
|
error_resp.message = "Invalid city";
|
|
return false;
|
|
}
|
|
std::vector<cc::special_event_data_t> sed;
|
|
db.get_cc_special_events(req.city, sed);
|
|
if (req.all)
|
|
for (const auto &e: sed)
|
|
res.history.push_back({e.special_event, e.start_height, e.duration});
|
|
else if (!sed.empty() && sed.back().duration == 0)
|
|
res.history.push_back({sed.back().special_event, sed.back().start_height, sed.back().duration});
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving special event data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_custom_items(const COMMAND_RPC_CC_GET_CUSTOM_ITEMS::request& req, COMMAND_RPC_CC_GET_CUSTOM_ITEMS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_custom_items);
|
|
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
if (req.ids.empty())
|
|
{
|
|
db.for_all_cc_custom_items([&req, &res](const cc::cc_custom_item_t &cid) {
|
|
std::string cid_hash = cid.hash == crypto::null_hash ? "" : epee::string_tools::pod_to_hex(cid.hash);
|
|
std::string multihash = cid.hash == crypto::null_hash ? "" : get_ipfs_hash(cid.hash);
|
|
std::string ipfs_data, ipfs_error;
|
|
if (req.include_ipfs_data && cid.hash != crypto::null_hash)
|
|
if (!ipfs_download(multihash, [&ipfs_error](const std::string &msg) { ipfs_error = msg; }, ipfs_data))
|
|
ipfs_data.clear();
|
|
res.items.push_back({cid.id, cid.creator, cid.creation_height, 0, cid.is_group, cid.is_public, cid.group, cid.ignore, cid.name, cid.primary_description, cid.secondary_description, cid.gold, cid.user_data, std::move(cid_hash), std::move(multihash), std::move(ipfs_data), std::move(ipfs_error)});
|
|
return true;
|
|
});
|
|
for (auto &e: res.items)
|
|
e.amount = db.get_cc_item_count(e.id);
|
|
}
|
|
else
|
|
{
|
|
for (uint32_t id: req.ids)
|
|
{
|
|
cc::cc_custom_item_t cid;
|
|
if (!db.get_cc_custom_item_data(id, cid) || cid.id != id)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving item data";
|
|
return false;
|
|
}
|
|
const uint64_t amount = db.get_cc_item_count(id);
|
|
std::string cid_hash = cid.hash == crypto::null_hash ? "" : epee::string_tools::pod_to_hex(cid.hash);
|
|
std::string multihash = cid.hash == crypto::null_hash ? "" : get_ipfs_hash(cid.hash);
|
|
std::string ipfs_data, ipfs_error;
|
|
if (req.include_ipfs_data && cid.hash != crypto::null_hash)
|
|
if (!ipfs_download(multihash, [&ipfs_error](const std::string &msg) { ipfs_error = msg; }, ipfs_data))
|
|
ipfs_data.clear();
|
|
res.items.push_back({cid.id, cid.creator, cid.creation_height, amount, cid.is_group, cid.is_public, cid.group, cid.ignore, cid.name, cid.primary_description, cid.secondary_description, cid.gold, std::move(cid.user_data), std::move(cid_hash), std::move(multihash), std::move(ipfs_data), std::move(ipfs_error)});
|
|
}
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving item data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_accounts(const COMMAND_RPC_CC_GET_ACCOUNTS::request& req, COMMAND_RPC_CC_GET_ACCOUNTS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_accounts);
|
|
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
db.for_all_cc_accounts([&res](const cryptonote::cc_account_data_t &data) {
|
|
res.accounts.push_back({data.id, epee::string_tools::pod_to_hex(data.public_key), std::move(data.name), data.ignore, data.balance});
|
|
return true;
|
|
});
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving accounts";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_flags(const COMMAND_RPC_CC_GET_FLAGS::request& req, COMMAND_RPC_CC_GET_FLAGS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_flags);
|
|
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
db.for_all_cc_flags([&res](const cc_flag_data_t &fd) {
|
|
res.flags.push_back({fd.id, fd.name, fd.ignore});
|
|
return true;
|
|
});
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving flags";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_badge(const COMMAND_RPC_CC_GET_BADGE::request& req, COMMAND_RPC_CC_GET_BADGE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_badge);
|
|
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
if (req.id.empty())
|
|
{
|
|
if (!db.for_all_cc_event_badges([&res](const cc::cc_badge_data_t &bd) {
|
|
res.badges.push_back({bd.id, bd.name, bd.desc, bd.icon});
|
|
return true;
|
|
}))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving badge data";
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (uint32_t id: req.id)
|
|
{
|
|
cc::cc_badge_data_t data;
|
|
if (!cc::get_badge_data(&db, (cc::cc_badge_t)id, data))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving badge data";
|
|
return false;
|
|
}
|
|
res.badges.push_back({id, std::move(data.name), std::move(data.desc), std::move(data.icon)});
|
|
}
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving badge data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_badge_totals(const COMMAND_RPC_CC_GET_BADGE_TOTALS::request& req, COMMAND_RPC_CC_GET_BADGE_TOTALS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_badge_totals);
|
|
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
if (!db.for_all_cc_accounts([&res](const cryptonote::cc_account_data_t &ad){
|
|
std::vector<uint32_t> counts(NUM_BADGE_LEVELS, 0);
|
|
for (const auto &e: ad.badges)
|
|
counts[e.second.first-1] += 1;
|
|
res.entries.push_back({ad.id, std::move(counts)});
|
|
return true;
|
|
}))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving badge data";
|
|
return false;
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving badge data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_attributes(const COMMAND_RPC_CC_GET_ATTRIBUTES::request& req, COMMAND_RPC_CC_GET_ATTRIBUTES::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_attributes);
|
|
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
if (!db.for_all_cc_attributes([&res](const cryptonote::cc_attribute_data_t &ad){
|
|
res.attributes.push_back({ad.id, ad.name, ad.description});
|
|
return true;
|
|
}))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving attribute data";
|
|
return false;
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving attribute data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_bonuses(const COMMAND_RPC_CC_GET_BONUSES::request& req, COMMAND_RPC_CC_GET_BONUSES::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_bonuses);
|
|
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
res.bonuses.reserve(req.accounts.size());
|
|
for (uint32_t account: req.accounts)
|
|
{
|
|
const uint32_t research_bonus = req.research ? cc::get_research_bonus(db, account) : 0;
|
|
const uint32_t firefighting_bonus = req.firefighting ? cc::get_firefighting_bonus_percent(db, account) : 0;
|
|
const uint32_t cartography_level = req.cartography_level ? cc::get_cartography_level(db, account) : 0;
|
|
const uint32_t logging_level = req.logging_level ? cc::get_logging_level(db, account) : 0;
|
|
const uint32_t quarrying_level = req.quarrying_level ? cc::get_quarrying_level(db, account) : 0;
|
|
res.bonuses.push_back({account, research_bonus, firefighting_bonus, cartography_level, logging_level, quarrying_level});
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving attribute data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_is_invitation_used(const COMMAND_RPC_CC_IS_INVITATION_USED::request& req, COMMAND_RPC_CC_IS_INVITATION_USED::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_is_invitation_used);
|
|
|
|
uint32_t account = 0;
|
|
uint64_t amount = 0;
|
|
crypto::public_key pubkey;
|
|
if (!epee::string_tools::hex_to_pod(req.invitation, pubkey))
|
|
{
|
|
cryptonote::blobdata inner;
|
|
crypto::signature inner_signature;
|
|
uint64_t expiration;
|
|
crypto::secret_key secret_key;
|
|
crypto::signature secret_key_signature;
|
|
crypto::public_key recipient;
|
|
if (!cc::parse_invitation(req.invitation, inner, inner_signature, account, amount, expiration, pubkey, recipient, secret_key, secret_key_signature) || account == 0)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PUBLIC_KEY;
|
|
error_resp.message = "Invalid invitation or key";
|
|
return false;
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
res.used = db.has_invitation(pubkey);
|
|
|
|
res.balance_ok = true;
|
|
if (account != 0)
|
|
{
|
|
uint64_t balance;
|
|
if (!db.get_cc_account_balance(account, balance))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Inviting account not found";
|
|
return false;
|
|
}
|
|
res.balance_ok = amount <= std::numeric_limits<uint64_t>::max() - CRYPTONOTE_CC_NEW_ACCOUNT_FEE && balance >= amount + CRYPTONOTE_CC_NEW_ACCOUNT_FEE;
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error retrieving attribute data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_flag_resizing_cost(const COMMAND_RPC_CC_GET_FLAG_RESIZING_COST::request& req, COMMAND_RPC_CC_GET_FLAG_RESIZING_COST::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_flag_resizing_cost);
|
|
|
|
uint32_t ox, oy;
|
|
cc::get_city_origin(req.city, ox, oy);
|
|
if (!cc::get_flag_resizing_cost(req.x0_0, req.y0_0, req.x1_0, req.y1_0, req.x0_1, req.y0_1, req.x1_1, req.y1_1, ox, oy, res.cost))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting flag resizing cost";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_service_fee(const COMMAND_RPC_CC_GET_SERVICE_FEE::request& req, COMMAND_RPC_CC_GET_SERVICE_FEE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_service_fee);
|
|
|
|
if (!cc::get_service_fee(req.service_price, req.x0, req.y0, req.x1, req.y1, res.fee))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting service fee";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_new_city_cost(const COMMAND_RPC_CC_GET_NEW_CITY_COST::request& req, COMMAND_RPC_CC_GET_NEW_CITY_COST::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_new_city_cost);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
const uint64_t height = db.height();
|
|
uint64_t total_shares;
|
|
if (!cc::get_weighted_city_shares(db, height, total_shares))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get total shares";
|
|
return false;
|
|
}
|
|
const uint32_t num_cities = db.get_num_cc_cities();
|
|
res.cost = cc::get_new_city_cost(num_cities, total_shares);
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_are_discoveries_enabled(const COMMAND_RPC_CC_ARE_DISCOVERIES_ENABLED::request& req, COMMAND_RPC_CC_ARE_DISCOVERIES_ENABLED::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_are_discoveries_enabled);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
res.enabled.reserve(req.discoveries.size());
|
|
for (uint32_t discovery: req.discoveries)
|
|
res.enabled.push_back(cc::is_discovery_enabled(db, discovery, req.account));
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_item_count(const COMMAND_RPC_CC_GET_ITEM_COUNT::request& req, COMMAND_RPC_CC_GET_ITEM_COUNT::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_item_count);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
res.counts.resize(req.id.size());
|
|
for (size_t i = 0; i < req.id.size(); ++i)
|
|
res.counts[i] = db.get_cc_item_count(req.id[i]);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Failed to get item count";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_calendar(const COMMAND_RPC_CC_GET_CALENDAR::request& req, COMMAND_RPC_CC_GET_CALENDAR::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_calendar);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
const uint64_t height = req.height == CRYPTONOTE_MAX_BLOCK_NUMBER ? db.height() - 1 : req.height;
|
|
|
|
res.top_height = height;
|
|
cc::get_calendar_date(height, &res.date, res.day, res.month, res.year);
|
|
res.vegetables_sowing_season = cc::is_valid_sowing_time(CROP_VEGETABLES, height);
|
|
res.vegetables_harvest_season = cc::is_valid_harvest_time(CROP_VEGETABLES, height);
|
|
res.grain_sowing_season = cc::is_valid_sowing_time(CROP_GRAIN, height);
|
|
res.grain_harvest_season = cc::is_valid_harvest_time(CROP_GRAIN, height);
|
|
res.blocks_since_vegetables_sowing_season_start = cc::get_blocks_since_sowing_season_start(CROP_VEGETABLES, height);
|
|
res.blocks_till_vegetables_sowing_season_start = cc::get_blocks_till_sowing_season_start(CROP_VEGETABLES, height);
|
|
res.blocks_till_vegetables_sowing_season_end = cc::get_blocks_till_sowing_season_end(CROP_VEGETABLES, height);
|
|
res.blocks_since_vegetables_harvest_season_start = cc::get_blocks_since_harvest_season_start(CROP_VEGETABLES, height);
|
|
res.blocks_till_vegetables_harvest_season_start = cc::get_blocks_till_harvest_season_start(CROP_VEGETABLES, height);
|
|
res.blocks_till_vegetables_harvest_season_end = cc::get_blocks_till_harvest_season_end(CROP_VEGETABLES, height);
|
|
res.blocks_since_grain_sowing_season_start = cc::get_blocks_since_sowing_season_start(CROP_GRAIN, height);
|
|
res.blocks_till_grain_sowing_season_start = cc::get_blocks_till_sowing_season_start(CROP_GRAIN, height);
|
|
res.blocks_till_grain_sowing_season_end = cc::get_blocks_till_sowing_season_end(CROP_GRAIN, height);
|
|
res.blocks_since_grain_harvest_season_start = cc::get_blocks_since_harvest_season_start(CROP_GRAIN, height);
|
|
res.blocks_till_grain_harvest_season_start = cc::get_blocks_till_harvest_season_start(CROP_GRAIN, height);
|
|
res.blocks_till_grain_harvest_season_end = cc::get_blocks_till_harvest_season_end(CROP_GRAIN, height);
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_temperature(const COMMAND_RPC_CC_GET_TEMPERATURE::request& req, COMMAND_RPC_CC_GET_TEMPERATURE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_temperature);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
const uint64_t height = db.height() - 1;
|
|
|
|
res.temperature = cc::get_actual_temperature(db, height, req.city);
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_is_nonce_used(const COMMAND_RPC_CC_IS_NONCE_USED::request& req, COMMAND_RPC_CC_IS_NONCE_USED::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_is_nonce_used);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
if (req.nonce == 0)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Nonce may not be 0";
|
|
return false;
|
|
}
|
|
crypto::hash txid;
|
|
res.used = db.is_cc_nonce_used(req.nonce, &res.accounts, &txid);
|
|
if (res.used)
|
|
res.txid = epee::string_tools::pod_to_hex(txid);
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_used_nonces(const COMMAND_RPC_CC_GET_USED_NONCES::request& req, COMMAND_RPC_CC_GET_USED_NONCES::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_used_nonces);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
db.for_all_cc_nonces_used([&res](uint64_t nonce, const std::vector<uint32_t> &accounts, const crypto::hash &txid) {
|
|
res.nonces.push_back({nonce, accounts, epee::string_tools::pod_to_hex(txid)});
|
|
return true;
|
|
});
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_new_nonces(const COMMAND_RPC_CC_GET_NEW_NONCES::request& req, COMMAND_RPC_CC_GET_NEW_NONCES::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_new_nonces);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
if (req.num_nonces > 256)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Too many nonces";
|
|
return false;
|
|
}
|
|
res.nonces.reserve(req.num_nonces);
|
|
uint32_t num_nonces = req.num_nonces;
|
|
while (num_nonces--)
|
|
{
|
|
res.nonces.push_back(0);
|
|
uint64_t &nonce = res.nonces.back();
|
|
while (1)
|
|
{
|
|
nonce = crypto::rand<uint64_t>();
|
|
if (nonce && !m_core.pool_has_nonce(nonce))
|
|
{
|
|
crypto::hash txid;
|
|
const bool used = db.is_cc_nonce_used(nonce, NULL, &txid);
|
|
if (!used || txid == crypto::null_hash)
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_scripts(const COMMAND_RPC_CC_GET_SCRIPTS::request& req, COMMAND_RPC_CC_GET_SCRIPTS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_scripts);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
const uint32_t num_scripts = db.get_num_cc_scripts();
|
|
const uint32_t first_script = req.first_script == 0 ? 1 : req.first_script;
|
|
const uint32_t last_script = req.last_script;
|
|
if (first_script <= num_scripts)
|
|
{
|
|
res.scripts.reserve(num_scripts - first_script);
|
|
for (uint32_t i = first_script; i <= num_scripts; ++i)
|
|
{
|
|
if (i > last_script)
|
|
break;
|
|
cryptonote::cc_script_data_t sd;
|
|
if (!db.get_cc_script_data(i, sd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting script";
|
|
return false;
|
|
}
|
|
|
|
if (!req.all)
|
|
{
|
|
if (req.owner == 0)
|
|
{
|
|
// standalone scripts
|
|
if (sd.owner != 0 || sd.is_public)
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
// building scripts
|
|
if (sd.owner != req.owner && !sd.is_public)
|
|
continue;
|
|
}
|
|
}
|
|
|
|
bool available = true;
|
|
cc::script::ScriptHandle script;
|
|
if (!cc::script::load_script_binary(sd.blob, script))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting script";
|
|
return false;
|
|
}
|
|
std::vector<cryptonote::COMMAND_RPC_CC_GET_SCRIPTS::requirement_t> requirements;
|
|
std::vector<cryptonote::COMMAND_RPC_CC_GET_SCRIPTS::override_t> all_overrides;
|
|
const auto script_overrides = cc::script::get_script_overrides(script);
|
|
all_overrides.reserve(script_overrides.size());
|
|
for (const auto &e: script_overrides)
|
|
all_overrides.push_back({std::get<0>(e), std::get<1>(e), std::get<2>(e)});
|
|
if (req.account)
|
|
{
|
|
std::vector<std::pair<std::string, bool>> reqs;
|
|
const uint32_t owner = sd.is_public ? req.owner : sd.owner == 0 ? GAME_ACCOUNT : sd.owner;
|
|
const std::vector<std::tuple<uint8_t, uint32_t, std::string>> overrides = cc::script::get_script_overrides(script);
|
|
std::vector<std::tuple<uint8_t, uint32_t, std::string>> runestone_overrides;
|
|
if (req.flag)
|
|
{
|
|
cc::runestone_t runestone;
|
|
if (!db.get_cc_runestone(req.flag, req.dx, req.dy, req.h, runestone))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting runestone";
|
|
return false;
|
|
}
|
|
runestone_overrides = std::move(runestone.overrides);
|
|
for (const auto &e: runestone_overrides)
|
|
{
|
|
for (auto &f: all_overrides)
|
|
{
|
|
if (std::get<0>(e) != f.type)
|
|
continue;
|
|
if (f.type == cc::script::override_string && std::get<1>(e) == f.idx)
|
|
f.str = std::get<2>(e);
|
|
else if (f.type == cc::script::override_local && std::get<2>(e) == f.str)
|
|
f.idx = std::get<1>(e);
|
|
}
|
|
}
|
|
}
|
|
available = cc::script::is_available(db, script, overrides, runestone_overrides, req.account, owner, req.city, req.include_requirements ? &reqs : NULL);
|
|
requirements.reserve(reqs.size());
|
|
for (const auto &e: reqs)
|
|
requirements.push_back({e.first, e.second});
|
|
}
|
|
res.scripts.push_back({sd.id, std::move(sd.name), std::move(sd.icon), std::move(sd.desc), req.include_blob ? epee::string_tools::buff_to_hex_nodelimer(sd.blob) : std::string(), sd.owner, sd.is_public, available, std::move(requirements), std::move(all_overrides), sd.enabled});
|
|
}
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting script";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_script(const COMMAND_RPC_CC_GET_SCRIPT::request& req, COMMAND_RPC_CC_GET_SCRIPT::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_script);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
cryptonote::cc_script_data_t sd;
|
|
if (!db.get_cc_script_data(req.script, sd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting script";
|
|
return false;
|
|
}
|
|
res.name = std::move(sd.name);
|
|
res.icon = std::move(sd.icon);
|
|
res.desc = std::move(sd.desc);
|
|
res.owner = sd.owner;
|
|
res.is_public = sd.is_public;
|
|
res.enabled = sd.enabled;
|
|
|
|
cc::script::ScriptHandle script;
|
|
if (!cc::script::load_script_binary(sd.blob, script))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error parsing script";
|
|
return false;
|
|
}
|
|
if (!cc::script::resolve_references(db, script, {}))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error reconstructing script";
|
|
return false;
|
|
}
|
|
|
|
res.blob = epee::string_tools::buff_to_hex_nodelimer(sd.blob);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting script";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_script_state(const COMMAND_RPC_CC_GET_SCRIPT_STATE::request& req, COMMAND_RPC_CC_GET_SCRIPT_STATE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_script_state);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
cryptonote::cc_script_data_t sd;
|
|
if (!db.get_cc_script_data(req.script, sd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting script";
|
|
return false;
|
|
}
|
|
|
|
cc::script::ScriptHandle script;
|
|
if (!cc::script::load_script_binary(sd.blob, script))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error parsing script";
|
|
return false;
|
|
}
|
|
if (!cc::script::resolve_references(db, script, {}))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error reconstructing script";
|
|
return false;
|
|
}
|
|
|
|
res.script_name = cc::script::get_script_name(script);
|
|
|
|
cc::BlockchainStateProxy proxy(db);
|
|
std::vector<std::tuple<uint32_t, std::string, std::string>> choices;
|
|
std::string image, ui, choices_ui;
|
|
if (!cc::script::get_state(proxy, script, req.account, req.owner, req.state, req.city, ui, res.text, image, choices, choices_ui))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting script state";
|
|
return false;
|
|
}
|
|
res.image = epee::string_tools::buff_to_hex_nodelimer(image);
|
|
res.ui = epee::string_tools::buff_to_hex_nodelimer(ui);
|
|
res.choices_ui = epee::string_tools::buff_to_hex_nodelimer(choices_ui);
|
|
res.choices.reserve(choices.size());
|
|
for (const auto &e: choices)
|
|
res.choices.push_back({std::get<0>(e), std::get<1>(e), std::get<2>(e)});
|
|
cryptonote::cc_account_data_t ad;
|
|
if (!db.get_cc_account_data(req.account, ad))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting account data";
|
|
return false;
|
|
}
|
|
res.overrides.reserve(ad.script_string_overrides.size());
|
|
for (const auto &e: ad.script_string_overrides)
|
|
res.overrides.push_back({cc::script::override_string, e.first, e.second});
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting script";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_script_variable(const COMMAND_RPC_CC_GET_SCRIPT_VARIABLE::request& req, COMMAND_RPC_CC_GET_SCRIPT_VARIABLE::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_script_variable);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
res.value = db.get_cc_script_variable(req.name);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting script variable";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_script_variables(const COMMAND_RPC_CC_GET_SCRIPT_VARIABLES::request& req, COMMAND_RPC_CC_GET_SCRIPT_VARIABLES::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_script_variables);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
res.script_variables.reserve(1024);
|
|
db.for_all_cc_script_variables([&res](const boost::string_ref &name, uint64_t value){ res.script_variables.push_back({std::string(name), value}); return true; });
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error enumerating script variables";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_foreclosures(const COMMAND_RPC_CC_GET_FORECLOSURES::request& req, COMMAND_RPC_CC_GET_FORECLOSURES::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_foreclosures);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
res.foreclosures.reserve(16);
|
|
db.for_all_cc_foreclosures([&res](const cc::foreclosure_t &foreclosure){ res.foreclosures.push_back({foreclosure.mortgage, foreclosure.flag, foreclosure.height, foreclosure.debt_per_share}); return true; });
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error enumerating script variables";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_runestones(const COMMAND_RPC_CC_GET_RUNESTONES::request& req, COMMAND_RPC_CC_GET_RUNESTONES::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_runestones);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
res.runestones.reserve(req.locations.size());
|
|
for (const auto &l: req.locations)
|
|
{
|
|
cc::runestone_t runestone;
|
|
if (!db.get_cc_runestone(l.flag, l.x, l.y, l.h, runestone))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting runestone data";
|
|
return false;
|
|
}
|
|
std::vector<COMMAND_RPC_CC_GET_RUNESTONES::override_t> overrides;
|
|
overrides.reserve(runestone.overrides.size());
|
|
for (auto &e: runestone.overrides)
|
|
overrides.push_back({std::get<0>(e), std::get<1>(e), std::move(std::get<2>(e))});
|
|
res.runestones.push_back({runestone.flag, runestone.x, runestone.y, runestone.h, runestone.script, std::move(runestone.message), std::move(overrides)});
|
|
}
|
|
if (req.flag)
|
|
{
|
|
std::vector<cc::runestone_t> runestones;
|
|
if (!db.get_cc_runestones(req.flag, runestones))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting runestone data";
|
|
return false;
|
|
}
|
|
for (auto &runestone: runestones)
|
|
{
|
|
std::vector<COMMAND_RPC_CC_GET_RUNESTONES::override_t> overrides;
|
|
overrides.reserve(runestone.overrides.size());
|
|
for (auto &e: runestone.overrides)
|
|
overrides.push_back({std::get<0>(e), std::get<1>(e), std::move(std::get<2>(e))});
|
|
res.runestones.push_back({runestone.flag, runestone.x, runestone.y, runestone.h, runestone.script, std::move(runestone.message), std::move(overrides)});
|
|
}
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error getting runestone data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_blob(const COMMAND_RPC_CC_GET_BLOB::request& req, COMMAND_RPC_CC_GET_BLOB::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_blob);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
crypto::hash hash;
|
|
if (!epee::string_tools::hex_to_pod(req.hash, hash))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Bad hash";
|
|
return false;
|
|
}
|
|
cryptonote::blobdata blob;
|
|
const uint32_t refs = db.get_cc_blob(hash, blob);
|
|
if (refs == 0)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Hash not found";
|
|
return false;
|
|
}
|
|
res.blob = epee::string_tools::buff_to_hex_nodelimer(blob);
|
|
res.refs = refs;
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_blob_info(const COMMAND_RPC_CC_GET_BLOB_INFO::request& req, COMMAND_RPC_CC_GET_BLOB_INFO::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_blob_info);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
for (const auto &e: req.hashes)
|
|
{
|
|
crypto::hash hash;
|
|
if (!epee::string_tools::hex_to_pod(e, hash))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Bad hash";
|
|
return false;
|
|
}
|
|
cryptonote::blobdata blob;
|
|
const uint32_t refs = db.get_cc_blob(hash, blob);
|
|
res.refs.push_back(refs);
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_blobs(const COMMAND_RPC_CC_GET_BLOBS::request& req, COMMAND_RPC_CC_GET_BLOBS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_blobs);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
res.hashes.reserve(256);
|
|
db.for_all_cc_blobs([&res](const crypto::hash &hash, uint32_t refs, const cryptonote::blobdata_ref&){
|
|
res.hashes.push_back(epee::string_tools::pod_to_hex(hash));
|
|
return true;
|
|
});
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_auctions(const COMMAND_RPC_CC_GET_AUCTIONS::request& req, COMMAND_RPC_CC_GET_AUCTIONS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_auctions);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
res.auctions.reserve(16);
|
|
db.for_all_cc_auctions([&res, &db](const cc::auction_t &auction) {
|
|
uint32_t bid_account = 0;
|
|
uint64_t bid_price = 0;
|
|
uint64_t bid_height = 0;
|
|
if (!auction.bids.empty())
|
|
{
|
|
const auto &b = auction.bids.back();
|
|
bid_account = std::get<0>(b);
|
|
bid_price = std::get<1>(b);
|
|
bid_height = std::get<2>(b);
|
|
}
|
|
uint64_t projected_end_time;
|
|
bool overtime;
|
|
std::tie(projected_end_time, overtime) = cc::get_auction_end_time(auction, db.height());
|
|
std::vector<cryptonote::COMMAND_RPC_CC_GET_AUCTIONS::entry_t> entries;
|
|
for (const auto &e: auction.entries)
|
|
entries.push_back({e.first, e.second});
|
|
res.auctions.push_back({auction.id, auction.seller, auction.type, std::move(entries), auction.mortgage, auction.creation_height, auction.base_ticks, auction.title, auction.description, bid_account, bid_price, bid_height, projected_end_time, overtime});
|
|
return true;
|
|
});
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
bool core_rpc_server::on_cc_get_stats(const COMMAND_RPC_CC_GET_STATS::request& req, COMMAND_RPC_CC_GET_STATS::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_stats);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
res.num_accounts = db.get_num_cc_accounts();
|
|
res.num_flags = 0;
|
|
res.num_cities = db.get_num_cc_cities();
|
|
res.num_active_flags = 0;
|
|
res.food_needed_per_tick = 0;
|
|
std::map<uint32_t, uint32_t> max_city_level;
|
|
db.for_all_cc_cities([&max_city_level](const cryptonote::cc_city_data_t &cd) {
|
|
max_city_level[cd.id] = cd.max_level;
|
|
return true;
|
|
});
|
|
db.for_all_cc_flags([&res, &db, &max_city_level](const cc_flag_data_t &flag) {
|
|
if (flag.active)
|
|
++res.num_active_flags;
|
|
uint32_t food_needs = cc::get_food_needs(flag.x0, flag.y0, flag.x1, flag.y1, flag.role, flag.economic_power);
|
|
if (cc::is_discovery_enabled(db, DISCOVERY_COOKING, flag.owner))
|
|
food_needs -= food_needs * 2 / 100;
|
|
res.food_needed_per_tick += food_needs;
|
|
uint64_t land_tax;
|
|
uint32_t ox, oy;
|
|
cc::get_city_origin(flag.city, ox, oy);
|
|
const uint32_t cartography_level = cc::get_cartography_level(db, flag.owner);
|
|
cc::get_land_tax(flag.x0, flag.y0, flag.x1, flag.y1, ox, oy, flag.economic_power, max_city_level[flag.city], cartography_level, land_tax);
|
|
res.land_tax += land_tax;
|
|
++res.num_flags;
|
|
return true;
|
|
});
|
|
res.current_food_supply = 0;
|
|
uint32_t points;
|
|
cc::get_food_points(ITEM_FOOD_VEGETABLES, points);
|
|
res.current_food_supply += db.get_cc_item_count(ITEM_FOOD_VEGETABLES) * points;
|
|
cc::get_food_points(ITEM_FOOD_GRAIN, points);
|
|
res.current_food_supply += db.get_cc_item_count(ITEM_FOOD_GRAIN) * points;
|
|
cc::get_food_points(ITEM_FOOD_MEAT, points);
|
|
res.current_food_supply += db.get_cc_item_count(ITEM_FOOD_MEAT) * points;
|
|
cc::get_food_points(ITEM_FOOD_SALTED_MEAT, points);
|
|
res.current_food_supply += db.get_cc_item_count(ITEM_FOOD_SALTED_MEAT) * points;
|
|
|
|
res.decay_cost.resize(std::max<size_t>(256, ITEM_LABOUR + 1), 0);
|
|
uint64_t last_update_height = db.height() - 1;
|
|
while (last_update_height > 0 && !cryptonote::is_game_update_block(last_update_height))
|
|
--last_update_height;
|
|
const cryptonote::block b = db.get_block_from_height(last_update_height);
|
|
const cryptonote::cc_command_game_update_t &game_update = boost::get<cryptonote::cc_command_game_update_t>(b.miner_tx.cc_cmd);
|
|
for (const auto &city: game_update.cities)
|
|
{
|
|
uint32_t id = 0xffffffff;
|
|
for (const auto &e: city.repair)
|
|
{
|
|
id += e.delta_id + 1;
|
|
cryptonote::cc_flag_data_t fd;
|
|
if (db.get_cc_flag_data(id, fd))
|
|
{
|
|
std::map<uint32_t, uint32_t> costs;
|
|
cc::get_repair_costs(fd.x0, fd.y0, fd.x1, fd.y1, fd.role, fd.economic_power, -e.delta_repair, costs);
|
|
for (const auto &c: costs)
|
|
res.decay_cost[c.first] += c.second;
|
|
}
|
|
}
|
|
}
|
|
|
|
res.coin_gold_content = 0;
|
|
res.custom_item_gold_content = 0;
|
|
db.for_all_cc_custom_items([&db, &res](const cc::cc_custom_item_t &cid){
|
|
if (cid.is_group)
|
|
return true;
|
|
if (cid.group == COINS_ITEM_GROUP)
|
|
{
|
|
if (cid.gold != cc::get_collectible_coin_gold_content(COLLECTIBLE_COIN_TYPE(cid.user_data)) * COIN)
|
|
MERROR("Inconsistent gold content for coin " << cid.id);
|
|
res.coin_gold_content += db.get_cc_item_count(cid.id) * cc::get_collectible_coin_gold_content(COLLECTIBLE_COIN_TYPE(cid.user_data)) * COIN;
|
|
}
|
|
else if (cid.gold > 0)
|
|
res.custom_item_gold_content += db.get_cc_item_count(cid.id) * cid.gold;
|
|
return true;
|
|
});
|
|
|
|
res.game_balances = 0;
|
|
res.treasury_balances = 0;
|
|
res.player_balances = 0;
|
|
db.for_all_cc_accounts([&db, &res](const cryptonote::cc_account_data_t &ad){
|
|
const bool is_treasury = ad.public_key == crypto::null_pkey;
|
|
uint64_t &balance = ad.id == GAME_ACCOUNT ? res.game_balances : is_treasury ? res.treasury_balances : res.player_balances;
|
|
balance += ad.balance;
|
|
for (const auto &e: ad.reserve)
|
|
balance += e.first;
|
|
return true;
|
|
});
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error gathering stats";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_item_ownership(const COMMAND_RPC_CC_GET_ITEM_OWNERSHIP::request& req, COMMAND_RPC_CC_GET_ITEM_OWNERSHIP::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_item_ownership);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
res.supply = db.get_cc_item_count(req.item);
|
|
db.for_all_cc_accounts([&req, &res](const cryptonote::cc_account_data_t &ad){
|
|
uint32_t amount = 0;
|
|
std::map<uint32_t, uint32_t>::const_iterator i;
|
|
if ((i = ad.item_balances.find(req.item)) != ad.item_balances.end())
|
|
amount += i->second;
|
|
for (const auto &e: ad.reserve)
|
|
if ((i = e.second.second.find(req.item)) != e.second.second.end())
|
|
amount += i->second;
|
|
if (amount > 0)
|
|
res.ownership.push_back({ad.id, amount});
|
|
return true;
|
|
});
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error gathering item ownership";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_predefined_item_info(const COMMAND_RPC_CC_GET_PREDEFINED_ITEM_INFO::request& req, COMMAND_RPC_CC_GET_PREDEFINED_ITEM_INFO::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_predefined_item_info);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
for (uint32_t id: req.ids)
|
|
{
|
|
if (id == 0 || id >= NUM_PREDEFINED_ITEMS || !cc::is_item_enabled(id, db.height()))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Invalid predefined item";
|
|
return false;
|
|
}
|
|
uint32_t food = 0;
|
|
if (!cc::get_food_points(id, food))
|
|
food = 0;
|
|
const uint32_t heating = cc::get_heating_points(id);
|
|
std::string name = cc::get_predefined_item_name(id);
|
|
const uint64_t amount = db.get_cc_item_count(id);
|
|
const bool is_block = id >= ITEM_FIRST_BLOCK && id <= ITEM_LAST_BLOCK;
|
|
const bool is_stone = id >= ITEM_FIRST_STONE && id <= ITEM_LAST_STONE;
|
|
const bool is_wood = id >= ITEM_FIRST_WOOD && id <= ITEM_LAST_WOOD;
|
|
const bool is_gemstone = id >= ITEM_FIRST_GEMSTONE && id <= ITEM_LAST_GEMSTONE;
|
|
const bool is_food = id >= ITEM_FIRST_FOOD && id <= ITEM_LAST_FOOD;
|
|
const bool is_patent = id >= ITEM_FIRST_PATENT && id <= ITEM_LAST_PATENT;
|
|
res.items.push_back({id, amount, std::move(name), food, heating, is_block, is_stone, is_wood, is_gemstone, is_food, is_patent});
|
|
}
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error gathering item info";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_is_custom_item_data(const COMMAND_RPC_CC_IS_CUSTOM_ITEM_DATA::request& req, COMMAND_RPC_CC_IS_CUSTOM_ITEM_DATA::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_is_custom_item_data);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
bool raw = false;
|
|
crypto::hash hash;
|
|
if (epee::string_tools::hex_to_pod(req.hash, hash))
|
|
raw = true;
|
|
|
|
try
|
|
{
|
|
res.id = 0;
|
|
res.ignore = false;
|
|
db.for_all_cc_custom_items([&req, &res, &hash, raw](const cc::cc_custom_item_t &cid) {
|
|
if (raw && cid.hash == hash)
|
|
{
|
|
res.id = cid.id;
|
|
res.ignore = cid.ignore;
|
|
res.creation_height = cid.creation_height;
|
|
return false;
|
|
}
|
|
else if (!raw && req.hash == get_ipfs_hash(cid.hash))
|
|
{
|
|
res.id = cid.id;
|
|
res.ignore = cid.ignore;
|
|
res.creation_height = cid.creation_height;
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error looking up item data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_terrain(const COMMAND_RPC_CC_GET_TERRAIN::request& req, COMMAND_RPC_CC_GET_TERRAIN::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_terrain);
|
|
|
|
const uint32_t x0 = req.x0;
|
|
const uint32_t y0 = req.y0;
|
|
const uint32_t x1 = req.x1;
|
|
const uint32_t y1 = req.y1;
|
|
|
|
if (x1 < x0 || y1 < y0)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Invalid coordinates";
|
|
return false;
|
|
}
|
|
if (x1 - x0 + 1 > 4096 || y1 - y0 + 1 > 4096 || (x1 - x0 + 1) * (y1 - y0 + 1) > 1024 * 1024)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Area is too large";
|
|
return false;
|
|
}
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
cryptonote::cc_city_data_t cd;
|
|
if (!db.get_cc_city_data(req.city, cd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "City not found";
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
const uint32_t endx = x1 + 1;
|
|
const uint32_t endy = y1 + 1;
|
|
|
|
#define WRITE_TERRAIN(field, func) \
|
|
if (req.field) \
|
|
{ \
|
|
for (uint32_t y = y0; y != endy; ++y) \
|
|
{ \
|
|
for (uint32_t x = x0; x != endx; ++x) \
|
|
{ \
|
|
res.field.push_back(func(x, y)); \
|
|
} \
|
|
} \
|
|
} \
|
|
|
|
const cc::cc_potential_state_t *state = cc::get_cc_potential_state(req.city, cd.seed);
|
|
WRITE_TERRAIN(height, [state](uint32_t x, uint32_t y) { return cc::get_cc_height(state, x, y); });
|
|
WRITE_TERRAIN(stability, [state](uint32_t x, uint32_t y) { return cc::get_cc_stability(state, x, y); });
|
|
WRITE_TERRAIN(agriculture, [state](uint32_t x, uint32_t y) { return cc::get_cc_agricultural_potential(state, x, y); });
|
|
WRITE_TERRAIN(geothermal_heating, [state](uint32_t x, uint32_t y) { return cc::get_cc_geothermal_heating(state, x, y); });
|
|
WRITE_TERRAIN(gemstone, [state](uint32_t x, uint32_t y) { return cc::get_cc_gemstone_potential(state, x, y); });
|
|
WRITE_TERRAIN(wood_type, [state](uint32_t x, uint32_t y) { return cc::get_cc_wood_type_potential(state, x, y); });
|
|
WRITE_TERRAIN(wood_quantity, [state](uint32_t x, uint32_t y) { return cc::get_cc_wood_quantity_potential(state, x, y); });
|
|
WRITE_TERRAIN(stone_type, [state](uint32_t x, uint32_t y) { return cc::get_cc_stone_type_potential(state, x, y); });
|
|
WRITE_TERRAIN(stone_quantity, [state](uint32_t x, uint32_t y) { return cc::get_cc_stone_quantity_potential(state, x, y); });
|
|
WRITE_TERRAIN(cliff, [state](uint32_t x, uint32_t y) { return cc::get_cc_cliff_potential(state, x, y); });
|
|
|
|
#undef WRITE_TERRAIN
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error looking up terrain data";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
bool core_rpc_server::on_cc_get_crop_yield(const COMMAND_RPC_CC_GET_CROP_YIELD::request& req, COMMAND_RPC_CC_GET_CROP_YIELD::response& res, epee::json_rpc::error& error_resp, const connection_context *ctx)
|
|
{
|
|
PERF_TIMER(on_cc_get_crop_yield);
|
|
|
|
BlockchainDB &db = m_core.get_blockchain_storage().get_db();
|
|
|
|
try
|
|
{
|
|
cryptonote::cc_flag_data_t fd;
|
|
if (!db.get_cc_flag_data(req.flag, fd))
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Flag not found";
|
|
return false;
|
|
}
|
|
if (fd.role != ROLE_AGRICULTURAL || fd.crop == CROP_NONE)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Flag is not growing a crop";
|
|
return false;
|
|
}
|
|
res.yield = cc::get_crop_yield(db, req.flag);
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
{
|
|
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
|
|
error_resp.message = "Error calculating crop yield ";
|
|
return false;
|
|
}
|
|
|
|
res.status = CORE_RPC_STATUS_OK;
|
|
return true;
|
|
}
|
|
//------------------------------------------------------------------------------------------------------------------------------
|
|
const command_line::arg_descriptor<std::string, false, true, 2> core_rpc_server::arg_rpc_bind_port = {
|
|
"rpc-bind-port"
|
|
, "Port for RPC server"
|
|
, std::to_string(config::RPC_DEFAULT_PORT)
|
|
, {{ &cryptonote::arg_testnet_on, &cryptonote::arg_stagenet_on }}
|
|
, [](std::array<bool, 2> testnet_stagenet, bool defaulted, std::string val)->std::string {
|
|
if (testnet_stagenet[0] && defaulted)
|
|
return std::to_string(config::testnet::RPC_DEFAULT_PORT);
|
|
else if (testnet_stagenet[1] && defaulted)
|
|
return std::to_string(config::stagenet::RPC_DEFAULT_PORT);
|
|
return val;
|
|
}
|
|
};
|
|
|
|
const command_line::arg_descriptor<std::string> core_rpc_server::arg_rpc_restricted_bind_port = {
|
|
"rpc-restricted-bind-port"
|
|
, "Port for restricted RPC server"
|
|
, ""
|
|
};
|
|
|
|
const command_line::arg_descriptor<bool> core_rpc_server::arg_restricted_rpc = {
|
|
"restricted-rpc"
|
|
, "Restrict RPC to view only commands and do not return privacy sensitive data in RPC calls"
|
|
, false
|
|
};
|
|
|
|
const command_line::arg_descriptor<std::string> core_rpc_server::arg_bootstrap_daemon_address = {
|
|
"bootstrap-daemon-address"
|
|
, "URL of a 'bootstrap' remote daemon that the connected wallets can use while this daemon is still not fully synced.\n"
|
|
"Use 'auto' to enable automatic public nodes discovering and bootstrap daemon switching"
|
|
, ""
|
|
};
|
|
|
|
const command_line::arg_descriptor<std::string> core_rpc_server::arg_bootstrap_daemon_login = {
|
|
"bootstrap-daemon-login"
|
|
, "Specify username:password for the bootstrap daemon login"
|
|
, ""
|
|
};
|
|
|
|
const command_line::arg_descriptor<std::string> core_rpc_server::arg_bootstrap_daemon_proxy = {
|
|
"bootstrap-daemon-proxy"
|
|
, "<ip>:<port> socks proxy to use for bootstrap daemon connections"
|
|
, ""
|
|
};
|
|
|
|
const command_line::arg_descriptor<std::string> core_rpc_server::arg_rpc_payment_address = {
|
|
"rpc-payment-address"
|
|
, "Restrict RPC to clients sending micropayment to this address"
|
|
, ""
|
|
};
|
|
|
|
const command_line::arg_descriptor<uint64_t> core_rpc_server::arg_rpc_payment_difficulty = {
|
|
"rpc-payment-difficulty"
|
|
, "Restrict RPC to clients sending micropayment at this difficulty"
|
|
, DEFAULT_PAYMENT_DIFFICULTY
|
|
};
|
|
|
|
const command_line::arg_descriptor<uint64_t> core_rpc_server::arg_rpc_payment_credits = {
|
|
"rpc-payment-credits"
|
|
, "Restrict RPC to clients sending micropayment, yields that many credits per payment"
|
|
, DEFAULT_PAYMENT_CREDITS_PER_HASH
|
|
};
|
|
|
|
const command_line::arg_descriptor<bool> core_rpc_server::arg_rpc_payment_allow_free_loopback = {
|
|
"rpc-payment-allow-free-loopback"
|
|
, "Allow free access from the loopback address (ie, the local host)"
|
|
, false
|
|
};
|
|
} // namespace cryptonote
|