Debian Codegraph @

monero/src/wallet/wallet_rpc_payments.cpp

raw symbols SCIP
1 // Copyright (c) 2018-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include <boost/optional/optional.hpp>
30 #include <boost/utility/value_init.hpp>
31 #include "include_base_utils.h"
32 #include "cryptonote_config.h"
33 #include "wallet_rpc_helpers.h"
34 #include "wallet2.h"
35 #include "cryptonote_basic/cryptonote_format_utils.h"
36 #include "rpc/core_rpc_server_commands_defs.h"
37 #include "rpc/rpc_payment_signature.h"
38 #include "misc_language.h"
39 #include "cryptonote_basic/cryptonote_basic_impl.h"
40 #include "int-util.h"
41 #include "crypto/crypto.h"
42 #include "cryptonote_basic/blobdatatype.h"
43 #include "common/i18n.h"
44 #include "common/util.h"
45 #include "common/threadpool.h"
46
47 #undef MONERO_DEFAULT_LOG_CATEGORY
48 #define MONERO_DEFAULT_LOG_CATEGORY "wallet.wallet2.rpc_payments"
49
50 #define RPC_PAYMENT_POLL_PERIOD 10 /* seconds*/
51
52 namespace tools
53 {
54 //----------------------------------------------------------------------------------------------------
55 std::string wallet2::get_client_signature() const
56 {
57 return cryptonote::make_rpc_payment_signature(m_rpc_client_secret_key);
58 }
59 //----------------------------------------------------------------------------------------------------
60 bool wallet2::get_rpc_payment_info(bool mining, bool &payment_required, uint64_t &credits, uint64_t &diff, uint64_t &credits_per_hash_found, cryptonote::blobdata &hashing_blob, uint64_t &height, uint64_t &seed_height, crypto::hash &seed_hash, crypto::hash &next_seed_hash, uint32_t &cookie)
61 {
62 boost::optional<std::string> result = m_node_rpc_proxy.get_rpc_payment_info(mining, payment_required, credits, diff, credits_per_hash_found, hashing_blob, height, seed_height, seed_hash, next_seed_hash, cookie);
63 credits = m_rpc_payment_state.credits;
64 if (result && *result != CORE_RPC_STATUS_OK)
65 return false;
66 return true;
67 }
68 //----------------------------------------------------------------------------------------------------
69 bool wallet2::daemon_requires_payment()
70 {
71 bool payment_required = false;
72 uint64_t credits, diff, credits_per_hash_found, height, seed_height;
73 uint32_t cookie;
74 cryptonote::blobdata blob;
75 crypto::hash seed_hash, next_seed_hash;
76 return get_rpc_payment_info(false, payment_required, credits, diff, credits_per_hash_found, blob, height, seed_height, seed_hash, next_seed_hash, cookie) && payment_required;
77 }
78 //----------------------------------------------------------------------------------------------------
79 bool wallet2::make_rpc_payment(uint32_t nonce, uint32_t cookie, uint64_t &credits, uint64_t &balance)
80 {
81 cryptonote::COMMAND_RPC_ACCESS_SUBMIT_NONCE::request req = AUTO_VAL_INIT(req);
82 cryptonote::COMMAND_RPC_ACCESS_SUBMIT_NONCE::response res = AUTO_VAL_INIT(res);
83 req.nonce = nonce;
84 req.cookie = cookie;
85 m_daemon_rpc_mutex.lock();
86 uint64_t pre_call_credits = m_rpc_payment_state.credits;
87 req.client = get_client_signature();
88 epee::json_rpc::error error;
89 bool r = epee::net_utils::invoke_http_json_rpc("/json_rpc", "rpc_access_submit_nonce", req, res, error, *m_http_client, rpc_timeout);
90 m_daemon_rpc_mutex.unlock();
91 THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, error, res, "rpc_access_submit_nonce");
92 THROW_WALLET_EXCEPTION_IF(res.credits < pre_call_credits, error::wallet_internal_error, "RPC payment did not increase balance");
93 if (m_rpc_payment_state.top_hash != res.top_hash)
94 {
95 m_rpc_payment_state.top_hash = res.top_hash;
96 m_rpc_payment_state.stale = true;
97 }
98
99 m_rpc_payment_state.credits = res.credits;
100 balance = res.credits;
101 credits = balance - pre_call_credits;
102 return true;
103 }
104 //----------------------------------------------------------------------------------------------------
105 void wallet2::check_rpc_cost(const char *call, uint64_t post_call_credits, uint64_t pre_call_credits, double expected_cost)
106 {
107 return tools::check_rpc_cost(m_rpc_payment_state, call, post_call_credits, pre_call_credits, expected_cost);
108 }
109 //----------------------------------------------------------------------------------------------------
110 }