Debian Codegraph @

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 bool wallet2::search_for_rpc_payment(uint64_t credits_target, uint32_t n_threads, const std::function<bool(uint64_t, uint64_t)> &startfunc, const std::function<bool(unsigned)> &contfunc, const std::function<bool(uint64_t)> &foundfunc, const std::function<void(const std::string&)> &errorfunc)
106 {
107 bool need_payment = false;
108 bool payment_required;
109 uint64_t credits, diff, credits_per_hash_found, height, seed_height;
110 uint32_t cookie;
111 unsigned int n_hashes = 0;
112 cryptonote::blobdata hashing_blob;
113 crypto::hash seed_hash, next_seed_hash;
114 try
115 {
116 need_payment = get_rpc_payment_info(false, payment_required, credits, diff, credits_per_hash_found, hashing_blob, height, seed_height, seed_hash, next_seed_hash, cookie) && payment_required && credits < credits_target;
117 if (!need_payment)
118 return true;
119 if (!startfunc(diff, credits_per_hash_found))
120 return true;
121 }
122 catch (const std::exception &e) { return false; }
123
124 static std::atomic<uint32_t> nonce(0);
125 while (contfunc(n_hashes))
126 {
127 try
128 {
129 need_payment = get_rpc_payment_info(true, payment_required, credits, diff, credits_per_hash_found, hashing_blob, height, seed_height, seed_hash, next_seed_hash, cookie) && payment_required && credits < credits_target;
130 if (!need_payment)
131 return true;
132 }
133 catch (const std::exception &e) { return false; }
134 if (hashing_blob.empty())
135 {
136 MERROR("Bad hashing blob from daemon");
137 if (errorfunc)
138 errorfunc("Bad hashing blob from daemon, trying again");
139 epee::misc_utils::sleep_no_w(1000);
140 continue;
141 }
142
143 if(n_threads == 0)
144 n_threads = boost::thread::hardware_concurrency();
145
146 std::vector<crypto::hash> hash(n_threads);
147 tools::threadpool& tpool = tools::threadpool::getInstanceForCompute();
148 tools::threadpool::waiter waiter(tpool);
149
150 const uint32_t local_nonce = nonce += n_threads; // wrapping's OK
151 for (size_t i = 0; i < n_threads; i++)
152 {
153 tpool.submit(&waiter, [&, i] {
154 *(uint32_t*)(hashing_blob.data() + 39) = SWAP32LE(local_nonce-i);
155 const uint8_t major_version = hashing_blob[0];
156 if (major_version >= RX_BLOCK_VERSION)
157 {
158 crypto::rx_slow_hash(seed_hash.data, hashing_blob.data(), hashing_blob.size(), hash[i].data);
159 }
160 else
161 {
162 int cn_variant = hashing_blob[0] >= 7 ? hashing_blob[0] - 6 : 0;
163 crypto::cn_slow_hash(hashing_blob.data(), hashing_blob.size(), hash[i], cn_variant, height);
164 }
165 });
166 }
167 waiter.wait();
168 n_hashes += n_threads;
169
170 for(size_t i=0; i < n_threads; i++)
171 {
172 if (cryptonote::check_hash(hash[i], diff))
173 {
174 uint64_t credits, balance;
175 try
176 {
177 make_rpc_payment(local_nonce-i, cookie, credits, balance);
178 if (credits != credits_per_hash_found)
179 {
180 MERROR("Found nonce, but daemon did not credit us with the expected amount");
181 if (errorfunc)
182 errorfunc("Found nonce, but daemon did not credit us with the expected amount");
183 return false;
184 }
185 MDEBUG("Found nonce " << local_nonce-i << " at diff " << diff << ", gets us " << credits_per_hash_found << ", now " << balance << " credits");
186 if (!foundfunc(credits))
187 break;
188 }
189 catch (const tools::error::wallet_coded_rpc_error &e)
190 {
191 MWARNING("Found a local_nonce at diff " << diff << ", but failed to send it to the daemon");
192 if (errorfunc)
193 errorfunc("Found nonce, but daemon errored out with error " + std::to_string(e.code()) + ": " + e.status() + ", continuing");
194 }
195 catch (const std::exception &e)
196 {
197 MWARNING("Found a local_nonce at diff " << diff << ", but failed to send it to the daemon");
198 if (errorfunc)
199 errorfunc("Found nonce, but daemon errored out with: '" + std::string(e.what()) + "', continuing");
200 }
201 }
202 }
203 }
204 return true;
205 }
206 //----------------------------------------------------------------------------------------------------
207 void wallet2::check_rpc_cost(const char *call, uint64_t post_call_credits, uint64_t pre_call_credits, double expected_cost)
208 {
209 return tools::check_rpc_cost(m_rpc_payment_state, call, post_call_credits, pre_call_credits, expected_cost);
210 }
211 //----------------------------------------------------------------------------------------------------
212 }