Debian Codegraph @

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::minutes(3) + std::chrono::seconds(30);
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_target_height_time = 0;
86 m_rpc_payment_diff = 0;
87 m_rpc_payment_credits_per_hash_found = 0;
88 m_rpc_payment_height = 0;
89 m_rpc_payment_cookie = 0;
90 m_daemon_hard_forks.clear();
91 }
92
93 boost::optional<std::string> NodeRPCProxy::get_rpc_version(uint32_t &rpc_version, std::vector<std::pair<uint8_t, uint64_t>> &daemon_hard_forks, uint64_t &height, uint64_t &target_height)
94 {
95 if (m_offline)
96 return boost::optional<std::string>("offline");
97 if (m_rpc_version == 0)
98 {
99 const time_t now = time(NULL);
100 cryptonote::COMMAND_RPC_GET_VERSION::request req_t = AUTO_VAL_INIT(req_t);
101 cryptonote::COMMAND_RPC_GET_VERSION::response resp_t = AUTO_VAL_INIT(resp_t);
102 {
103 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
104 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_version", req_t, resp_t, m_http_client, rpc_timeout);
105 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "get_version");
106 }
107
108 m_rpc_version = resp_t.version;
109 m_daemon_hard_forks.clear();
110 for (const auto &hf : resp_t.hard_forks)
111 m_daemon_hard_forks.push_back(std::make_pair(hf.hf_version, hf.height));
112 if (resp_t.current_height > 0 || resp_t.target_height > 0)
113 {
114 m_height = resp_t.current_height;
115 m_target_height = resp_t.target_height;
116 m_height_time = now;
117 m_target_height_time = now;
118 }
119 }
120
121 rpc_version = m_rpc_version;
122 daemon_hard_forks = m_daemon_hard_forks;
123 boost::optional<std::string> result = get_height(height);
124 if (result)
125 return result;
126 result = get_target_height(target_height);
127 if (result)
128 return result;
129 return boost::optional<std::string>();
130 }
131
132 void NodeRPCProxy::set_height(uint64_t h)
133 {
134 m_height = h;
135 m_height_time = time(NULL);
136 }
137
138 boost::optional<std::string> NodeRPCProxy::get_info()
139 {
140 if (m_offline)
141 return boost::optional<std::string>("offline");
142 const time_t now = time(NULL);
143 if (now >= m_get_info_time + 30) // re-cache every 30 seconds
144 {
145 cryptonote::COMMAND_RPC_GET_INFO::request req_t = AUTO_VAL_INIT(req_t);
146 cryptonote::COMMAND_RPC_GET_INFO::response resp_t = AUTO_VAL_INIT(resp_t);
147
148 {
149 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
150 uint64_t pre_call_credits = m_rpc_payment_state.credits;
151 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
152 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_info", req_t, resp_t, m_http_client, rpc_timeout);
153 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "get_info");
154 check_rpc_cost(m_rpc_payment_state, "get_info", resp_t.credits, pre_call_credits, COST_PER_GET_INFO);
155 }
156
157 m_height = resp_t.height;
158 m_target_height = resp_t.target_height;
159 m_block_weight_limit = resp_t.block_weight_limit ? resp_t.block_weight_limit : resp_t.block_size_limit;
160 m_adjusted_time = resp_t.adjusted_time;
161 m_get_info_time = now;
162 m_height_time = now;
163 m_target_height_time = now;
164 }
165 return boost::optional<std::string>();
166 }
167
168 boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height)
169 {
170 const time_t now = time(NULL);
171 if (now < m_height_time + 30) // re-cache every 30 seconds
172 {
173 height = m_height;
174 return boost::optional<std::string>();
175 }
176
177 auto res = get_info();
178 if (res)
179 return res;
180 height = m_height;
181 return boost::optional<std::string>();
182 }
183
184 boost::optional<std::string> NodeRPCProxy::get_target_height(uint64_t &height)
185 {
186 const time_t now = time(NULL);
187 if (now < m_target_height_time + 30) // re-cache every 30 seconds
188 {
189 height = m_target_height;
190 return boost::optional<std::string>();
191 }
192
193 auto res = get_info();
194 if (res)
195 return res;
196 height = m_target_height;
197 return boost::optional<std::string>();
198 }
199
200 boost::optional<std::string> NodeRPCProxy::get_block_weight_limit(uint64_t &block_weight_limit)
201 {
202 auto res = get_info();
203 if (res)
204 return res;
205 block_weight_limit = m_block_weight_limit;
206 return boost::optional<std::string>();
207 }
208
209 boost::optional<std::string> NodeRPCProxy::get_adjusted_time(uint64_t &adjusted_time)
210 {
211 auto res = get_info();
212 if (res)
213 return res;
214 adjusted_time = m_adjusted_time;
215 return boost::optional<std::string>();
216 }
217
218 boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version, uint64_t &earliest_height)
219 {
220 if (m_offline)
221 return boost::optional<std::string>("offline");
222 if (m_earliest_height[version] == 0)
223 {
224 cryptonote::COMMAND_RPC_HARD_FORK_INFO::request req_t = AUTO_VAL_INIT(req_t);
225 cryptonote::COMMAND_RPC_HARD_FORK_INFO::response resp_t = AUTO_VAL_INIT(resp_t);
226 req_t.version = version;
227
228 {
229 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
230 uint64_t pre_call_credits = m_rpc_payment_state.credits;
231 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
232 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "hard_fork_info", req_t, resp_t, m_http_client, rpc_timeout);
233 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "hard_fork_info");
234 check_rpc_cost(m_rpc_payment_state, "hard_fork_info", resp_t.credits, pre_call_credits, COST_PER_HARD_FORK_INFO);
235 }
236
237 m_earliest_height[version] = resp_t.earliest_height;
238 }
239
240 earliest_height = m_earliest_height[version];
241 return boost::optional<std::string>();
242 }
243
244 boost::optional<std::string> NodeRPCProxy::get_dynamic_base_fee_estimate_2021_scaling(uint64_t grace_blocks, std::vector<uint64_t> &fees)
245 {
246 uint64_t height;
247
248 boost::optional<std::string> result = get_height(height);
249 if (result)
250 return result;
251
252 if (m_offline)
253 return boost::optional<std::string>("offline");
254 if (m_dynamic_base_fee_estimate_cached_height != height || m_dynamic_base_fee_estimate_grace_blocks != grace_blocks)
255 {
256 cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::request req_t = AUTO_VAL_INIT(req_t);
257 cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::response resp_t = AUTO_VAL_INIT(resp_t);
258 req_t.grace_blocks = grace_blocks;
259
260 {
261 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
262 uint64_t pre_call_credits = m_rpc_payment_state.credits;
263 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
264 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_fee_estimate", req_t, resp_t, m_http_client, rpc_timeout);
265 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "get_fee_estimate");
266 check_rpc_cost(m_rpc_payment_state, "get_fee_estimate", resp_t.credits, pre_call_credits, COST_PER_FEE_ESTIMATE);
267 }
268
269 m_dynamic_base_fee_estimate = resp_t.fee;
270 m_dynamic_base_fee_estimate_cached_height = height;
271 m_dynamic_base_fee_estimate_grace_blocks = grace_blocks;
272 m_dynamic_base_fee_estimate_vector = !resp_t.fees.empty() ? std::move(resp_t.fees) : std::vector<uint64_t>{m_dynamic_base_fee_estimate};
273 m_fee_quantization_mask = resp_t.quantization_mask;
274 }
275
276 fees = m_dynamic_base_fee_estimate_vector;
277 return boost::optional<std::string>();
278 }
279
280 boost::optional<std::string> NodeRPCProxy::get_dynamic_base_fee_estimate(uint64_t grace_blocks, uint64_t &fee)
281 {
282 std::vector<uint64_t> fees;
283 auto res = get_dynamic_base_fee_estimate_2021_scaling(grace_blocks, fees);
284 if (res)
285 return res;
286 fee = fees[0];
287 return boost::none;
288 }
289
290 boost::optional<std::string> NodeRPCProxy::get_fee_quantization_mask(uint64_t &fee_quantization_mask)
291 {
292 uint64_t height;
293
294 boost::optional<std::string> result = get_height(height);
295 if (result)
296 return result;
297
298 if (m_offline)
299 return boost::optional<std::string>("offline");
300 if (m_dynamic_base_fee_estimate_cached_height != height)
301 {
302 cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::request req_t = AUTO_VAL_INIT(req_t);
303 cryptonote::COMMAND_RPC_GET_BASE_FEE_ESTIMATE::response resp_t = AUTO_VAL_INIT(resp_t);
304 req_t.grace_blocks = m_dynamic_base_fee_estimate_grace_blocks;
305
306 {
307 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
308 uint64_t pre_call_credits = m_rpc_payment_state.credits;
309 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
310 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_fee_estimate", req_t, resp_t, m_http_client, rpc_timeout);
311 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "get_fee_estimate");
312 check_rpc_cost(m_rpc_payment_state, "get_fee_estimate", resp_t.credits, pre_call_credits, COST_PER_FEE_ESTIMATE);
313 }
314
315 m_dynamic_base_fee_estimate = resp_t.fee;
316 m_dynamic_base_fee_estimate_cached_height = height;
317 m_fee_quantization_mask = resp_t.quantization_mask;
318 }
319
320 fee_quantization_mask = m_fee_quantization_mask;
321 if (fee_quantization_mask == 0)
322 {
323 MERROR("Fee quantization mask is 0, forcing to 1");
324 fee_quantization_mask = 1;
325 }
326 return boost::optional<std::string>();
327 }
328
329 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)
330 {
331 const time_t now = time(NULL);
332 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
333 {
334 cryptonote::COMMAND_RPC_ACCESS_INFO::request req_t = AUTO_VAL_INIT(req_t);
335 cryptonote::COMMAND_RPC_ACCESS_INFO::response resp_t = AUTO_VAL_INIT(resp_t);
336
337 {
338 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
339 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
340 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "rpc_access_info", req_t, resp_t, m_http_client, rpc_timeout);
341 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "rpc_access_info");
342 m_rpc_payment_state.stale = false;
343 }
344
345 m_rpc_payment_diff = resp_t.diff;
346 m_rpc_payment_credits_per_hash_found = resp_t.credits_per_hash_found;
347 m_rpc_payment_height = resp_t.height;
348 m_rpc_payment_seed_height = resp_t.seed_height;
349 m_rpc_payment_cookie = resp_t.cookie;
350
351 if (m_rpc_payment_diff == 0)
352 {
353 // If no payment required daemon doesn't give us back a hashing blob
354 m_rpc_payment_blob.clear();
355 }
356 else if (!epee::string_tools::parse_hexstr_to_binbuff(resp_t.hashing_blob, m_rpc_payment_blob) || m_rpc_payment_blob.size() < 43)
357 {
358 MERROR("Invalid hashing blob: " << resp_t.hashing_blob);
359 return std::string("Invalid hashing blob");
360 }
361 if (resp_t.seed_hash.empty())
362 {
363 m_rpc_payment_seed_hash = crypto::null_hash;
364 }
365 else if (!epee::string_tools::hex_to_pod(resp_t.seed_hash, m_rpc_payment_seed_hash))
366 {
367 MERROR("Invalid seed_hash: " << resp_t.seed_hash);
368 return std::string("Invalid seed hash");
369 }
370 if (resp_t.next_seed_hash.empty())
371 {
372 m_rpc_payment_next_seed_hash = crypto::null_hash;
373 }
374 else if (!epee::string_tools::hex_to_pod(resp_t.next_seed_hash, m_rpc_payment_next_seed_hash))
375 {
376 MERROR("Invalid next_seed_hash: " << resp_t.next_seed_hash);
377 return std::string("Invalid next seed hash");
378 }
379 m_rpc_payment_info_time = now;
380 }
381
382 payment_required = m_rpc_payment_diff > 0;
383 credits = m_rpc_payment_state.credits;
384 diff = m_rpc_payment_diff;
385 credits_per_hash_found = m_rpc_payment_credits_per_hash_found;
386 blob = m_rpc_payment_blob;
387 height = m_rpc_payment_height;
388 seed_height = m_rpc_payment_seed_height;
389 seed_hash = m_rpc_payment_seed_hash;
390 next_seed_hash = m_rpc_payment_next_seed_hash;
391 cookie = m_rpc_payment_cookie;
392 return boost::none;
393 }
394
395 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)
396 {
397 const size_t SLICE_SIZE = 100; // RESTRICTED_TRANSACTIONS_COUNT as defined in rpc/core_rpc_server.cpp
398 for (size_t offset = 0; offset < txids.size(); offset += SLICE_SIZE)
399 {
400 cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req_t = AUTO_VAL_INIT(req_t);
401 cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response resp_t = AUTO_VAL_INIT(resp_t);
402
403 const size_t n_txids = std::min<size_t>(SLICE_SIZE, txids.size() - offset);
404 for (size_t n = offset; n < (offset + n_txids); ++n)
405 req_t.txs_hashes.push_back(epee::string_tools::pod_to_hex(txids[n]));
406 MDEBUG("asking for " << req_t.txs_hashes.size() << " transactions");
407 req_t.decode_as_json = false;
408 req_t.prune = true;
409
410 bool r = false;
411 {
412 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
413 uint64_t pre_call_credits = m_rpc_payment_state.credits;
414 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
415 r = net_utils::invoke_http_json("/gettransactions", req_t, resp_t, m_http_client, rpc_timeout);
416 if (r && resp_t.status == CORE_RPC_STATUS_OK)
417 check_rpc_cost(m_rpc_payment_state, "/gettransactions", resp_t.credits, pre_call_credits, resp_t.txs.size() * COST_PER_TX);
418 }
419
420 f(req_t, resp_t, r);
421 }
422 return boost::optional<std::string>();
423 }
424
425 boost::optional<std::string> NodeRPCProxy::get_block_header_by_height(uint64_t height, cryptonote::block_header_response &block_header)
426 {
427 if (m_offline)
428 return boost::optional<std::string>("offline");
429
430 cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request req_t = AUTO_VAL_INIT(req_t);
431 cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response resp_t = AUTO_VAL_INIT(resp_t);
432 req_t.height = height;
433
434 {
435 const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
436 uint64_t pre_call_credits = m_rpc_payment_state.credits;
437 req_t.client = cryptonote::make_rpc_payment_signature(m_client_id_secret_key);
438 bool r = net_utils::invoke_http_json_rpc("/json_rpc", "getblockheaderbyheight", req_t, resp_t, m_http_client, rpc_timeout);
439 RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "getblockheaderbyheight");
440 check_rpc_cost(m_rpc_payment_state, "getblockheaderbyheight", resp_t.credits, pre_call_credits, COST_PER_BLOCK_HEADER);
441 }
442
443 block_header = std::move(resp_t.block_header);
444 return boost::optional<std::string>();
445 }
446
447 }