-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrpc.cpp
More file actions
478 lines (420 loc) · 17.6 KB
/
Copy pathrpc.cpp
File metadata and controls
478 lines (420 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// SPDX-License-Identifier: AGPL-3.0-or-later
#include "rpc.hpp"
#include <impl/dash/coin/rpc_request.hpp>
#include <core/log.hpp>
#include <core/hash.hpp>
#include <core/core_util.hpp> // core::timestamp() (getwork latency)
#include <util/strencodings.h> // ParseHex / HexStr
namespace dash
{
namespace coin
{
// DASH chain-identity genesis hashes, DASH_MIN_DAEMON_VERSION and
// make_gbt_request: see impl/dash/coin/rpc_request.hpp (oracle/identity SSOT).
// check() probes getblockheader(dash_genesis_hash(IS_TESTNET)) to confirm the
// daemon is a real dashd on the selected network.
NodeRPC::NodeRPC(io::io_context* context, dash::interfaces::Node* coin, bool testnet)
: IS_TESTNET(testnet), m_coin(coin), m_context(context), m_stream(*context),
m_resolver(*context), m_client(*this, RPC_VER)
{
}
void NodeRPC::connect(NetService address, std::string userpass)
{
m_address = address;
m_userpass = userpass;
m_auth = std::make_unique<RPCAuthData>();
m_http_request = {http::verb::post, "/", 11};
m_auth->host = address.to_string();
m_http_request.set(http::field::host, m_auth->host);
m_http_request.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
m_http_request.set(http::field::content_type, "application/json");
m_http_request.set(http::field::connection, "keep-alive");
std::string encoded_login2;
encoded_login2.resize(boost::beast::detail::base64::encoded_size(userpass.size()));
const auto result = boost::beast::detail::base64::encode(&encoded_login2[0], userpass.data(), userpass.size());
encoded_login2.resize(result);
m_auth->authorization = "Basic " + encoded_login2;
m_http_request.set(http::field::authorization, m_auth->authorization);
// Async DNS resolve — must NOT use the blocking m_resolver.resolve() overload
// as that stalls the entire io_context thread for the DNS round-trip.
m_resolver.async_resolve(address.address(), address.port_str(),
[this](boost::system::error_code ec, boost::asio::ip::tcp::resolver::results_type results)
{
if (ec)
{
LOG_ERROR << "CoindRPC DNS resolve failed: " << ec.message() << ", retrying in 15s";
m_reconnect_timer = std::make_unique<core::Timer>(m_context, false);
m_reconnect_timer->start(15, [this]() { connect(m_address, m_userpass); });
return;
}
boost::asio::ip::tcp::endpoint endpoint = *results.begin();
m_stream.async_connect(endpoint,
[this](boost::system::error_code ec)
{
if (ec)
{
if (ec == boost::system::errc::operation_canceled)
return;
LOG_ERROR << "CoindRPC error when try connect: [" << ec.message() << "].";
} else
{
try
{
if (check())
{
m_connected = true;
LOG_INFO << "...CoindRPC connected!";
return;
}
}
catch(const std::runtime_error& ec)
{
LOG_ERROR << "Error when try check CoindRPC: " << ec.what();
}
}
LOG_INFO << "Retry after 15 seconds...";
m_connected = false;
m_stream.close();
m_reconnect_timer = std::make_unique<core::Timer>(m_context, false);
m_reconnect_timer->start(15, [this]() { connect(m_address, m_userpass); });
}
);
});
}
NodeRPC::~NodeRPC()
{
beast::error_code ec;
m_stream.socket().shutdown(io::ip::tcp::socket::shutdown_both, ec);
if (ec)
{
// shutdown errors on close are typically benign; ignore
}
}
void NodeRPC::reconnect()
{
if (!m_connected)
return; // already reconnecting or never connected
m_connected = false;
LOG_WARNING << "RPC connection lost — reconnecting in 15 seconds...";
m_stream.close();
m_reconnect_timer = std::make_unique<core::Timer>(m_context, false);
m_reconnect_timer->start(15, [this]() { connect(m_address, m_userpass); });
}
void NodeRPC::sync_reconnect()
{
beast::error_code ec;
m_stream.socket().shutdown(io::ip::tcp::socket::shutdown_both, ec);
m_stream.close();
// Blocking resolve + connect for immediate retry
auto results = m_resolver.resolve(m_address.address(), m_address.port_str(), ec);
if (ec) {
LOG_WARNING << "CoindRPC sync_reconnect resolve failed: " << ec.message();
return;
}
m_stream.connect(*results.begin(), ec);
if (ec) {
LOG_WARNING << "CoindRPC sync_reconnect connect failed: " << ec.message();
return;
}
LOG_INFO << "CoindRPC reconnected (sync)";
}
std::string NodeRPC::Send(const std::string &request)
{
// Retry once after synchronous reconnect on write/read failure
for (int attempt = 0; attempt < 2; ++attempt)
{
m_http_request.body() = request;
m_http_request.prepare_payload();
try
{
http::write(m_stream, m_http_request);
}
catch(const std::exception& e)
{
LOG_WARNING << "CoindRPC write failed: " << e.what()
<< (attempt == 0 ? " — reconnecting..." : "");
if (attempt == 0) {
sync_reconnect();
continue;
}
return {};
}
beast::flat_buffer buffer;
boost::beast::http::response<boost::beast::http::dynamic_body> response;
try
{
boost::beast::http::read(m_stream, buffer, response);
}
catch (const std::exception& ex)
{
LOG_WARNING << "CoindRPC read failed: " << ex.what()
<< (attempt == 0 ? " — reconnecting..." : "");
if (attempt == 0) {
sync_reconnect();
continue;
}
return {};
}
auto body = boost::beast::buffers_to_string(response.body().data());
if (body.empty()) {
static int _empty_count = 0;
if (_empty_count++ < 5)
LOG_WARNING << "CoindRPC empty response: HTTP " << response.result_int()
<< " content-length=" << response[http::field::content_length]
<< " connection=" << response[http::field::connection];
if (attempt == 0 && response.result_int() != 200) {
sync_reconnect();
continue;
}
}
return body;
}
return {};
}
nlohmann::json NodeRPC::CallAPIMethod(const std::string& method, const jsonrpccxx::positional_parameter& params)
{
return m_client.CallMethod<nlohmann::json>(ID, method, params);
}
bool NodeRPC::check()
{
uint256 genesis = uint256S(dash_genesis_hash(IS_TESTNET));
bool has_block = check_blockheader(genesis);
const std::string chain = getblockchaininfo()["chain"].get<std::string>();
bool is_main_chain = chain == "main";
if (is_main_chain && !has_block)
{
LOG_ERROR << "Check failed! Make sure that you're connected to the right dashd with --dash-rpc-port, and that it has finished syncing!" << std::endl;
return false;
}
// Chain-identity guard (bad-cb-payee root-cause hardening): a net-mode /
// daemon-chain mismatch previously passed this probe silently in BOTH
// wrong directions except mainnet-vs-mainnet-genesis-missing. The lethal
// one is a MAINNET-mode binary against a testnet daemon: every GBT
// masternode payee ('y...') fails base58 decode under mainnet
// address_version=76, the required payee output vanished from the built
// coinbase, and every won block was rejected bad-cb-payee (hex-confirmed
// against dashd getblocktemplate proposal mode). Fail LOUDLY at connect
// instead. --testnet accepts test/regtest/devnet (the tuned-net + G3b
// harness posture) but never "main".
if (!IS_TESTNET && !is_main_chain)
{
LOG_ERROR << "Chain mismatch: dashd reports chain='" << chain
<< "' but c2pool-dash is in MAINNET mode. A mainnet-mode "
"coinbase drops the daemon's masternode payee outputs "
"(bad-cb-payee -- every won block would be lost). "
"Run with --testnet or point --coin-rpc at a mainnet dashd.";
return false;
}
if (IS_TESTNET && is_main_chain)
{
LOG_ERROR << "Chain mismatch: dashd reports chain='main' but "
"c2pool-dash is in TESTNET mode (--testnet). Drop "
"--testnet or point --coin-rpc at a testnet dashd.";
return false;
}
try
{
auto networkinfo = getnetworkinfo();
bool version_check_result = daemon_version_acceptable(networkinfo["version"].get<int>());
if (!version_check_result)
{
LOG_ERROR << "Dash daemon too old! Upgrade!";
return false;
}
} catch (const jsonrpccxx::JsonRpcException& ex)
{
LOG_WARNING << "NodeRPC::check() exception: " << ex.what();
return false;
}
// DASH older-than-v35 baseline has NO segwit and no required-softfork gate
// (params.hpp: softforks_required == {}). Unlike DGB (reservealgo/odo/
// nversionbips), DASH carries no startup-readiness softfork requirement set,
// so the genesis-identity + version-floor probe above is the full check.
return true;
}
bool NodeRPC::check_blockheader(uint256 header)
{
try
{
getblockheader(header);
return true;
} catch (const jsonrpccxx::JsonRpcException& ex)
{
return false;
}
}
DashWorkData NodeRPC::getwork()
{
auto start = core::timestamp();
// DASH: X11, no segwit -> plain rules (no "algo", no injected "segwit").
auto work = getblocktemplate({});
auto end = core::timestamp();
DashWorkData w;
w.m_raw = work;
// ----- Standard Bitcoin-family fields -----
if (work.contains("version")) w.m_version = work["version"].get<int32_t>();
if (work.contains("previousblockhash"))
w.m_previous_block = uint256S(work["previousblockhash"].get<std::string>());
if (work.contains("coinbasevalue")) w.m_coinbase_value = work["coinbasevalue"].get<uint64_t>();
if (work.contains("curtime")) w.m_curtime = work["curtime"].get<uint32_t>();
if (work.contains("mintime")) w.m_mintime = work["mintime"].get<uint32_t>();
if (work.contains("coinbaseaux") && work["coinbaseaux"].contains("flags"))
w.m_coinbase_flags_hex = work["coinbaseaux"]["flags"].get<std::string>();
if (work.contains("bits"))
{
// GBT "bits" is a hex string (compact nBits).
w.m_bits = static_cast<uint32_t>(std::stoul(work["bits"].get<std::string>(), nullptr, 16));
}
// Height: prefer GBT's, else previous block + 1.
if (work.contains("height"))
w.m_height = work["height"].get<uint32_t>();
else if (work.contains("previousblockhash"))
w.m_height = getblock(w.m_previous_block)["height"].get<uint32_t>() + 1;
// ----- Transactions (full parse + raw hex + txid + fees) -----
if (work.contains("transactions"))
{
for (auto& packed_tx : work["transactions"])
{
std::string data_hex;
if (packed_tx.is_object() && packed_tx.contains("data"))
data_hex = packed_tx["data"].get<std::string>();
else if (packed_tx.is_string())
data_hex = packed_tx.get<std::string>();
if (data_hex.empty())
continue;
w.m_tx_data_hex.push_back(data_hex);
// Deserialize into a full MutableTransaction (DASH tx codec handles
// the version|(type<<16) field + DIP3/DIP4 special-tx payload).
MutableTransaction mtx;
try
{
PackStream ps_tx(ParseHex(data_hex));
ps_tx >> mtx;
}
catch (const std::exception& ex)
{
LOG_WARNING << "getwork: tx deserialize failed: " << ex.what();
}
w.m_txs.emplace_back(mtx); // Transaction(MutableTransaction) is explicit
// txid: prefer GBT's "txid" field, else recompute (DASH non-witness
// canonical serialization == dash_txid).
uint256 txid;
if (packed_tx.is_object() && packed_tx.contains("txid"))
txid = uint256S(packed_tx["txid"].get<std::string>());
else
{
PackStream ps_id(ParseHex(data_hex));
txid = Hash(ps_id.get_span());
}
w.m_tx_hashes.push_back(txid);
uint64_t fee = 0;
if (packed_tx.is_object() && packed_tx.contains("fee"))
fee = packed_tx["fee"].get<uint64_t>();
w.m_tx_fees.push_back(fee);
}
}
// ----- DASH masternode + superblock + platform payments -----
// Normalize into m_packed_payments in the EXACT coinbase-output order, using
// the same "!hex" raw-script / base58-address payee convention that
// coin/embedded_gbt.hpp::gbt_xcheck compares the embedded build against.
//
// dashd GBT carries these as either:
// - the v0.13+ "masternode" array (objects {payee, script, amount}) and
// "superblock" array (same shape), and/or
// - the legacy single-object "masternode"/"payee"+"payee_amount" fields.
// We accept both shapes. Platform credit-pool OP_RETURN burns surface as a
// payee with an empty/"6a" script -> normalized to "!6a".
auto push_payment = [&w](const nlohmann::json& entry) {
// bad-cb-payee fix: empty payee strings normalize to the raw "!"+script
// form (see rpc_data.hpp::normalize_payment) instead of being dropped.
PackedPayment pp = normalize_payment(entry);
if (pp.amount == 0)
return;
w.m_packed_payments.push_back(std::move(pp));
w.m_payment_amount += w.m_packed_payments.back().amount;
};
// Platform credit-pool OP_RETURN burn FIRST (dashcore GetBlockTxOuts order).
if (work.contains("coinbase_payload_burn"))
{
PackedPayment burn;
burn.payee = "!6a";
burn.amount = work["coinbase_payload_burn"].get<uint64_t>();
if (burn.amount > 0)
{
w.m_packed_payments.push_back(std::move(burn));
w.m_payment_amount += w.m_packed_payments.back().amount;
}
}
if (work.contains("masternode"))
{
if (work["masternode"].is_array())
for (auto& e : work["masternode"]) push_payment(e);
else if (work["masternode"].is_object())
push_payment(work["masternode"]);
}
if (work.contains("superblock") && work["superblock"].is_array())
for (auto& e : work["superblock"]) push_payment(e);
// ----- DIP3/DIP4 coinbase extra payload -----
if (work.contains("coinbase_payload") && work["coinbase_payload"].is_string())
{
auto payload = ParseHex(work["coinbase_payload"].get<std::string>());
w.m_coinbase_payload.assign(payload.begin(), payload.end());
}
w.m_latency = end - start;
return w;
}
void NodeRPC::submit_block(BlockType& block, bool ignore_failure)
{
// DASH is non-segwit, non-MWEB: full-block packing is the plain block codec.
// NOTE: BlockType transaction-aware (de)serialization of m_txs is the S5
// deferral (coin/block.hpp); until that lands, the hex submit arm
// (submit_block_hex) carrying a fully-assembled block is the live won-block
// RPC path. This typed overload packs the header form.
PackStream packed_block = pack<dash::coin::BlockType>(block);
auto result = m_client.CallMethod<nlohmann::json>(ID, "submitblock", {HexStr(packed_block.get_span())});
bool success = result.is_null();
auto success_expected = true;
if ((!success && success_expected && !ignore_failure) || (success && !success_expected))
LOG_ERROR << "Block submittal result: " << success << "(" << result.dump() << ") Expected: " << success_expected;
}
bool NodeRPC::submit_block_hex(const std::string& block_hex, bool ignore_failure)
{
auto result = m_client.CallMethod<nlohmann::json>(ID, "submitblock", {block_hex});
bool success = result.is_null();
if (!success && !ignore_failure)
LOG_ERROR << "submit_block_hex result: " << result.dump();
else if (success)
LOG_INFO << "submit_block_hex accepted";
return success;
}
// RPC Methods
nlohmann::json NodeRPC::getblocktemplate(std::vector<std::string> rules)
{
// Body shape (plain rules, NO algo, NO injected segwit) is the
// rpc_request.hpp SSOT -- the key DASH<->DGB divergence.
return CallAPIMethod("getblocktemplate", {make_gbt_request(rules)});
}
nlohmann::json NodeRPC::getnetworkinfo()
{
return CallAPIMethod("getnetworkinfo");
}
nlohmann::json NodeRPC::getblockchaininfo()
{
return CallAPIMethod("getblockchaininfo");
}
nlohmann::json NodeRPC::getmininginfo()
{
return CallAPIMethod("getmininginfo");
}
// verbose: true -- json result, false -- hex-encode result;
nlohmann::json NodeRPC::getblockheader(uint256 header, bool verbose)
{
return CallAPIMethod("getblockheader", {header, verbose});
}
// verbosity: 0 for hex-encoded data, 1 for a json object, and 2 for json object with transaction data
nlohmann::json NodeRPC::getblock(uint256 blockhash, int verbosity)
{
return CallAPIMethod("getblock", {blockhash, verbosity});
}
} // namespace coin
} // namespace dash