Debian Codegraph @

monero/src/wallet/node_rpc_proxy.cpp

raw symbols SCIP
1 // Copyright (c) 2017-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 "node_rpc_proxy.h"
30 #include "rpc/core_rpc_server_commands_defs.h"
31 #include "rpc/rpc_payment_signature.h"
32 #include "rpc/rpc_payment_costs.h"
33 #include "storages/http_abstract_invoke.h"
34
35 #include <boost/thread.hpp>
36
37 #define RETURN_ON_RPC_RESPONSE_ERROR(r, error, res, method) \
38 do { \
39 CHECK_AND_ASSERT_MES(error.code == 0, error.message, error.message); \
40 handle_payment_changes(res, std::integral_constant<bool, HasCredits<decltype(res)>::Has>()); \
41 CHECK_AND_ASSERT_MES(r, std::string("Failed to connect to daemon"), "Failed to connect to daemon"); \
42 /* empty string -> not connection */ \
43 CHECK_AND_ASSERT_MES(!res.status.empty(), res.status, "No connection to daemon"); \
44 CHECK_AND_ASSERT_MES(res.status != CORE_RPC_STATUS_BUSY, res.status, "Daemon busy"); \
45 CHECK_AND_ASSERT_MES(res.status != CORE_RPC_STATUS_PAYMENT_REQUIRED, res.status, "Payment required"); \
46 CHECK_AND_ASSERT_MES(res.status == CORE_RPC_STATUS_OK, res.status, "Error calling " + std::string(method) + " daemon RPC"); \
47 } while(0)
48
49 using namespace epee;
50
51 namespace tools
52 {
53
54 static const std::chrono::seconds rpc_timeout = std::chrono::seconds(10);
55
56 NodeRPCProxy::NodeRPCProxy(epee::net_utils::http::abstract_http_client &http_client, rpc_payment_state_t &rpc_payment_state, boost::recursive_mutex &mutex)
57 : m_http_client(http_client)
58 , m_rpc_payment_state(rpc_payment_state)
59 , m_daemon_rpc_mutex(mutex)
60 , m_offline(false)
61 {
62 invalidate();
63 }
64
65 void NodeRPCProxy::invalidate()
66 {
67 m_height = 0;
68 for (size_t n = 0; n < 256; ++n)
69 m_earliest_height[n] = 0;
70 m_dynamic_base_fee_estimate = 0;
71 m_dynamic_base_fee_estimate_cached_height = 0;
72 m_dynamic_base_fee_estimate_grace_blocks = 0;
73 m_dynamic_base_fee_estimate_vector.clear();
74 m_fee_quantization_mask = 1;
75 m_rpc_version = 0;
76 m_target_height = 0;
77 m_block_weight_limit = 0;
78 m_adjusted_time = 0;
79 m_get_info_time = 0;
80 m_rpc_payment_info_time = 0;
81 m_rpc_payment_seed_height = 0;
82 m_rpc_payment_seed_hash = crypto::null_hash;
83 m_rpc_payment_next_seed_hash = crypto::null_hash;
84 m_height_time = 0;
85 m_rpc_payment_diff = 0;
86 m_rpc_payment_credits_per_hash_found = 0;
87 m_rpc_payment_height = 0;
88 m_rpc_payment_cookie = 0;
89 }
90
91 boost::optional<std::string> NodeRPCProxy::get_rpc_version(uint32_t &rpc_version)
92 {
93 if (m_offline)
94 return boost::optional<std::string>("offline");
95 if (m_rpc_version == 0)
96 {
97 cryptonote::COMMAND_RPC_GET_VERSION::request req_t = AUTO_VAL_INIT(req_t);
98 cryptonote::COMMAND_RPC_GET_VERSION::response resp_t = AUTO_VAL_INIT(resp_t);
99 {
100 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
101 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_version", req_t, resp_t, m_http_client, rpc_timeout);
102 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "get_version");
103 }
104 m_rpc_version = resp_t.version;
105 }
106 rpc_version = m_rpc_version;
107 return boost::optional<std::string>();
108 }
109
110 void NodeRPCProxy::set_height(uint64_t h)
111 {
112 m_height = h;
113 }
114
115 boost::optional<std::string> NodeRPCProxy::get_info()
116 {
117 if (m_offline)
118 return boost::optional<std::string>("offline");
119 const time_t now = time(NULL);
120 if (now >= m_get_info_time + 30) // re-cache every 30 seconds
121 {
122 // If this function gets called in multiple threads we want to avoid calling get_info more than once every 30 seconds
123 m_get_info_time = now;
124
125 cryptonote::COMMAND_RPC_GET_INFO::request req_t = AUTO_VAL_INIT(req_t);
126 cryptonote::COMMAND_RPC_GET_INFO::response resp_t = AUTO_VAL_INIT(resp_t);
127
128 {
129 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
130 uint64_t pre_call_credits = m_rpc_payment_state.credits;
131 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
132
133 // Called often, fails often. Try twice to force reconnect.
134 int attempts = 2;
135 while (true) {
136 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_info", req_t, resp_t, m_http_client, rpc_timeout);
137 if (r) {
138 break;
139 }
140
141 attempts--;
142 if (attempts <= 0) {
143 m_get_info_time = 0; // Allow get_info to be called again before 30s cache
144 m_height = 0;
145 m_target_height = 0;
146 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "get_info");
147 }
148 }
149
150 check_rpc_cost(m_rpc_payment_state, "get_info", resp_t.credits, pre_call_credits, COST_PER_GET_INFO);
151 }
152
153 m_height = resp_t.height;
154 m_target_height = resp_t.target_height;
155 m_block_weight_limit = resp_t.block_weight_limit ? resp_t.block_weight_limit : resp_t.block_size_limit;
156 m_adjusted_time = resp_t.adjusted_time;
157 m_get_info_time = now;
158 m_height_time = now;
159 }
160 return boost::optional<std::string>();
161 }
162
163 boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height)
164 {
165 auto res = get_info();
166 if (res)
167 return res;
168 height = m_height;
169 return boost::optional<std::string>();
170 }
171
172 boost::optional<std::string> NodeRPCProxy::get_target_height(uint64_t &height)
173 {
174 auto res = get_info();
175 if (res)
176 return res;
177 // A target height that is lower than the block height makes no sense
178 // We adjust it here so consumers don't forget about this
179 height = m_target_height < m_height ? m_height : m_target_height;
180 return boost::optional<std::string>();
181 }
182
183 boost::optional<std::string> NodeRPCProxy::get_block_weight_limit(uint64_t &block_weight_limit)
184 {
185 auto res = get_info();
186 if (res)
187 return res;
188 block_weight_limit = m_block_weight_limit;
189 return boost::optional<std::string>();
190 }
191
192 boost::optional<std::string> NodeRPCProxy::get_adjusted_time(uint64_t &adjusted_time)
193 {
194 auto res = get_info();
195 if (res)
196 return res;
197 adjusted_time = m_adjusted_time;
198 return boost::optional<std::string>();
199 }
200
201 boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version, uint64_t &earliest_height)
202 {
203 if (m_offline)
204 return boost::optional<std::string>("offline");
205 if (m_earliest_height[version] == 0)
206 {
207 cryptonote::COMMAND_RPC_HARD_FORK_INFO::request req_t = AUTO_VAL_INIT(req_t);
208 cryptonote::COMMAND_RPC_HARD_FORK_INFO::response resp_t = AUTO_VAL_INIT(resp_t);
209 req_t.version = version;
210
211 {
212 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
213 uint64_t pre_call_credits = m_rpc_payment_state.credits;
214 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
215 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "hard_fork_info", req_t, resp_t, m_http_client, rpc_timeout);
216 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "hard_fork_info");
217 check_rpc_cost(m_rpc_payment_state, "hard_fork_info", resp_t.credits, pre_call_credits, COST_PER_HARD_FORK_INFO);
218 }
219
220 m_earliest_height[version] = resp_t.earliest_height;
221 }
222
223 earliest_height = m_earliest_height[version];
224 return boost::optional<std::string>();
225 }
226
227 boost::optional<std::string> NodeRPCProxy::get_dynamic_base_fee_estimate_2021_scaling(uint64_t grace_blocks, std::vector<uint64_t> &fees)
228 {
229 uint64_t height;
230
231 boost::optional<std::string> result = get_height(height);
232 if (result)
233 return result;
234
235 if (m_offline)
236 return boost::optional<std::string>("offline");
237 if (m_dynamic_base_fee_estimate_cached_height != height || m_dynamic_base_fee_estimate_grace_blocks != grace_blocks)
238 {
239 cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::request req_t = AUTO_VAL_INIT(req_t);
240 cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::response resp_t = AUTO_VAL_INIT(resp_t);
241 req_t.grace_blocks = grace_blocks;
242
243 {
244 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
245 uint64_t pre_call_credits = m_rpc_payment_state.credits;
246 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
247 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_fee_estimate", req_t, resp_t, m_http_client, rpc_timeout);
248 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "get_fee_estimate");
249 check_rpc_cost(m_rpc_payment_state, "get_fee_estimate", resp_t.credits, pre_call_credits, COST_PER_FEE_ESTIMATE);
250 }
251
252 m_dynamic_base_fee_estimate = resp_t.fee;
253 m_dynamic_base_fee_estimate_cached_height = height;
254 m_dynamic_base_fee_estimate_grace_blocks = grace_blocks;
255 m_dynamic_base_fee_estimate_vector = !resp_t.fees.empty() ? std::move(resp_t.fees) : std::vector<uint64_t>{m_dynamic_base_fee_estimate};
256 m_fee_quantization_mask = resp_t.quantization_mask;
257 }
258
259 fees = m_dynamic_base_fee_estimate_vector;
260 return boost::optional<std::string>();
261 }
262
263 boost::optional<std::string> NodeRPCProxy::get_dynamic_base_fee_estimate(uint64_t grace_blocks, uint64_t &fee)
264 {
265 std::vector<uint64_t> fees;
266 auto res = get_dynamic_base_fee_estimate_2021_scaling(grace_blocks, fees);
267 if (res)
268 return res;
269 fee = fees[0];
270 return boost::none;
271 }
272
273 boost::optional<std::string> NodeRPCProxy::get_fee_quantization_mask(uint64_t &fee_quantization_mask)
274 {
275 uint64_t height;
276
277 boost::optional<std::string> result = get_height(height);
278 if (result)
279 return result;
280
281 if (m_offline)
282 return boost::optional<std::string>("offline");
283 if (m_dynamic_base_fee_estimate_cached_height != height)
284 {
285 cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::request req_t = AUTO_VAL_INIT(req_t);
286 cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::response resp_t = AUTO_VAL_INIT(resp_t);
287 req_t.grace_blocks = m_dynamic_base_fee_estimate_grace_blocks;
288
289 {
290 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
291 uint64_t pre_call_credits = m_rpc_payment_state.credits;
292 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
293 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_fee_estimate", req_t, resp_t, m_http_client, rpc_timeout);
294 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "get_fee_estimate");
295 check_rpc_cost(m_rpc_payment_state, "get_fee_estimate", resp_t.credits, pre_call_credits, COST_PER_FEE_ESTIMATE);
296 }
297
298 m_dynamic_base_fee_estimate = resp_t.fee;
299 m_dynamic_base_fee_estimate_cached_height = height;
300 m_fee_quantization_mask = resp_t.quantization_mask;
301 }
302
303 fee_quantization_mask = m_fee_quantization_mask;
304 if (fee_quantization_mask == 0)
305 {
306 MERROR("Fee quantization mask is 0, forcing to 1");
307 fee_quantization_mask = 1;
308 }
309 return boost::optional<std::string>();
310 }
311
312 boost::optional<std::string> NodeRPCProxy::get_rpc_payment_info(bool mining, bool &payment_required, uint64_t &credits, uint64_t &diff, uint64_t &credits_per_hash_found, cryptonote::blobdata &blob, uint64_t &height, uint64_t &seed_height, crypto::hash &seed_hash, crypto::hash &next_seed_hash, uint32_t &cookie)
313 {
314 const time_t now = time(NULL);
315 if (m_rpc_payment_state.stale || now >= m_rpc_payment_info_time + 5*60 || (mining && now >= m_rpc_payment_info_time + 10)) // re-cache every 10 seconds if mining, 5 minutes otherwise
316 {
317 cryptonote::COMMAND_RPC_ACCESS_INFO::request req_t = AUTO_VAL_INIT(req_t);
318 cryptonote::COMMAND_RPC_ACCESS_INFO::response resp_t = AUTO_VAL_INIT(resp_t);
319
320 {
321 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
322 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
323 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "rpc_access_info", req_t, resp_t, m_http_client, rpc_timeout);
324 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "rpc_access_info");
325 m_rpc_payment_state.stale = false;
326 }
327
328 m_rpc_payment_diff = resp_t.diff;
329 m_rpc_payment_credits_per_hash_found = resp_t.credits_per_hash_found;
330 m_rpc_payment_height = resp_t.height;
331 m_rpc_payment_seed_height = resp_t.seed_height;
332 m_rpc_payment_cookie = resp_t.cookie;
333
334 if (m_rpc_payment_diff == 0)
335 {
336 // If no payment required daemon doesn't give us back a hashing blob
337 m_rpc_payment_blob.clear();
338 }
339 else if (!epee::string_tools::parse_hexstr_to_binbuff(resp_t.hashing_blob, m_rpc_payment_blob) || m_rpc_payment_blob.size() < 43)
340 {
341 MERROR("Invalid hashing blob: " << resp_t.hashing_blob);
342 return std::string("Invalid hashing blob");
343 }
344 if (resp_t.seed_hash.empty())
345 {
346 m_rpc_payment_seed_hash = crypto::null_hash;
347 }
348 else if (!epee::string_tools::hex_to_pod(resp_t.seed_hash, m_rpc_payment_seed_hash))
349 {
350 MERROR("Invalid seed_hash: " << resp_t.seed_hash);
351 return std::string("Invalid seed hash");
352 }
353 if (resp_t.next_seed_hash.empty())
354 {
355 m_rpc_payment_next_seed_hash = crypto::null_hash;
356 }
357 else if (!epee::string_tools::hex_to_pod(resp_t.next_seed_hash, m_rpc_payment_next_seed_hash))
358 {
359 MERROR("Invalid next_seed_hash: " << resp_t.next_seed_hash);
360 return std::string("Invalid next seed hash");
361 }
362 m_rpc_payment_info_time = now;
363 }
364
365 payment_required = m_rpc_payment_diff > 0;
366 credits = m_rpc_payment_state.credits;
367 diff = m_rpc_payment_diff;
368 credits_per_hash_found = m_rpc_payment_credits_per_hash_found;
369 blob = m_rpc_payment_blob;
370 height = m_rpc_payment_height;
371 seed_height = m_rpc_payment_seed_height;
372 seed_hash = m_rpc_payment_seed_hash;
373 next_seed_hash = m_rpc_payment_next_seed_hash;
374 cookie = m_rpc_payment_cookie;
375 return boost::none;
376 }
377
378 boost::optional<std::string> NodeRPCProxy::get_transactions(const std::vector<crypto::hash> &txids, const std::function<void(const cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request&, const cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response&, bool)> &f)
379 {
380 const size_t SLICE_SIZE = 100; // RESTRICTED_TRANSACTIONS_COUNT as defined in rpc/core_rpc_server.cpp
381 for (size_t offset = 0; offset < txids.size(); offset += SLICE_SIZE)
382 {
383 cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req_t = AUTO_VAL_INIT(req_t);
384 cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response resp_t = AUTO_VAL_INIT(resp_t);
385
386 const size_t n_txids = std::min<size_t>(SLICE_SIZE, txids.size() - offset);
387 for (size_t n = offset; n < (offset + n_txids); ++n)
388 req_t.txs_hashes.push_back(epee::string_tools::pod_to_hex(txids[n]));
389 MDEBUG("asking for " << req_t.txs_hashes.size() << " transactions");
390 req_t.decode_as_json = false;
391 req_t.prune = true;
392
393 bool r = false;
394 {
395 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
396 uint64_t pre_call_credits = m_rpc_payment_state.credits;
397 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
398 r = net_utils::invoke_http_json("/gettransactions", req_t, resp_t, m_http_client, rpc_timeout);
399 if (r && resp_t.status == CORE_RPC_STATUS_OK)
400 check_rpc_cost(m_rpc_payment_state, "/gettransactions", resp_t.credits, pre_call_credits, resp_t.txs.size() * COST_PER_TX);
401 }
402
403 f(req_t, resp_t, r);
404 }
405 return boost::optional<std::string>();
406 }
407
408 boost::optional<std::string> NodeRPCProxy::get_block_header_by_height(uint64_t height, cryptonote::block_header_response &block_header)
409 {
410 if (m_offline)
411 return boost::optional<std::string>("offline");
412
413 cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request req_t = AUTO_VAL_INIT(req_t);
414 cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response resp_t = AUTO_VAL_INIT(resp_t);
415 req_t.height = height;
416
417 {
418 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
419 uint64_t pre_call_credits = m_rpc_payment_state.credits;
420 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
421 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "getblockheaderbyheight", req_t, resp_t, m_http_client, rpc_timeout);
422 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "getblockheaderbyheight");
423 check_rpc_cost(m_rpc_payment_state, "getblockheaderbyheight", resp_t.credits, pre_call_credits, COST_PER_BLOCK_HEADER);
424 }
425
426 block_header = std::move(resp_t.block_header);
427 return boost::optional<std::string>();
428 }
429
430 }